aprender-serve 0.64.0

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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Phase 54: Fixture-Based GGUF Loader Tests
//!
//! Uses the ModelFixture pattern for standardized testing with RAII-based cleanup.
//! Covers loader.rs dark zones with various model configurations.

use crate::fixtures::{ModelConfig, ModelFixture, ModelFormat};
use crate::gguf::GGUFModel;

// =============================================================================
// Architecture Loading Tests
// =============================================================================

/// Test loading a tiny llama-style model via fixture
#[test]
fn test_fixture_load_tiny_llama() {
    let fixture = ModelFixture::gguf("tiny_llama", ModelConfig::tiny());
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    // Verify architecture
    assert_eq!(model.architecture(), Some("llama"));

    // Verify dimensions from config
    assert_eq!(model.embedding_dim(), Some(64));
    assert_eq!(model.num_layers(), Some(1));
    assert_eq!(model.num_heads(), Some(4));
    assert_eq!(model.num_kv_heads(), Some(4));

    // Verify tensors exist
    assert!(!model.tensors.is_empty());
}

/// Test loading a small model (larger than tiny)
#[test]
fn test_fixture_load_small_model() {
    let fixture = ModelFixture::gguf("small", ModelConfig::small());
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    assert_eq!(model.embedding_dim(), Some(256));
    assert_eq!(model.num_layers(), Some(2));
    assert_eq!(model.num_heads(), Some(8));
    assert_eq!(model.context_length(), Some(1024));
}

/// Test loading a GQA model (different num_heads vs num_kv_heads)
#[test]
fn test_fixture_load_gqa_model() {
    let fixture = ModelFixture::gguf("gqa", ModelConfig::gqa());
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    // GQA has 8 Q heads but only 2 KV heads
    assert_eq!(model.num_heads(), Some(8));
    assert_eq!(model.num_kv_heads(), Some(2));
}

/// Test loading a Phi-2 style model (LayerNorm + GELU)
#[test]
fn test_fixture_load_phi_model() {
    let fixture = ModelFixture::gguf("phi2", ModelConfig::phi());
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    assert_eq!(model.architecture(), Some("phi2"));
}

/// Test loading a Qwen-style model
#[test]
fn test_fixture_load_qwen_model() {
    let fixture = ModelFixture::gguf("qwen", ModelConfig::qwen());
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    assert_eq!(model.architecture(), Some("qwen2"));
    assert_eq!(model.num_kv_heads(), Some(4));
    // Qwen uses different RoPE theta
    assert!(
        model
            .rope_freq_base()
            .expect("test value should be present")
            > 100000.0
    );
}

// =============================================================================
// Error Handling Tests
// =============================================================================

/// Test invalid magic detection
#[test]
fn test_fixture_invalid_magic_detected() {
    let fixture = ModelFixture::gguf_invalid_magic("bad_magic");
    let bytes = fixture.read_bytes().expect("read fixture");

    let result = GGUFModel::from_bytes(&bytes);
    assert!(result.is_err());

    let err = result.unwrap_err();
    let err_str = err.to_string();
    // Should mention invalid magic or GGUF header
    assert!(err_str.contains("magic") || err_str.contains("GGUF") || err_str.contains("invalid"));
}

/// Test invalid version detection
#[test]
fn test_fixture_invalid_version_detected() {
    let fixture = ModelFixture::gguf_invalid_version("bad_version");
    let bytes = fixture.read_bytes().expect("read fixture");

    let result = GGUFModel::from_bytes(&bytes);
    assert!(result.is_err());

    let err = result.unwrap_err();
    let err_str = err.to_string();
    // Should mention version or unsupported
    assert!(
        err_str.contains("version") || err_str.contains("unsupported") || err_str.contains("999")
    );
}

// =============================================================================
// Metadata Tests
// =============================================================================

/// Test RoPE frequency base metadata
#[test]
fn test_fixture_rope_freq_base() {
    let config = ModelConfig::tiny().with_architecture("llama");
    let fixture = ModelFixture::gguf("rope_test", config);
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    let rope_base = model.rope_freq_base();
    assert!(rope_base.is_some());
    assert!((rope_base.expect("test value should be present") - 10000.0).abs() < 1.0);
}

/// Test RMS epsilon metadata
#[test]
fn test_fixture_rms_epsilon() {
    let config = ModelConfig::tiny();
    let fixture = ModelFixture::gguf("eps_test", config);
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    let eps = model.rms_epsilon();
    assert!(eps.is_some());
    assert!(eps.expect("test value should be present") < 0.001);
}

/// Test context length metadata
#[test]
fn test_fixture_context_length() {
    let config = ModelConfig::small();
    let fixture = ModelFixture::gguf("ctx_test", config);
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    assert_eq!(model.context_length(), Some(1024));
}

// =============================================================================
// Custom Configuration Tests
// =============================================================================

/// Test builder pattern for custom configs
#[test]
fn test_fixture_custom_config() {
    let config = ModelConfig::tiny()
        .with_hidden_dim(128)
        .with_layers(3)
        .with_vocab_size(5000)
        .with_gqa(8, 2);

    let fixture = ModelFixture::gguf("custom", config);
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    assert_eq!(model.embedding_dim(), Some(128));
    assert_eq!(model.num_layers(), Some(3));
    assert_eq!(model.num_heads(), Some(8));
    assert_eq!(model.num_kv_heads(), Some(2));
}

/// Test model with many layers
#[test]
fn test_fixture_many_layers() {
    let config = ModelConfig::tiny().with_layers(8);
    let fixture = ModelFixture::gguf("deep", config);
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    assert_eq!(model.num_layers(), Some(8));
}

/// Test model with large vocabulary
#[test]
fn test_fixture_large_vocab() {
    let config = ModelConfig::tiny().with_vocab_size(32000);
    let fixture = ModelFixture::gguf("large_vocab", config);
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    // Verify embedding tensor exists
    let embed = model.tensors.iter().find(|t| t.name.contains("embd"));
    assert!(embed.is_some());
}

// =============================================================================
// Format Tests
// =============================================================================

/// Test ModelFormat::Gguf extension
#[test]
fn test_fixture_format_gguf() {
    let fixture = ModelFixture::gguf("fmt_test", ModelConfig::tiny());
    assert_eq!(fixture.format(), ModelFormat::Gguf);
    assert!(fixture.path().to_string_lossy().ends_with(".gguf"));
}

/// Test ModelFormat::SafeTensors extension
#[test]
fn test_fixture_format_safetensors() {
    let fixture = ModelFixture::safetensors("st_test", ModelConfig::tiny());
    assert_eq!(fixture.format(), ModelFormat::SafeTensors);
    assert!(fixture.path().to_string_lossy().ends_with(".safetensors"));
}

/// Test ModelFormat::Apr extension
#[test]
fn test_fixture_format_apr() {
    let fixture = ModelFixture::apr("apr_test", ModelConfig::tiny());
    assert_eq!(fixture.format(), ModelFormat::Apr);
    assert!(fixture.path().to_string_lossy().ends_with(".apr"));
}

// =============================================================================
// RAII Cleanup Tests
// =============================================================================

/// Test that fixture cleanup works correctly
#[test]
fn test_fixture_cleanup() {
    let path = {
        let fixture = ModelFixture::gguf("cleanup_test", ModelConfig::tiny());
        let p = fixture.path().to_path_buf();
        assert!(p.exists(), "File should exist during fixture lifetime");
        p
    };
    // After fixture drops, file should be cleaned up
    assert!(
        !path.exists(),
        "File should be cleaned up after fixture drops"
    );
}

/// Test multiple fixtures in same test
#[test]
fn test_fixture_multiple_coexist() {
    let f1 = ModelFixture::gguf("multi_1", ModelConfig::tiny());
    let f2 = ModelFixture::gguf("multi_2", ModelConfig::small());
    let f3 = ModelFixture::gguf("multi_3", ModelConfig::gqa());

    // All should exist simultaneously
    assert!(f1.path().exists());
    assert!(f2.path().exists());
    assert!(f3.path().exists());

    // Paths should be different
    assert_ne!(f1.path(), f2.path());
    assert_ne!(f2.path(), f3.path());
}

// =============================================================================
// Tensor Loading Tests
// =============================================================================

/// Test loading token embedding tensor
#[test]
fn test_fixture_load_embedding_tensor() {
    let fixture = ModelFixture::gguf("embed_test", ModelConfig::tiny());
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    // Find and load embedding
    let embed_tensor = model.tensors.iter().find(|t| t.name == "token_embd.weight");
    assert!(
        embed_tensor.is_some(),
        "Should have token_embd.weight tensor"
    );

    let tensor_info = embed_tensor.expect("test value should be present");
    let embed_data = model.get_tensor_f32(&tensor_info.name, &bytes);
    assert!(embed_data.is_ok(), "Should be able to load embedding data");
}

/// Test loading quantized Q4_K tensor
#[test]
fn test_fixture_load_q4k_tensor() {
    let fixture = ModelFixture::gguf("q4k_test", ModelConfig::tiny());
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    // Find a Q4_K tensor (attention weights)
    let q4k_tensor = model.tensors.iter().find(|t| t.name.contains("attn_qkv"));
    assert!(q4k_tensor.is_some(), "Should have Q4_K attention tensor");

    let tensor_info = q4k_tensor.expect("test value should be present");
    // Q4_K type is 12
    assert_eq!(tensor_info.qtype, 12, "Should be Q4_K type");
}

/// Test loading output norm tensor (F32)
#[test]
fn test_fixture_load_f32_tensor() {
    let fixture = ModelFixture::gguf("f32_test", ModelConfig::tiny());
    let bytes = fixture.read_bytes().expect("read fixture");

    let model = GGUFModel::from_bytes(&bytes).expect("parse GGUF");

    // Find norm tensor (should be F32)
    let norm_tensor = model
        .tensors
        .iter()
        .find(|t| t.name.contains("output_norm"));
    assert!(norm_tensor.is_some(), "Should have output_norm tensor");

    let tensor_info = norm_tensor.expect("test value should be present");
    // F32 type is 0
    assert_eq!(tensor_info.qtype, 0, "Norm should be F32 type");
}

// =============================================================================
// Cross-Architecture Tests
// =============================================================================

/// Test that different architectures produce different metadata
#[test]
fn test_fixture_architecture_differences() {
    let llama = ModelFixture::gguf("arch_llama", ModelConfig::tiny().with_architecture("llama"));
    let phi = ModelFixture::gguf("arch_phi", ModelConfig::phi());
    let qwen = ModelFixture::gguf("arch_qwen", ModelConfig::qwen());

    let llama_bytes = llama.read_bytes().expect("read");
    let phi_bytes = phi.read_bytes().expect("read");
    let qwen_bytes = qwen.read_bytes().expect("read");

    let llama_model = GGUFModel::from_bytes(&llama_bytes).expect("parse");
    let phi_model = GGUFModel::from_bytes(&phi_bytes).expect("parse");
    let qwen_model = GGUFModel::from_bytes(&qwen_bytes).expect("parse");

    // Different architectures
    assert_ne!(llama_model.architecture(), phi_model.architecture());
    assert_ne!(phi_model.architecture(), qwen_model.architecture());

    // All should be valid
    assert!(llama_model.architecture().is_some());
    assert!(phi_model.architecture().is_some());
    assert!(qwen_model.architecture().is_some());
}

/// Test MHA vs GQA configurations
#[test]
fn test_fixture_mha_vs_gqa() {
    let mha_config = ModelConfig::tiny(); // num_heads == num_kv_heads
    let gqa_config = ModelConfig::gqa(); // num_heads != num_kv_heads

    let mha = ModelFixture::gguf("mha", mha_config);
    let gqa = ModelFixture::gguf("gqa", gqa_config);

    let mha_bytes = mha.read_bytes().expect("read");
    let gqa_bytes = gqa.read_bytes().expect("read");

    let mha_model = GGUFModel::from_bytes(&mha_bytes).expect("parse");
    let gqa_model = GGUFModel::from_bytes(&gqa_bytes).expect("parse");

    // MHA: num_heads == num_kv_heads
    assert_eq!(mha_model.num_heads(), mha_model.num_kv_heads());

    // GQA: num_heads != num_kv_heads
    assert_ne!(gqa_model.num_heads(), gqa_model.num_kv_heads());

    // GQA should have more Q heads than KV heads
    assert!(
        gqa_model.num_heads().expect("test value should be present")
            > gqa_model
                .num_kv_heads()
                .expect("test value should be present")
    );
}

// =============================================================================
// Header Validation Tests
// =============================================================================

/// Test GGUF magic bytes are correct
#[test]
fn test_fixture_gguf_magic() {
    let fixture = ModelFixture::gguf("magic_test", ModelConfig::tiny());
    let bytes = fixture.read_bytes().expect("read");

    // First 4 bytes should be GGUF magic: 0x46554747 ("GGUF" in little endian)
    assert_eq!(&bytes[0..4], &[0x47, 0x47, 0x55, 0x46]);
}

/// Test GGUF version is 3
#[test]
fn test_fixture_gguf_version() {
    let fixture = ModelFixture::gguf("version_test", ModelConfig::tiny());
    let bytes = fixture.read_bytes().expect("read");

    // Bytes 4-8 should be version (little endian u32)
    let version = u32::from_le_bytes(bytes[4..8].try_into().expect("try_into conversion failed"));
    assert_eq!(version, 3, "Should be GGUF v3");
}

// =============================================================================
// Config Accessor Tests
// =============================================================================

/// Test ModelConfig accessors
#[test]
fn test_fixture_config_accessors() {
    let config = ModelConfig::small();
    let fixture = ModelFixture::gguf("accessor_test", config.clone());

    let returned_config = fixture.config();
    assert_eq!(returned_config.hidden_dim, 256);
    assert_eq!(returned_config.intermediate_dim, 512);
    assert_eq!(returned_config.num_heads, 8);
    assert_eq!(returned_config.num_layers, 2);
}

/// Test ModelConfig::default is tiny
#[test]
fn test_model_config_default() {
    let default = ModelConfig::default();
    let tiny = ModelConfig::tiny();

    assert_eq!(default.hidden_dim, tiny.hidden_dim);
    assert_eq!(default.num_layers, tiny.num_layers);
}