-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpu_batch_processor.go
More file actions
358 lines (301 loc) · 8.79 KB
/
gpu_batch_processor.go
File metadata and controls
358 lines (301 loc) · 8.79 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// +build gpu
package gobed
import (
"log"
"runtime"
"sync"
"time"
)
// GPUBatchProcessor handles parallel batch processing of embeddings on GPU
type GPUBatchProcessor struct {
cache *TokenPatternCache
model *EmbeddingModel
batchSize int
maxConcurrent int
// GPU memory management
gpuBuffers []GPUBuffer
bufferPool sync.Pool
// Metrics
processedBatches uint64
totalLatency time.Duration
mu sync.RWMutex
}
// GPUBuffer represents a GPU memory buffer for batch processing
type GPUBuffer struct {
TokenIDs [][]int // Batch of token sequences
Embeddings [][]float32 // Result embeddings
Quantized [][]int8 // Quantized results
Scales []float32 // Quantization scales
InUse bool
}
// NewGPUBatchProcessor creates a GPU-optimized batch processor
func NewGPUBatchProcessor(model *EmbeddingModel, cache *TokenPatternCache) *GPUBatchProcessor {
numCPU := runtime.NumCPU()
// Use dynamic batch sizing based on available memory
batchSize := GetOptimalGPUBatchSize()
processor := &GPUBatchProcessor{
cache: cache,
model: model,
batchSize: batchSize,
maxConcurrent: numCPU * 2, // Allow 2x CPU count for GPU overlap
gpuBuffers: make([]GPUBuffer, numCPU*2),
}
// Initialize buffer pool
processor.bufferPool = sync.Pool{
New: func() interface{} {
return &GPUBuffer{
TokenIDs: make([][]int, batchSize),
Embeddings: make([][]float32, batchSize),
Quantized: make([][]int8, batchSize),
Scales: make([]float32, batchSize),
}
},
}
// Pre-allocate buffers
for i := range processor.gpuBuffers {
processor.gpuBuffers[i] = GPUBuffer{
TokenIDs: make([][]int, batchSize),
Embeddings: make([][]float32, batchSize),
Quantized: make([][]int8, batchSize),
Scales: make([]float32, batchSize),
}
}
log.Printf("GPU Batch Processor initialized: batch_size=%d, max_concurrent=%d",
batchSize, processor.maxConcurrent)
return processor
}
// ProcessBatch processes a batch of texts in parallel using GPU
func (p *GPUBatchProcessor) ProcessBatch(texts []string) ([]*EmbedInt8Result, error) {
startTime := time.Now()
defer func() {
p.mu.Lock()
p.processedBatches++
p.totalLatency += time.Since(startTime)
p.mu.Unlock()
}()
numTexts := len(texts)
results := make([]*EmbedInt8Result, numTexts)
// Stage 1: Tokenize all texts in parallel
tokenBatches := make([][]int, numTexts)
var tokenizeWg sync.WaitGroup
for i := 0; i < numTexts; i += p.batchSize {
end := i + p.batchSize
if end > numTexts {
end = numTexts
}
tokenizeWg.Add(1)
go func(start, end int) {
defer tokenizeWg.Done()
for j := start; j < end; j++ {
text := normalizeText(texts[j])
if text == "" {
tokenBatches[j] = []int{}
continue
}
encoding, err := p.model.tokenizer.EncodeSingle(text, false)
if err != nil {
tokenBatches[j] = []int{}
continue
}
// Use buffer pool
tokens := GetTokenBuffer()
for _, id := range encoding.Ids {
tokens = append(tokens, int(id))
}
// Apply stopword filtering if needed
if p.cache != nil && len(tokens) > 15 {
tokens = p.cache.FilterStopwords(tokens, len(text))
}
tokenBatches[j] = tokens
}
}(i, end)
}
tokenizeWg.Wait()
// Stage 2: Check cache for all patterns in parallel
cachedEmbeddings, cacheHits := p.cache.BatchGetEmbeddings(tokenBatches)
// Stage 3: Process uncached items in GPU batches
uncachedIndices := make([]int, 0, numTexts)
for i, hit := range cacheHits {
if !hit {
uncachedIndices = append(uncachedIndices, i)
} else {
// Use cached result
results[i] = &EmbedInt8Result{
Vector: cachedEmbeddings[i].VectorI8,
Scale: cachedEmbeddings[i].Scale,
}
}
}
if len(uncachedIndices) > 0 {
// Process uncached items on GPU
if err := p.processOnGPU(tokenBatches, uncachedIndices, results); err != nil {
// Fallback to CPU processing
return p.fallbackToCPU(tokenBatches, uncachedIndices, results)
}
}
log.Printf("Processed batch of %d texts: %d cache hits, %d GPU processed, latency: %v",
numTexts, numTexts-len(uncachedIndices), len(uncachedIndices), time.Since(startTime))
return results, nil
}
// processOnGPU sends tokens to GPU for embedding computation
func (p *GPUBatchProcessor) processOnGPU(tokenBatches [][]int, indices []int, results []*EmbedInt8Result) error {
// Get a free GPU buffer
buffer := p.getBuffer()
defer p.releaseBuffer(buffer)
// Process in chunks that fit GPU memory
for i := 0; i < len(indices); i += p.batchSize {
end := i + p.batchSize
if end > len(indices) {
end = len(indices)
}
batchIndices := indices[i:end]
// Prepare batch for GPU
for j, idx := range batchIndices {
buffer.TokenIDs[j] = tokenBatches[idx]
}
// GPU computation would happen here
// For now, simulate with parallel CPU processing
var wg sync.WaitGroup
for j, idx := range batchIndices {
wg.Add(1)
go func(j, idx int) {
defer wg.Done()
embedding, err := p.model.computeEmbedding(buffer.TokenIDs[j])
if err != nil {
results[idx] = &EmbedInt8Result{
Vector: make([]int8, p.model.EmbedDim),
Scale: 1.0,
}
return
}
quantized, scale := quantizeEmbedding(embedding)
results[idx] = &EmbedInt8Result{
Vector: quantized,
Scale: scale,
}
// Update cache with new embedding
if p.cache != nil && len(buffer.TokenIDs[j]) <= 4 {
p.updateCache(buffer.TokenIDs[j], embedding, quantized, scale)
}
}(j, idx)
}
wg.Wait()
}
return nil
}
// fallbackToCPU processes embeddings on CPU when GPU is unavailable
func (p *GPUBatchProcessor) fallbackToCPU(tokenBatches [][]int, indices []int, results []*EmbedInt8Result) ([]*EmbedInt8Result, error) {
var wg sync.WaitGroup
for _, idx := range indices {
wg.Add(1)
go func(idx int) {
defer wg.Done()
embedding, err := p.model.computeEmbedding(tokenBatches[idx])
if err != nil {
results[idx] = &EmbedInt8Result{
Vector: make([]int8, p.model.EmbedDim),
Scale: 1.0,
}
return
}
quantized, scale := quantizeEmbedding(embedding)
results[idx] = &EmbedInt8Result{
Vector: quantized,
Scale: scale,
}
}(idx)
}
wg.Wait()
return results, nil
}
// updateCache adds newly computed embeddings to cache
func (p *GPUBatchProcessor) updateCache(tokens []int, embedding []float32, quantized []int8, scale float32) {
if p.cache == nil {
return
}
p.cache.mu.Lock()
defer p.cache.mu.Unlock()
key := makePatternKey(tokens)
cached := &CachedEmbedding{
Vector: embedding,
VectorI8: quantized,
Scale: scale,
UseCount: 1,
LastUsed: time.Now().Unix(),
}
switch len(tokens) {
case 1:
p.cache.singleTokens[tokens[0]] = cached
case 2:
p.cache.bigrams[key] = cached
case 3:
p.cache.trigrams[key] = cached
case 4:
p.cache.fourgrams[key] = cached
}
}
// getBuffer gets a free GPU buffer
func (p *GPUBatchProcessor) getBuffer() *GPUBuffer {
// Try to get from pool first
if buffer := p.bufferPool.Get(); buffer != nil {
return buffer.(*GPUBuffer)
}
// Find a free buffer
for i := range p.gpuBuffers {
if !p.gpuBuffers[i].InUse {
p.gpuBuffers[i].InUse = true
return &p.gpuBuffers[i]
}
}
// All buffers in use, create a new one
return &GPUBuffer{
TokenIDs: make([][]int, p.batchSize),
Embeddings: make([][]float32, p.batchSize),
Quantized: make([][]int8, p.batchSize),
Scales: make([]float32, p.batchSize),
InUse: true,
}
}
// releaseBuffer returns a buffer to the pool
func (p *GPUBatchProcessor) releaseBuffer(buffer *GPUBuffer) {
buffer.InUse = false
p.bufferPool.Put(buffer)
}
// GetStats returns processing statistics
func (p *GPUBatchProcessor) GetStats() map[string]interface{} {
p.mu.RLock()
defer p.mu.RUnlock()
avgLatency := time.Duration(0)
if p.processedBatches > 0 {
avgLatency = p.totalLatency / time.Duration(p.processedBatches)
}
cacheStats := map[string]interface{}{}
if p.cache != nil {
cacheStats = p.cache.GetStats()
}
return map[string]interface{}{
"processed_batches": p.processedBatches,
"avg_latency_ms": avgLatency.Milliseconds(),
"batch_size": p.batchSize,
"max_concurrent": p.maxConcurrent,
"cache_stats": cacheStats,
}
}
// WarmupCache preloads common patterns into cache
func (p *GPUBatchProcessor) WarmupCache(commonTexts []string) {
log.Printf("Warming up cache with %d common texts...", len(commonTexts))
// Process in batches to populate cache
for i := 0; i < len(commonTexts); i += p.batchSize {
end := i + p.batchSize
if end > len(commonTexts) {
end = len(commonTexts)
}
batch := commonTexts[i:end]
_, _ = p.ProcessBatch(batch)
}
if p.cache != nil {
stats := p.cache.GetStats()
log.Printf("Cache warmed up: %v hits, %.1f%% hit rate",
stats["hits"], stats["hit_rate"])
}
}