61 lines
3.3 KiB
Python
61 lines
3.3 KiB
Python
"""Postfit gates and existing-budget accounting for the approved S3-A continuation."""
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from freeze_g1_components import sha
|
|
from s2_custody_common import read_json,checked
|
|
from s3a_data import features,columns
|
|
from s3a_recovery_common import state
|
|
|
|
|
|
def validate_terminal(batch,frozen):
|
|
ids=[r['index'] for r in batch['jobs']]
|
|
if (batch['status']!='ALL_S3A_FITS_TERMINAL' or batch['stop_reason'] is not None
|
|
or not batch['accounting_complete'] or len(ids)!=390 or set(ids)!=set(range(390))):
|
|
raise ValueError('All390 canonical terminal jobs required')
|
|
if frozen['status']!='ALL_S3A_FITS_TERMINAL' or frozen['hpo_terminal_indices']!=list(range(360)):
|
|
raise ValueError('360 HPO terminal freeze required')
|
|
if len(frozen['final_fits'])!=30 or frozen['final_fits']!=batch['final_fits']:
|
|
raise ValueError('Final checkpoint freeze differs')
|
|
|
|
|
|
def budget(fit_seconds,already_postfit_seconds,limits):
|
|
if fit_seconds<0 or fit_seconds>limits['fitting_worker_cpu_seconds'] or already_postfit_seconds<0:
|
|
raise ValueError('Invalid completed CPU ledger')
|
|
remaining=min(limits['postfit_reserved_cpu_seconds']-already_postfit_seconds,
|
|
limits['worker_cpu_seconds']-fit_seconds-already_postfit_seconds)
|
|
if remaining<=0:raise ValueError('Postfit budget exhausted')
|
|
return remaining
|
|
|
|
|
|
def predict_seconds(model,d,arm):
|
|
x=features(d,arm)
|
|
if model.n_features_in_!=len(columns(arm)):raise ValueError('Model feature width differs')
|
|
values=np.asarray(model.predict(x),dtype=np.float64)*d['t_set']
|
|
if values.shape!=(len(d['raw_ids']),) or not np.isfinite(values).all():
|
|
raise ValueError('Invalid prediction shape/values')
|
|
return values # Finite negative predictions are retained, never clipped.
|
|
|
|
|
|
def gate(root):
|
|
root=Path(root);c,scope,old,passed=state(root)
|
|
batch=read_json(root/'batch-complete.json');frozen=read_json(root/'terminal-freeze.json')
|
|
validate_terminal(batch,frozen)
|
|
checked(frozen['batch_receipt'],frozen['batch_receipt_sha256'])
|
|
manifest=read_json(checked(root/'two-source-manifest.json',batch['two_source_manifest_sha256']))
|
|
if len(manifest['records'])!=390 or {r['index'] for r in manifest['records']}!=set(range(390)):
|
|
raise ValueError('Two-source canonical manifest incomplete')
|
|
if manifest['cumulative_worker_CPU_s']!=batch['worker_cpu_seconds']:
|
|
raise ValueError('Two-source cumulative accounting differs')
|
|
for entry in manifest['records']:
|
|
if entry['job']!=c['jobs'][entry['index']]:raise ValueError('Job specification mutation')
|
|
if entry['guard_path']:checked(entry['guard_path'],entry['guard_sha256'])
|
|
if entry['result_path']:checked(entry['result_path'],entry['result_sha256'])
|
|
code=read_json(root/'postfit-code-freeze.json')
|
|
if not code['git_commit'] or code['contract_sha256']!=sha(root/'contract.json'):
|
|
raise ValueError('Postfit implementation not Git fixed')
|
|
for p,h in code['source_sha256'].items():checked(p,h)
|
|
terminal_git=read_json(root/'terminal-git-freeze.json')
|
|
if not terminal_git['git_commit'] or terminal_git['terminal_freeze_sha256']!=sha(root/'terminal-freeze.json'):
|
|
raise ValueError('Terminal checkpoints must be Git fixed before predictions')
|
|
return c,scope,batch,frozen
|