|
| 1 | +"""ADR 0008 §7 GA gate G3 — INV-3 byte-exact determinism. |
| 2 | +
|
| 3 | +Drives two independent ``GenerationCoordinator`` instances against |
| 4 | +**real** Qwen3-0.6B verifiers through identical history fed via |
| 5 | +different chunkings, and asserts the resulting greedy token |
| 6 | +streams are byte-identical. This is the integration-level |
| 7 | +counterpart of the Linux unit test |
| 8 | +``tests/inference_engine/session/test_generator.py::TestDeterminism``, |
| 9 | +which uses the deterministic ``FakeVerifier`` to verify the |
| 10 | +**dispatch** is non-stateful; this file verifies the same property |
| 11 | +holds against the actual verifier numerics on the target hardware. |
| 12 | +
|
| 13 | +Replaces the deleted ``tests/core/test_determinism_gate.py`` (PR-A3 |
| 14 | +removed it together with ``verifier.path_select``; the replacement |
| 15 | +landed here, in the integration suite, instead of in |
| 16 | +``tests/core/`` because integration is where Mac-M4-only GA gates |
| 17 | +belong per ADR 0008 §9). |
| 18 | +
|
| 19 | +Marker |
| 20 | +------ |
| 21 | +This whole file inherits ``@pytest.mark.integration`` via |
| 22 | +``conftest.py``. Bare ``pytest`` skips it; opt in with:: |
| 23 | +
|
| 24 | + pytest -m integration tests/integration/test_inv3_session_determinism_gate.py |
| 25 | +
|
| 26 | +Fixture cost |
| 27 | +------------ |
| 28 | +``fresh_verifier_factory`` (from ``tests/conftest.py``) loads |
| 29 | +Qwen3-0.6B from the HF cache. On Mac M4 with a warm cache the load |
| 30 | +is <2 s; cold takes 10-30 s plus download. Weights are cached |
| 31 | +across tests in this file via ``session_verifier_pair``. |
| 32 | +""" |
| 33 | + |
| 34 | +from __future__ import annotations |
| 35 | + |
| 36 | +from typing import List |
| 37 | + |
| 38 | +import pytest |
| 39 | +import torch |
| 40 | + |
| 41 | +from inference_engine.session import ( |
| 42 | + AppendTokensCoordinator, |
| 43 | + GenerationCoordinator, |
| 44 | + SessionStore, |
| 45 | + TokenEvent, |
| 46 | +) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture(scope="module") |
| 50 | +def session_verifier_pair(): |
| 51 | + """Two independent verifiers + stores + coordinator pairs. |
| 52 | +
|
| 53 | + Module-scoped: loading Qwen3-0.6B twice costs ~2-4 s on Mac M4 |
| 54 | + with a warm HF cache. Tests share the pair; each test resets |
| 55 | + each verifier's state via ``reset()`` before driving its own |
| 56 | + workload, so cross-test bleed-over is impossible by construction. |
| 57 | +
|
| 58 | + Inline-build the verifier (rather than going through |
| 59 | + ``fresh_verifier_factory`` which is function-scoped in |
| 60 | + ``tests/conftest.py``) so the module scope is consistent — |
| 61 | + pytest forbids a module-scoped fixture depending on a function- |
| 62 | + scoped one. |
| 63 | + """ |
| 64 | + import torch |
| 65 | + from kv_cache_proposer.verifier import SinkWindowVerifier, VerifierConfig |
| 66 | + |
| 67 | + def _build(sink: int, window: int) -> SinkWindowVerifier: |
| 68 | + return SinkWindowVerifier( |
| 69 | + VerifierConfig( |
| 70 | + dtype=torch.bfloat16, |
| 71 | + device="cpu", |
| 72 | + sink_size=sink, |
| 73 | + window_size=window, |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + fv_a = _build(sink=4, window=64) |
| 78 | + fv_b = _build(sink=4, window=64) |
| 79 | + yield fv_a, fv_b |
| 80 | + |
| 81 | + |
| 82 | +def _drive( |
| 83 | + *, |
| 84 | + verifier, |
| 85 | + chunks: List[List[int]], |
| 86 | + max_tokens: int, |
| 87 | +) -> List[int]: |
| 88 | + """Set up a fresh SessionStore + coordinators on the given |
| 89 | + verifier, append the chunks in order, then greedy-generate and |
| 90 | + return the emitted token ids. |
| 91 | + """ |
| 92 | + verifier.reset() |
| 93 | + store = SessionStore(capacity=1, cache_inspector=verifier) |
| 94 | + append_coord = AppendTokensCoordinator(store, verifier) |
| 95 | + gen_coord = GenerationCoordinator(store, verifier) |
| 96 | + |
| 97 | + sess = store.create_session() |
| 98 | + for chunk in chunks: |
| 99 | + append_coord.append_tokens(sess.session_id, chunk) |
| 100 | + |
| 101 | + tokens: List[int] = [] |
| 102 | + for ev in gen_coord.generate(sess.session_id, max_tokens=max_tokens): |
| 103 | + if isinstance(ev, TokenEvent): |
| 104 | + tokens.append(ev.token_id) |
| 105 | + return tokens |
| 106 | + |
| 107 | + |
| 108 | +def test_one_call_vs_two_calls_yield_byte_identical_tokens( |
| 109 | + session_verifier_pair, |
| 110 | +): |
| 111 | + """The minimal INV-3 gate: same total token sequence delivered |
| 112 | + in 1 call vs. 2 calls produces bit-identical greedy output.""" |
| 113 | + fv_a, fv_b = session_verifier_pair |
| 114 | + full_history = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] |
| 115 | + |
| 116 | + tokens_one_call = _drive( |
| 117 | + verifier=fv_a, chunks=[full_history], max_tokens=12, |
| 118 | + ) |
| 119 | + tokens_two_calls = _drive( |
| 120 | + verifier=fv_b, |
| 121 | + chunks=[full_history[:5], full_history[5:]], |
| 122 | + max_tokens=12, |
| 123 | + ) |
| 124 | + |
| 125 | + assert tokens_one_call == tokens_two_calls, ( |
| 126 | + f"INV-3 violated: chunking changed greedy output\n" |
| 127 | + f" one-call = {tokens_one_call!r}\n" |
| 128 | + f" two-calls = {tokens_two_calls!r}" |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +def test_chunking_invariance_across_three_splits( |
| 133 | + session_verifier_pair, |
| 134 | +): |
| 135 | + """Stronger version: three different chunkings all produce the |
| 136 | + same final greedy stream. This catches any chunk-boundary |
| 137 | + numerical drift the 1-vs-2 case might miss (e.g., a bug that |
| 138 | + only triggers when a chunk crosses a sink+window trim |
| 139 | + boundary). |
| 140 | +
|
| 141 | + The verifier's sink+window is (4, 64) = 68 capacity. We pick a |
| 142 | + history short enough to stay under that bound on the first |
| 143 | + pass and long enough to span more than two chunkings. |
| 144 | + """ |
| 145 | + fv_a, fv_b = session_verifier_pair |
| 146 | + |
| 147 | + full = [ |
| 148 | + 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, |
| 149 | + 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, |
| 150 | + ] |
| 151 | + |
| 152 | + chunkings = [ |
| 153 | + [full], # 1×20 |
| 154 | + [full[:7], full[7:14], full[14:]], # 3×medium |
| 155 | + [full[i : i + 2] for i in range(0, 20, 2)], # 10×small |
| 156 | + ] |
| 157 | + |
| 158 | + runs = [] |
| 159 | + for chunks in chunkings: |
| 160 | + # Alternate which verifier we use to keep state fully |
| 161 | + # disjoint across chunkings (we have two; the third |
| 162 | + # chunking reuses fv_a after a reset). |
| 163 | + verifier = fv_a if len(runs) % 2 == 0 else fv_b |
| 164 | + runs.append(_drive(verifier=verifier, chunks=chunks, max_tokens=8)) |
| 165 | + |
| 166 | + assert runs[0] == runs[1] == runs[2], ( |
| 167 | + f"INV-3 violated: chunkings produced divergent token streams\n" |
| 168 | + f" 1×20 = {runs[0]!r}\n" |
| 169 | + f" 3×med = {runs[1]!r}\n" |
| 170 | + f" 10×sm = {runs[2]!r}" |
| 171 | + ) |
| 172 | + |
| 173 | + |
| 174 | +def test_repeated_runs_with_same_history_byte_identical( |
| 175 | + session_verifier_pair, |
| 176 | +): |
| 177 | + """Determinism in the trivial sense: running the SAME workload |
| 178 | + on the SAME verifier twice produces the same output. This is a |
| 179 | + sanity check against accidental RNG (greedy decoding has no |
| 180 | + legitimate source of nondeterminism).""" |
| 181 | + fv_a, _ = session_verifier_pair |
| 182 | + history = [42, 43, 44, 45, 46] |
| 183 | + |
| 184 | + first = _drive(verifier=fv_a, chunks=[history], max_tokens=6) |
| 185 | + second = _drive(verifier=fv_a, chunks=[history], max_tokens=6) |
| 186 | + |
| 187 | + assert first == second, ( |
| 188 | + f"non-determinism in repeated greedy runs:\n" |
| 189 | + f" first = {first!r}\n" |
| 190 | + f" second = {second!r}" |
| 191 | + ) |
| 192 | + # Sanity: greedy with a real verifier should produce SOMETHING. |
| 193 | + assert len(first) > 0 |
0 commit comments