131 lines
7.4 KiB
Python
131 lines
7.4 KiB
Python
"""Freeze the reviewed-scope Track A shared-slot delta using only metadata."""
|
|
|
|
import ast
|
|
import fcntl
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from phase2_gate_outcome_review import validate_review
|
|
from phase2_h1_worker_v2 import checked, save, sha
|
|
from phase2_home_freeze_io import freeze, verify
|
|
from phase3_shared_capacity import GIB, GPUS
|
|
from track_a_h2_prepare import local_code_closure
|
|
from track_a_postfit_rebind_common import config as reviewed_config, selected_training, terminal
|
|
from track_a_postfit_shared_common import (ROOT, PREVIOUS, RULING, SCIENCE_REVIEW, SCIENCE_FREEZE,
|
|
GUARD_CPU, MARGIN, config, fold_cap, postfit_cap, read)
|
|
|
|
|
|
BASE = Path(__file__).resolve().parents[1]
|
|
AUDIT = BASE/'artifacts/track-a-postfit-shared-2026-11-19'
|
|
REFERENCE = BASE/'artifacts/phase3-item-b-shared-budget-2026-11-19/shared-cost-evidence.json'
|
|
C_ROOT = ROOT.with_name('phase3-item-c-2026-11-18')
|
|
B_ROOT = ROOT.with_name('phase3-item-b-shared-2026-11-19')
|
|
|
|
|
|
def counts(contract, cases):
|
|
return dict(parts=len(cases), nodes=sum(contract['parts'][case]['nodes'] for case in cases),
|
|
edges=sum(contract['geometry'][case]['directed_edges'] for case in cases))
|
|
|
|
|
|
def workload_projections(tc):
|
|
reference = read(REFERENCE)
|
|
c = read(C_ROOT/'contract.json')
|
|
if sha(C_ROOT/'contract.json') != reference['c_contract_sha256']:
|
|
raise ValueError('Original c timing configuration changed')
|
|
pilot = read(checked(c['pilot_contract'], c['source_sha256'][c['pilot_contract']]))
|
|
measured = []
|
|
for row in reference['folds']:
|
|
index = row['fold']
|
|
witness = row['worst_epoch']
|
|
original = read(checked(witness['file'], witness['sha256']))
|
|
if original['train_epoch_seconds'] != row['max_train_epoch_seconds'] or original['fold'] != index:
|
|
raise ValueError('Same-card epoch witness changed')
|
|
support = counts(pilot, c['jobs'][index]['learning'])
|
|
measured.append(dict(fold=index, shared_epoch_seconds=row['max_train_epoch_seconds'],
|
|
learning_support=support, witness=witness, samples=len(row['candidates'])))
|
|
folds = []
|
|
for job in selected_training(tc)['jobs']:
|
|
n = counts(tc, job['test'])
|
|
proxy = max(x['shared_epoch_seconds'] * max(1., *(n[k]/x['learning_support'][k] for k in n))
|
|
for x in measured)
|
|
folds.append(dict(split=job['id'], evaluation_support=n, proxy_epoch_seconds=proxy,
|
|
cap_gpu_seconds=fold_cap(proxy)))
|
|
return measured, folds
|
|
|
|
|
|
def main():
|
|
start = time.process_time()
|
|
if ROOT.exists():
|
|
raise FileExistsError('Already prepared shared Track A entry')
|
|
prior, tc, _ = reviewed_config(PREVIOUS)
|
|
validate_review(PREVIOUS)
|
|
if (sha(PREVIOUS/'review.json') != SCIENCE_REVIEW or
|
|
verify(PREVIOUS, 'implementation-review')['commit'] != SCIENCE_FREEZE or
|
|
terminal(tc) != prior['training_evidence']):
|
|
raise PermissionError('Scientific pre-score review lineage changed')
|
|
measured, folds = workload_projections(tc)
|
|
jobs = [dict(id=x['split'], gpu_seconds=x['cap_gpu_seconds'], cpu_seconds=GUARD_CPU,
|
|
rss_bytes=4*GIB, recorded_vram_bytes=4*GIB, process_vram_bytes=4*GIB,
|
|
disk_bytes=20*GIB) for x in folds]
|
|
cap = postfit_cap(jobs)
|
|
training = prior['training_evidence']['training_gpu_seconds_once']
|
|
if cap > prior['postfit_gpu_cap'] or training + cap > prior['gpu_cap']:
|
|
raise ValueError('Shared planning exceeds the reviewed Track A allowance')
|
|
old_worker = ast.parse((BASE/'tools/track_a_postfit_rebind_worker.py').read_text())
|
|
new_worker = ast.parse((BASE/'tools/track_a_postfit_shared_worker.py').read_text())
|
|
old_score = next(x for x in old_worker.body if isinstance(x, ast.FunctionDef) and x.name=='score')
|
|
new_score = next(x for x in new_worker.body if isinstance(x, ast.FunctionDef) and x.name=='score')
|
|
if ast.dump(old_score) != ast.dump(new_score):
|
|
raise ValueError('Scoring logic differs')
|
|
code = {**prior['code_sha256'], **local_code_closure([Path(__file__),
|
|
*sorted((BASE/'tools').glob('track_a_postfit_shared*.py')),
|
|
BASE/'tools/test_track_a_postfit_shared.py'])}
|
|
live_pins = 0
|
|
for root in (C_ROOT, B_ROOT, ROOT.with_name('phase3-seed1-v2-2026-11-18')):
|
|
for p,h in read(root/'contract.json')['code_sha256'].items():
|
|
checked(p,h)
|
|
live_pins += 1
|
|
context = [PREVIOUS/n for n in ('contract.json', 'review.json', 'expected-review-binding.json',
|
|
'implementation-review-git-freeze.json', 'implementation-review-authorization.json')]
|
|
c_contract = read(C_ROOT/'contract.json')
|
|
context.extend([REFERENCE, C_ROOT/'contract.json', Path(c_contract['pilot_contract'])])
|
|
context.extend(Path(m['witness']['file']) for m in measured)
|
|
with Path('/tmp/meltmind-single-writer.lock').open('a') as lock:
|
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
evidence = AUDIT/'shared-cost-evidence.json'
|
|
if evidence.exists():
|
|
raise FileExistsError('Prior shared Track A cost evidence exists')
|
|
save(evidence, dict(ruling=RULING, measured_shared_reference=measured, splits=folds, margin=MARGIN,
|
|
method='Conservative full c training-epoch proxy scaled by the maximum of part, node and edge ratios',
|
|
proxy_limit='Prediction and compression not timed on c; original 300 CPU-s per-fold allowance added',
|
|
wait_reserve=max(x['gpu_seconds'] for x in jobs), postfit_gpu_cap=cap,
|
|
Track_A_total_gpu_seconds_with_planned_postfit=training+cap,
|
|
one_slot=True, case_data_decoded=False, score_ast_identical=True, live_other_pins_verified=live_pins))
|
|
sources = {**prior['source_sha256'], **{str(p):sha(p) for p in context+[evidence]}}
|
|
contract = dict(prior, version='track-a-four-split-shared-postfit', shared_ruling=RULING,
|
|
shared_budget_evidence=str(evidence), shared_cards=GPUS[:3], predictions=jobs,
|
|
postfit_gpu_cap=cap, code_sha256=code, source_sha256=sources)
|
|
for key in ('gpu_uuid', 'worker_cpus', 'idle_wait_seconds'):
|
|
del contract[key]
|
|
ROOT.mkdir()
|
|
save(ROOT/'contract.json',contract)
|
|
save(ROOT/'preparation-receipt.json',dict(status='PENDING_EXACT_DISPATCH_DELTA_REVIEW',
|
|
science_review_sha256=SCIENCE_REVIEW, science_review_freeze=SCIENCE_FREEZE,
|
|
shared_gpu_cap=cap, split_caps=[x['gpu_seconds'] for x in jobs],
|
|
prior_gpu_seconds=training, code_pins=len(code), source_pins=len(sources),
|
|
metadata_cpu_seconds=time.process_time()-start, launched=False, test_targets_read=False))
|
|
freeze(ROOT,'implementation',[ROOT/'contract.json',ROOT/'preparation-receipt.json',
|
|
*(Path(p) for p in code),evidence,AUDIT/'synthetic-tests.log',
|
|
BASE/'docs/track-a-shared-postfit-2026-11-19.md'])
|
|
final=verify(ROOT,'implementation')
|
|
config(ROOT)
|
|
save(ROOT/'expected-review-binding.json',dict(reviewed_commit=final['commit'],
|
|
contract_sha256=sha(ROOT/'contract.json'),implementation_proof_sha256=sha(ROOT/'implementation-git-freeze.json'),
|
|
comparison_contract_sha256=contract['comparison_contract_sha256'],code_sha256=code))
|
|
print(dict(implementation=final['commit'],contract=sha(ROOT/'contract.json'),
|
|
binding=sha(ROOT/'expected-review-binding.json'),gpu_cap=cap,code_pins=len(code)))
|
|
|
|
|
|
if __name__=='__main__':
|
|
main()
|