apr-cli 0.4.12

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

    // ========================================================================
    // NEW: validate_fingerprints comprehensive tests
    // ========================================================================

    fn make_fingerprint(
        name: &str,
        mean: f32,
        std: f32,
        nan_count: u32,
        inf_count: u32,
    ) -> TensorFingerprint {
        TensorFingerprint {
            name: name.to_string(),
            shape: vec![10, 20],
            dtype: "F32".to_string(),
            mean,
            std,
            min: -1.0,
            max: 1.0,
            p5: -0.9,
            p25: -0.25,
            p50: 0.0,
            p75: 0.25,
            p95: 0.9,
            nan_count,
            inf_count,
            zero_fraction: 0.0,
            checksum: 0,
        }
    }

    #[test]
    fn test_validate_fingerprints_identical() {
        let actual = vec![make_fingerprint("tensor_a", 0.5, 1.0, 0, 0)];
        let reference = vec![make_fingerprint("tensor_a", 0.5, 1.0, 0, 0)];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        assert!(anomalies.is_empty());
    }

    #[test]
    fn test_validate_fingerprints_mean_deviation_above_threshold() {
        let actual = vec![make_fingerprint("tensor_a", 5.0, 1.0, 0, 0)];
        let reference = vec![make_fingerprint("tensor_a", 0.5, 1.0, 0, 0)];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        assert!(!anomalies.is_empty());
        assert_eq!(anomalies[0].field, "mean");
    }

    #[test]
    fn test_validate_fingerprints_mean_deviation_below_threshold() {
        let actual = vec![make_fingerprint("tensor_a", 0.6, 1.0, 0, 0)];
        let reference = vec![make_fingerprint("tensor_a", 0.5, 1.0, 0, 0)];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        // 0.1 sigma deviation < 3.0 threshold
        assert!(anomalies.is_empty());
    }

    #[test]
    fn test_validate_fingerprints_nan_anomaly() {
        let actual = vec![make_fingerprint("tensor_a", 0.5, 1.0, 5, 0)];
        let reference = vec![make_fingerprint("tensor_a", 0.5, 1.0, 0, 0)];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        // NaN in actual but not in reference = anomaly
        let nan_anomaly = anomalies.iter().find(|a| a.field == "nan_count");
        assert!(nan_anomaly.is_some());
        assert_eq!(
            nan_anomaly.expect("nan anomaly").deviation_sigma,
            f32::INFINITY
        );
    }

    #[test]
    fn test_validate_fingerprints_inf_anomaly() {
        let actual = vec![make_fingerprint("tensor_a", 0.5, 1.0, 0, 3)];
        let reference = vec![make_fingerprint("tensor_a", 0.5, 1.0, 0, 0)];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        let inf_anomaly = anomalies.iter().find(|a| a.field == "inf_count");
        assert!(inf_anomaly.is_some());
    }

    #[test]
    fn test_validate_fingerprints_nan_not_anomaly_when_reference_has_nan() {
        // Both have NaN => not anomalous
        let actual = vec![make_fingerprint("tensor_a", 0.5, 1.0, 5, 0)];
        let reference = vec![make_fingerprint("tensor_a", 0.5, 1.0, 5, 0)];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        let nan_anomaly = anomalies.iter().find(|a| a.field == "nan_count");
        assert!(nan_anomaly.is_none());
    }

    #[test]
    fn test_validate_fingerprints_missing_reference_tensor() {
        let actual = vec![make_fingerprint("tensor_only_in_actual", 0.5, 1.0, 0, 0)];
        let reference = vec![make_fingerprint("tensor_only_in_ref", 0.5, 1.0, 0, 0)];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        // No matching tensor name => no anomalies (tensor is just skipped)
        assert!(anomalies.is_empty());
    }

    #[test]
    fn test_validate_fingerprints_strict_mode_layernorm() {
        // LayerNorm has tighter threshold (2.0) in strict mode
        let actual = vec![make_fingerprint(
            "model.layers.0.input_layernorm.weight",
            3.5,
            1.0,
            0,
            0,
        )];
        let reference = vec![make_fingerprint(
            "model.layers.0.input_layernorm.weight",
            1.0,
            1.0,
            0,
            0,
        )];
        let anomalies = validate_fingerprints(&actual, &reference, 5.0, true);
        // Deviation = 2.5 sigma, strict threshold for layernorm = 2.0
        assert!(!anomalies.is_empty());
    }

    #[test]
    fn test_validate_fingerprints_strict_mode_embedding() {
        // Embeddings have looser threshold (5.0) in strict mode
        let actual = vec![make_fingerprint(
            "model.embed_tokens.weight",
            3.5,
            1.0,
            0,
            0,
        )];
        let reference = vec![make_fingerprint(
            "model.embed_tokens.weight",
            0.5,
            1.0,
            0,
            0,
        )];
        let anomalies = validate_fingerprints(&actual, &reference, 2.0, true);
        // Deviation = 3.0 sigma, strict threshold for embed = 5.0 => no anomaly
        assert!(anomalies.is_empty());
    }

    #[test]
    fn test_validate_fingerprints_zero_std_reference() {
        // When reference std is near zero, deviation is scaled up
        let actual = vec![make_fingerprint("tensor_a", 0.001, 0.0, 0, 0)];
        let reference = vec![make_fingerprint("tensor_a", 0.0, 0.0, 0, 0)];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        // deviation = 0.001 * 1000 = 1.0 < threshold 3.0 => no anomaly
        assert!(anomalies.is_empty());
    }

    #[test]
    fn test_validate_fingerprints_cross_format_names() {
        // GGUF name should match APR name via normalize_tensor_name
        let actual = vec![make_fingerprint("blk.0.attn_q.weight", 5.0, 1.0, 0, 0)];
        let reference = vec![make_fingerprint(
            "model.layers.0.self_attn.q_proj.weight",
            0.5,
            1.0,
            0,
            0,
        )];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        // Should match and detect the mean deviation
        assert!(!anomalies.is_empty());
    }

    #[test]
    fn test_validate_fingerprints_multiple_tensors() {
        let actual = vec![
            make_fingerprint("tensor_a", 0.5, 1.0, 0, 0),
            make_fingerprint("tensor_b", 10.0, 1.0, 0, 0),
            make_fingerprint("tensor_c", 0.5, 1.0, 3, 0),
        ];
        let reference = vec![
            make_fingerprint("tensor_a", 0.5, 1.0, 0, 0),
            make_fingerprint("tensor_b", 0.5, 1.0, 0, 0),
            make_fingerprint("tensor_c", 0.5, 1.0, 0, 0),
        ];
        let anomalies = validate_fingerprints(&actual, &reference, 3.0, false);
        // tensor_b has mean deviation, tensor_c has NaN anomaly
        assert!(anomalies.len() >= 2);
    }

    // ========================================================================
    // NEW: get_role_threshold specific return value tests
    // ========================================================================

    #[test]
    fn test_get_role_threshold_layernorm_value() {
        assert_eq!(
            get_role_threshold("model.layers.0.input_layernorm.weight"),
            2.0
        );
    }

    #[test]
    fn test_get_role_threshold_layer_norm_underscore_value() {
        assert_eq!(get_role_threshold("some.layer_norm.weight"), 2.0);
    }

    #[test]
    fn test_get_role_threshold_ln_prefix_value() {
        assert_eq!(get_role_threshold("ln_1.weight"), 2.0);
    }

    #[test]
    fn test_get_role_threshold_embed_value() {
        assert_eq!(get_role_threshold("model.embed_tokens.weight"), 5.0);
    }

    #[test]
    fn test_get_role_threshold_lm_head_value() {
        assert_eq!(get_role_threshold("lm_head.weight"), 3.0);
    }

    #[test]
    fn test_get_role_threshold_output_value() {
        assert_eq!(get_role_threshold("output.weight"), 3.0);
    }

    #[test]
    fn test_get_role_threshold_default_value() {
        assert_eq!(get_role_threshold("some.random.tensor"), 3.0);
    }

    #[test]
    fn test_get_role_threshold_case_insensitive() {
        // Should detect "LAYERNORM" even though check uses lowercase
        assert_eq!(get_role_threshold("model.LAYERNORM.weight"), 2.0);
        assert_eq!(get_role_threshold("model.EMBED.weight"), 5.0);
    }

    // ========================================================================
    // NEW: fingerprints_to_json comprehensive tests
    // ========================================================================

    #[test]
    fn test_fingerprints_to_json_multiple() {
        let fingerprints = vec![
            make_fingerprint("tensor_a", 0.5, 1.0, 0, 0),
            make_fingerprint("tensor_b", 1.5, 2.0, 1, 2),
        ];
        let json = fingerprints_to_json(&fingerprints);
        assert!(json.contains("tensor_a"));
        assert!(json.contains("tensor_b"));
        // First entry should have trailing comma, last should not (between entries)
        assert!(json.contains("},\n"));
    }

    #[test]
    fn test_fingerprints_to_json_special_values() {
        let fp = TensorFingerprint {
            name: "test".to_string(),
            shape: vec![],
            dtype: "Q4_K".to_string(),
            mean: 0.0,
            std: 0.0,
            min: 0.0,
            max: 0.0,
            p5: 0.0,
            p25: 0.0,
            p50: 0.0,
            p75: 0.0,
            p95: 0.0,
            nan_count: 100,
            inf_count: 50,
            zero_fraction: 0.99,
            checksum: 0xDEADBEEF,
        };
        let json = fingerprints_to_json(&[fp]);
        assert!(json.contains("\"nan_count\": 100"));
        assert!(json.contains("\"inf_count\": 50"));
        assert!(json.contains("Q4_K"));
        assert!(json.contains(&format!("{}", 0xDEADBEEF_u32)));
    }

    #[test]
    fn test_fingerprints_to_json_roundtrip_structure() {
        let fps = vec![make_fingerprint("t1", 0.1, 0.2, 0, 0)];
        let json = fingerprints_to_json(&fps);
        assert!(json.starts_with('{'));
        assert!(json.ends_with('}'));
        assert!(json.contains("\"fingerprints\""));
    }

    // ========================================================================
    // NEW: load_fingerprints_from_json with valid content
    // ========================================================================

    #[test]
    fn test_load_fingerprints_from_json_valid_name_fields() {
        let mut file = NamedTempFile::with_suffix(".json").expect("create temp file");
        file.write_all(b"{\n  \"fingerprints\": [\n    {\"name\": \"tensor_a\", \"mean\": 0.5},\n    {\"name\": \"tensor_b\", \"mean\": 1.0}\n  ]\n}").expect("write");

        let result = load_fingerprints_from_json(file.path());
        assert!(result.is_ok());
        let fps = result.expect("parsed");
        assert_eq!(fps.len(), 2);
        assert_eq!(fps[0].name, "tensor_a");
        assert_eq!(fps[1].name, "tensor_b");
        // Deserialized mean values
        assert!((fps[0].mean - 0.5).abs() < f32::EPSILON);
        assert!((fps[1].mean - 1.0).abs() < f32::EPSILON);
        // Missing fields get serde defaults
        assert_eq!(fps[0].dtype, "unknown");
    }

    #[test]
    fn test_load_fingerprints_from_json_no_name_fields() {
        let mut file = NamedTempFile::with_suffix(".json").expect("create temp file");
        file.write_all(b"{\"data\": [1, 2, 3]}").expect("write");

        let result = load_fingerprints_from_json(file.path());
        // Not in {"fingerprints": [...]} format — returns Err
        assert!(result.is_err());
    }

    // ========================================================================
    // parse_tensor_stats_json
    // ========================================================================

    #[test]
    fn test_parse_tensor_stats_json_edge_cases() {
        // Empty JSON object has no "tensors" key
        assert!(parse_tensor_stats_json("{}").is_none());
        // Empty tensors map
        assert!(parse_tensor_stats_json("{\"tensors\": {}}").is_none());
        // Empty string is invalid JSON
        assert!(parse_tensor_stats_json("").is_none());
    }

    // ========================================================================
    // NEW: normalize_tensor_name edge cases
    // ========================================================================

    #[test]
    fn test_normalize_tensor_name_output_weight() {
        assert_eq!(normalize_tensor_name("output.weight"), "lm_head.weight");
    }

    #[test]
    fn test_normalize_tensor_name_output_not_weight() {
        // "output.bias" should NOT map to lm_head
        let result = normalize_tensor_name("output.bias");
        assert_ne!(result, "lm_head.weight");
        assert_eq!(result, "output.bias"); // stays as-is (falls through)
    }

    #[test]
    fn test_normalize_tensor_name_deeply_nested() {
        // Only first occurrence of prefixes is stripped
        let result = normalize_tensor_name("model.layers.10.self_attn.q_proj.weight");
        assert_eq!(result, "10.q_proj.weight");
    }

    #[test]
    fn test_normalize_tensor_name_no_match() {
        // Name with no recognized patterns should pass through mostly unchanged
        let result = normalize_tensor_name("custom_tensor_name");
        assert_eq!(result, "custom_tensor_name");
    }

    #[test]
    fn test_normalize_tensor_name_gguf_all_mappings() {
        assert_eq!(
            normalize_tensor_name("token_embd.weight"),
            "embed_tokens.weight"
        );
        assert_eq!(normalize_tensor_name("output_norm.weight"), "norm.weight");
    }

    // ========================================================================
    // NEW: is_transposed_dims edge cases
    // ========================================================================

    #[test]
    fn test_is_transposed_dims_square_matrix() {
        // [512, 512] vs [512, 512] - same shape, NOT transposed
        assert!(!is_transposed_dims(&[512, 512], &[512, 512]));
    }

    #[test]
    fn test_is_transposed_dims_empty_shapes() {
        assert!(!is_transposed_dims(&[], &[]));
    }

    #[test]
    fn test_is_transposed_dims_3d_shapes() {
        assert!(!is_transposed_dims(&[2, 3, 4], &[4, 3, 2]));
    }

    #[test]
    fn test_is_transposed_dims_one_empty_one_not() {
        assert!(!is_transposed_dims(&[768, 3072], &[]));
    }

    #[test]
    fn test_is_transposed_dims_different_sizes() {
        // Shapes that are NOT transposed versions of each other
        assert!(!is_transposed_dims(&[768, 3072], &[768, 1024]));
    }

    // ========================================================================
    // NEW: strip_ansi edge cases
    // ========================================================================

    #[test]
    fn test_strip_ansi_escape_without_bracket() {
        // ESC not followed by [ should just skip the ESC char
        let text = "\x1b Hello";
        let stripped = strip_ansi(text);
        assert_eq!(stripped, " Hello");
    }

    #[test]
    fn test_strip_ansi_nested_escape_sequences() {
        let text = "\x1b[1;31;42mColored\x1b[0m";
        let stripped = strip_ansi(text);
        assert_eq!(stripped, "Colored");
    }

    #[test]
    fn test_strip_ansi_only_escape_sequences() {
        let text = "\x1b[31m\x1b[0m";
        let stripped = strip_ansi(text);
        assert_eq!(stripped, "");
    }

    #[test]
    fn test_strip_ansi_preserves_non_ansi_content() {
        let text = "Hello [World] (test)";
        let stripped = strip_ansi(text);
        assert_eq!(stripped, "Hello [World] (test)");
    }

    // ========================================================================
    // NEW: truncate_path edge cases
    // ========================================================================

    #[test]
    fn test_truncate_path_empty_string() {
        let result = truncate_path(String::new(), 10);
        assert_eq!(result, "");
    }

    #[test]
    fn test_truncate_path_single_char() {
        let result = truncate_path("a".to_string(), 1);
        assert_eq!(result, "a");
    }