Skip to content

Commit c21b85d

Browse files
committed
test(v7-i04): H17 single-doc passthrough = mathematically identity
Closes plan item #45 / bd cppmega-mlx-5qmb. Pre-V7-I04 the H17 spec promised the doc_ids mask was an identity no-op when all tokens belonged to one document — but the only proof was a string flag in extras.side_channels_forward_effect.single_doc_passthrough. A latent mask-application bug could silently bias the loss. Three tests in tests/v4/test_h17_identity_mask_real_forward.py: 1. Uniform doc_ids=[0]*8 → flag flips on, mask_density==0.0. 2. REAL forward proof: loss trajectory with single-doc side-channel bit-equal (atol 1e-4) to loss trajectory without any side-channel at all. Any drift means the mask leaked into the math. 3. Inverse sanity: non-uniform doc_ids → flag flips off AND loss trajectory differs from baseline by >1e-5 — proves the mask path really engages when conditions are met (not always a no-op).
1 parent 77a1404 commit c21b85d

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""V7-I04: H17 single-doc passthrough must be mathematically identity.
2+
3+
The passthrough flag is set when sc_doc_ids_mask_density==0 (i.e. all
4+
tokens belong to one document). The spec promised the mask is then a
5+
no-op — but pre-V7-I04 the only proof was a string flag in extras.
6+
7+
This test runs the same 2-step train twice:
8+
(a) WITH a single-doc side-channel input (all token doc_ids = 0)
9+
(b) WITHOUT the side-channel at all
10+
and asserts the loss trajectories are bit-identical. A real
11+
identity mask cannot produce a different loss; any drift here would
12+
expose a silent mask-application bug.
13+
"""
14+
15+
from __future__ import annotations
16+
17+
from cppmega_v4.jsonrpc.schema import VerifyParams
18+
from cppmega_v4.runner import Pipeline, run_pipeline
19+
20+
21+
def _spec() -> VerifyParams:
22+
return VerifyParams.model_validate({
23+
"graph": {
24+
"nodes": [
25+
{"id": "attn", "kind": "attention",
26+
"params": {"num_heads": 4, "head_dim": 64}},
27+
{"id": "mlp", "kind": "mlp", "params": {}},
28+
],
29+
"edges": [{"src": "attn", "dst": "mlp"}],
30+
},
31+
"dim_env": {"B": 1, "S": 8, "H": 128,
32+
"nh": 2, "nkv": 1, "head_dim": 64},
33+
"loss": {"kind": "cross_entropy", "head_outputs": ["mlp"]},
34+
"optim": {"kind": "adamw",
35+
"groups": [{"matcher": "all", "lr": 1e-3,
36+
"weight_decay": 0.01,
37+
"betas": [0.9, 0.95]}]},
38+
})
39+
40+
41+
def _run(side_channels: dict | None) -> dict:
42+
opts = {"num_steps": 2, "run_id": "h17-id-mask"}
43+
if side_channels is not None:
44+
opts["side_channels"] = side_channels
45+
rep = run_pipeline(_spec(), Pipeline.from_dict({
46+
"stages": ["parse", "verify_build_spec", "build_model", "train"],
47+
"stage_options": {"train": opts},
48+
}))
49+
tr = next(s for s in rep.stages if s.name == "train")
50+
assert tr.status == "ok", f"train failed: {tr.error}"
51+
return tr.extras
52+
53+
54+
def test_v7_i04_single_doc_passthrough_flag_is_true_when_doc_ids_uniform():
55+
"""When all 8 tokens share doc_id=0, the H17 flag flips on."""
56+
extras = _run({"doc_ids": [0] * 8})
57+
assert extras.get("side_channels_forward_effect") is not None
58+
sc = extras["side_channels_forward_effect"]
59+
assert sc.get("single_doc_passthrough") is True, sc
60+
assert sc.get("doc_ids_mask_density") == 0.0
61+
# Observed list still records the family name.
62+
assert "doc_ids" in extras["side_channels_observed"]
63+
64+
65+
def test_v7_i04_passthrough_loss_equals_no_side_channel_loss():
66+
"""REAL forward proof: with single-doc passthrough the mask must
67+
be mathematically identity → same loss as no side-channel run."""
68+
extras_passthrough = _run({"doc_ids": [0] * 8})
69+
extras_baseline = _run(None)
70+
losses_p = extras_passthrough["losses"]
71+
losses_b = extras_baseline["losses"]
72+
assert len(losses_p) == len(losses_b) == 2
73+
# Bit-identical at single-precision tolerance: any drift means the
74+
# mask quietly leaked into the math.
75+
for a, b in zip(losses_p, losses_b):
76+
assert abs(a - b) < 1e-4, (
77+
f"H17 passthrough deviates from baseline: "
78+
f"passthrough={losses_p} baseline={losses_b}")
79+
80+
81+
def test_v7_i04_multi_doc_input_does_not_passthrough_and_loss_differs():
82+
"""Sanity inverse: when doc_ids are NOT uniform, mask is applied
83+
(passthrough=False) AND the loss differs from baseline. Pins that
84+
the per-test fixture actually engages the mask path."""
85+
multi = _run({"doc_ids": [0, 0, 0, 0, 1, 1, 1, 1]})
86+
sc = multi["side_channels_forward_effect"]
87+
assert sc.get("single_doc_passthrough") is False, sc
88+
assert sc.get("doc_ids_mask_density") > 0.0
89+
90+
baseline = _run(None)
91+
losses_m = multi["losses"]
92+
losses_b = baseline["losses"]
93+
# At least one step must differ once the mask is real.
94+
deltas = [abs(a - b) for a, b in zip(losses_m, losses_b)]
95+
assert max(deltas) > 1e-5, (
96+
f"multi-doc mask claimed applied but loss unchanged: "
97+
f"multi={losses_m} baseline={losses_b}")

0 commit comments

Comments
 (0)