98 lines
4.8 KiB
Python
98 lines
4.8 KiB
Python
"""Execution-only shared-slot binding layered on the approved four-split science."""
|
||
|
||
import fcntl
|
||
import math
|
||
import os
|
||
from pathlib import Path
|
||
|
||
from phase2_gate_outcome_review import validate_review
|
||
from phase2_h1_worker_v2 import checked, sha
|
||
from phase2_home_freeze_io import verify
|
||
from phase3_shared_capacity import GIB, GPUS, SHARED, alive
|
||
from track_a_postfit_rebind_common import ROOT as PREVIOUS
|
||
from track_a_postfit_rebind_common import SPLITS, adopted, config as reviewed_config, read
|
||
|
||
|
||
ROOT = PREVIOUS.with_name('track-a-postfit-four-split-shared-2026-11-19')
|
||
RULING = '1795024014.778499'
|
||
SCIENCE_REVIEW = 'a5fa4a6e040493ff187b084e07cba0d95e3cae8f0fc0fa0d571f4af9efd30fce'
|
||
SCIENCE_FREEZE = '958abd8c916614c5366b5f0216958d46e00b8504'
|
||
GUARD_CPU = 300
|
||
SETUP = 60
|
||
CLEANUP = 120
|
||
MARGIN = 1.25
|
||
EXECUTION_KEYS = {'version', 'shared_ruling', 'shared_budget_evidence', 'shared_cards',
|
||
'gpu_uuid', 'worker_cpus', 'idle_wait_seconds', 'postfit_gpu_cap',
|
||
'predictions', 'code_sha256', 'source_sha256'}
|
||
|
||
|
||
def fold_cap(proxy_epoch):
|
||
if not math.isfinite(proxy_epoch) or proxy_epoch <= 0:
|
||
raise ValueError('Positive shared timeframe proxy required')
|
||
return math.ceil(SETUP + CLEANUP + MARGIN * (proxy_epoch + GUARD_CPU))
|
||
|
||
|
||
def postfit_cap(predictions):
|
||
caps = [p['gpu_seconds'] for p in predictions]
|
||
return SETUP + CLEANUP + sum(caps) + max(caps)
|
||
|
||
|
||
def config(root):
|
||
if root.resolve() != ROOT:
|
||
raise PermissionError('Exact shared Track A root')
|
||
c = read(root/'contract.json')
|
||
verify(root, 'implementation')
|
||
original, tc, entry = reviewed_config(PREVIOUS)
|
||
validate_review(PREVIOUS)
|
||
if (sha(PREVIOUS/'review.json') != SCIENCE_REVIEW or
|
||
read(PREVIOUS/'implementation-review-git-freeze.json')['commit'] != SCIENCE_FREEZE or
|
||
any((PREVIOUS / name).exists() for name in ('started.json', 'launch-attempt.json', 'complete.json'))):
|
||
raise PermissionError('Original science review or superseded-entry identity')
|
||
if ({k:v for k,v in c.items() if k not in EXECUTION_KEYS}
|
||
!= {k:v for k,v in original.items() if k not in EXECUTION_KEYS}):
|
||
raise ValueError('Track A scoring scope changed')
|
||
if c['shared_ruling'] != RULING or c['shared_cards'] != GPUS[:3]:
|
||
raise PermissionError('Authorized GPU0–2 shared entry')
|
||
for path, digest in {**c['source_sha256'], **c['code_sha256']}.items():
|
||
checked(path, digest)
|
||
evidence = read(c['shared_budget_evidence'])
|
||
expected = [dict(id=row['split'], gpu_seconds=fold_cap(row['proxy_epoch_seconds']),
|
||
cpu_seconds=GUARD_CPU, rss_bytes=4*GIB, recorded_vram_bytes=4*GIB,
|
||
process_vram_bytes=4*GIB, disk_bytes=20*GIB)
|
||
for row in evidence['splits']]
|
||
if (tuple(x['id'] for x in expected) != SPLITS or c['predictions'] != expected
|
||
or c['postfit_gpu_cap'] != postfit_cap(expected) or c['postfit_gpu_cap'] > original['postfit_gpu_cap']
|
||
or c['training_evidence']['training_gpu_seconds_once'] + c['postfit_gpu_cap'] > c['gpu_cap']):
|
||
raise ValueError('Track A projected resource caps differ')
|
||
return c, tc, entry
|
||
|
||
|
||
def assignment(root, contract, fold, digest):
|
||
path = root/'jobs'/str(fold)/'assignment.json'
|
||
a = read(checked(path, digest))
|
||
if a['id'] != fold or a['contract_sha256'] != sha(root/'contract.json') or a['gpu_uuid'] not in GPUS[:3]:
|
||
raise PermissionError('Shared prediction assignment identity')
|
||
job = next(x for x in contract['predictions'] if x['id'] == fold)
|
||
return dict(job, **{k:a[k] for k in ('gpu_uuid', 'worker_cpus', 'token', 'queue_identity')})
|
||
|
||
|
||
def worker_lease(root, fold, job):
|
||
with (SHARED/'capacity.lock').open('r') as lock:
|
||
fcntl.flock(lock, fcntl.LOCK_SH)
|
||
lease = read(SHARED/'registry.json')['leases'][job['token']]
|
||
if (lease['state'] != 'GUARD_BOUND' or lease['root'] != str(root) or lease['fold'] != fold
|
||
or lease['owner']['pid'] != os.getppid() or not alive(lease['owner'])
|
||
or lease['gpu_uuid'] != job['gpu_uuid'] or lease['worker_cpus'] != job['worker_cpus']
|
||
or lease['contract_sha256'] != sha(root/'contract.json')):
|
||
raise PermissionError('Prediction worker must be the shared guard child')
|
||
|
||
|
||
def budget(c, gpu_elapsed, cpu_elapsed, pending):
|
||
if any(not math.isfinite(x) or x < 0 for x in (gpu_elapsed, cpu_elapsed)):
|
||
raise ValueError('Invalid execution ledger')
|
||
predicted = gpu_elapsed + sum(p['gpu_seconds'] for p in pending)
|
||
if predicted > c['postfit_gpu_cap'] or predicted + c['training_evidence']['training_gpu_seconds_once'] > c['gpu_cap']:
|
||
raise RuntimeError('Track A shared postfit total budget gate')
|
||
if cpu_elapsed + GUARD_CPU*len(pending) + 900 + CLEANUP > c['postfit_cpu_cap']:
|
||
raise RuntimeError('Track A original postfit CPU guard')
|