|
| 1 | +"""Resume failed final serving with unchanged quality gates and retained evidence.""" |
| 2 | +import argparse |
| 3 | +import fcntl |
| 4 | +import json |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import time |
| 10 | + |
| 11 | +from .finish_memory_application import load, sha |
| 12 | +from .tree_node_quality import write |
| 13 | + |
| 14 | + |
| 15 | +def validate_ready(root): |
| 16 | + ready = load(root / 'quality-ready.json') |
| 17 | + if ready['status'] != 'quality-complete': raise ValueError('Complete original quality gate required') |
| 18 | + for name, checksum in ready['gates'].items(): |
| 19 | + path = root / name |
| 20 | + if sha(path) != checksum or load(path)['status'] != 'complete': |
| 21 | + raise ValueError('Original quality gate changed: ' + name) |
| 22 | + for family, checksum in ready['frozen_selections'].items(): |
| 23 | + if sha(root / family / 'selection.json') != checksum: raise ValueError('Frozen choice changed') |
| 24 | + return ready |
| 25 | + |
| 26 | + |
| 27 | +def run(args): |
| 28 | + root = args.root; validate_ready(root) |
| 29 | + old = load(root / 'final-serving-dispatch.json') |
| 30 | + if old['status'] != 'failed': raise ValueError('Only a failed, inactive final dispatch may be resumed') |
| 31 | + backup = root / 'recovery-20261110'; backup.mkdir(exist_ok=True); stamp = str(time.time_ns()) |
| 32 | + snapshots = {} |
| 33 | + for name in ('final-serving-dispatch.json', 'retime-llm-gpu/report.json', 'gpu-spare-lanes/report.json', |
| 34 | + 'publication-prepared/report.json'): |
| 35 | + path = root / name |
| 36 | + if path.exists(): |
| 37 | + target = backup / (name.replace('/', '-') + '-' + stamp + '.json') |
| 38 | + target.write_bytes(path.read_bytes()); snapshots[name] = {'path': str(target), 'sha256': sha(target)} |
| 39 | + commit = subprocess.check_output(['git', 'rev-parse', 'HEAD'], text=True).strip() |
| 40 | + write(root / 'gpu-recovery.json', {'source_commit': commit, 'source_sha256': sha(Path(__file__)), |
| 41 | + 'registered': time.time(), 'snapshots': snapshots, |
| 42 | + 'reason': 'Externally competing GPU programs interrupted measurements; passed outputs are retained and interrupted attempts are archived'}) |
| 43 | + report = {'status': 'resuming-final-serving', 'started': time.time(), 'source_commit': commit, |
| 44 | + 'complete': [v for v in old['complete'] if v['name'] not in ('retime-llm-gpu', 'retime-llm-cpu', 'retime-long')], |
| 45 | + 'failures': [], 'previous_failures': old.get('previous_failures', []) + old.get('failures', [])} |
| 46 | + write(root / 'final-serving-dispatch.json', report) |
| 47 | + env = dict(os.environ, CUDA_VISIBLE_DEVICES='', OMP_NUM_THREADS='1', OPENBLAS_NUM_THREADS='1', |
| 48 | + MKL_NUM_THREADS='1', CUBLAS_WORKSPACE_CONFIG=':4096:8', HF_HUB_OFFLINE='1', TRANSFORMERS_OFFLINE='1', TOKENIZERS_PARALLELISM='false') |
| 49 | + def phase(name, cmd): |
| 50 | + log = root / (name + '-resume-' + stamp + '.log') |
| 51 | + with log.open('w') as stream: result = subprocess.run(cmd, env=env, stdout=stream, stderr=subprocess.STDOUT) |
| 52 | + item = {'name': name, 'command': cmd, 'returncode': result.returncode, 'log': str(log), 'finished': time.time()} |
| 53 | + if result.returncode: |
| 54 | + report['failures'].append(item); raise RuntimeError('Recovery phase failed: ' + str(log)) |
| 55 | + report['complete'].append(item); write(root / 'final-serving-dispatch.json', report) |
| 56 | + common = ['--controls', str(root / 'independent-controls'), '--ready', str(root / 'quality-ready.json'), |
| 57 | + '--upstream', str(args.upstream), '--requests', '32'] |
| 58 | + spare = None |
| 59 | + try: |
| 60 | + with (root / ('spare-gpu-resume-' + stamp + '.log')).open('w') as stream: |
| 61 | + spare = subprocess.Popen([sys.executable, '-m', 'tools.gpu_spare_lanes', '--plan', str(root / 'gpu-spare-lane-plan.json')], |
| 62 | + env=env, stdin=subprocess.DEVNULL, stdout=stream, stderr=subprocess.STDOUT) |
| 63 | + phase('retime-llm-gpu', [sys.executable, '-m', 'tools.finalize_memory_application', 'retime-llm-gpu', |
| 64 | + '--frontier', str(root / 'llm-frontier'), '--output', str(root / 'retime-llm-gpu'), |
| 65 | + '--gpu-addendum', str(Path.cwd() / 'benchmarks/memory_application_serving_addendum_v1.json'), |
| 66 | + '--gpu-affinity', str(root / 'gpu-worker-affinity.json'), *common]) |
| 67 | + if spare.wait() != 0 or load(root / 'gpu-spare-lanes/report.json')['status'] != 'complete': |
| 68 | + raise RuntimeError('Complete spare cohort required before CPU timing') |
| 69 | + (root / 'all-compute-finished').write_text('All registered GPU measurements completed.\n') |
| 70 | + for name, family in (('retime-llm-cpu', 'llm-frontier'), ('retime-long', 'long-frontier')): |
| 71 | + report['status'] = 'running-' + name; write(root / 'final-serving-dispatch.json', report) |
| 72 | + phase(name, [sys.executable, '-m', 'tools.finalize_memory_application', name, '--frontier', str(root / family), |
| 73 | + '--output', str(root / name), *common]) |
| 74 | + report.update(status='complete', finished=time.time()); write(root / 'final-serving-dispatch.json', report) |
| 75 | + except BaseException as error: |
| 76 | + report.update(status='failed', error=repr(error), finished=time.time()); write(root / 'final-serving-dispatch.json', report) |
| 77 | + raise |
| 78 | + output = root / 'publication-prepared' |
| 79 | + if output.exists(): output.rename(root / ('publication-prepared-before-resume-' + stamp)) |
| 80 | + with (root / ('publication-resume-' + stamp + '.log')).open('w') as stream: |
| 81 | + subprocess.run([sys.executable, '-m', 'tools.finish_memory_application', '--root', str(root), |
| 82 | + '--sources', str(args.sources), '--output', str(output), '--path-map', str(root / 'publication-path-map.json'), |
| 83 | + '--release-url', args.release_url], cwd=args.publication_source, env=env, stdout=stream, stderr=subprocess.STDOUT, check=True) |
| 84 | + |
| 85 | + |
| 86 | +def main(): |
| 87 | + parser = argparse.ArgumentParser(description=__doc__) |
| 88 | + for name in ('root', 'sources', 'upstream', 'publication-source'): parser.add_argument('--' + name, type=Path, required=True) |
| 89 | + parser.add_argument('--release-url', required=True); args = parser.parse_args() |
| 90 | + with (args.root / 'final-serving-recovery.lock').open('w') as lock: |
| 91 | + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB); run(args) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': main() |
0 commit comments