realizar 0.8.5

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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

    /// Create a minimal test config for testing
    fn test_config() -> GGUFConfig {
        GGUFConfig {
            architecture: "test".to_string(),
            constraints: crate::gguf::ArchConstraints::from_architecture("test"),
            hidden_dim: 64,
            intermediate_dim: 128,
            num_heads: 4,
            num_kv_heads: 4,
            num_layers: 1,
            vocab_size: 100,
            rope_theta: 10000.0,
            context_length: 512,
            eps: 1e-5,
            rope_type: 0,
            explicit_head_dim: None,
            bos_token_id: None,
            eos_token_id: None,
        }
    }

    /// Create a minimal cached sync model for testing
    fn create_test_model() -> OwnedQuantizedModelCachedSync {
        let config = test_config();
        let model = create_test_model_with_config(&config);
        OwnedQuantizedModelCachedSync::new(model)
    }

    // ========================================================================
    // Constructor Tests
    // ========================================================================

    #[test]
    fn test_new_creates_empty_scheduler() {
        let model = create_test_model();
        // Scheduler should be None initially (lazy init)
        assert!(!model.is_gpu_cache_warm());
    }

    #[test]
    fn test_model_accessor() {
        let config = test_config();
        let model = create_test_model();
        assert_eq!(model.model().config.hidden_dim, config.hidden_dim);
        assert_eq!(model.model().config.vocab_size, config.vocab_size);
    }

    // ========================================================================
    // GPU Cache Tests
    // ========================================================================

    #[test]
    fn test_is_gpu_cache_warm_false_initially() {
        let model = create_test_model();
        assert!(!model.is_gpu_cache_warm());
    }

    #[test]
    fn test_gpu_cache_memory_zero_when_not_warm() {
        let model = create_test_model();
        assert_eq!(model.gpu_cache_memory(), 0);
    }

    #[test]
    fn test_warmup_gpu_cache_success() {
        let model = create_test_model();
        let result = model.warmup_gpu_cache();
        assert!(result.is_ok());
        let (memory_bytes, cached_count) = result.expect("expected value");
        assert!(memory_bytes > 0);
        assert_eq!(cached_count, 1); // 1 layer
        assert!(model.is_gpu_cache_warm());
    }

    #[test]
    fn test_get_dequantized_ffn_weights_none_before_warmup() {
        let model = create_test_model();
        let weights = model.get_dequantized_ffn_weights(0);
        assert!(weights.is_none());
    }

    #[test]
    fn test_get_dequantized_ffn_weights_some_after_warmup() {
        let model = create_test_model();
        let _ = model.warmup_gpu_cache();
        let weights = model.get_dequantized_ffn_weights(0);
        assert!(weights.is_some());
        let w = weights.expect("w");
        // Check dimensions match config
        let config = test_config();
        assert_eq!(w.up.len(), config.hidden_dim * config.intermediate_dim);
        assert_eq!(w.down.len(), config.intermediate_dim * config.hidden_dim);
    }

    #[test]
    fn test_get_dequantized_ffn_weights_out_of_bounds() {
        let model = create_test_model();
        let _ = model.warmup_gpu_cache();
        // Layer index 99 doesn't exist
        let weights = model.get_dequantized_ffn_weights(99);
        assert!(weights.is_none());
    }

    // ========================================================================
    // Batch Stats Tests
    // ========================================================================

    #[test]
    fn test_batch_stats_before_warmup() {
        let model = create_test_model();
        let stats = model.batch_stats();
        assert!(!stats.gpu_cache_ready);
        assert_eq!(stats.cache_memory_gb, 0.0);
        assert_eq!(stats.num_layers, 1);
        assert_eq!(stats.hidden_dim, 64);
        assert_eq!(stats.intermediate_dim, 128);
    }

    #[test]
    fn test_batch_stats_after_warmup() {
        let model = create_test_model();
        let _ = model.warmup_gpu_cache();
        let stats = model.batch_stats();
        assert!(stats.gpu_cache_ready);
        assert!(stats.cache_memory_gb > 0.0);
    }

    #[test]
    fn test_batch_stats_recommended_values() {
        let model = create_test_model();
        let stats = model.batch_stats();
        assert_eq!(stats.recommended_batch_size, 32);
        assert_eq!(stats.max_batch_size, 64);
    }

    // ========================================================================
    // Adaptive Attention Tests
    // ========================================================================

    #[test]
    fn test_adaptive_fused_attention_short_sequence_uses_cpu() {
        let model = create_test_model();
        let config = test_config();
        let head_dim = config.hidden_dim / config.num_heads;
        let seq_len = 4; // Short sequence, below GPU threshold (64)
        let scale = 1.0 / (head_dim as f32).sqrt();

        let q = vec![0.1f32; seq_len * head_dim];
        let k = vec![0.2f32; seq_len * head_dim];
        let v = vec![0.3f32; seq_len * head_dim];

        let result = model.adaptive_fused_attention(&q, &k, &v, seq_len, head_dim, scale);
        assert!(result.is_ok());
        let output = result.expect("output");
        assert_eq!(output.len(), seq_len * head_dim);
    }

    #[test]
    fn test_adaptive_fused_attention_long_sequence_uses_gpu() {
        let model = create_test_model();
        let config = test_config();
        let head_dim = config.hidden_dim / config.num_heads;
        let seq_len = 128; // Long sequence, above GPU threshold (64)
        let scale = 1.0 / (head_dim as f32).sqrt();

        let q = vec![0.1f32; seq_len * head_dim];
        let k = vec![0.2f32; seq_len * head_dim];
        let v = vec![0.3f32; seq_len * head_dim];

        let result = model.adaptive_fused_attention(&q, &k, &v, seq_len, head_dim, scale);
        assert!(result.is_ok());
        let output = result.expect("output");
        assert_eq!(output.len(), seq_len * head_dim);
    }

    // ========================================================================
    // Adaptive Multihead Attention Tests
    // ========================================================================

    #[test]
    fn test_adaptive_multihead_attention_basic() {
        let model = create_test_model();
        let config = test_config();
        let hidden_dim = config.hidden_dim;
        let seq_len = 4;

        let q = vec![0.1f32; seq_len * hidden_dim];
        let k = vec![0.2f32; seq_len * hidden_dim];
        let v = vec![0.3f32; seq_len * hidden_dim];

        let result = model.adaptive_multihead_attention(&q, &k, &v, seq_len);
        assert!(result.is_ok());
        let output = result.expect("output");
        assert_eq!(output.len(), seq_len * hidden_dim);
    }

    // ========================================================================
    // Forward Pass Tests
    // ========================================================================

    #[test]
    fn test_forward_batch_gpu_cached_basic() {
        let model = create_test_model();
        let token_ids = vec![1u32, 2, 3];

        let result = model.forward_batch_gpu_cached(&token_ids);
        assert!(result.is_ok());
        let output = result.expect("output");
        let config = test_config();
        assert_eq!(output.len(), token_ids.len() * config.vocab_size);
    }

    #[test]
    fn test_forward_batch_gpu_cached_empty_input() {
        let model = create_test_model();
        let token_ids: Vec<u32> = vec![];

        let result = model.forward_batch_gpu_cached(&token_ids);
        assert!(result.is_ok());
        let output = result.expect("output");
        assert_eq!(output.len(), 0);
    }

    // ========================================================================
    // Batch FFN GPU Tests
    // ========================================================================

    #[test]
    fn test_batch_ffn_gpu_not_warmed() {
        let model = create_test_model();
        let config = test_config();
        let hidden_states = vec![0.1f32; config.hidden_dim];

        let result = model.batch_ffn_gpu(&hidden_states, 0);
        assert!(result.is_err());
        match result {
            Err(RealizarError::UnsupportedOperation { operation, reason }) => {
                assert_eq!(operation, "batch_ffn_gpu");
                assert!(reason.contains("not cached"));
            },
            _ => panic!("Expected UnsupportedOperation error"),
        }
    }

    #[test]
    fn test_batch_ffn_gpu_after_warmup() {
        let model = create_test_model();
        let config = test_config();
        let _ = model.warmup_gpu_cache();

        let batch_size = 2;
        let hidden_states = vec![0.1f32; batch_size * config.hidden_dim];

        let result = model.batch_ffn_gpu(&hidden_states, 0);
        assert!(result.is_ok());
        let output = result.expect("output");
        assert_eq!(output.len(), batch_size * config.hidden_dim);
    }

    #[test]
    fn test_batch_ffn_gpu_empty_batch() {
        let model = create_test_model();
        let _ = model.warmup_gpu_cache();
        let hidden_states: Vec<f32> = vec![];

        let result = model.batch_ffn_gpu(&hidden_states, 0);
        assert!(result.is_err());
        match result {
            Err(RealizarError::UnsupportedOperation { operation, reason }) => {
                assert_eq!(operation, "batch_ffn_gpu");
                assert!(reason.contains("Empty batch"));
            },
            _ => panic!("Expected UnsupportedOperation error"),
        }
    }

    // ========================================================================
    // Batch Generate Tests
    // ========================================================================

    #[test]
    fn test_batch_generate_gpu_not_warmed() {
        let model = create_test_model();
        let prompts = vec![vec![1u32, 2, 3]];
        let config = QuantizedGenerateConfig::deterministic(5);

        let result = model.batch_generate_gpu(&prompts, &config);
        assert!(result.is_err());
        match result {
            Err(RealizarError::UnsupportedOperation { operation, reason }) => {
                assert_eq!(operation, "batch_generate_gpu");
                assert!(reason.contains("not warmed up"));
            },
            _ => panic!("Expected UnsupportedOperation error"),
        }
    }

    #[test]
    fn test_batch_generate_gpu_empty_prompts() {
        let model = create_test_model();
        let _ = model.warmup_gpu_cache();
        let prompts: Vec<Vec<u32>> = vec![];
        let config = QuantizedGenerateConfig::deterministic(5);

        let result = model.batch_generate_gpu(&prompts, &config);
        assert!(result.is_ok());
        assert!(result.expect("expected value").is_empty());
    }

    // ========================================================================
    // Forward Batch With GPU FFN Tests
    // ========================================================================

    #[test]
    fn test_forward_batch_with_gpu_ffn_empty() {
        let model = create_test_model();
        let token_ids: Vec<u32> = vec![];
        let mut caches: Vec<crate::gguf::OwnedQuantizedKVCache> = vec![];
        let positions: Vec<usize> = vec![];

        let result = model.forward_batch_with_gpu_ffn(&token_ids, &mut caches, &positions);
        assert!(result.is_ok());
        assert!(result.expect("expected value").is_empty());
    }

    #[test]
    fn test_forward_batch_with_gpu_ffn_mismatched_sizes() {
        let model = create_test_model();
        let config = test_config();
        let max_seq_len = 64;

        let token_ids = vec![1u32, 2, 3];
        let mut caches = vec![
            crate::gguf::OwnedQuantizedKVCache::from_config(&config, max_seq_len),
            crate::gguf::OwnedQuantizedKVCache::from_config(&config, max_seq_len),
        ];
        let positions = vec![0usize];

        let result = model.forward_batch_with_gpu_ffn(&token_ids, &mut caches, &positions);
        assert!(result.is_err());
        match result {
            Err(RealizarError::InvalidShape { reason }) => {
                assert!(reason.contains("mismatch"));
            },
            _ => panic!("Expected InvalidShape error"),
        }
    }

    #[test]
    fn test_forward_batch_with_gpu_ffn_small_batch_cpu_path() {
        let model = create_test_model();
        let config = test_config();
        let max_seq_len = 64;

        // Small batch (< 32) should use CPU path
        let token_ids = vec![1u32, 2, 3];
        let mut caches: Vec<_> = (0..3)
            .map(|_| crate::gguf::OwnedQuantizedKVCache::from_config(&config, max_seq_len))
            .collect();
        let positions = vec![0usize, 0, 0];

        let result = model.forward_batch_with_gpu_ffn(&token_ids, &mut caches, &positions);
        assert!(result.is_ok());
        let logits = result.expect("logits");
        assert_eq!(logits.len(), 3);
        assert_eq!(logits[0].len(), config.vocab_size);
    }

    // ========================================================================
    // Generate With Cache Tests
    // ========================================================================

    #[test]
    fn test_generate_with_cache_basic() {
        let model = create_test_model();
        let prompt = vec![1u32, 2, 3];
        let config = QuantizedGenerateConfig::deterministic(3);

        let result = model.generate_with_cache(&prompt, &config);
        assert!(result.is_ok());
        let output = result.expect("output");
        // Output includes prompt + generated tokens
        assert!(output.len() >= prompt.len());
    }

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

    #[test]
    fn test_send_sync_bounds() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<OwnedQuantizedModelCachedSync>();
    }

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

        let model = Arc::new(create_test_model());
        let _ = model.warmup_gpu_cache();

        let mut handles = vec![];
        for _ in 0..4 {
            let model_clone = Arc::clone(&model);
            let handle = thread::spawn(move || {
                let stats = model_clone.batch_stats();
                assert!(stats.gpu_cache_ready);
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().expect("thread join failed");
        }
    }

    // ========================================================================
    // Batch Matmul Input Validation Tests
    // ========================================================================

    #[test]
    fn test_batch_matmul_invalid_input_size() {
        let model = create_test_model();
        let _ = model.warmup_gpu_cache();

        // Input size doesn't match batch_size * hidden_dim
        let config = test_config();
        let wrong_size_input = vec![0.1f32; config.hidden_dim + 1];

        let result = model.batch_ffn_gpu(&wrong_size_input, 0);
        // The batch_size calculation would be wrong, leading to error
        assert!(result.is_ok() || result.is_err());
    }