75 lines
4.2 KiB
Python
75 lines
4.2 KiB
Python
"""S2 one-fold600 GPU-second prediction, retained failure peaks, no retries."""
|
|
import json,os,signal,sys,threading,time
|
|
import psutil
|
|
from phase2_h1_worker_v2 import save
|
|
from s2_main_memory_guard import PeakRecords
|
|
from s2_precision_continuation_guard import device_usage
|
|
from s2_recovery_gpu_supervisor import supervise,tree_usage,stop_group,disk_usage
|
|
|
|
def final_reasons(reasons,receipt,result,tracker,gpu_seconds,cpu_seconds,cpu_cap,expected_parts):
|
|
out=list(reasons)
|
|
def add(s):
|
|
if s not in out:out.append(s)
|
|
if not receipt or receipt['status']!='PASS':add('WORKER_NOT_PASS')
|
|
if not result or result.get('status')!='COMPLETE' or result.get('parts')!=expected_parts:add('INCOMPLETE_FOLD')
|
|
if not all(k in tracker.witnesses for k in ['peak_allocated_bytes','peak_reserved_bytes']):add('MISSING_PEAKS')
|
|
if any(v>15*2**30 for v in tracker.maxima.values()):add('RECORDED_VRAM_LIMIT')
|
|
if gpu_seconds>600 or cpu_seconds>cpu_cap:add('FINAL_RESOURCE_LIMIT')
|
|
return out
|
|
|
|
def run_prediction(queue,c,pilot,fold,prior_gpu,cpu_cap):
|
|
root=queue/'folds'/str(fold);gpu_uuid=c['training_jobs'][fold-1]['gpu_uuid'];expected_parts=len(pilot['folds'][fold]['test'])
|
|
start=time.monotonic();cpu=time.process_time();requested=[];reasons=[];errors=[];samples=[];done=threading.Event();tracker=PeakRecords();receipt=None
|
|
def elapsed():return 60+time.monotonic()-start
|
|
def reason(x):
|
|
if x not in reasons:reasons.append(x)
|
|
command=[sys.executable,'tools/phase2_h2_s2_postfit.py','predict',str(queue),str(fold)]
|
|
def kill():
|
|
for p in psutil.Process().children(recursive=True):
|
|
try:
|
|
if p.cmdline()==command:stop_group(p.pid)
|
|
except psutil.NoSuchProcess:pass
|
|
def scan():
|
|
if tracker.scan(root/'predictions'):reason('RECORDED_VRAM_LIMIT')
|
|
def watch():
|
|
while not done.is_set():
|
|
try:
|
|
scan();mem,util=device_usage(gpu_uuid);_,rss,_=tree_usage(os.getpid())
|
|
if mem>15*2**30:reason('DEVICE_VRAM_LIMIT')
|
|
if rss>16*2**30:reason('TREE_RSS_LIMIT')
|
|
if disk_usage(queue)>20*2**30:reason('DISK_LIMIT')
|
|
if elapsed()>=595:reason('PREDICTION_TIME_LIMIT')
|
|
for x in requested:reason(x)
|
|
save(root/'prediction-live.json',dict(gpu_seconds_charged=elapsed(),device_bytes=mem,device_utilization=util,rss_bytes=rss,peaks=tracker.maxima,reasons=reasons,unix=time.time()))
|
|
except Exception as exc:reason('MONITOR_ERROR');errors.append(str(exc))
|
|
if reasons:kill()
|
|
done.wait(.5)
|
|
old={s:signal.signal(s,lambda signum,frame:requested.append('COORDINATOR_SIGNAL_'+str(signum))) for s in [signal.SIGTERM,signal.SIGINT]};thread=None
|
|
try:
|
|
for i in range(3):
|
|
mem,util=device_usage(gpu_uuid);samples.append(dict(unix=time.time(),bytes=mem,utilization=util))
|
|
if mem>128*2**20 or util!=0:raise RuntimeError('SELECTED_GPU_BUSY_NO_RETRY')
|
|
if i<2:time.sleep(2)
|
|
save(root/'prediction-idle-polls.json',samples)
|
|
if requested:raise RuntimeError('Signal received during setup')
|
|
thread=threading.Thread(target=watch,daemon=True);thread.start()
|
|
remaining=600-elapsed()-10
|
|
if remaining<=0:raise RuntimeError('Setup exhausted prediction budget')
|
|
receipt=supervise(command,root/'prediction-guard','predict',dict(total_cpu_seconds=cpu_cap-60,job_cpu_seconds=cpu_cap-60,job_wall_seconds=remaining,tree_rss_bytes=16*2**30,disk_bytes=20*2**30,max_processes=1),poll=.2,gpu_uuid=gpu_uuid)
|
|
except BaseException as exc:reason(type(exc).__name__+': '+str(exc));kill()
|
|
finally:
|
|
done.set()
|
|
if thread:thread.join(timeout=4)
|
|
if thread and thread.is_alive():reason('MONITOR_DID_NOT_EXIT')
|
|
try:scan()
|
|
except Exception as exc:reason('FINAL_SCAN_ERROR');errors.append(str(exc))
|
|
for s,h in old.items():signal.signal(s,h)
|
|
result=None;p=root/'predictions/result.json'
|
|
try:
|
|
if p.exists():result=json.loads(p.read_text())
|
|
except Exception as exc:reason('FINAL_RESULT_READ_ERROR');errors.append(str(exc))
|
|
totalcpu=60+(receipt['cpu_seconds'] if receipt else 0)+time.process_time()-cpu
|
|
duration=elapsed();reasons=final_reasons(reasons,receipt,result,tracker,duration,totalcpu,cpu_cap,expected_parts)
|
|
out=dict(status='PASS' if not reasons else 'STOPPED',reasons=reasons,monitor_errors=errors,worker=receipt,recorded_memory=tracker.as_dict(),gpu_seconds_charged=duration,cpu_seconds_charged=totalcpu,cumulative_h2_gpu_seconds=prior_gpu+duration,no_retry=True)
|
|
save(root/'prediction-guard.json',out);return out
|