hive-gpu 0.2.0

High-performance GPU acceleration for vector operations with Device Info API (Metal, CUDA, ROCm)
Documentation
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
//! GPU Hardware Detection Tests
//!
//! Comprehensive tests for GPU hardware detection across all backends:
//! - Metal (macOS)
//! - CUDA (Linux/Windows)
//! - ROCm (Linux)
//! - CPU fallback

#[cfg(all(target_os = "macos", feature = "metal-native"))]
mod metal_detection_tests {
    use hive_gpu::error::HiveGpuError;
    use hive_gpu::metal::MetalNativeContext;
    use hive_gpu::traits::GpuContext;

    #[test]
    fn test_metal_device_availability() {
        // Test that Metal backend can check device availability
        let result = MetalNativeContext::new();

        match result {
            Ok(context) => {
                println!("✅ Metal device available");
                println!("   Context created successfully");

                // Verify context is functional
                let info = context.device_info().expect("Failed to get device info");
                assert!(!info.name.is_empty(), "Device name should not be empty");
                assert_eq!(info.backend, "Metal", "Backend should be Metal");
            }
            Err(HiveGpuError::NoDeviceAvailable) => {
                println!("⚠️  Metal device not available on this system");
                // This is acceptable - not all systems have Metal
            }
            Err(e) => {
                panic!("Unexpected error during Metal detection: {}", e);
            }
        }
    }

    #[test]
    fn test_metal_device_name_retrieval() {
        let context = match MetalNativeContext::new() {
            Ok(ctx) => ctx,
            Err(HiveGpuError::NoDeviceAvailable) => {
                println!("⚠️  Metal not available, skipping test");
                return;
            }
            Err(e) => panic!("Failed to create Metal context: {}", e),
        };

        let info = context.device_info().expect("Failed to get device info");

        // Verify device name
        assert!(!info.name.is_empty(), "Device name should not be empty");
        println!("✅ Metal device name: {}", info.name);

        // Common Apple Silicon names
        let valid_prefixes = ["Apple", "AMD", "Intel"];
        let has_valid_prefix = valid_prefixes
            .iter()
            .any(|prefix| info.name.starts_with(prefix));
        assert!(
            has_valid_prefix,
            "Device name should start with known vendor: {}",
            info.name
        );
    }

    #[test]
    fn test_metal_device_capabilities() {
        let context = match MetalNativeContext::new() {
            Ok(ctx) => ctx,
            Err(HiveGpuError::NoDeviceAvailable) => {
                println!("⚠️  Metal not available, skipping test");
                return;
            }
            Err(e) => panic!("Failed to create Metal context: {}", e),
        };

        let info = context.device_info().expect("Failed to get device info");

        // Verify capabilities are populated
        assert!(
            info.max_threads_per_block > 0,
            "Max threads should be positive"
        );
        assert!(
            info.max_shared_memory_per_block > 0,
            "Max shared memory should be positive"
        );
        assert!(info.total_vram_bytes > 0, "Total VRAM should be positive");

        println!("✅ Metal capabilities:");
        println!("   Max threads/block: {}", info.max_threads_per_block);
        println!(
            "   Max shared memory: {} KB",
            info.max_shared_memory_per_block / 1024
        );
        println!("   Total VRAM: {} MB", info.total_vram_mb());

        // Sanity checks for Apple Silicon
        // Apple Silicon typically has:
        // - Max threads: 512-1024
        // - Shared memory: 32KB
        // - VRAM: 8GB-128GB (unified memory)
        if info.name.contains("Apple") {
            assert!(
                info.max_threads_per_block >= 512,
                "Apple Silicon should support at least 512 threads per block"
            );
            assert!(
                info.max_shared_memory_per_block >= 16 * 1024,
                "Apple Silicon should have at least 16KB shared memory"
            );
        }
    }

    #[test]
    fn test_metal_multiple_contexts() {
        // Test creating multiple Metal contexts (should work)
        let context1 = match MetalNativeContext::new() {
            Ok(ctx) => ctx,
            Err(HiveGpuError::NoDeviceAvailable) => {
                println!("⚠️  Metal not available, skipping test");
                return;
            }
            Err(e) => panic!("Failed to create first Metal context: {}", e),
        };

        let context2 = match MetalNativeContext::new() {
            Ok(ctx) => ctx,
            Err(e) => panic!("Failed to create second Metal context: {}", e),
        };

        // Both contexts should be functional
        let info1 = context1
            .device_info()
            .expect("Failed to get info from context 1");
        let info2 = context2
            .device_info()
            .expect("Failed to get info from context 2");

        // Should refer to the same device
        assert_eq!(
            info1.name, info2.name,
            "Both contexts should use same device"
        );
        assert_eq!(
            info1.backend, info2.backend,
            "Both should use Metal backend"
        );

        println!("✅ Multiple Metal contexts created successfully");
        println!("   Context 1: {}", info1.name);
        println!("   Context 2: {}", info2.name);
    }

    #[test]
    fn test_metal_vram_query() {
        let context = match MetalNativeContext::new() {
            Ok(ctx) => ctx,
            Err(HiveGpuError::NoDeviceAvailable) => {
                println!("⚠️  Metal not available, skipping test");
                return;
            }
            Err(e) => panic!("Failed to create Metal context: {}", e),
        };

        let info = context.device_info().expect("Failed to get device info");

        // Verify VRAM information is consistent
        assert!(info.total_vram_bytes > 0, "Total VRAM should be positive");
        assert!(
            info.available_vram_bytes <= info.total_vram_bytes,
            "Available VRAM should not exceed total"
        );
        assert!(
            info.used_vram_bytes == info.total_vram_bytes - info.available_vram_bytes,
            "Used VRAM should equal total - available"
        );

        println!("✅ VRAM information:");
        println!("   Total: {} MB", info.total_vram_mb());
        println!("   Available: {} MB", info.available_vram_mb());
        println!("   Used: {} MB", info.used_vram_bytes / (1024 * 1024));
        println!("   Usage: {:.1}%", info.vram_usage_percent());
    }
}

/*
// CUDA tests - will be uncommented when CUDA backend is implemented

#[cfg(feature = "cuda")]
mod cuda_detection_tests {
    use hive_gpu::cuda::CudaContext;
    use hive_gpu::traits::GpuContext;

    #[test]
    fn test_cuda_device_availability() {
        // Test CUDA backend availability check
        let is_available = CudaContext::is_available();

        if is_available {
            println!("✅ CUDA is available on this system");

            // Try to create context
            let result = CudaContext::new();
            assert!(
                result.is_ok(),
                "CUDA context creation should succeed when available"
            );

            let context = result.unwrap();
            let info = context.device_info().expect("Failed to get device info");
            assert_eq!(info.backend, "CUDA", "Backend should be CUDA");
            println!("   CUDA device: {}", info.name);
        } else {
            println!("⚠️  CUDA not available on this system");

            // Verify that context creation fails gracefully
            let result = CudaContext::new();
            assert!(
                result.is_err(),
                "CUDA context should fail when not available"
            );
        }
    }

    #[test]
    fn test_cuda_device_enumeration() {
        if !CudaContext::is_available() {
            println!("⚠️  CUDA not available, skipping test");
            return;
        }

        // Test enumerating CUDA devices
        let device_count = CudaContext::device_count().expect("Failed to get device count");
        assert!(device_count > 0, "Should have at least one CUDA device");

        println!("✅ CUDA devices found: {}", device_count);

        // Get info for each device
        for i in 0..device_count {
            let context = CudaContext::with_device(i).expect("Failed to create context for device");
            let info = context.device_info().expect("Failed to get device info");

            println!("   Device {}: {}", i, info.name);
            println!("      Compute Capability: {:?}", info.compute_capability);
            println!("      VRAM: {} MB", info.total_vram_mb());
        }
    }

    #[test]
    fn test_cuda_compute_capability() {
        if !CudaContext::is_available() {
            println!("⚠️  CUDA not available, skipping test");
            return;
        }

        let context = CudaContext::new().expect("Failed to create CUDA context");
        let info = context.device_info().expect("Failed to get device info");

        // CUDA should expose compute capability
        assert!(
            info.compute_capability.is_some(),
            "CUDA should report compute capability"
        );

        let cc = info.compute_capability.unwrap();
        println!("✅ CUDA compute capability: {}", cc);

        // Parse compute capability (format: "X.Y")
        let parts: Vec<&str> = cc.split('.').collect();
        assert_eq!(parts.len(), 2, "Compute capability should have format X.Y");

        let major: u32 = parts[0].parse().expect("Major version should be a number");
        let minor: u32 = parts[1].parse().expect("Minor version should be a number");

        // Modern CUDA devices should have at least compute capability 3.5
        assert!(
            major >= 3,
            "CUDA device should have compute capability >= 3.0"
        );
        println!("   Major: {}, Minor: {}", major, minor);
    }

    #[test]
    fn test_cuda_pci_bus_id() {
        if !CudaContext::is_available() {
            println!("⚠️  CUDA not available, skipping test");
            return;
        }

        let context = CudaContext::new().expect("Failed to create CUDA context");
        let info = context.device_info().expect("Failed to get device info");

        // CUDA should expose PCI bus ID
        assert!(info.pci_bus_id.is_some(), "CUDA should report PCI bus ID");

        let pci = info.pci_bus_id.unwrap();
        println!("✅ CUDA PCI bus ID: {}", pci);

        // PCI bus ID should match format: "0000:XX:YY.Z"
        assert!(
            pci.contains(':') && pci.contains('.'),
            "PCI bus ID should have standard format"
        );
    }
}
*/

/*
// ROCm tests - will be uncommented when ROCm backend is implemented

#[cfg(feature = "rocm")]
mod rocm_detection_tests {
    use hive_gpu::rocm::RocmContext;
    use hive_gpu::traits::GpuContext;

    #[test]
    fn test_rocm_device_availability() {
        // Test ROCm backend availability check
        let is_available = RocmContext::is_available();

        if is_available {
            println!("✅ ROCm is available on this system");

            let result = RocmContext::new();
            assert!(
                result.is_ok(),
                "ROCm context creation should succeed when available"
            );

            let context = result.unwrap();
            let info = context.device_info().expect("Failed to get device info");
            assert_eq!(info.backend, "ROCm", "Backend should be ROCm");
            println!("   ROCm device: {}", info.name);
        } else {
            println!("⚠️  ROCm not available on this system");
        }
    }

    #[test]
    fn test_rocm_device_enumeration() {
        if !RocmContext::is_available() {
            println!("⚠️  ROCm not available, skipping test");
            return;
        }

        let device_count = RocmContext::device_count().expect("Failed to get device count");
        assert!(device_count > 0, "Should have at least one ROCm device");

        println!("✅ ROCm devices found: {}", device_count);
    }

    #[test]
    fn test_rocm_architecture() {
        if !RocmContext::is_available() {
            println!("⚠️  ROCm not available, skipping test");
            return;
        }

        let context = RocmContext::new().expect("Failed to create ROCm context");
        let info = context.device_info().expect("Failed to get device info");

        // ROCm should expose architecture (e.g., "gfx1030")
        assert!(
            info.compute_capability.is_some(),
            "ROCm should report architecture"
        );

        let arch = info.compute_capability.unwrap();
        println!("✅ ROCm architecture: {}", arch);
        assert!(
            arch.starts_with("gfx"),
            "ROCm architecture should start with 'gfx'"
        );
    }
}
*/

/// Fallback tests - these run regardless of GPU availability
mod fallback_tests {
    use hive_gpu::backends::detector::{
        GpuBackendType, detect_available_backends, select_best_backend,
    };

    #[test]
    fn test_backend_detection() {
        // Test that backend detection works
        let backends = detect_available_backends();

        println!("✅ Detected backends: {:?}", backends);
        assert!(
            !backends.is_empty(),
            "Should detect at least one backend (CPU)"
        );

        // Backend should include at least CPU
        assert!(
            backends.contains(&GpuBackendType::Cpu),
            "CPU should always be available as fallback"
        );

        // Print all detected backends
        for backend in &backends {
            match backend {
                GpuBackendType::Metal => {
                    println!("   Metal backend available");
                    #[cfg(not(target_os = "macos"))]
                    panic!("Metal should only be detected on macOS");
                }
                GpuBackendType::Cuda => {
                    println!("   CUDA backend available");
                }
                GpuBackendType::Rocm => {
                    println!("   ROCm backend available");
                }
                GpuBackendType::Intel => {
                    println!("   Intel backend available");
                }
                GpuBackendType::Cpu => {
                    println!("   CPU backend available");
                }
            }
        }
    }

    #[test]
    fn test_best_backend_selection() {
        // Test selecting the best available backend
        let best = select_best_backend().expect("Should always find a backend");

        println!("✅ Best backend selected: {:?}", best);

        // Best backend should be one of the detected ones
        let available = detect_available_backends();
        assert!(
            available.contains(&best),
            "Best backend should be in available backends"
        );

        // Priority should be: Metal > CUDA > CPU
        #[cfg(all(target_os = "macos", feature = "metal-native"))]
        {
            if available.contains(&GpuBackendType::Metal) {
                assert_eq!(
                    best,
                    GpuBackendType::Metal,
                    "Metal should be preferred on macOS"
                );
            }
        }
    }

    #[test]
    fn test_graceful_fallback_no_gpu() {
        // This test ensures graceful behavior when no GPU is available

        println!("✅ Testing graceful fallback behavior");

        // Should always be able to select a backend (CPU fallback)
        let backend = select_best_backend().expect("Should fallback to CPU if no GPU");

        // Should always return a valid backend
        println!("   Fallback backend: {:?}", backend);

        // Get backend info
        let info = hive_gpu::backends::detector::get_backend_info(backend);
        if let Ok(info_str) = info {
            println!("   Info: {}", info_str);
        }
    }

    #[test]
    fn test_backend_performance_info() {
        // Test getting performance characteristics for each backend
        let backends = detect_available_backends();

        println!("✅ Backend performance characteristics:");
        for backend in backends {
            let perf = hive_gpu::backends::detector::get_backend_performance_info(backend);
            println!("   {} ({}):", perf.name, backend);
            println!(
                "      Memory bandwidth: {:.1} GB/s",
                perf.memory_bandwidth_gbps
            );
            println!("      Compute units: {}", perf.compute_units);
            println!("      Memory size: {} GB", perf.memory_size_gb);
            println!("      HNSW support: {}", perf.supports_hnsw);
            println!("      Batch support: {}", perf.supports_batch);
        }
    }
}