apr-cli 0.4.16

CLI tool for APR model inspection, debugging, and operations
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

    #[test]
    fn test_embedding_validity_check_contains_inf() {
        let embedding = vec![0.1_f32, 0.2, f32::INFINITY, 0.4];
        let test_tokens_len = 2;
        let hidden_dim = 2;
        let embedding_ok = !embedding.is_empty()
            && embedding.len() == test_tokens_len * hidden_dim
            && !embedding.iter().any(|x| x.is_nan() || x.is_infinite());
        assert!(!embedding_ok);
    }

    // ========================================================================
    // Tokenizer Details Formatting
    // ========================================================================

    #[test]
    fn test_tokenizer_details_format_ok() {
        let test_tokens = vec![1u32, 2];
        let embedding_len = 512;
        let details = format!("tokens={:?} \u{2192} {} floats", test_tokens, embedding_len);
        assert!(details.contains("[1, 2]"));
        assert!(details.contains("512 floats"));
    }

    #[test]
    fn test_tokenizer_details_format_failed() {
        let details = "Tokenizer/embedding failed".to_string();
        assert!(details.contains("failed"));
    }

    // ========================================================================
    // Full 10-Stage Pipeline Result Table Rendering
    // ========================================================================

    #[test]
    fn test_print_full_pipeline_all_pass() {
        let stage_names = [
            "Tokenizer",
            "Embedding",
            "Positional Encoding",
            "Q/K/V Projection",
            "Attention Scores",
            "Feed-Forward (MLP)",
            "Layer Norm",
            "LM Head",
            "Logits \u{2192} Probs",
            "Sampler/Decode",
        ];
        let results: Vec<StageResult> = stage_names
            .iter()
            .map(|name| StageResult {
                name,
                eli5: "test",
                passed: true,
                details: Some("OK".to_string()),
            })
            .collect();
        assert_eq!(results.len(), 10);
        let passed = results.iter().filter(|r| r.passed).count();
        assert_eq!(passed, 10);
        // Should not panic
        print_results_table(&results);
    }

    #[test]
    fn test_print_full_pipeline_mixed_results() {
        let results = vec![
            StageResult {
                name: "Tokenizer",
                eli5: "Words \u{2192} numbers",
                passed: true,
                details: Some("tokens=[1, 2] \u{2192} 512 floats".to_string()),
            },
            StageResult {
                name: "Embedding",
                eli5: "Numbers \u{2192} vectors",
                passed: true,
                details: Some("Found embedding tensor".to_string()),
            },
            StageResult {
                name: "Positional Encoding",
                eli5: "\"You are word #3\"",
                passed: true,
                details: Some("rope_theta=10000.0".to_string()),
            },
            StageResult {
                name: "Q/K/V Projection",
                eli5: "Make 3 question copies",
                passed: false,
                details: Some("Missing Q/K/V tensors".to_string()),
            },
            StageResult {
                name: "Attention Scores",
                eli5: "\"Who to look at?\"",
                passed: false,
                details: Some("Missing attention output tensor".to_string()),
            },
            StageResult {
                name: "Feed-Forward (MLP)",
                eli5: "\"Think about it\"",
                passed: true,
                details: Some("MLP tensors found".to_string()),
            },
            StageResult {
                name: "Layer Norm",
                eli5: "Keep numbers stable",
                passed: true,
                details: Some("32 layers".to_string()),
            },
            StageResult {
                name: "LM Head",
                eli5: "Vector \u{2192} vocab scores",
                passed: true,
                details: Some("vocab_size=32000".to_string()),
            },
            StageResult {
                name: "Logits \u{2192} Probs",
                eli5: "Scores \u{2192} percentages",
                passed: true,
                details: Some("logits[32000]: min=-5.20, max=12.30".to_string()),
            },
            StageResult {
                name: "Sampler/Decode",
                eli5: "Pick word, return",
                passed: false,
                details: Some("FAIL: softmax sum = 0.500000 (expected 1.0)".to_string()),
            },
        ];
        let passed = results.iter().filter(|r| r.passed).count();
        assert_eq!(passed, 7);
        assert_eq!(results.len(), 10);
        // Should not panic - exercises truncation for long details
        print_results_table(&results);
    }

    // ========================================================================
    // print_results_table: Details Truncation In-Function Behavior
    // ========================================================================

    #[test]
    fn test_print_results_table_truncates_long_details() {
        // This exercises the truncation branch inside print_results_table
        // Details > 36 chars should be truncated to 33 + "..."
        let long_detail = "a]bcdefghijklmnopqrstuvwxyz0123456789EXTRA";
        assert!(long_detail.len() > 36);
        let results = vec![StageResult {
            name: "Long",
            eli5: "test",
            passed: true,
            details: Some(long_detail.to_string()),
        }];
        // Should not panic, and should truncate internally
        print_results_table(&results);
    }

    #[test]
    fn test_print_results_table_exact_boundary_details() {
        // Exactly 36 chars - should NOT truncate
        let exact_36 = "a".repeat(36);
        assert_eq!(exact_36.len(), 36);
        let results = vec![StageResult {
            name: "Exact",
            eli5: "test",
            passed: false,
            details: Some(exact_36),
        }];
        print_results_table(&results);
    }

    #[test]
    fn test_print_results_table_one_over_boundary() {
        // 37 chars - should truncate
        let over_37 = "b".repeat(37);
        assert_eq!(over_37.len(), 37);
        let results = vec![StageResult {
            name: "Over",
            eli5: "test",
            passed: true,
            details: Some(over_37),
        }];
        print_results_table(&results);
    }

    // ========================================================================
    // Success/Failure Message Formatting (mirrors run() lines 57-79)
    // ========================================================================

    #[test]
    fn test_success_message_format() {
        let passed_count = 10;
        let total_count = 10;
        let msg = format!(
            "\u{2705} {}/{} STAGES PASSED. MODEL PROVEN CORRECT.",
            passed_count, total_count
        );
        assert!(msg.contains("10/10"));
        assert!(msg.contains("PROVEN CORRECT"));
    }

    #[test]
    fn test_failure_message_format() {
        let passed_count = 7;
        let total_count = 10;
        let msg = format!(
            "\u{274c} {}/{} STAGES PASSED. CHECK STAGE LOGS.",
            passed_count, total_count
        );
        assert!(msg.contains("7/10"));
        assert!(msg.contains("CHECK STAGE LOGS"));
    }

    #[test]
    fn test_failure_message_zero_passed() {
        let passed_count = 0;
        let total_count = 10;
        let msg = format!(
            "\u{274c} {}/{} STAGES PASSED. CHECK STAGE LOGS.",
            passed_count, total_count
        );
        assert!(msg.contains("0/10"));
    }

    // ========================================================================
    // Vocab Size with Dims Matching (GGUF LM Head check)
    // ========================================================================

    #[test]
    fn test_vocab_dim_matching_present() {
        let dims: Vec<u64> = vec![32000, 4096];
        let vocab_size = 32000_usize;
        let matches = dims.iter().any(|&d| d as usize == vocab_size);
        assert!(matches);
    }

    #[test]
    fn test_vocab_dim_matching_absent() {
        let dims: Vec<u64> = vec![4096, 4096];
        let vocab_size = 32000_usize;
        let matches = dims.iter().any(|&d| d as usize == vocab_size);
        assert!(!matches);
    }

    #[test]
    fn test_vocab_dim_matching_empty_dims() {
        let dims: Vec<u64> = vec![];
        let vocab_size = 32000_usize;
        let matches = dims.iter().any(|&d| d as usize == vocab_size);
        assert!(!matches);
    }

    // ========================================================================
    // APR Metadata Defaults (mirrors unwrap_or defaults)
    // ========================================================================

    #[test]
    fn test_metadata_defaults_num_layers() {
        let val: Option<usize> = None;
        assert_eq!(val.unwrap_or(0), 0);
    }

    #[test]
    fn test_metadata_defaults_hidden_size() {
        let val: Option<usize> = None;
        assert_eq!(val.unwrap_or(0), 0);
    }

    #[test]
    fn test_metadata_defaults_vocab_size() {
        // C-16 (Meyer DbC): vocab_size defaults to 0 (no hidden assumption)
        let val: Option<usize> = None;
        assert_eq!(val.unwrap_or(0), 0);
    }

    #[test]
    fn test_metadata_defaults_num_heads() {
        let val: Option<usize> = None;
        assert_eq!(val.unwrap_or(0), 0);
    }

    #[test]
    fn test_metadata_present_overrides_default() {
        let val: Option<usize> = Some(128256);
        assert_eq!(val.unwrap_or(0), 128256);
    }

    // ========================================================================
    // Softmax Edge Cases (additional precision tests)
    // ========================================================================

    #[test]
    fn test_softmax_identical_logits_uniform() {
        // All identical logits should produce uniform distribution
        let logits = vec![3.14_f32; 100];
        let max_logit = logits.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let exp_sum: f32 = logits.iter().map(|x| (x - max_logit).exp()).sum();
        let probs: Vec<f32> = logits
            .iter()
            .map(|x| (x - max_logit).exp() / exp_sum)
            .collect();
        let prob_sum: f32 = probs.iter().sum();
        assert!((prob_sum - 1.0).abs() < 0.001);
        // Each prob should be ~0.01
        for p in &probs {
            assert!((p - 0.01).abs() < 0.001);
        }
    }

    #[test]
    fn test_softmax_very_negative_logits() {
        // All very negative logits - should still sum to 1
        let logits = vec![-1000.0_f32, -1001.0, -999.0];
        let max_logit = logits.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let exp_sum: f32 = logits.iter().map(|x| (x - max_logit).exp()).sum();
        let probs: Vec<f32> = logits
            .iter()
            .map(|x| (x - max_logit).exp() / exp_sum)
            .collect();
        let prob_sum: f32 = probs.iter().sum();
        assert!((prob_sum - 1.0).abs() < 0.001);
        assert!(!probs.iter().any(|x| x.is_nan()));
    }

    #[test]
    fn test_softmax_vocab_size_logits() {
        // Simulate realistic vocab size
        let logits: Vec<f32> = (0..32000).map(|i| (i as f32) * 0.001 - 16.0).collect();
        let max_logit = logits.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let exp_sum: f32 = logits.iter().map(|x| (x - max_logit).exp()).sum();
        let probs: Vec<f32> = logits
            .iter()
            .map(|x| (x - max_logit).exp() / exp_sum)
            .collect();
        let prob_sum: f32 = probs.iter().sum();
        assert!(
            (prob_sum - 1.0).abs() < 0.01,
            "softmax over 32k logits should sum to ~1.0, got {}",
            prob_sum
        );
    }

    // ========================================================================
    // APR vs GGUF Tensor Name Convention Cross-Check
    // ========================================================================

    #[test]
    fn test_apr_and_gguf_embedding_names_differ() {
        // APR uses "emb"/"wte"/"token_embd"; GGUF uses "token_embd"/"embed_tokens"
        // "token_embd" is common to both
        let apr_check =
            |n: &str| n.contains("emb") || n.contains("wte") || n.contains("token_embd");
        let gguf_check = |n: &str| n.contains("token_embd") || n.contains("embed_tokens");

        // "token_embd.weight" matches both
        assert!(apr_check("token_embd.weight"));
        assert!(gguf_check("token_embd.weight"));

        // "embed_tokens" matches GGUF but also APR (via "emb" substring)
        assert!(apr_check("model.embed_tokens.weight"));
        assert!(gguf_check("model.embed_tokens.weight"));

        // "wte" only matches APR
        assert!(apr_check("transformer.wte.weight"));
        assert!(!gguf_check("transformer.wte.weight"));
    }

    #[test]
    fn test_full_model_tensor_inventory_gguf() {
        // Simulate a complete GGUF model's tensor names
        let names = vec![
            "token_embd.weight",
            "blk.0.attn_norm.weight",
            "blk.0.attn_q.weight",
            "blk.0.attn_k.weight",
            "blk.0.attn_v.weight",
            "blk.0.attn_output.weight",
            "blk.0.ffn_norm.weight",
            "blk.0.ffn_gate.weight",
            "blk.0.ffn_up.weight",
            "blk.0.ffn_down.weight",
            "output_norm.weight",
            "output.weight",
        ];

        // All stage checks should pass
        let has_embed = names
            .iter()
            .any(|n| n.contains("token_embd") || n.contains("embed_tokens"));
        let has_q = names
            .iter()
            .any(|n| n.contains("blk.0.attn_q") || n.contains("layers.0.self_attn.q_proj"));
        let has_k = names
            .iter()
            .any(|n| n.contains("blk.0.attn_k") || n.contains("layers.0.self_attn.k_proj"));
        let has_v = names
            .iter()
            .any(|n| n.contains("blk.0.attn_v") || n.contains("layers.0.self_attn.v_proj"));
        let has_attn_out = names
            .iter()
            .any(|n| n.contains("attn_output") || n.contains("o_proj"));
        let has_gate = names
            .iter()
            .any(|n| n.contains("ffn_gate") || n.contains("gate_proj"));
        let has_up = names
            .iter()
            .any(|n| n.contains("ffn_up") || n.contains("up_proj"));
        let has_down = names
            .iter()
            .any(|n| n.contains("ffn_down") || n.contains("down_proj"));
        let has_attn_norm = names
            .iter()
            .any(|n| n.contains("attn_norm") || n.contains("input_layernorm"));
        let has_ffn_norm = names
            .iter()
            .any(|n| n.contains("ffn_norm") || n.contains("post_attention_layernorm"));
        let has_lm_head = names
            .iter()
            .any(|n| *n == "output.weight" || n.contains("lm_head"));

        assert!(has_embed);
        assert!(has_q && has_k && has_v);
        assert!(has_attn_out);
        assert!(has_gate && has_up && has_down);
        assert!(has_attn_norm && has_ffn_norm);
        assert!(has_lm_head);
    }