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
//! GPU Memory Management Tests
//!
//! Tests for:
//! - Buffer allocation and deallocation
//! - Memory leak detection
//! - Large allocation handling
//! - Buffer pool efficiency
//! - Memory fragmentation

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

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

    #[test]
    fn test_small_buffer_allocation() {
        // Test allocation of small buffer (1KB)
        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 = 64; // 64 * 4 bytes = 256 bytes per vector
        let mut storage = context
            .create_storage(dimension, GpuDistanceMetric::Cosine)
            .expect("Failed to create storage");

        let vectors = create_test_vectors(4, dimension); // ~1KB total
        storage
            .add_vectors(&vectors)
            .expect("Failed to add vectors");

        assert_eq!(storage.vector_count(), 4);
        println!("✅ Small buffer allocation (1KB) successful");
    }

    #[test]
    fn test_medium_buffer_allocation() {
        // Test allocation of medium buffer (1MB)
        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 = 512; // Standard embedding size
        let count = 500; // 500 * 512 * 4 bytes = ~1MB
        let vectors = create_test_vectors(count, dimension);

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

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

        assert_eq!(storage.vector_count(), count);
        println!("✅ Medium buffer allocation (~1MB) successful");
        println!("   Vectors: {}", count);
        println!("   Size: ~{} MB", (count * dimension * 4) / 1024 / 1024);
    }

    #[test]
    fn test_large_buffer_allocation() {
        // Test allocation of large buffer (100MB)
        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 = 1024;
        let count = 25000; // 25K * 1024 * 4 bytes = ~100MB

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

        // Add in batches to avoid timeout and duplicate IDs
        let batch_size = 1000;
        let mut total_added = 0;
        for batch_idx in 0..(count / batch_size) {
            let start = batch_idx * batch_size;
            let end = start + batch_size;

            // Create vectors with unique IDs across batches
            let vectors: Vec<GpuVector> = (start..end)
                .map(|i| {
                    let data: Vec<f32> =
                        (0..dimension).map(|d| (i * dimension + d) as f32).collect();
                    GpuVector::new(format!("vec_{}", i), data)
                })
                .collect();

            storage
                .add_vectors(&vectors)
                .unwrap_or_else(|_| panic!("Failed to add batch {}", batch_idx));

            total_added += vectors.len();

            if batch_idx % 5 == 0 {
                println!("   Progress: {}/{} vectors", total_added, count);
            }
        }

        assert_eq!(storage.vector_count(), count);
        println!("✅ Large buffer allocation (~100MB) successful");
        println!("   Total vectors: {}", count);
        println!(
            "   Total size: ~{} MB",
            (count * dimension * 4) / 1024 / 1024
        );
    }

    #[test]
    fn test_multiple_allocations() {
        // Test multiple independent allocations
        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 storages_count = 5;

        let mut storages = Vec::new();
        for i in 0..storages_count {
            let mut storage = context
                .create_storage(dimension, GpuDistanceMetric::Cosine)
                .unwrap_or_else(|_| panic!("Failed to create storage {}", i));

            let vectors = create_test_vectors(10, dimension);
            storage
                .add_vectors(&vectors)
                .expect("Failed to add vectors");

            storages.push(storage);
        }

        // Verify all storages are independent
        for (i, storage) in storages.iter().enumerate() {
            assert_eq!(
                storage.vector_count(),
                10,
                "Storage {} should have 10 vectors",
                i
            );
        }

        println!("✅ Multiple independent allocations successful");
        println!("   Storages created: {}", storages_count);
        println!("   Vectors per storage: 10");
    }

    #[test]
    fn test_deallocation() {
        // Test that deallocation happens when storage is dropped
        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_before = context.device_info().expect("Failed to get device info");
        let used_before = info_before.used_vram_bytes;

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

            let vectors = create_test_vectors(count, dimension);
            storage
                .add_vectors(&vectors)
                .expect("Failed to add vectors");

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

            println!("   VRAM before: {} MB", used_before / 1024 / 1024);
            println!("   VRAM during: {} MB", used_during / 1024 / 1024);

            // Metal's unified memory may show fluctuations
            if used_during > used_before {
                println!(
                    "   VRAM allocated: {} MB",
                    (used_during - used_before) / 1024 / 1024
                );
            } else {
                println!(
                    "   VRAM change: {} MB (Metal unified memory)",
                    used_during.saturating_sub(used_before) / 1024 / 1024
                );
            }

            // Storage creation is successful (VRAM accounting may vary with Metal)
            // The important part is no crash occurs
            println!("   ✅ Storage allocated successfully");

            // storage drops here
        }

        // Give GPU time to cleanup
        std::thread::sleep(std::time::Duration::from_millis(100));

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

        println!("   VRAM after: {} MB", used_after / 1024 / 1024);

        // Calculate change (can be positive or negative due to Metal's memory management)
        if used_after > used_before {
            println!(
                "   VRAM increased by: {} MB",
                (used_after - used_before) / 1024 / 1024
            );
        } else {
            println!(
                "   VRAM decreased by: {} MB",
                (used_before - used_after) / 1024 / 1024
            );
        }

        // Metal's unified memory architecture means VRAM can fluctuate
        // The important thing is we don't leak indefinitely
        // We allow for reasonable tolerance due to Metal's memory management
        let tolerance = 50 * 1024 * 1024; // 50MB tolerance for Metal
        let diff = used_after.abs_diff(used_before);

        assert!(
            diff <= tolerance,
            "VRAM change should be within tolerance (before: {} MB, after: {} MB, diff: {} MB)",
            used_before / 1024 / 1024,
            used_after / 1024 / 1024,
            diff / 1024 / 1024
        );

        println!("✅ Deallocation successful (within tolerance)");
    }

    #[test]
    fn test_repeated_allocation_deallocation() {
        // Test repeated allocation and 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 cycles = 10;
        let dimension = 256;
        let count = 100;

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

            let vectors = create_test_vectors(count, dimension);
            storage
                .add_vectors(&vectors)
                .unwrap_or_else(|_| panic!("Failed to add vectors in cycle {}", cycle));

            assert_eq!(storage.vector_count(), count);

            // storage drops here
        }

        println!("✅ Repeated allocation/deallocation successful");
        println!("   Cycles completed: {}", cycles);
        println!("   Vectors per cycle: {}", count);
    }

    #[test]
    fn test_clear_vectors() {
        // Test clearing vectors from storage
        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 mut storage = context
            .create_storage(dimension, GpuDistanceMetric::Cosine)
            .expect("Failed to create storage");

        // Add vectors
        let vectors = create_test_vectors(50, dimension);
        storage
            .add_vectors(&vectors)
            .expect("Failed to add vectors");
        assert_eq!(storage.vector_count(), 50);

        // Clear vectors
        storage.clear().expect("Failed to clear storage");
        assert_eq!(storage.vector_count(), 0);

        // Add new vectors after clear
        let new_vectors = create_test_vectors(30, dimension);
        storage
            .add_vectors(&new_vectors)
            .expect("Failed to add vectors after clear");
        assert_eq!(storage.vector_count(), 30);

        println!("✅ Clear vectors successful");
        println!("   Initial: 50 vectors");
        println!("   After clear: 0 vectors");
        println!("   After re-add: 30 vectors");
    }

    #[test]
    fn test_memory_reuse() {
        // Test that memory can be reused efficiently
        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 mut storage = context
            .create_storage(dimension, GpuDistanceMetric::Cosine)
            .expect("Failed to create storage");

        // First allocation
        let vectors1 = create_test_vectors(100, dimension);
        storage
            .add_vectors(&vectors1)
            .expect("Failed to add first batch");

        // Clear and reallocate
        storage.clear().expect("Failed to clear");

        let vectors2 = create_test_vectors(150, dimension);
        storage
            .add_vectors(&vectors2)
            .expect("Failed to add second batch");

        assert_eq!(storage.vector_count(), 150);

        println!("✅ Memory reuse successful");
        println!("   First allocation: 100 vectors");
        println!("   Second allocation: 150 vectors");
    }

    #[test]
    fn test_zero_allocation() {
        // Test creating storage with zero vectors (edge case)
        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 storage = context
            .create_storage(dimension, GpuDistanceMetric::Cosine)
            .expect("Failed to create empty storage");

        assert_eq!(storage.vector_count(), 0);

        println!("✅ Zero allocation (empty storage) successful");
    }

    #[test]
    fn test_memory_stress() {
        // Stress test: many small allocations
        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 = 64;
        let iterations = 50;
        let vectors_per_iteration = 20;

        for i in 0..iterations {
            let mut storage = context
                .create_storage(dimension, GpuDistanceMetric::Cosine)
                .unwrap_or_else(|_| panic!("Failed to create storage iteration {}", i));

            let vectors = create_test_vectors(vectors_per_iteration, dimension);
            storage
                .add_vectors(&vectors)
                .unwrap_or_else(|_| panic!("Failed to add vectors iteration {}", i));

            if i % 10 == 0 {
                println!("   Iteration {}/{}", i, iterations);
            }

            // storage drops here
        }

        println!("✅ Memory stress test successful");
        println!("   Iterations: {}", iterations);
        println!("   Allocations per iteration: {}", vectors_per_iteration);
        println!(
            "   Total allocations: {}",
            iterations * vectors_per_iteration
        );
    }
}