56 lines
4.2 KiB
Python
56 lines
4.2 KiB
Python
"""S2 identity/roles and cumulative GPU arithmetic; no fitting or target I/O."""
|
|
import json,math
|
|
from pathlib import Path
|
|
from phase2_fixed_io import BASE
|
|
from phase2_h1_worker_v2 import checked,sha
|
|
from phase2_home_freeze_io import verify
|
|
|
|
def read(p):return json.loads(Path(p).read_text())
|
|
def execution_command(args):
|
|
return any(Path(x).name in {'phase2_h2_s2_queue.py','phase2_h2_s2_guard.py','phase2_h2_s2_worker.py','phase2_h2_s1_guard.py','phase2_h2_s1_worker.py','phase2_h2_s1_postfit.py'} for x in args)
|
|
def fold_view(pilot,fold):
|
|
if fold not in (1,2,3,4):raise PermissionError('Only new folds1..4; fold0 is immutable reuse')
|
|
f=pilot['folds'][fold]
|
|
if f['fold']!=fold:raise ValueError('Original fold identity')
|
|
roles=[set(f[k]) for k in ('learning','validation','test')]
|
|
if any(roles[i]&roles[j] for i in range(3) for j in range(i)):raise ValueError('Role overlap')
|
|
return dict(pilot,folds=[f]) # Original preprocessing reads slot0; embedded fold identity remains actual fold.
|
|
def config(root):
|
|
c=read(root/'contract.json');verify(root,'implementation')
|
|
if c['approval']!='1794923791.467409' or c['total_h2_gpu_seconds']!=144000 or c['epochs']!=200 or c['seed']!=0 or [j['fold'] for j in c['jobs']]!=[1,2,3,4]:raise PermissionError('Fixed S2 scope')
|
|
if c['prior_gpu_seconds']!=18858.21490871499 or c['prediction_gpu_reserve']!=2400 or c['postfit_cpu_cap']!=3600 or c['new_fits']!=4:raise ValueError('Fixed prior ledger and bounded postfit reserve')
|
|
if len({j['gpu_uuid'] for j in c['jobs']})!=4:raise ValueError('Four distinct GPUs')
|
|
if c['train_gpu_cap']!=144000-c['prior_gpu_seconds']-c['prediction_gpu_reserve']:raise ValueError('GPU ledger identity')
|
|
for p,h in {**c['source_sha256'],**c['code_sha256']}.items():checked(p,h)
|
|
pilot=read(c['pilot_contract']);prior=read(Path(c['s1_root'])/'post-terminal-cost.json')
|
|
import torch,numpy as np
|
|
if torch.__version__!=pilot['versions']['torch'] or np.__version__!=pilot['versions']['numpy']:raise ValueError('Original scientific library versions required')
|
|
if prior['cumulative_h2_gpu_seconds']!=c['prior_gpu_seconds'] or not prior['within_cap']:raise ValueError('Original settled S1 cost')
|
|
for j in c['jobs']:
|
|
v=fold_view(pilot,j['fold']);f=v['folds'][0]
|
|
if j['learning_parts']!=len(f['learning']) or j['learning_nodes']!=f['learning_nodes'] or j['validation_nodes']!=f['validation_nodes']:raise ValueError('Original fold roster changed')
|
|
return c,pilot
|
|
|
|
def projection(c,observations):
|
|
if set(observations)!={1,2,3,4}:raise ValueError('All four first epochs required')
|
|
rows=[]
|
|
for j in c['jobs']:
|
|
v=observations[j['fold']];epoch=v['epoch'];charged=v['charged_gpu_seconds'];train=v['max_train_epoch_seconds'];validation=max(v.get('max_validation_seconds',0),j['validation_seconds_bound'])
|
|
if not isinstance(epoch,int) or not 1<=epoch<=200 or not all(math.isfinite(x) and x>=0 for x in [charged,train,validation]) or train<=0:raise ValueError('Invalid timing evidence')
|
|
future=1.25*((200-epoch)*train+(20-epoch//10)*validation)+60
|
|
estimate=charged+future;rows.append(dict(fold=j['fold'],completed_epochs=epoch,charged_gpu_seconds=charged,max_train_epoch_seconds=train,validation_seconds_bound=validation,projected_new_gpu_seconds=estimate,job_cap=j['gpu_seconds'],within_job_cap=estimate<=j['gpu_seconds']))
|
|
total=c['prior_gpu_seconds']+sum(r['projected_new_gpu_seconds'] for r in rows)+c['prediction_gpu_reserve']
|
|
passed=total<=c['total_h2_gpu_seconds'] and all(r['within_job_cap'] for r in rows)
|
|
return dict(status='PASS' if passed else 'STOP_BUDGET_PROJECTION',rows=rows,prior_gpu_once=c['prior_gpu_seconds'],prediction_gpu_reserve=c['prediction_gpu_reserve'],projected_total_gpu_seconds=total,total_cap=c['total_h2_gpu_seconds'],safety_factor=1.25,no_retry=True)
|
|
|
|
def first_epoch_release(root,contract_sha):
|
|
if (root/'global-stop.json').exists():raise RuntimeError('Global S2 stop before next epoch')
|
|
p=root/'first-epoch-projection.json'
|
|
if not p.exists():return False
|
|
d=read(p)
|
|
if d.get('contract_sha256')!=contract_sha or d.get('status')!='PASS':raise RuntimeError('First-epoch budget gate rejected')
|
|
return True
|
|
|
|
def confirmed(high,parts,components):
|
|
return bool(parts==552 and components==204 and high is not None and math.isfinite(high) and high<0)
|