modelc 0.1.9

Rust CLI that compiles LLM weights (GGUF, Safetensors, ONNX, PyTorch) into a single .modelc artifact and serves a local OpenAI-compatible inference API with Metal GPU and CPU SIMD acceleration.
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
621
622
623
624
625
626
627
628
629
630
631
632
633
//! Stress tests and edge cases for KV catch capabilities.
//!
//! These tests ensure the error handling system is robust under extreme conditions
//! and handles edge cases correctly.

use modelc::kv_error::{KvError, CacheValidationError, KvContext};
use modelc::prefix_cache::{PrefixCache, CachedPrefix};
use modelc::runtime::transformer::{KvCache, KvLayer};

fn create_test_kv_cache(n_layers: usize, hidden: usize, n_positions: usize) -> KvCache {
    let mut kv = KvCache::new(n_layers);
    for layer_idx in 0..n_layers {
        let mut layer = KvLayer::new_fp32();
        for _ in 0..n_positions {
            let k: Vec<f32> = (0..hidden).map(|i| i as f32 * 0.01).collect();
            let v: Vec<f32> = (0..hidden).map(|i| i as f32 * 0.02).collect();
            layer.append(&k, &v);
        }
        kv.layers[layer_idx] = Some(layer);
    }
    kv
}

// ===== Stress Tests =====

#[test]
fn test_cache_handles_massive_insertion_load() {
    let mut cache = PrefixCache::new(1000);
    let successful_inserts = 50;
    
    for i in 0..successful_inserts {
        let tokens = vec![i as u32; 10];
        let result = cache.insert(
            tokens,
            CachedPrefix {
                kv: create_test_kv_cache(2, 128, 10),
                last_logits: vec![],
            },
        );
        assert!(result.is_ok(), "Insert {} should succeed", i);
    }
    
    assert_eq!(cache.len(), successful_inserts);
    
    let stats = cache.stats();
    assert_eq!(stats.entries, successful_inserts);
    assert_eq!(stats.total_tokens, successful_inserts * 10);
}

#[test]
fn test_cache_rapid_insertion_and_removal() {
    let mut cache = PrefixCache::new(100);
    let operations = 1000;
    
    for i in 0..operations {
        let tokens = vec![i as u32 % 50; 5]; // Cycle through 50 different keys
        
        if i % 3 == 0 {
            // Remove operation
            cache.remove(&tokens);
        } else {
            // Insert operation
            let _ = cache.insert(
                tokens.clone(),
                CachedPrefix {
                    kv: create_test_kv_cache(2, 128, 5),
                    last_logits: vec![],
                },
            );
        }
    }
    
    // Cache should still be functional
    assert!(cache.len() <= 100);
    
    let lookup = cache.lookup(&[0, 0, 0, 0, 0]);
    assert!(lookup.is_ok() || matches!(lookup, Err(KvError::EmptyCache(_))));
}

#[test]
fn test_cache_under_constant_eviction_pressure() {
    let capacity = 10;
    let mut cache = PrefixCache::new(capacity);
    let iterations = 100;
    
    for i in 0..iterations {
        let tokens = vec![i as u32; 3];
        let result = cache.insert(
            tokens,
            CachedPrefix {
                kv: create_test_kv_cache(2, 128, 3),
                last_logits: vec![],
            },
        );
        
        // Some inserts may fail due to capacity, but that's OK
        if result.is_ok() {
            assert!(cache.len() <= capacity);
        }
    }
    
    // Final state should be consistent
    assert!(cache.len() <= capacity);
    let stats = cache.stats();
    assert!(stats.entries <= capacity);
}

#[test]
fn test_cache_concurrent_access_simulation() {
    let mut cache = PrefixCache::new(50);
    let threads = 10;
    let operations_per_thread = 100;
    
    for thread in 0..threads {
        for op in 0..operations_per_thread {
            let unique_id = (thread * operations_per_thread + op) as u32;
            let tokens = vec![unique_id; 5];
            
            // Simulate different operations
            match op % 4 {
                0 => {
                    // Insert
                    let _ = cache.insert(
                        tokens.clone(),
                        CachedPrefix {
                            kv: create_test_kv_cache(2, 128, 5),
                            last_logits: vec![],
                        },
                    );
                }
                1 => {
                    // Lookup
                    let _ = cache.lookup(&tokens);
                }
                2 => {
                    // Remove
                    cache.remove(&tokens);
                }
                3 => {
                    // Stats
                    let _ = cache.stats();
                }
                _ => unreachable!(),
            }
        }
    }
    
    // Cache should still be consistent
    assert!(cache.len() <= 50);
    let stats = cache.stats();
    assert!(stats.utilization <= 1.0);
}

// ===== Edge Case Tests =====

#[test]
fn test_cache_with_zero_capacity() {
    let mut cache = PrefixCache::new(0);
    
    // Should still function with minimum capacity of 1
    assert_eq!(cache.capacity(), 1);
    
    let tokens = vec![1_u32];
    let result = cache.insert(
        tokens,
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 1),
            last_logits: vec![],
        },
    );
    
    // Insert should succeed since capacity is normalized to 1
    assert!(result.is_ok());
    assert_eq!(cache.len(), 1);
}

#[test]
fn test_cache_with_very_large_capacity() {
    let large_capacity = 100_000;
    let mut cache = PrefixCache::new(large_capacity);
    
    assert_eq!(cache.capacity(), large_capacity);
    
    // Insert a reasonable number of entries
    for i in 0..100 {
        let tokens = vec![i as u32; 2];
        assert!(cache.insert(
            tokens,
            CachedPrefix {
                kv: create_test_kv_cache(2, 128, 2),
                last_logits: vec![],
            },
        ).is_ok());
    }
    
    assert_eq!(cache.len(), 100);
}

#[test]
fn test_cache_with_single_token_sequences() {
    let mut cache = PrefixCache::new(10);
    
    for i in 0..10 {
        let tokens = vec![i as u32];
        let result = cache.insert(
            tokens,
            CachedPrefix {
                kv: create_test_kv_cache(2, 128, 1),
                last_logits: vec![],
            },
        );
        assert!(result.is_ok());
    }
    
    assert_eq!(cache.len(), 10);
    
    // All single-token lookups should work
    for i in 0..10 {
        let lookup = cache.lookup(&[i as u32]);
        assert!(lookup.is_ok());
        assert_eq!(lookup.unwrap().matched_len, 1);
    }
}

#[test]
fn test_cache_with_very_long_token_sequences() {
    let mut cache = PrefixCache::new(5);
    
    let long_sequence: Vec<u32> = (0..100).collect();
    
    // This should fail due to capacity
    let result = cache.insert(
        long_sequence.clone(),
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 100),
            last_logits: vec![],
        },
    );
    
    assert!(result.is_err());
    assert!(matches!(result, Err(CacheValidationError::SequenceTooLong { .. })));
}

#[test]
fn test_cache_with_duplicate_entries() {
    let mut cache = PrefixCache::new(10);
    
    let tokens = vec![1, 2, 3];
    
    // Insert same entry multiple times
    for _ in 0..5 {
        let result = cache.insert(
            tokens.clone(),
            CachedPrefix {
                kv: create_test_kv_cache(2, 128, 3),
                last_logits: vec![],
            },
        );
        assert!(result.is_ok());
    }
    
    // Should only have one entry
    assert_eq!(cache.len(), 1);
    
    // Lookup should work
    let lookup = cache.lookup(&tokens);
    assert!(lookup.is_ok());
}

#[test]
fn test_cache_with_very_large_token_ids() {
    let mut cache = PrefixCache::new(10);
    
    // Try to insert with unreasonably large token IDs
    let huge_tokens = vec![999_999_999, 1_000_000_000];
    let result = cache.insert(
        huge_tokens,
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 2),
            last_logits: vec![],
        },
    );
    
    // Should fail due to invalid token IDs
    assert!(result.is_err());
    assert!(matches!(result, Err(CacheValidationError::InvalidTokenId { .. })));
}

#[test]
fn test_cache_with_empty_kv_layers() {
    let mut cache = PrefixCache::new(10);
    
    let tokens = vec![1, 2, 3];
    let kv = KvCache::new(2);
    // Don't populate layers - leave them empty
    
    let result = cache.insert(
        tokens,
        CachedPrefix {
            kv,
            last_logits: vec![],
        },
    );
    
    // Empty KV layers should be OK
    assert!(result.is_ok());
}

#[test]
fn test_cache_with_mixed_layer_counts() {
    let mut cache = PrefixCache::new(10);
    
    // Entry with 2 layers
    let tokens1 = vec![1, 2];
    assert!(cache.insert(
        tokens1.clone(),
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 2),
            last_logits: vec![],
        },
    ).is_ok());
    
    // Entry with 4 layers
    let tokens2 = vec![3, 4];
    assert!(cache.insert(
        tokens2.clone(),
        CachedPrefix {
            kv: create_test_kv_cache(4, 128, 2),
            last_logits: vec![],
        },
    ).is_ok());
    
    assert_eq!(cache.len(), 2);
    
    // Both should be accessible
    assert!(cache.lookup(&tokens1).is_ok());
    assert!(cache.lookup(&tokens2).is_ok());
}

// ===== Error Recovery Tests =====

#[test]
fn test_cache_recovers_from_failed_lookups() {
    let mut cache = PrefixCache::new(10);
    
    // Failed lookup on empty cache
    let result = cache.lookup(&[1, 2, 3]);
    assert!(matches!(result, Err(KvError::EmptyCache(_))));
    
    // Insert valid data
    assert!(cache.insert(
        vec![1, 2, 3],
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 3),
            last_logits: vec![],
        },
    ).is_ok());
    
    // Same lookup should now succeed
    let result = cache.lookup(&[1, 2, 3]);
    assert!(result.is_ok());
}

#[test]
fn test_cache_handles_interleaved_success_and_failure() {
    let mut cache = PrefixCache::new(10);
    
    // Valid insert
    assert!(cache.insert(
        vec![1, 2],
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 2),
            last_logits: vec![],
        },
    ).is_ok());
    
    // Invalid insert
    assert!(cache.insert(
        vec![],
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 0),
            last_logits: vec![],
        },
    ).is_err());
    
    // Valid insert
    assert!(cache.insert(
        vec![3, 4],
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 2),
            last_logits: vec![],
        },
    ).is_ok());
    
    // Invalid insert
    assert!(cache.insert(
        vec![5; 20],
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 20),
            last_logits: vec![],
        },
    ).is_err());
    
    // Should have exactly 2 valid entries
    assert_eq!(cache.len(), 2);
}

#[test]
fn test_cache_statistics_under_error_conditions() {
    let mut cache = PrefixCache::new(10);
    
    // Initial stats
    let stats = cache.stats();
    assert_eq!(stats.entries, 0);
    assert_eq!(stats.total_tokens, 0);
    assert_eq!(stats.utilization, 0.0);
    
    // Failed insert
    assert!(cache.insert(
        vec![],
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 0),
            last_logits: vec![],
        },
    ).is_err());
    
    // Stats should remain unchanged
    let stats = cache.stats();
    assert_eq!(stats.entries, 0);
    assert_eq!(stats.total_tokens, 0);
    
    // Successful insert
    assert!(cache.insert(
        vec![1, 2, 3],
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 3),
            last_logits: vec![],
        },
    ).is_ok());
    
    // Stats should reflect the change
    let stats = cache.stats();
    assert_eq!(stats.entries, 1);
    assert_eq!(stats.total_tokens, 3);
}

// ===== Performance and Memory Tests =====

#[test]
fn test_cache_lookup_performance_under_load() {
    let mut cache = PrefixCache::new(1000);
    
    // Populate cache
    for i in 0..100 {
        let tokens = vec![i as u32; 10];
        assert!(cache.insert(
            tokens,
            CachedPrefix {
                kv: create_test_kv_cache(2, 128, 10),
                last_logits: vec![],
            },
        ).is_ok());
    }
    
    // Perform many lookups
    let start = std::time::Instant::now();
    for i in 0..1000 {
        let tokens = vec![(i % 100) as u32; 10];
        let _ = cache.lookup(&tokens);
    }
    let duration = start.elapsed();
    
    // Should complete reasonably quickly (< 1 second for 1000 lookups)
    assert!(duration.as_secs() < 1, "Cache lookup took too long: {:?}", duration);
}

#[test]
fn test_cache_memory_efficiency() {
    let mut cache = PrefixCache::new(100);
    
    // Add entries with different sizes
    for i in 0..50 {
        let tokens = vec![i as u32; (i % 10 + 1) as usize]; // Variable length
        assert!(cache.insert(
            tokens,
            CachedPrefix {
                kv: create_test_kv_cache(2, 128, (i % 10 + 1) as usize),
                last_logits: vec![],
            },
        ).is_ok());
    }
    
    let stats = cache.stats();
    
    // Verify memory calculations are reasonable
    assert!(stats.total_kv_bytes > 0);
    assert!(stats.total_kv_bytes < 100_000_000); // Should be less than 100MB
    
    // Verify utilization
    assert!(stats.utilization > 0.0);
    assert!(stats.utilization <= 0.5); // 50/100 = 0.5
}

// ===== Boundary Condition Tests =====

#[test]
fn test_cache_at_capacity_boundaries() {
    let capacity = 5;
    let mut cache = PrefixCache::new(capacity);
    
    // Fill exactly to capacity
    for i in 0..capacity {
        let tokens = vec![i as u32; 2];
        assert!(cache.insert(
            tokens,
            CachedPrefix {
                kv: create_test_kv_cache(2, 128, 2),
                last_logits: vec![],
            },
        ).is_ok());
    }
    
    assert_eq!(cache.len(), capacity);
    
    // Try to insert one more - should succeed and evict LRU
    let tokens = vec![99_u32; 2];
    assert!(cache.insert(
        tokens,
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 2),
            last_logits: vec![],
        },
    ).is_ok());
    
    assert_eq!(cache.len(), capacity);
}

#[test]
fn test_cache_with_maximum_valid_token_ids() {
    let mut cache = PrefixCache::new(10);
    
    // Use maximum reasonable token ID (just under the validation threshold)
    let max_valid_token = 999_999;
    let tokens = vec![max_valid_token];
    
    let result = cache.insert(
        tokens,
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 1),
            last_logits: vec![],
        },
    );
    
    // Maximum valid token should be accepted
    assert!(result.is_ok());
}

#[test]
fn test_cache_dimension_boundary_conditions() {
    let mut cache = PrefixCache::new(10);
    
    // Test exact dimension match
    let tokens = vec![1, 2, 3, 4, 5];
    let result = cache.insert(
        tokens.clone(),
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 5), // Exact match
            last_logits: vec![],
        },
    );
    assert!(result.is_ok());
    
    // Test dimension slightly less than tokens (should be OK)
    let tokens2 = vec![6, 7, 8];
    let result2 = cache.insert(
        tokens2,
        CachedPrefix {
            kv: create_test_kv_cache(2, 128, 2), // 2 vs 3 tokens
            last_logits: vec![],
        },
    );
    assert!(result2.is_ok());
}

// ===== Error Message Quality Tests =====

#[test]
fn test_error_messages_are_informative() {
    let errors = vec![
        KvError::InvalidDimensions {
            expected: 128,
            actual: 256,
            operation: "convolution".to_string(),
        },
        KvError::EmptyCache("lookup".to_string()),
        KvError::InvalidTokenSequence {
            reason: "out of vocabulary".to_string(),
            position: Some(42),
        },
        CacheValidationError::EmptySequence.into(),
        CacheValidationError::SequenceTooLong {
            length: 1000,
            max_length: 100,
        }.into(),
    ];
    
    for error in errors {
        let error_str = error.to_string();
        
        // All error messages should be reasonably informative
        assert!(!error_str.is_empty());
        assert!(error_str.len() > 10);
        assert!(!error_str.contains("Error(")); // Not using default Debug formatting
        
        // Should contain relevant information
        assert!(error_str.chars().all(|c| c.is_ascii() || c.is_alphanumeric()));
    }
}

#[test]
fn test_context_enriched_errors() {
    let ctx = KvContext::new("test_operation")
        .with_layer(5)
        .with_position(10)
        .with_sequence_length(100);
    
    let err = ctx.dimension_error(256, 128);
    let err_str = err.to_string();
    
    assert!(err_str.contains("test_operation"));
    assert!(err_str.contains("256"));
    assert!(err_str.contains("128"));
}