Files
MeltMind/tools/phase2_h1_recovery_worker.py

58 lines
3.6 KiB
Python

"""Labelled inference-only recovery; original prediction arithmetic unchanged."""
import argparse
import json
from pathlib import Path
import time
import numpy as np
from threadpoolctl import threadpool_limits
from phase2_h1_worker_v2 import contract,checked,sha,save,role_rows,load_features,decode_seconds
from phase2_h1_recovery_gate import verified_gate
def predict(root, c, job):
import joblib
import xgboost
verified_gate(root,'models')
terminal = json.loads((root/'batch-complete.json').read_text())
if terminal['global_stop'] or not terminal['accounting_complete'] or terminal['attempted'] != len(c['jobs']):
raise PermissionError('Fit queue not complete')
freeze = json.loads((root/'models-terminal.json').read_text())
if len(freeze['models']) != len(c['jobs']): raise PermissionError('Incomplete model manifest')
entry = next(r for r in freeze['models'] if r['index'] == job['index'])
if entry['status'] != 'PASS': raise ValueError('No checkpoint for failed cell')
out = root/'predictions'/f"job-{job['index']:02d}"; out.mkdir(parents=True, exist_ok=False)
model = xgboost.XGBRegressor(); model.load_model(checked(entry['checkpoint'], entry['checkpoint_sha256']))
old = job['baseline_model']; checked(old['path'], old['sha256'])
if old['format'] == 'joblib':
baseline = joblib.load(old['path'])
elif old['format'] == 'ubj':
baseline = xgboost.XGBRegressor(); baseline.load_model(old['path'])
else:
raise ValueError('Unsupported frozen A checkpoint format')
clock = json.loads(checked(entry['clock_path'], entry['clock_sha256']).read_text())['c']
index = []; cpu = time.process_time()
with threadpool_limits(job['threads']):
for row in role_rows(c, job, 'test'):
x, f, ids = load_features(row) # No target.npy load on this path.
seconds = decode_seconds(model.predict(x), f, clock, row['t_set'])
old_seconds = np.asarray(baseline.predict(x), dtype=np.float64)*row['t_set']
if old_seconds.shape != seconds.shape or not np.isfinite(old_seconds).all(): raise ValueError('Invalid A prediction')
if job['protocol']=='grouped':
prior=c['baseline_predictions'][str(job['index'])][row['case']]
with np.load(checked(prior['path'],prior['sha256']),allow_pickle=False) as z:
iq=np.searchsorted(ids,z['raw_ids'])
if np.any(iq>=len(ids)) or not np.array_equal(ids[iq],z['raw_ids']):raise ValueError('Historical A query identity differs')
if not np.array_equal(old_seconds[iq],z[prior['array']]):raise ValueError('Historical A query prediction differs')
path = out/(row['case']+'.npz')
np.savez_compressed(path, seconds=seconds, A_seconds=old_seconds, cF_seconds=clock*f*row['t_set'],
EIK_seconds=f*row['t_set'], raw_ids=ids)
index.append({'case': row['case'], 'path': str(path), 'sha256': sha(path),
'negative_predictions': int(np.sum(seconds < 0)), 'nodes': len(ids)})
save(out/'progress.json', {'parts': len(index), 'cpu_seconds': time.process_time()-cpu})
save(out/'index.json', index)
save(out/'result.json', {'status': 'PREDICTIONS_COMPLETE_NO_SCORES', 'index_sha256': sha(out/'index.json'),
'cpu_seconds': time.process_time()-cpu, 'target_values_read': False})
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int);a=p.parse_args()
c=contract(a.root);predict(a.root,c,c['jobs'][a.index])