File size: 14,163 Bytes
e1acc15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Round 14 near-tie contract (NDM 1.1-nat ceiling) and retained Round 12 fresh-cache diagnostics."""
from __future__ import annotations

import hashlib
import io
import json
import subprocess
import urllib.request
from pathlib import Path
from typing import cast
from unittest.mock import patch

import pytest

from offload.gb10_budget import window_cells as wc
from offload.launch.windows import pp4_cells as c
from offload.launch.windows import up2_checks as ck
from offload.prefill_maxlen.evidence import Json, number, record

ROOT = Path(__file__).resolve().parents[4]
PRIMARY = Path('/opt/dsv41/campaigns/DSV41-OFFLOAD/PP4-429-admission-W-20261027T133630Z/admission-B-greedy.json')
PRIMARY_SHA = 'ace208cbb3135015577c163417f4504da9ab95ebefd8910e97d2dd7c78ef93eb'
REGISTRATION = 'pp4-r14-near-tie-1p1'


def top20(margin: float, step: float) -> dict[str, Json]:
    """Supplemental CPU fixture, NOT recovered historical logprobs."""
    return {'token_id:200': -.25, 'token_id:100': -.25-margin,
            **{'token_id:'+str(300+j): -.25-(j+2)*step for j in range(18)}}


class Api(wc.Http):
    def __init__(self, cases: list[tuple[int, float, float]]) -> None:
        super().__init__('http://unused.invalid')
        self.cases = cases
        self.chats = 0
        self.calls: list[tuple[str, dict[str, Json]]] = []
        self.override: dict[str, Json] | None = None
        self.length_change = False
        self.prefix_only = False
        self.shorter = False

    def metrics(self) -> str:
        return 'vllm:num_requests_running 0\nvllm:num_requests_waiting 0\n'

    def post(self, endpoint: str, body: dict[str, Json]) -> dict[str, Json]:
        self.calls.append((endpoint, body))
        if endpoint in ('/v1/chat/completions', '/v1/completions'):
            assert isinstance(body['cache_salt'], str) and str(body['cache_salt']).strip()
        if endpoint == '/v1/chat/completions':
            i = self.chats
            self.chats += 1
            text = f'public-experiment-{i}' if i < len(self.cases) else f'public-reference-{i}'
            return {'choices': [{'message': {'content': text}}]}
        i = self.chats-1
        index, margin, step = self.cases[i]
        if endpoint == '/tokenize':
            if 'messages' in body:
                return {'tokens': [1, 2, 3]}
            tokens = [10]*120
            tokens[index] = 100
            if str(body['prompt']).startswith('public-experiment'):
                tokens[index] = 200
                if self.length_change:
                    tokens.append(7)
                if self.prefix_only:
                    tokens[index] = 100
                if self.shorter:
                    tokens = tokens[:100]
            return {'tokens': cast(Json, tokens)}
        assert endpoint == '/v1/completions'
        assert body['prompt'] == [1, 2, 3, *([10]*index)]
        assert body['temperature'] == 0 and body['max_tokens'] == 1 and body['logprobs'] == 20
        assert body['return_tokens_as_token_ids'] is True
        return {'choices': [{'logprobs': {'top_logprobs': [
            self.override if self.override is not None else top20(margin, step)]}}]}


def run(api: Api, output: Path) -> dict[str, Json]:
    return c.greedy(api, 1e12, ['public-fixture']*6,
                    [{'i': i, 'text': f'public-reference-{i}'} for i in range(6)], output)


def test_real_admission_margins_pass_under_one_point_one_nat_ceiling(tmp_path: Path) -> None:
    raw = PRIMARY.read_bytes()
    assert hashlib.sha256(raw).hexdigest() == PRIMARY_SHA
    historical = ck.rows(record(cast(Json, json.loads(raw)))['checks'])
    assert [r['token_index'] for r in historical] == [97, 78, 63]
    assert [r['margin_nats'] for r in historical] == [0., .5, 1.]
    assert [r['gap_to_top_nats'] for r in historical] == [0., 0., 0.]
    assert all('top20_logprobs' not in r for r in historical)
    cases = [(int(number(r['token_index'])), number(r['margin_nats']), .5 if i < 2 else 1.)
             for i, r in enumerate(historical)]
    api = Api(cases)
    output = tmp_path/'real-margins.json'
    result = run(api, output)
    assert result['greedy_registration'] == REGISTRATION and result['near_tie'] == 3 and result['identical'] == 3
    rows = ck.rows(ck.read(output)['checks'])
    assert [row['near_tie'] for row in rows[:3]] == [True, True, True]
    assert all(row.get('identical') is True for row in rows[3:])
    for i, row in enumerate(rows[:3]):
        assert row['token_index'] == historical[i]['token_index']
        assert row['margin_nats'] == historical[i]['margin_nats']
        assert row['gap_to_top_nats'] == 0
        assert row['top20_logprobs'] == top20(cases[i][1], cases[i][2])
    assert PRIMARY.read_bytes() == raw  # primary evidence is read-only; no retroactive pass


def test_generation_and_probe_salts_are_distinct_and_retries_are_fresh(tmp_path: Path) -> None:
    cases = [(97, 0., .5), (78, .5, .5), (63, .9, 1.)]
    api = Api(cases)
    output = tmp_path/'pass.json'
    result = run(api, output)
    assert result['greedy_registration'] == REGISTRATION
    assert result['identical'] == result['near_tie'] == 3 and api.chats == 6
    posts = [(endpoint, body) for endpoint, body in api.calls if endpoint != '/tokenize']
    salts = [body['cache_salt'] for _, body in posts]
    assert len(salts) == len(set(cast(list[str], salts))) == 9
    for _, body in posts:
        assert body['model'] == c.MODEL and body['temperature'] == 0
    assert all(body['max_tokens'] == 300 and body['chat_template_kwargs'] == {'thinking': False}
               for endpoint, body in posts if endpoint == '/v1/chat/completions')
    persisted = output.read_text()
    assert 'public-reference' not in persisted and 'public-experiment' not in persisted and 'public-fixture' not in persisted
    retry = Api(cases)
    run(retry, output)
    assert not set(cast(list[str], salts)) & {str(body['cache_salt']) for endpoint, body in retry.calls if endpoint != '/tokenize'}


@pytest.mark.parametrize('margin', [1.11, 1.5, 2., 1.1011])
def test_margin_above_registered_allowance_stops(tmp_path: Path, margin: float) -> None:
    api = Api([(63, margin, .5)])
    output = tmp_path/'stop.json'
    with pytest.raises(ValueError, match='fails pp4-r14-near-tie-1p1'):
        run(api, output)
    assert api.chats == 1
    row = ck.rows(ck.read(output)['checks'])[0]
    assert row['near_tie'] is False and row['top20_logprobs'] == top20(margin, .5)
    returned = top20(margin, .5)
    assert row['margin_nats'] == abs(number(returned['token_id:100'])-number(returned['token_id:200']))
    assert row['diagnostic_error']


@pytest.mark.parametrize('count', [4, 6])
def test_no_extra_divergence_count_limit(tmp_path: Path, count: int) -> None:
    api = Api([(10+i, .5, .5) for i in range(count)])
    result = run(api, tmp_path/'many.json')
    rows = ck.rows(result['checks'])
    assert result['near_tie'] == count and result['identical'] == 6-count
    assert api.chats == len(rows) == 6
    rows = rows[:count]
    assert all(len(record(r['top20_logprobs'])) == 20 for r in rows)


@pytest.mark.parametrize('change', ['missing', 'short-top20', 'clamped', 'bad-id', 'alias-id', 'nan', 'inf'])
def test_bad_evidence_stops_and_retains_full_returned_values(tmp_path: Path, change: str) -> None:
    api = Api([(63, .5, .5)])
    top = top20(.5, .5)
    if change == 'missing': top['token_id:99'] = top.pop('token_id:100')
    if change == 'short-top20': top.pop('token_id:317')
    if change == 'clamped': top['token_id:317'] = -9999.
    if change == 'bad-id': top['malformed'] = top.pop('token_id:317')
    if change == 'alias-id': top['token_id:0200'] = top.pop('token_id:317')
    if change == 'nan': top['token_id:317'] = float('nan')
    if change == 'inf': top['token_id:317'] = float('-inf')
    if change in ('nan', 'inf'):
        # Nonfinite JSON cannot be persisted; exercise the validator directly.
        assert c.near_tie_values(top, 100, 200)['near_tie'] is False
        return
    api.override = top
    output = tmp_path/'stop.json'
    with pytest.raises(ValueError, match='fails pp4-r14-near-tie-1p1'):
        run(api, output)
    row = ck.rows(ck.read(output)['checks'])[0]
    assert row['near_tie'] is False and row['top20_logprobs'] == top


@pytest.mark.parametrize('kind', ['longer', 'shorter'])
def test_token_flip_with_unequal_lengths_is_probed(tmp_path: Path, kind: str) -> None:
    """Round 15 (PP4-941 admission stop 2026-10-27): a flip at 63 whose continuations end at different lengths is a token near-tie."""
    api = Api([(63, 0., .5)])
    api.length_change = kind == 'longer'
    api.shorter = kind == 'shorter'
    result = run(api, tmp_path/'unequal.json')
    assert result['near_tie'] == 1 and result['identical'] == 5
    row = ck.rows(ck.read(tmp_path/'unequal.json')['checks'])[0]
    assert row['token_index'] == 63 and row['near_tie'] is True
    assert row['reference_tokens'] == 120 and row['experiment_tokens'] == (121 if kind == 'longer' else 100)


@pytest.mark.parametrize('kind', ['prefix-equal', 'prefix-longer'])
def test_pure_prefix_differences_stop_before_probe(tmp_path: Path, kind: str) -> None:
    api = Api([(63, 0., .5)])
    api.prefix_only = True
    api.length_change = kind == 'prefix-longer'
    output = tmp_path/'prefix.json'
    with pytest.raises(ValueError, match='pure prefix difference'):
        run(api, output)
    assert not any(endpoint == '/v1/completions' for endpoint, _ in api.calls)
    assert ck.rows(ck.read(output)['checks'])[0]['near_tie'] is False


@pytest.mark.parametrize('margin', [0., .5, .75, .9, 1., 1.1, 1.1005, 1.101])
def test_registered_margin_and_rounding_allowance_pass(tmp_path: Path, margin: float) -> None:
    api = Api([(63, margin, .5)])
    result = run(api, tmp_path/'boundary.json')
    assert result['near_tie'] == 1 and result['identical'] == 5
    assert ck.rows(result['checks'])[0]['margin_nats'] == pytest.approx(margin)


@pytest.mark.parametrize('gap,passes', [(1.1, True), (1.1005, True), (1.101, True), (1.1011, False), (1.11, False)])
@pytest.mark.parametrize('reference_better', [False, True])
def test_better_of_two_gap_to_top_is_independent_of_margin(
    tmp_path: Path, gap: float, passes: bool, reference_better: bool,
) -> None:
    api = Api([(63, .5, .5)])
    top = top20(.5, .5)
    # Neither token is top; both are in top20 and half a nat apart.
    top['token_id:300'] = -.25
    top['token_id:100'] = -.25-gap-(0. if reference_better else .5)
    top['token_id:200'] = -.25-gap-(.5 if reference_better else 0.)
    api.override = top
    output = tmp_path/'gap.json'
    if passes:
        assert run(api, output)['near_tie'] == 1
    else:
        with pytest.raises(ValueError, match='fails pp4-r14-near-tie-1p1'):
            run(api, output)
    row = ck.rows(ck.read(output)['checks'])[0]
    assert row['near_tie'] is passes and row['gap_to_top_nats'] == pytest.approx(gap)
    assert row['experiment_gap_to_top_nats'] == pytest.approx(gap+(.5 if reference_better else 0.))
    assert row['top20_logprobs'] == top


@pytest.mark.parametrize('change', ['off-grid', 'all-equal', 'reference-top'])
def test_grid_and_experiment_is_top_restrictions_are_retired(tmp_path: Path, change: str) -> None:
    api = Api([(63, .5, .5)])
    top = top20(.5, .5)
    if change == 'off-grid': top['token_id:317'] = -10.1
    if change == 'all-equal': top = dict.fromkeys(top, -.25)
    if change == 'reference-top': top['token_id:200'], top['token_id:100'] = -.75, -.25
    api.override = top
    result = run(api, tmp_path/'no-grid.json')
    assert result['near_tie'] == 1 and result['identical'] == 5


@pytest.mark.parametrize('endpoint', ['/v1/chat/completions', '/v1/completions'])
@pytest.mark.parametrize('salt', [None, '', ' ', 1])
def test_actual_http_refuses_missing_salt_before_transport(endpoint: str, salt: Json) -> None:
    with patch.object(urllib.request, 'urlopen') as opened:
        with pytest.raises(ValueError, match='cache_salt'):
            c.Http('http://unused.invalid').post(endpoint, {'cache_salt': salt})
        opened.assert_not_called()


def test_actual_http_forwards_salts_on_both_endpoints() -> None:
    seen: list[tuple[str, dict[str, Json]]] = []
    def opened(request: urllib.request.Request, *, timeout: int) -> io.BytesIO:
        assert timeout == 900 and isinstance(request.data, bytes)
        seen.append((request.full_url, record(cast(Json, json.loads(request.data)))))
        return io.BytesIO(b'{"ok":true}')
    with patch.object(urllib.request, 'urlopen', opened):
        for endpoint in ('/v1/chat/completions', '/v1/completions'):
            assert c.Http('http://unused.invalid').post(endpoint, {'cache_salt': 'fresh-'+endpoint}) == {'ok': True}
    assert [body['cache_salt'] for _, body in seen] == ['fresh-/v1/chat/completions', 'fresh-/v1/completions']


def test_worker_and_shared_nbw_bytes_unchanged_from_qualified_candidate() -> None:
    # Includes new tracked files/deletions and actual worktree contents, not just HEAD.
    result = subprocess.run(['git', 'diff', '--exit-code', '42928552727ee85561600d886a704d3342ad969a', '--',
        'offload/dspark/worker'], cwd=ROOT, capture_output=True, text=True, check=False)
    assert result.returncode == 0, result.stdout+result.stderr
    # R-PP4: the shared NBW module is exactly WIN2's reviewed version (WIN2-R2 2b48eec: defaulted threshold/distribution
    # parameters, NBW's own defaults unchanged); PP4 never edits it.
    shared = subprocess.run(['git', 'diff', '--exit-code', '2b48eeccf325770e3c7dc48fdbf863b80e4387ce', '--', 'offload/launch/windows/nbw_cells.py'],
        cwd=ROOT, capture_output=True, text=True, check=False)
    assert shared.returncode == 0, shared.stdout+shared.stderr
    untracked = subprocess.run(['git', 'ls-files', '--others', '--exclude-standard', '--', 'offload/dspark/worker'],
        cwd=ROOT, capture_output=True, text=True, check=True)
    assert not untracked.stdout
    restore = Path('/opt/dsv41/campaigns/DSV41-OFFLOAD/W8/restore.sh')
    assert hashlib.sha256(restore.read_bytes()).hexdigest() == 'd1c4479dbfbd90bc2036c7bf1561db5b564c2b162a7cde4a121e46bfb9f86a49'