Files
MeltMind/tools/verify_sampled_o2.py

115 lines
8.2 KiB
Python

"""Audit saved sampled O2 outputs and eligibility; never compute kernels or S1."""
import argparse
import hashlib
import json
from pathlib import Path
import numpy as np
import o2_rep as rep
def read(p):return json.loads(Path(p).read_text())
def sha(p):return hashlib.sha256(Path(p).read_bytes()).hexdigest()
def main():
parser=argparse.ArgumentParser();parser.add_argument('output',type=Path)
parser.add_argument('--sources',action='store_true');parser.add_argument('--report',type=Path)
a=parser.parse_args();out=a.output;c=read(out/'execution_contract.json');checks=[]
for path,h in c['code_sha256'].items():assert sha(path)==h,path
gate=read(out/'equivalence_gate.json');assert gate['status']=='PASS' and gate['fields']==6
for s in c['equivalence']:
dest=out/'equivalence'/s['job'];r=read(dest/'result.json');e=read(dest/'equivalence.json');sup=read(out/'equivalence'/(s['job']+'.supervisor.json'))
assert sha(dest/'result.json')==gate['result_sha256'][s['job']]==sup['result_sha256']
assert e['status']=='PASS' and len(e['arrays'])==30 and not e['mismatches']
assert all(row['bitwise_equal'] for row in e['arrays']) and sup['status']=='PASS'
for name,h in r['output_sha256'].items():assert sha(dest/name)==h
completion=read(out/'panel.completion.json');assert len(completion['records'])==24
for s,sup0 in zip(c['panel'],completion['records']):
job=s['job'];dest=out/'panel'/job;sup=read(out/'panel'/(job+'.supervisor.json'))
assert sup==sup0 and sup['job']==job
if sup['status']!='PASS':
assert sup['n_ref'] is None and sup['eligible_draw_count'] is None
checks.append(dict(case=s['case'],status='FAILURE_RETAINED',stop_reason=sup['stop_reason']));continue
assert sup['wall_seconds']<=1800 and sup['peak_rss_bytes']<=1342177280
assert sup['returncode']==0 and sup['stop_reason'] is None and sup['producer_final_peak_available']
r=read(dest/'result.json');assert sha(dest/'result.json')==sup['result_sha256']
assert r['status']=='COMPLETE' and r['neighbor_visits']==s['exact_unique_visits']
assert not r['S1_scoring'] and not r['training'] and r['new_distance_queries']==0
for name,h in r['output_sha256'].items():assert sha(dest/name)==h
for path,h in s['input_sha256'].items():
if a.sources or str(path).startswith('artifacts/s1-sampled-draw-2026-11-14'):assert sha(path)==h,path
with np.load(dest/'nodes.npz',allow_pickle=False) as z:n={k:z[k] for k in z.files}
with np.load(dest/'edges.npz',allow_pickle=False) as z:e={k:z[k] for k in z.files}
with np.load(s['anchor_file'],allow_pickle=False) as z:
assert np.array_equal(z['unique_ids'],n['anchor_ids']) and np.array_equal(z['multiplicity'],n['multiplicity'])
drawroot=Path(s['anchor_file']).parent
with np.load(drawroot/'inputs.npz',allow_pickle=False) as z:
ids=n['anchor_ids'];assert np.array_equal(z['boundary_distance_raw'][ids],n['distance'])
assert np.array_equal(z['node_lumped_mass'][ids],n['node_mass'])
assert np.array_equal(z['raw_node_ids'][ids],n['raw_node_ids'])
assert sum(map(int,z['neighbor_counts'][ids]))==r['neighbor_visits']
recomputed=rep.readout(n['tensor'],n['kernel_mass'],n['distance']>0,n['effective_samples'])
for key,value in recomputed.items():
assert value.shape==n[key].shape and value.dtype==n[key].dtype and value.tobytes()==n[key].tobytes(),(job,key)
ids=n['anchor_ids'];multiplicity=n['multiplicity'];u=len(ids)
assert int(multiplicity.sum())==2000 and u==r['anchors']
assert np.array_equal(e['anchor_ids'],ids[e['anchor_local']])
assert np.array_equal(e['anchor_ids'],e['edge_node_ids'][np.arange(len(e['q'])),e['endpoint_side']])
assert np.all(np.diff(e['edge_indices']*2+e['endpoint_side'])>0)
local=e['anchor_local'];valid=e['channel_valid']&np.isfinite(e['q']).all(1)
degree=np.bincount(local,minlength=u);vd=np.bincount(local[valid],minlength=u)
assert np.array_equal(degree,n['edge_count']) and np.array_equal(vd,n['valid_edge_count'])
assert np.array_equal(e['channel_valid'],e['direction_valid']&n['valid'][local])
assert np.array_equal(n['gate_channel_valid'],n['gate_direction_valid']&n['valid'])
assert np.all(e['q'][~e['channel_valid']]==0) and np.all(n['gate_q'][~n['gate_channel_valid']]==0)
for qvalues,gaps,mask in [(e['q'],n['g'][local],e['channel_valid']),
(n['gate_q'],n['g'],n['gate_channel_valid'])]:
if mask.any():
assert np.isfinite(qvalues[mask]).all()
assert np.max(-qvalues[mask],initial=0)<=c['frozen_algebra_tolerance']
assert np.max(qvalues[mask]-gaps[mask],initial=0)<=c['frozen_algebra_tolerance']
# Independent regrouping checks the all-valid-edge summary, allowing frozen algebra roundoff.
summary=np.full((u,4),np.nan)
for i in range(u):
values=e['q'][valid&(local==i)]
if len(values):summary[i]=[values[:,0].mean(),values[:,0].max(),values[:,1].mean(),values[:,1].max()]
np.testing.assert_allclose(summary,n['edge_summary'],rtol=0,atol=1e-10,equal_nan=True)
assert np.array_equal(n['edge_block_complete'],(vd>0)&np.isfinite(summary).all(1))
finite=np.isfinite(n['g']).all(1)&np.isfinite(n['gate_q']).all(1)&np.isfinite(n['gate_length'])&np.isfinite(n['distance'])
eligible=(n['distance']>0)&n['valid']&n['neff_pass']&n['gate_direction_valid']&n['gate_channel_valid']&n['edge_block_complete']&finite
assert np.array_equal(eligible,n['eligible'])
assert int(eligible.sum())==r['n_ref']==sup['n_ref']
assert int(multiplicity[eligible].sum())==r['eligible_draw_count']==sup['eligible_draw_count']
near=(n['distance']>0)&(n['distance']/r['t_part']<=.25)
assert np.array_equal(near,n['near_wall'])
assert int(multiplicity[eligible&near].sum())==r['eligible_near_wall_draw_count']
for code,name in rep.STATUS.items():
assert int((n['status']==code).sum())==r['status_counts'][name]
assert int(multiplicity[n['status']==code].sum())==r['status_draw_counts'][name]
if a.sources:
with np.load(s['geometry'],allow_pickle=False) as z:x=z['points_raw'];edges=z['edges'];gate_location=z['gate_location_raw'];mass=z['node_volume_raw']
full_degree=np.bincount(edges.ravel(),minlength=len(x))
assert np.array_equal(full_degree[ids],degree)
assert np.array_equal(edges[e['edge_indices']],e['edge_node_ids'])
lengths=np.linalg.norm(x[edges[:,1]]-x[edges[:,0]],axis=1);L=float(np.median(lengths[np.isfinite(lengths)&(lengths>0)]))
assert L==r['L_med_raw']
center=(mass[:,None]*x).sum(0)/mass.sum();Rg=float(np.sqrt(np.sum(mass*np.sum((x-center)**2,axis=1))/mass.sum()))
assert Rg==r['R_g_raw']
g=rep.contract_direction(n['B'],n['mu'],n['valid'],gate_location-x[ids],L,'gate')
for k,v in g.items():
key={'q':'gate_q','length':'gate_length','direction_valid':'gate_direction_valid','channel_valid':'gate_channel_valid','direction_reason':'gate_direction_reason'}[k]
assert v.tobytes()==n[key].tobytes()
for begin in range(0,len(local),4096):
stop=begin+4096;j=local[begin:stop];ends=e['edge_node_ids'][begin:stop]
q=rep.contract_direction(n['B'][j],n['mu'][j],n['valid'][j],x[ends[:,1]]-x[ends[:,0]],L,'edge')
for k,v in q.items():assert v.tobytes()==e[k][begin:stop].tobytes(),(job,k)
checks.append(dict(case=s['case'],status='PASS',n_ref=r['n_ref'],eligible_draw_count=r['eligible_draw_count'],neighbor_visits=r['neighbor_visits'],sources_checked=a.sources))
result=dict(status='PASS',parts=checks,equivalence_fields=6,shortfall_gate_evaluated=False,S1_scoring=False,training=False,
sources_checked=a.sources,note='Audits produced outputs; does not release HELD review status.')
if a.report:
with a.report.open('x') as f:json.dump(result,f,indent=2);f.write('\n')
print(json.dumps({k:v for k,v in result.items() if k!='parts'},indent=2))
if __name__=='__main__':main()