|
| 1 | +"""Precompute the unchanged LLM CPU grid while local GPU serving continues. |
| 2 | +
|
| 3 | +Use the original six physical-core lanes only after the SGNS main grid drains. |
| 4 | +The original dispatcher later consumes these fresh outputs without repeating |
| 5 | +queries. Reference coordinators wait for this cohort, preventing core overlap. |
| 6 | +""" |
| 7 | +import argparse |
| 8 | +from concurrent.futures import ThreadPoolExecutor |
| 9 | +import json |
| 10 | +import os |
| 11 | +from pathlib import Path |
| 12 | +import socket |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | +import threading |
| 16 | +import time |
| 17 | + |
| 18 | +from .parallel_cpu_serving import check_sample, digest, load |
| 19 | + |
| 20 | + |
| 21 | +def validate(root): |
| 22 | + from .finalize_memory_application import llm_requests |
| 23 | + path = root / 'cpu-phase-overlap-plan.json' |
| 24 | + plan = load(path) |
| 25 | + original = root / 'cpu-serving-parallel-plan.json' |
| 26 | + hardware = load(original) |
| 27 | + if plan['measurement_host'] != socket.gethostname(): |
| 28 | + raise ValueError('Wrong CPU overlap host') |
| 29 | + if plan['original_cpu_plan_sha256'] != digest(original): |
| 30 | + raise ValueError('Original CPU affinity plan changed') |
| 31 | + requests = [r for r in llm_requests(root / 'llm-frontier', root / 'independent-controls') |
| 32 | + if r['mode'] == 'codec' and r['batch'] == 1] |
| 33 | + if requests != plan['requests']: |
| 34 | + raise ValueError('The complete original LLM CPU request grid changed') |
| 35 | + return plan, hardware, original |
| 36 | + |
| 37 | + |
| 38 | +def ready(root): |
| 39 | + path = root / 'retime-long/report.json' |
| 40 | + if not path.exists(): |
| 41 | + return False |
| 42 | + report = load(path) |
| 43 | + if report['status'] == 'failed': |
| 44 | + raise RuntimeError('SGNS main grid failed; overlap remains blocked') |
| 45 | + return bool(report['requests']) and len(report['complete']) == len(report['requests']) |
| 46 | + |
| 47 | + |
| 48 | +def run(root): |
| 49 | + from .tree_node_quality import environment, write |
| 50 | + plan, hardware, original = validate(root) |
| 51 | + folder = root / 'cpu-phase-overlap' |
| 52 | + folder.mkdir(exist_ok=True) |
| 53 | + target = folder / 'report.json' |
| 54 | + if target.exists(): |
| 55 | + raise ValueError('Preserve previous overlap controller before restarting') |
| 56 | + report = {'status': 'waiting-for-complete-sgns-main-grid', |
| 57 | + 'environment': environment(), 'plan_sha256': digest(root / 'cpu-phase-overlap-plan.json'), |
| 58 | + 'phase_source_sha256': digest(__file__), 'requests': plan['requests'], |
| 59 | + 'complete': [], 'failures': [], 'workers': 6} |
| 60 | + write(target, report) |
| 61 | + deadline = time.monotonic() + 24 * 3600 |
| 62 | + while not ready(root): |
| 63 | + if time.monotonic() >= deadline: |
| 64 | + raise TimeoutError('CPU main-grid wait expired; existing workers preserved') |
| 65 | + time.sleep(5) |
| 66 | + report.update(status='running', started=time.time()) |
| 67 | + write(target, report) |
| 68 | + phase = 'retime-llm-cpu' |
| 69 | + outputs = root / phase |
| 70 | + outputs.mkdir(exist_ok=True) |
| 71 | + lock = threading.Lock() |
| 72 | + failed = threading.Event() |
| 73 | + |
| 74 | + def lane_work(lane): |
| 75 | + cpus = hardware['lanes'][lane]['cpus'] |
| 76 | + for index, request in enumerate(plan['requests']): |
| 77 | + if failed.is_set(): |
| 78 | + return |
| 79 | + if hardware['model_lanes'][phase][request['model']] != lane: |
| 80 | + continue |
| 81 | + output = outputs / f'{index:05d}.json' |
| 82 | + try: |
| 83 | + if output.exists(): |
| 84 | + raise ValueError('Fresh overlap output required; another scheduler is active') |
| 85 | + command = ['taskset', '--cpu-list', ','.join(map(str, cpus)), sys.executable, |
| 86 | + '-m', 'tools.parallel_cpu_serving', 'worker', '--phase', phase, |
| 87 | + '--job', request['job'], '--output', str(output), '--plan', str(original), |
| 88 | + '--lane', lane, '--cohort', 'parallel-shared-host', '--cache', str(request['cache']), |
| 89 | + '--requests', '32', '--upstream', plan['upstream']] |
| 90 | + log = folder / f'{index:05d}.{time.time_ns()}.log' |
| 91 | + with log.open('w') as stream: |
| 92 | + result = subprocess.run(command, stdout=stream, stderr=subprocess.STDOUT, |
| 93 | + env=dict(os.environ, CUDA_VISIBLE_DEVICES='', OMP_NUM_THREADS='1', |
| 94 | + OPENBLAS_NUM_THREADS='1', MKL_NUM_THREADS='1')) |
| 95 | + if result.returncode: |
| 96 | + raise RuntimeError('LLM CPU overlap worker failed: ' + str(log)) |
| 97 | + check_sample(load(output), request, plan['original_cpu_plan_sha256'], lane, cpus, |
| 98 | + 'parallel-shared-host') |
| 99 | + with lock: |
| 100 | + report['complete'].append({'request': request, 'output': str(output), 'sha256': digest(output)}) |
| 101 | + write(target, report) |
| 102 | + print({'llm_cpu_complete': len(report['complete']), 'total': len(plan['requests'])}, flush=True) |
| 103 | + except BaseException as error: |
| 104 | + failed.set() |
| 105 | + with lock: |
| 106 | + report['failures'].append({'request': request, 'error': repr(error)}) |
| 107 | + raise |
| 108 | + |
| 109 | + try: |
| 110 | + lanes = sorted(set(hardware['model_lanes'][phase].values())) |
| 111 | + with ThreadPoolExecutor(max_workers=6) as pool: |
| 112 | + list(pool.map(lane_work, lanes)) |
| 113 | + if len(report['complete']) != len(plan['requests']): |
| 114 | + raise ValueError('Complete unchanged LLM CPU cohort required') |
| 115 | + report.update(status='complete', finished=time.time()) |
| 116 | + except BaseException as error: |
| 117 | + report.update(status='failed', error=repr(error)) |
| 118 | + raise |
| 119 | + finally: |
| 120 | + write(target, report) |
| 121 | + |
| 122 | + |
| 123 | +def main(): |
| 124 | + parser = argparse.ArgumentParser(description=__doc__) |
| 125 | + parser.add_argument('--root', type=Path, required=True) |
| 126 | + args = parser.parse_args() |
| 127 | + try: |
| 128 | + run(args.root) |
| 129 | + except BaseException as error: |
| 130 | + path = args.root / 'cpu-phase-overlap/report.json' |
| 131 | + if path.exists(): |
| 132 | + value = load(path) |
| 133 | + value.update(status='failed', error=repr(error)) |
| 134 | + from .tree_node_quality import write |
| 135 | + write(path, value) |
| 136 | + raise |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + main() |
0 commit comments