vecboost 0.2.0

High-performance embedding vector service written in Rust
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright (c) 2025-2026 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

use candle_core::{Device, Result as CandleResult, Tensor};
use log::{debug, info, warn};
use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicU64, Ordering};

use super::config::TensorPoolConfig;
use crate::error::VecboostError;

/// 池统计信息
#[derive(Debug, Clone, Default)]
pub struct PoolStats {
    /// 总分配次数
    pub total_allocations: u64,
    /// 总释放次数
    pub total_releases: u64,
    /// 缓存命中次数
    pub cache_hits: u64,
    /// 缓存未命中次数
    pub cache_misses: u64,
    /// 当前池大小
    pub current_pool_size: usize,
    /// 总内存使用量(字节)
    pub total_memory_bytes: u64,
}

/// GPU 张量池
///
/// 用于预分配和复用 GPU 张量,减少频繁的内存分配和释放开销
pub struct TensorPool {
    /// 设备
    device: Device,
    /// 最大批量大小
    max_batch_size: usize,
    /// 最大序列长度
    max_sequence_length: usize,
    /// 池: (batch_size, seq_len) -> 张量队列
    pools: HashMap<(usize, usize), VecDeque<Tensor>>,
    /// 配置
    config: TensorPoolConfig,
    /// 统计信息
    stats: PoolStats,
    /// 总内存使用量(原子)
    total_memory_bytes: AtomicU64,
}

impl TensorPool {
    /// 创建新的张量池
    pub fn new(device: Device, config: TensorPoolConfig) -> Self {
        info!(
            "Creating TensorPool on device {:?} with max_batch_size={}, max_seq_len={}",
            device, config.max_batch_size, config.max_sequence_length
        );

        Self {
            device,
            max_batch_size: config.max_batch_size,
            max_sequence_length: config.max_sequence_length,
            pools: HashMap::new(),
            config,
            stats: PoolStats::default(),
            total_memory_bytes: AtomicU64::new(0),
        }
    }

    /// 预分配张量
    pub fn preallocate(&mut self) -> Result<(), VecboostError> {
        info!("Preallocating tensors...");

        // 预分配常用的批量大小
        let batch_sizes = vec![1, 4, 8, 16, 32, 64, 128];
        let seq_lengths = vec![128, 256, 512, 1024, 2048, 4096, 8192];

        for &batch_size in &batch_sizes {
            if batch_size > self.max_batch_size {
                continue;
            }

            for &seq_len in &seq_lengths {
                if seq_len > self.max_sequence_length {
                    continue;
                }

                let key = (batch_size, seq_len);
                self.pools.entry(key).or_default();

                let pool = self.pools.get_mut(&key).unwrap();
                let pool_size = self.config.pool_size_per_shape;

                for _ in 0..pool_size {
                    // 先创建张量
                    let tensor_result: Result<Tensor, VecboostError> = {
                        let size = batch_size * seq_len;
                        let data = vec![0i64; size];

                        let tensor = Tensor::new(data, &self.device)
                            .and_then(|t| t.reshape(&[batch_size, seq_len]))
                            .map_err(|e| {
                                VecboostError::InferenceError(format!(
                                    "Failed to create tensor: {}",
                                    e
                                ))
                            })?;

                        // 更新内存统计
                        let tensor_size = (batch_size * seq_len * 8) as u64; // i64 = 8 bytes
                        self.total_memory_bytes
                            .fetch_add(tensor_size, Ordering::Relaxed);

                        Ok(tensor)
                    };

                    match tensor_result {
                        Ok(tensor) => {
                            pool.push_back(tensor);
                            self.stats.total_allocations += 1;
                        }
                        Err(e) => {
                            warn!(
                                "Failed to preallocate tensor for shape ({}, {}): {}",
                                batch_size, seq_len, e
                            );
                            // 继续尝试其他形状
                        }
                    }
                }
            }
        }

        info!(
            "Preallocation complete. Total tensors: {}",
            self.pools.values().map(|q| q.len()).sum::<usize>()
        );

        Ok(())
    }

    /// 获取张量
    pub fn acquire(&mut self, batch_size: usize, seq_len: usize) -> Result<Tensor, VecboostError> {
        // 验证参数
        if batch_size > self.max_batch_size {
            return Err(VecboostError::InvalidInput(format!(
                "Batch size {} exceeds maximum {}",
                batch_size, self.max_batch_size
            )));
        }

        if seq_len > self.max_sequence_length {
            return Err(VecboostError::InvalidInput(format!(
                "Sequence length {} exceeds maximum {}",
                seq_len, self.max_sequence_length
            )));
        }

        let key = (batch_size, seq_len);

        // 尝试从池中获取
        if let Some(pool) = self.pools.get_mut(&key)
            && let Some(tensor) = pool.pop_front()
        {
            self.stats.cache_hits += 1;
            self.stats.total_allocations += 1;
            debug!(
                "Acquired tensor from pool for shape ({}, {})",
                batch_size, seq_len
            );
            return Ok(tensor);
        }

        // 池中没有,创建新的
        self.stats.cache_misses += 1;
        self.stats.total_allocations += 1;
        debug!(
            "Creating new tensor for shape ({}, {})",
            batch_size, seq_len
        );

        self.create_tensor(batch_size, seq_len)
    }

    /// 释放张量回池
    pub fn release(&mut self, tensor: Tensor, batch_size: usize, seq_len: usize) {
        let key = (batch_size, seq_len);

        self.pools.entry(key).or_default();

        let pool = self.pools.get_mut(&key).unwrap();

        // 如果池未满,则放回池中
        if pool.len() < self.config.pool_size_per_shape {
            pool.push_back(tensor);
            self.stats.total_releases += 1;
            debug!(
                "Released tensor to pool for shape ({}, {})",
                batch_size, seq_len
            );
        } else {
            // 池已满,直接丢弃(Tensor 会被 Drop)
            self.stats.total_releases += 1;
            // 更新内存统计
            let tensor_size = (batch_size * seq_len * 8) as u64; // i64 = 8 bytes
            self.total_memory_bytes
                .fetch_sub(tensor_size, Ordering::Relaxed);
            debug!(
                "Pool full for shape ({}, {}), tensor dropped, memory reduced by {} bytes",
                batch_size, seq_len, tensor_size
            );
        }
    }

    /// 创建新张量
    fn create_tensor(&self, batch_size: usize, seq_len: usize) -> Result<Tensor, VecboostError> {
        let size = batch_size * seq_len;
        let data = vec![0i64; size];

        let tensor = Tensor::new(data, &self.device)
            .map_err(|e| VecboostError::InferenceError(format!("Failed to create tensor: {}", e)))?
            .reshape(&[batch_size, seq_len])
            .map_err(|e| {
                VecboostError::InferenceError(format!("Failed to reshape tensor: {}", e))
            })?;

        // 更新内存统计
        let tensor_size = (batch_size * seq_len * 8) as u64; // i64 = 8 bytes
        self.total_memory_bytes
            .fetch_add(tensor_size, Ordering::Relaxed);

        Ok(tensor)
    }

    /// 获取统计信息
    pub fn get_stats(&self) -> PoolStats {
        let current_pool_size = self.pools.values().map(|q| q.len()).sum();
        let total_memory_bytes = self.total_memory_bytes.load(Ordering::Relaxed);

        PoolStats {
            total_allocations: self.stats.total_allocations,
            total_releases: self.stats.total_releases,
            cache_hits: self.stats.cache_hits,
            cache_misses: self.stats.cache_misses,
            current_pool_size,
            total_memory_bytes,
        }
    }

    /// 清空池
    pub fn clear(&mut self) {
        info!("Clearing tensor pool...");
        self.pools.clear();
        self.total_memory_bytes.store(0, Ordering::Relaxed);
        info!("Tensor pool cleared");
    }

    /// 获取设备
    pub fn device(&self) -> &Device {
        &self.device
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tensor_pool_creation() {
        let device = Device::Cpu;
        let config = TensorPoolConfig::default();
        let pool = TensorPool::new(device, config);

        assert_eq!(pool.get_stats().total_allocations, 0);
        assert_eq!(pool.get_stats().current_pool_size, 0);
    }

    #[test]
    fn test_acquire_release() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 32,
            max_sequence_length: 512,
            pool_size_per_shape: 2,
            ..Default::default()
        };

        let mut pool = TensorPool::new(device, config);

        // 获取张量
        let tensor = pool.acquire(16, 256).unwrap();
        assert_eq!(pool.get_stats().cache_misses, 1);

        // 释放张量
        pool.release(tensor, 16, 256);
        assert_eq!(pool.get_stats().total_releases, 1);

        // 再次获取,应该从池中获取
        let _tensor2 = pool.acquire(16, 256).unwrap();
        assert_eq!(pool.get_stats().cache_hits, 1);
    }

    #[test]
    fn test_preallocate() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 32,
            max_sequence_length: 512,
            pool_size_per_shape: 2,
            preallocate_on_startup: true,
            ..Default::default()
        };

        let mut pool = TensorPool::new(device, config);

        pool.preallocate().unwrap();

        let stats = pool.get_stats();
        assert!(stats.current_pool_size > 0);
        assert!(stats.total_memory_bytes > 0);
    }

    #[test]
    fn test_batch_size_limit() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 16,
            max_sequence_length: 512,
            ..Default::default()
        };

        let mut pool = TensorPool::new(device, config);

        // 尝试获取超过最大批次的张量
        let result = pool.acquire(32, 256);
        assert!(result.is_err());
    }

    #[test]
    fn test_seq_len_limit() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 32,
            max_sequence_length: 256,
            ..Default::default()
        };

        let mut pool = TensorPool::new(device, config);

        let result = pool.acquire(16, 512);
        assert!(result.is_err());
    }

    #[test]
    fn test_release_pool_full_drops_tensor() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 32,
            max_sequence_length: 256,
            pool_size_per_shape: 1,
            ..Default::default()
        };
        let mut pool = TensorPool::new(device, config);

        let t1 = pool.acquire(8, 128).unwrap();
        let t2 = pool.acquire(8, 128).unwrap();
        let stats_after_acquire = pool.get_stats();
        let mem_after_acquire = stats_after_acquire.total_memory_bytes;
        assert_eq!(stats_after_acquire.cache_misses, 2);
        assert_eq!(stats_after_acquire.total_allocations, 2);

        pool.release(t1, 8, 128);
        assert_eq!(pool.get_stats().current_pool_size, 1);
        assert_eq!(pool.get_stats().total_releases, 1);

        pool.release(t2, 8, 128);
        assert_eq!(pool.get_stats().current_pool_size, 1);
        assert_eq!(pool.get_stats().total_releases, 2);
        let mem_after_drop = pool.get_stats().total_memory_bytes;
        assert_eq!(mem_after_drop, mem_after_acquire - (8 * 128 * 8) as u64);
    }

    #[test]
    fn test_clear_resets_pool() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 32,
            max_sequence_length: 512,
            pool_size_per_shape: 2,
            preallocate_on_startup: true,
            ..Default::default()
        };
        let mut pool = TensorPool::new(device, config);
        pool.preallocate().unwrap();
        assert!(pool.get_stats().current_pool_size > 0);
        assert!(pool.get_stats().total_memory_bytes > 0);

        pool.clear();

        let stats = pool.get_stats();
        assert_eq!(stats.current_pool_size, 0);
        assert_eq!(stats.total_memory_bytes, 0);
        assert!(stats.total_allocations > 0);
    }

    #[test]
    fn test_device_getter() {
        let device = Device::Cpu;
        let config = TensorPoolConfig::default();
        let pool = TensorPool::new(device, config);

        let returned_device = pool.device();
        assert!(matches!(returned_device, Device::Cpu));
    }

    #[test]
    fn test_acquire_after_clear_creates_new() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 32,
            max_sequence_length: 256,
            pool_size_per_shape: 2,
            ..Default::default()
        };
        let mut pool = TensorPool::new(device, config);

        let t = pool.acquire(8, 128).unwrap();
        pool.release(t, 8, 128);
        assert_eq!(pool.get_stats().current_pool_size, 1);

        pool.clear();
        assert_eq!(pool.get_stats().current_pool_size, 0);

        let _ = pool.acquire(8, 128).unwrap();
        let stats = pool.get_stats();
        assert_eq!(stats.cache_misses, 2);
        assert_eq!(stats.cache_hits, 0);
    }

    #[test]
    fn test_preallocate_skips_oversized_shapes() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 8,
            max_sequence_length: 256,
            pool_size_per_shape: 1,
            preallocate_on_startup: true,
            ..Default::default()
        };
        let mut pool = TensorPool::new(device, config);
        pool.preallocate().unwrap();

        let stats = pool.get_stats();
        assert_eq!(stats.current_pool_size, 6);
        assert_eq!(stats.total_allocations, 6);
    }

    #[test]
    fn test_release_different_shapes_independent() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 32,
            max_sequence_length: 256,
            pool_size_per_shape: 2,
            ..Default::default()
        };
        let mut pool = TensorPool::new(device, config);

        let t1 = pool.acquire(8, 128).unwrap();
        let t2 = pool.acquire(16, 256).unwrap();

        pool.release(t1, 8, 128);
        pool.release(t2, 16, 256);
        assert_eq!(pool.get_stats().current_pool_size, 2);

        let _ = pool.acquire(8, 128).unwrap();
        let _ = pool.acquire(16, 256).unwrap();
        let stats = pool.get_stats();
        assert_eq!(stats.cache_hits, 2);
        assert_eq!(stats.cache_misses, 2);
    }

    #[test]
    fn test_preallocate_then_acquire_hits_cache() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 32,
            max_sequence_length: 512,
            pool_size_per_shape: 2,
            preallocate_on_startup: true,
            ..Default::default()
        };
        let mut pool = TensorPool::new(device, config);
        pool.preallocate().unwrap();

        let pre_stats = pool.get_stats();
        let _ = pool.acquire(8, 128).unwrap();
        let post_stats = pool.get_stats();
        assert_eq!(post_stats.cache_hits, 1);
        assert_eq!(post_stats.cache_misses, 0);
        assert_eq!(
            post_stats.total_allocations,
            pre_stats.total_allocations + 1
        );
    }

    #[test]
    fn test_repeated_acquire_release_cycle() {
        let device = Device::Cpu;
        let config = TensorPoolConfig {
            max_batch_size: 32,
            max_sequence_length: 256,
            pool_size_per_shape: 3,
            ..Default::default()
        };
        let mut pool = TensorPool::new(device, config);

        for _ in 0..5 {
            let t = pool.acquire(4, 64).unwrap();
            pool.release(t, 4, 64);
        }

        let stats = pool.get_stats();
        assert_eq!(stats.cache_misses, 1);
        assert_eq!(stats.cache_hits, 4);
        assert_eq!(stats.total_allocations, 5);
        assert_eq!(stats.total_releases, 5);
        assert_eq!(stats.current_pool_size, 1);
    }
}