ruvllm 2.2.1

LLM serving runtime with Ruvector integration - Paged attention, KV cache, and SONA learning
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
#![allow(
    clippy::all,
    unused_imports,
    unused_variables,
    dead_code,
    unused_mut,
    unused_assignments,
    non_camel_case_types,
    clippy::approx_constant,
    unexpected_cfgs,
    unused_must_use,
    unused_parens
)]
//! Integration tests for Apple Neural Engine (ANE) / Core ML functionality
//!
//! These tests verify end-to-end functionality of the ANE/CoreML backend,
//! including hybrid pipeline switching, fallback behavior, and memory management.
//!
//! ## Running Tests
//!
//! ```bash
//! # Run all ANE tests (requires Apple Silicon)
//! cargo test --features coreml ane_integration
//!
//! # Run with hybrid pipeline support
//! cargo test --features hybrid-ane ane_integration
//!
//! # Run on non-Apple Silicon (tests fallback behavior)
//! cargo test ane_integration
//! ```

// Import from the crate being tested
// Note: CoreMLBackend methods require the coreml feature
#[cfg(feature = "coreml")]
use ruvllm::backends::CoreMLBackend;
use ruvllm::backends::{
    AneCapabilities, ComputeUnits, GenerateParams, LlmBackend, ModelArchitecture, ModelConfig,
    Quantization,
};
use ruvllm::error::{Result, RuvLLMError};

// ============================================================================
// Platform Detection Helpers
// ============================================================================

/// Check if running on Apple Silicon
fn is_apple_silicon() -> bool {
    cfg!(all(target_os = "macos", target_arch = "aarch64"))
}

/// Check if ANE is available
fn is_ane_available() -> bool {
    let caps = AneCapabilities::detect();
    caps.available
}

// ============================================================================
// Core ML Backend Integration Tests
// ============================================================================

#[test]
fn test_ane_capabilities_detection() {
    let caps = AneCapabilities::detect();

    if is_apple_silicon() {
        assert!(caps.available, "ANE should be available on Apple Silicon");
        assert!(caps.tops > 0.0, "TOPS should be positive on Apple Silicon");
        assert!(
            caps.max_model_size_mb > 0,
            "Max model size should be positive"
        );
        assert!(
            !caps.supported_ops.is_empty(),
            "Should have supported operations"
        );

        // Verify common operations are supported
        let expected_ops = ["MatMul", "GELU", "SiLU", "LayerNorm", "Softmax"];
        for op in &expected_ops {
            assert!(
                caps.supported_ops.iter().any(|s| s == *op),
                "Operation {} should be supported",
                op
            );
        }
    } else {
        assert!(
            !caps.available,
            "ANE should not be available on non-Apple Silicon"
        );
        assert_eq!(caps.tops, 0.0, "TOPS should be 0 when unavailable");
        assert_eq!(
            caps.max_model_size_mb, 0,
            "Max model size should be 0 when unavailable"
        );
        assert!(
            caps.supported_ops.is_empty(),
            "No operations when unavailable"
        );
    }
}

#[test]
fn test_compute_units_selection() {
    // Test default selection
    let default = ComputeUnits::default();
    assert_eq!(default, ComputeUnits::All);

    // Test ANE-focused configuration
    let ane_focus = ComputeUnits::CpuAndNeuralEngine;
    assert!(ane_focus.uses_ane());
    assert!(!ane_focus.uses_gpu());

    // Test GPU-focused configuration
    let gpu_focus = ComputeUnits::CpuAndGpu;
    assert!(!gpu_focus.uses_ane());
    assert!(gpu_focus.uses_gpu());

    // Test all units
    let all = ComputeUnits::All;
    assert!(all.uses_ane());
    assert!(all.uses_gpu());
}

#[test]
fn test_model_suitability_for_ane() {
    let caps = AneCapabilities::detect();

    if is_apple_silicon() {
        // Small models should be suitable
        assert!(caps.is_model_suitable(500), "500MB model should fit");
        assert!(caps.is_model_suitable(1000), "1GB model should fit");
        assert!(caps.is_model_suitable(2048), "2GB model should fit");

        // Large models may not fit
        // (depends on actual device, but 10GB is likely too large)
        // Skip this assertion as it's hardware-dependent
    }
}

// ============================================================================
// Core ML Backend Creation Tests
// ============================================================================

#[test]
#[cfg(feature = "coreml")]
fn test_coreml_backend_creation() {
    if is_apple_silicon() {
        let result = CoreMLBackend::new();
        assert!(result.is_ok(), "Should create backend on Apple Silicon");

        let backend = result.unwrap();
        assert!(!backend.is_model_loaded());
        assert!(backend.model_info().is_none());
    } else {
        let result = CoreMLBackend::new();
        assert!(result.is_err(), "Should fail on non-Apple Silicon");
    }
}

#[test]
#[cfg(feature = "coreml")]
fn test_coreml_backend_configuration() {
    if !is_apple_silicon() {
        return; // Skip on non-Apple Silicon
    }

    let backend = CoreMLBackend::new()
        .unwrap()
        .with_compute_units(ComputeUnits::CpuAndNeuralEngine);

    let caps = backend.ane_capabilities();
    assert!(caps.available);
    assert!(caps.tops > 0.0);
}

// ============================================================================
// Fallback Behavior Tests
// ============================================================================

#[test]
fn test_fallback_when_coreml_unavailable() {
    // When coreml feature is not enabled, CoreMLBackend type doesn't exist
    // so we can only test the AneCapabilities fallback
    #[cfg(not(feature = "coreml"))]
    {
        // Without coreml feature, ANE capabilities should report unavailable
        let caps = AneCapabilities::detect();
        // On non-Apple Silicon or without the feature, it should gracefully handle this
        if !is_apple_silicon() {
            assert!(
                !caps.available,
                "ANE should not be available without coreml feature on non-Apple Silicon"
            );
        }
    }

    #[cfg(feature = "coreml")]
    {
        if !is_apple_silicon() {
            let result = CoreMLBackend::new();
            assert!(result.is_err());

            let err = result.unwrap_err();
            let err_str = err.to_string();
            assert!(
                err_str.contains("not available"),
                "Should indicate ANE not available"
            );
        }
    }
}

#[test]
fn test_graceful_degradation() {
    // Even when ANE is not available, the AneCapabilities struct should work
    let caps = AneCapabilities {
        available: false,
        tops: 0.0,
        max_model_size_mb: 0,
        supported_ops: vec![],
    };

    // All operations should return false/empty gracefully
    assert!(!caps.is_model_suitable(100));
    assert!(!caps.is_model_suitable(0));
    assert!(!caps.available);
}

// ============================================================================
// Model Loading Error Handling Tests
// ============================================================================

#[test]
#[cfg(all(feature = "coreml", target_os = "macos", target_arch = "aarch64"))]
fn test_unsupported_model_format_error() {
    let mut backend = CoreMLBackend::new().unwrap();

    // Try various unsupported formats
    let unsupported_formats = [
        "model.safetensors",
        "model.bin",
        "model.pt",
        "model.pth",
        "model.onnx",
    ];

    for format in &unsupported_formats {
        let result = backend.load_model(format, ModelConfig::default());
        assert!(
            result.is_err(),
            "Should reject unsupported format: {}",
            format
        );
    }
}

#[test]
#[cfg(all(feature = "coreml", target_os = "macos", target_arch = "aarch64"))]
fn test_nonexistent_model_error() {
    let mut backend = CoreMLBackend::new().unwrap();

    let result = backend.load_model("/nonexistent/path/model.mlmodel", ModelConfig::default());
    assert!(result.is_err());
}

#[test]
#[cfg(all(feature = "coreml", target_os = "macos", target_arch = "aarch64"))]
fn test_gguf_conversion_error() {
    let mut backend = CoreMLBackend::new().unwrap();

    // GGUF conversion is not yet implemented
    let result = backend.load_model("/path/to/model.gguf", ModelConfig::default());
    assert!(result.is_err());

    let err = result.unwrap_err();
    let err_str = err.to_string();
    assert!(
        err_str.contains("not") || err_str.contains("conversion"),
        "Error should mention conversion issue: {}",
        err_str
    );
}

// ============================================================================
// Memory Management Tests
// ============================================================================

#[test]
#[cfg(all(feature = "coreml", target_os = "macos", target_arch = "aarch64"))]
fn test_model_unloading() {
    let mut backend = CoreMLBackend::new().unwrap();

    // Initial state
    assert!(!backend.is_model_loaded());

    // Unload should be safe even without loaded model
    backend.unload_model();
    assert!(!backend.is_model_loaded());
    assert!(backend.model_info().is_none());
}

#[test]
#[cfg(all(feature = "coreml", target_os = "macos", target_arch = "aarch64"))]
fn test_multiple_unload_calls() {
    let mut backend = CoreMLBackend::new().unwrap();

    // Multiple unload calls should be safe
    for _ in 0..5 {
        backend.unload_model();
        assert!(!backend.is_model_loaded());
    }
}

// ============================================================================
// Hybrid Pipeline Tests
// ============================================================================

#[cfg(feature = "hybrid-ane")]
mod hybrid_pipeline_tests {
    use super::*;

    #[test]
    fn test_hybrid_feature_enabled() {
        // Verify hybrid-ane feature combines metal-compute and coreml
        // This test just confirms the feature flag works
        assert!(true, "Hybrid ANE feature is enabled");
    }

    #[test]
    #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
    fn test_hybrid_configuration() {
        // Test that we can configure for hybrid operation
        let ane_caps = AneCapabilities::detect();

        if ane_caps.available {
            // In hybrid mode, we'd route:
            // - MatMul/FFN to ANE
            // - Attention to GPU (Metal)
            assert!(ane_caps.supported_ops.contains(&"MatMul".to_string()));
        }
    }
}

// ============================================================================
// Performance Characteristics Tests
// ============================================================================

#[test]
fn test_ane_tops_values() {
    // Test known TOPS values for various chips
    struct ChipSpec {
        name: &'static str,
        min_tops: f32,
        max_tops: f32,
    }

    // Known Apple Silicon TOPS ranges
    let chip_specs = [
        ChipSpec {
            name: "M1",
            min_tops: 11.0,
            max_tops: 11.5,
        },
        ChipSpec {
            name: "M1 Pro/Max",
            min_tops: 11.0,
            max_tops: 11.5,
        },
        ChipSpec {
            name: "M2",
            min_tops: 15.0,
            max_tops: 16.0,
        },
        ChipSpec {
            name: "M3",
            min_tops: 18.0,
            max_tops: 18.5,
        },
        ChipSpec {
            name: "M4",
            min_tops: 35.0,
            max_tops: 40.0,
        },
    ];

    if is_apple_silicon() {
        let caps = AneCapabilities::detect();
        // Detected TOPS should fall within one of the known ranges
        let in_known_range = chip_specs
            .iter()
            .any(|spec| caps.tops >= spec.min_tops && caps.tops <= spec.max_tops + 5.0);

        // Just verify it's a reasonable positive value
        assert!(caps.tops > 0.0, "TOPS should be positive");
        assert!(caps.tops < 100.0, "TOPS should be reasonable (< 100)");
    }
}

// ============================================================================
// Error Type Tests
// ============================================================================

#[test]
fn test_error_messages() {
    // Test that error messages are informative
    let caps = AneCapabilities {
        available: false,
        tops: 0.0,
        max_model_size_mb: 0,
        supported_ops: vec![],
    };

    // Debug output should be readable
    let debug = format!("{:?}", caps);
    assert!(debug.contains("available"));
    assert!(debug.contains("false"));
}

#[test]
#[cfg(feature = "coreml")]
fn test_error_chain() {
    if !is_apple_silicon() {
        let result: Result<CoreMLBackend> = CoreMLBackend::new();
        let err = result.unwrap_err();

        // Error should be a Config error
        match &err {
            RuvLLMError::Config(msg) => {
                assert!(msg.contains("not available") || msg.contains("feature"));
            }
            other => {
                panic!("Expected Config error, got {:?}", other);
            }
        }
    }
}

// ============================================================================
// Thread Safety Tests
// ============================================================================

#[test]
fn test_ane_capabilities_thread_safe() {
    use std::sync::Arc;
    use std::thread;

    let caps = Arc::new(AneCapabilities::detect());

    let handles: Vec<_> = (0..4)
        .map(|i| {
            let caps = Arc::clone(&caps);
            thread::spawn(move || {
                // Read operations should be thread-safe
                let _ = caps.available;
                let _ = caps.tops;
                let _ = caps.max_model_size_mb;
                let _ = caps.is_model_suitable(1000);
                let _ = format!("{:?}", caps);
                i
            })
        })
        .collect();

    for handle in handles {
        handle.join().expect("Thread should complete successfully");
    }
}

// ============================================================================
// Benchmark-style Tests (Run with --release)
// ============================================================================

#[test]
#[ignore] // Run with: cargo test --release -- --ignored
fn test_ane_capabilities_detection_performance() {
    use std::time::Instant;

    let iterations = 1000;
    let start = Instant::now();

    for _ in 0..iterations {
        let _ = AneCapabilities::detect();
    }

    let duration = start.elapsed();
    let avg_ns = duration.as_nanos() as f64 / iterations as f64;

    println!(
        "AneCapabilities::detect() average time: {:.2} ns ({:.2} us)",
        avg_ns,
        avg_ns / 1000.0
    );

    // Detection should be fast (< 1ms)
    assert!(
        avg_ns < 1_000_000.0,
        "Detection should be < 1ms, was {} ns",
        avg_ns
    );
}

// ============================================================================
// Documentation Examples Tests
// ============================================================================

#[test]
fn test_readme_example_capabilities() {
    // Example from module documentation
    let caps = AneCapabilities::detect();

    if caps.available {
        println!("ANE available with {} TOPS", caps.tops);
        println!("Max model size: {} MB", caps.max_model_size_mb);
        println!("Supported ops: {:?}", caps.supported_ops);
    } else {
        println!("ANE not available on this device");
    }
}

#[test]
fn test_readme_example_compute_units() {
    // Example from module documentation
    let units = ComputeUnits::CpuAndNeuralEngine;

    println!("Compute units: {}", units.description());
    println!("Uses ANE: {}", units.uses_ane());
    println!("Uses GPU: {}", units.uses_gpu());

    assert!(units.uses_ane());
    assert!(!units.uses_gpu());
}

// ============================================================================
// Property-based Test Helpers
// ============================================================================

#[test]
fn test_model_suitability_monotonic() {
    // Model suitability should be monotonic: if a larger model fits, smaller ones should too
    let caps = AneCapabilities {
        available: true,
        tops: 38.0,
        max_model_size_mb: 2048,
        supported_ops: vec!["MatMul".to_string()],
    };

    // If 2048 fits, all smaller sizes should fit
    if caps.is_model_suitable(2048) {
        for size in [0, 1, 100, 500, 1000, 1500, 2000, 2047] {
            assert!(
                caps.is_model_suitable(size),
                "Size {} should fit if {} fits",
                size,
                2048
            );
        }
    }

    // If 2049 doesn't fit, all larger sizes shouldn't fit either
    if !caps.is_model_suitable(2049) {
        for size in [2050, 3000, 4096, 10000] {
            assert!(
                !caps.is_model_suitable(size),
                "Size {} should not fit if {} doesn't fit",
                size,
                2049
            );
        }
    }
}