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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// Copyright (c) 2025-2026 Kirky.X
//
// Licensed under the MIT License
// See LICENSE file in the project root for full license information.

#![allow(unused)]

use crate::config::model::DeviceType;
use crate::device::memory_optimizer::{
    GpuMemoryConfig, ModelMemoryRequirements, SharedGpuMemoryManager,
};
use crate::device::{DeviceCapability, DeviceInfo, DeviceStatus};
use log::{debug, error, info, warn};
use serde::Serialize;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct CudaGpuInfo {
    pub device_id: usize,
    pub name: String,
    pub total_memory_bytes: u64,
    pub available_memory_bytes: u64,
    pub compute_capability: (u8, u8),
    pub supports_float16: bool,
    pub supports_tensor_cores: bool,
    pub sm_count: u32,
    pub max_threads_per_multiprocessor: u32,
    pub cuda_cores_count: u32,
}

#[derive(Debug, Clone, PartialEq)]
pub struct CudaDevice {
    device_id: usize,
    name: String,
    total_memory: u64,
    compute_capability: (u8, u8),
    capability: DeviceCapability,
}

impl CudaDevice {
    #[inline]
    pub fn new(
        device_id: usize,
        name: String,
        total_memory: u64,
        compute_capability: (u8, u8),
    ) -> Self {
        let (major, _minor) = compute_capability;
        let supports_float16 = major >= 7;
        let supports_tensor_cores = major >= 7;

        Self {
            device_id,
            name,
            total_memory,
            compute_capability,
            capability: DeviceCapability {
                supports_float16,
                supports_tensor_cores,
                max_memory_bytes: total_memory,
                compute_capability: Some(compute_capability),
            },
        }
    }

    #[inline]
    pub fn device_id(&self) -> usize {
        self.device_id
    }

    #[inline]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[inline]
    pub fn total_memory(&self) -> u64 {
        self.total_memory
    }

    #[inline]
    pub fn compute_capability(&self) -> (u8, u8) {
        self.compute_capability
    }

    #[inline]
    pub fn capability(&self) -> &DeviceCapability {
        &self.capability
    }

    pub fn info(&self) -> DeviceInfo {
        DeviceInfo {
            device_type: DeviceType::Cuda,
            name: self.name.clone(),
            status: DeviceStatus::Available,
            memory_bytes: Some(self.total_memory),
            is_default: self.device_id == 0,
        }
    }
}

#[derive(Clone)]
pub struct CudaDeviceManager {
    devices: Arc<RwLock<Vec<CudaDevice>>>,
    primary_device_id: Arc<RwLock<Option<usize>>>,
    memory_managers: Arc<RwLock<HashMap<usize, SharedGpuMemoryManager>>>,
    initialized: Arc<RwLock<bool>>,
}

impl CudaDeviceManager {
    pub fn new() -> Self {
        Self {
            devices: Arc::new(RwLock::new(Vec::new())),
            primary_device_id: Arc::new(RwLock::new(None)),
            memory_managers: Arc::new(RwLock::new(HashMap::new())),
            initialized: Arc::new(RwLock::new(false)),
        }
    }

    pub async fn initialize(&self) -> Result<(), String> {
        let mut initialized = self.initialized.write().await;
        if *initialized {
            return Ok(());
        }

        // detect 失败时返回空设备列表(Ok),与 is_supported() = (count > 0) 语义一致:
        // 无 CUDA 硬件 / driver 不可用 / 沙箱限制均视为"无可用设备",不算错误。
        let devices = self.detect_cuda_devices().await.unwrap_or_else(|e| {
            warn!(
                "CUDA detection returned error, treating as no available devices: {}",
                e
            );
            Vec::new()
        });

        let mut devices_guard = self.devices.write().await;
        *devices_guard = devices;

        // 为每个设备创建内存管理器
        let mut memory_managers = self.memory_managers.write().await;
        memory_managers.clear();

        for device in &*devices_guard {
            let memory_config = GpuMemoryConfig::default();
            let memory_manager = SharedGpuMemoryManager::new(device.total_memory(), memory_config);
            memory_managers.insert(device.device_id(), memory_manager);
            info!(
                "Created memory manager for device {}: {} MB",
                device.device_id(),
                device.total_memory() / (1024 * 1024)
            );
        }

        if !devices_guard.is_empty() {
            let mut primary = self.primary_device_id.write().await;
            *primary = Some(0);
        }

        *initialized = true;
        info!(
            "CUDA device manager initialized with {} device(s)",
            devices_guard.len()
        );
        Ok(())
    }

    async fn detect_cuda_devices(&self) -> Result<Vec<CudaDevice>, String> {
        #[cfg(feature = "cuda")]
        {
            self.detect_with_candle().await
        }

        #[cfg(not(feature = "cuda"))]
        {
            Ok(self.detect_compatible().await)
        }
    }

    #[cfg(feature = "cuda")]
    async fn detect_with_candle(&self) -> Result<Vec<CudaDevice>, String> {
        match candle_core::Device::cuda_if_available(0) {
            Ok(_device) => {
                let cuda_device = CudaDevice::new(
                    0,
                    "CUDA Device (via candle-core)".to_string(),
                    8 * 1024 * 1024 * 1024, // 8GB default
                    (7, 0),
                );

                info!("Detected CUDA device via candle-core");
                Ok(vec![cuda_device])
            }
            Err(e) => {
                // CUDA driver 不可用 / 沙箱限制 / 无 GPU 硬件均视为"无可用设备"。
                // 保留 Err 给上层调用方根据需要区分"完全不可用"与"无设备但可降级 CPU"。
                warn!("Failed to initialize CUDA device via candle-core: {}", e);
                Err(format!("CUDA device detection failed: {}", e))
            }
        }
    }

    #[cfg(not(feature = "cuda"))]
    async fn detect_compatible(&self) -> Vec<CudaDevice> {
        let mut devices = Vec::new();

        if let Ok(nvidia_info) = detect_nvidia_driver().await
            && nvidia_info.cuda_version.is_some()
        {
            let cuda_device = CudaDevice::new(
                0,
                nvidia_info
                    .device_name
                    .unwrap_or_else(|| "Unknown NVIDIA GPU".to_string()),
                nvidia_info.total_vram_bytes,
                nvidia_info.compute_capability.unwrap_or((7, 0)),
            );
            info!("Detected compatible CUDA device: {}", cuda_device.name());
            devices.push(cuda_device);
        }

        if devices.is_empty() {
            debug!("No CUDA devices detected (CUDA feature not enabled or no NVIDIA GPU found)");
        }

        devices
    }

    pub async fn devices(&self) -> Vec<CudaDevice> {
        self.devices.read().await.clone()
    }

    pub async fn primary_device(&self) -> Option<CudaDevice> {
        let devices = self.devices.read().await;
        let primary_id = *self.primary_device_id.read().await;

        match primary_id {
            Some(id) if id < devices.len() => Some(devices[id].clone()),
            Some(id) if !devices.is_empty() => Some(devices[0].clone()),
            _ => devices.first().cloned(),
        }
    }

    pub async fn get_device(&self, device_id: usize) -> Option<CudaDevice> {
        let devices = self.devices.read().await;
        devices.get(device_id).cloned()
    }

    pub async fn device_count(&self) -> usize {
        self.devices.read().await.len()
    }

    pub async fn available_memory(&self, device_id: usize) -> Option<u64> {
        let devices = self.devices.read().await;
        devices.get(device_id).map(|d| d.total_memory())
    }

    pub async fn get_optimal_batch_size(&self, device_id: usize) -> usize {
        if let Some(_device) = self.get_device(device_id).await {
            if let Some(memory_manager) = self.memory_managers.read().await.get(&device_id).cloned()
            {
                // 使用智能内存管理器计算批量大小(需要先注册模型需求)
                // 如果没有模型需求注册,使用原始的简单算法
                let stats = memory_manager.get_performance_stats().await;
                if stats.current_batch_size > 0 {
                    stats.current_batch_size
                } else {
                    // 降级到简单算法
                    let available = memory_manager.get_available_memory().await;
                    let available_mb = available / (1024 * 1024);
                    std::cmp::min(32, (available_mb / 2048) as usize + 1)
                }
            } else {
                16
            }
        } else {
            16
        }
    }

    /// 注册模型的内存需求
    pub async fn register_model_memory_requirements(
        &self,
        device_id: usize,
        requirements: ModelMemoryRequirements,
    ) -> Result<(), String> {
        let memory_managers = self.memory_managers.read().await;
        let memory_manager = memory_managers
            .get(&device_id)
            .ok_or_else(|| format!("Device {} not found", device_id))?
            .clone();
        drop(memory_managers);

        memory_manager.register_model(requirements).await;
        Ok(())
    }

    /// 获取设备的内存使用率
    pub async fn get_memory_usage_percent(&self, device_id: usize) -> f64 {
        if let Some(memory_manager) = self.memory_managers.read().await.get(&device_id).cloned() {
            memory_manager.get_memory_usage_percent().await
        } else {
            0.0
        }
    }

    /// 动态调整批量大小
    pub async fn adjust_batch_size_dynamically(
        &self,
        device_id: usize,
        latency_ms: f64,
        memory_usage_percent: f64,
    ) {
        if let Some(memory_manager) = self.memory_managers.read().await.get(&device_id).cloned() {
            memory_manager
                .adjust_batch_size_dynamically(latency_ms, memory_usage_percent)
                .await
        }
    }

    /// 获取性能统计
    pub async fn get_performance_stats(
        &self,
        device_id: usize,
    ) -> Option<crate::device::memory_optimizer::PerformanceStats> {
        if let Some(manager) = self.memory_managers.read().await.get(&device_id).cloned() {
            Some(manager.get_performance_stats().await)
        } else {
            None
        }
    }

    pub async fn is_supported(&self) -> bool {
        !self.devices.read().await.is_empty()
    }

    pub async fn reset_primary_device(&self, device_id: usize) -> Result<(), String> {
        let devices = self.devices.read().await;
        if device_id >= devices.len() {
            return Err(format!("Device {} not found", device_id));
        }

        let mut primary = self.primary_device_id.write().await;
        *primary = Some(device_id);
        info!("Primary CUDA device set to: {}", devices[device_id].name());

        Ok(())
    }
}

impl Default for CudaDeviceManager {
    fn default() -> Self {
        Self::new()
    }
}

async fn detect_nvidia_driver() -> Result<NvidiaDriverInfo, String> {
    use std::process::Command;
    use std::str;

    let mut info = NvidiaDriverInfo {
        cuda_version: None,
        driver_version: None,
        device_name: None,
        total_vram_bytes: 0,
        compute_capability: None,
    };

    let nvidia_smi_output = Command::new("nvidia-smi")
        .arg("--query-gpu=name,memory.total,compute_cap")
        .arg("--format=csv,noheader,nounits")
        .output()
        .map_err(|e| format!("Failed to execute nvidia-smi: {}", e))?;

    if nvidia_smi_output.status.success()
        && let Ok(output) = str::from_utf8(&nvidia_smi_output.stdout)
    {
        let parts: Vec<&str> = output.trim().split(',').map(|s| s.trim()).collect();
        if parts.len() >= 3 {
            info.device_name = Some(parts[0].to_string());

            if let Ok(vram_mb) = parts[1].parse::<u64>() {
                info.total_vram_bytes = vram_mb * 1024 * 1024;
            }

            let cc_parts: Vec<&str> = parts[2].split('.').collect();
            if cc_parts.len() >= 2
                && let (Ok(major), Ok(minor)) =
                    (cc_parts[0].parse::<u8>(), cc_parts[1].parse::<u8>())
            {
                info.compute_capability = Some((major, minor));
            }
        }
    }

    let cuda_version_output = Command::new("nvidia-smi")
        .arg("--query-gpu=driver_version")
        .arg("--format=csv,noheader")
        .output()
        .ok()
        .filter(|o| o.status.success())
        .and_then(|o| str::from_utf8(&o.stdout).ok().map(|s| s.trim().to_string()));

    info.driver_version = cuda_version_output;

    if info.total_vram_bytes > 0 {
        info.cuda_version = Some(11);
    }

    Ok(info)
}

#[derive(Debug)]
struct NvidiaDriverInfo {
    cuda_version: Option<u32>,
    driver_version: Option<String>,
    device_name: Option<String>,
    total_vram_bytes: u64,
    compute_capability: Option<(u8, u8)>,
}

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

    #[tokio::test]
    async fn test_cuda_device_manager_creation() {
        let manager = CudaDeviceManager::new();

        assert_eq!(manager.device_count().await, 0);
        assert!(!manager.is_supported().await);
    }

    #[tokio::test]
    async fn test_cuda_device_info() {
        let device = CudaDevice::new(
            0,
            "NVIDIA GeForce RTX 3060".to_string(),
            12 * 1024 * 1024 * 1024,
            (8, 6),
        );

        assert_eq!(device.device_id(), 0);
        assert_eq!(device.name(), "NVIDIA GeForce RTX 3060");
        assert_eq!(device.total_memory(), 12 * 1024 * 1024 * 1024);
        assert_eq!(device.compute_capability(), (8, 6));

        let capability = device.capability();
        assert!(capability.supports_float16);
        assert!(capability.supports_tensor_cores);
        assert_eq!(capability.max_memory_bytes, 12 * 1024 * 1024 * 1024);
    }

    #[tokio::test]
    async fn test_cuda_device_info_conversion() {
        let device = CudaDevice::new(
            1,
            "NVIDIA GeForce GTX 1080".to_string(),
            8 * 1024 * 1024 * 1024,
            (6, 1),
        );

        let info = device.info();
        assert_eq!(info.device_type, DeviceType::Cuda);
        assert_eq!(info.name, "NVIDIA GeForce GTX 1080");
        assert_eq!(info.status, DeviceStatus::Available);
        assert_eq!(info.memory_bytes, Some(8 * 1024 * 1024 * 1024));
        assert!(!info.is_default);
    }

    #[tokio::test]
    async fn test_compute_capability_support() {
        let sm_70_device = CudaDevice::new(0, "V100".to_string(), 16 * 1024 * 1024 * 1024, (7, 0));
        assert!(sm_70_device.capability.supports_float16);
        assert!(sm_70_device.capability.supports_tensor_cores);

        let sm_60_device =
            CudaDevice::new(0, "Pascal GPU".to_string(), 8 * 1024 * 1024 * 1024, (6, 0));
        assert!(!sm_60_device.capability.supports_float16);
        assert!(!sm_60_device.capability.supports_tensor_cores);

        let sm_50_device =
            CudaDevice::new(0, "Maxwell GPU".to_string(), 4 * 1024 * 1024 * 1024, (5, 0));
        assert!(!sm_50_device.capability.supports_float16);
        assert!(!sm_50_device.capability.supports_tensor_cores);
    }

    #[tokio::test]
    async fn test_reset_primary_device() {
        let manager = CudaDeviceManager::new();

        assert_eq!(manager.primary_device().await, None);

        let result = manager.reset_primary_device(0).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_initialize_without_cuda_hardware() {
        let manager = CudaDeviceManager::new();

        let result = manager.initialize().await;
        assert!(
            result.is_ok(),
            "initialize should succeed even without CUDA"
        );
        let count = manager.device_count().await;
        assert_eq!(manager.is_supported().await, count > 0);
    }

    #[tokio::test]
    async fn test_initialize_is_idempotent() {
        let manager = CudaDeviceManager::new();

        manager.initialize().await.unwrap();
        let first_count = manager.device_count().await;

        manager.initialize().await.unwrap();
        let second_count = manager.device_count().await;

        assert_eq!(first_count, second_count);
    }

    #[tokio::test]
    async fn test_devices_returns_empty_after_init() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        let devices = manager.devices().await;
        assert_eq!(devices.len(), manager.device_count().await);
    }

    #[tokio::test]
    async fn test_primary_device_returns_none_when_empty() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        let count = manager.device_count().await;
        let primary = manager.primary_device().await;
        assert_eq!(primary.is_some(), count > 0);
    }

    #[tokio::test]
    async fn test_get_device_returns_none_for_invalid_id() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        assert!(manager.get_device(999).await.is_none());
    }

    #[tokio::test]
    async fn test_available_memory_returns_none_for_invalid_id() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        assert!(manager.available_memory(999).await.is_none());
    }

    #[tokio::test]
    async fn test_get_optimal_batch_size_no_device_returns_default() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        assert_eq!(manager.get_optimal_batch_size(0).await, 16);
        assert_eq!(manager.get_optimal_batch_size(999).await, 16);
    }

    #[tokio::test]
    async fn test_register_model_requirements_unknown_device_errors() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        let requirements = ModelMemoryRequirements {
            model_name: "test-model".to_string(),
            base_memory_bytes: 100_000_000,
            per_token_memory_bytes: 1000,
            per_vector_memory_bytes: 4000,
            max_sequence_length: 512,
        };

        let result = manager
            .register_model_memory_requirements(999, requirements)
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("not found"));
    }

    #[tokio::test]
    async fn test_get_memory_usage_percent_no_manager_returns_zero() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        assert_eq!(manager.get_memory_usage_percent(0).await, 0.0);
        assert_eq!(manager.get_memory_usage_percent(999).await, 0.0);
    }

    #[tokio::test]
    async fn test_adjust_batch_size_dynamically_no_manager_no_panic() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        manager.adjust_batch_size_dynamically(0, 50.0, 60.0).await;
        manager.adjust_batch_size_dynamically(999, 50.0, 60.0).await;
    }

    #[tokio::test]
    async fn test_get_performance_stats_no_manager_returns_none() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        assert!(manager.get_performance_stats(999).await.is_none());
    }

    #[tokio::test]
    async fn test_reset_primary_device_invalid_id_errors() {
        let manager = CudaDeviceManager::new();
        manager.initialize().await.unwrap();

        let result = manager.reset_primary_device(5).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("not found"));
    }

    #[tokio::test]
    async fn test_cuda_device_default_manager_uses_new() {
        let manager = CudaDeviceManager::default();
        assert_eq!(manager.device_count().await, 0);
        assert!(!manager.is_supported().await);
    }

    #[test]
    fn test_cuda_gpu_info_fields() {
        let info = CudaGpuInfo {
            device_id: 0,
            name: "Test GPU".to_string(),
            total_memory_bytes: 8 * 1024 * 1024 * 1024,
            available_memory_bytes: 4 * 1024 * 1024 * 1024,
            compute_capability: (8, 6),
            supports_float16: true,
            supports_tensor_cores: true,
            sm_count: 84,
            max_threads_per_multiprocessor: 1536,
            cuda_cores_count: 5888,
        };

        assert_eq!(info.device_id, 0);
        assert_eq!(info.name, "Test GPU");
        assert_eq!(info.total_memory_bytes, 8 * 1024 * 1024 * 1024);
        assert_eq!(info.available_memory_bytes, 4 * 1024 * 1024 * 1024);
        assert_eq!(info.compute_capability, (8, 6));
        assert!(info.supports_float16);
        assert!(info.supports_tensor_cores);
        assert_eq!(info.sm_count, 84);
        assert_eq!(info.max_threads_per_multiprocessor, 1536);
        assert_eq!(info.cuda_cores_count, 5888);
    }

    #[test]
    fn test_cuda_device_info_is_default_for_device_zero() {
        let device = CudaDevice::new(0, "RTX 4090".to_string(), 24 * 1024 * 1024 * 1024, (8, 9));

        let info = device.info();
        assert!(info.is_default);
    }

    #[test]
    fn test_cuda_device_info_not_default_for_nonzero_id() {
        let device = CudaDevice::new(2, "RTX 4090".to_string(), 24 * 1024 * 1024 * 1024, (8, 9));

        let info = device.info();
        assert!(!info.is_default);
    }

    #[test]
    fn test_cuda_device_capability_set_for_high_compute_capability() {
        let device = CudaDevice::new(0, "H100".to_string(), 80 * 1024 * 1024 * 1024, (9, 0));

        let cap = device.capability();
        assert!(cap.supports_float16);
        assert!(cap.supports_tensor_cores);
        assert_eq!(cap.compute_capability, Some((9, 0)));
        assert_eq!(cap.max_memory_bytes, 80 * 1024 * 1024 * 1024);
    }
}