Download tests/test_causal_geometry.py from Anonymous1611/Lattice_Spike: direct link, hf CLI and curl.
- Browser
- Download file 2.6 kB
-
https://huggingface.co/Anonymous1611/Lattice_Spike/resolve/main/tests/test_causal_geometry.py
- Command line
-
hf download hf://Anonymous1611/Lattice_Spike/tests/test_causal_geometry.py
-
curl -L -o test_causal_geometry.py https://huggingface.co/Anonymous1611/Lattice_Spike/resolve/main/tests/test_causal_geometry.py
2.6 kB
| """Causal-alignment regressions for LatticeSpike's geometric channels.""" | |
| import os | |
| import sys | |
| import torch | |
| _HERE = os.path.dirname(os.path.abspath(__file__)) | |
| _ROOT = os.path.dirname(_HERE) | |
| _PARENT = os.path.dirname(_ROOT) | |
| _PKG = os.path.basename(_ROOT) | |
| if _PARENT not in sys.path: | |
| sys.path.insert(0, _PARENT) | |
| _boxes = __import__(f"{_PKG}.boxes", fromlist=["boxes"]) | |
| BoxBatch = _boxes.BoxBatch | |
| geometric_signals = _boxes.geometric_signals | |
| LatticeSpike = __import__( | |
| f"{_PKG}.detector", fromlist=["LatticeSpike"] | |
| ).LatticeSpike | |
| def test_geometric_residual_is_assigned_to_current_child_step(): | |
| box = BoxBatch( | |
| c_min=torch.tensor([[[0.0], [0.2], [0.1]]]), | |
| c_max=torch.tensor([[[1.0], [0.8], [0.9]]]), | |
| ) | |
| leakage, kappa = geometric_signals(box) | |
| # Token zero has no predecessor. B1 is nested in B0, while B2 leaks by | |
| # 0.1 through both faces of B1; that violation belongs to token two. | |
| assert torch.allclose( | |
| leakage, | |
| torch.tensor([[[0.0, 0.0], [0.0, 0.0], [0.1, 0.1]]]), | |
| ) | |
| assert kappa[0, 0] == 0.0 | |
| assert kappa[0, 1] > 0.0 | |
| assert kappa[0, 2] < 0.0 | |
| def _signals(*, batch=1, steps=6, d_model=8, layers=4, top_k=3, vocab=19): | |
| return { | |
| "h_box": torch.randn(batch, steps, d_model), | |
| "delta": torch.rand(batch, steps, layers), | |
| "attn_ent": torch.rand(batch, steps), | |
| "erank": torch.rand(batch, steps), | |
| "topk_probs": torch.softmax(torch.randn(batch, steps, top_k), dim=-1), | |
| "topk_ids": torch.randint(0, vocab, (batch, steps, top_k)), | |
| } | |
| def test_detector_score_is_invariant_to_post_emission_suffix(): | |
| torch.manual_seed(7) | |
| vocab = 19 | |
| model = LatticeSpike( | |
| d_model=8, | |
| vocab_size=vocab, | |
| d_box=4, | |
| num_layers=4, | |
| mrl_k=3, | |
| n_neurons=8, | |
| window=2, | |
| anomaly_window=1, | |
| ).eval() | |
| signals = _signals(vocab=vocab) | |
| boundary = 3 | |
| mutated = {name: value.clone() for name, value in signals.items()} | |
| for name, value in mutated.items(): | |
| suffix = value[:, boundary + 1 :] | |
| if value.is_floating_point(): | |
| suffix.copy_(torch.randn_like(suffix) * 25.0) | |
| else: | |
| suffix.copy_((suffix + 7).remainder(vocab)) | |
| with torch.no_grad(): | |
| original = model(signals).score | |
| changed = model(mutated).score | |
| # A different target token can first change cached hidden state B[t+1]. | |
| # Therefore scores through token t must be identical under any later suffix. | |
| assert torch.equal(original[:, : boundary + 1], changed[:, : boundary + 1]) | |