|
| 1 | +"""Long-span regression for ``attend_most_recent_matching``. |
| 2 | +
|
| 3 | +Plan C guards the DOOM renderer's long recency reads. The important |
| 4 | +behavior is the compiled attention backend, not the small oracle |
| 5 | +``Attn.compute`` path: at 32,768 positions the oracle would build a |
| 6 | +large CPU attention matrix, while the compiled path uses the same SDPA |
| 7 | +backend the real graph uses. |
| 8 | +""" |
| 9 | + |
| 10 | +import pytest |
| 11 | +import torch |
| 12 | + |
| 13 | +from torchwright.compiler.device import get_device |
| 14 | +from torchwright.compiler.forward.compile import forward_compile |
| 15 | +from torchwright.ops.attention_ops import attend_most_recent_matching |
| 16 | +from torchwright.ops.inout_nodes import create_input, create_pos_encoding |
| 17 | + |
| 18 | +MATCH_GAIN_LONG = 300_000.0 |
| 19 | +SPANS = (8_500, 32_768) |
| 20 | +D = 64 |
| 21 | +D_HEAD = 16 |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="module") |
| 25 | +def compiled_long_span_pick(): |
| 26 | + device = get_device(verbose=False) |
| 27 | + if device.type != "cuda": |
| 28 | + pytest.skip("long-span recency regression requires the CUDA backend") |
| 29 | + |
| 30 | + assert torch.get_default_dtype() == torch.float32 |
| 31 | + assert torch.get_float32_matmul_precision() == "highest" |
| 32 | + assert not torch.backends.cuda.matmul.allow_tf32 |
| 33 | + |
| 34 | + pos = create_pos_encoding() |
| 35 | + query = create_input("query", 1, value_range=(0.0, 1.0)) |
| 36 | + key = create_input("key", 1, value_range=(0.0, 1.0)) |
| 37 | + value = create_input("value", 1, value_range=(0.0, float(max(SPANS)))) |
| 38 | + out = attend_most_recent_matching( |
| 39 | + pos, |
| 40 | + query, |
| 41 | + key, |
| 42 | + value, |
| 43 | + match_gain=MATCH_GAIN_LONG, |
| 44 | + ) |
| 45 | + |
| 46 | + net = forward_compile( |
| 47 | + d=D, |
| 48 | + d_head=D_HEAD, |
| 49 | + output_node=out, |
| 50 | + pos_encoding=pos, |
| 51 | + verbose=False, |
| 52 | + max_layers=10, |
| 53 | + ) |
| 54 | + assert net.device.type == "cuda" |
| 55 | + return net, out |
| 56 | + |
| 57 | + |
| 58 | +def _run_case(compiled_long_span_pick, n_pos: int, key: torch.Tensor) -> torch.Tensor: |
| 59 | + net, out = compiled_long_span_pick |
| 60 | + inputs = { |
| 61 | + "query": torch.ones(n_pos, 1), |
| 62 | + "key": key, |
| 63 | + "value": torch.arange(float(n_pos)).unsqueeze(1), |
| 64 | + } |
| 65 | + return net.compute(n_pos, inputs)[out].detach().cpu().squeeze(1) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("n_pos", SPANS) |
| 69 | +def test_long_span_dense_adjacent_matches_pick_most_recent( |
| 70 | + compiled_long_span_pick, |
| 71 | + n_pos: int, |
| 72 | +) -> None: |
| 73 | + """Dense adjacent matches still resolve the one-position recency gap.""" |
| 74 | + result = _run_case(compiled_long_span_pick, n_pos, torch.ones(n_pos, 1)) |
| 75 | + |
| 76 | + for probe in (n_pos // 2, n_pos - 1): |
| 77 | + assert result[probe].item() == pytest.approx(float(probe), abs=0.05) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize("n_pos", SPANS) |
| 81 | +def test_long_span_sparse_match_beats_recent_non_matches( |
| 82 | + compiled_long_span_pick, |
| 83 | + n_pos: int, |
| 84 | +) -> None: |
| 85 | + """At ``300_000`` gain, one old unit match beats newer non-matches.""" |
| 86 | + match_pos = n_pos // 3 |
| 87 | + key = torch.zeros(n_pos, 1) |
| 88 | + key[match_pos, 0] = 1.0 |
| 89 | + |
| 90 | + result = _run_case(compiled_long_span_pick, n_pos, key) |
| 91 | + |
| 92 | + for probe in (match_pos, match_pos + 1, n_pos // 2, n_pos - 1): |
| 93 | + assert result[probe].item() == pytest.approx(float(match_pos), abs=0.05) |
0 commit comments