apr-cli 0.64.0

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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

/// Compute statistics for a tensor
#[allow(clippy::type_complexity)]
pub(crate) fn compute_tensor_stats(
    values: &[f32],
) -> (
    f32,
    f32,
    f32,
    f32,
    f32,
    f32,
    f32,
    f32,
    f32,
    u32,
    u32,
    f32,
    u32,
) {
    if values.is_empty() {
        return (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0.0, 0);
    }

    let mut nan_count = 0u32;
    let mut inf_count = 0u32;
    let mut zero_count = 0u32;
    let mut sum = 0.0f64;
    let mut min = f32::INFINITY;
    let mut max = f32::NEG_INFINITY;
    let mut checksum = 0u32;

    // Collect valid values for percentile calculation
    let mut valid_values: Vec<f32> = Vec::with_capacity(values.len());

    for &v in values {
        // Update checksum (simple CRC-like)
        checksum = checksum.wrapping_add(v.to_bits());

        if v.is_nan() {
            nan_count += 1;
        } else if v.is_infinite() {
            inf_count += 1;
        } else {
            valid_values.push(v);
            sum += v as f64;
            if v < min {
                min = v;
            }
            if v > max {
                max = v;
            }
            if v == 0.0 {
                zero_count += 1;
            }
        }
    }

    let n = valid_values.len();
    if n == 0 {
        return (
            0.0, 0.0, min, max, 0.0, 0.0, 0.0, 0.0, 0.0, nan_count, inf_count, 0.0, checksum,
        );
    }

    let mean = (sum / n as f64) as f32;

    // Compute std
    let variance: f64 = valid_values
        .iter()
        .map(|&v| {
            let diff = v as f64 - sum / n as f64;
            diff * diff
        })
        .sum::<f64>()
        / n as f64;
    let std = variance.sqrt() as f32;

    // Compute percentiles (sort for percentile calculation)
    valid_values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let percentile = |p: f32| -> f32 {
        let idx = ((p / 100.0) * (n - 1) as f32) as usize;
        valid_values[idx.min(n - 1)]
    };

    let p5 = percentile(5.0);
    let p25 = percentile(25.0);
    let p50 = percentile(50.0);
    let p75 = percentile(75.0);
    let p95 = percentile(95.0);

    let zero_fraction = zero_count as f32 / values.len() as f32;

    (
        mean,
        std,
        min,
        max,
        p5,
        p25,
        p50,
        p75,
        p95,
        nan_count,
        inf_count,
        zero_fraction,
        checksum,
    )
}

/// Print fingerprints
fn print_fingerprints(fingerprints: &[TensorFingerprint], verbose: bool, json: bool) -> Result<()> {
    if json {
        println!("{}", fingerprints_to_json(fingerprints));
        return Ok(());
    }

    for fp in fingerprints {
        println!("║ {:<74} ║", truncate_path(fp.name.clone(), 74));
        println!("║   shape={:?} dtype={:<10} ║", fp.shape, fp.dtype);
        if verbose {
            println!(
                "║   mean={:>10.6} std={:>10.6} min={:>10.6} max={:>10.6} ║",
                fp.mean, fp.std, fp.min, fp.max
            );
            println!(
                "║   p5={:>10.6} p25={:>10.6} p50={:>10.6} p75={:>10.6} p95={:>10.6} ║",
                fp.p5, fp.p25, fp.p50, fp.p75, fp.p95
            );
            println!(
                "║   nan={} inf={} zero_frac={:.4} checksum=0x{:08X}",
                fp.nan_count, fp.inf_count, fp.zero_fraction, fp.checksum
            );
        } else {
            println!(
                "║   mean={:>10.6} std={:>10.6} nan={} inf={}",
                fp.mean, fp.std, fp.nan_count, fp.inf_count
            );
        }
        println!(
            "{}",
            "╠──────────────────────────────────────────────────────────────────────────────╣"
                .cyan()
        );
    }

    println!("║ Total tensors: {:<61} ║", fingerprints.len());

    Ok(())
}

/// Print fingerprint diff between two models
/// Compute normalized mean diff and detect anomalies between two fingerprints.
fn fingerprint_anomaly(fp_a: &TensorFingerprint, fp_b: &TensorFingerprint) -> (f32, bool) {
    let mean_diff = if fp_a.std > 1e-10 {
        (fp_a.mean - fp_b.mean).abs() / fp_a.std
    } else {
        (fp_a.mean - fp_b.mean).abs()
    };
    let has_anomaly =
        mean_diff > 3.0 || fp_a.nan_count != fp_b.nan_count || fp_a.inf_count != fp_b.inf_count;
    (mean_diff, has_anomaly)
}

/// Print a single tensor comparison row (text mode).
fn print_diff_row(
    fp_a: &TensorFingerprint,
    fp_b: &TensorFingerprint,
    mean_diff: f32,
    has_anomaly: bool,
) {
    let status = if has_anomaly {
        "⚠️".yellow()
    } else {
        "".green()
    };
    println!(
        "{} {:<72} ║",
        status,
        truncate_path(fp_a.name.clone(), 72)
    );
    println!(
        "║   A: mean={:>10.6} std={:>10.6} nan={} inf={}",
        fp_a.mean, fp_a.std, fp_a.nan_count, fp_a.inf_count
    );
    println!(
        "║   B: mean={:>10.6} std={:>10.6} nan={} inf={}",
        fp_b.mean, fp_b.std, fp_b.nan_count, fp_b.inf_count
    );
    if has_anomaly {
        println!(
            "{} mean_diff={:.2}σ ║",
            "ANOMALY:".red().bold(),
            mean_diff
        );
    }
    println!(
        "{}",
        "╠──────────────────────────────────────────────────────────────────────────────╣".cyan()
    );
}

/// Anomaly `field` marker for a tensor of model A that has no counterpart in B.
const FIELD_MISSING_IN_B: &str = "missing_in_b";
/// Anomaly `field` marker for a tensor of model B that has no counterpart in A.
const FIELD_MISSING_IN_A: &str = "missing_in_a";

/// Count anomalies of a given `field` kind.
fn count_field(anomalies: &[StatisticalAnomaly], field: &str) -> usize {
    anomalies.iter().filter(|a| a.field == field).count()
}

/// Print the diff summary (JSON or text).
// serde_json::json!() macro uses infallible unwrap internally
#[allow(clippy::disallowed_methods)]
fn print_diff_summary(total: usize, anomalies: &[StatisticalAnomaly], json: bool) {
    let missing_in_b = count_field(anomalies, FIELD_MISSING_IN_B);
    let missing_in_a = count_field(anomalies, FIELD_MISSING_IN_A);

    if json {
        println!(
            "{:#}",
            serde_json::json!({
                "total_tensors": total,
                "anomalies": anomalies.len(),
                "missing_in_b": missing_in_b,
                "missing_in_a": missing_in_a,
                "passed": anomalies.is_empty(),
            })
        );
    } else if anomalies.is_empty() {
        println!(
            "{}",
            "✓ No statistical anomalies detected".green().bold()
        );
    } else {
        println!(
            "{}",
            format!(
                "{} ANOMALIES DETECTED ({} missing in B, {} missing in A)",
                anomalies.len(),
                missing_in_b,
                missing_in_a
            )
            .red()
            .bold()
        );
    }
}

/// Build an anomaly for a tensor present in one model and absent from the other.
fn missing_tensor_anomaly(name: &str, field: &'static str) -> StatisticalAnomaly {
    StatisticalAnomaly {
        tensor: name.to_string(),
        field: field.to_string(),
        expected: 0.0,
        actual: 0.0,
        deviation_sigma: f32::INFINITY,
    }
}

fn print_fingerprint_diff(
    fps_a: &[TensorFingerprint],
    fps_b: &[TensorFingerprint],
    verbose: bool,
    json: bool,
) -> Result<()> {
    // GH-202: Use normalized names for cross-format matching
    let map_b: std::collections::HashMap<_, _> = fps_b
        .iter()
        .map(|fp| (normalize_tensor_name(&fp.name), fp))
        .collect();

    let mut anomalies = Vec::new();

    if !json {
        println!(
            "{}",
            "║                              FINGERPRINT DIFF                                ║"
                .yellow()
        );
        println!(
            "{}",
            "╠──────────────────────────────────────────────────────────────────────────────╣"
                .cyan()
        );
    }

    let mut matched_in_b: std::collections::HashSet<String> = std::collections::HashSet::new();

    for fp_a in fps_a {
        let norm_name_a = normalize_tensor_name(&fp_a.name);
        let Some(fp_b) = map_b.get(&norm_name_a) else {
            // A tensor that model B does not have at all is the maximum possible
            // anomaly. It used to be printed and then dropped on the floor, so a
            // diff where every tensor was missing still reported "No statistical
            // anomalies detected" / "passed": true and exited 0.
            if !json {
                println!(
                    "{} {:<72} ║",
                    "".red(),
                    truncate_path(fp_a.name.clone(), 72)
                );
                println!("║   Missing in Model B ║");
            }
            anomalies.push(missing_tensor_anomaly(&fp_a.name, FIELD_MISSING_IN_B));
            continue;
        };
        matched_in_b.insert(norm_name_a);

        let (mean_diff, has_anomaly) = fingerprint_anomaly(fp_a, fp_b);

        if has_anomaly || verbose {
            if !json {
                print_diff_row(fp_a, fp_b, mean_diff, has_anomaly);
            }
            if has_anomaly {
                anomalies.push(StatisticalAnomaly {
                    tensor: fp_a.name.clone(),
                    field: "mean".to_string(),
                    expected: fp_a.mean,
                    actual: fp_b.mean,
                    deviation_sigma: mean_diff,
                });
            }
        }
    }

    // Tensors that exist only in model B are just as much a difference as tensors
    // that exist only in model A; the walker above can never see them.
    for fp_b in fps_b {
        let norm_name_b = normalize_tensor_name(&fp_b.name);
        if !matched_in_b.contains(&norm_name_b) {
            if !json {
                println!(
                    "{} {:<72} ║",
                    "+".red(),
                    truncate_path(fp_b.name.clone(), 72)
                );
                println!("║   Missing in Model A ║");
            }
            anomalies.push(missing_tensor_anomaly(&fp_b.name, FIELD_MISSING_IN_A));
        }
    }

    print_diff_summary(fps_a.len(), &anomalies, json);

    if anomalies.is_empty() {
        return Ok(());
    }
    Err(CliError::ValidationFailed(format!(
        "{} statistical anomalies detected between the two models \
         ({} tensors missing in B, {} missing in A)",
        anomalies.len(),
        count_field(&anomalies, FIELD_MISSING_IN_B),
        count_field(&anomalies, FIELD_MISSING_IN_A),
    )))
}

/// Convert fingerprints to JSON
fn fingerprints_to_json(fingerprints: &[TensorFingerprint]) -> String {
    let mut json = String::from("{\n  \"fingerprints\": [\n");

    for (i, fp) in fingerprints.iter().enumerate() {
        let comma = if i < fingerprints.len() - 1 { "," } else { "" };
        write!(
            json,
            "    {{\n      \"name\": \"{}\",\n      \"shape\": {:?},\n      \"dtype\": \"{}\",\n      \"mean\": {},\n      \"std\": {},\n      \"min\": {},\n      \"max\": {},\n      \"p5\": {},\n      \"p25\": {},\n      \"p50\": {},\n      \"p75\": {},\n      \"p95\": {},\n      \"nan_count\": {},\n      \"inf_count\": {},\n      \"zero_fraction\": {},\n      \"checksum\": {}\n    }}{}\n",
            fp.name, fp.shape, fp.dtype, fp.mean, fp.std, fp.min, fp.max,
            fp.p5, fp.p25, fp.p50, fp.p75, fp.p95,
            fp.nan_count, fp.inf_count, fp.zero_fraction, fp.checksum, comma
        )
        .expect("write to String should not fail");
    }

    json.push_str("  ]\n}");
    json
}

/// Load fingerprints from JSON file
fn load_fingerprints_from_json(path: &Path) -> Result<Vec<TensorFingerprint>> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| CliError::ValidationFailed(format!("Failed to read fingerprints: {e}")))?;

    #[derive(serde::Deserialize)]
    struct FingerprintFile {
        fingerprints: Vec<TensorFingerprint>,
    }

    let parsed: FingerprintFile = serde_json::from_str(&content)
        .map_err(|e| CliError::ValidationFailed(format!("Failed to parse fingerprints JSON: {e}")))?;

    Ok(parsed.fingerprints)
}

/// Compare a single tensor fingerprint against a reference and collect anomalies.
fn compare_tensor_fingerprint(
    actual_fp: &TensorFingerprint,
    ref_fp: &TensorFingerprint,
    threshold: f32,
    strict: bool,
    anomalies: &mut Vec<StatisticalAnomaly>,
) {
    let role_threshold = if strict {
        get_role_threshold(&actual_fp.name)
    } else {
        threshold
    };

    let mean_deviation = if ref_fp.std > 1e-10 {
        (actual_fp.mean - ref_fp.mean).abs() / ref_fp.std
    } else {
        (actual_fp.mean - ref_fp.mean).abs() * 1000.0
    };

    if mean_deviation > role_threshold {
        anomalies.push(StatisticalAnomaly {
            tensor: actual_fp.name.clone(),
            field: "mean".to_string(),
            expected: ref_fp.mean,
            actual: actual_fp.mean,
            deviation_sigma: mean_deviation,
        });
    }

    if actual_fp.nan_count > 0 && ref_fp.nan_count == 0 {
        anomalies.push(StatisticalAnomaly {
            tensor: actual_fp.name.clone(),
            field: "nan_count".to_string(),
            expected: ref_fp.nan_count as f32,
            actual: actual_fp.nan_count as f32,
            deviation_sigma: f32::INFINITY,
        });
    }

    if actual_fp.inf_count > 0 && ref_fp.inf_count == 0 {
        anomalies.push(StatisticalAnomaly {
            tensor: actual_fp.name.clone(),
            field: "inf_count".to_string(),
            expected: ref_fp.inf_count as f32,
            actual: actual_fp.inf_count as f32,
            deviation_sigma: f32::INFINITY,
        });
    }
}

/// Validate fingerprints against reference
fn validate_fingerprints(
    actual: &[TensorFingerprint],
    reference: &[TensorFingerprint],
    threshold: f32,
    strict: bool,
) -> Vec<StatisticalAnomaly> {
    let ref_map: std::collections::HashMap<_, _> = reference
        .iter()
        .map(|fp| (normalize_tensor_name(&fp.name), fp))
        .collect();

    let mut anomalies = Vec::new();

    for actual_fp in actual {
        let norm_name = normalize_tensor_name(&actual_fp.name);
        if let Some(ref_fp) = ref_map.get(&norm_name) {
            compare_tensor_fingerprint(actual_fp, ref_fp, threshold, strict, &mut anomalies);
        }
    }

    anomalies
}

/// Get role-specific threshold based on tensor name
fn get_role_threshold(tensor_name: &str) -> f32 {
    let name_lower = tensor_name.to_lowercase();

    if name_lower.contains("layernorm")
        || name_lower.contains("layer_norm")
        || name_lower.contains("ln_")
    {
        // LayerNorm weights should be very close to 1.0 - tight threshold
        2.0
    } else if name_lower.contains("embed") {
        // Embeddings can have more variance
        5.0
    } else if name_lower.contains("lm_head") || name_lower.contains("output") {
        // Output layers - moderate threshold
        3.0
    } else {
        // Default threshold for other weights
        3.0
    }
}