Files
MeltMind/tools/summarize_s2_ten_part_measurement.py

75 lines
6.2 KiB
Python

"""Verify measured artifacts and report costs; no recomputation or model fitting."""
import csv
import json
from pathlib import Path
import numpy as np
from freeze_g1_components import sha
from s2_e1_supervisor import disk_usage
ROOT=Path('artifacts/s2-ten-part-measurement-2026-11-15')
def main():
batch=json.loads((ROOT/'batch-complete.json').read_text())
contract=json.loads((ROOT/'contract.json').read_text())
public=ROOT/'public';rows=[];checks=[]
for attempt in batch['attempts']:
i=attempt['index'];spec=contract['cases'][i];dest=ROOT/'parts'/spec['case']
row={k:attempt[k] for k in ['index','case','status','stop_reason','cpu_seconds','wall_seconds','peak_tree_rss_bytes']}
row.update(nodes=spec['tet_nodes'],tets=spec['tet_cells'],tertile=spec['tertile'],output_disk_bytes=disk_usage(dest))
if (dest/'result.json').exists():
r=json.loads((dest/'result.json').read_text())
for name,h in r['output_sha256'].items():assert sha(dest/name)==h
row.update(preparation_status=r['status'],eligible_unique=r['eligible_unique'],eligible_slots=r['eligible_slots'],
conditional_coverage=r['conditional_coverage'],full_lumped_coverage=r['full_lumped_coverage'],
distance_mode=r['distance_mode'],o2_mode=r['o2_mode'],r2_error=r['r2_error'],timings=r['timings'],result_sha256=sha(dest/'result.json'))
base=np.load(dest/'base.npy',allow_pickle=False,mmap_mode='r')
assert base.shape==(r['nodes'],10) and np.isfinite(base).all()
with np.load(dest/'queries.npz',allow_pickle=False) as q:
ids=q['anchor_ids'];counts=q['multiplicity'];eligible=q['eligible_s2']
assert np.all(np.diff(ids)>0) and counts.sum()==2000 and np.all(counts>0)
expected=(q['distance']>0)&q['spectral_valid']&q['neff_pass']&q['edge_block_complete']&np.isfinite(q['g']).all(1)&np.isfinite(q['edge_summary']).all(1)
assert np.array_equal(eligible,expected)
assert int(counts[eligible].sum())==r['eligible_slots']
with np.load(dest/'draw.npz',allow_pickle=False) as draw:
assert np.array_equal(ids[draw['draw_to_unique']],draw['draw_ids'])
assert np.array_equal(counts,draw['multiplicity'])
if r['r2_error'] is None:
with np.load(dest/'r2.npz',allow_pickle=False) as z:assert z['features'].shape==(r['nodes'],20) and np.isfinite(z['features']).all()
assert r['label_arrays_decoded']==[] and not r['model_fit'] and not r['gpu'] and not r['gate_q_used_for_eligibility']
checks.append(dict(case=spec['case'],status='PASS',checks=['output hashes','base shape/finiteness','draw identity/multiplicity','S2 common eligibility/coverage','R2 shape/finiteness if completed','no labels/fit/GPU recorded']))
elif (dest/'failure.json').exists():row['failure']=json.loads((dest/'failure.json').read_text())
rows.append(row)
for p,h in contract['code_sha256'].items():assert sha(p)==h,p
parts=json.loads(Path('artifacts/s2-g1-components-2026-11-15/custodian/part-splits.json').read_text())
non_test=[p for p in parts if p['assignments'][0]['role']!='test']
requested={p['case'] for p in non_test};completed={r['case'] for r in rows if r.get('preparation_status')=='COMPLETE' and r['status']=='PASS'}
pilot=dict(proposed_fixed_fold=0,proposed_seed=0,learning_parts=387,validation_parts=112,sealed_test_parts=125,
non_test_parts=len(non_test),non_test_nodes=sum(p['nodes'] for p in non_test),
prepared_from_this_ten_part_stage=len(requested&completed),remaining_after_this_stage=len(requested-completed),
train_loop_and_GPU_supervisor='NOT_YET_ACCEPTED',labels_read=False,pilot_started=False)
# Transparent planning scenarios, not acceptance bounds: compute total roster
# costs using rates from nine newly prepared parts, excluding cached 666.
cold=[r for r in rows if r['case'] in completed and not r['case'].startswith('00000666')]
if cold:
rates=np.array([r['cpu_seconds']/r['nodes'] for r in cold]);bytes_per_node=np.array([r['output_disk_bytes']/r['nodes'] for r in rows if r['case'] in completed])
missing_nodes=sum(p['nodes'] for p in non_test if p['case'] not in completed)
pilot['missing_non_test_nodes']=missing_nodes
pilot['linear_node_rate_scenarios']={name:dict(cpu_hours=float(np.quantile(rates,q)*missing_nodes/3600),additional_gib=float(np.quantile(bytes_per_node,q)*missing_nodes/2**30)) for name,q in [('median',.5),('p95',.95),('max',1)]}
pilot['scenario_limit']='Shape/neighbourhood-dependent runtime; nine new cases, cache reuse and cold largest-part costs differ; these are not guarantees or authority to exceed caps'
result=dict(status='MEASUREMENT_RECORDED_AWAITING_REVIEW',contract_sha256=sha(ROOT/'contract.json'),batch_sha256=sha(ROOT/'batch-complete.json'),
attempted=len(rows),complete=sum(r.get('preparation_status')=='COMPLETE' and r['status']=='PASS' for r in rows),
failed_or_partial=sum(r.get('preparation_status')!='COMPLETE' or r['status']!='PASS' for r in rows),not_run=batch['not_run'],
worker_cpu_seconds=batch['worker_cpu_seconds'],supervisor_cpu_seconds=batch['supervisor_cpu_seconds'],wall_seconds=batch['wall_seconds'],
peak_worker_tree_rss_bytes=batch['peak_worker_tree_rss_bytes'],disk_bytes_before_publication=disk_usage(ROOT),
checks=checks,rows=rows,pilot_readiness=pilot,
no_repeated_G1_geometry_registration_review=True,no_labels_fit_gpu=True,code_sha256=sha(__file__))
with (public/'measurement-result.json').open('x') as f:json.dump(result,f,ensure_ascii=False,indent=2);f.write('\n')
with (public/'cost-table.csv').open('x') as f:
fields=['index','case','nodes','tets','tertile','status','preparation_status','cpu_seconds','wall_seconds','peak_tree_rss_bytes','output_disk_bytes','eligible_slots','distance_mode','o2_mode']
w=csv.DictWriter(f,fields,extrasaction='ignore');w.writeheader();w.writerows(rows)
print(json.dumps({k:v for k,v in result.items() if k not in ['checks','rows']},ensure_ascii=False))
if __name__=='__main__':main()