amari-gpu 0.23.0

GPU acceleration for mathematical computations
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
//! Integration tests for GPU Verification Framework (Phase 4B)
//!
//! This test suite validates the boundary verification system, statistical
//! verification, and adaptive platform selection for GPU-accelerated
//! geometric algebra operations.

mod common;

use amari_core::Multivector;
use amari_gpu::{
    AdaptiveVerificationLevel, AdaptiveVerifier, GpuBoundaryVerifier, GpuCliffordAlgebra,
    PlatformCapabilities, StatisticalGpuVerifier, VerificationConfig, VerificationPlatform,
    VerificationStrategy, VerifiedMultivector,
};
use common::direct_gpu_runtime_available;
use std::time::Duration;

/// Test verified multivector creation and invariant checking
#[tokio::test]
async fn test_verified_multivector_operations() {
    let mv1 =
        Multivector::<3, 0, 0>::from_coefficients(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
    let mv2 =
        Multivector::<3, 0, 0>::from_coefficients(vec![2.0, 1.0, 4.0, 3.0, 6.0, 5.0, 8.0, 7.0]);

    let verified1 = VerifiedMultivector::new(mv1);
    let verified2 = VerifiedMultivector::new(mv2);

    // Test signature verification
    assert_eq!(VerifiedMultivector::<3, 0, 0>::signature(), (3, 0, 0));

    // Test invariant checking
    assert!(verified1.verify_invariants().is_ok());
    assert!(verified2.verify_invariants().is_ok());

    // Test inner access
    assert_eq!(verified1.inner().get(0), 1.0);
    assert_eq!(verified2.inner().get(1), 1.0);
}

/// Test boundary verification with small batches
#[tokio::test]
async fn test_boundary_verification_small_batch() {
    if !direct_gpu_runtime_available() {
        println!("Skipping GPU boundary verification test in this environment");
        return;
    }
    let config = VerificationConfig {
        strategy: VerificationStrategy::Boundary,
        performance_budget: Duration::from_millis(100),
        tolerance: 1e-12,
        enable_invariant_checking: true,
    };

    let mut verifier = GpuBoundaryVerifier::new(config);

    // Create small test batch
    let batch_size = 5;
    let mut a_batch = Vec::new();
    let mut b_batch = Vec::new();

    for i in 0..batch_size {
        let a = Multivector::<3, 0, 0>::from_coefficients(vec![
            i as f64, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        ]);
        let b = Multivector::<3, 0, 0>::from_coefficients(vec![
            1.0, i as f64, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        ]);

        a_batch.push(VerifiedMultivector::new(a));
        b_batch.push(VerifiedMultivector::new(b));
    }

    // Test boundary verification without GPU (should fall back to verification logic)
    // Note: This test focuses on the verification framework, not GPU execution
    match GpuCliffordAlgebra::new::<3, 0, 0>().await {
        Ok(gpu) => {
            let result = verifier
                .verified_batch_geometric_product(&gpu, &a_batch, &b_batch)
                .await;

            match result {
                Ok(verified_results) => {
                    assert_eq!(verified_results.len(), batch_size);

                    // Verify each result maintains verification properties
                    for (i, result) in verified_results.iter().enumerate() {
                        assert!(result.verify_invariants().is_ok());

                        // Check that result matches expected geometric product
                        let expected = a_batch[i].inner().geometric_product(b_batch[i].inner());
                        let tolerance = 1e-12;

                        for j in 0..8 {
                            let diff = (result.inner().get(j) - expected.get(j)).abs();
                            assert!(
                                diff < tolerance,
                                "Component {} mismatch: expected {}, got {}, diff {}",
                                j,
                                expected.get(j),
                                result.inner().get(j),
                                diff
                            );
                        }
                    }

                    // Check performance statistics
                    let stats = verifier.performance_stats();
                    assert!(stats.operation_count() > 0);
                    assert!(stats.average_duration() > Duration::ZERO);
                }
                Err(e) => {
                    // GPU verification may fail in test environments
                    println!(
                        "Boundary verification failed (expected in test env): {:?}",
                        e
                    );
                }
            }
        }
        Err(_) => {
            // No GPU available - test the verification logic components
            println!("No GPU available for boundary verification test");
        }
    }
}

/// Test statistical verification sampling strategies
#[tokio::test]
async fn test_statistical_verification() {
    if !direct_gpu_runtime_available() {
        return;
    }

    let mut verifier = StatisticalGpuVerifier::<3, 0, 0>::new(0.2, 1e-12);

    // Create test batch with known results
    let batch_size = 20;
    let mut inputs = Vec::new();
    let mut gpu_results = Vec::new();

    for i in 0..batch_size {
        let a = VerifiedMultivector::new(Multivector::<3, 0, 0>::from_coefficients(vec![
            i as f64, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        ]));
        let b = VerifiedMultivector::new(Multivector::<3, 0, 0>::from_coefficients(vec![
            1.0, i as f64, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        ]));

        let expected_result = a.inner().geometric_product(b.inner());
        inputs.push((a, b));
        gpu_results.push(expected_result);
    }

    // Test statistical verification
    match GpuCliffordAlgebra::new::<3, 0, 0>().await {
        Ok(gpu) => {
            let result = verifier
                .verify_batch_statistical(&gpu, &inputs, &gpu_results)
                .await;

            match result {
                Ok(verified_results) => {
                    assert_eq!(verified_results.len(), batch_size);

                    // All results should be verified correctly
                    for result in &verified_results {
                        assert!(result.verify_invariants().is_ok());
                    }
                }
                Err(e) => {
                    println!(
                        "Statistical verification failed (expected in test env): {:?}",
                        e
                    );
                }
            }
        }
        Err(_) => {
            println!("No GPU available for statistical verification test");
        }
    }
}

/// Test adaptive verification platform detection and strategy selection
#[tokio::test]
async fn test_adaptive_verification_strategies() {
    if !direct_gpu_runtime_available() {
        println!("Skipping GPU adaptive verification test in this environment");
        return;
    }

    // Test platform-specific behavior
    match AdaptiveVerifier::new().await {
        Ok(mut verifier) => {
            println!("Detected platform: {:?}", verifier.platform());
            println!("Verification level: {:?}", verifier.verification_level());
            println!("Performance budget: {:?}", verifier.performance_budget());

            // Test single operation verification
            let a = VerifiedMultivector::new(Multivector::<3, 0, 0>::from_coefficients(vec![
                1.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            ]));
            let b = VerifiedMultivector::new(Multivector::<3, 0, 0>::from_coefficients(vec![
                2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            ]));

            let result = verifier.verified_geometric_product(&a, &b).await;
            match result {
                Ok(verified_result) => {
                    assert!(verified_result.verify_invariants().is_ok());

                    // Verify mathematical correctness
                    let expected = a.inner().geometric_product(b.inner());
                    for i in 0..8 {
                        let diff = (verified_result.inner().get(i) - expected.get(i)).abs();
                        assert!(diff < 1e-12, "Component {} verification failed", i);
                    }
                }
                Err(e) => {
                    println!("Single operation verification failed: {:?}", e);
                }
            }

            // Test batch operation verification
            let batch_size = 10;
            let mut a_batch = Vec::new();
            let mut b_batch = Vec::new();

            for i in 0..batch_size {
                let a = VerifiedMultivector::new(Multivector::<3, 0, 0>::from_coefficients(vec![
                    i as f64, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                ]));
                let b = VerifiedMultivector::new(Multivector::<3, 0, 0>::from_coefficients(vec![
                    1.0, i as f64, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                ]));

                a_batch.push(a);
                b_batch.push(b);
            }

            let batch_result = verifier
                .verified_batch_geometric_product(&a_batch, &b_batch)
                .await;

            match batch_result {
                Ok(verified_results) => {
                    assert_eq!(verified_results.len(), batch_size);

                    for (i, result) in verified_results.iter().enumerate() {
                        assert!(result.verify_invariants().is_ok());

                        // Verify against expected result
                        let expected = a_batch[i].inner().geometric_product(b_batch[i].inner());
                        for j in 0..8 {
                            let diff = (result.inner().get(j) - expected.get(j)).abs();
                            assert!(
                                diff < 1e-12,
                                "Batch result[{}][{}] verification failed",
                                i,
                                j
                            );
                        }
                    }
                }
                Err(e) => {
                    println!("Batch verification failed: {:?}", e);
                }
            }

            // Test GPU usage decision
            let should_use_small = verifier.should_use_gpu(10);
            let should_use_large = verifier.should_use_gpu(1000);

            match verifier.platform() {
                VerificationPlatform::Gpu { .. } => {
                    println!(
                        "GPU decisions: small={}, large={}",
                        should_use_small, should_use_large
                    );
                }
                _ => {
                    assert!(!should_use_small);
                    assert!(!should_use_large);
                }
            }
        }
        Err(e) => {
            println!(
                "Adaptive verifier creation failed (expected in limited env): {:?}",
                e
            );
        }
    }
}

/// Test verification level adaptation and performance budgets
#[tokio::test]
async fn test_verification_level_adaptation() {
    if !direct_gpu_runtime_available() {
        println!("Skipping GPU verification level adaptation test in this environment");
        return;
    }
    let levels = vec![
        AdaptiveVerificationLevel::Maximum,
        AdaptiveVerificationLevel::High,
        AdaptiveVerificationLevel::Balanced,
        AdaptiveVerificationLevel::Minimal,
    ];

    for level in levels {
        match AdaptiveVerifier::with_config(level.clone(), Duration::from_millis(50)).await {
            Ok(mut verifier) => {
                assert_eq!(*verifier.verification_level(), level);
                assert_eq!(verifier.performance_budget(), Duration::from_millis(50));

                // Test level change
                verifier.set_verification_level(AdaptiveVerificationLevel::Minimal);
                assert_eq!(
                    *verifier.verification_level(),
                    AdaptiveVerificationLevel::Minimal
                );

                println!("Successfully tested verification level: {:?}", level);
            }
            Err(e) => {
                println!("Verification level {:?} test failed: {:?}", level, e);
            }
        }
    }
}

/// Test platform capabilities interface
#[test]
fn test_platform_capabilities() {
    use amari_gpu::{CpuFeatures, GpuBackend, WasmEnvironment};

    let platforms = vec![
        VerificationPlatform::NativeCpu {
            features: CpuFeatures {
                supports_simd: true,
                core_count: 8,
                cache_size_kb: 8192,
            },
        },
        VerificationPlatform::Gpu {
            backend: GpuBackend::Vulkan,
            memory_mb: 2048,
            compute_units: 32,
        },
        VerificationPlatform::Wasm {
            env: WasmEnvironment::Browser {
                engine: "V8".to_string(),
            },
        },
    ];

    for platform in platforms {
        println!("Testing platform: {:?}", platform);

        let max_batch = platform.max_batch_size();
        assert!(max_batch > 0);

        let strategy_small = platform.optimal_strategy(10);
        let strategy_large = platform.optimal_strategy(10000);

        println!("  Max batch size: {}", max_batch);
        println!("  Small workload strategy: {:?}", strategy_small);
        println!("  Large workload strategy: {:?}", strategy_large);

        let concurrent_support = platform.supports_concurrent_verification();
        println!("  Concurrent verification: {}", concurrent_support);

        let profile = platform.performance_characteristics();
        println!("  Performance profile: {:?}", profile);

        // Validate performance profile values
        assert!(profile.verification_overhead_percent >= 0.0);
        assert!(profile.memory_bandwidth_gbps > 0.0);
        assert!(profile.compute_throughput_gflops > 0.0);
        assert!(profile.latency_microseconds > 0.0);
    }
}

/// Test error handling and edge cases
#[tokio::test]
async fn test_verification_error_handling() {
    if !direct_gpu_runtime_available() {
        return;
    }

    // Test mismatched batch sizes
    let config = VerificationConfig::default();
    let mut verifier = GpuBoundaryVerifier::new(config);

    let a_batch = vec![VerifiedMultivector::new(Multivector::<3, 0, 0>::zero())];
    let b_batch = vec![
        VerifiedMultivector::new(Multivector::<3, 0, 0>::zero()),
        VerifiedMultivector::new(Multivector::<3, 0, 0>::zero()),
    ];

    // This should fail due to mismatched batch sizes
    if let Ok(gpu) = GpuCliffordAlgebra::new::<3, 0, 0>().await {
        let result = verifier
            .verified_batch_geometric_product(&gpu, &a_batch, &b_batch)
            .await;

        assert!(result.is_err());
        println!("Correctly detected batch size mismatch");
    }

    // Test invalid multivector (infinite magnitude)
    let invalid_mv = Multivector::<3, 0, 0>::from_coefficients(vec![
        f64::INFINITY,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
        0.0,
    ]);
    let invalid_verified = VerifiedMultivector::new(invalid_mv);

    // This should fail invariant checking
    let invariant_result = invalid_verified.verify_invariants();
    assert!(invariant_result.is_err());
    println!("Correctly detected invalid magnitude");
}

/// Performance benchmark for verification overhead
#[tokio::test]
async fn test_verification_performance_overhead() {
    if !direct_gpu_runtime_available() {
        println!("Skipping GPU performance test in this environment");
        return;
    }

    use std::time::Instant;

    let batch_size = 100;
    let mut a_batch = Vec::new();
    let mut b_batch = Vec::new();

    // Create test batch
    for i in 0..batch_size {
        let a = Multivector::<3, 0, 0>::from_coefficients(vec![
            i as f64, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        ]);
        let b = Multivector::<3, 0, 0>::from_coefficients(vec![
            1.0, i as f64, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        ]);

        a_batch.push(a);
        b_batch.push(b);
    }

    // Benchmark unverified CPU computation
    let start_unverified = Instant::now();
    let mut cpu_results = Vec::new();
    for (a, b) in a_batch.iter().zip(b_batch.iter()) {
        cpu_results.push(a.geometric_product(b));
    }
    let unverified_duration = start_unverified.elapsed();

    println!("Unverified CPU computation: {:?}", unverified_duration);

    // Benchmark verified computation
    match AdaptiveVerifier::new().await {
        Ok(mut verifier) => {
            let verified_a: Vec<_> = a_batch.into_iter().map(VerifiedMultivector::new).collect();
            let verified_b: Vec<_> = b_batch.into_iter().map(VerifiedMultivector::new).collect();

            let start_verified = Instant::now();
            let verified_results = verifier
                .verified_batch_geometric_product(&verified_a, &verified_b)
                .await
                .expect("verified batch product should succeed");
            let verified_duration = start_verified.elapsed();

            println!("Verified computation: {:?}", verified_duration);
            assert_eq!(verified_results.len(), cpu_results.len());

            if verified_duration > Duration::ZERO && unverified_duration > Duration::ZERO {
                let overhead_percent =
                    (verified_duration.as_secs_f64() / unverified_duration.as_secs_f64() - 1.0)
                        * 100.0;
                println!("Verification overhead: {:.1}%", overhead_percent);

                // This integration test is a smoke/diagnostic guard, not a
                // benchmark. On laptops, first-run shader/device setup,
                // scheduler noise, Vulkan driver behavior, and CPU fallback
                // can easily dominate this tiny batch. Treat overhead as a
                // reported diagnostic and only assert that timings are finite.
                assert!(
                    overhead_percent.is_finite(),
                    "verification overhead should be finite"
                );
            }
        }
        Err(e) => {
            println!(
                "Performance test skipped due to verifier creation failure: {:?}",
                e
            );
        }
    }
}

/// Test verification strategy effectiveness
#[test]
fn test_verification_strategies() {
    let strategies = vec![
        VerificationStrategy::Strict,
        VerificationStrategy::Statistical { sample_rate: 0.1 },
        VerificationStrategy::Statistical { sample_rate: 0.5 },
        VerificationStrategy::Boundary,
        VerificationStrategy::Minimal,
    ];

    for strategy in strategies {
        let config = VerificationConfig {
            strategy: strategy.clone(),
            performance_budget: Duration::from_millis(10),
            tolerance: 1e-12,
            enable_invariant_checking: true,
        };

        let _verifier = GpuBoundaryVerifier::new(config);
        println!(
            "Successfully created verifier with strategy: {:?}",
            strategy
        );
    }
}