Publish exact H2 S2 pooled scorer review binding, no postfit launch

This commit is contained in:
MeltAstra committed 2026-11-17 22:56:30 +08:00
1 parent 0436fba83f
commit 8354a2f6c0
22 files changed
+76542

No files matched your search

+125
View File
@@ -0,0 +1,125 @@
"""Single S2 postfit: four frozen-checkpoint predictions then exact-reviewed pooled score."""
import argparse,fcntl,os,resource,sys,time
from pathlib import Path
import numpy as np
import psutil
from phase2_h1_worker_v2 import save,sha,checked
from phase2_home_freeze_io import freeze,verify
from phase2_gate_outcome_review import validate_review
from phase2_h2_s2_postfit_common import read,config,terminal_inputs,adopted_inputs,checkpoint_identity,comparisons,decision,require_budget,roster,reuse_fold0_metric
from phase2_h2_s2_common import fold_view
from phase2_h2_s1_postfit import query_index
from phase2_residual_math import decode_seconds
from s2_custody_common import prediction_inputs
from phase2_b1213_features import cached
from s2_custody_statistics import part_metrics
from r4bc_guard import supervise
def predict(root,fold):
import torch
from phase2_h2_model import H2Geo
from phase2_h2_data import to_device,arguments
from phase2_h2_s1_test_data import load_test_part
if fold not in (1,2,3,4):raise PermissionError('Fold0 predictions are hash reuse only')
c,pilot,tc=config(root);validate_review(root);adopted=adopted_inputs(root,c,pilot,tc);m=next(m for m in adopted['models'] if m['fold']==fold);view=fold_view(pilot,fold);f=view['folds'][0];job=tc['jobs'][fold-1];stats=read(m['standardization']);by=roster(c,pilot)
dest=root/'folds'/str(fold)/'predictions';dest.mkdir(exist_ok=False)
if torch.__version__!=pilot['versions']['torch'] or np.__version__!=pilot['versions']['numpy']:raise ValueError('Original scientific library versions')
if os.environ.get('CUDA_VISIBLE_DEVICES')!=job['gpu_uuid'] or os.environ.get('PYTORCH_CUDA_ALLOC_CONF')!='expandable_segments:True':raise ValueError('GPU/allocator identity')
os.sched_setaffinity(0,job['worker_cpus']);torch.set_num_threads(1);torch.set_num_interop_threads(1);torch.backends.cuda.matmul.allow_tf32=False;torch.backends.cudnn.allow_tf32=False
torch.cuda.set_device(0);torch.cuda.init();torch.cuda.reset_peak_memory_stats();start=time.monotonic();cpu=time.process_time();entries=[]
def peaks():return dict(peak_allocated_bytes=int(torch.cuda.max_memory_allocated()),peak_reserved_bytes=int(torch.cuda.max_memory_reserved()))
try:
blob=torch.load(checked(m['checkpoint'],m['checkpoint_sha256']),map_location='cpu',weights_only=False);checkpoint_identity(blob,c,fold,len(f['learning']));model=H2Geo().cuda();model.load_state_dict(blob['model'],strict=True);model.eval();del blob
save(dest/'environment.json',dict(fold=fold,torch=torch.__version__,gpu_uuid=job['gpu_uuid'],allocator_settings=str(torch.cuda.memory._snapshot().get('allocator_settings',{})),model_dtype='FP32',autocast=False,checkpoint_sha256=m['checkpoint_sha256']))
with torch.no_grad():
for case in f['test']:
row=by[case];data=to_device(load_test_part(view,case,stats),'cuda');residual=model(*arguments(data),checkpoint_blocks=False)
if not bool(torch.isfinite(residual).all()):raise ValueError('Nonfinite H2 residual')
seconds=decode_seconds(residual.cpu().numpy(),data['F'],f['c'],data['t_set']);torch.cuda.synchronize();q=prediction_inputs(row);a=c['baseline_predictions'][case]
with np.load(checked(a['path'],a['sha256']),allow_pickle=False) as old:
ix=query_index(data['raw_ids'],q,old);A=old['A'].copy();G=old['pred'].copy()
if not np.isfinite(A).all() or not np.isfinite(G).all() or len(seconds)!=row['nodes']:raise ValueError('Full-node/baseline coverage')
path=dest/(case+'.npz');np.savez_compressed(path,seconds=seconds,raw_ids=data['raw_ids'],pred=seconds[ix],A=A,B13_E1b=G,query_raw_ids=q['raw_ids'],counts=q['counts'],coordinates_over_Rg=q['coordinates_over_Rg'],near_wall=q['near_wall'])
entries.append(dict(case=case,component_id=row['component_id'],fold=fold,path=str(path),sha256=sha(path),nodes=row['nodes'],query_nodes=len(ix),A_source_sha256=a['sha256'],negative_full_predictions=int(np.sum(seconds<0)),negative_query_predictions=int(np.sum(seconds[ix]<0)),origin='S2_FROZEN_CHECKPOINT'))
save(dest/'progress.json',dict(stage='TEST_PREDICTION_NO_TARGETS',fold=fold,parts=len(entries),wall_seconds=time.monotonic()-start,cpu_seconds=time.process_time()-cpu,**peaks()));del data,residual,seconds
save(dest/'manifest.json',dict(entries=entries,test_targets_read=False,new_fits=0,full_nodes=sum(e['nodes'] for e in entries)))
save(dest/'result.json',dict(status='COMPLETE',fold=fold,parts=len(entries),test_targets_read=False,wall_seconds=time.monotonic()-start,cpu_seconds=time.process_time()-cpu,**peaks()))
except BaseException as exc:
save(dest/'failure.json',dict(status='FAILED',fold=fold,reason=type(exc).__name__+': '+str(exc),completed_parts=len(entries),wall_seconds=time.monotonic()-start,cpu_seconds=time.process_time()-cpu,**peaks()));raise
def manifest_identity(entries,c,pilot):
by=roster(c,pilot);old={e['case']:e for e in read(c['fold0_manifest'])['entries']}
if len(entries)!=552 or len({e['case'] for e in entries})!=552 or {e['case'] for e in entries}!=set(by):raise ValueError('All552 distinct predictions required')
for e in entries:
row=by[e['case']];fold=next(a['fold'] for a in row['assignments'] if a['role']=='test')
if e['fold']!=fold or e['component_id']!=row['component_id'] or e['nodes']!=row['nodes']:raise ValueError('Prediction roster identity')
if fold==0:
if e.get('origin')!='S1_HASH_REUSE' or {k:v for k,v in e.items() if k!='origin'}!=old[e['case']]:raise ValueError('Fold0 entry must be exact original hash reuse')
elif e.get('origin')!='S2_FROZEN_CHECKPOINT' or e['A_source_sha256']!=c['baseline_predictions'][e['case']]['sha256'] or Path(e['path']).resolve()!=(Path(c['postfit_root'])/'folds'/str(fold)/'predictions'/(e['case']+'.npz')).resolve():raise ValueError('New prediction source identity')
for key,limit in [('negative_full_predictions',e['nodes']),('negative_query_predictions',e['query_nodes'])]:
if isinstance(e[key],bool) or not isinstance(e[key],int) or not 0<=e[key]<=limit:raise ValueError('Invalid unclipped negative count')
return by
def score(root):
c,pilot,tc=config(root);validate_review(root);adopted_inputs(root,c,pilot,tc);verify(root,'predictions')
entries=read(root/'predictions-manifest.json')['entries'];by=manifest_identity(entries,c,pilot);dest=root/'score';dest.mkdir(exist_ok=False)
save(dest/'review-consumed.json',dict(unix=time.time(),review_sha256=sha(root/'review.json'),before_old_score_or_target_values=True))
oldA={r['case']:r for r in read(checked(c['baseline_score'],c['source_sha256'][c['baseline_score']]))['rows']};oldH={r['case']:r for r in read(checked(c['fold0_score'],c['source_sha256'][c['fold0_score']]))['rows']};rows=[];access=[]
if len(oldH)!=121 or set(oldH)!=set(pilot['folds'][0]['test']) or len(oldA)!=552 or set(oldA)!=set(by):raise ValueError('Frozen reference score coverage')
for e in entries:
case=e['case'];row=by[case];old=oldA[case];checked(e['path'],e['sha256'])
if old['fold']!=e['fold'] or old['component_id']!=e['component_id']:raise ValueError('Baseline score pairing')
if e['fold']==0:
metric=reuse_fold0_metric(oldH[case],old,e) # No fold0 target read, prediction, or per-part metric recomputation.
else:
access.append(dict(case=case,fold=e['fold'],role='test',target_sha256=row['v5_cache']['output_sha256']['seconds.npy']));save(dest/'target-access.json',access)
truth=cached(row,'seconds');raw=cached(row,'raw_ids');base=c['baseline_predictions'][case]
with np.load(checked(e['path'],e['sha256']),allow_pickle=False) as z,np.load(checked(base['path'],base['sha256']),allow_pickle=False) as original:
q={k:z['query_raw_ids' if k=='raw_ids' else k] for k in ['raw_ids','counts','coordinates_over_Rg','near_wall']};ix=query_index(raw,q,original)
if not np.array_equal(z['raw_ids'],raw) or not np.array_equal(z['pred'],z['seconds'][ix]) or not np.array_equal(z['A'],original['A']) or not np.array_equal(z['B13_E1b'],original['pred']):raise ValueError('Full/query/reference prediction identity')
if len(z['seconds'])!=e['nodes'] or len(ix)!=e['query_nodes'] or int(np.sum(z['seconds']<0))!=e['negative_full_predictions'] or int(np.sum(z['pred']<0))!=e['negative_query_predictions']:raise ValueError('Negative counts or coverage differ')
metric=part_metrics(z['pred'],truth[ix],q['counts'],q['near_wall'],q['raw_ids'],q['coordinates_over_Rg'],int(case.split('_')[0]))
rows.append(dict(case=case,component_id=e['component_id'],fold=e['fold'],H2=metric,A=old['A'],B13_E1b=old['new'],baseline_G_valid=old['G_valid'],H2_origin=e['origin']));save(dest/'progress.json',dict(parts=len(rows),new_test_target_reads=len(access)))
if len(access)!=431 or len({x['case'] for x in access})!=431:raise ValueError('Exactly431 new held-out targets; fold0 untouched')
metrics=comparisons(rows);result=decision(metrics,len(rows),len({r['component_id'] for r in rows}))
save(dest/'result.json',dict(status='COMPLETE',decision=result,parts=552,components=204,comparisons=metrics,per_fold={str(f):comparisons([r for r in rows if r['fold']==f]) for f in range(5)},rows=rows,confirmation_rule='pooled H2-A field CI upper<0, complete552/204; B13/last/tau report only',bootstrap=dict(replicates=2000,seed=2026111408),negative_predictions=dict(full_nodes=sum(e['negative_full_predictions'] for e in entries),eligible_unique_query_nodes=sum(e['negative_query_predictions'] for e in entries),clipped=False),full_nodes=sum(e['nodes'] for e in entries),eligible_unique_query_nodes=sum(e['query_nodes'] for e in entries),baseline_G_invalid_parts=sum(not r['baseline_G_valid'] for r in rows),fold0_score_reused=121,new_test_target_reads=431,new_decodes=0,new_fits=0,review_sha256=sha(root/'review.json'),data_sufficiency_started=False,interpretation='Five-fold recipe confirmation after observing fold0; no pure connectivity causality or untouched-five-fold preregistration claim'))
def run(root):
start=time.process_time();u0=resource.getrusage(resource.RUSAGE_CHILDREN);c,pilot,tc=config(root);validate_review(root);adopted=terminal_inputs(c,pilot,tc);lock=(root/'run.lock').open('a');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
if (root/'started.json').exists():raise FileExistsError('Single pooled postfit attempt')
for p in psutil.process_iter(['pid','cmdline']):
if p.pid!=os.getpid() and any(Path(a).name=='phase2_h2_s2_postfit.py' for a in p.info['cmdline'] or []):raise RuntimeError('Existing S2 postfit process')
os.sched_setaffinity(0,[12,13]);os.environ['PYTORCH_CUDA_ALLOC_CONF']='expandable_segments:True';gpu=0.;charged=0.;records=[];prediction_start=None;prior=adopted['prior_gpu_seconds']
def cpu():
u=resource.getrusage(resource.RUSAGE_CHILDREN);return 360+time.process_time()-start+max(charged,u.ru_utime+u.ru_stime-u0.ru_utime-u0.ru_stime)
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),prior_gpu_once=prior,old_s1_gpu_once=adopted['old_s1_gpu_once'],new_training_gpu_once=adopted['new_training_gpu_once'],review_sha256=sha(root/'review.json'),new_fits=0,fold0_reexecution=False))
try:
require_budget(prior,gpu,cpu(),4);save(root/'models-adopted.json',adopted);train=Path(c['training_root'])
freeze(root,'models',[root/'contract.json',root/'models-adopted.json',train/'complete.json',train/'models-terminal.json',train/'models-git-freeze.json',train/'terminal-git-freeze.json',*[Path(m['checkpoint']) for m in adopted['models']],*[Path(m['standardization']) for m in adopted['models']]])
entries=[dict(e,origin='S1_HASH_REUSE') for e in read(c['fold0_manifest'])['entries']]
for e in entries:checked(e['path'],e['sha256'])
from phase2_h2_s2_prediction_guard import run_prediction
for fold in range(1,5):
require_budget(prior,gpu,cpu(),5-fold);(root/'folds'/str(fold)).mkdir(parents=True,exist_ok=False);prediction_start=time.monotonic();receipt=run_prediction(root,c,pilot,fold,prior+gpu,600)
observed=60+time.monotonic()-prediction_start;prediction_start=None;cost=max(receipt['gpu_seconds_charged'],observed);gpu+=cost;charged+=receipt['worker']['cpu_seconds'] if receipt['worker'] else 0.;records.append(dict(mode='predict',fold=fold,charged_gpu_seconds=cost,guard=receipt))
save(root/'resource-progress.json',dict(records=records,prediction_gpu_seconds=gpu,postfit_cpu_seconds=cpu(),cumulative_h2_gpu_seconds=prior+gpu))
if receipt['status']!='PASS' or cost>600 or gpu>2400 or prior+gpu>144000:raise RuntimeError('Prediction not PASS or cumulative GPU limit')
m=read(root/'folds'/str(fold)/'predictions/manifest.json')
if len(m['entries'])!=len(pilot['folds'][fold]['test']) or m['full_nodes']!=pilot['folds'][fold]['test_nodes']:raise ValueError('Full-fold prediction coverage')
for e in m['entries']:checked(e['path'],e['sha256'])
entries.extend(m['entries'])
manifest_identity(entries,c,pilot);save(root/'predictions-manifest.json',dict(entries=entries,new_fits=0,fold0_reused=121,new_predictions=431,test_targets_read=False));freeze(root,'predictions',[root/'contract.json',root/'predictions-manifest.json',root/'models-adopted.json',root/'resource-progress.json',*[Path(p) for p in c['code_sha256']]])
require_budget(prior,gpu,cpu(),0)
g=supervise([sys.executable,'tools/phase2_h2_s2_postfit.py','score',str(root)],root/'score-guard',dict(cpu_seconds=600,wall_seconds=780,rss_bytes=16*2**30,disk_bytes=2*2**30));charged+=g['cpu_seconds'];records.append(dict(mode='score',guard=g))
if g['status']!='PASS' or cpu()>3600:raise RuntimeError('Score not PASS or CPU limit')
save(root/'complete.json',dict(status='COMPLETE',records=records,prediction_gpu_seconds=gpu,postfit_cpu_seconds=cpu(),prior_gpu_once=prior,cumulative_h2_gpu_seconds=prior+gpu,decision=read(root/'score/result.json')['decision'],new_fits=0,fold0_reexecution=False,data_sufficiency_started=False,no_retry=True));freeze(root,'terminal',[root/'contract.json',root/'complete.json',root/'score/result.json',root/'review.json'])
save(root/'post-terminal-cost.json',dict(postfit_cpu_seconds=cpu(),prediction_gpu_seconds=gpu,cumulative_h2_gpu_seconds=prior+gpu,within_cap=cpu()<=3600 and gpu<=2400 and prior+gpu<=144000))
except BaseException as exc:
if prediction_start is not None:gpu+=60+time.monotonic()-prediction_start
save(root/'complete.json',dict(status='STOPPED',reason=type(exc).__name__+': '+str(exc),records=records,postfit_cpu_seconds=cpu(),prediction_gpu_seconds=gpu,prior_gpu_once=prior,cumulative_h2_gpu_seconds=prior+gpu,new_fits=0,no_retry=True));raise
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('action',choices=['run','predict','score']);p.add_argument('root',type=Path);p.add_argument('fold',type=int,nargs='?');a=p.parse_args()
if a.action=='predict':predict(a.root,a.fold)
elif a.fold is not None:p.error('Fold allowed only for predict')
elif a.action=='score':score(a.root)
else:run(a.root)
+103
View File
@@ -0,0 +1,103 @@
"""S2 postfit identities, all-four terminal barrier and unchanged paired statistics."""
import json,math,os
from pathlib import Path
import numpy as np
from phase2_h1_worker_v2 import checked,sha
from phase2_home_freeze_io import verify
from phase2_h2_s2_common import confirmed
from phase2_b13_e1b import pooled
def read(p):return json.loads(Path(p).read_text())
def roster(c,pilot):
rows=c['rows'];by={r['case']:r for r in rows}
if len(rows)!=552 or len(by)!=552 or len({r['component_id'] for r in rows})!=204:raise ValueError('Fixed552/204 roster')
alltest=[];component_folds={}
for f in pilot['folds']:
for case in f['test']:
row=by[case];pr=pilot['parts'][case];a=c['baseline_predictions'][case]
tests=[v['fold'] for v in row['assignments'] if v['role']=='test']
if tests!=[f['fold']] or a['fold']!=f['fold'] or row['component_id']!=a['component_id'] or row['component_id']!=pr['clock_group'] or row['v5_cache']['output_sha256']!=pr['output_sha256'] or row['nodes']!=pr['nodes'] or row['preparation']['t_set']!=pr['t_set']:raise ValueError('Original test/cache/clock/query baseline identity')
group=row['component_id']
if group in component_folds and component_folds[group]!=f['fold']:raise ValueError('Component spans test folds')
component_folds[group]=f['fold'];alltest.append(case)
if len(alltest)!=552 or len(set(alltest))!=552 or set(alltest)!=set(by):raise ValueError('Original five-fold partition')
return by
def config(root):
c=read(root/'contract.json');verify(root,'implementation')
if c['approval']!='1794925470.101149' or c['total_h2_gpu_seconds']!=144000 or c['prediction_gpu_seconds']!=2400 or c['postfit_cpu_seconds']!=3600 or c['new_fits']!=0 or c['setup_cpu_seconds']!=360:raise PermissionError('Fixed pooled postfit scope')
for p,h in {**c['source_sha256'],**c['code_sha256']}.items():checked(p,h)
train=Path(c['training_root']);proof=verify(train,'implementation')
if proof['commit']!=c['training_implementation']:raise ValueError('Wrong frozen training implementation')
tc=read(train/'contract.json');pilot=read(c['pilot_contract'])
if np.__version__!=pilot['versions']['numpy']:raise ValueError('Original NumPy version required')
verify(Path(c['s1_root']),'predictions');verify(Path(c['s1_root']),'terminal')
if tc['prior_gpu_seconds']!=18858.21490871499 or tc['prediction_gpu_reserve']!=2400 or tc['postfit_cpu_cap']!=3600 or tc['epochs']!=200 or tc['seed']!=0:raise ValueError('Original training budget/recipe')
if [j['fold'] for j in tc['jobs']]!=[1,2,3,4] or c['training_jobs']!=tc['jobs']:raise ValueError('Original four jobs')
roster(c,pilot);return c,pilot,tc
def terminal_identity(tc,done,ledger,models):
if done.get('status')!='TRAIN_COMPLETE_POSTFIT_PENDING' or done.get('postfit_started') is not False:raise PermissionError('All-four training terminal required')
entries=done.get('terminal',[])
if len(entries)!=4 or {e['fold'] for e in entries}!={1,2,3,4} or any(e['status']!='PASS' or e['returncode']!=0 for e in entries):raise ValueError('Exactly four PASS terminals')
if ledger['entries']!=entries or len(models['models'])!=4 or {m['fold'] for m in models['models']}!={1,2,3,4}:raise ValueError('Complete terminal/model manifest')
if models['fold0_reused_by_hash']!=tc['fold0_reuse'] or models['new_fits']!=4 or models['test_targets_read'] is not False or models['postfit_started'] is not False:raise ValueError('Model manifest roles')
costs=[e['gpu_seconds_charged'] for e in entries]
if not all(math.isfinite(x) and x>=0 for x in costs):raise ValueError('Invalid settled GPU cost')
total=sum(costs)
if done['prior_gpu_once']!=tc['prior_gpu_seconds'] or ledger['prior_gpu_once']!=tc['prior_gpu_seconds'] or ledger['settled_gpu_seconds']!=total or done['new_gpu_seconds_charged']!=total or done['cumulative_h2_gpu_seconds']!=tc['prior_gpu_seconds']+total:raise ValueError('Prior and four new costs must be counted once')
if total>tc['train_gpu_cap'] or done['cumulative_h2_gpu_seconds']+2400>144000:raise ValueError('No original prediction reserve')
return done['cumulative_h2_gpu_seconds']
def terminal_inputs(c,pilot,tc):
import psutil
train=Path(c['training_root']);terminal=verify(train,'terminal');modelproof=verify(train,'models')
if (train/'global-stop.json').exists():raise PermissionError('Training stopped')
done=read(train/'complete.json');ledger=read(train/'terminal-ledger.json');manifest=read(train/'models-terminal.json');prior=terminal_identity(tc,done,ledger,manifest)
for p in psutil.process_iter(['pid','cmdline']):
args=p.info['cmdline'] or []
if str(train) in args and any(Path(x).name in ['phase2_h2_s2_queue.py','phase2_h2_s2_guard.py','phase2_h2_s2_worker.py'] for x in args):raise RuntimeError('Training process still alive')
entries={e['fold']:e for e in done['terminal']};out=[]
for m in sorted(manifest['models'],key=lambda m:m['fold']):
f=m['fold'];folder=train/'folds'/str(f);guard=read(checked(folder/'complete.json',entries[f]['complete_sha256']));resultpath=checked(folder/'worker/result.json',m['worker_result_sha256']);result=read(resultpath);p=pilot['folds'][f]
if guard['status']!='PASS' or guard['reasons'] or guard['monitor_errors'] or guard['worker_result_sha256']!=m['worker_result_sha256']:raise ValueError('Original guard/result identity')
if result['fold']!=f or result['completed_epochs']!=200 or result['optimizer_steps']!=200*len(p['learning']) or result['learning_nodes_per_epoch']!=p['learning_nodes'] or result['test_targets_read'] is not False or result['validation_used_for_selection'] is not False:raise ValueError('Final200/role identity')
expected=folder/'worker/last-epoch200.pt';stdpath=folder/'worker/standardization.json'
if Path(m['checkpoint']).resolve()!=expected.resolve() or Path(m['standardization']).resolve()!=stdpath.resolve() or m['checkpoint_sha256']!=result['checkpoint_sha256'] or m['standardization_sha256']!=result['standardization_sha256']:raise ValueError('Checkpoint/normalization path identity')
for path,digest in [(expected,m['checkpoint_sha256']),(stdpath,m['standardization_sha256'])]:
checked(path,digest)
if modelproof['files'].get(str(path.resolve()))!=digest:raise ValueError('Checkpoint/normalization omitted from model freeze')
stats=read(stdpath)
if stats['parts']!=p['learning'] or stats['nodes']!=p['learning_nodes'] or len(stats['mean'])!=19 or len(stats['scale'])!=19 or not np.isfinite(stats['mean']).all() or not np.isfinite(stats['scale']).all() or min(stats['scale'])<=0:raise ValueError('Original learning-only normalization')
out.append(m)
return dict(training_terminal_commit=terminal['commit'],training_models_commit=modelproof['commit'],training_complete_sha256=sha(train/'complete.json'),training_models_sha256=sha(train/'models-terminal.json'),training_contract_sha256=c['source_sha256'][str(train/'contract.json')],models=out,prior_gpu_seconds=prior,old_s1_gpu_once=tc['prior_gpu_seconds'],new_training_gpu_once=done['new_gpu_seconds_charged'])
def adopted_inputs(root,c,pilot,tc):
verify(root,'models');a=read(root/'models-adopted.json')
if a!=terminal_inputs(c,pilot,tc):raise ValueError('Adopted four frozen checkpoints changed')
return a
def checkpoint_identity(blob,c,fold,parts):
if fold not in (1,2,3,4) or blob['completed_epochs']!=200 or blob['optimizer_steps']!=200*parts or blob['seed']!=0 or blob['fold']!=fold or blob['role']!='last_epoch_only' or blob['contract_sha256']!=c['source_sha256'][str(Path(c['training_root'])/'contract.json')]:raise ValueError('Wrong fold/final checkpoint')
def comparisons(rows):
out={}
for reference in ['A','B13_E1b']:
raw=pooled([dict(component_id=r['component_id'],new=r['H2'],A=r[reference]) for r in rows]);out[reference]={}
for name,m in raw.items():out[reference][name]=dict(means={'H2':m['means']['new'],reference:m['means']['A']},components={'H2':m['components']['new'],reference:m['components']['A']},H2_minus_reference=m['new_minus_A'])
return out
def reuse_fold0_metric(prior,baseline,entry):
if entry['fold']!=0 or prior['fold']!=0 or prior['component_id']!=entry['component_id'] or prior['A']!=baseline['A']:raise ValueError('Fold0 frozen score identity')
return prior['new']
def decision(results,parts,groups):
field=results['A']['relative_l2'];paired=field['H2_minus_reference']
if parts!=552 or groups!=204 or field['components']!={'H2':204,'A':204} or paired['components']!=204:return 'PARK_INCOMPLETE_FIELD_SUPPORT'
return 'CONFIRMED' if confirmed(paired['high'],parts,groups) else 'NOT_CONFIRMED_FIXED_RECIPE'
def require_budget(prior,gpu_spent,cpu_spent,remaining_predictions):
if not all(math.isfinite(x) and x>=0 for x in [prior,gpu_spent,cpu_spent]) or remaining_predictions not in range(5):raise ValueError('Invalid resource ledger')
if gpu_spent+remaining_predictions*600>2400 or prior+gpu_spent+remaining_predictions*600>144000:raise RuntimeError('Prediction GPU reservation')
# All four60-second local CPU setup allowances are already in setup_cpu_seconds=360.
if cpu_spent+remaining_predictions*540+600+120>3600:raise RuntimeError('Postfit CPU reservation')
+74
View File
@@ -0,0 +1,74 @@
"""S2 one-fold600 GPU-second prediction, retained failure peaks, no retries."""
import json,os,signal,sys,threading,time
import psutil
from phase2_h1_worker_v2 import save
from s2_main_memory_guard import PeakRecords
from s2_precision_continuation_guard import device_usage
from s2_recovery_gpu_supervisor import supervise,tree_usage,stop_group,disk_usage
def final_reasons(reasons,receipt,result,tracker,gpu_seconds,cpu_seconds,cpu_cap,expected_parts):
out=list(reasons)
def add(s):
if s not in out:out.append(s)
if not receipt or receipt['status']!='PASS':add('WORKER_NOT_PASS')
if not result or result.get('status')!='COMPLETE' or result.get('parts')!=expected_parts:add('INCOMPLETE_FOLD')
if not all(k in tracker.witnesses for k in ['peak_allocated_bytes','peak_reserved_bytes']):add('MISSING_PEAKS')
if any(v>15*2**30 for v in tracker.maxima.values()):add('RECORDED_VRAM_LIMIT')
if gpu_seconds>600 or cpu_seconds>cpu_cap:add('FINAL_RESOURCE_LIMIT')
return out
def run_prediction(queue,c,pilot,fold,prior_gpu,cpu_cap):
root=queue/'folds'/str(fold);gpu_uuid=c['training_jobs'][fold-1]['gpu_uuid'];expected_parts=len(pilot['folds'][fold]['test'])
start=time.monotonic();cpu=time.process_time();requested=[];reasons=[];errors=[];samples=[];done=threading.Event();tracker=PeakRecords();receipt=None
def elapsed():return 60+time.monotonic()-start
def reason(x):
if x not in reasons:reasons.append(x)
command=[sys.executable,'tools/phase2_h2_s2_postfit.py','predict',str(queue),str(fold)]
def kill():
for p in psutil.Process().children(recursive=True):
try:
if p.cmdline()==command:stop_group(p.pid)
except psutil.NoSuchProcess:pass
def scan():
if tracker.scan(root/'predictions'):reason('RECORDED_VRAM_LIMIT')
def watch():
while not done.is_set():
try:
scan();mem,util=device_usage(gpu_uuid);_,rss,_=tree_usage(os.getpid())
if mem>15*2**30:reason('DEVICE_VRAM_LIMIT')
if rss>16*2**30:reason('TREE_RSS_LIMIT')
if disk_usage(queue)>20*2**30:reason('DISK_LIMIT')
if elapsed()>=595:reason('PREDICTION_TIME_LIMIT')
for x in requested:reason(x)
save(root/'prediction-live.json',dict(gpu_seconds_charged=elapsed(),device_bytes=mem,device_utilization=util,rss_bytes=rss,peaks=tracker.maxima,reasons=reasons,unix=time.time()))
except Exception as exc:reason('MONITOR_ERROR');errors.append(str(exc))
if reasons:kill()
done.wait(.5)
old={s:signal.signal(s,lambda signum,frame:requested.append('COORDINATOR_SIGNAL_'+str(signum))) for s in [signal.SIGTERM,signal.SIGINT]};thread=None
try:
for i in range(3):
mem,util=device_usage(gpu_uuid);samples.append(dict(unix=time.time(),bytes=mem,utilization=util))
if mem>128*2**20 or util!=0:raise RuntimeError('SELECTED_GPU_BUSY_NO_RETRY')
if i<2:time.sleep(2)
save(root/'prediction-idle-polls.json',samples)
if requested:raise RuntimeError('Signal received during setup')
thread=threading.Thread(target=watch,daemon=True);thread.start()
remaining=600-elapsed()-10
if remaining<=0:raise RuntimeError('Setup exhausted prediction budget')
receipt=supervise(command,root/'prediction-guard','predict',dict(total_cpu_seconds=cpu_cap-60,job_cpu_seconds=cpu_cap-60,job_wall_seconds=remaining,tree_rss_bytes=16*2**30,disk_bytes=20*2**30,max_processes=1),poll=.2,gpu_uuid=gpu_uuid)
except BaseException as exc:reason(type(exc).__name__+': '+str(exc));kill()
finally:
done.set()
if thread:thread.join(timeout=4)
if thread and thread.is_alive():reason('MONITOR_DID_NOT_EXIT')
try:scan()
except Exception as exc:reason('FINAL_SCAN_ERROR');errors.append(str(exc))
for s,h in old.items():signal.signal(s,h)
result=None;p=root/'predictions/result.json'
try:
if p.exists():result=json.loads(p.read_text())
except Exception as exc:reason('FINAL_RESULT_READ_ERROR');errors.append(str(exc))
totalcpu=60+(receipt['cpu_seconds'] if receipt else 0)+time.process_time()-cpu
duration=elapsed();reasons=final_reasons(reasons,receipt,result,tracker,duration,totalcpu,cpu_cap,expected_parts)
out=dict(status='PASS' if not reasons else 'STOPPED',reasons=reasons,monitor_errors=errors,worker=receipt,recorded_memory=tracker.as_dict(),gpu_seconds_charged=duration,cpu_seconds_charged=totalcpu,cumulative_h2_gpu_seconds=prior_gpu+duration,no_retry=True)
save(root/'prediction-guard.json',out);return out
+62
View File
@@ -0,0 +1,62 @@
"""Prepare exact S2 review binding while original training continues; no launch."""
import json,shutil,time
from pathlib import Path
from phase2_fixed_io import BASE,verify as old_verify
from phase2_home_freeze_io import freeze,verify
from phase2_h1_worker_v2 import checked,sha,save
from phase2_h2_s2_common import config as training_config
from phase2_h2_s2_postfit_common import read,roster,config
def main():
root=Path('/home/frank14f/MeltMind-runs/phase2-h2-s2-postfit-2026-11-17')
if root.exists():raise FileExistsError('Existing S2 postfit; inspect, never duplicate')
train=Path('/home/frank14f/MeltMind-runs/phase2-h2-s2-2026-11-17');tc,pilot=training_config(train);tp=verify(train,'implementation')
if tp['commit']!='1f57bad66eb062d4f2269347532321929f873717' or sha(train/'contract.json')!='76e1976bed88fc390812238c3d80693446daea3840dc009ac7d8870ddc43a31d':raise ValueError('Original pinned training identity')
s1=Path(tc['s1_root']);sc=read(s1/'contract.json');s1pred=verify(s1,'predictions');s1terminal=verify(s1,'terminal')
aroot=BASE/'artifacts/phase2-b13-e1b-2026-11-17';a=read(aroot/'contract.json');old_verify(aroot,'predictions');old_verify(aroot,'terminal-result')
hroot=BASE/'artifacts/phase2-h1-execution-2026-11-17';h=read(hroot/'contract.json');jobs={j['fold']:j for j in h['jobs'] if j['protocol']=='grouped' and j['pool']==100}
rows=[];models=[]
for f in pilot['folds']:
af=a['folds'][f['fold']];hj=jobs[f['fold']]
if hj['baseline_model']!=af['A_baseline'] or set(hj['learning'])!=set(f['learning']) or set(hj['test'])!=set(f['test']):raise ValueError('H1/A/H2 original roles or baseline model differ')
if {r['case'] for r in af['rows']['learning']}!=set(f['learning']) or {r['case'] for r in af['rows']['test']}!=set(f['test']):raise ValueError('Original E1b learning/test roles differ')
by={r['case']:r for r in af['rows']['test']};rows.extend(by[k] for k in f['test']);models.append(hj['baseline_model'])
entries=read(aroot/'predictions-manifest.json')['entries'];baseline={e['case']:e for e in entries};fold0=read(s1/'predictions-manifest.json')['entries']
if len(entries)!=552 or len(baseline)!=552 or len(fold0)!=121 or {e['case'] for e in fold0}!=set(pilot['folds'][0]['test']):raise ValueError('Frozen prediction manifest coverage')
for e in entries:
if e['A_source_sha256']!=a['A_predictions'][e['case']]['sha256']:raise ValueError('Frozen A source identity')
for e in fold0:
if e['fold']!=0 or e['A_source_sha256']!=baseline[e['case']]['sha256']:raise ValueError('S1 original baseline pairing')
sources={}
def add(p,digest):
p=str((BASE/Path(p)).resolve())
if p in sources and sources[p]!=digest:raise ValueError('Conflicting input hash: '+p)
sources[p]=digest
for old in [tc,sc]:
for p,digest in old['source_sha256'].items():add(p,digest)
for p in [train/'contract.json',train/'implementation-git-freeze.json',Path(tc['pilot_contract']),s1/'contract.json',s1/'predictions-manifest.json',s1/'predictions-git-freeze.json',s1/'terminal-git-freeze.json',s1/'complete.json',s1/'post-terminal-cost.json',s1/'score/result.json',aroot/'contract.json',aroot/'predictions-manifest.json',aroot/'score/result.json',aroot/'predictions-git-freeze.json',aroot/'terminal-result-git-freeze.json',hroot/'contract.json']:add(p,sha(p))
for e in entries+fold0:add(e['path'],e['sha256'])
for m in models:add(m['path'],m['sha256'])
for p,digest in sources.items():checked(p,digest) # Opaque bytes only; no score/target value parsing.
codes={}
for old in [tc,sc,a]:
for p,digest in old['code_sha256'].items():
p=str((BASE/Path(p)).resolve())
if p in codes and codes[p]!=digest:raise ValueError('Conflicting source pin: '+p)
checked(p,digest);codes[p]=digest
for name in ['phase2_h2_s2_postfit_common.py','phase2_h2_s2_postfit.py','phase2_h2_s2_prediction_guard.py','prepare_phase2_h2_s2_postfit.py','test_phase2_h2_s2_postfit.py']:
p=BASE/'tools'/name;codes[str(p)]=sha(p)
doc=BASE/'docs/phase2-h2-s2-postfit-2026-11-17.md';codes[str(doc)]=sha(doc)
comparison=dict(approval='1794925470.101149',test_parts=552,test_components=204,fold0_prediction_and_metric_hash_reuse=True,new_prediction_parts=431,checkpoint_folds=[1,2,3,4],checkpoint_epoch=200,seed=0,A_models=models,clocks=[dict(fold=f['fold'],c=f['c'],path=f['clock_path'],sha256=f['clock_sha256']) for f in pilot['folds']],metrics=['relative_l2','last_location','kendall_tau_b'],query='Original eligible S2 queries; multiplicities once; raw-ID minimum tie rule; last distance/Rg',pooling='Original E1b pooled(): per-arm component requires all constituent part metrics defined; component means equally weighted; paired intersection',bootstrap=dict(replicates=2000,seed=2026111408),confirmation='CONFIRMED iff complete552/204 paired field H2-A upper95CI<0; strict finite bound; incomplete field support PARK',report_only=['H2-B13_E1b','last_location','kendall_tau_b'],negative_clipping=False,all_four_checkpoints_frozen_and_training_exited_before_prediction=True,actual_new_semantic_PASS_before_any_target=True,predictions_frozen_before_score=True,new_fits=0,new_decodes=0,no_retry=True,no_follow_on_launch=True,interpretation='Five-fold recipe confirmation after observing fold0; not an untouched-five-fold preregistration or connectivity causal claim')
log=Path('/tmp/h2-s2-postfit-synthetic-attempt1.log').read_text()
if 'Ran 23 tests' not in log or not log.rstrip().endswith('OK'):raise ValueError('Actual23 synthetic PASS required')
c=dict(version='H2-S2-pooled-postfit-v1',approval='1794925470.101149',postfit_root=str(root),training_root=str(train),training_implementation=tp['commit'],training_jobs=tc['jobs'],s1_root=str(s1),pilot_contract=tc['pilot_contract'],fold0_manifest=str(s1/'predictions-manifest.json'),fold0_score=str(s1/'score/result.json'),baseline_score=str(aroot/'score/result.json'),rows=rows,baseline_predictions=baseline,source_sha256=sources,code_sha256=codes,total_h2_gpu_seconds=144000,prediction_gpu_seconds=2400,postfit_cpu_seconds=3600,setup_cpu_seconds=360,prediction_per_fold_gpu_seconds=600,prediction_per_fold_worker_cpu_seconds=540,score_worker_cpu_seconds=600,terminal_cpu_reserve=120,coordinator_cpus=[12,13],vram_bytes=15*2**30,rss_bytes=16*2**30,disk_bytes=20*2**30,comparison_contract=str(root/'comparison-contract.json'),new_fits=0,new_decodes=0,no_retry=True,actual_new_semantic_PASS_required=True,all_four_terminal_before_prediction=True,no_auto_launch=True)
roster(c,pilot);root.mkdir();save(root/'comparison-contract.json',comparison);c['comparison_contract_sha256']=sha(root/'comparison-contract.json');save(root/'contract.json',c)
shutil.copyfile('/tmp/h2-s2-postfit-synthetic-attempt1.log',root/'synthetic-attempt1.log')
save(root/'acceptance.json',dict(status='PASS',tests=23,elapsed_seconds=1.477,log_sha256=sha(root/'synthetic-attempt1.log'),test_source_sha256=sha(BASE/'tools/test_phase2_h2_s2_postfit.py'),scope='CPU synthetic plus actual isolated Git/file review-validator tests; no real prediction, target read, fitting or CUDA workload',setup_cpu_seconds_reserved=360,unix=time.time()))
save(root/'preparation-audit.json',dict(status='PREPARED_NOT_LAUNCHED',unix=time.time(),training_implementation=tp['commit'],S1_prediction_commit=s1pred['commit'],S1_terminal_commit=s1terminal['commit'],test_parts=552,test_components=204,fold_test_counts=[len(f['test']) for f in pilot['folds']],original_learning_test_roles_and_A_models_equal=True,original_prediction_hashes_checked=673,new_fits=0,new_predictions=0,new_scores=0,old_score_values_read=False,test_target_values_read=False,training_untouched=True,gate_queue_untouched=True,future_checkpoint_adoption='Not present or required at preparation; all four original models/terminal freezes and process exit mandatory at execution',actual_new_review_pending=True))
freeze(root,'implementation',[root/'contract.json',root/'comparison-contract.json',root/'acceptance.json',root/'synthetic-attempt1.log',root/'preparation-audit.json',*[Path(p) for p in codes]])
proof=verify(root,'implementation');save(root/'expected-review-binding.json',dict(reviewed_commit=proof['commit'],contract_sha256=sha(root/'contract.json'),implementation_proof_sha256=sha(root/'implementation-git-freeze.json'),comparison_contract_sha256=c['comparison_contract_sha256'],code_sha256=codes))
freeze(root,'review-binding',[root/'contract.json',root/'expected-review-binding.json',root/'implementation-git-freeze.json',root/'implementation-authorization.json']);config(root)
print(json.dumps(dict(root=str(root),implementation=proof['commit'],contract_sha256=sha(root/'contract.json'),binding_sha256=sha(root/'expected-review-binding.json'),code_files=len(codes),review_pending=True,launched=False)))
if __name__=='__main__':main()
+101
View File
@@ -0,0 +1,101 @@
"""Synthetic pooled postfit boundaries; no real model prediction or target values."""
import ast,copy,inspect,json,tempfile,unittest
from pathlib import Path
from phase2_h2_s2_postfit_common import terminal_identity,terminal_inputs,checkpoint_identity,comparisons,decision,require_budget,reuse_fold0_metric
from phase2_h2_s2_postfit import predict,score,manifest_identity
from phase2_h2_s2_prediction_guard import final_reasons
from s2_main_memory_guard import PeakRecords
from test_phase2_gate_outcome_postfit import SemanticReview
def metric(v):return {k:dict(value=v) for k in ['relative_l2','last_location','kendall_tau_b']}
class TerminalAndBudget(unittest.TestCase):
def fixture(self):
tc=dict(prior_gpu_seconds=18858.21490871499,train_gpu_cap=122741.78509128501,fold0_reuse={'hash':'original'});entries=[dict(fold=f,status='PASS',returncode=0,gpu_seconds_charged=20000.) for f in range(1,5)]
done=dict(status='TRAIN_COMPLETE_POSTFIT_PENDING',postfit_started=False,terminal=entries,prior_gpu_once=tc['prior_gpu_seconds'],new_gpu_seconds_charged=80000.,cumulative_h2_gpu_seconds=tc['prior_gpu_seconds']+80000.)
ledger=dict(entries=entries,prior_gpu_once=tc['prior_gpu_seconds'],settled_gpu_seconds=80000.);models=dict(models=[dict(fold=f) for f in range(1,5)],fold0_reused_by_hash=tc['fold0_reuse'],new_fits=4,test_targets_read=False,postfit_started=False);return tc,done,ledger,models
def test_all_four_costs_and_prior_once(self):
a=self.fixture();self.assertEqual(terminal_identity(*a),98858.21490871499)
def test_partial_failed_or_wrong_role_models_rejected(self):
for kind in ['partial','failed','duplicate','predicted']:
t,d,l,m=self.fixture()
if kind=='partial':d['terminal']=d['terminal'][:3]
if kind=='failed':d['terminal'][0]['status']='FAILED'
if kind=='duplicate':m['models'][3]['fold']=1
if kind=='predicted':m['test_targets_read']=True
with self.assertRaises((ValueError,PermissionError)):terminal_identity(t,d,l,m)
def test_mismatched_or_double_counted_ledger_rejected(self):
for key in ['prior_gpu_once','new_gpu_seconds_charged','cumulative_h2_gpu_seconds']:
t,d,l,m=self.fixture();d[key]+=1
with self.assertRaises(ValueError):terminal_identity(t,d,l,m)
def test_still_training_without_model_freeze_cannot_enter(self):
with tempfile.TemporaryDirectory() as p:
with self.assertRaises(FileNotFoundError):terminal_inputs({'training_root':p},{},{})
def test_correct_new_fold_final_checkpoint_only(self):
c=dict(training_root='/train',source_sha256={'/train/contract.json':'training-fixed'});b=dict(completed_epochs=200,optimizer_steps=73200,seed=0,fold=2,role='last_epoch_only',contract_sha256='training-fixed');checkpoint_identity(b,c,2,366)
for k,v in dict(fold=0,completed_epochs=199,optimizer_steps=64000,contract_sha256='s1',role='best').items():
with self.assertRaises(ValueError):checkpoint_identity(dict(b,**{k:v}),c,2,366)
def test_budget_reserves_all_remaining_predictions_and_score(self):
require_budget(98858.,0,360,4);require_budget(98858.,300,800,1)
for args in [(142000,0,360,4),(98858,2200,500,1),(98858,0,1000,4),(98858,0,3000,0)]:
with self.assertRaises(RuntimeError):require_budget(*args)
def test_prediction_final_peaks_and_per_fold_coverage(self):
t=PeakRecords();t.witnesses={'peak_allocated_bytes':{},'peak_reserved_bytes':{}}
self.assertEqual(final_reasons([],{'status':'PASS'},dict(status='COMPLETE',parts=91),t,100,100,600,91),[])
self.assertIn('INCOMPLETE_FOLD',final_reasons([],{'status':'PASS'},dict(status='COMPLETE',parts=121),t,100,100,600,91))
t.maxima['peak_reserved_bytes']=16*2**30
self.assertIn('RECORDED_VRAM_LIMIT',final_reasons([],{'status':'FAILED'},None,t,100,100,600,91))
self.assertIn('FINAL_RESOURCE_LIMIT',final_reasons([],{'status':'PASS'},dict(status='COMPLETE',parts=91),t,601,100,600,91))
class StatisticsAndCustody(unittest.TestCase):
def test_component_equal_weights_and_b13_report_comparison(self):
rows=[dict(component_id=g,H2=metric(h),A=metric(a),B13_E1b=metric(b)) for g,h,a,b in [('x',2,1,0),('x',2,1,0),('y',1,1,0)]];d=comparisons(rows)
self.assertEqual(d['A']['relative_l2']['H2_minus_reference']['value'],.5);self.assertEqual(d['B13_E1b']['relative_l2']['H2_minus_reference']['value'],1.5)
for arm in d:
self.assertEqual(d[arm]['relative_l2']['H2_minus_reference']['replicates'],2000);self.assertEqual(d[arm]['relative_l2']['H2_minus_reference']['seed'],2026111408)
def test_original_whole_component_undefined_rule(self):
rows=[dict(component_id=g,H2=metric(h),A=metric(1),B13_E1b=metric(1)) for g,h in [('x',2),('x',None),('y',1)]];d=comparisons(rows);self.assertEqual(d['A']['relative_l2']['components'],{'H2':1,'A':2});self.assertEqual(d['A']['relative_l2']['H2_minus_reference']['components'],1)
def test_only_strict_complete_field_vs_A_confirms(self):
def result(high):return {'A':{'relative_l2':dict(components={'H2':204,'A':204},H2_minus_reference=dict(components=204,high=high))},'B13_E1b':{'relative_l2':{'arbitrarily_bad_report_only':999}}}
self.assertEqual(decision(result(-.001),552,204),'CONFIRMED')
for high in [0,.001,None,float('nan')]:self.assertEqual(decision(result(high),552,204),'NOT_CONFIRMED_FIXED_RECIPE')
self.assertEqual(decision(result(-.001),551,204),'PARK_INCOMPLETE_FIELD_SUPPORT')
r=result(-.001);r['A']['relative_l2']['components']['H2']=203;self.assertEqual(decision(r,552,204),'PARK_INCOMPLETE_FIELD_SUPPORT')
def test_fold0_metrics_are_reused_not_recomputed(self):
h=metric(.25);a=metric(.5);prior=dict(fold=0,component_id='x',new=h,A=a);entry=dict(fold=0,component_id='x');self.assertIs(reuse_fold0_metric(prior,{'A':a},entry),h)
with self.assertRaises(ValueError):reuse_fold0_metric(prior,{'A':metric(.6)},entry)
def test_fold0_prediction_forbidden_before_config_or_CUDA(self):
with self.assertRaises(PermissionError):predict(Path('/nonexistent'),0)
def test_semantic_pass_and_all_models_precede_value_reads(self):
s=inspect.getsource(score)
for token in ["c['baseline_score']","c['fold0_score']","cached(row,'seconds')"]:
self.assertLess(s.index('validate_review(root)'),s.index(token));self.assertLess(s.index('adopted_inputs('),s.index(token))
node=ast.parse(s);branch=next(n for n in ast.walk(node) if isinstance(n,ast.If) and ast.unparse(n.test)=="e['fold'] == 0")
self.assertNotIn('cached(',ast.unparse(branch.body));self.assertIn('reuse_fold0_metric',ast.unparse(branch.body));self.assertIn("cached(row, 'seconds')",ast.unparse(branch.orelse))
class ManifestReuse(unittest.TestCase):
def setUp(self):
self.tmp=tempfile.TemporaryDirectory();self.root=Path(self.tmp.name);self.rows=[];parts={};baseline={};folds=[dict(fold=f,test=[]) for f in range(5)];self.entries=[];old=[]
for i in range(552):
case='case'+str(i);g='g'+str(i%204);f=(i%204)%5;cache={'seconds.npy':'label-digest'};folds[f]['test'].append(case);parts[case]=dict(nodes=3,clock_group=g,output_sha256=cache,t_set=.4);baseline[case]=dict(fold=f,component_id=g,sha256='baseline-'+case)
self.rows.append(dict(case=case,component_id=g,nodes=3,assignments=[dict(fold=f,role='test')],v5_cache={'output_sha256':cache},preparation={'t_set':.4}))
e=dict(case=case,component_id=g,fold=f,nodes=3,query_nodes=2,path=str(self.root/'folds'/str(f)/'predictions'/(case+'.npz')),sha256='prediction-'+case,A_source_sha256='baseline-'+case,negative_full_predictions=1,negative_query_predictions=1)
if f==0:old.append(dict(e))
self.entries.append(dict(e,origin='S1_HASH_REUSE' if f==0 else 'S2_FROZEN_CHECKPOINT'))
p=self.root/'old.json';p.write_text(json.dumps(dict(entries=old)));self.c=dict(rows=self.rows,baseline_predictions=baseline,fold0_manifest=str(p),postfit_root=str(self.root));self.pilot=dict(folds=folds,parts=parts)
def tearDown(self):self.tmp.cleanup()
def test_complete_roster_and_exact_fold0_entries(self):self.assertEqual(len(manifest_identity(self.entries,self.c,self.pilot)),552)
def test_fold0_changed_hash_rejected(self):
d=copy.deepcopy(self.entries);next(e for e in d if e['fold']==0)['sha256']='different'
with self.assertRaises(ValueError):manifest_identity(d,self.c,self.pilot)
def test_duplicate_missing_and_wrong_new_fold_path_rejected(self):
for kind in ['duplicate','missing','path']:
d=copy.deepcopy(self.entries)
if kind=='duplicate':d[-1]=d[0]
if kind=='missing':d.pop()
if kind=='path':next(e for e in d if e['fold']==1)['path']='/other/pred.npz'
with self.assertRaises(ValueError):manifest_identity(d,self.c,self.pilot)
def test_negative_counts_not_silently_changed_or_boolean(self):
for n in [-1,4,True]:
d=copy.deepcopy(self.entries);next(e for e in d if e['fold']==1)['negative_full_predictions']=n
with self.assertRaises(ValueError):manifest_identity(d,self.c,self.pilot)
if __name__=='__main__':unittest.main()