vecboost 0.3.0-rc.1

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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

#![allow(unused)]

use crate::config::model::DeviceType;
use log::{debug, info, warn};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use tokio::sync::RwLock;

/// AMD GPU 检测不可用时的 fallback 默认参数
const DEFAULT_OPENCL_VRAM_BYTES: u64 = 8 * 1024 * 1024 * 1024; // 8 GB
const DEFAULT_OPENCL_COMPUTE_CAP: (u32, u32) = (5, 0);
const DEFAULT_ROCM_VRAM_BYTES: u64 = 16 * 1024 * 1024 * 1024; // 16 GB
const DEFAULT_ROCM_COMPUTE_CAP: (u32, u32) = (9, 0);
const DEFAULT_DRIVER_VERSION: &str = "unknown";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AmdGpuInfo {
    pub name: String,
    pub device_id: u32,
    pub vram_bytes: u64,
    pub compute_capability: (u32, u32),
    pub opencl_version: String,
    pub roc_version: Option<String>,
    pub driver_version: String,
    pub is_available: bool,
}

impl Default for AmdGpuInfo {
    fn default() -> Self {
        Self {
            name: "Unknown AMD GPU".to_string(),
            device_id: 0,
            vram_bytes: 0,
            compute_capability: (0, 0),
            opencl_version: "3.0".to_string(),
            roc_version: None,
            driver_version: "unknown".to_string(),
            is_available: false,
        }
    }
}

#[derive(Debug)]
pub struct AmdDevice {
    info: AmdGpuInfo,
    device_type: DeviceType,
    memory_used: AtomicU64,
    memory_allocated: AtomicU64,
    compute_units: u32,
    max_work_group_size: usize,
    max_work_item_dimensions: u32,
    is_busy: AtomicBool,
}

impl Clone for AmdDevice {
    fn clone(&self) -> Self {
        Self {
            info: self.info.clone(),
            device_type: self.device_type.clone(),
            memory_used: AtomicU64::new(self.memory_used.load(Ordering::Relaxed)),
            memory_allocated: AtomicU64::new(self.memory_allocated.load(Ordering::Relaxed)),
            compute_units: self.compute_units,
            max_work_group_size: self.max_work_group_size,
            max_work_item_dimensions: self.max_work_item_dimensions,
            is_busy: AtomicBool::new(self.is_busy.load(Ordering::Relaxed)),
        }
    }
}

impl AmdDevice {
    pub fn new(info: AmdGpuInfo) -> Self {
        let compute_units = (info.vram_bytes / (1024 * 1024 * 1024) * 64).min(80) as u32;

        Self {
            info,
            device_type: DeviceType::Amd,
            memory_used: AtomicU64::new(0),
            memory_allocated: AtomicU64::new(0),
            compute_units,
            max_work_group_size: 256,
            max_work_item_dimensions: 3,
            is_busy: AtomicBool::new(false),
        }
    }

    pub fn from_opencl(index: usize) -> Option<Self> {
        debug!("Attempting to detect AMD GPU via OpenCL at index {}", index);

        // OpenCL 无法直接查询 VRAM,使用 fallback 默认值
        warn!(
            "AMD GPU (OpenCL) 使用 fallback 默认参数 ({}GB, compute {}.{}). \
             OpenCL 无法查询真实 VRAM,建议通过 ROCm 路径获取准确信息",
            DEFAULT_OPENCL_VRAM_BYTES / (1024 * 1024 * 1024),
            DEFAULT_OPENCL_COMPUTE_CAP.0,
            DEFAULT_OPENCL_COMPUTE_CAP.1,
        );

        let info = AmdGpuInfo {
            name: format!("AMD GPU (OpenCL) - Device {}", index),
            device_id: index as u32,
            vram_bytes: DEFAULT_OPENCL_VRAM_BYTES,
            compute_capability: DEFAULT_OPENCL_COMPUTE_CAP,
            opencl_version: "3.0".to_string(),
            roc_version: None,
            driver_version: query_amd_driver_version(),
            is_available: true,
        };

        Some(Self::new(info))
    }

    pub fn from_rocm(index: usize) -> Option<Self> {
        debug!("Attempting to detect AMD GPU via ROCm at index {}", index);

        // 检查 rocm-smi 是否可用,不可用时不创建伪设备
        if std::process::Command::new("rocm-smi")
            .arg("--version")
            .output()
            .is_err()
        {
            debug!(
                "rocm-smi not available, skipping ROCm device at index {}",
                index
            );
            return None;
        }

        let vram = query_rocm_vram(index);

        let info = AmdGpuInfo {
            name: format!("AMD GPU (ROCm) - Device {}", index),
            device_id: index as u32,
            vram_bytes: vram,
            compute_capability: DEFAULT_ROCM_COMPUTE_CAP,
            opencl_version: "3.0".to_string(),
            roc_version: query_rocm_version(),
            driver_version: query_amd_driver_version(),
            is_available: true,
        };

        Some(Self::new(info))
    }

    pub fn info(&self) -> &AmdGpuInfo {
        &self.info
    }

    pub fn device_type(&self) -> DeviceType {
        self.device_type.clone()
    }

    pub fn name(&self) -> &str {
        &self.info.name
    }

    pub fn vram_bytes(&self) -> u64 {
        self.info.vram_bytes
    }

    pub fn available_memory(&self) -> u64 {
        self.info.vram_bytes - self.memory_used.load(Ordering::SeqCst)
    }

    pub fn memory_usage_percent(&self) -> f64 {
        let used = self.memory_used.load(Ordering::SeqCst);
        if self.info.vram_bytes == 0 {
            0.0
        } else {
            (used as f64 / self.info.vram_bytes as f64) * 100.0
        }
    }

    pub fn compute_units(&self) -> u32 {
        self.compute_units
    }

    pub fn max_work_group_size(&self) -> usize {
        self.max_work_group_size
    }

    pub fn allocate(&self, bytes: u64) -> bool {
        // 原子 check-then-act:避免并发调用导致超分配
        let result =
            self.memory_allocated
                .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |current| {
                    let new_allocated = current + bytes;
                    if new_allocated > self.info.vram_bytes {
                        None // 拒绝:超出 VRAM
                    } else {
                        Some(new_allocated)
                    }
                });

        match result {
            Ok(_old) => {
                self.memory_used.store(
                    self.memory_allocated.load(Ordering::SeqCst),
                    Ordering::SeqCst,
                );
                true
            }
            Err(_) => {
                log::warn!(
                    "GPU memory allocation failed: requested {} bytes, available {} bytes",
                    bytes,
                    self.available_memory()
                );
                false
            }
        }
    }

    pub fn deallocate(&self, bytes: u64) {
        let current = self.memory_allocated.load(Ordering::SeqCst);
        self.memory_allocated
            .store(current.saturating_sub(bytes), Ordering::SeqCst);
        self.memory_used.store(
            self.memory_allocated.load(Ordering::SeqCst),
            Ordering::SeqCst,
        );
    }

    pub fn is_busy(&self) -> bool {
        self.is_busy.load(Ordering::SeqCst)
    }

    pub fn set_busy(&self, busy: bool) {
        self.is_busy.store(busy, Ordering::SeqCst);
    }

    pub fn supports_precision(&self, precision: &str) -> bool {
        matches!(precision, "fp32" | "fp16" | "bf16")
    }

    pub fn supports_operation(&self, operation: &str) -> bool {
        matches!(
            operation,
            "matrix_multiply" | "convolution" | "activation" | "normalization" | "reduction"
        )
    }
}

/// 查询 AMD 驱动版本
///
/// 优先读取 `/sys/module/amdgpu/version`(Linux amdgpu 内核模块)。
/// 不可用时 fallback 到 `DEFAULT_DRIVER_VERSION`。
fn query_amd_driver_version() -> String {
    // 尝试从 sysfs 读取 amdgpu 内核模块版本
    if let Ok(version) = std::fs::read_to_string("/sys/module/amdgpu/version") {
        let trimmed = version.trim().to_string();
        if !trimmed.is_empty() {
            info!("AMD 驱动版本 (sysfs): {}", trimmed);
            return trimmed;
        }
    }
    // 尝试 rocm-smi 查询
    if let Ok(output) = std::process::Command::new("rocm-smi")
        .arg("--showdriverversion")
        .output()
        && output.status.success()
    {
        let text = String::from_utf8_lossy(&output.stdout).to_string();
        // 解析 "Driver version: X.Y.Z" 格式
        for line in text.lines() {
            if let Some(ver) = line.strip_prefix("Driver version:") {
                let trimmed = ver.trim().to_string();
                if !trimmed.is_empty() {
                    info!("AMD 驱动版本 (rocm-smi): {}", trimmed);
                    return trimmed;
                }
            }
        }
    }
    debug!(
        "AMD 驱动版本查询失败,使用 fallback: {}",
        DEFAULT_DRIVER_VERSION
    );
    DEFAULT_DRIVER_VERSION.to_string()
}

/// 查询 ROCm VRAM 大小
///
/// 优先通过 `rocm-smi --showmeminfo vram` 查询真实显存。
/// 不可用时 fallback 到 `DEFAULT_ROCM_VRAM_BYTES`。
fn query_rocm_vram(index: usize) -> u64 {
    if let Ok(output) = std::process::Command::new("rocm-smi")
        .args(["--showmeminfo", "vram", "--json"])
        .output()
        && output.status.success()
    {
        let text = String::from_utf8_lossy(&output.stdout);
        // rocm-smi --json 输出格式: {"card0": {"VRAM Total Memory (MiB)": "16384", ...}}
        // 简单解析: 查找 "VRAM Total" 相关字段
        for line in text.lines() {
            if line.contains("VRAM Total") || line.contains("Total Memory") {
                // 尝试提取数字 (MiB)
                if let Some(mib) = extract_number_from_line(line) {
                    let bytes = mib * 1024 * 1024;
                    info!("ROCm VRAM (rocm-smi, device {}): {} MB", index, mib);
                    return bytes;
                }
            }
        }
    }
    warn!(
        "ROCm VRAM 查询失败,使用 fallback 默认值: {}GB",
        DEFAULT_ROCM_VRAM_BYTES / (1024 * 1024 * 1024)
    );
    DEFAULT_ROCM_VRAM_BYTES
}

/// 查询 ROCm 版本
fn query_rocm_version() -> Option<String> {
    if let Ok(output) = std::process::Command::new("rocm-smi")
        .arg("--showdriverversion")
        .output()
        && output.status.success()
    {
        let text = String::from_utf8_lossy(&output.stdout);
        for line in text.lines() {
            if let Some(ver) = line.strip_prefix("ROCm version:") {
                let trimmed = ver.trim().to_string();
                if !trimmed.is_empty() {
                    info!("ROCm 版本: {}", trimmed);
                    return Some(trimmed);
                }
            }
        }
    }
    None
}

/// 从文本行中提取第一个出现的数字序列
fn extract_number_from_line(line: &str) -> Option<u64> {
    // 提取第一个连续数字序列,避免多数字行拼接导致解析错误
    let mut num_str = String::new();
    let mut found_digits = false;
    for c in line.chars() {
        if c.is_ascii_digit() {
            num_str.push(c);
            found_digits = true;
        } else if found_digits {
            // 遇到非数字字符且已有数字序列,停止提取
            break;
        }
    }
    if found_digits {
        num_str.parse().ok()
    } else {
        None
    }
}

pub struct AmdDeviceManager {
    devices: Arc<RwLock<Vec<Arc<AmdDevice>>>>,
    primary_device: Arc<RwLock<Option<usize>>>,
    opencl_available: AtomicBool,
    rocm_available: AtomicBool,
    initialized: Arc<AtomicBool>,
}

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

impl AmdDeviceManager {
    pub fn new() -> Self {
        Self {
            devices: Arc::new(RwLock::new(Vec::new())),
            primary_device: Arc::new(RwLock::new(None)),
            opencl_available: AtomicBool::new(false),
            rocm_available: AtomicBool::new(false),
            initialized: Arc::new(AtomicBool::new(false)),
        }
    }

    pub async fn initialize(&self) -> Result<(), crate::error::VecboostError> {
        if self.initialized.load(Ordering::SeqCst) {
            return Ok(());
        }

        log::info!("Initializing AMD GPU device manager...");

        let mut devices = self.devices.write().await;
        devices.clear();

        let mut opencl_found = false;
        let mut rocm_found = false;

        for i in 0..4 {
            if let Some(device) = AmdDevice::from_rocm(i) {
                log::info!(
                    "Found ROCm-compatible AMD GPU: {} with {} bytes VRAM",
                    device.name(),
                    device.vram_bytes()
                );
                devices.push(Arc::new(device));
                rocm_found = true;
            }
        }

        if !rocm_found {
            for i in 0..4 {
                if let Some(device) = AmdDevice::from_opencl(i) {
                    log::info!(
                        "Found OpenCL-compatible AMD GPU: {} with {} bytes VRAM",
                        device.name(),
                        device.vram_bytes()
                    );
                    devices.push(Arc::new(device));
                    opencl_found = true;
                }
            }
        }

        self.opencl_available.store(opencl_found, Ordering::SeqCst);
        self.rocm_available.store(rocm_found, Ordering::SeqCst);

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

        self.initialized.store(true, Ordering::SeqCst);

        log::info!(
            "AMD GPU initialization complete. Found {} device(s) (ROCm: {}, OpenCL: {})",
            devices.len(),
            rocm_found,
            opencl_found
        );

        Ok(())
    }

    pub fn is_initialized(&self) -> bool {
        self.initialized.load(Ordering::SeqCst)
    }

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

    pub async fn primary_device(&self) -> Option<Arc<AmdDevice>> {
        let primary = self.primary_device.read().await;
        let devices = self.devices.read().await;
        match *primary {
            Some(idx) if idx < devices.len() => Some(devices[idx].clone()),
            _ => devices.first().cloned(),
        }
    }

    pub fn is_opencl_available(&self) -> bool {
        self.opencl_available.load(Ordering::SeqCst)
    }

    pub fn is_rocm_available(&self) -> bool {
        self.rocm_available.load(Ordering::SeqCst)
    }

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

    pub async fn total_vram(&self) -> u64 {
        let devices = self.devices.read().await;
        devices.iter().map(|d| d.vram_bytes()).sum()
    }

    pub async fn available_vram(&self) -> u64 {
        let devices = self.devices.read().await;
        devices.iter().map(|d| d.available_memory()).sum()
    }

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

    pub async fn memory_usage_summary(&self) -> String {
        let devices = self.devices.read().await;
        let total_used: u64 = devices
            .iter()
            .map(|d| d.memory_used.load(Ordering::SeqCst))
            .sum();
        let total_vram: u64 = devices.iter().map(|d| d.vram_bytes()).sum();

        format!(
            "AMD GPU Memory: {} bytes used / {} bytes total ({:.1}%)",
            total_used,
            total_vram,
            if total_vram > 0 {
                (total_used as f64 / total_vram as f64) * 100.0
            } else {
                0.0
            }
        )
    }

    pub async fn set_primary(&self, index: usize) -> bool {
        let devices = self.devices.read().await;
        if index < devices.len() {
            let mut primary = self.primary_device.write().await;
            *primary = Some(index);
            true
        } else {
            false
        }
    }

    pub async fn reset(&self) {
        let devices = self.devices.write().await;
        for device in devices.iter() {
            device.memory_allocated.store(0, Ordering::SeqCst);
            device.memory_used.store(0, Ordering::SeqCst);
            device.is_busy.store(false, Ordering::SeqCst);
        }
    }
}

pub async fn create_amd_device_manager() -> Result<AmdDeviceManager, crate::error::VecboostError> {
    let manager = AmdDeviceManager::new();
    manager.initialize().await?;
    Ok(manager)
}

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

    #[test]
    fn test_amd_gpu_info_default() {
        let info = AmdGpuInfo::default();
        assert_eq!(info.name, "Unknown AMD GPU");
        assert_eq!(info.device_id, 0);
        assert_eq!(info.vram_bytes, 0);
        assert_eq!(info.compute_capability, (0, 0));
        assert_eq!(info.opencl_version, "3.0");
        assert!(info.roc_version.is_none());
        assert_eq!(info.driver_version, "unknown");
        assert!(!info.is_available);
    }

    #[test]
    fn test_amd_gpu_info_default_eq() {
        let a = AmdGpuInfo::default();
        let b = AmdGpuInfo::default();
        assert_eq!(a, b);
    }

    fn make_info(vram_bytes: u64) -> AmdGpuInfo {
        AmdGpuInfo {
            name: "AMD Radeon RX 7900 XTX".to_string(),
            device_id: 0x73BF,
            vram_bytes,
            compute_capability: (9, 0),
            opencl_version: "3.0".to_string(),
            roc_version: Some("6.0.0".to_string()),
            driver_version: "24.0.0".to_string(),
            is_available: true,
        }
    }

    #[test]
    fn test_amd_device_new_compute_units_cap_at_80() {
        let info = make_info(64 * 1024 * 1024 * 1024);
        let device = AmdDevice::new(info);
        assert_eq!(device.compute_units(), 80);
        assert_eq!(device.max_work_group_size(), 256);
    }

    #[test]
    fn test_amd_device_new_small_vram_compute_units() {
        let info = make_info(1024 * 1024 * 1024);
        let device = AmdDevice::new(info);
        assert_eq!(device.compute_units(), 64);
    }

    #[test]
    fn test_amd_device_new_medium_vram_compute_units_capped() {
        let info = make_info(4 * 1024 * 1024 * 1024);
        let device = AmdDevice::new(info);
        assert_eq!(device.compute_units(), 80);
    }

    #[test]
    fn test_amd_device_new_zero_vram_compute_units() {
        let info = make_info(0);
        let device = AmdDevice::new(info);
        assert_eq!(device.compute_units(), 0);
    }

    #[test]
    fn test_amd_device_from_opencl() {
        let device = AmdDevice::from_opencl(2).expect("from_opencl should return Some");
        assert!(device.info().is_available);
        assert_eq!(device.device_type(), DeviceType::Amd);
        assert!(device.name().contains("OpenCL"));
        assert!(device.name().contains("Device 2"));
        assert_eq!(device.vram_bytes(), 8 * 1024 * 1024 * 1024);
    }

    #[test]
    fn test_amd_device_from_rocm() {
        // from_rocm 的结果取决于 rocm-smi 是否可用(可能被 mock 测试影响 PATH)
        let result = AmdDevice::from_rocm(1);
        match result {
            None => {} // rocm-smi 不可用
            Some(device) => {
                assert!(device.info().is_available);
                assert_eq!(device.device_type(), DeviceType::Amd);
                assert!(device.name().contains("ROCm"));
                assert!(device.name().contains("Device 1"));
                assert!(device.vram_bytes() > 0);
            }
        }
    }

    #[test]
    fn test_amd_device_info_accessors() {
        let info = make_info(16 * 1024 * 1024 * 1024);
        let device = AmdDevice::new(info.clone());
        assert_eq!(device.info().name, info.name);
        assert_eq!(device.info().device_id, info.device_id);
        assert_eq!(device.info().vram_bytes, info.vram_bytes);
        assert_eq!(device.name(), info.name);
        assert_eq!(device.vram_bytes(), info.vram_bytes);
    }

    #[test]
    fn test_amd_device_available_memory_initial() {
        let device = AmdDevice::new(make_info(1024));
        assert_eq!(device.available_memory(), 1024);
    }

    #[test]
    fn test_amd_device_memory_usage_percent_zero_vram() {
        let device = AmdDevice::new(make_info(0));
        assert_eq!(device.memory_usage_percent(), 0.0);
    }

    #[test]
    fn test_amd_device_memory_usage_percent_after_allocate() {
        let device = AmdDevice::new(make_info(1024));
        assert!(device.allocate(256));
        assert!((device.memory_usage_percent() - 25.0).abs() < 0.001);
    }

    #[test]
    fn test_amd_device_allocate_success() {
        let device = AmdDevice::new(make_info(1024));
        assert!(device.allocate(512));
        assert_eq!(device.available_memory(), 512);
    }

    #[test]
    fn test_amd_device_allocate_exceeds_vram() {
        let device = AmdDevice::new(make_info(1024));
        assert!(device.allocate(512));
        assert!(!device.allocate(1024));
    }

    #[test]
    fn test_amd_device_deallocate() {
        let device = AmdDevice::new(make_info(1024));
        assert!(device.allocate(512));
        device.deallocate(256);
        assert_eq!(device.available_memory(), 768);
    }

    #[test]
    fn test_amd_device_deallocate_saturating() {
        let device = AmdDevice::new(make_info(1024));
        device.deallocate(2048);
        assert_eq!(device.available_memory(), 1024);
    }

    #[test]
    fn test_amd_device_is_busy_set_busy() {
        let device = AmdDevice::new(make_info(1024));
        assert!(!device.is_busy());
        device.set_busy(true);
        assert!(device.is_busy());
        device.set_busy(false);
        assert!(!device.is_busy());
    }

    #[test]
    fn test_amd_device_supports_precision() {
        let device = AmdDevice::new(make_info(1024));
        assert!(device.supports_precision("fp32"));
        assert!(device.supports_precision("fp16"));
        assert!(device.supports_precision("bf16"));
        assert!(!device.supports_precision("int8"));
        assert!(!device.supports_precision("fp64"));
    }

    #[test]
    fn test_amd_device_supports_operation() {
        let device = AmdDevice::new(make_info(1024));
        assert!(device.supports_operation("matrix_multiply"));
        assert!(device.supports_operation("convolution"));
        assert!(device.supports_operation("activation"));
        assert!(device.supports_operation("normalization"));
        assert!(device.supports_operation("reduction"));
        assert!(!device.supports_operation("unknown"));
    }

    #[test]
    fn test_amd_device_clone_preserves_state() {
        let device = AmdDevice::new(make_info(1024));
        assert!(device.allocate(128));
        device.set_busy(true);

        let cloned = device.clone();
        assert_eq!(cloned.name(), device.name());
        assert_eq!(cloned.vram_bytes(), device.vram_bytes());
        assert_eq!(cloned.compute_units(), device.compute_units());
        assert_eq!(cloned.available_memory(), device.available_memory());
        assert!(cloned.is_busy());
    }

    #[tokio::test]
    async fn test_amd_device_manager_new_default() {
        let manager = AmdDeviceManager::new();
        assert!(!manager.is_initialized());
        assert!(!manager.is_opencl_available());
        assert!(!manager.is_rocm_available());
        assert_eq!(manager.device_count().await, 0);

        let default_mgr = AmdDeviceManager::default();
        assert!(!default_mgr.is_initialized());
    }

    #[tokio::test]
    async fn test_amd_device_manager_initialize_finds_rocm_devices() {
        let manager = AmdDeviceManager::new();
        manager
            .initialize()
            .await
            .expect("initialize should succeed");

        assert!(manager.is_initialized());
        // 设备数量始终为 4(ROCm 可用时用 ROCm,不可用时 fallback 到 OpenCL)
        assert_eq!(manager.device_count().await, 4);
        let total = manager.total_vram().await;
        assert!(total > 0);
    }

    #[tokio::test]
    async fn test_amd_device_manager_initialize_idempotent() {
        let manager = AmdDeviceManager::new();
        manager.initialize().await.unwrap();
        let count_after_first = manager.device_count().await;

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

        assert_eq!(count_after_first, count_after_second);
    }

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

        let primary = manager.primary_device().await;
        // 初始化后应有主设备(ROCm 或 OpenCL)
        assert!(primary.is_some());
        let name = primary.as_ref().unwrap().name();
        assert!(name.contains("ROCm") || name.contains("OpenCL"));
    }

    #[tokio::test]
    async fn test_amd_device_manager_primary_device_none_when_empty() {
        let manager = AmdDeviceManager::new();
        let primary = manager.primary_device().await;
        assert!(primary.is_none());
    }

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

        let device = manager.get_device(1).await;
        assert!(device.is_some());
        assert!(device.as_ref().unwrap().name().contains("Device 1"));
    }

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

        let device = manager.get_device(100).await;
        assert!(device.is_none());
    }

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

        let available = manager.available_vram().await;
        assert_eq!(available, manager.total_vram().await);
    }

    #[tokio::test]
    async fn test_amd_device_manager_memory_usage_summary_empty() {
        let manager = AmdDeviceManager::new();
        let summary = manager.memory_usage_summary().await;
        assert!(summary.contains("0 bytes used"));
        assert!(summary.contains("0.0%"));
    }

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

        let summary = manager.memory_usage_summary().await;
        assert!(summary.contains("AMD GPU Memory"));
        assert!(summary.contains("0.0%"));
    }

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

        let count = manager.device_count().await;
        if count > 0 {
            assert!(manager.set_primary(0).await);
            let primary = manager.primary_device().await;
            assert!(primary.is_some());
        }
    }

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

        assert!(!manager.set_primary(100).await);
    }

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

        let primary = manager.primary_device().await.unwrap();
        assert!(primary.allocate(1024));
        assert!(!primary.is_busy());
        primary.set_busy(true);

        manager.reset().await;

        let primary_after = manager.primary_device().await.unwrap();
        assert_eq!(primary_after.available_memory(), primary_after.vram_bytes());
        assert!(!primary_after.is_busy());
    }

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

        let devices = manager.devices().await;
        assert!(!devices.is_empty());
    }

    #[tokio::test]
    async fn test_create_amd_device_manager() {
        let _path_guard = PATH_MUTEX.lock().await;
        let manager = create_amd_device_manager()
            .await
            .expect("create_amd_device_manager should succeed");
        assert!(manager.is_initialized());
        assert!(manager.device_count().await > 0);
    }

    #[tokio::test]
    async fn test_amd_device_manager_primary_device_index_out_of_range_falls_back() {
        // 与 mock rocm-smi 测试共享 PATH 全局态:必须持锁,否则 mock 注入的
        // PATH 会令 initialize() 凭空枚举出伪设备(实测 Device 2)
        let _path_guard = PATH_MUTEX.lock().await;
        let manager = AmdDeviceManager::new();
        manager.initialize().await.unwrap();

        manager.set_primary(100).await;
        let primary = manager.primary_device().await;
        // 机器封闭化:initialize() 枚举真实系统状态(WSL2/CI 无 AMD GPU 时
        // 设备表可能为空)。越界 set_primary 的契约是"不 panic 且返回安全值":
        // 有设备 → 回退 Device 0;无设备 → primary 为 None。
        if manager.device_count().await > 0 {
            assert!(primary.is_some());
            assert!(primary.as_ref().unwrap().name().contains("Device 0"));
        } else {
            assert!(
                primary.is_none(),
                "no devices enumerated → primary must be None"
            );
        }
    }

    #[test]
    fn test_extract_number_from_line_basic() {
        assert_eq!(extract_number_from_line("16384"), Some(16384));
    }

    #[test]
    fn test_extract_number_from_line_with_prefix() {
        assert_eq!(
            extract_number_from_line("VRAM Total: 16384 MiB"),
            Some(16384)
        );
    }

    #[test]
    fn test_extract_number_from_line_with_trailing_text() {
        assert_eq!(extract_number_from_line("16384 MiB"), Some(16384));
    }

    #[test]
    fn test_extract_number_from_line_no_digits() {
        assert_eq!(extract_number_from_line("no numbers here"), None);
    }

    #[test]
    fn test_extract_number_from_line_empty() {
        assert_eq!(extract_number_from_line(""), None);
    }

    #[test]
    fn test_extract_number_from_line_multiple_numbers() {
        // Should extract the first contiguous digit sequence only
        assert_eq!(extract_number_from_line("card0: 16384 8192"), Some(0));
        // First digit sequence is the actual number
        assert_eq!(extract_number_from_line("16384 8192"), Some(16384));
    }

    #[test]
    fn test_extract_number_from_line_large_number() {
        assert_eq!(
            extract_number_from_line("VRAM Total Memory (MiB): 16384"),
            Some(16384)
        );
    }

    #[test]
    fn test_query_rocm_vram_fallback_when_rocm_smi_unavailable() {
        // rocm-smi 不可用时应返回 DEFAULT_ROCM_VRAM_BYTES
        let vram = query_rocm_vram(0);
        if std::process::Command::new("rocm-smi").output().is_err() {
            assert_eq!(vram, DEFAULT_ROCM_VRAM_BYTES);
        }
    }

    #[test]
    fn test_query_rocm_version_returns_none_without_rocm_smi() {
        // rocm-smi 不可用时应返回 None
        if std::process::Command::new("rocm-smi").output().is_err() {
            assert_eq!(query_rocm_version(), None);
        }
    }

    #[test]
    fn test_query_amd_driver_version_fallback() {
        let version = query_amd_driver_version();
        // 在无 amdgpu sysfs 和 rocm-smi 的环境中应返回 fallback
        let has_sysfs = std::fs::read_to_string("/sys/module/amdgpu/version")
            .map(|v| !v.trim().is_empty())
            .unwrap_or(false);
        let has_rocm_smi = std::process::Command::new("rocm-smi")
            .arg("--showdriverversion")
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false);
        if !has_sysfs && !has_rocm_smi {
            assert_eq!(version, DEFAULT_DRIVER_VERSION);
        }
    }

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

        // Allocate memory on first device
        let device = manager.get_device(0).await.unwrap();
        assert!(device.allocate(1024 * 1024));

        let summary = manager.memory_usage_summary().await;
        assert!(summary.contains("AMD GPU Memory"));
        // Should show some usage after allocation
        assert!(summary.contains("bytes used"));
    }

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

        let total_before = manager.total_vram().await;
        let available_before = manager.available_vram().await;
        assert_eq!(total_before, available_before);

        // Allocate on first device
        let device = manager.get_device(0).await.unwrap();
        assert!(device.allocate(1024));

        let available_after = manager.available_vram().await;
        assert_eq!(available_after, available_before - 1024);
    }

    #[test]
    fn test_from_opencl_different_indices() {
        let d0 = AmdDevice::from_opencl(0).unwrap();
        let d3 = AmdDevice::from_opencl(3).unwrap();
        assert!(d0.name().contains("Device 0"));
        assert!(d3.name().contains("Device 3"));
        assert_eq!(d0.info().device_id, 0);
        assert_eq!(d3.info().device_id, 3);
    }

    #[test]
    fn test_amd_device_allocate_concurrent_safety() {
        let device = AmdDevice::new(make_info(1024));
        // First allocation succeeds
        assert!(device.allocate(600));
        // Second allocation that would exceed VRAM fails
        assert!(!device.allocate(600));
        // But a smaller one that fits should succeed
        assert!(device.allocate(424));
    }

    #[test]
    fn test_amd_device_memory_usage_percent_with_various_vram() {
        let device = AmdDevice::new(make_info(2048));
        assert_eq!(device.memory_usage_percent(), 0.0);
        assert!(device.allocate(1024));
        assert!((device.memory_usage_percent() - 50.0).abs() < 0.001);
    }

    /// 创建 mock rocm-smi 脚本并返回其所在目录
    fn create_mock_rocm_smi() -> tempfile::TempDir {
        let temp_dir = tempfile::tempdir().unwrap();
        let script_path = temp_dir.path().join("rocm-smi");
        std::fs::write(
            &script_path,
            r#"#!/bin/bash
if echo "$@" | grep -q "showmeminfo"; then
    echo "  VRAM Total Memory (MiB): 16384"
elif echo "$@" | grep -q "showdriverversion"; then
    echo "ROCm version: 6.0.0"
    echo "Driver version: 24.0.0"
elif echo "$@" | grep -q "version"; then
    echo "rocm-smi 6.0.0"
else
    echo "AMD GPU"
fi
"#,
        )
        .unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&script_path, std::fs::Permissions::from_mode(0o755)).unwrap();
        }
        temp_dir
    }

    fn prepend_path(dir: &std::path::Path) {
        let current = std::env::var("PATH").unwrap_or_default();
        // SAFETY: 测试中使用 mutex 序列化,无并发访问
        unsafe { std::env::set_var("PATH", format!("{}:{}", dir.display(), current)) };
    }

    fn restore_path(original: &str) {
        // SAFETY: 测试中使用 mutex 序列化,无并发访问
        unsafe { std::env::set_var("PATH", original) };
    }

    // 序列化 PATH 修改,避免并行测试干扰
    // tokio Mutex:异步测试需跨 .await 持锁(std MutexGuard 跨 await 会触发
    // clippy::await_holding_lock 且阻塞其他运行时线程)
    static PATH_MUTEX: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

    #[tokio::test]
    async fn test_query_rocm_vram_with_mock() {
        let _guard = PATH_MUTEX.lock().await;
        let original_path = std::env::var("PATH").unwrap_or_default();
        let temp = create_mock_rocm_smi();
        prepend_path(temp.path());

        let vram = query_rocm_vram(0);
        assert_eq!(vram, 16384 * 1024 * 1024);

        restore_path(&original_path);
    }

    #[tokio::test]
    async fn test_query_rocm_version_with_mock() {
        let _guard = PATH_MUTEX.lock().await;
        let original_path = std::env::var("PATH").unwrap_or_default();
        let temp = create_mock_rocm_smi();
        prepend_path(temp.path());

        let version = query_rocm_version();
        assert_eq!(version, Some("6.0.0".to_string()));

        restore_path(&original_path);
    }

    #[tokio::test]
    async fn test_query_amd_driver_version_with_mock() {
        let _guard = PATH_MUTEX.lock().await;
        let original_path = std::env::var("PATH").unwrap_or_default();
        let temp = create_mock_rocm_smi();
        prepend_path(temp.path());

        // 跳过 sysfs (此系统无 amdgpu 模块)
        let has_sysfs = std::fs::read_to_string("/sys/module/amdgpu/version")
            .map(|v| !v.trim().is_empty())
            .unwrap_or(false);

        let version = query_amd_driver_version();
        if !has_sysfs {
            assert_eq!(version, "24.0.0");
        }

        restore_path(&original_path);
    }

    #[tokio::test]
    async fn test_from_rocm_with_mock() {
        let _guard = PATH_MUTEX.lock().await;
        let original_path = std::env::var("PATH").unwrap_or_default();
        let temp = create_mock_rocm_smi();
        prepend_path(temp.path());

        let device = AmdDevice::from_rocm(0).expect("from_rocm should return Some with mock");
        assert!(device.info().is_available);
        assert_eq!(device.device_type(), DeviceType::Amd);
        assert!(device.name().contains("ROCm"));
        assert!(device.name().contains("Device 0"));
        assert_eq!(device.vram_bytes(), 16384 * 1024 * 1024);
        assert!(device.info().roc_version.is_some());

        restore_path(&original_path);
    }

    #[tokio::test]
    async fn test_initialize_with_mock_rocm_smi() {
        let _guard = PATH_MUTEX.lock().await;
        let original_path = std::env::var("PATH").unwrap_or_default();
        let temp = create_mock_rocm_smi();
        prepend_path(temp.path());

        let manager = AmdDeviceManager::new();
        manager.initialize().await.unwrap();

        assert!(manager.is_initialized());
        assert!(manager.is_rocm_available());
        assert_eq!(manager.device_count().await, 4);

        let total = manager.total_vram().await;
        assert_eq!(total, 4 * 16384 * 1024 * 1024u64);

        restore_path(&original_path);
    }

    #[tokio::test]
    async fn test_create_amd_device_manager_with_mock() {
        let _guard = PATH_MUTEX.lock().await;
        let original_path = std::env::var("PATH").unwrap_or_default();
        let temp = create_mock_rocm_smi();
        prepend_path(temp.path());

        let manager = create_amd_device_manager().await.unwrap();
        assert!(manager.is_initialized());
        assert!(manager.is_rocm_available());

        restore_path(&original_path);
    }
}