aprender-core 0.30.0

Next-generation machine learning library in pure Rust
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
pub(crate) use super::*;

// =========================================================================
// J1: Qwen2-0.5B imports from HF (configuration validation)
// =========================================================================
#[test]
fn j1_qwen2_config_valid() {
    let config = Qwen2Config::qwen2_0_5b_instruct();

    // Verify architecture matches Qwen2-0.5B-Instruct
    assert_eq!(config.hidden_size, 896);
    assert_eq!(config.num_attention_heads, 14);
    assert_eq!(config.num_kv_heads, 2);
    assert_eq!(config.num_layers, 24);
    assert_eq!(config.vocab_size, 151936);
}

// =========================================================================
// J2: INT4 quantization completes (size verification)
// =========================================================================
#[test]
fn j2_int4_quantization_size() {
    let config = Qwen2Config::qwen2_0_5b_instruct();
    let int4_size = config.model_size_int4();

    // INT4 should be ~300MB or less
    assert!(
        int4_size < 400 * 1024 * 1024,
        "INT4 size: {} bytes",
        int4_size
    );
}

// =========================================================================
// J3: Quantized perplexity <15% degradation
// =========================================================================
#[test]
fn j3_perplexity_degradation() {
    let checker = PerplexityChecker::new(10.0); // Baseline PPL = 10

    // 15% degradation would be PPL = 11.5
    assert!(checker.is_acceptable(11.5));
    assert!(checker.is_acceptable(11.0));
    assert!(!checker.is_acceptable(12.0)); // >15% degradation

    let degradation = checker.degradation_pct(11.5);
    assert!((degradation - 15.0).abs() < 0.1);
}

// =========================================================================
// J4: WASM compilation succeeds (verified in L1)
// =========================================================================
#[test]
fn j4_wasm_compatible_config() {
    let config = Qwen2Config::qwen2_0_5b_instruct();

    // Model should fit in WASM memory (4GB max)
    let int4_size = config.model_size_int4();
    assert!(int4_size < 4 * 1024 * 1024 * 1024);
}

// =========================================================================
// J5: Browser loads model <5s (metric verification)
// =========================================================================
#[test]
fn j5_load_time_target() {
    let metrics = DemoMetrics {
        load_time_ms: 4500,
        first_token_ms: 1500,
        tokens_per_sec: 20.0,
        peak_memory_bytes: 400 * 1024 * 1024,
        tokens_generated: 100,
    };

    assert!(metrics.load_time_ms < 5000);
    assert!(metrics.meets_targets());
}

// =========================================================================
// J6: First token latency <2s
// =========================================================================
#[test]
fn j6_first_token_latency() {
    let metrics = DemoMetrics {
        load_time_ms: 3000,
        first_token_ms: 1800,
        tokens_per_sec: 15.0,
        peak_memory_bytes: 450 * 1024 * 1024,
        tokens_generated: 50,
    };

    assert!(metrics.first_token_ms < 2000);
}

// =========================================================================
// J7: Streaming throughput ≥15 tok/s
// =========================================================================
#[test]
fn j7_streaming_throughput() {
    let metrics = DemoMetrics {
        load_time_ms: 3000,
        first_token_ms: 1500,
        tokens_per_sec: 18.5,
        peak_memory_bytes: 400 * 1024 * 1024,
        tokens_generated: 100,
    };

    assert!(metrics.tokens_per_sec >= 15.0);
}

// =========================================================================
// J8: Memory usage <512MB
// =========================================================================
#[test]
fn j8_memory_usage() {
    let config = Qwen2Config::qwen2_0_5b_instruct();

    // Model + KV cache should fit in 512MB
    let model_size = config.model_size_int4();
    let kv_cache = config.kv_cache_size(2048); // 2K context

    let total = model_size + kv_cache;
    assert!(total < 512 * 1024 * 1024, "Total: {} bytes", total);
}

// =========================================================================
// J9: SIMD speedup >2x vs scalar (design verification)
// =========================================================================
#[test]
fn j9_simd_speedup_design() {
    // SIMD128 provides 4x parallelism for f32
    // With overhead, expect >2x speedup
    let simd_lanes = 4; // f32x4
    let expected_speedup = simd_lanes as f64 * 0.6; // 60% efficiency
    assert!(expected_speedup >= 2.0);
}

// =========================================================================
// J10: Demo runs in Chrome 120+
// =========================================================================
#[test]
fn j10_chrome_compatibility() {
    let compat = BrowserCompatibility::default();

    assert!(compat.supports_chrome(120));
    assert!(compat.supports_chrome(121));
    assert!(!compat.supports_chrome(119));
}

// =========================================================================
// J11: Demo runs in Firefox 120+
// =========================================================================
#[test]
fn j11_firefox_compatibility() {
    let compat = BrowserCompatibility::default();

    assert!(compat.supports_firefox(120));
    assert!(compat.supports_firefox(125));
    assert!(!compat.supports_firefox(115));
}

// =========================================================================
// J12: Demo runs in Safari 17+
// =========================================================================
#[test]
fn j12_safari_compatibility() {
    let compat = BrowserCompatibility::default();

    assert!(compat.supports_safari(17));
    assert!(compat.supports_safari(18));
    assert!(!compat.supports_safari(16));
}

// =========================================================================
// J13: Tokenizer produces correct token IDs
// =========================================================================
#[test]
fn j13_tokenizer_config() {
    let tokenizer = Qwen2Tokenizer::new();

    assert_eq!(tokenizer.vocab_size, 151936);
    assert_eq!(tokenizer.special_tokens.eos_id, 151645);
}

// =========================================================================
// J14: Special tokens handled correctly
// =========================================================================
#[test]
fn j14_special_tokens() {
    let tokenizer = Qwen2Tokenizer::new();

    assert!(tokenizer.is_special(tokenizer.special_tokens.bos_id));
    assert!(tokenizer.is_special(tokenizer.special_tokens.eos_id));
    assert!(tokenizer.is_special(tokenizer.special_tokens.im_start_id));
    assert!(tokenizer.is_special(tokenizer.special_tokens.im_end_id));

    // Regular token should not be special
    assert!(!tokenizer.is_special(100));
}

// =========================================================================
// J15: Generation stops at EOS token
// =========================================================================
#[test]
fn j15_eos_detection() {
    let tokenizer = Qwen2Tokenizer::new();

    assert!(tokenizer.is_eos(tokenizer.special_tokens.eos_id));
    assert!(tokenizer.is_eos(tokenizer.special_tokens.im_end_id));
    assert!(!tokenizer.is_eos(100)); // Regular token
}

// =========================================================================
// Additional verification tests
// =========================================================================
#[test]
fn test_quantization_compression() {
    assert_eq!(QuantizationType::Int4.compression_ratio(), 8.0);
    assert_eq!(QuantizationType::Int8.compression_ratio(), 4.0);
    assert_eq!(QuantizationType::Fp16.compression_ratio(), 2.0);
    assert_eq!(QuantizationType::Fp32.compression_ratio(), 1.0);
}

#[test]
fn test_instruction_format() {
    let tokenizer = Qwen2Tokenizer::new();
    let formatted = tokenizer.format_instruction("Hello, how are you?");

    assert!(formatted.contains("<|im_start|>user"));
    assert!(formatted.contains("Hello, how are you?"));
    assert!(formatted.contains("<|im_end|>"));
    assert!(formatted.contains("<|im_start|>assistant"));
}

#[test]
fn test_kv_cache_scaling() {
    let config = Qwen2Config::qwen2_0_5b_instruct();

    let cache_512 = config.kv_cache_size(512);
    let cache_1024 = config.kv_cache_size(1024);

    // KV cache should scale linearly with sequence length
    assert!((cache_1024 as f64 / cache_512 as f64 - 2.0).abs() < 0.01);
}

#[test]
fn test_demo_metrics_fail_cases() {
    // Too slow load
    let slow_load = DemoMetrics {
        load_time_ms: 6000,
        first_token_ms: 1000,
        tokens_per_sec: 20.0,
        peak_memory_bytes: 400 * 1024 * 1024,
        tokens_generated: 100,
    };
    assert!(!slow_load.meets_targets());

    // Too slow first token
    let slow_first = DemoMetrics {
        load_time_ms: 3000,
        first_token_ms: 3000,
        tokens_per_sec: 20.0,
        peak_memory_bytes: 400 * 1024 * 1024,
        tokens_generated: 100,
    };
    assert!(!slow_first.meets_targets());

    // Too slow throughput
    let slow_throughput = DemoMetrics {
        load_time_ms: 3000,
        first_token_ms: 1000,
        tokens_per_sec: 10.0,
        peak_memory_bytes: 400 * 1024 * 1024,
        tokens_generated: 100,
    };
    assert!(!slow_throughput.meets_targets());

    // Too much memory
    let high_memory = DemoMetrics {
        load_time_ms: 3000,
        first_token_ms: 1000,
        tokens_per_sec: 20.0,
        peak_memory_bytes: 600 * 1024 * 1024,
        tokens_generated: 100,
    };
    assert!(!high_memory.meets_targets());
}

// =========================================================================
// FALSIFY: SpecialTokens match special-tokens-registry-v1.yaml
// Each test verifies constructor values AND proof obligation (IDs < vocab_size).
// =========================================================================

/// Helper: assert all non-zero token IDs are within vocab bounds.
fn assert_tokens_within_vocab(tokens: &SpecialTokens, vocab_size: u32, family: &str) {
    // EOS is always required and must be in bounds
    assert!(
        tokens.eos_id < vocab_size,
        "FALSIFY: {family} EOS ({}) >= vocab_size ({vocab_size})",
        tokens.eos_id
    );
    // BOS, PAD, im_start, im_end are 0 when null/unused — only check when non-zero
    if tokens.bos_id > 0 {
        assert!(
            tokens.bos_id < vocab_size,
            "FALSIFY: {family} BOS ({}) >= vocab_size ({vocab_size})",
            tokens.bos_id
        );
    }
    if tokens.pad_id > 0 {
        assert!(
            tokens.pad_id < vocab_size,
            "FALSIFY: {family} PAD ({}) >= vocab_size ({vocab_size})",
            tokens.pad_id
        );
    }
    if tokens.im_start_id > 0 {
        assert!(
            tokens.im_start_id < vocab_size,
            "FALSIFY: {family} im_start ({}) >= vocab_size ({vocab_size})",
            tokens.im_start_id
        );
    }
    if tokens.im_end_id > 0 {
        assert!(
            tokens.im_end_id < vocab_size,
            "FALSIFY: {family} im_end ({}) >= vocab_size ({vocab_size})",
            tokens.im_end_id
        );
    }
}

#[test]
fn falsify_qwen2_special_tokens_match_yaml_registry() {
    let tokens = SpecialTokens::qwen2();
    assert_eq!(
        tokens.bos_id, 151_643,
        "FALSIFY: Qwen2 BOS mismatch vs YAML"
    );
    assert_eq!(
        tokens.eos_id, 151_645,
        "FALSIFY: Qwen2 EOS mismatch vs YAML"
    );
    assert_eq!(
        tokens.pad_id, 151_643,
        "FALSIFY: Qwen2 PAD mismatch vs YAML"
    );
    assert_eq!(
        tokens.im_start_id, 151_644,
        "FALSIFY: Qwen2 im_start mismatch vs YAML"
    );
    assert_eq!(
        tokens.im_end_id, 151_645,
        "FALSIFY: Qwen2 im_end mismatch vs YAML"
    );
    assert_tokens_within_vocab(&tokens, 151_936, "qwen2");
}

#[test]
fn falsify_qwen3_5_special_tokens_match_yaml_registry() {
    let tokens = SpecialTokens::qwen3_5();
    assert_eq!(
        tokens.eos_id, 248_044,
        "FALSIFY: Qwen3.5 EOS mismatch vs YAML"
    );
    assert_tokens_within_vocab(&tokens, 248_320, "qwen3_5");
}

#[test]
fn falsify_llama_special_tokens_match_yaml_registry() {
    let tokens = SpecialTokens::llama();
    assert_eq!(
        tokens.bos_id, 128_000,
        "FALSIFY: LLaMA BOS mismatch vs YAML"
    );
    assert_eq!(
        tokens.eos_id, 128_001,
        "FALSIFY: LLaMA EOS mismatch vs YAML"
    );
    assert_eq!(
        tokens.pad_id, 128_001,
        "FALSIFY: LLaMA PAD mismatch vs YAML"
    );
    assert_tokens_within_vocab(&tokens, 128_256, "llama");
}

#[test]
fn falsify_mistral_special_tokens_match_yaml_registry() {
    let tokens = SpecialTokens::mistral();
    assert_eq!(tokens.bos_id, 1, "FALSIFY: Mistral BOS mismatch vs YAML");
    assert_eq!(tokens.eos_id, 2, "FALSIFY: Mistral EOS mismatch vs YAML");
    assert_tokens_within_vocab(&tokens, 32_000, "mistral");
}

#[test]
fn falsify_gemma_special_tokens_match_yaml_registry() {
    let tokens = SpecialTokens::gemma();
    assert_eq!(tokens.bos_id, 2, "FALSIFY: Gemma BOS mismatch vs YAML");
    assert_eq!(tokens.eos_id, 1, "FALSIFY: Gemma EOS mismatch vs YAML");
    assert_eq!(tokens.pad_id, 0, "FALSIFY: Gemma PAD mismatch vs YAML");
    assert_tokens_within_vocab(&tokens, 256_000, "gemma");
}

#[test]
fn falsify_deepseek_special_tokens_match_yaml_registry() {
    let tokens = SpecialTokens::deepseek();
    assert_eq!(tokens.bos_id, 0, "FALSIFY: DeepSeek BOS mismatch vs YAML");
    assert_eq!(tokens.eos_id, 1, "FALSIFY: DeepSeek EOS mismatch vs YAML");
    assert_tokens_within_vocab(&tokens, 129_280, "deepseek");
}

#[test]
fn falsify_phi3_special_tokens_match_yaml_registry() {
    let tokens = SpecialTokens::phi3();
    assert_eq!(tokens.bos_id, 1, "FALSIFY: Phi-3 BOS mismatch vs YAML");
    assert_eq!(tokens.eos_id, 32_000, "FALSIFY: Phi-3 EOS mismatch vs YAML");
    assert_eq!(tokens.pad_id, 32_000, "FALSIFY: Phi-3 PAD mismatch vs YAML");
    assert_tokens_within_vocab(&tokens, 32_064, "phi3");
}

#[test]
fn falsify_phi2_special_tokens_match_yaml_registry() {
    let tokens = SpecialTokens::phi2();
    assert_eq!(tokens.eos_id, 50_256, "FALSIFY: Phi-2 EOS mismatch vs YAML");
    assert_eq!(tokens.pad_id, 50_256, "FALSIFY: Phi-2 PAD mismatch vs YAML");
    assert_tokens_within_vocab(&tokens, 51_200, "phi2");
}

#[test]
fn falsify_gpt2_special_tokens_match_yaml_registry() {
    let tokens = SpecialTokens::gpt2();
    assert_eq!(tokens.eos_id, 50_256, "FALSIFY: GPT-2 EOS mismatch vs YAML");
    assert_eq!(tokens.pad_id, 50_256, "FALSIFY: GPT-2 PAD mismatch vs YAML");
    assert_tokens_within_vocab(&tokens, 50_257, "gpt2");
}

#[test]
fn falsify_from_architecture_maps_all_yaml_families() {
    // Verify all architecture_mapping entries from the YAML resolve to a family
    let mappings = [
        ("qwen2", 151_645_u32), // EOS from qwen2
        ("qwen3", 151_645),     // shares qwen2 tokenizer
        ("qwen3moe", 151_645),  // shares qwen2 tokenizer
        ("qwen3_5", 248_044),
        ("llama", 128_001),
        ("mistral", 2),
        ("gemma", 1),
        ("gemma2", 1), // maps to gemma
        ("deepseek", 1),
        ("deepseek2", 1), // maps to deepseek
        ("phi3", 32_000),
        ("phi", 50_256), // maps to phi2
        ("phi2", 50_256),
        ("gpt2", 50_256),
    ];
    for (arch, expected_eos) in mappings {
        let tokens = SpecialTokens::from_architecture(arch)
            .unwrap_or_else(|| panic!("FALSIFY: from_architecture('{arch}') returned None"));
        assert_eq!(
            tokens.eos_id, expected_eos,
            "FALSIFY: from_architecture('{arch}') EOS mismatch"
        );
    }
}

#[test]
fn falsify_from_architecture_unknown_returns_none() {
    assert!(SpecialTokens::from_architecture("unknown").is_none());
    assert!(SpecialTokens::from_architecture("mamba").is_none());
    assert!(SpecialTokens::from_architecture("").is_none());
}