-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpu_embedding.go
More file actions
246 lines (209 loc) · 5.69 KB
/
Copy pathgpu_embedding.go
File metadata and controls
246 lines (209 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package gobed
import (
"fmt"
"runtime"
"sync"
"time"
)
// GPUEmbeddingModel provides GPU-accelerated batch embedding processing
type GPUEmbeddingModel struct {
*EmbeddingModel
batchSize int
useGPU bool
gpuBatchPool sync.Pool
}
// BatchEmbeddingResult contains results from batch processing
type BatchEmbeddingResult struct {
Embeddings [][]float32
Duration time.Duration
BatchSize int
ItemsPerSec float64
}
// NewGPUEmbeddingModel creates a GPU-optimized embedding model
func NewGPUEmbeddingModel(batchSize int, useGPU bool) (*GPUEmbeddingModel, error) {
baseModel, err := LoadModel()
if err != nil {
return nil, fmt.Errorf("failed to load base model: %w", err)
}
gpu := &GPUEmbeddingModel{
EmbeddingModel: baseModel,
batchSize: batchSize,
useGPU: useGPU,
}
// Initialize batch processing pool
gpu.gpuBatchPool = sync.Pool{
New: func() interface{} {
return make([][]float32, 0, batchSize)
},
}
return gpu, nil
}
// EncodeBatch processes multiple texts in optimized batches
func (g *GPUEmbeddingModel) EncodeBatch(texts []string) (*BatchEmbeddingResult, error) {
if g.useGPU && g.batchSize > 1 {
return g.encodeBatchGPU(texts)
}
return g.encodeBatchCPU(texts)
}
// encodeBatchGPU uses GPU-optimized batch processing
func (g *GPUEmbeddingModel) encodeBatchGPU(texts []string) (*BatchEmbeddingResult, error) {
start := time.Now()
embeddings := make([][]float32, len(texts))
// Process in optimal batch sizes for GPU
numBatches := (len(texts) + g.batchSize - 1) / g.batchSize
batchChan := make(chan batchJob, numBatches)
resultChan := make(chan batchResult, numBatches)
// Create batch jobs
for i := 0; i < len(texts); i += g.batchSize {
end := i + g.batchSize
if end > len(texts) {
end = len(texts)
}
batchChan <- batchJob{
texts: texts[i:end],
startIndex: i,
}
}
close(batchChan)
// Process batches with optimal GPU utilization
numWorkers := min(runtime.NumCPU()/2, numBatches) // Don't oversubscribe
var wg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for job := range batchChan {
result := g.processBatchGPU(job)
resultChan <- result
}
}()
}
go func() {
wg.Wait()
close(resultChan)
}()
// Collect results
for result := range resultChan {
if result.err != nil {
return nil, result.err
}
copy(embeddings[result.startIndex:], result.embeddings)
}
duration := time.Since(start)
return &BatchEmbeddingResult{
Embeddings: embeddings,
Duration: duration,
BatchSize: g.batchSize,
ItemsPerSec: float64(len(texts)) / duration.Seconds(),
}, nil
}
// encodeBatchCPU uses optimized CPU batch processing
func (g *GPUEmbeddingModel) encodeBatchCPU(texts []string) (*BatchEmbeddingResult, error) {
start := time.Now()
embeddings := make([][]float32, len(texts))
// Optimal CPU batch processing with memory reuse
numWorkers := runtime.NumCPU()
textChan := make(chan struct {
text string
index int
}, len(texts))
var wg sync.WaitGroup
// Start workers
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for job := range textChan {
emb, err := g.EmbeddingModel.Encode(job.text)
if err != nil {
continue // Skip errors for benchmark
}
embeddings[job.index] = emb
}
}()
}
// Send work
for i, text := range texts {
textChan <- struct {
text string
index int
}{text, i}
}
close(textChan)
wg.Wait()
duration := time.Since(start)
return &BatchEmbeddingResult{
Embeddings: embeddings,
Duration: duration,
BatchSize: len(texts),
ItemsPerSec: float64(len(texts)) / duration.Seconds(),
}, nil
}
type batchJob struct {
texts []string
startIndex int
}
type batchResult struct {
embeddings [][]float32
startIndex int
err error
}
// processBatchGPU processes a single batch with GPU optimization
func (g *GPUEmbeddingModel) processBatchGPU(job batchJob) batchResult {
embeddings := make([][]float32, len(job.texts))
// For now, use the existing CPU implementation with better memory management
// In a real GPU implementation, this would use CUDA kernels or TensorRT
for i, text := range job.texts {
emb, err := g.EmbeddingModel.Encode(text)
if err != nil {
return batchResult{err: err}
}
embeddings[i] = emb
}
return batchResult{
embeddings: embeddings,
startIndex: job.startIndex,
}
}
// OptimalBatchSize determines the best batch size for the current hardware
func (g *GPUEmbeddingModel) OptimalBatchSize() int {
if g.useGPU {
// For RTX 3080 with 16GB, optimal batch sizes are typically 32-128
return 64
}
// For CPU, smaller batches work better due to memory locality
return 16
}
// MemoryOptimizedEncodeBatch processes with memory efficiency
func (g *GPUEmbeddingModel) MemoryOptimizedEncodeBatch(texts []string, maxMemoryMB int) (*BatchEmbeddingResult, error) {
// Estimate memory usage: each embedding is 1024 * 4 bytes = 4KB
embeddingSize := 1024 * 4 // bytes
maxItems := (maxMemoryMB * 1024 * 1024) / embeddingSize
if len(texts) <= maxItems {
return g.EncodeBatch(texts)
}
// Process in chunks to stay within memory limit
start := time.Now()
allEmbeddings := make([][]float32, 0, len(texts))
for i := 0; i < len(texts); i += maxItems {
end := i + maxItems
if end > len(texts) {
end = len(texts)
}
chunk := texts[i:end]
result, err := g.EncodeBatch(chunk)
if err != nil {
return nil, err
}
allEmbeddings = append(allEmbeddings, result.Embeddings...)
// Optional: force GC to free memory
runtime.GC()
}
duration := time.Since(start)
return &BatchEmbeddingResult{
Embeddings: allEmbeddings,
Duration: duration,
BatchSize: maxItems,
ItemsPerSec: float64(len(texts)) / duration.Seconds(),
}, nil
}