File size: 4,367 Bytes
8bc8906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Actual replay/parity/abstention/tamper checks for all three published architectures."""
import os
for k in ['OMP_NUM_THREADS','MKL_NUM_THREADS','OPENBLAS_NUM_THREADS','TOKENIZERS_PARALLELISM']:os.environ[k]='1' if k!='TOKENIZERS_PARALLELISM' else 'false'
import sys,json,math,hashlib,shutil,tempfile,time
from pathlib import Path
import numpy as np
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT))
from runtime import predict
from windtunnel_response.sources import read,write,digest
from windtunnel_response.baseline import probabilities
checks=[];receipts=[]
for entry in read(ROOT/'results/initial-stage-inventory.json'):
 folder=ROOT/entry['directory'];request=read(folder/'example-request.json');start=time.perf_counter();result=predict(folder,request);elapsed=time.perf_counter()-start
 assert result['status']=='ok',(folder,result);p=result['prediction']['probabilities'];assert math.isclose(sum(p.values()),1,abs_tol=1e-6)
 manifest=read(folder/'runtime-manifest.json');q=next(q for q in read(folder/'questions.json') if q['question_id']==request['question_id']);model=read(folder/manifest['model_file']) if manifest['architecture']!='encoder_safetensors' else None
 if manifest['architecture']=='known_item_json':
  item=model['items'][q['question_id']];expected=item['cell_probabilities'][request['cell_id']] if item['champion']=='shrinkage' else probabilities(item,request['cell_id']).tolist()
 elif manifest['architecture']=='cross_wave_json':
  item=next(i for i in model['items'] if i['question_id']==q['question_id']);features=np.asarray(model['cell_features'][request['cell_id']]);from scipy.special import softmax
  expected=softmax(np.asarray(item['coefficients'])@features+item['intercepts']).tolist()
  calibrated={**request,'mode':'2024-development-calibrated'};out=predict(folder,calibrated);ref=softmax(np.asarray(item['coefficients'])@features+item['intercepts']+item['intercept_offsets_from_target_dev']);assert out['status']=='ok' and np.max(np.abs(np.asarray(list(out['prediction']['probabilities'].values()))-ref))<1e-10;assert out['provenance']['target_development_labels_used'] is True
 else:
  from windtunnel_response.encoder import predict_checkpoint
  expected=list(predict_checkpoint(folder/'base',folder/manifest['model_file'],q,[request['cell_id']])[request['cell_id']].values())
 tolerance=1e-6 if manifest['architecture']=='encoder_safetensors' else 1e-12
 assert np.max(np.abs(np.asarray(list(p.values()))-expected))<tolerance
 for mutation,reason,status in [({'question_id':'unsupported-question'},'unsupported_question','abstained'),({'instrument_hash':'0'*64},'instrument_mismatch','abstained'),({'cell_id':'unknown'},'unsupported_population_cell','abstained'),({'mode':'future-wave'},'unsupported_mode','abstained'),({'expected_manifest_sha256':'0'*64},'manifest_checksum_mismatch','error')]:
  rejected=predict(folder,{**request,**mutation});assert rejected['status']==status and rejected['reason']['code']==reason and rejected['prediction'] is None,rejected
 checks.append({'bundle':folder.name,'architecture':manifest['architecture'],'actual_inference':'passed','reference_parity_tolerance':tolerance,'supported_request_seconds':elapsed,'guards':'Unknown question, changed instrument, unknown cell, future mode and wrong trusted checksum rejected.'})
 receipts.append({'bundle':folder.name,'request':request,'result':result})
 # Integrity tests deliberately alter a copied weight without changing the trusted manifest.
 if manifest['architecture']!='encoder_safetensors':
  with tempfile.TemporaryDirectory(prefix='tamper-',dir=ROOT) as temp:
   target=Path(temp);shutil.copytree(folder,target,dirs_exist_ok=True);path=target/manifest['model_file'];path.write_bytes(path.read_bytes()+b' ');bad=predict(target,request);assert bad['status']=='error' and bad['reason']['code']=='artifact_checksum_mismatch',bad
# Standard-library-only baseline smoke is checked in a separate interpreter by the shell invocation.
write(ROOT/'results/runtime-checks.json',{'passed':True,'checks':checks,'weights_loaded':'JSON and safetensors only; local base, no remote code or inference providers.','source_uploads':0,'paid_compute_usd':0})
write(ROOT/'results/runtime-smoke-receipts.json',receipts)
print(json.dumps({'passed':True,'bundles':len(checks),'architectures':sorted({c['architecture'] for c in checks})}))