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
493
494
495
496
497
//! Tests for the `apr kernel parity` producer (aprender#2377 finding 3).
//!
//! The load-bearing test is `round_trip_*`: the producer's own output is fed to
//! `apr attn-parity-lint` and the lint must ACCEPT it — at the SHIPPED default
//! tolerances, not at tolerances chosen to make it pass. The negative half
//! corrupts the body and requires the lint to reject it, so the round trip
//! cannot pass vacuously.

use super::*;
use crate::commands::attn_parity_lint;

fn dims() -> ParityDims {
    ParityDims {
        seq_len: 32,
        num_heads: 4,
        num_kv_heads: 2,
        head_dim: 64,
        seed: 7,
    }
}

// ── ROUND TRIP ───────────────────────────────────────────────────────────

#[cfg(feature = "inference")]
#[test]
fn round_trip_producer_output_is_accepted_by_attn_parity_lint() {
    let dir = tempfile::tempdir().expect("tempdir");
    let obs = dir.path().join("parity.json");

    run(
        KernelImpl::Tiled,
        KernelRef::Naive,
        dims(),
        true,
        Some(&obs),
        false,
    )
    .expect("the tiled kernel must produce a measurement");

    // One body, both gates — parity numerics AND provenance — at the shipped
    // defaults (5e-3 / 0.9999).
    attn_parity_lint::run(
        Some(&obs),
        Some(&obs),
        None,
        attn_parity_lint::ATTN_PARITY_DEFAULT_MAX_ABS_DIFF,
        attn_parity_lint::ATTN_PARITY_DEFAULT_MIN_COSINE_SIM,
        false,
    )
    .expect("attn-parity-lint must accept the producer's own observation");
}

#[cfg(feature = "inference")]
#[test]
fn round_trip_cannot_pass_vacuously_when_the_body_is_corrupted() {
    let dir = tempfile::tempdir().expect("tempdir");
    let obs = dir.path().join("parity.json");
    run(
        KernelImpl::Tiled,
        KernelRef::Naive,
        dims(),
        true,
        Some(&obs),
        false,
    )
    .expect("producer");
    let good: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&obs).expect("read")).expect("parse");

    for (label, mutate) in [
        (
            "max_abs_diff past the FA2 bound",
            Box::new(|v: &mut serde_json::Value| v["max_abs_diff"] = serde_json::json!(0.5))
                as Box<dyn Fn(&mut serde_json::Value)>,
        ),
        (
            "cosine below the floor",
            Box::new(|v: &mut serde_json::Value| v["cosine_sim"] = serde_json::json!(0.9)),
        ),
        (
            "provenance claiming flash2 with no pinned sha",
            Box::new(|v: &mut serde_json::Value| {
                v["attn_impl"] = serde_json::json!("flash2");
                v["kernel_source"] = serde_json::Value::Null;
            }),
        ),
        (
            "fallback reason blanked out",
            Box::new(|v: &mut serde_json::Value| v["fallback"] = serde_json::json!("")),
        ),
    ] {
        let mut bad = good.clone();
        mutate(&mut bad);
        let path = dir.path().join("bad.json");
        std::fs::write(&path, serde_json::to_string(&bad).expect("ser")).expect("write");
        let err = attn_parity_lint::run(
            Some(&path),
            Some(&path),
            None,
            attn_parity_lint::ATTN_PARITY_DEFAULT_MAX_ABS_DIFF,
            attn_parity_lint::ATTN_PARITY_DEFAULT_MIN_COSINE_SIM,
            false,
        )
        .expect_err(&format!("lint must reject: {label}"));
        assert!(
            matches!(err, CliError::ValidationFailed(_)),
            "{label}: expected a validation refusal, got {err:?}"
        );
    }
}

/// The head-dim refusal is itself an observation: the error JSON it writes must
/// be accepted by `attn-parity-lint --head-dim-error-file`.
#[test]
fn round_trip_head_dim_refusal_is_accepted_by_the_head_dim_gate() {
    let dir = tempfile::tempdir().expect("tempdir");
    let err_json = dir.path().join("head-dim.json");
    let mut d = dims();
    d.head_dim = 96;

    let err = run(
        KernelImpl::Flash2,
        KernelRef::Naive,
        d,
        true,
        Some(&err_json),
        false,
    )
    .expect_err("head_dim 96 must be refused, not slow-pathed");
    assert!(matches!(err, CliError::ValidationFailed(_)), "{err:?}");
    assert!(err.exit_code_value() != 0, "a refusal must not exit 0");

    attn_parity_lint::run(
        None,
        None,
        Some(&err_json),
        attn_parity_lint::ATTN_PARITY_DEFAULT_MAX_ABS_DIFF,
        attn_parity_lint::ATTN_PARITY_DEFAULT_MIN_COSINE_SIM,
        false,
    )
    .expect("the head-dim gate must accept the producer's own error body");
}

/// The supported set must render as a SET, not `[64, 128]` — which reads as a
/// closed interval and would make head_dim 96 look supported by the very
/// message refusing it.
#[test]
fn the_head_dim_refusal_names_a_set_not_an_interval() {
    let mut d = dims();
    d.head_dim = 96;
    let err = run(KernelImpl::Flash2, KernelRef::Naive, d, false, None, false)
        .expect_err("96 must be refused");
    let msg = err.to_string();
    assert!(msg.contains("{64, 128}"), "got: {msg}");
    assert!(
        !msg.contains("[64, 128]"),
        "interval notation would imply 96 is in range: {msg}"
    );
}

#[test]
fn head_dim_gate_rejects_an_error_body_that_is_not_about_head_dim() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("other.json");
    std::fs::write(&path, r#"{"error":"out of memory"}"#).expect("write");
    let err = attn_parity_lint::run(
        None,
        None,
        Some(&path),
        attn_parity_lint::ATTN_PARITY_DEFAULT_MAX_ABS_DIFF,
        attn_parity_lint::ATTN_PARITY_DEFAULT_MIN_COSINE_SIM,
        false,
    )
    .expect_err("an unrelated error must not discharge the head-dim gate");
    assert!(matches!(err, CliError::ValidationFailed(_)), "{err:?}");
}

// ── honest refusals ──────────────────────────────────────────────────────

#[test]
fn flash2_is_refused_rather_than_answered_by_the_tiled_kernel() {
    let dir = tempfile::tempdir().expect("tempdir");
    let out = dir.path().join("flash2.json");
    let err = run(
        KernelImpl::Flash2,
        KernelRef::Naive,
        dims(),
        true,
        Some(&out),
        false,
    )
    .expect_err("a kernel this binary does not embed must not report a measurement");
    assert!(
        matches!(err, CliError::NotImplemented(_)),
        "expected NotImplemented, got {err:?}"
    );

    let body: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(&out).expect("read")).expect("parse");
    assert!(
        body.get("max_abs_diff").is_none(),
        "a refusal must not carry a parity number: {body}"
    );
    assert!(
        body["error"]
            .as_str()
            .is_some_and(|s| s.contains("flash2-kernel-unavailable")),
        "the refusal must name what is missing: {body}"
    );
}

#[test]
fn flash2_at_a_supported_head_dim_still_refuses_without_the_kernel() {
    for head_dim in FLASH2_SUPPORTED_HEAD_DIMS {
        let mut d = dims();
        d.head_dim = head_dim;
        let err = run(KernelImpl::Flash2, KernelRef::Naive, d, false, None, false)
            .expect_err("head_dim being supported does not conjure the kernel");
        assert!(matches!(err, CliError::NotImplemented(_)), "{err:?}");
    }
}

#[test]
fn zero_head_dim_is_refused_with_a_head_dim_message() {
    let mut d = dims();
    d.head_dim = 0;
    let err = run(KernelImpl::Tiled, KernelRef::Naive, d, false, None, false)
        .expect_err("head_dim 0 is not a kernel configuration");
    assert!(err.to_string().contains("head-dim"), "got: {err}");
}

#[test]
fn gqa_group_mismatch_is_refused() {
    let mut d = dims();
    d.num_heads = 5;
    d.num_kv_heads = 2;
    let err = run(KernelImpl::Tiled, KernelRef::Naive, d, false, None, false)
        .expect_err("5 query heads cannot be split into 2 whole KV groups");
    assert!(err.to_string().contains("whole groups"), "got: {err}");
}

#[test]
fn refuses_to_clobber_an_existing_output_without_force() {
    let dir = tempfile::tempdir().expect("tempdir");
    let out = dir.path().join("existing.json");
    std::fs::write(&out, "precious").expect("write");
    let err = run(
        KernelImpl::Tiled,
        KernelRef::Naive,
        dims(),
        true,
        Some(&out),
        false,
    )
    .expect_err("an existing output must not be overwritten silently");
    assert!(err.to_string().contains("--force"), "got: {err}");
}

// ── the measurement itself ───────────────────────────────────────────────

#[cfg(feature = "inference")]
#[test]
fn tiled_and_naive_agree_far_inside_the_fa2_bound() {
    use realizar::brick::FlashAttentionBrick;
    let d = dims();
    let (q, k, v) = draw_qkv(&d);
    let tiled = FlashAttentionBrick::new(d.num_heads, d.num_kv_heads, d.head_dim)
        .forward(&q, &k, &v, d.seq_len)
        .expect("tiled forward");
    let naive = naive_attention(&q, &k, &v, &d);
    let mad = max_abs_diff(&tiled, &naive);
    assert!(
        mad < 1e-5,
        "two f32 implementations of the same attention must agree to ~1e-6; got {mad:e}"
    );
}

// ── the EMITTED numbers are the MEASURED ones ────────────────────────────
//
// Everything above this point can pass while the producer fabricates its
// verdict. Replacing the two measured fields in `measure_tiled` with the
// literals `0.0` / `1.0` left ALL 16 tests green — including both round trips
// and `the_parity_metrics_are_not_vacuous`, which never reaches `run()` at all:
// it re-runs the brick itself on synthetic vectors and so says nothing about
// what the shipped body WROTE. Nothing connected the emitted JSON to a
// measurement. These two tests are that connection.

/// Recompute the parity metrics for `dims` without going through `run()`.
#[cfg(feature = "inference")]
fn measure_independently(dims: &ParityDims) -> (f64, f64) {
    use realizar::brick::FlashAttentionBrick;
    let (q, k, v) = draw_qkv(dims);
    let tiled = FlashAttentionBrick::new(dims.num_heads, dims.num_kv_heads, dims.head_dim)
        .forward(&q, &k, &v, dims.seq_len)
        .expect("tiled forward");
    let naive = naive_attention(&q, &k, &v, dims);
    (
        max_abs_diff(&tiled, &naive),
        cosine_sim(&tiled, &naive).expect("non-zero norms"),
    )
}

/// Put a value through the same JSON round trip the observation file makes.
///
/// `serde_json` is 1 ULP lossy on f64: `0x3e68_0000_0000_0000` comes back as
/// `0x3e68_0000_0000_0001`. Comparing a parsed observation against an in-memory
/// f64 with `==` would therefore be testing serde's float fidelity rather than
/// the producer. Sending the measured value through the same pipe cancels that
/// out and lets the comparison stay EXACT — which matters, because the gap
/// between a real cosine (0.999999999999992…) and a fabricated `1.0` is only
/// ~32 ulps, and a hand-picked epsilon could easily swallow it.
fn through_json(v: f64) -> f64 {
    let s = serde_json::to_string_pretty(&serde_json::json!({ "v": v })).expect("ser");
    serde_json::from_str::<serde_json::Value>(&s).expect("parse")["v"]
        .as_f64()
        .expect("f64")
}

/// Read the two metrics out of a `run()`-produced observation file.
#[cfg(feature = "inference")]
fn emitted_metrics(dims: ParityDims, path: &std::path::Path) -> (f64, f64) {
    run(
        KernelImpl::Tiled,
        KernelRef::Naive,
        dims,
        true,
        Some(path),
        true,
    )
    .expect("the tiled kernel must produce a measurement");
    let body: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(path).expect("read")).expect("parse");
    (
        body["max_abs_diff"].as_f64().expect("max_abs_diff"),
        body["cosine_sim"].as_f64().expect("cosine_sim"),
    )
}

/// The numbers in the observation must be the numbers the kernels produced —
/// bit for bit, not merely "inside the tolerance the lint happens to allow".
///
/// A producer that reports a constant passes every tolerance gate ever written,
/// which is exactly why this asserts EQUALITY TO AN INDEPENDENT MEASUREMENT
/// rather than membership of a passing range.
#[cfg(feature = "inference")]
#[test]
fn the_emitted_parity_metrics_are_the_ones_that_were_measured() {
    let dir = tempfile::tempdir().expect("tempdir");
    let obs = dir.path().join("parity.json");

    for (label, d) in [
        ("the shipped default shape", dims()),
        (
            "a longer KV cache",
            ParityDims {
                seq_len: 128,
                ..dims()
            },
        ),
        (
            "head_dim 128, no GQA",
            ParityDims {
                num_heads: 2,
                num_kv_heads: 2,
                head_dim: 128,
                ..dims()
            },
        ),
    ] {
        let (emitted_mad, emitted_cos) = emitted_metrics(d, &obs);
        let (measured_mad, measured_cos) = measure_independently(&d);
        assert_eq!(
            emitted_mad,
            through_json(measured_mad),
            "{label}: the emitted max_abs_diff is not the measured one \
             (a fabricated constant would land here)"
        );
        assert_eq!(
            emitted_cos,
            through_json(measured_cos),
            "{label}: the emitted cosine_sim is not the measured one"
        );
        // The measurement must also be a real one, not a degenerate value a
        // constant could coincide with.
        assert!(
            measured_mad > 0.0 && measured_cos < 1.0,
            "{label}: two independent f32 kernels agreed EXACTLY \
             (max_abs_diff={measured_mad}, cosine_sim={measured_cos}); this test can no \
             longer tell a measurement from a hardcoded 0.0/1.0"
        );
    }
}

/// Perturbing the inputs must MOVE the emitted numbers.
///
/// The seed pins Q/K/V, so changing it changes what the two kernels are asked to
/// agree about, and the f32 rounding they disagree by changes with it. A body
/// that reports a constant cannot move, so this is red for any hardcoded pair —
/// including one that happened to be numerically plausible.
#[cfg(feature = "inference")]
#[test]
fn perturbing_the_inputs_moves_the_emitted_parity_metrics() {
    use std::collections::BTreeSet;

    let dir = tempfile::tempdir().expect("tempdir");
    let obs = dir.path().join("parity.json");
    let mut mads: BTreeSet<u64> = BTreeSet::new();
    let mut coss: BTreeSet<u64> = BTreeSet::new();

    for seed in [7u64, 8, 9, 1234, 20_260_813] {
        let (mad, cos) = emitted_metrics(ParityDims { seed, ..dims() }, &obs);
        assert!(
            mad.is_finite() && cos.is_finite(),
            "seed {seed}: emitted a non-finite metric ({mad}, {cos})"
        );
        mads.insert(mad.to_bits());
        coss.insert(cos.to_bits());
    }

    assert!(
        mads.len() > 1,
        "max_abs_diff was identical across 5 different seeded inputs, so it is not \
         derived from them: {mads:?}"
    );
    assert!(
        coss.len() > 1,
        "cosine_sim was identical across 5 different seeded inputs, so it is not \
         derived from them: {coss:?}"
    );
}

/// The comparison must be able to FAIL — otherwise it proves nothing about the
/// kernel. Perturbing one output by 0.5 has to blow past the 5e-3 bound.
///
/// NOTE the limit of this test, which the audit found being over-claimed: it
/// operates on synthetic vectors and never calls `run()`, so it proves only that
/// the two metric FUNCTIONS can see a perturbation. That the shipped body
/// actually reports what they returned is proved by the two tests above.
#[test]
fn the_parity_metrics_are_not_vacuous() {
    let a = vec![0.25f32, -0.5, 0.75, 1.0];
    let mut b = a.clone();
    b[2] += 0.5;
    assert!(
        max_abs_diff(&a, &b) > 5e-3,
        "max_abs_diff must see the perturbation"
    );
    let cos = cosine_sim(&a, &b).expect("non-zero norms");
    assert!(cos < 0.9999, "cosine must see the perturbation, got {cos}");
    assert_eq!(max_abs_diff(&a, &a), 0.0);
}

#[test]
fn cosine_of_a_zero_vector_is_undefined_not_one() {
    assert_eq!(cosine_sim(&[0.0, 0.0], &[1.0, 1.0]), None);
}

#[test]
fn the_same_seed_draws_the_same_inputs() {
    let (q1, k1, v1) = draw_qkv(&dims());
    let (q2, k2, v2) = draw_qkv(&dims());
    assert_eq!(q1, q2);
    assert_eq!(k1, k2);
    assert_eq!(v1, v2);

    let mut other = dims();
    other.seed = 8;
    let (q3, _, _) = draw_qkv(&other);
    assert_ne!(q1, q3, "a different seed must draw different inputs");
}

#[test]
fn drawn_values_stay_inside_the_unit_interval() {
    let (q, k, v) = draw_qkv(&dims());
    for (name, xs) in [("q", &q), ("k", &k), ("v", &v)] {
        assert!(
            xs.iter().all(|x| (-1.0..1.0).contains(x)),
            "{name} escaped [-1, 1)"
        );
    }
}

/// A single-head, single-position case where the answer is known by hand:
/// attention over one key returns that value exactly.
#[test]
fn naive_attention_over_one_position_returns_that_value() {
    let d = ParityDims {
        seq_len: 1,
        num_heads: 1,
        num_kv_heads: 1,
        head_dim: 2,
        seed: 0,
    };
    let out = naive_attention(&[1.0, 0.0], &[0.5, 0.5], &[3.0, -4.0], &d);
    assert!((out[0] - 3.0).abs() < 1e-6, "got {out:?}");
    assert!((out[1] - -4.0).abs() < 1e-6, "got {out:?}");
}