"""Retest a new graph against immutable long-context BF16 reference bytes.""" import json import argparse from pathlib import Path import urllib.request import numpy as np RUN = Path(r'D:\webbrain-training\compass-tiny-v3-spark-x25-20260926') SOURCE = RUN / 'onnx-v3' TARGET = RUN / 'onnx-v4' def main(): global TARGET parser = argparse.ArgumentParser() parser.add_argument('--root', type=Path, default=TARGET) args = parser.parse_args() TARGET = args.root reports = [] for length in (4096, 8192): source = SOURCE / f'context-{length}-v1' ids = json.loads((source / 'selection.json').read_text())['inputIds'] reference = [np.load(source / f'bf16-{step}.npy').astype(np.float64) for step in range(4)] fixture = {'inputIds': ids, 'teacherTokens': [int(v.argmax()) for v in reference]} request = urllib.request.Request('http://127.0.0.1:8561/infer', data=json.dumps(fixture).encode(), headers={'Content-Type': 'application/json'}) with urllib.request.urlopen(request, timeout=600) as response: actual = json.load(response) checks = [] for step, values in enumerate(actual['logits']): got, expected = np.array(values, dtype=np.float64), reference[step] checks.append({'step': step, 'relativeRms': float(np.linalg.norm(got-expected)/np.linalg.norm(expected)), 'cosine': float(np.dot(got, expected)/(np.linalg.norm(got)*np.linalg.norm(expected))), 'finite': bool(np.isfinite(got).all()), 'argmaxMatched': bool(got.argmax()==expected.argmax())}) passed = all(c['finite'] and c['relativeRms'] <= .02 and c['cosine'] >= .999 for c in checks) report = {'passed': passed, 'prefillTokens': length-3, 'finalCacheTokens': length, 'checks': checks, 'bf16Reference': str(source), 'thresholds': {'relativeRmsMax': .02, 'cosineMin': .999}, 'gpu': actual['gpuInfo']} reports.append(report) print(json.dumps(report), flush=True) target = TARGET / 'context-validation.json' with target.open('x', encoding='utf-8') as f: json.dump({'passed': all(r['passed'] for r in reports), 'contexts': reports}, f, indent=2) assert all(r['passed'] for r in reports), 'Long-context precision validation still fails' if __name__ == '__main__': main()