aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / evidence / QUANTUM_EFFICIENCY / 2026-10-22 / validate_probe.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Independent CPU oracle checks of the throwaway probe, never inference. |
| 3 | Run: uv run --with numpy --with ml-dtypes validate_probe.py |
| 4 | Synthetic bytes only. No SSH, GPU, model loading, or timing. |
| 5 | """ |
| 6 | import base64 |
| 7 | import hashlib |
| 8 | import json |
| 9 | import pathlib |
| 10 | from unittest.mock import patch |
| 11 | import numpy as np |
| 12 | import ml_dtypes |
| 13 | import weight_probe as probe |
| 14 | |
| 15 | |
| 16 | def payload(codes): |
| 17 | name = 'model.language_model.layers.0.mlp.experts.0.gate_proj' |
| 18 | raw = codes.tobytes() |
| 19 | scale = np.array([0x3F80], dtype='<u2').tobytes() # exact BF16 1 |
| 20 | return json.dumps({ |
| 21 | name+'.weight': {'meta': {'dtype': 'F8_E4M3', 'shape': [128, 128], |
| 22 | 'data_offsets': [0, len(raw)]}, |
| 23 | 'raw': base64.b64encode(raw).decode(), |
| 24 | 'sha256': hashlib.sha256(raw).hexdigest()}, |
| 25 | name+'.weight_scale_inv': {'meta': {'dtype': 'BF16', 'shape': [1, 1], |
| 26 | 'data_offsets': [len(raw), len(raw)+2]}, |
| 27 | 'raw': base64.b64encode(scale).decode(), |
| 28 | 'sha256': hashlib.sha256(scale).hexdigest()} |
| 29 | }).encode() |
| 30 | |
| 31 | |
| 32 | finite = np.array([i for i in range(256) if i not in (127, 255)], dtype=np.uint8) |
| 33 | codes = np.resize(finite, (128, 128)) |
| 34 | reference = codes.view(ml_dtypes.float8_e4m3fn).astype(np.float64) |
| 35 | with patch.object(probe.subprocess, 'check_output', return_value=payload(codes)): |
| 36 | actual, _ = probe.fetch(0, 0, 'gate_proj') |
| 37 | assert np.array_equal(actual, reference) |
| 38 | assert np.array_equal(np.signbit(actual), np.signbit(reference)) |
| 39 | refusals = [] |
| 40 | for bad in (127, 255): |
| 41 | broken = codes.copy() |
| 42 | broken[0, 0] = bad |
| 43 | with patch.object(probe.subprocess, 'check_output', return_value=payload(broken)): |
| 44 | try: |
| 45 | probe.fetch(0, 0, 'gate_proj') |
| 46 | except AssertionError as exc: |
| 47 | assert str(exc) == 'NaN FP8 code' |
| 48 | refusals.append(bad) |
| 49 | else: |
| 50 | raise AssertionError('NaN decoder mutant escaped') |
| 51 | result = {'verdict': 'PASS', 'finite_fp8_codes_compared': len(finite), |
| 52 | 'signed_zero_checked': True, 'nan_codes_refused': refusals, |
| 53 | 'transform_controls': probe.selfcheck(), 'numpy': np.__version__, |
| 54 | 'ml_dtypes': ml_dtypes.__version__, |
| 55 | 'probe_sha256': hashlib.sha256(pathlib.Path(probe.__file__).read_bytes()).hexdigest(), |
| 56 | 'scope': 'Independent library FP8 oracle plus synthetic controls, not inference quality or speed'} |
| 57 | pathlib.Path(__file__).with_name('probe-validation.json').write_text(json.dumps(result, indent=2)+'\n') |
| 58 | print(json.dumps(result, indent=2)) |