Implement approved R4a paired propagation with guarded budgets and synthetic acceptance
This commit is contained in:
1 parent
f9633bd219
commit
7c85d1a29a
12 files changed
+59816
No files matched your search
@@ -0,0 +1,120 @@
|
||||
"""One R4(a) label-free paired prediction; no feature/moment/model execution."""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import time
|
||||
import traceback
|
||||
import hashlib
|
||||
import numpy as np
|
||||
from gre24_prepare_v2 import read_geometry, sha
|
||||
from gre24_features_v2 import mesh, exterior_faces, gate_nodes, cell_flow_speed, solve_eikonal, strict_volume_cdf
|
||||
from s2_custody_common import prediction_inputs, checked
|
||||
from s2_recovery_supervisor import save
|
||||
from r4a_core import graph_field
|
||||
|
||||
|
||||
def contract(root):
|
||||
c = json.loads((Path(root)/'contract.json').read_text())
|
||||
for p, h in {**c['code_sha256'], **c['source_sha256']}.items():
|
||||
checked(p, h)
|
||||
a = c['archive_stat']; s = Path(c['archive']).stat()
|
||||
if (s.st_size, s.st_mtime_ns, s.st_ino) != (a['bytes'], a['mtime_ns'], a['inode']):
|
||||
raise ValueError('Frozen v5 archive identity changed')
|
||||
return c
|
||||
|
||||
|
||||
def fingerprint(a):
|
||||
a = np.ascontiguousarray(a)
|
||||
return dict(shape=list(a.shape), dtype=a.dtype.str, sha256=hashlib.sha256(a.tobytes()).hexdigest())
|
||||
|
||||
|
||||
def exact(a, b, label):
|
||||
if not np.array_equal(a, b):
|
||||
raise ValueError('Identity mismatch: '+label)
|
||||
|
||||
|
||||
def worker(root, index):
|
||||
c = contract(root); row = c['parts'][index]; case = row['case']
|
||||
out = root/'parts'/case/'output'; out.mkdir(exist_ok=False)
|
||||
cpu = time.process_time(); start = time.monotonic()
|
||||
def progress(stage, **kw):
|
||||
save(out/'progress.json', dict(stage=stage, case=case, pid=os.getpid(), unix=time.time(),
|
||||
cpu_seconds=time.process_time()-cpu, wall_seconds=time.monotonic()-start, **kw))
|
||||
try:
|
||||
progress('GEOMETRY_AND_CACHE_IDENTITY')
|
||||
x, t, raw, meta = read_geometry(c['archive'], case)
|
||||
g = mesh(x, t); faces = exterior_faces(t)
|
||||
sources, gate_mode = gate_nodes(x, raw, meta['inlet_config']['inlet_location'])
|
||||
prep = row['preparation']; source = Path(prep['preparation_root'])
|
||||
checked(source/'geometry.npz', prep['output_sha256']['geometry.npz'])
|
||||
with np.load(source/'geometry.npz', allow_pickle=False) as z:
|
||||
exact(z['raw_node_ids'], raw, 'S2 raw IDs')
|
||||
exact(z['points_raw'], x, 'S2 coordinates/units')
|
||||
exact(z['tetra'], t, 'S2 tet connectivity')
|
||||
exact(np.sort(z['boundary_triangles'], axis=1)[np.lexsort(np.sort(z['boundary_triangles'], axis=1).T[::-1])],
|
||||
faces[np.lexsort(faces.T[::-1])], 'wall triangles')
|
||||
exact(z['gate_location_raw'], meta['inlet_config']['inlet_location'], 'gate location')
|
||||
query = prediction_inputs(row)
|
||||
qids = np.searchsorted(raw, query['raw_ids'])
|
||||
if np.any(qids >= len(raw)): raise ValueError('Query outside retained tet domain')
|
||||
exact(raw[qids], query['raw_ids'], 'query original IDs')
|
||||
old = row.get('v5_cache')
|
||||
if old:
|
||||
cache = Path(old['output'])
|
||||
old_result = json.loads(checked(cache/'result.json', old['result_sha256']).read_text())
|
||||
if old_result['status'] != 'PASS' or old_result['case'] != case: raise ValueError('Cache result identity')
|
||||
if old_result['geometry'] != meta: raise ValueError('Cache geometry metadata')
|
||||
loaded = {}
|
||||
for name in ['Dw', 'Df', 'raw_ids', 'mass', 'all_wall_tet_ids']:
|
||||
loaded[name] = np.load(checked(cache/(name+'.npy'), old['output_sha256'][name+'.npy']), allow_pickle=False)
|
||||
exact(loaded['raw_ids'], raw, 'cached raw IDs')
|
||||
exact(loaded['mass'], g['mass'], 'cached volume measure')
|
||||
dw, df = loaded['Dw'], loaded['Df']
|
||||
if float(old_result['t_scale_seconds']) != float(query['t_set']): raise ValueError('Frozen time setting mismatch')
|
||||
else:
|
||||
if case not in c['distance_only_missing_parts']: raise ValueError('Unapproved cache fallback')
|
||||
progress('DISTANCE_ONLY_DW'); dw = solve_eikonal(g, np.unique(faces), 1.)
|
||||
speed, zero = cell_flow_speed(g, dw, faces)
|
||||
if old:
|
||||
f = old_result['features']
|
||||
exact(loaded['all_wall_tet_ids'], zero, 'all-wall correction list')
|
||||
exact(f['gate_raw_ids'], raw[sources], 'cached gate source')
|
||||
if f['wall_mode'] != 'tet_exterior_proxy' or f['precision'] != 'float64' or f['max_iterations'] != 100000:
|
||||
raise ValueError('Cached scientific settings differ')
|
||||
if f['speed_min'] != float(speed.min()) or f['speed_max'] != float(speed.max()):
|
||||
raise ValueError('Cached speed provenance mismatch')
|
||||
scale = 1./float(speed.min()) if float(speed.min())**2 <= 1e-4 else 1.
|
||||
if f['Df_coordinate_and_speed_scale'] != scale: raise ValueError('Cached scale mismatch')
|
||||
else:
|
||||
progress('DISTANCE_ONLY_DF'); df = solve_eikonal(g, sources, speed)
|
||||
if df.shape != (len(raw),) or not np.isfinite(df).all() or np.any(df < 0) or np.any(df[sources] != 0):
|
||||
raise ValueError('Invalid F field')
|
||||
progress('DIJKSTRA_SHARED_SPEED'); dg, graph_meta = graph_field(g, speed, sources)
|
||||
progress('FULL_TET_CDF_AND_QUERY_PREDICTIONS')
|
||||
cf = strict_volume_cdf(df, g['mass']); cg = strict_volume_cdf(dg, g['mass'])
|
||||
for name, array in dict(Dw=dw, Df=df, Dg=dg, a_K=speed, all_wall_tet_ids=zero,
|
||||
mass=g['mass'], raw_ids=raw, cdf_F=cf, cdf_G=cg).items():
|
||||
np.save(out/(name+'.npy'), array)
|
||||
np.savez(out/'query-predictions.npz', raw_ids=query['raw_ids'], counts=query['counts'],
|
||||
F=cf[qids]*query['t_set'], G=cg[qids]*query['t_set'])
|
||||
proof = dict(mesh_x=fingerprint(x), mesh_t=fingerprint(t), wall_faces=fingerprint(faces),
|
||||
gate=fingerprint(raw[sources]), dw=fingerprint(dw), a_K=fingerprint(speed),
|
||||
mass=fingerprint(g['mass']), query_raw_ids=fingerprint(query['raw_ids']),
|
||||
query_counts=fingerprint(query['counts']), t_set=float(query['t_set']))
|
||||
save(out/'result.json', dict(status='PASS', case=case, component_id=row['component_id'],
|
||||
nodes=len(raw), query_nodes=len(qids), volume=float(g['volume'].sum()), gate_mode=gate_mode,
|
||||
reused_frozen_df=bool(old), cache_identity_verified=bool(old), fingerprints=proof, graph=graph_meta,
|
||||
output_sha256={p.name:sha(p) for p in out.iterdir() if p.suffix in ('.npy', '.npz')},
|
||||
cpu_seconds=time.process_time()-cpu, wall_seconds=time.monotonic()-start,
|
||||
decoded_labels=[], models_fitted=0, spatial_moment_computed=False))
|
||||
progress('PASS')
|
||||
except Exception as e:
|
||||
save(out/'failure.json', dict(status='FAILED', case=case, type=type(e).__name__, message=str(e),
|
||||
traceback=traceback.format_exc(), automatic_retry=False))
|
||||
raise
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int)
|
||||
a=p.parse_args();worker(a.root,a.index)
|
||||
Reference in new issue
Block a user