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
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
//! GPU Stress Tests
//!
//! Tests that push the system to its limits:
//! - Sustained load over time
//! - Maximum vector count handling
//! - Memory pressure scenarios
//! - Rapid allocation/deallocation cycles
//! - Concurrent high-load operations

#[cfg(all(target_os = "macos", feature = "metal-native"))]
mod metal_stress_tests {
    use hive_gpu::error::HiveGpuError;
    use hive_gpu::metal::MetalNativeContext;
    use hive_gpu::traits::GpuContext;
    use hive_gpu::types::{GpuDistanceMetric, GpuVector};
    use std::time::{Duration, Instant};

    /// Helper to create test vectors
    fn create_test_vectors(count: usize, dimension: usize, offset: usize) -> Vec<GpuVector> {
        (0..count)
            .map(|i| {
                let data: Vec<f32> = (0..dimension)
                    .map(|d| ((offset + i) * dimension + d) as f32)
                    .collect();
                GpuVector::new(format!("vec_{}_{}", offset, i), data)
            })
            .collect()
    }

    #[test]
    fn test_sustained_load() {
        // Test sustained high-load operations over time
        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 dimension = 256;
        let vectors_per_batch = 500;
        let num_batches = 10;
        let duration_target = Duration::from_secs(5);

        println!("✅ Sustained Load Test:");
        println!("   Duration: {:?}", duration_target);
        println!("   Batches: {}", num_batches);
        println!("   Vectors per batch: {}", vectors_per_batch);
        println!();

        let start = Instant::now();
        let mut total_vectors = 0;
        let mut batch_times = Vec::new();

        for batch in 0..num_batches {
            if start.elapsed() >= duration_target {
                break;
            }

            let vectors =
                create_test_vectors(vectors_per_batch, dimension, batch * vectors_per_batch);
            let mut storage = context
                .create_storage(dimension, GpuDistanceMetric::Cosine)
                .expect("Failed to create storage");

            let batch_start = Instant::now();
            storage
                .add_vectors(&vectors)
                .expect("Failed to add vectors");
            let batch_time = batch_start.elapsed();

            batch_times.push(batch_time);
            total_vectors += vectors_per_batch;

            if batch % 2 == 0 {
                println!(
                    "   Batch {}: {:?} ({} vectors)",
                    batch + 1,
                    batch_time,
                    total_vectors
                );
            }
        }

        let total_time = start.elapsed();
        let avg_batch_time = batch_times.iter().sum::<Duration>() / batch_times.len() as u32;
        let throughput = total_vectors as f64 / total_time.as_secs_f64();

        println!();
        println!("   Total vectors processed: {}", total_vectors);
        println!("   Total time: {:?}", total_time);
        println!("   Avg batch time: {:?}", avg_batch_time);
        println!("   Throughput: {:.2} vectors/sec", throughput);
        println!("   ✅ Sustained high load completed successfully");

        // Verify system remained stable under load
        assert!(
            total_vectors >= vectors_per_batch * 5,
            "Should process at least 5 batches"
        );
    }

    #[test]
    fn test_maximum_vector_count() {
        // Test handling of very large vector counts
        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 dimension = 128;
        let target_count = 10_000; // 10k vectors

        println!("✅ Maximum Vector Count Test:");
        println!("   Target: {} vectors × {}D", target_count, dimension);
        println!();

        let mut storage = context
            .create_storage(dimension, GpuDistanceMetric::Cosine)
            .expect("Failed to create storage");

        let batch_size = 1000;
        let num_batches = target_count / batch_size;
        let start = Instant::now();

        for batch in 0..num_batches {
            let vectors = create_test_vectors(batch_size, dimension, batch * batch_size);
            storage
                .add_vectors(&vectors)
                .expect("Failed to add vectors");

            if batch % 2 == 0 {
                let elapsed = start.elapsed();
                let processed = (batch + 1) * batch_size;
                println!(
                    "   Progress: {}/{} vectors ({:?})",
                    processed, target_count, elapsed
                );
            }
        }

        let total_time = start.elapsed();
        let throughput = target_count as f64 / total_time.as_secs_f64();

        println!();
        println!("   Total time: {:?}", total_time);
        println!("   Throughput: {:.2} vectors/sec", throughput);
        println!("   ✅ Successfully handled {} vectors", target_count);

        // Verify search still works with large dataset
        let query = vec![1.0; dimension];
        let results = storage.search(&query, 10).expect("Search should work");
        assert_eq!(
            results.len(),
            10,
            "Should return 10 results from large dataset"
        );
    }

    #[test]
    fn test_memory_pressure() {
        // Test behavior under memory pressure
        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),
        };

        println!("✅ Memory Pressure Test:");

        let device_info = context.device_info().expect("Failed to get device info");
        let total_vram = device_info.total_vram_bytes;
        let initial_available = device_info.available_vram_bytes;

        println!(
            "   Total VRAM: {:.2} GB",
            total_vram as f64 / 1024.0 / 1024.0 / 1024.0
        );
        println!(
            "   Initially available: {:.2} GB",
            initial_available as f64 / 1024.0 / 1024.0 / 1024.0
        );
        println!();

        // Try to allocate increasingly large amounts
        let dimension = 512;
        let sizes = vec![1000, 2000, 5000, 10000];

        for size in sizes {
            let data_size_mb = (size * dimension * 4) as f64 / 1024.0 / 1024.0;
            let vectors = create_test_vectors(size, dimension, 0);

            println!("   Attempting {} vectors ({:.2} MB)...", size, data_size_mb);

            let mut storage = context
                .create_storage(dimension, GpuDistanceMetric::Cosine)
                .expect("Failed to create storage");

            match storage.add_vectors(&vectors) {
                Ok(_) => {
                    let device_info = context.device_info().expect("Failed to get device info");
                    let used_vram = device_info.total_vram_bytes - device_info.available_vram_bytes;
                    println!("      ✅ Allocated successfully");
                    println!(
                        "      VRAM used: {:.2} MB",
                        used_vram as f64 / 1024.0 / 1024.0
                    );
                }
                Err(e) => {
                    println!("      ⚠️  Allocation failed: {}", e);
                    // It's okay to fail under extreme memory pressure
                    break;
                }
            }
        }

        println!();
        println!("   ✅ Memory pressure handling validated");
    }

    #[test]
    fn test_rapid_allocation_cycles() {
        // Test rapid allocation/deallocation cycles
        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 dimension = 256;
        let vectors_per_cycle = 200;
        let num_cycles = 50;

        println!("✅ Rapid Allocation Cycles Test:");
        println!("   Cycles: {}", num_cycles);
        println!("   Vectors per cycle: {}", vectors_per_cycle);
        println!();

        let start = Instant::now();
        let mut cycle_times = Vec::new();

        for cycle in 0..num_cycles {
            let vectors =
                create_test_vectors(vectors_per_cycle, dimension, cycle * vectors_per_cycle);
            let mut storage = context
                .create_storage(dimension, GpuDistanceMetric::Cosine)
                .expect("Failed to create storage");

            let cycle_start = Instant::now();
            storage
                .add_vectors(&vectors)
                .expect("Failed to add vectors");
            // Storage dropped here (deallocation)
            let cycle_time = cycle_start.elapsed();

            cycle_times.push(cycle_time);

            if cycle % 10 == 0 && cycle > 0 {
                let avg = cycle_times.iter().sum::<Duration>() / cycle_times.len() as u32;
                println!("   After {} cycles: avg {:?}/cycle", cycle, avg);
            }
        }

        let total_time = start.elapsed();
        let avg_cycle_time = cycle_times.iter().sum::<Duration>() / cycle_times.len() as u32;
        let min_cycle_time = cycle_times.iter().min().unwrap();
        let max_cycle_time = cycle_times.iter().max().unwrap();

        println!();
        println!("   Total time: {:?}", total_time);
        println!("   Avg cycle: {:?}", avg_cycle_time);
        println!("   Min cycle: {:?}", min_cycle_time);
        println!("   Max cycle: {:?}", max_cycle_time);
        println!("   ✅ All cycles completed successfully");

        // Verify performance didn't degrade too much
        // Some variation is expected due to system load and Metal's internal optimizations
        let max_avg_ratio = max_cycle_time.as_secs_f64() / avg_cycle_time.as_secs_f64();
        assert!(
            max_avg_ratio < 5.0,
            "Max cycle time too high vs avg: {:.2}x",
            max_avg_ratio
        );
    }

    #[test]
    fn test_concurrent_high_load() {
        // Test multiple concurrent high-load operations
        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 dimension = 192;
        let vectors_per_storage = 500;
        let num_storages = 10;

        println!("✅ Concurrent High Load Test:");
        println!("   Storages: {}", num_storages);
        println!("   Vectors per storage: {}", vectors_per_storage);
        println!();

        let start = Instant::now();
        let mut storages = Vec::new();

        for i in 0..num_storages {
            let vectors =
                create_test_vectors(vectors_per_storage, dimension, i * vectors_per_storage);
            let mut storage = context
                .create_storage(dimension, GpuDistanceMetric::Cosine)
                .expect("Failed to create storage");

            storage
                .add_vectors(&vectors)
                .expect("Failed to add vectors");
            storages.push(storage);

            if i % 2 == 0 {
                println!("   Created storage {}/{}", i + 1, num_storages);
            }
        }

        let creation_time = start.elapsed();

        // Now perform concurrent searches
        let search_start = Instant::now();
        let query = vec![1.0; dimension];
        let mut total_results = 0;

        for (i, storage) in storages.iter().enumerate() {
            let results = storage.search(&query, 10).expect("Search failed");
            total_results += results.len();

            if i % 2 == 0 {
                println!("   Searched storage {}/{}", i + 1, num_storages);
            }
        }

        let search_time = search_start.elapsed();
        let total_time = start.elapsed();

        println!();
        println!("   Creation time: {:?}", creation_time);
        println!("   Search time: {:?}", search_time);
        println!("   Total time: {:?}", total_time);
        println!("   Total results: {}", total_results);
        println!("   ✅ Concurrent operations completed");

        assert_eq!(
            total_results,
            num_storages * 10,
            "Should get 10 results from each storage"
        );
    }

    #[test]
    fn test_sustained_search_load() {
        // Test sustained search operations
        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 dimension = 256;
        let vector_count = 2000;
        let vectors = create_test_vectors(vector_count, dimension, 0);

        let mut storage = context
            .create_storage(dimension, GpuDistanceMetric::Cosine)
            .expect("Failed to create storage");

        storage
            .add_vectors(&vectors)
            .expect("Failed to add vectors");

        println!("✅ Sustained Search Load Test:");
        println!("   Dataset: {} vectors", vector_count);
        println!("   Search duration: 2 seconds");
        println!();

        let duration = Duration::from_secs(2);
        let query = vec![1.0; dimension];
        let start = Instant::now();
        let mut num_searches = 0;
        let mut total_results = 0;

        while start.elapsed() < duration {
            let results = storage.search(&query, 10).expect("Search failed");
            total_results += results.len();
            num_searches += 1;
        }

        let elapsed = start.elapsed();
        let qps = num_searches as f64 / elapsed.as_secs_f64();

        println!("   Searches performed: {}", num_searches);
        println!("   Total results: {}", total_results);
        println!("   QPS: {:.0} queries/sec", qps);
        println!("   ✅ Sustained search load completed");

        // Should be able to do many searches per second
        assert!(qps > 1000.0, "QPS too low: {:.0}", qps);
    }

    #[test]
    fn test_mixed_workload() {
        // Test mixed read/write workload
        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 dimension = 256;
        let initial_vectors = 1000;
        let num_iterations = 20;
        let vectors_per_iteration = 100;

        println!("✅ Mixed Workload Test:");
        println!("   Initial vectors: {}", initial_vectors);
        println!("   Iterations: {}", num_iterations);
        println!();

        let vectors = create_test_vectors(initial_vectors, dimension, 0);
        let mut storage = context
            .create_storage(dimension, GpuDistanceMetric::Cosine)
            .expect("Failed to create storage");

        storage
            .add_vectors(&vectors)
            .expect("Failed to add vectors");

        let start = Instant::now();
        let query = vec![1.0; dimension];
        let mut total_searches = 0;
        let mut total_additions = 0;

        for i in 0..num_iterations {
            // Perform some searches
            for _ in 0..10 {
                storage.search(&query, 5).expect("Search failed");
                total_searches += 1;
            }

            // Add more vectors
            let new_vectors = create_test_vectors(
                vectors_per_iteration,
                dimension,
                initial_vectors + i * vectors_per_iteration,
            );
            storage
                .add_vectors(&new_vectors)
                .expect("Failed to add vectors");
            total_additions += vectors_per_iteration;

            if i % 5 == 0 {
                println!(
                    "   Iteration {}: {} searches, {} vectors added",
                    i + 1,
                    total_searches,
                    total_additions
                );
            }
        }

        let elapsed = start.elapsed();

        println!();
        println!("   Total time: {:?}", elapsed);
        println!("   Total searches: {}", total_searches);
        println!("   Total additions: {}", total_additions);
        println!("   ✅ Mixed workload completed");

        assert_eq!(total_searches, num_iterations * 10);
        assert_eq!(total_additions, num_iterations * vectors_per_iteration);
    }

    #[test]
    fn test_long_running_stability() {
        // Test stability over extended period
        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 dimension = 256;
        let duration = Duration::from_secs(5); // 5 seconds for CI
        let batch_size = 200;

        println!("✅ Long Running Stability Test:");
        println!("   Duration: {:?}", duration);
        println!();

        let start = Instant::now();
        let mut batches_processed = 0;
        let mut total_vectors = 0;

        while start.elapsed() < duration {
            let vectors =
                create_test_vectors(batch_size, dimension, batches_processed * batch_size);
            let mut storage = context
                .create_storage(dimension, GpuDistanceMetric::Cosine)
                .expect("Failed to create storage");

            storage
                .add_vectors(&vectors)
                .expect("Failed to add vectors");

            // Perform some searches
            let query = vec![1.0; dimension];
            storage.search(&query, 10).expect("Search failed");

            batches_processed += 1;
            total_vectors += batch_size;
        }

        let elapsed = start.elapsed();

        println!("   Batches processed: {}", batches_processed);
        println!("   Total vectors: {}", total_vectors);
        println!("   Elapsed: {:?}", elapsed);
        println!("   ✅ System remained stable");

        assert!(batches_processed >= 5, "Should process at least 5 batches");
    }

    #[test]
    fn test_recovery_after_errors() {
        // Test system recovery after intentional errors
        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 dimension = 256;

        println!("✅ Recovery After Errors Test:");
        println!();

        // Try to add empty vectors (should fail gracefully)
        let mut storage = context
            .create_storage(dimension, GpuDistanceMetric::Cosine)
            .expect("Failed to create storage");

        let empty_vectors: Vec<GpuVector> = vec![];
        match storage.add_vectors(&empty_vectors) {
            Ok(_) => {} // Empty vector addition is allowed
            Err(e) => println!("   Expected error for empty vectors: {}", e),
        }

        // Now add valid vectors (should work)
        let valid_vectors = create_test_vectors(100, dimension, 0);
        storage
            .add_vectors(&valid_vectors)
            .expect("Should work after empty vector attempt");

        println!("   ✅ Added 100 valid vectors after error");

        // Note: Our implementation may accept queries of different dimensions
        // by padding/truncating, so we test successful operations instead

        // Search with correct dimension (should work)
        let query = vec![1.0; dimension];
        let results = storage
            .search(&query, 10)
            .expect("Search should work after error recovery");

        println!("   ✅ Search succeeded: {} results", results.len());

        // Try adding more vectors (should work)
        let more_vectors = create_test_vectors(50, dimension, 100);
        storage
            .add_vectors(&more_vectors)
            .expect("Should continue adding vectors");

        println!("   ✅ Added 50 more vectors");

        // Search again
        let results2 = storage
            .search(&query, 10)
            .expect("Search should still work");

        println!("   ✅ Second search succeeded: {} results", results2.len());
        println!("   ✅ System recovered and remained stable");

        assert_eq!(results.len(), 10);
        assert_eq!(results2.len(), 10);
    }
}