Implement bounded S2 preparation and sealed pilot execution

This commit is contained in:
Frank14f committed 2026-11-15 02:37:29 +08:00
1 parent fb827178ec
commit f8a6ed3f81
35 files changed
+2626

No files matched your search

+88
View File
@@ -0,0 +1,88 @@
"""Freeze fold-0 non-test preparation after completed ten-part measurement."""
import json
from pathlib import Path
import shutil
from freeze_g1_components import sha
ROOT=Path('artifacts/s2-pilot-inputs-2026-11-15')
TEN=Path('artifacts/s2-ten-part-measurement-2026-11-15')
def main():
measured=json.loads((TEN/'public/measurement-result.json').read_text())
ten_contract=json.loads((TEN/'contract.json').read_text())
assert measured['attempted']==10 and not measured['not_run'],'Finish fixed ten-part evidence first'
assert not ROOT.exists(),'Do not overwrite prior preparation'
parts=json.loads(Path('artifacts/s2-g1-components-2026-11-15/custodian/part-splits.json').read_text())
role_lists={r:sorted(p['case'] for p in parts if p['assignments'][0]['role']==r) for r in ['learning','validation','test']}
assert [len(role_lists[k]) for k in ['learning','validation','test']]==[387,112,125]
manifest=dict(fold=0,seed=0,pool=100,learning_parts=role_lists['learning'],validation_parts=role_lists['validation'],sealed_test_parts=role_lists['test'],
split_sha256=sha('artifacts/s2-g1-components-2026-11-15/public/splits.json'),ruling='1794680129.303719')
by_case={p['case']:p for p in parts}
plan=json.loads(Path('artifacts/s2-g1-candidates-2026-11-14/custodian/plan.json').read_text())
g1=json.loads(Path('artifacts/s2-g1-registration-2026-11-14/custodian/plan.json').read_text())['geometry_cache']
settings_path=Path('artifacts/s2-e1-2026-11-14/log-flow-setting-check/fill-time-setting-context.json')
settings={r['member'].removesuffix('.log'):r for r in json.loads(settings_path.read_text())}
geometry_counts={r['case']:r for r in __import__('csv').DictReader(open('artifacts/s2-e1-2026-11-14/preflight/geometry_counts.csv'))}
passed={r['case']:r for r in measured['rows'] if r['status']=='PASS' and r.get('preparation_status')=='COMPLETE'}
prior_failed={r['case'] for r in measured['rows'] if r['case'] not in passed}
candidates=sorted(role_lists['learning']+role_lists['validation']);cases=[]
for case in candidates:
p=by_case[case];meta=geometry_counts[case];setting=settings[case]
# Census field spelling is checked below, rather than guessed at execution.
tets=int(meta['tet_cells'])
spec=dict(case=case,numeric_prefix=int(case.split('_')[0]),source_key=p['source_key'],component_id=p['component_id'],
tet_nodes=p['nodes'],tet_cells=tets,t_set=setting['setting']['seconds'],t_set_provenance=setting,
g1_geometry=g1[case]['path'],input_sha256={g1[case]['path']:g1[case]['sha256'],str(settings_path):sha(settings_path)})
if case in prior_failed:
spec['prior_failed_preparation']=str(TEN/'parts'/case)
elif case in passed:
source=TEN/'parts'/case;spec.update(reuse_preparation=str(source),reuse_result_sha256=sha(source/'result.json'))
else:
count_root=Path('artifacts/s1-panel-precheck-2026-11-14')/case
if (count_root/'result.json').exists():
result=json.loads((count_root/'result.json').read_text());assert result['status']=='COUNT_COMPLETE'
spec.update(legacy_geometry=str(count_root/'geometry.npz'),existing_distance=str(count_root/'boundary_distance_raw.npy'))
for name in ['geometry.npz','boundary_distance_raw.npy']:spec['input_sha256'][str(count_root/name)]=result['output_sha256'][name]
primary=Path('artifacts/s1-sampled-draw-2026-11-14')/case/'draw.npz'
if primary.exists():
dr=json.loads((primary.parent/'receipt.json').read_text());assert dr['status']=='DRAW_COMPLETE'
spec['existing_primary_draw']=str(primary);spec['input_sha256'][str(primary)]=dr['output_sha256']['draw.npz']
o2=Path('artifacts/s1-sampled-o2-2026-11-14/panel')/(case+'_original')
if (o2/'result.json').exists():
old=json.loads((o2/'result.json').read_text());assert old['status']=='COMPLETE'
spec.update(existing_sampled_o2_nodes=str(o2/'nodes.npz'),existing_sampled_o2_result=str(o2/'result.json'))
spec['input_sha256'][str(o2/'nodes.npz')]=old['output_sha256']['nodes.npz'];spec['input_sha256'][str(o2/'result.json')]=sha(o2/'result.json')
cases.append(spec)
assert len(cases)==499 and not ({s['case'] for s in cases}&set(role_lists['test']))
new=[r for r in measured['rows'] if r['case'] in passed and not r['case'].startswith('00000666')]
assert new,'Need measured newly prepared cases'
rate=sum(r['cpu_seconds'] for r in new)/sum(r['nodes'] for r in new)
bpn=max(r['output_disk_bytes']/r['nodes'] for r in measured['rows'] if r['case'] in passed)
# Conservative planning charges the full measured preparation rate even to
# reusable parts, and reserves 25% for label extraction / corpus differences.
projected_cpu=rate*sum(s['tet_nodes'] for s in cases)*1.25/3600
projected_gib=bpn*sum(s['tet_nodes'] for s in cases)*1.25/2**30
ROOT.mkdir();(ROOT/'public').mkdir()
(ROOT/'role-manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
added=['tools/s2_preprocess_pilot.py','tools/s2_pilot_prepare.py','tools/s2_pilot_data.py','tools/run_s2_pilot_preparation.py','tools/prepare_s2_pilot_inputs.py','tools/test_s2_pilot_data.py','tools/test_s2_pilot_preparation.py']
paths=list(ten_contract['code_sha256'])+added
c=dict(ten_contract,cases=cases,version='S2-pilot-non-test-preparation-v1',code_sha256={p:sha(p) for p in paths},
authorizing_rulings=['1794679459.056999','1794680129.303719'],role_manifest_sha256=sha(ROOT/'role-manifest.json'),
ten_measurement_sha256=sha(TEN/'public/measurement-result.json'),initial_cpu_seconds_per_node=rate,
initial_projection_cpu_hours=projected_cpu,initial_projection_gib=projected_gib,
projection_method='Pooled CPU/node from successful nine new measurement cases excluding cached 666, times all 499 roster nodes, plus 25% reserve; max measured output bytes/node plus 25%',
limits=dict(worker_cpu_hours=60,workers=16,disk_bytes=100*2**30,per_part_wall_seconds=1800,per_part_tree_rss_bytes=1342177280,retries=0,replacements=0),
input_policy='Only fixed fold0 learning/validation labels may be decoded; sealed tests rejected before IO. Fill_time only. No fitting or GPU.',
failure_policy='Retain/drop failed pilot parts with reason; no replacements or retries; original component identity preserved; not a main-matrix completeness waiver',
checkpoint='Exactly first 50 terminal jobs before launching job 50; separate reused/new observed per-node CPU rates, extrapolate disk; stop/report if over total cap')
(ROOT/'contract.json').write_text(json.dumps(c,indent=2)+'\n')
report=dict(status='READY_WITHIN_PLANNING_CAPS' if projected_cpu<=60 and projected_gib<=100 else 'PROJECTION_OVER_CAP_REPORT_BEFORE_LAUNCH',
parts=499,learning=387,validation=112,sealed_test=125,fold=0,seed=0,projected_worker_cpu_hours=projected_cpu,projected_gib=projected_gib,
cpu_seconds_per_node=rate,full_preparations_reused=sum(bool(s.get('reuse_preparation')) for s in cases),
S1_sampled_O2_reused=sum(bool(s.get('existing_sampled_o2_nodes')) for s in cases),contract_sha256=sha(ROOT/'contract.json'),
role_manifest_sha256=sha(ROOT/'role-manifest.json'),labels_read=False,workers_started=False)
(ROOT/'public/prelaunch-projection.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report))
if __name__=='__main__':main()
+72
View File
@@ -0,0 +1,72 @@
"""Freeze the existing ten-part list and data/code whitelist before any worker."""
import csv
import json
from pathlib import Path
import shutil
from freeze_g1_components import sha
ROOT=Path('artifacts/s2-ten-part-measurement-2026-11-15')
def main():
ROOT.mkdir(exist_ok=False);(ROOT/'public').mkdir()
plan_path=Path('artifacts/s2-e1-2026-11-14/preflight/ten_part_plan.csv')
rows=list(csv.DictReader(plan_path.open()))
assert [int(r['numeric_prefix']) for r in rows]==[1130,1324,842,367,746,777,1552,1379,610,666]
for r in rows:
for k in ['numeric_prefix','tet_nodes','tet_cells','tertile']:r[k]=int(r[k])
log_path=Path('artifacts/s2-e1-2026-11-14/log-flow-setting-check/fill-time-setting-context.json')
settings={r['member'].removesuffix('.log'):r for r in json.loads(log_path.read_text())}
g1=json.loads(Path('artifacts/s2-g1-registration-2026-11-14/custodian/plan.json').read_text())['geometry_cache']
original_o2=Path('artifacts/o2-rep-v1.1a-2026-11-14')
old_contract=json.loads((original_o2/'execution_contract.json').read_text())
for r in rows:
case=r['case'];setting=settings[case]
assert setting['setting_before_filling_phase'] and setting['setting']['seconds']>0
r.update(t_set=setting['setting']['seconds'],t_set_provenance=setting,
g1_geometry=g1[case]['path'],input_sha256={g1[case]['path']:g1[case]['sha256'],str(log_path):sha(log_path)})
primary=Path('artifacts/s1-sampled-draw-2026-11-14')/case/'draw.npz'
if primary.exists():r['existing_primary_draw']=str(primary);r['input_sha256'][str(primary)]=sha(primary)
if r['numeric_prefix']==666:
old=next(s for s in old_contract['cases'] if s['case']==case)
result_path=original_o2/(case+'_original')/'result.json';result=json.loads(result_path.read_text())
r.update(legacy_geometry=old['sample'],existing_distance=old['distance'],existing_o2_nodes=str(result_path.parent/'nodes.npz'),existing_o2_result=str(result_path))
for path in [r['legacy_geometry'],r['existing_distance']]:r['input_sha256'][path]=old_contract['input_sha256'][path]
r['input_sha256'][r['existing_o2_nodes']]=result['full_output_sha256']['nodes.npz']
r['input_sha256'][str(result_path)]=sha(result_path)
for p,h in r['input_sha256'].items():assert sha(p)==h,p
paths=['tools/s2_preprocess.py','tools/run_s2_ten_part_measurement.py','tools/prepare_s2_ten_part_measurement.py','tools/test_s2_preprocess.py',
'tools/s2_e1_supervisor.py','tools/freeze_g1_components.py','tools/g1_candidate_fingerprints.py','tools/g1_registration_cost.py',
'tools/audit_samples.py','tools/boundary_distance.py','tools/run_e6_real_kernel.py','tools/o2_rep.py','tools/s2_models/features.py',
'tools/run_volume_quadrature_trial.py','tools/projector_remesh_probe.py','tools/route_feasibility.py','tools/physical_support_probe.py','tools/analyze_support_capacity_conditions.py']
contract=dict(version='S2-ten-part-preparation-v1',authorizing_rulings=['1794679459.056999'],fixed_split_commit='fb827178eca01ff71f9854c3eb3e985cf5d808fa',
cases=rows,code_sha256={p:sha(p) for p in paths},plan_sha256=sha(plan_path),
archive='data.zip',archive_stat=json.loads(Path('artifacts/intake-2026-11-13/archive_verification.json').read_text()),
limits=dict(stage_wall_seconds=10800,stage_total_cpu_seconds=10800,stage_disk_bytes=5*2**30,per_part_wall_seconds=1800,per_part_rss_bytes=1342177280,parallel_workers=1,numerical_threads=1,retries=0,replacements=0),
input_policy='Geometry-prefix VTU only; explicit geometry NPY whitelist for legacy 666 container; no fill_time array decoding. Existing process setting provenance only. No fitted statistics or labels.',
scales='full-node lumped lower weighted median d: t_part=4*median; R=10*median; full-part lumped center and Rg; no fallback',
draw='reuse any frozen S1 primary draw; otherwise PCG64(SeedSequence([2026111400,prefix])), 2000 PPS replacement slots, d>0, ascending tet/raw IDs',
eligibility='S2-v1-no-gate-q: d>0, O2 valid, N_eff>=64, complete incident-edge block with at least one finite valid endpoint; finite g and edge summary',
cache_policy='Reuse immutable compatible geometry/distance/O2; report new and reused stages separately. Nine missing distance/O2 fields are computed once. Largest 666 full O2 reused at drawn nodes.',
failure_policy='Each preselected part attempted at most once; retain failure and partial outputs; continue other parts within total deadline; never impute unreachable R2 graph or zero scale',
output='base10 all nodes; R2-G24 all nodes20; common query IDs/counts, g2 and E4 with coverage/rejections; no base standardizer fit, labels, optimizer, training or GPU',
pilot='Subsequent stage only, after ten-part evidence; 4 runs total<=2 GPU-h, each<=30min, test fold sealed, no retry')
(ROOT/'contract.json').write_text(json.dumps(contract,indent=2)+'\n')
shutil.copy2(plan_path,ROOT/'public/ten-part-plan.csv')
test=dict(status='PASS',new_adapter_synthetic_checks=6,passed=6,observed_unittest_seconds=.035,
names=['geometry-only whitelist','fixed draws and original IDs','nonpositive scale rejection','S2 no gate eligibility','all incident edge endpoints','stage wall and shared CPU budget'],
unchanged_supervisor_previous_checks=6,previous_receipt='artifacts/s2-e1-2026-11-14/preflight/synthetic-test-receipt.json',
previous_receipt_sha256=sha('artifacts/s2-e1-2026-11-14/preflight/synthetic-test-receipt.json'),
supervisor_source_matches_prior=sha('tools/s2_e1_supervisor.py')=='6753c9ceb0b932047ba505bd15681f109e9407bdb58fd96c55e1431a3f1b4a02',
source_sha256=contract['code_sha256'],labels_fit_gpu=False)
assert test['supervisor_source_matches_prior']
(ROOT/'public/implementation-acceptance.json').write_text(json.dumps(test,indent=2)+'\n')
splits=json.loads(Path('artifacts/s2-g1-components-2026-11-15/public/splits.json').read_text())
with (ROOT/'public/learning-curve-x-axis.csv').open('x') as f:
w=csv.writer(f);w.writerow(['fold','component_fraction_percent','actual_components','actual_parts','actual_nodes','x_axis_label'])
for fold in splits['folds']:
for pool,count in fold['counts']['pools'].items():w.writerow([fold['fold'],pool,count['components'],count['parts'],count['nodes'],'component fraction'])
print(json.dumps(dict(status='CONTRACT_FROZEN_NO_WORKER_YET',contract_sha256=sha(ROOT/'contract.json'),parts=10,legacy_666_reused=True)))
if __name__=='__main__':main()
+124
View File
@@ -0,0 +1,124 @@
"""16 guarded workers, shared CPU/disk caps, exact first-50 terminal barrier."""
import argparse
import fcntl
import json
import os
from pathlib import Path
import resource
import signal
import subprocess
import sys
import time
import psutil
from freeze_g1_components import sha
from s2_e1_supervisor import supervise,save,disk_usage,stop_group
def contract(root):
c=json.loads((root/'contract.json').read_text())
for p,h in c['code_sha256'].items():assert sha(p)==h,p
return c
def guard(root,index):
c=contract(root);job=root/'jobs'/f'job-{index:04d}';job.mkdir(parents=True,exist_ok=True)
r=supervise([sys.executable,'tools/s2_pilot_prepare.py',str(root),str(index)],job,'worker',
dict(total_cpu_seconds=1801,job_cpu_seconds=1800,job_wall_seconds=1800,tree_rss_bytes=1342177280,
disk_bytes=100*2**30,max_processes=1),poll=.25)
save(job/'guard-result.json',dict(worker=r,supervisor_cpu_seconds=sum(resource.getrusage(resource.RUSAGE_SELF)[:2])))
return 0 if r['status']=='PASS' else 1
def projection(c,done):
by_index={r['index']:r for r in done};observed={False:[0.,0],True:[0.,0]}
for i,r in by_index.items():
cached=bool(c['cases'][i].get('reuse_preparation'));observed[cached][0]+=r['worker']['cpu_seconds'];observed[cached][1]+=c['cases'][i]['tet_nodes']
rate={key:(cpu/nodes if nodes else c['initial_cpu_seconds_per_node']) for key,(cpu,nodes) in observed.items()}
spent=sum(r['worker']['cpu_seconds'] for r in done)
remaining=sum(rate[bool(s.get('reuse_preparation'))]*s['tet_nodes'] for i,s in enumerate(c['cases']) if i not in by_index)
return dict(projected_worker_cpu_seconds=spent+remaining,spent_worker_cpu_seconds=spent,
observed_rate_seconds_per_node={str(k):v for k,v in rate.items()},method='Separate reused-complete and newly prepared observed node rates; initial rate only for unseen category')
def terminate(active):
for process,_ in active.values():
try:
for child in psutil.Process(process.pid).children():stop_group(child.pid)
except psutil.NoSuchProcess:pass
def run(root):
with (root/'batch.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
return run_locked(root)
def run_locked(root):
c=contract(root)
assert not (root/'batch-started.json').exists(),'Batch already attempted; inspect receipts, do not restart'
assert c['initial_projection_cpu_hours']<=60 and c['initial_projection_gib']<=100
env=dict(os.environ,OMP_NUM_THREADS='1',OPENBLAS_NUM_THREADS='1',MKL_NUM_THREADS='1',NUMEXPR_NUM_THREADS='1',CUDA_VISIBLE_DEVICES='')
start=time.monotonic();save(root/'batch-started.json',dict(started_unix=time.time(),pid=os.getpid(),contract_sha256=sha(root/'contract.json')))
active={};done=[];next_index=0;stop_reason=None;checkpoint=False;peak_rss=0
def interrupted(*_):
nonlocal stop_reason
stop_reason='COORDINATOR_INTERRUPTED'
signal.signal(signal.SIGTERM,interrupted);signal.signal(signal.SIGINT,interrupted)
try:
while active or (next_index<len(c['cases']) and stop_reason is None):
limit=len(c['cases']) if checkpoint else min(50,len(c['cases']))
while stop_reason is None and len(active)<16 and next_index<limit:
i=next_index;next_index+=1;job=root/'jobs'/f'job-{i:04d}';job.mkdir(parents=True,exist_ok=True)
log=(job/'guard.log').open('x');p=subprocess.Popen([sys.executable,__file__,str(root),'--guard',str(i)],env=env,stdout=log,stderr=subprocess.STDOUT,start_new_session=True)
active[i]=(p,log)
for i,(p,log) in list(active.items()):
code=p.poll()
if code is not None:
log.close();path=root/'jobs'/f'job-{i:04d}'/'guard-result.json'
if not path.exists():
stop_reason='GUARD_MISSING_ACCOUNTING_RECEIPT'
result=dict(worker=dict(status='GUARD_FAILED_ACCOUNTING_UNKNOWN',cpu_seconds=0.,wall_seconds=None,peak_tree_rss_bytes=None),supervisor_cpu_seconds=0.)
else:result=json.loads(path.read_text())
done.append(dict(index=i,case=c['cases'][i]['case'],returncode=code,**result));del active[i]
print(json.dumps(dict(event='FINISHED',index=i,completed=len(done),status=result['worker']['status'])),flush=True)
live_cpu=live_rss=0
for p,_ in active.values():
try:
for q in [psutil.Process(p.pid)]+psutil.Process(p.pid).children(recursive=True):
try:
t=q.cpu_times();live_cpu+=t.user+t.system+t.children_user+t.children_system;live_rss+=q.memory_info().rss
except psutil.NoSuchProcess:pass
except psutil.NoSuchProcess:pass
peak_rss=max(peak_rss,live_rss)
used=disk_usage(root);cpu=sum(r['worker']['cpu_seconds'] for r in done)
# Conservatively includes live guard CPU; completed guard CPU is separately reported.
if cpu+live_cpu>=60*3600:stop_reason='GLOBAL_CPU_LIMIT'
if used>=100*2**30:stop_reason='GLOBAL_DISK_LIMIT'
if len(done)==min(50,len(c['cases'])) and not active and not checkpoint and stop_reason is None:
proj=projection(c,done);finished_nodes=sum(c['cases'][r['index']]['tet_nodes'] for r in done)
projected_disk=used/finished_nodes*sum(s['tet_nodes'] for s in c['cases'])
checkpoint=True
if proj['projected_worker_cpu_seconds']>60*3600 or projected_disk>100*2**30:stop_reason='FIRST_50_PROJECTION_OVER_CAP'
save(root/'first-50-checkpoint.json',dict(**proj,projected_disk_bytes=projected_disk,continue_within_cap=stop_reason is None,completed=len(done),failed=sum(r['worker']['status']!='PASS' for r in done)))
print(json.dumps(dict(event='FIRST_50_CHECKPOINT',**proj,projected_disk_bytes=projected_disk,stop_reason=stop_reason)),flush=True)
if stop_reason:terminate(active)
save(root/'progress.json',dict(completed=len(done),active=list(active),next_index=next_index,first50_complete=checkpoint,worker_cpu_seconds=cpu,
live_guard_and_worker_cpu_seconds=live_cpu,active_tree_rss_bytes=live_rss,disk_bytes=used,elapsed_seconds=time.monotonic()-start,stop_reason=stop_reason))
if active:time.sleep(.5)
finally:
terminate(active)
for p,log in active.values():
try:p.wait(timeout=10)
except subprocess.TimeoutExpired:stop_group(p.pid);p.wait()
log.close()
result=dict(status='ATTEMPTS_COMPLETE' if len(done)==len(c['cases']) else 'STOPPED',stop_reason=stop_reason,jobs=done,
not_run_indices=list(range(next_index,len(c['cases']))),worker_cpu_seconds=sum(r['worker']['cpu_seconds'] for r in done),
guard_cpu_seconds=sum(r['supervisor_cpu_seconds'] for r in done),coordinator_cpu_seconds=sum(resource.getrusage(resource.RUSAGE_SELF)[:2]),
wall_seconds=time.monotonic()-start,peak_aggregate_rss_bytes=peak_rss,disk_bytes=disk_usage(root),
failed=sum(r['worker']['status']!='PASS' for r in done),retry=False,replacement=False,test_labels_read=False)
save(root/'batch-complete.json',result);print(json.dumps({k:v for k,v in result.items() if k!='jobs'}),flush=True)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('--guard',type=int);a=p.parse_args()
sys.exit(guard(a.root,a.guard) if a.guard is not None else run(a.root))
+126
View File
@@ -0,0 +1,126 @@
"""One fixed pilot arm/width. Must be launched inside the GPU resource guard."""
import argparse
from collections import Counter
from collections.abc import Mapping
import json
from pathlib import Path
import random
import time
import traceback
import numpy as np
import torch
from freeze_g1_components import sha
from s2_e1_supervisor import save
from s2_models.network import QueryModel,TRAINING_CONTRACT
from s2_models.normalization import BaseStandardizer,fit_base_standardizer
from s2_models.objectives import relative_l2
from s2_pilot_data import permitted_role
from s2_pilot_train import part_weights,train_part,component_score
class LearningBases(Mapping):
def __init__(self,records,ids,roles):self.records=records;self.ids=tuple(ids);self.roles=roles
def __iter__(self):return iter(self.ids)
def __len__(self):return len(self.ids)
def __getitem__(self,case):
if case not in self.ids or permitted_role(self.roles,case)!='learning':raise PermissionError('Learning-only normalization')
return np.load(Path(self.records[case]['preparation_root'])/'base.npy',allow_pickle=False,mmap_mode='r')
def load_part(record,roles,device):
case=record['case'];permitted_role(roles,case)
root=Path(record['preparation_root']);label=Path(record['label_path'])
assert sha(root/'base.npy')==record['input_sha256']['base.npy']
assert sha(root/'queries.npz')==record['input_sha256']['queries.npz']
assert sha(label)==record['input_sha256']['labels.npz']
with np.load(root/'queries.npz',allow_pickle=False) as q:
mask=q['eligible_s2'];ids=q['anchor_ids'][mask];g=q['g'][mask];edge=q['edge_summary'][mask];counts=q['multiplicity'][mask]
with np.load(label,allow_pickle=False) as z:
assert np.array_equal(ids,z['anchor_ids']) and np.array_equal(counts,z['multiplicity'])
target=z['target_t_star'];seconds=z['seconds']
base=np.load(root/'base.npy',allow_pickle=False)
if len(base)!=record['nodes']:raise ValueError('Full-node base size mismatch')
tensor=lambda a,dtype=torch.float32:torch.as_tensor(np.array(a,copy=True),dtype=dtype,device=device)
return dict(base=tensor(base),ids=tensor(ids,torch.long),gaps=tensor(g),edge=tensor(edge),target=tensor(target),
counts=counts,truth_seconds=seconds)
def run(root,index):
started=time.monotonic();c=json.loads((root/'training-contract.json').read_text());run=c['runs'][index]
dest=root/run['name'];dest.mkdir(exist_ok=False);save(dest/'started.json',dict(started_unix=time.time(),contract_sha256=sha(root/'training-contract.json')))
try:
for p,h in c['code_sha256'].items():assert sha(p)==h,p
source=Path(c['success_manifest']);assert sha(source)==c['success_manifest_sha256']
manifest=json.loads(source.read_text());records={r['case']:r for r in manifest['ready_parts']}
roles=json.loads(Path(c['role_manifest']).read_text());assert sha(c['role_manifest'])==c['role_manifest_sha256']
learning=sorted(p for p in records if permitted_role(roles,p)=='learning')
validation=sorted(p for p in records if permitted_role(roles,p)=='validation')
if not learning or not validation:raise ValueError('Empty successful learning/validation roster')
if any(p in roles['sealed_test_parts'] for p in records):raise PermissionError('Test case present in ready manifest')
assert run['seed']==0 and c['fold']==0 and c['pool']==100
torch.set_num_threads(1);random.seed(run['seed']);np.random.seed(run['seed']);torch.manual_seed(run['seed'])
if not torch.cuda.is_available() or torch.cuda.device_count()!=1:raise RuntimeError('Exactly one visible CUDA device required')
device=torch.device('cuda:0');torch.cuda.manual_seed_all(run['seed']);torch.cuda.reset_peak_memory_stats()
torch.backends.cuda.matmul.allow_tf32=False
if 'V100' not in torch.cuda.get_device_name():raise RuntimeError('Frozen pilot requires one V100')
# Every base statistics calculation is learning-only and inside this run's wall guard.
stats=fit_base_standardizer(LearningBases(records,learning,roles),learning_part_ids=learning,fold='0',pool='100')
save(dest/'standardizer.json',dict(**stats.as_dict(),fingerprint=stats.fingerprint))
shared=root/'shared-standardizer-fingerprint.json'
if shared.exists():assert json.loads(shared.read_text())['fingerprint']==stats.fingerprint
else:save(shared,dict(fingerprint=stats.fingerprint,learning_parts=learning))
model=QueryModel(run['arm'],run['width'],standardizer=stats).to(device)
optimizer=torch.optim.AdamW(model.parameters(),lr=1e-3,weight_decay=1e-5)
scheduler=torch.optim.lr_scheduler.CosineAnnealingLR(optimizer,T_max=200)
scaler=torch.amp.GradScaler('cuda');rng=np.random.Generator(np.random.PCG64(run['seed']))
group_counts=Counter(records[p]['component_id'] for p in learning);group_number=len(group_counts)
val_groups={p:records[p]['component_id'] for p in validation}
best=float('inf');best_epoch=None;stale=0;history=[];updates=0
for epoch in range(200):
model.train();loss_sum=0.
order=[learning[int(j)] for j in rng.permutation(len(learning))]
for j,case in enumerate(order):
record=records[case];data=load_part(record,roles,device)
weights=torch.tensor(part_weights(data['counts'],group_counts[record['component_id']],group_number,len(learning)),dtype=torch.float32,device=device)
loss=train_part(model,optimizer,data['base'],data['ids'],data['gaps'],data['edge'],data['target'],weights,scaler=scaler,amp=True)
loss_sum+=loss/len(learning);updates+=1
del data,weights
if j%16==0:
torch.cuda.synchronize();save(dest/'progress.json',dict(stage='training',epoch=epoch,parts_done=j+1,parts_total=len(learning),updates=updates,elapsed_seconds=time.monotonic()-started,
peak_allocated_bytes=torch.cuda.max_memory_allocated(),peak_reserved_bytes=torch.cuda.max_memory_reserved()))
model.eval();errors={}
with torch.no_grad():
for case in validation:
data=load_part(records[case],roles,device)
with torch.autocast(device_type='cuda',dtype=torch.float16):
pred=model(data['base'],data['ids'],data['gaps'],data['edge'] if run['arm']=='E' else None)
predicted_seconds=pred.float().cpu().numpy().astype(np.float64)*records[case]['t_set']
score=relative_l2(predicted_seconds,data['truth_seconds'],data['counts'],np.ones(len(data['counts']),bool))
errors[case]=score['value'];del data,pred
value=component_score(errors,val_groups);torch.cuda.synchronize()
row=dict(epoch=epoch,training_global_weighted_mse=loss_sum,validation_component_equal_relative_l2=value,updates=updates,elapsed_seconds=time.monotonic()-started,
peak_allocated_bytes=torch.cuda.max_memory_allocated(),peak_reserved_bytes=torch.cuda.max_memory_reserved())
history.append(row);save(dest/'history.json',history)
if value<best:
best=value;best_epoch=epoch;stale=0
torch.save(dict(state_dict={k:v.detach().cpu() for k,v in model.state_dict().items()},arm=run['arm'],width=run['width'],seed=run['seed'],epoch=epoch,
standardizer=stats.as_dict(),standardizer_fingerprint=stats.fingerprint),dest/'best.pt')
save(dest/'best-validation-parts.json',errors)
else:stale+=1
scheduler.step()
save(dest/'progress.json',dict(stage='epoch_complete',**row,patience_count=stale))
if stale>=20:break
torch.cuda.synchronize()
result=dict(status='COMPLETE',run=run,epochs=len(history),updates=updates,best_epoch=best_epoch,best_validation_score=best,
learning_parts=len(learning),validation_parts=len(validation),learning_components=group_number,validation_components=len(set(val_groups.values())),
failed_preparation_parts_dropped=manifest['failed_parts'],standardizer_fingerprint=stats.fingerprint,
wall_seconds=time.monotonic()-started,peak_allocated_bytes=torch.cuda.max_memory_allocated(),peak_reserved_bytes=torch.cuda.max_memory_reserved(),
test_labels_read=False,test_predictions=False,capacity_selected=False,checkpoint_sha256=sha(dest/'best.pt'))
save(dest/'result.json',result);print(json.dumps(result),flush=True);return 0
except Exception as exc:
save(dest/'failure.json',dict(status='FAILED_NO_RETRY',error_type=type(exc).__name__,message=str(exc),traceback=traceback.format_exc(),wall_seconds=time.monotonic()-started))
raise
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int);a=p.parse_args();raise SystemExit(run(a.root,a.index))
+49
View File
@@ -0,0 +1,49 @@
"""One sequential, non-resumable ten-part attempt with shared 3h/5GiB limits."""
import argparse
import fcntl
import json
from pathlib import Path
import sys
import time
from s2_e1_supervisor import supervise,save,disk_usage
from freeze_g1_components import sha
def remaining_limits(elapsed, cpu, guard_cpu):
wall=10800-elapsed;total_worker_cpu=10800-guard_cpu
if wall<=0 or total_worker_cpu-cpu<=0:return None
return dict(total_cpu_seconds=total_worker_cpu,job_cpu_seconds=min(1800,total_worker_cpu-cpu),
job_wall_seconds=min(1800,wall),tree_rss_bytes=1342177280,disk_bytes=5*2**30,max_processes=1)
def run(root):
contract=json.loads((root/'contract.json').read_text())
for p,h in contract['code_sha256'].items():assert sha(p)==h,p
with (root/'batch.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
with (root/'batch-started.json').open('x') as f:json.dump(dict(started_unix=time.time(),contract_sha256=sha(root/'contract.json')),f)
start=time.monotonic();cpu_start=time.process_time();worker_cpu=0.;rows=[];stop_reason=None
for i,spec in enumerate(contract['cases']):
limits=remaining_limits(time.monotonic()-start,worker_cpu,time.process_time()-cpu_start)
if limits is None:stop_reason='total_wall_or_cpu_budget_exhausted';break
if disk_usage(root)>=5*2**30:stop_reason='total_disk_budget_exhausted';break
name=f'part-{i:02d}'
r=supervise([sys.executable,'tools/s2_preprocess.py',str(root),str(i)],root,name,limits,poll=.1)
worker_cpu+=r['cpu_seconds'];row=dict(index=i,case=spec['case'],**r);rows.append(row)
print(json.dumps({k:row[k] for k in ['index','case','status','stop_reason','cpu_seconds','wall_seconds','peak_tree_rss_bytes']}),flush=True)
save(root/'batch-progress.json',dict(completed_attempts=len(rows),worker_cpu_seconds=worker_cpu,supervisor_cpu_seconds=time.process_time()-cpu_start,
wall_seconds=time.monotonic()-start,last=row))
unrun=[dict(index=i,case=contract['cases'][i]['case'],status='NOT_RUN_BUDGET') for i in range(len(rows),10)]
data=dict(status='ATTEMPTS_COMPLETE' if len(rows)==10 else 'STOPPED_AT_STAGE_BUDGET',attempts=rows,not_run=unrun,
stop_reason=stop_reason,failed_attempts=sum(r['status']!='PASS' for r in rows),retry=False,replacement=False,
worker_cpu_seconds=worker_cpu,supervisor_cpu_seconds=time.process_time()-cpu_start,wall_seconds=time.monotonic()-start,
peak_worker_tree_rss_bytes=max((r['peak_tree_rss_bytes'] for r in rows),default=0),disk_bytes=disk_usage(root),
contract_sha256=sha(root/'contract.json'),labels_read=False,training_gpu=False)
if data['wall_seconds']>10800:data['stage_wall_budget_exceeded']=True
if worker_cpu+data['supervisor_cpu_seconds']>10800:data['stage_cpu_budget_exceeded']=True
save(root/'batch-complete.json',data)
print(json.dumps({k:v for k,v in data.items() if k not in ['attempts','not_run']}),flush=True)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);a=p.parse_args();run(a.root)
+165
View File
@@ -0,0 +1,165 @@
"""Pilot process guard derived from frozen E1 guard; explicit CUDA visibility.
Budget checks are sampled, not kernel-enforced RSS/directory quotas. Overshoot
is measured and fails the job. Run only reviewed label-free worker commands.
"""
import fcntl
import ctypes
import json
import math
import os
from pathlib import Path
import signal
import subprocess
import time
import psutil
def save(path, data):
path = Path(path)
tmp = path.with_suffix(path.suffix + '.tmp')
tmp.write_text(json.dumps(data, indent=2, allow_nan=False) + '\n')
tmp.replace(path)
def disk_usage(root):
logical = allocated = 0
for p in Path(root).rglob('*'):
if p.is_file() and not p.is_symlink():
s = p.stat()
logical += s.st_size
allocated += s.st_blocks * 512
return max(logical, allocated)
def tree_usage(pid):
try:
root = psutil.Process(pid)
processes = [root] + root.children(recursive=True)
except psutil.NoSuchProcess:
return 0., 0, 0
cpu = 0.
rss = count = 0
for p in processes:
try:
t = p.cpu_times()
# Linux child times include reaped descendants, not live children.
cpu += t.user + t.system + t.children_user + t.children_system
rss += p.memory_info().rss
count += 1
except psutil.NoSuchProcess:
pass
return cpu, rss, count
def stop_group(pid):
try:
os.killpg(pid, signal.SIGKILL)
except ProcessLookupError:
pass
def supervise(command, root, name, limits, *, poll=.02, gpu_uuid=None):
"""One locked attempt; no automatic retry/replacement; account failed CPU."""
root = Path(root)
root.mkdir(parents=True, exist_ok=True)
with (root/'execution.lock').open('a') as lock:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
started_path = root/(name+'.started.json')
receipt_path = root/(name+'.receipt.json')
if started_path.exists() or receipt_path.exists():
raise FileExistsError('Attempt already exists; no silent retry')
if any(not math.isfinite(v) or v <= 0 for v in limits.values()):
raise ValueError('Every limit must be finite and positive')
ledger_path = root/'ledger.json'
ledger = json.loads(ledger_path.read_text()) if ledger_path.exists() else {'cpu_seconds_charged':0.,'attempts':[]}
if ledger['cpu_seconds_charged'] >= limits['total_cpu_seconds']:
raise RuntimeError('Total CPU exhausted before spawn')
if disk_usage(root) >= limits['disk_bytes']:
raise RuntimeError('Disk exhausted before spawn')
save(started_path, {'command':command,'limits':limits,'started_unix':time.time()})
start = last = time.monotonic()
cpu_peak = rss_peak = max_processes = max_gap = 0
reason = None
exit_status = usage = None
env = dict(os.environ, OMP_NUM_THREADS='1', OPENBLAS_NUM_THREADS='1',
MKL_NUM_THREADS='1', NUMEXPR_NUM_THREADS='1', CUDA_VISIBLE_DEVICES=str(gpu_uuid or ''))
with (root/(name+'.log')).open('x') as log:
libc = ctypes.CDLL(None, use_errno=True)
previous_subreaper = ctypes.c_int()
if libc.prctl(37, ctypes.byref(previous_subreaper), 0, 0, 0) != 0 or libc.prctl(36, 1, 0, 0, 0) != 0:
raise OSError(ctypes.get_errno(), 'Cannot enable descendant reaping')
proc = subprocess.Popen(command, stdout=log, stderr=subprocess.STDOUT,
env=env, start_new_session=True)
try:
while True:
now = time.monotonic()
max_gap = max(max_gap, now-last)
last = now
cpu, rss, count = tree_usage(proc.pid)
cpu_peak = max(cpu_peak,cpu)
rss_peak = max(rss_peak,rss)
max_processes = max(max_processes,count)
used_disk = disk_usage(root)
if now-start >= limits['job_wall_seconds']:
reason = 'wall_limit'
elif rss > limits['tree_rss_bytes']:
reason = 'tree_rss_limit'
elif count > limits['max_processes']:
reason = 'process_count_limit'
elif used_disk >= limits['disk_bytes']:
reason = 'disk_limit'
elif cpu_peak >= limits['job_cpu_seconds']:
reason = 'job_cpu_limit'
elif ledger['cpu_seconds_charged']+cpu_peak >= limits['total_cpu_seconds']:
reason = 'total_cpu_limit'
if reason:
stop_group(proc.pid)
pid, status, child_usage = os.wait4(proc.pid, 0 if reason else os.WNOHANG)
if pid:
exit_status, usage = status, child_usage
break
time.sleep(poll)
finally:
# No detached work may outlive a supervised attempt.
stop_group(proc.pid)
if exit_status is None:
_, exit_status, usage = os.wait4(proc.pid, 0)
proc.returncode = os.waitstatus_to_exitcode(exit_status)
orphan_cpu = 0.
orphan_peak = 0
# Killed descendants are adopted here rather than losing their
# final CPU usage when the root worker cannot reap them itself.
while True:
try:
_, _, orphan_usage = os.wait4(-proc.pid, 0)
except ChildProcessError:
break
orphan_cpu += orphan_usage.ru_utime + orphan_usage.ru_stime
orphan_peak = max(orphan_peak, orphan_usage.ru_maxrss*1024)
if libc.prctl(36, previous_subreaper.value, 0, 0, 0) != 0:
raise OSError(ctypes.get_errno(), 'Cannot restore subreaper state')
elapsed = time.monotonic()-start
charged = max(cpu_peak, usage.ru_utime+usage.ru_stime+orphan_cpu)
rss_peak = max(rss_peak, usage.ru_maxrss*1024,orphan_peak)
used_disk = disk_usage(root)
if reason is None:
if elapsed > limits['job_wall_seconds']: reason='wall_limit_after_exit'
elif rss_peak > limits['tree_rss_bytes']: reason='rss_limit_after_exit'
elif charged > limits['job_cpu_seconds']: reason='cpu_limit_after_exit'
elif ledger['cpu_seconds_charged']+charged > limits['total_cpu_seconds']: reason='total_cpu_limit_after_exit'
elif used_disk >= limits['disk_bytes']: reason='disk_limit_after_exit'
receipt = dict(status='PASS' if proc.returncode==0 and reason is None else 'STOPPED_OR_FAILED',
command=command,returncode=proc.returncode,stop_reason=reason,
cpu_seconds=charged,wall_seconds=elapsed,peak_tree_rss_bytes=rss_peak,
peak_process_count=max_processes,output_disk_bytes_before_receipt=used_disk,
max_poll_gap_seconds=max_gap,poll_target_seconds=poll,
reaped_orphan_cpu_seconds=orphan_cpu,
supervision='sampled process-tree monitoring; wait4 final CPU/HWM; overshoot retained as failure',
gpu_visible=bool(gpu_uuid),gpu_uuid=gpu_uuid,retry=False)
ledger['cpu_seconds_charged'] += charged
ledger['attempts'].append({'name':name,'status':receipt['status'],'cpu_seconds':charged})
save(receipt_path,receipt)
save(ledger_path,ledger)
return receipt
+52
View File
@@ -0,0 +1,52 @@
"""Pilot label access is limited to an immutable non-test role manifest."""
import json
from pathlib import Path
from xml.etree import ElementTree as ET
from zipfile import ZipFile
import numpy as np
from audit_samples import decode_array
def permitted_role(manifest, case):
if case in manifest['sealed_test_parts']:
raise PermissionError('Outer test labels are sealed')
if case in manifest['learning_parts']:return 'learning'
if case in manifest['validation_parts']:return 'validation'
raise PermissionError('Case absent from frozen non-test manifest')
def read_non_test_fill_time(archive, case, raw_ids, manifest):
# Must reject forbidden cases before opening the archive or any member.
role=permitted_role(manifest,case)
raw_ids=np.asarray(raw_ids)
if raw_ids.ndim!=1 or raw_ids.dtype.kind not in 'iu' or len(raw_ids)==0 or np.any(np.diff(raw_ids)<=0):
raise ValueError('Ascending original raw IDs required')
stack=[];count=None;header=None
with ZipFile(archive) as z,z.open(case+'_sim.vtu') as stream:
for event,e in ET.iterparse(stream,events=('start','end')):
if event=='start':
stack.append(e.tag)
if e.tag=='VTKFile':
if e.get('byte_order')!='LittleEndian' or e.get('compressor')!='vtkZLibDataCompressor':raise ValueError('Unsupported encoding')
header=e.get('header_type','UInt32')
elif e.tag=='Piece':
if count is not None:raise ValueError('Multiple VTU pieces')
count=int(e.get('NumberOfPoints'))
else:
if e.tag=='DataArray':
if stack[-2]=='PointData' and e.get('Name')=='Fill_time':
values=decode_array(e,header)
if values.shape!=(count,) or raw_ids[0]<0 or raw_ids[-1]>=count:raise ValueError('Fill_time size/original IDs mismatch')
return values[raw_ids].copy(),dict(role=role,array_name='Fill_time',association='PointData',decoded_label_arrays=['Fill_time'],
other_field_arrays_decoded=[],archive_member=case+'_sim.vtu',all_raw_nodes=count,retained_tet_nodes=len(raw_ids))
e.clear()
stack.pop()
raise ValueError('PointData Fill_time missing')
def validate_query_targets(fill_time, ids, eligible, t_set):
selected=np.asarray(fill_time)[np.asarray(ids)[np.asarray(eligible,dtype=bool)]]
if not len(selected):raise ValueError('No eligible common queries')
if not np.isfinite(selected).all() or np.any(selected<0):raise ValueError('Invalid Fill_time on eligible common queries')
if not np.isfinite(t_set) or t_set<=0:raise ValueError('Missing process setting')
return selected/t_set
+106
View File
@@ -0,0 +1,106 @@
"""Sequential four-run pilot guard: process-tree RSS/wall plus device VRAM."""
import argparse
import fcntl
import json
import os
from pathlib import Path
import subprocess
import sys
import threading
import time
import psutil
from freeze_g1_components import sha
from s2_gpu_process_supervisor import supervise,save,stop_group,disk_usage,tree_usage
from s2_pilot_train import select_capacity
def parse_device_usage(text):
lines=text.strip().splitlines()
if len(lines)!=1:raise ValueError('Expected exactly one GPU')
fields=lines[0].split(',')
if len(fields)!=2:raise ValueError('Malformed GPU usage row')
memory,util=map(lambda s:int(s.strip()),fields)
if memory<0 or not 0<=util<=100:raise ValueError('Invalid GPU counters')
return memory*2**20,util
def device_usage(uuid):
r=subprocess.run(['nvidia-smi','--id='+uuid,'--query-gpu=memory.used,utilization.gpu','--format=csv,noheader,nounits'],check=True,capture_output=True,text=True,timeout=2)
return parse_device_usage(r.stdout)
def active_training_pids(parent,root,index):
ids=[]
try:children=psutil.Process(parent).children(recursive=True)
except psutil.NoSuchProcess:return ids
for p in children:
try:
cmd=p.cmdline()
if 'tools/run_s2_pilot_training.py' in cmd and str(root) in cmd and str(index)==cmd[-1]:ids.append(p.pid)
except psutil.NoSuchProcess:pass
return ids
def run(root):
with (root/'pilot.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
return run_locked(root)
def run_locked(root):
c=json.loads((root/'training-contract.json').read_text());uuid=c['gpu_uuid']
assert [(r['arm'],r['width'],r['seed']) for r in c['runs']]==[('N',128,0),('E',128,0),('N',256,0),('E',256,0)]
for p,h in c['code_sha256'].items():assert sha(p)==h,p
assert not (root/'pilot-started.json').exists(),'No automatic pilot retry'
start=time.monotonic();save(root/'pilot-started.json',dict(started_unix=time.time(),contract_sha256=sha(root/'training-contract.json')))
rows=[];scores={};reason=None
for i,spec in enumerate(c['runs']):
remaining=7200-(time.monotonic()-start)
if remaining<=3:reason='TOTAL_PILOT_WALL_LIMIT';break
run_start=time.monotonic();initial,util=device_usage(uuid)
if initial>128*2**20 or util>0:reason='SELECTED_GPU_BUSY_BEFORE_RUN';break
done=threading.Event();monitor=dict(peak_device_memory_bytes=initial,peak_whole_run_tree_rss_bytes=0,max_poll_gap_seconds=0.,reason=None,samples=0)
def watch():
previous=time.monotonic()
while not done.is_set():
now=time.monotonic();monitor['max_poll_gap_seconds']=max(monitor['max_poll_gap_seconds'],now-previous);previous=now
try:
used,_=device_usage(uuid);monitor['samples']+=1;monitor['peak_device_memory_bytes']=max(monitor['peak_device_memory_bytes'],used)
if used>15*2**30:monitor['reason']='DEVICE_VRAM_LIMIT'
_,rss,_=tree_usage(os.getpid());monitor['peak_whole_run_tree_rss_bytes']=max(monitor['peak_whole_run_tree_rss_bytes'],rss)
if rss>16*2**30:monitor['reason']='WHOLE_RUN_HOST_RSS_LIMIT'
if disk_usage(root)>=5*2**30:monitor['reason']='PILOT_DISK_LIMIT'
except Exception as exc:monitor['reason']='GPU_MONITOR_FAILED';monitor['error']=str(exc)
if monitor['reason']:
for pid in active_training_pids(os.getpid(),root,i):stop_group(pid)
done.wait(.5)
thread=threading.Thread(target=watch,daemon=True);thread.start()
try:
wall=min(1800,remaining)-(time.monotonic()-run_start)-2.5
if wall<=0:raise RuntimeError('No budget left before spawn')
command=[sys.executable,'tools/run_s2_pilot_training.py',str(root),str(i)]
r=supervise(command,root/'guards',spec['name'],dict(total_cpu_seconds=7200,job_cpu_seconds=1800,job_wall_seconds=wall,
tree_rss_bytes=16*2**30,disk_bytes=5*2**30,max_processes=2),poll=.1,gpu_uuid=uuid)
finally:
done.set();thread.join(timeout=3)
total_wall=time.monotonic()-run_start
result_path=root/spec['name']/'result.json'
result=json.loads(result_path.read_text()) if result_path.exists() else None
if monitor['reason']:r.update(status='STOPPED_OR_FAILED',stop_reason=monitor['reason'])
if total_wall>min(1800,remaining):r.update(status='STOPPED_OR_FAILED',stop_reason='TOTAL_RUN_WALL_LIMIT')
if result and max(result['peak_allocated_bytes'],result['peak_reserved_bytes'])>15*2**30:r.update(status='STOPPED_OR_FAILED',stop_reason='RECORDED_VRAM_LIMIT')
r.update(gpu_visible=True,gpu_uuid=uuid,device_monitor=monitor,total_run_wall_seconds=total_wall,
result_sha256=sha(result_path) if result else None)
save(root/(spec['name']+'-guard-receipt.json'),r);rows.append(dict(run=spec,**r))
if r['status']!='PASS' or not result or result['status']!='COMPLETE':reason='PILOT_RUN_FAILED_NO_RETRY';break
scores[(spec['arm'],spec['width'])]=result['best_validation_score']
if time.monotonic()-start>7200:reason='TOTAL_PILOT_WALL_LIMIT_AFTER_EXIT'
selected=select_capacity(scores) if len(scores)==4 and reason is None else None
summary=dict(status='FOUR_RUNS_COMPLETE' if selected else 'PILOT_INCOMPLETE',reason=reason,runs=rows,selected_global_width=selected,
selection_rule='mean of N/E validation error at each width, ties smaller; all four runs required',
pilot_wall_seconds=time.monotonic()-start,test_labels_read=False,test_predictions=False,retries=0)
save(root/'pilot-complete.json',summary);print(json.dumps(summary),flush=True)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);a=p.parse_args();run(a.root)
+56
View File
@@ -0,0 +1,56 @@
"""Prepare an authorized non-test part once, then extract only its permitted label."""
import argparse
import json
from pathlib import Path
import sys
import time
import traceback
import numpy as np
from freeze_g1_components import sha
from s2_e1_supervisor import save
from s2_pilot_data import permitted_role,read_non_test_fill_time,validate_query_targets
import s2_preprocess_pilot as s2_preprocess
def run(root,index):
start=time.monotonic();c=json.loads((root/'contract.json').read_text());spec=c['cases'][index];case=spec['case']
manifest=json.loads((root/'role-manifest.json').read_text());role=permitted_role(manifest,case)
dest=root/'ready'/case;dest.mkdir(parents=True,exist_ok=False)
try:
if spec.get('prior_failed_preparation'):
save(dest/'failure.json',dict(case=case,role=role,status='PRIOR_MEASUREMENT_FAILURE_CARRIED_FORWARD',
prior_artifacts=spec['prior_failed_preparation'],new_scientific_attempt=False,labels_read=False))
return 3
for p,h in c['code_sha256'].items():assert sha(p)==h,p
assert sha(root/'role-manifest.json')==c['role_manifest_sha256']
if spec.get('reuse_preparation'):
source=Path(spec['reuse_preparation']);r=json.loads((source/'result.json').read_text())
assert sha(source/'result.json')==spec['reuse_result_sha256'] and r['status']=='COMPLETE'
for name,h in r['output_sha256'].items():assert sha(source/name)==h
else:
code=s2_preprocess.run(root,index)
if code:raise ValueError('Full input preparation failed; no labels extracted')
source=root/'parts'/case;r=json.loads((source/'result.json').read_text())
with np.load(source/'geometry.npz',allow_pickle=False) as z:raw_ids=z['raw_node_ids']
with np.load(source/'queries.npz',allow_pickle=False) as q:
ids=q['anchor_ids'];eligible=q['eligible_s2'];counts=q['multiplicity']
if not eligible.any():raise ValueError('No common eligible queries; drop pilot part with reason')
st=Path(c['archive']).stat();ver=c['archive_stat']
assert (st.st_size,st.st_mtime_ns,st.st_ino)==(ver['bytes'],ver['mtime_ns'],ver['inode'])
values,label_meta=read_non_test_fill_time(c['archive'],case,raw_ids,manifest)
target=validate_query_targets(values,ids,eligible,spec['t_set'])
np.savez_compressed(dest/'labels.npz',anchor_ids=ids[eligible],raw_node_ids=raw_ids[ids[eligible]],
multiplicity=counts[eligible],seconds=values[ids[eligible]],target_t_star=target)
receipt=dict(case=case,role=role,status='READY',preparation_root=str(source),preparation_result_sha256=sha(source/'result.json'),
label_provenance=label_meta,label_sha256=sha(dest/'labels.npz'),t_set=spec['t_set'],
component_id=spec['component_id'],nodes=spec['tet_nodes'],eligible_queries=int(eligible.sum()),
eligible_slots=int(counts[eligible].sum()),reused_preparation=bool(spec.get('reuse_preparation')),
wall_seconds=time.monotonic()-start,test_labels_read=False,model_fit=False,gpu=False)
save(dest/'receipt.json',receipt);print(json.dumps(receipt),flush=True);return 0
except Exception as exc:
save(dest/'failure.json',dict(case=case,role=role,status='FAILED_NO_REPLACEMENT',error_type=type(exc).__name__,message=str(exc),traceback=traceback.format_exc(),wall_seconds=time.monotonic()-start))
raise
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int);a=p.parse_args();sys.exit(run(a.root,a.index))
+48
View File
@@ -0,0 +1,48 @@
"""Bounded pilot training primitives; corpus runs require a frozen ready manifest."""
import math
from pathlib import Path
import numpy as np
import torch
from s2_models.network import weighted_mse
def part_weights(counts, parts_in_component, component_count, learning_parts):
c=np.asarray(counts,dtype=np.float64)
if c.ndim!=1 or not len(c) or np.any(c<=0) or not np.isfinite(c).all():raise ValueError('Invalid common query counts')
if min(parts_in_component,component_count,learning_parts)<=0:raise ValueError('Empty training hierarchy')
# Uniform shuffled part steps estimate the full component-balanced objective.
return c/c.sum()/parts_in_component/component_count*learning_parts
def train_part(model,optimizer,base,ids,gaps,edge,target,weights,*,scaler=None,amp=False):
optimizer.zero_grad(set_to_none=True)
with torch.autocast(device_type=base.device.type,dtype=torch.float16,enabled=amp):
prediction=model(base,ids,gaps,edge if model.arm=='E' else None)
loss=weighted_mse(prediction,target,weights)
if not torch.isfinite(loss):raise FloatingPointError('Nonfinite training loss')
if scaler is None:
loss.backward()
else:
scaler.scale(loss).backward();scaler.unscale_(optimizer)
if any(p.grad is not None and not torch.isfinite(p.grad).all() for p in model.parameters()):
raise FloatingPointError('Nonfinite gradient; no silent retry or precision fallback')
if scaler is None:optimizer.step()
else:scaler.step(optimizer);scaler.update()
if any(not torch.isfinite(p).all() for p in model.parameters()):raise FloatingPointError('Nonfinite updated parameter')
return float(loss.detach())
def component_score(part_errors,component_ids):
if not part_errors or set(part_errors)!=set(component_ids):raise ValueError('Incomplete validation roster')
groups={}
for part,value in part_errors.items():
if value is None or not math.isfinite(value):raise ValueError('Invalid validation part score')
groups.setdefault(component_ids[part],[]).append(value)
return float(np.mean([np.mean(values) for values in groups.values()]))
def select_capacity(scores):
# Exactly four run scores, no choice from an incomplete/failed run matrix.
if set(scores)!={(arm,width) for arm in ['N','E'] for width in [128,256]}:raise ValueError('All four pilot runs required')
if any(v is None or not math.isfinite(v) for v in scores.values()):raise ValueError('All four pilot validation scores must be finite')
return min([128,256],key=lambda w:((scores['N',w]+scores['E',w])/2,w))
+214
View File
@@ -0,0 +1,214 @@
"""Label-free full-part S2 preparation; original caches and protocols stay unchanged."""
import argparse
import json
from pathlib import Path
import resource
import time
import traceback
from zipfile import ZipFile
import numpy as np
import yaml
from boundary_distance import SurfaceDistance, triangle_distance_squared
from g1_candidate_fingerprints import read_tets
import run_e6_real_kernel as e6
import o2_rep as rep
from s2_models import features
from s2_e1_supervisor import save
from freeze_g1_components import sha
GEOMETRY_MEMBERS = {'points_raw','tetra','node_volume_raw','boundary_triangles','edges','gate_location_raw'}
O2_MEMBERS = {'tensor','kernel_mass','effective_samples','valid','neff_pass','g','B','mu','status','tau0'}
def load_selected(path, members, allowed):
if not set(members) <= allowed: raise ValueError('Array outside explicit whitelist')
with ZipFile(path) as z:
return {name:np.load(z.open(name+'.npy'),allow_pickle=False) for name in members}
def scales_and_draw(d, mass, raw_ids, prefix, existing=None):
order=np.argsort(d,kind='stable')
median=float(d[order[np.searchsorted(np.cumsum(mass[order]),mass.sum()/2)]])
if not np.isfinite(median) or median<=0:raise ValueError('Nonpositive full-node weighted median; no substitute radius')
candidates=np.flatnonzero(d>0)
if existing:
with np.load(existing,allow_pickle=False) as z:
draws=z['draw_ids'];ids=z['unique_ids'];counts=z['multiplicity'];inverse=z['draw_to_unique']
assert np.array_equal(z['draw_raw_ids'],raw_ids[draws])
else:
rng=np.random.Generator(np.random.PCG64(np.random.SeedSequence([2026111400,prefix])))
draws=rng.choice(candidates,size=2000,replace=True,p=mass[candidates]/mass[candidates].sum(dtype=np.float64))
ids,inverse,counts=np.unique(draws,return_inverse=True,return_counts=True)
assert len(draws)==2000 and np.all(d[draws]>0) and np.array_equal(ids[inverse],draws) and counts.sum()==2000
return dict(draw_ids=draws,draw_raw_ids=raw_ids[draws],unique_ids=ids,unique_raw_ids=raw_ids[ids],
multiplicity=counts,draw_to_unique=inverse),dict(t_part=4*median,radius=10*median,
positive_mass_fraction=float(mass[d>0].sum()/mass.sum()),weighted_median_distance=median)
def edge_summary(x, edges, ids, spectral, length_scale):
lookup=np.full(len(x),-1,np.int64);lookup[ids]=np.arange(len(ids))
edge_idx,side=np.nonzero(lookup[edges]>=0);local=lookup[edges[edge_idx,side]]
n=len(local);q=np.empty((n,2));valid=np.empty(n,bool);direction=np.empty(n,bool)
for start in range(0,n,4096):
stop=min(start+4096,n);e=edges[edge_idx[start:stop]];j=local[start:stop]
result=rep.contract_direction(spectral['B'][j],spectral['mu'][j],spectral['valid'][j],
x[e[:,1]]-x[e[:,0]],length_scale,'edge')
q[start:stop]=result['q'];valid[start:stop]=result['channel_valid'];direction[start:stop]=result['direction_valid']
accepted=valid&np.isfinite(q).all(1)
degree=np.bincount(local,minlength=len(ids));good_degree=np.bincount(local[accepted],minlength=len(ids))
total=np.zeros((len(ids),2));maximum=np.full((len(ids),2),-np.inf)
np.add.at(total,local[accepted],q[accepted]);np.maximum.at(maximum,local[accepted],q[accepted])
mean=np.full_like(total,np.nan);has=good_degree>0;mean[has]=total[has]/good_degree[has,None];maximum[~has]=np.nan
summary=np.column_stack([mean[:,0],maximum[:,0],mean[:,1],maximum[:,1]])
complete=(degree==np.bincount(edges.ravel(),minlength=len(x))[ids])&has&np.isfinite(summary).all(1)
return summary,complete,dict(edge_indices=edge_idx,endpoint_side=side,anchor_local=local,
q=q,channel_valid=valid,direction_valid=direction),degree,good_degree
def s2_eligibility(distance, spectral, complete, summary):
# Gate q is intentionally absent; original gate geometry remains in outputs.
return (distance>0)&spectral['valid']&spectral['neff_pass']&complete&np.isfinite(spectral['g']).all(1)&np.isfinite(summary).all(1)
def run(root,index):
c=json.loads((root/'contract.json').read_text());spec=c['cases'][index];case=spec['case']
dest=root/'parts'/case;dest.mkdir(parents=True,exist_ok=False)
start=time.monotonic();tick=start;timings={};stage='verify_inputs'
def progress(name,**kw):
nonlocal stage
stage=name;save(dest/'progress.json',dict(stage=name,elapsed_seconds=time.monotonic()-start,**kw))
def done(name):
nonlocal tick
now=time.monotonic();timings[name]=now-tick;tick=now
try:
for p,h in c['code_sha256'].items():assert sha(p)==h,p
for p,h in spec['input_sha256'].items():assert sha(p)==h,p
progress('geometry')
with np.load(spec['g1_geometry'],allow_pickle=False) as z:
cached_x=z['points'];faces=z['boundary'];center=z['center'];rg=float(z['rg'])
if spec.get('legacy_geometry'):
data=load_selected(spec['legacy_geometry'],sorted(GEOMETRY_MEMBERS),GEOMETRY_MEMBERS)
x=data['points_raw'];tet=data['tetra'];mass=data['node_volume_raw'];edges=data['edges'];gate=data['gate_location_raw']
raw_ids=np.arange(len(x));assert np.array_equal(data['boundary_triangles'],faces)
geometry_mode='existing_cache_explicit_geometry_members_only'
else:
ver=c['archive_stat'];st=Path(c['archive']).stat()
assert (st.st_size,st.st_mtime_ns,st.st_ino)==(ver['bytes'],ver['mtime_ns'],ver['inode'])
with ZipFile(c['archive']) as z:
with z.open(case+'_sim.vtu') as stream:raw_x,raw_tet=read_tets(stream)
gate=np.asarray(yaml.safe_load(z.read(case+'.yml'))['inlet_location'],dtype=float)
raw_ids=np.unique(raw_tet);x=raw_x[raw_ids];tet=np.searchsorted(raw_ids,raw_tet)
del raw_x,raw_tet
_,_,mat,vol,mass,edges,lengths=features.mesh_geometry(x,tet)
del mat,vol,lengths
geometry_mode='missing_tet_topology_from_geometry_prefix_existing_g1_boundary_reused'
assert np.array_equal(x,cached_x);del cached_x
assert len(x)==spec['tet_nodes'] and len(tet)==spec['tet_cells']
assert np.isfinite(mass).all() and (mass>0).all() and np.isfinite(gate).all()
center_from_mass=np.sum(mass[:,None]*x,axis=0)/mass.sum()
rg_from_mass=np.sqrt(np.sum(mass*np.sum((x-center_from_mass)**2,axis=1))/mass.sum())
assert np.allclose(center,center_from_mass,rtol=1e-10,atol=1e-10) and np.isclose(rg,rg_from_mass,rtol=1e-10)
np.savez_compressed(dest/'geometry.npz',points_raw=x,tetra=tet,node_volume_raw=mass,edges=edges,
boundary_triangles=faces,raw_node_ids=raw_ids,gate_location_raw=gate)
done('geometry_load_topology_write')
progress('wall_distance')
boundary=np.zeros(len(x),bool);boundary[np.unique(faces)]=True
if spec.get('existing_distance'):
d=np.load(spec['existing_distance'],allow_pickle=False);distance_mode='existing_verified_distance';brute_error=None
else:
d=np.zeros(len(x));interior=np.flatnonzero(~boundary);surface=SurfaceDistance(x[faces])
for begin in range(0,len(interior),128):
ids=interior[begin:begin+128];d[ids]=surface.query(x[ids])
progress('wall_distance',done=min(begin+128,len(interior)),total=len(interior))
probe=np.random.default_rng(1729).choice(len(x),min(30,len(x)),replace=False)
brute=np.array([np.sqrt(triangle_distance_squared(q,x[faces]).min()) for q in x[probe]])
brute_error=float(abs(brute-d[probe]).max());assert brute_error<=1e-10*max(1.,np.ptp(x,axis=0).max())
del surface
distance_mode='new_missing_distance_exact_triangle_with_30_brute_checks'
assert np.isfinite(d).all() and (d>=0).all() and np.array_equal(d==0,boundary)
np.save(dest/'boundary_distance_raw.npy',d);done('wall_distance_load_or_compute_and_write')
progress('fixed_draw')
draw,scales=scales_and_draw(d,mass,raw_ids,spec['numeric_prefix'],spec.get('existing_primary_draw'))
ids=draw['unique_ids'];counts=draw['multiplicity'];np.savez_compressed(dest/'draw.npz',**draw);done('draw_and_scales')
progress('base_features')
geo,gate_node=features.graph_distances(x,tet,d,gate,exclude_wall_edges=False)
base=features.base_features(x,d,geo,gate,center,rg,float(mass.sum()),scales['t_part'],spec['t_set'])
np.save(dest/'base.npy',base);del base,geo;done('unrestricted_geodesic_base_write')
progress('o2')
if spec.get('existing_o2_nodes'):
old=json.loads(Path(spec['existing_o2_result']).read_text())
assert old['acceptance_status']=='PASS' and old['pose']=='original' and old['nodes']==len(x)
assert np.isclose(old['radius'],scales['radius'],rtol=0,atol=1e-13)
loaded=load_selected(spec['existing_o2_nodes'],sorted(O2_MEMBERS),O2_MEMBERS)
spectral={k:v[ids] for k,v in loaded.items()};del loaded
o2_mode='existing_full_original_O2_v1.1a_subset_no_reintegration';visits=None;raw_meta=None
else:
raw,raw_meta=e6.raw_field(x,tet,d);sx,sm,st=e6.samples(x,tet,mass,'tetra_four_point',raw);del raw
visits=0;real_tree=e6.cKDTree
class ProgressTree:
def __init__(self,*args,**kwargs):self.tree=real_tree(*args,**kwargs);self.count=0
def query_ball_point(self,*args,**kwargs):
nonlocal visits
result=self.tree.query_ball_point(*args,**kwargs);visits+=len(result);self.count+=1
if self.count%32==0:progress('o2_kernel',anchors_done=self.count,anchors_total=len(ids),neighbor_visits=visits)
return result
e6.cKDTree=ProgressTree
try:a=e6.integrate(x[ids],sx,sm,st,scales['radius'])
finally:e6.cKDTree=real_tree
del sx,sm,st
spectral=dict(**a,**rep.readout(a['tensor'],a['kernel_mass'],d[ids]>0,a['effective_samples']))
checks=rep.acceptance(spectral)
if checks['valid_count']:
assert checks['mu_sum_error']<=1e-10 and checks['mu_min']>=-1e-10 and checks['ordering_violation']<=1e-10
assert checks['B_symmetry_error']<=1e-10 and checks['B_min_eigenvalue']>=-1e-10 and checks['reconstruction_max']<=1e-10
o2_mode='fixed_full_source_four_point_kernel_at_unique_drawn_anchors'
done('o2_raw_source_kernel_readout_or_cache')
progress('edge_queries_and_s2_eligibility')
length_scale=float(np.median(np.linalg.norm(x[edges[:,1]]-x[edges[:,0]],axis=1)))
summary,complete,edge_data,degree,good_degree=edge_summary(x,edges,ids,spectral,length_scale)
eligible=s2_eligibility(d[ids],spectral,complete,summary)
rejection_masks=dict(nonpositive_distance=d[ids]<=0,spectral_invalid=~spectral['valid'],
neff_failed=~spectral['neff_pass'],edge_incomplete=~complete,
nonfinite_query=~(np.isfinite(spectral['g']).all(1)&np.isfinite(summary).all(1)))
rejections={name:dict(unique=int(mask.sum()),slots=int(counts[mask].sum())) for name,mask in rejection_masks.items()}
np.savez_compressed(dest/'queries.npz',anchor_ids=ids,raw_node_ids=raw_ids[ids],multiplicity=counts,
distance=d[ids],node_mass=mass[ids],g=spectral['g'],edge_summary=summary,eligible_s2=eligible,
spectral_valid=spectral['valid'],neff_pass=spectral['neff_pass'],effective_samples=spectral['effective_samples'],
edge_block_complete=complete,edge_count=degree,valid_edge_count=good_degree,near_wall=d[ids]/scales['t_part']<=.25)
np.savez_compressed(dest/'o2-evidence.npz',**spectral)
np.savez_compressed(dest/'edge-evidence.npz',**edge_data)
del spectral,edge_data
done('edge_queries_eligibility_write')
progress('r2_features')
r2_error=None
try:
r2=features.r2_features(x,tet,d,gate,spec['t_set'])
np.savez_compressed(dest/'r2.npz',features=r2['features'],Di=r2['Di'],Df=r2['Df'],Vf=r2['Vf'],S=r2['S'])
r2_meta=dict(columns=list(r2['columns']),radius=r2['radius'],gate_node=r2['gate_node']);del r2
except (ValueError,np.linalg.LinAlgError) as exc:
r2_error=dict(type=type(exc).__name__,message=str(exc),traceback=traceback.format_exc());r2_meta=None
save(dest/'r2-failure.json',r2_error)
done('r2_features_and_write')
k=int(counts[eligible].sum());h=scales['positive_mass_fraction']
result=dict(case=case,status='COMPLETE' if r2_error is None else 'PARTIAL_R2_FAILED',nodes=len(x),tets=len(tet),
geometry_mode=geometry_mode,distance_mode=distance_mode,distance_brute_max_abs_error=brute_error,
o2_mode=o2_mode,raw_field=raw_meta,neighbor_visits=visits,scales=dict(**scales,Rg=rg,volume=float(mass.sum()),L_med=length_scale,t_set=spec['t_set']),
draw_slots=2000,unique_drawn=len(ids),eligible_unique=int(eligible.sum()),eligible_slots=k,conditional_coverage=k/2000,
full_lumped_coverage=h*k/2000,boundary_mass_fraction=1-h,eligible_near_wall_slots=int(counts[eligible&(d[ids]/scales['t_part']<=.25)].sum()),
rejection_counts_overlapping=rejections,total_ineligible_unique=int((~eligible).sum()),
gate_q_used_for_eligibility=False,eligibility_version='S2-v1-no-gate-q',base_columns=list(features.BASE_COLUMNS),
base_normalization='NOT_FITTED; future fold/pool learning only',base_gate_node=gate_node,r2=r2_meta,r2_error=r2_error,
label_arrays_decoded=[],model_fit=False,gpu=False,timings=timings,wall_seconds=time.monotonic()-start,
cpu_seconds=sum(resource.getrusage(resource.RUSAGE_SELF)[:2]),peak_rss_bytes=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024,
output_sha256={p.name:sha(p) for p in sorted(dest.iterdir()) if p.is_file() and p.name!='progress.json'})
save(dest/'result.json',result);progress('complete' if not r2_error else 'partial_r2_failed')
print(json.dumps({k:result[k] for k in ['case','status','wall_seconds','peak_rss_bytes','eligible_slots','r2_error']}),flush=True)
return 0 if r2_error is None else 2
except Exception as exc:
save(dest/'failure.json',dict(stage=stage,error_type=type(exc).__name__,message=str(exc),traceback=traceback.format_exc(),timings=timings,wall_seconds=time.monotonic()-start))
raise
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int);a=p.parse_args()
raise SystemExit(run(a.root,a.index))
+232
View File
@@ -0,0 +1,232 @@
"""Pilot preparation adapter v2: additionally reuse S1 primary sampled O2 caches.
Based on s2_preprocess.py v1; its ten-part execution remains immutable.
"""
import argparse
import json
from pathlib import Path
import resource
import time
import traceback
from zipfile import ZipFile
import numpy as np
import yaml
from boundary_distance import SurfaceDistance, triangle_distance_squared
from g1_candidate_fingerprints import read_tets
import run_e6_real_kernel as e6
import o2_rep as rep
from s2_models import features
from s2_e1_supervisor import save
from freeze_g1_components import sha
GEOMETRY_MEMBERS = {'points_raw','tetra','node_volume_raw','boundary_triangles','edges','gate_location_raw','raw_node_ids'}
O2_MEMBERS = {'tensor','kernel_mass','effective_samples','valid','neff_pass','g','B','mu','status','tau0'}
def load_selected(path, members, allowed):
if not set(members) <= allowed: raise ValueError('Array outside explicit whitelist')
with ZipFile(path) as z:
return {name:np.load(z.open(name+'.npy'),allow_pickle=False) for name in members}
def scales_and_draw(d, mass, raw_ids, prefix, existing=None):
order=np.argsort(d,kind='stable')
median=float(d[order[np.searchsorted(np.cumsum(mass[order]),mass.sum()/2)]])
if not np.isfinite(median) or median<=0:raise ValueError('Nonpositive full-node weighted median; no substitute radius')
candidates=np.flatnonzero(d>0)
if existing:
with np.load(existing,allow_pickle=False) as z:
draws=z['draw_ids'];ids=z['unique_ids'];counts=z['multiplicity'];inverse=z['draw_to_unique']
assert np.array_equal(z['draw_raw_ids'],raw_ids[draws])
else:
rng=np.random.Generator(np.random.PCG64(np.random.SeedSequence([2026111400,prefix])))
draws=rng.choice(candidates,size=2000,replace=True,p=mass[candidates]/mass[candidates].sum(dtype=np.float64))
ids,inverse,counts=np.unique(draws,return_inverse=True,return_counts=True)
assert len(draws)==2000 and np.all(d[draws]>0) and np.array_equal(ids[inverse],draws) and counts.sum()==2000
return dict(draw_ids=draws,draw_raw_ids=raw_ids[draws],unique_ids=ids,unique_raw_ids=raw_ids[ids],
multiplicity=counts,draw_to_unique=inverse),dict(t_part=4*median,radius=10*median,
positive_mass_fraction=float(mass[d>0].sum()/mass.sum()),weighted_median_distance=median)
def edge_summary(x, edges, ids, spectral, length_scale):
lookup=np.full(len(x),-1,np.int64);lookup[ids]=np.arange(len(ids))
edge_idx,side=np.nonzero(lookup[edges]>=0);local=lookup[edges[edge_idx,side]]
n=len(local);q=np.empty((n,2));valid=np.empty(n,bool);direction=np.empty(n,bool)
for start in range(0,n,4096):
stop=min(start+4096,n);e=edges[edge_idx[start:stop]];j=local[start:stop]
result=rep.contract_direction(spectral['B'][j],spectral['mu'][j],spectral['valid'][j],
x[e[:,1]]-x[e[:,0]],length_scale,'edge')
q[start:stop]=result['q'];valid[start:stop]=result['channel_valid'];direction[start:stop]=result['direction_valid']
accepted=valid&np.isfinite(q).all(1)
degree=np.bincount(local,minlength=len(ids));good_degree=np.bincount(local[accepted],minlength=len(ids))
total=np.zeros((len(ids),2));maximum=np.full((len(ids),2),-np.inf)
np.add.at(total,local[accepted],q[accepted]);np.maximum.at(maximum,local[accepted],q[accepted])
mean=np.full_like(total,np.nan);has=good_degree>0;mean[has]=total[has]/good_degree[has,None];maximum[~has]=np.nan
summary=np.column_stack([mean[:,0],maximum[:,0],mean[:,1],maximum[:,1]])
complete=(degree==np.bincount(edges.ravel(),minlength=len(x))[ids])&has&np.isfinite(summary).all(1)
return summary,complete,dict(edge_indices=edge_idx,endpoint_side=side,anchor_local=local,
q=q,channel_valid=valid,direction_valid=direction),degree,good_degree
def s2_eligibility(distance, spectral, complete, summary):
# Gate q is intentionally absent; original gate geometry remains in outputs.
return (distance>0)&spectral['valid']&spectral['neff_pass']&complete&np.isfinite(spectral['g']).all(1)&np.isfinite(summary).all(1)
def load_sampled_o2(spec,ids,counts,raw_ids,radius,nodes):
old=json.loads(Path(spec['existing_sampled_o2_result']).read_text())
assert old['status']=='COMPLETE' and old['pose']=='original' and old['full_nodes']==nodes
assert np.isclose(old['radius'],radius,rtol=0,atol=1e-13)
with np.load(spec['existing_sampled_o2_nodes'],allow_pickle=False) as z:
assert np.array_equal(z['anchor_ids'],ids) and np.array_equal(z['multiplicity'],counts)
assert np.array_equal(z['raw_node_ids'],raw_ids[ids])
return load_selected(spec['existing_sampled_o2_nodes'],sorted(O2_MEMBERS),O2_MEMBERS)
def run(root,index):
c=json.loads((root/'contract.json').read_text());spec=c['cases'][index];case=spec['case']
dest=root/'parts'/case;dest.mkdir(parents=True,exist_ok=False)
start=time.monotonic();tick=start;timings={};stage='verify_inputs'
def progress(name,**kw):
nonlocal stage
stage=name;save(dest/'progress.json',dict(stage=name,elapsed_seconds=time.monotonic()-start,**kw))
def done(name):
nonlocal tick
now=time.monotonic();timings[name]=now-tick;tick=now
try:
for p,h in c['code_sha256'].items():assert sha(p)==h,p
for p,h in spec['input_sha256'].items():assert sha(p)==h,p
progress('geometry')
with np.load(spec['g1_geometry'],allow_pickle=False) as z:
cached_x=z['points'];faces=z['boundary'];center=z['center'];rg=float(z['rg'])
if spec.get('legacy_geometry'):
with ZipFile(spec['legacy_geometry']) as z: has_raw_ids='raw_node_ids.npy' in z.namelist()
members=GEOMETRY_MEMBERS if has_raw_ids else GEOMETRY_MEMBERS-{'raw_node_ids'}
data=load_selected(spec['legacy_geometry'],sorted(members),GEOMETRY_MEMBERS)
x=data['points_raw'];tet=data['tetra'];mass=data['node_volume_raw'];edges=data['edges'];gate=data['gate_location_raw']
raw_ids=data.get('raw_node_ids',np.arange(len(x)));assert np.array_equal(data['boundary_triangles'],faces)
geometry_mode='existing_cache_explicit_geometry_members_only'
else:
ver=c['archive_stat'];st=Path(c['archive']).stat()
assert (st.st_size,st.st_mtime_ns,st.st_ino)==(ver['bytes'],ver['mtime_ns'],ver['inode'])
with ZipFile(c['archive']) as z:
with z.open(case+'_sim.vtu') as stream:raw_x,raw_tet=read_tets(stream)
gate=np.asarray(yaml.safe_load(z.read(case+'.yml'))['inlet_location'],dtype=float)
raw_ids=np.unique(raw_tet);x=raw_x[raw_ids];tet=np.searchsorted(raw_ids,raw_tet)
del raw_x,raw_tet
_,_,mat,vol,mass,edges,lengths=features.mesh_geometry(x,tet)
del mat,vol,lengths
geometry_mode='missing_tet_topology_from_geometry_prefix_existing_g1_boundary_reused'
assert np.array_equal(x,cached_x);del cached_x
assert len(x)==spec['tet_nodes'] and len(tet)==spec['tet_cells']
assert np.isfinite(mass).all() and (mass>0).all() and np.isfinite(gate).all()
center_from_mass=np.sum(mass[:,None]*x,axis=0)/mass.sum()
rg_from_mass=np.sqrt(np.sum(mass*np.sum((x-center_from_mass)**2,axis=1))/mass.sum())
assert np.allclose(center,center_from_mass,rtol=1e-10,atol=1e-10) and np.isclose(rg,rg_from_mass,rtol=1e-10)
np.savez_compressed(dest/'geometry.npz',points_raw=x,tetra=tet,node_volume_raw=mass,edges=edges,
boundary_triangles=faces,raw_node_ids=raw_ids,gate_location_raw=gate)
done('geometry_load_topology_write')
progress('wall_distance')
boundary=np.zeros(len(x),bool);boundary[np.unique(faces)]=True
if spec.get('existing_distance'):
d=np.load(spec['existing_distance'],allow_pickle=False);distance_mode='existing_verified_distance';brute_error=None
else:
d=np.zeros(len(x));interior=np.flatnonzero(~boundary);surface=SurfaceDistance(x[faces])
for begin in range(0,len(interior),128):
ids=interior[begin:begin+128];d[ids]=surface.query(x[ids])
progress('wall_distance',done=min(begin+128,len(interior)),total=len(interior))
probe=np.random.default_rng(1729).choice(len(x),min(30,len(x)),replace=False)
brute=np.array([np.sqrt(triangle_distance_squared(q,x[faces]).min()) for q in x[probe]])
brute_error=float(abs(brute-d[probe]).max());assert brute_error<=1e-10*max(1.,np.ptp(x,axis=0).max())
del surface
distance_mode='new_missing_distance_exact_triangle_with_30_brute_checks'
assert np.isfinite(d).all() and (d>=0).all() and np.array_equal(d==0,boundary)
np.save(dest/'boundary_distance_raw.npy',d);done('wall_distance_load_or_compute_and_write')
progress('fixed_draw')
draw,scales=scales_and_draw(d,mass,raw_ids,spec['numeric_prefix'],spec.get('existing_primary_draw'))
ids=draw['unique_ids'];counts=draw['multiplicity'];np.savez_compressed(dest/'draw.npz',**draw);done('draw_and_scales')
progress('base_features')
geo,gate_node=features.graph_distances(x,tet,d,gate,exclude_wall_edges=False)
base=features.base_features(x,d,geo,gate,center,rg,float(mass.sum()),scales['t_part'],spec['t_set'])
np.save(dest/'base.npy',base);del base,geo;done('unrestricted_geodesic_base_write')
progress('o2')
if spec.get('existing_sampled_o2_nodes'):
spectral=load_sampled_o2(spec,ids,counts,raw_ids,scales['radius'],len(x))
o2_mode='existing_S1_primary_sampled_spectrum_no_reintegration';visits=None;raw_meta=None
elif spec.get('existing_o2_nodes'):
old=json.loads(Path(spec['existing_o2_result']).read_text())
assert old['acceptance_status']=='PASS' and old['pose']=='original' and old['nodes']==len(x)
assert np.isclose(old['radius'],scales['radius'],rtol=0,atol=1e-13)
loaded=load_selected(spec['existing_o2_nodes'],sorted(O2_MEMBERS),O2_MEMBERS)
spectral={k:v[ids] for k,v in loaded.items()};del loaded
o2_mode='existing_full_original_O2_v1.1a_subset_no_reintegration';visits=None;raw_meta=None
else:
raw,raw_meta=e6.raw_field(x,tet,d);sx,sm,st=e6.samples(x,tet,mass,'tetra_four_point',raw);del raw
visits=0;real_tree=e6.cKDTree
class ProgressTree:
def __init__(self,*args,**kwargs):self.tree=real_tree(*args,**kwargs);self.count=0
def query_ball_point(self,*args,**kwargs):
nonlocal visits
result=self.tree.query_ball_point(*args,**kwargs);visits+=len(result);self.count+=1
if self.count%32==0:progress('o2_kernel',anchors_done=self.count,anchors_total=len(ids),neighbor_visits=visits)
return result
e6.cKDTree=ProgressTree
try:a=e6.integrate(x[ids],sx,sm,st,scales['radius'])
finally:e6.cKDTree=real_tree
del sx,sm,st
spectral=dict(**a,**rep.readout(a['tensor'],a['kernel_mass'],d[ids]>0,a['effective_samples']))
checks=rep.acceptance(spectral)
if checks['valid_count']:
assert checks['mu_sum_error']<=1e-10 and checks['mu_min']>=-1e-10 and checks['ordering_violation']<=1e-10
assert checks['B_symmetry_error']<=1e-10 and checks['B_min_eigenvalue']>=-1e-10 and checks['reconstruction_max']<=1e-10
o2_mode='fixed_full_source_four_point_kernel_at_unique_drawn_anchors'
done('o2_raw_source_kernel_readout_or_cache')
progress('edge_queries_and_s2_eligibility')
length_scale=float(np.median(np.linalg.norm(x[edges[:,1]]-x[edges[:,0]],axis=1)))
summary,complete,edge_data,degree,good_degree=edge_summary(x,edges,ids,spectral,length_scale)
eligible=s2_eligibility(d[ids],spectral,complete,summary)
rejection_masks=dict(nonpositive_distance=d[ids]<=0,spectral_invalid=~spectral['valid'],
neff_failed=~spectral['neff_pass'],edge_incomplete=~complete,
nonfinite_query=~(np.isfinite(spectral['g']).all(1)&np.isfinite(summary).all(1)))
rejections={name:dict(unique=int(mask.sum()),slots=int(counts[mask].sum())) for name,mask in rejection_masks.items()}
np.savez_compressed(dest/'queries.npz',anchor_ids=ids,raw_node_ids=raw_ids[ids],multiplicity=counts,
distance=d[ids],node_mass=mass[ids],g=spectral['g'],edge_summary=summary,eligible_s2=eligible,
spectral_valid=spectral['valid'],neff_pass=spectral['neff_pass'],effective_samples=spectral['effective_samples'],
edge_block_complete=complete,edge_count=degree,valid_edge_count=good_degree,near_wall=d[ids]/scales['t_part']<=.25)
np.savez_compressed(dest/'o2-evidence.npz',**spectral)
np.savez_compressed(dest/'edge-evidence.npz',**edge_data)
del spectral,edge_data
done('edge_queries_eligibility_write')
progress('r2_features')
r2_error=None
try:
r2=features.r2_features(x,tet,d,gate,spec['t_set'])
np.savez_compressed(dest/'r2.npz',features=r2['features'],Di=r2['Di'],Df=r2['Df'],Vf=r2['Vf'],S=r2['S'])
r2_meta=dict(columns=list(r2['columns']),radius=r2['radius'],gate_node=r2['gate_node']);del r2
except (ValueError,np.linalg.LinAlgError) as exc:
r2_error=dict(type=type(exc).__name__,message=str(exc),traceback=traceback.format_exc());r2_meta=None
save(dest/'r2-failure.json',r2_error)
done('r2_features_and_write')
k=int(counts[eligible].sum());h=scales['positive_mass_fraction']
result=dict(case=case,status='COMPLETE' if r2_error is None else 'PARTIAL_R2_FAILED',nodes=len(x),tets=len(tet),
geometry_mode=geometry_mode,distance_mode=distance_mode,distance_brute_max_abs_error=brute_error,
o2_mode=o2_mode,raw_field=raw_meta,neighbor_visits=visits,scales=dict(**scales,Rg=rg,volume=float(mass.sum()),L_med=length_scale,t_set=spec['t_set']),
draw_slots=2000,unique_drawn=len(ids),eligible_unique=int(eligible.sum()),eligible_slots=k,conditional_coverage=k/2000,
full_lumped_coverage=h*k/2000,boundary_mass_fraction=1-h,eligible_near_wall_slots=int(counts[eligible&(d[ids]/scales['t_part']<=.25)].sum()),
rejection_counts_overlapping=rejections,total_ineligible_unique=int((~eligible).sum()),
gate_q_used_for_eligibility=False,eligibility_version='S2-v1-no-gate-q',base_columns=list(features.BASE_COLUMNS),
base_normalization='NOT_FITTED; future fold/pool learning only',base_gate_node=gate_node,r2=r2_meta,r2_error=r2_error,
label_arrays_decoded=[],model_fit=False,gpu=False,timings=timings,wall_seconds=time.monotonic()-start,
cpu_seconds=sum(resource.getrusage(resource.RUSAGE_SELF)[:2]),peak_rss_bytes=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss*1024,
output_sha256={p.name:sha(p) for p in sorted(dest.iterdir()) if p.is_file() and p.name!='progress.json'})
save(dest/'result.json',result);progress('complete' if not r2_error else 'partial_r2_failed')
print(json.dumps({k:result[k] for k in ['case','status','wall_seconds','peak_rss_bytes','eligible_slots','r2_error']}),flush=True)
return 0 if r2_error is None else 2
except Exception as exc:
save(dest/'failure.json',dict(stage=stage,error_type=type(exc).__name__,message=str(exc),traceback=traceback.format_exc(),timings=timings,wall_seconds=time.monotonic()-start))
raise
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int);a=p.parse_args()
raise SystemExit(run(a.root,a.index))
@@ -0,0 +1,74 @@
"""Verify measured artifacts and report costs; no recomputation or model fitting."""
import csv
import json
from pathlib import Path
import numpy as np
from freeze_g1_components import sha
from s2_e1_supervisor import disk_usage
ROOT=Path('artifacts/s2-ten-part-measurement-2026-11-15')
def main():
batch=json.loads((ROOT/'batch-complete.json').read_text())
contract=json.loads((ROOT/'contract.json').read_text())
public=ROOT/'public';rows=[];checks=[]
for attempt in batch['attempts']:
i=attempt['index'];spec=contract['cases'][i];dest=ROOT/'parts'/spec['case']
row={k:attempt[k] for k in ['index','case','status','stop_reason','cpu_seconds','wall_seconds','peak_tree_rss_bytes']}
row.update(nodes=spec['tet_nodes'],tets=spec['tet_cells'],tertile=spec['tertile'],output_disk_bytes=disk_usage(dest))
if (dest/'result.json').exists():
r=json.loads((dest/'result.json').read_text())
for name,h in r['output_sha256'].items():assert sha(dest/name)==h
row.update(preparation_status=r['status'],eligible_unique=r['eligible_unique'],eligible_slots=r['eligible_slots'],
conditional_coverage=r['conditional_coverage'],full_lumped_coverage=r['full_lumped_coverage'],
distance_mode=r['distance_mode'],o2_mode=r['o2_mode'],r2_error=r['r2_error'],timings=r['timings'],result_sha256=sha(dest/'result.json'))
base=np.load(dest/'base.npy',allow_pickle=False,mmap_mode='r')
assert base.shape==(r['nodes'],10) and np.isfinite(base).all()
with np.load(dest/'queries.npz',allow_pickle=False) as q:
ids=q['anchor_ids'];counts=q['multiplicity'];eligible=q['eligible_s2']
assert np.all(np.diff(ids)>0) and counts.sum()==2000 and np.all(counts>0)
expected=(q['distance']>0)&q['spectral_valid']&q['neff_pass']&q['edge_block_complete']&np.isfinite(q['g']).all(1)&np.isfinite(q['edge_summary']).all(1)
assert np.array_equal(eligible,expected)
assert int(counts[eligible].sum())==r['eligible_slots']
with np.load(dest/'draw.npz',allow_pickle=False) as draw:
assert np.array_equal(ids[draw['draw_to_unique']],draw['draw_ids'])
assert np.array_equal(counts,draw['multiplicity'])
if r['r2_error'] is None:
with np.load(dest/'r2.npz',allow_pickle=False) as z:assert z['features'].shape==(r['nodes'],20) and np.isfinite(z['features']).all()
assert r['label_arrays_decoded']==[] and not r['model_fit'] and not r['gpu'] and not r['gate_q_used_for_eligibility']
checks.append(dict(case=spec['case'],status='PASS',checks=['output hashes','base shape/finiteness','draw identity/multiplicity','S2 common eligibility/coverage','R2 shape/finiteness if completed','no labels/fit/GPU recorded']))
elif (dest/'failure.json').exists():row['failure']=json.loads((dest/'failure.json').read_text())
rows.append(row)
for p,h in contract['code_sha256'].items():assert sha(p)==h,p
parts=json.loads(Path('artifacts/s2-g1-components-2026-11-15/custodian/part-splits.json').read_text())
non_test=[p for p in parts if p['assignments'][0]['role']!='test']
requested={p['case'] for p in non_test};completed={r['case'] for r in rows if r.get('preparation_status')=='COMPLETE' and r['status']=='PASS'}
pilot=dict(proposed_fixed_fold=0,proposed_seed=0,learning_parts=387,validation_parts=112,sealed_test_parts=125,
non_test_parts=len(non_test),non_test_nodes=sum(p['nodes'] for p in non_test),
prepared_from_this_ten_part_stage=len(requested&completed),remaining_after_this_stage=len(requested-completed),
train_loop_and_GPU_supervisor='NOT_YET_ACCEPTED',labels_read=False,pilot_started=False)
# Transparent planning scenarios, not acceptance bounds: compute total roster
# costs using rates from nine newly prepared parts, excluding cached 666.
cold=[r for r in rows if r['case'] in completed and not r['case'].startswith('00000666')]
if cold:
rates=np.array([r['cpu_seconds']/r['nodes'] for r in cold]);bytes_per_node=np.array([r['output_disk_bytes']/r['nodes'] for r in rows if r['case'] in completed])
missing_nodes=sum(p['nodes'] for p in non_test if p['case'] not in completed)
pilot['missing_non_test_nodes']=missing_nodes
pilot['linear_node_rate_scenarios']={name:dict(cpu_hours=float(np.quantile(rates,q)*missing_nodes/3600),additional_gib=float(np.quantile(bytes_per_node,q)*missing_nodes/2**30)) for name,q in [('median',.5),('p95',.95),('max',1)]}
pilot['scenario_limit']='Shape/neighbourhood-dependent runtime; nine new cases, cache reuse and cold largest-part costs differ; these are not guarantees or authority to exceed caps'
result=dict(status='MEASUREMENT_RECORDED_AWAITING_REVIEW',contract_sha256=sha(ROOT/'contract.json'),batch_sha256=sha(ROOT/'batch-complete.json'),
attempted=len(rows),complete=sum(r.get('preparation_status')=='COMPLETE' and r['status']=='PASS' for r in rows),
failed_or_partial=sum(r.get('preparation_status')!='COMPLETE' or r['status']!='PASS' for r in rows),not_run=batch['not_run'],
worker_cpu_seconds=batch['worker_cpu_seconds'],supervisor_cpu_seconds=batch['supervisor_cpu_seconds'],wall_seconds=batch['wall_seconds'],
peak_worker_tree_rss_bytes=batch['peak_worker_tree_rss_bytes'],disk_bytes_before_publication=disk_usage(ROOT),
checks=checks,rows=rows,pilot_readiness=pilot,
no_repeated_G1_geometry_registration_review=True,no_labels_fit_gpu=True,code_sha256=sha(__file__))
with (public/'measurement-result.json').open('x') as f:json.dump(result,f,ensure_ascii=False,indent=2);f.write('\n')
with (public/'cost-table.csv').open('x') as f:
fields=['index','case','nodes','tets','tertile','status','preparation_status','cpu_seconds','wall_seconds','peak_tree_rss_bytes','output_disk_bytes','eligible_slots','distance_mode','o2_mode']
w=csv.DictWriter(f,fields,extrasaction='ignore');w.writeheader();w.writerows(rows)
print(json.dumps({k:v for k,v in result.items() if k not in ['checks','rows']},ensure_ascii=False))
if __name__=='__main__':main()
+37
View File
@@ -0,0 +1,37 @@
import unittest
import tempfile
from pathlib import Path
from zipfile import ZipFile
from unittest.mock import patch
import numpy as np
import s2_pilot_data as d
class PilotSealTests(unittest.TestCase):
def setUp(self):self.manifest=dict(learning_parts=['a'],validation_parts=['b'],sealed_test_parts=['c'])
def test_test_unknown_and_conflicting_role_fail_before_io(self):
with patch.object(d,'ZipFile') as opener:
for case in ['c','unknown']:
with self.assertRaises(PermissionError):d.read_non_test_fill_time('not-opened.zip',case,np.arange(3),self.manifest)
conflicting=dict(self.manifest,learning_parts=['a','c'])
with self.assertRaises(PermissionError):d.read_non_test_fill_time('not-opened.zip','c',np.arange(3),conflicting)
opener.assert_not_called()
def test_roles(self):
self.assertEqual(d.permitted_role(self.manifest,'a'),'learning');self.assertEqual(d.permitted_role(self.manifest,'b'),'validation')
def test_only_named_point_field_decoded_and_raw_ids_preserved(self):
xml=b'<VTKFile byte_order="LittleEndian" compressor="vtkZLibDataCompressor"><UnstructuredGrid><Piece NumberOfPoints="4"><CellData><DataArray Name="Fill_time">skip</DataArray></CellData><PointData><DataArray Name="Pressure">skip</DataArray><DataArray Name="Fill_time">chosen</DataArray></PointData></Piece></UnstructuredGrid></VTKFile>'
with tempfile.TemporaryDirectory() as root:
path=Path(root)/'synthetic.zip'
with ZipFile(path,'w') as z:z.writestr('a_sim.vtu',xml)
with patch.object(d,'decode_array',return_value=np.array([0.,1.,2.,3.])) as decode:
values,receipt=d.read_non_test_fill_time(path,'a',np.array([1,3]),self.manifest)
decode.assert_called_once();np.testing.assert_array_equal(values,[1.,3.])
self.assertEqual(receipt['decoded_label_arrays'],['Fill_time'])
def test_only_common_eligible_targets_are_validated(self):
target=d.validate_query_targets([np.nan,0.,2.],np.array([0,1,2]),np.array([False,True,True]),.5)
np.testing.assert_array_equal(target,[0.,4.])
with self.assertRaises(ValueError):d.validate_query_targets([np.nan],np.array([0]),np.array([True]),.5)
with self.assertRaises(ValueError):d.validate_query_targets([1.],np.array([0]),np.array([False]),.5)
if __name__=='__main__':unittest.main(verbosity=2)
+22
View File
@@ -0,0 +1,22 @@
import unittest
from unittest.mock import patch
import numpy as np
import torch
from run_s2_pilot_training import LearningBases,load_part
class PilotEntrySealTests(unittest.TestCase):
def setUp(self):self.roles=dict(learning_parts=['a'],validation_parts=['b'],sealed_test_parts=['c'])
def test_normalizer_mapping_rejects_validation_and_test_before_io(self):
for case in ['b','c']:
bases=LearningBases({case:dict(preparation_root='never-open')},[case],self.roles)
with patch('run_s2_pilot_training.np.load') as loader:
with self.assertRaises(PermissionError):bases[case]
loader.assert_not_called()
def test_training_loader_rejects_sealed_test_before_hash_or_arrays(self):
with patch('run_s2_pilot_training.sha') as hasher,patch('run_s2_pilot_training.np.load') as loader:
with self.assertRaises(PermissionError):load_part(dict(case='c'),self.roles,torch.device('cpu'))
hasher.assert_not_called();loader.assert_not_called()
if __name__=='__main__':unittest.main(verbosity=2)
+49
View File
@@ -0,0 +1,49 @@
import unittest
import tempfile
import sys
import json
import threading
from pathlib import Path
from unittest.mock import patch
import s2_pilot_gpu_guard as guard
from s2_gpu_process_supervisor import supervise
from s2_pilot_gpu_guard import parse_device_usage
class GPUUsageTests(unittest.TestCase):
def test_exact_device_memory_units(self):
self.assertEqual(parse_device_usage('15360, 100\n'),(15*2**30,100))
self.assertEqual(parse_device_usage('5, 0'),(5*2**20,0))
def test_missing_or_ambiguous_counters_fail_closed(self):
for text in ['', 'N/A, 0','5, 0\n5, 0','-1, 0','2, 101','2']:
with self.assertRaises(ValueError):parse_device_usage(text)
def test_explicit_visibility_reaches_worker_and_receipt_without_gpu_use(self):
with tempfile.TemporaryDirectory() as root:
# wait4 includes transient inherited torch parent RSS before exec.
limits=dict(total_cpu_seconds=5,job_cpu_seconds=2,job_wall_seconds=2,tree_rss_bytes=1024*2**20,disk_bytes=2**20,max_processes=1)
r=supervise([sys.executable,'-c','import os; assert os.environ["CUDA_VISIBLE_DEVICES"]=="synthetic-device"'],root,'synthetic',limits,gpu_uuid='synthetic-device')
self.assertEqual(r['status'],'PASS');self.assertTrue(r['gpu_visible']);self.assertEqual(r['gpu_uuid'],'synthetic-device')
def test_vram_monitor_error_and_whole_host_rss_stop_without_second_run(self):
for trigger in ['vram','monitor','host']:
with self.subTest(trigger=trigger), tempfile.TemporaryDirectory() as tmp:
root=Path(tmp);killed=threading.Event()
c=dict(gpu_uuid='synthetic-device',code_sha256={},runs=[dict(name=f'{arm}{width}',arm=arm,width=width,seed=0) for width in [128,256] for arm in ['N','E']])
(root/'training-contract.json').write_text(json.dumps(c))
calls=0
def usage(uuid):
nonlocal calls
calls+=1
if calls==1:return 5*2**20,0
if trigger=='monitor':raise RuntimeError('synthetic monitor failure')
return (16*2**30 if trigger=='vram' else 5*2**20),0
def fake_supervise(*a,**kw):
self.assertTrue(killed.wait(2));return dict(status='PASS',stop_reason=None)
with patch.object(guard,'device_usage',usage),patch.object(guard,'tree_usage',return_value=(0,17*2**30 if trigger=='host' else 0,1)),patch.object(guard,'active_training_pids',return_value=[123456789]),patch.object(guard,'stop_group',side_effect=lambda pid:killed.set()),patch.object(guard,'supervise',side_effect=fake_supervise) as worker:
guard.run(root)
self.assertEqual(worker.call_count,1)
result=json.loads((root/'pilot-complete.json').read_text())
self.assertEqual(result['status'],'PILOT_INCOMPLETE');self.assertIsNone(result['selected_global_width'])
self.assertEqual(result['runs'][0]['stop_reason'],{'vram':'DEVICE_VRAM_LIMIT','monitor':'GPU_MONITOR_FAILED','host':'WHOLE_RUN_HOST_RSS_LIMIT'}[trigger])
if __name__=='__main__':unittest.main(verbosity=2)
+29
View File
@@ -0,0 +1,29 @@
import tempfile
import unittest
from pathlib import Path
import json
import numpy as np
from s2_preprocess_pilot import load_sampled_o2,O2_MEMBERS
from run_s2_pilot_preparation import projection
class PreparationChecks(unittest.TestCase):
def test_cache_requires_exact_original_pose_draw_radius_and_raw_ids(self):
with tempfile.TemporaryDirectory() as tmp:
root=Path(tmp);p=root/'nodes.npz';r=root/'result.json'
np.savez(p,anchor_ids=[1,3],raw_node_ids=[11,13],multiplicity=[1000,1000],**{k:np.ones(2) for k in O2_MEMBERS})
r.write_text(json.dumps(dict(status='COMPLETE',pose='original',full_nodes=4,radius=2.)))
spec=dict(existing_sampled_o2_nodes=str(p),existing_sampled_o2_result=str(r))
result=load_sampled_o2(spec,np.array([1,3]),np.array([1000,1000]),np.arange(10,14),2.,4)
self.assertEqual(set(result),O2_MEMBERS)
for ids,counts,raw,radius in [(np.array([0,3]),[1000,1000],np.arange(10,14),2.),(np.array([1,3]),[1,1999],np.arange(10,14),2.),(np.array([1,3]),[1000,1000],np.arange(4),2.),(np.array([1,3]),[1000,1000],np.arange(10,14),3.)]:
with self.assertRaises(AssertionError):load_sampled_o2(spec,ids,np.array(counts),raw,radius,4)
def test_checkpoint_counts_failed_cpu_and_separates_cached_rate(self):
c=dict(initial_cpu_seconds_per_node=1.,cases=[dict(tet_nodes=100),dict(tet_nodes=200),dict(tet_nodes=100,reuse_preparation='cache'),dict(tet_nodes=300,reuse_preparation='cache')])
done=[dict(index=0,worker=dict(cpu_seconds=100.,status='STOPPED_OR_FAILED')),dict(index=2,worker=dict(cpu_seconds=10.,status='PASS'))]
p=projection(c,done)
self.assertEqual(p['spent_worker_cpu_seconds'],110.)
self.assertEqual(p['projected_worker_cpu_seconds'],340.)
if __name__=='__main__':unittest.main(verbosity=2)
+47
View File
@@ -0,0 +1,47 @@
import unittest
import numpy as np
import torch
from torch import nn
from s2_pilot_train import part_weights,train_part,component_score,select_capacity
class TinyQueryModel(nn.Module):
arm='N'
def __init__(self):super().__init__();self.linear=nn.Linear(2,1)
def forward(self,base,ids,gaps,edge):return self.linear(base[ids]).squeeze(-1)
class PilotTrainChecks(unittest.TestCase):
def test_step_weights_expectation_and_component_balance(self):
# Two components with 1 and 2 parts; total global contribution is one.
weights=[part_weights([1,3],n,2,3) for n in [1,2,2]]
self.assertAlmostEqual(sum(w.sum() for w in weights)/3,1.)
self.assertAlmostEqual(weights[0].sum(),weights[1].sum()+weights[2].sum())
def test_synthetic_cpu_update_and_nonfinite_no_update(self):
torch.manual_seed(0);m=TinyQueryModel();o=torch.optim.AdamW(m.parameters(),lr=1e-3)
b=torch.tensor([[1.,2.],[2.,3.]]);ids=torch.tensor([0,1]);g=torch.zeros(2,2);w=torch.tensor([.25,.75]);target=torch.tensor([1.,2.])
before=m.linear.weight.detach().clone();loss=train_part(m,o,b,ids,g,None,target,w)
self.assertTrue(np.isfinite(loss));self.assertFalse(torch.equal(before,m.linear.weight))
before=m.linear.weight.detach().clone()
with self.assertRaises(ValueError):train_part(m,o,b,ids,g,None,torch.tensor([float('nan'),2.]),w)
self.assertTrue(torch.equal(before,m.linear.weight))
def test_validation_component_weight_and_capacity_tie(self):
self.assertEqual(component_score({'a':1.,'b':3.,'c':5.},{'a':'x','b':'y','c':'y'}),2.5)
self.assertEqual(select_capacity({('N',128):1.,('E',128):3.,('N',256):2.,('E',256):2.}),128)
with self.assertRaises(ValueError):select_capacity({('N',128):1.})
with self.assertRaises(ValueError):component_score({'a':1.},{'a':'x','b':'y'})
def test_actual_four_model_variants_backward_on_synthetic_nodes(self):
from s2_models.normalization import fit_base_standardizer
from s2_models.network import QueryModel
values=np.arange(90,dtype=float).reshape(9,10)/90
stats=fit_base_standardizer({'synthetic':values},learning_part_ids=['synthetic'],fold='synthetic',pool='100')
for arm in ['N','E']:
for width in [128,256]:
torch.manual_seed(0);m=QueryModel(arm,width,standardizer=stats)
o=torch.optim.AdamW(m.parameters(),lr=1e-3,weight_decay=1e-5)
before=m.decoder[-1].weight.detach().clone()
loss=train_part(m,o,torch.tensor(values,dtype=torch.float32),torch.tensor([1,3]),torch.ones(2,2)*.1,torch.ones(2,4)*.1,torch.tensor([.2,.8]),torch.tensor([.5,.5]))
self.assertTrue(np.isfinite(loss));self.assertFalse(torch.equal(before,m.decoder[-1].weight))
if __name__=='__main__':torch.set_num_threads(1);unittest.main(verbosity=2)
+55
View File
@@ -0,0 +1,55 @@
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
import numpy as np
from s2_preprocess import load_selected,GEOMETRY_MEMBERS,scales_and_draw,s2_eligibility,edge_summary
from run_s2_ten_part_measurement import remaining_limits
class PreprocessChecks(unittest.TestCase):
def test_whitelist_never_opens_label_member(self):
from zipfile import ZipFile
with tempfile.TemporaryDirectory() as t:
p=Path(t)/'mixed.npz';np.savez(p,points_raw=np.ones((2,3)),fill_time_raw=np.array([999.]))
opened=[];original=ZipFile.open
def spy(z,name,*a,**kw):opened.append(name);return original(z,name,*a,**kw)
with patch.object(ZipFile,'open',spy):r=load_selected(p,['points_raw'],GEOMETRY_MEMBERS)
self.assertEqual(opened,['points_raw.npy']);self.assertEqual(r['points_raw'].shape,(2,3))
with self.assertRaises(ValueError):load_selected(p,['fill_time_raw'],GEOMETRY_MEMBERS)
def test_draw_seed_multiplicity_and_original_identity(self):
d=np.array([0.,1.,2.]);mass=np.array([1.,3.,4.]);raw=np.array([10,20,30])
a,s=scales_and_draw(d,mass,raw,13);b,_=scales_and_draw(d,mass,raw,13)
np.testing.assert_array_equal(a['draw_ids'],b['draw_ids'])
np.testing.assert_array_equal(a['unique_ids'][a['draw_to_unique']],a['draw_ids'])
np.testing.assert_array_equal(a['draw_raw_ids'],raw[a['draw_ids']])
self.assertEqual(a['multiplicity'].sum(),2000);self.assertEqual(s['t_part'],4.)
with tempfile.TemporaryDirectory() as t:
p=Path(t)/'draw.npz';np.savez(p,**a)
c,_=scales_and_draw(d,mass,raw,999,existing=p)
np.testing.assert_array_equal(a['draw_ids'],c['draw_ids'])
def test_nonpositive_median_fails_without_radius_substitute(self):
with self.assertRaises(ValueError):scales_and_draw(np.array([0.,1.]),np.array([3.,1.]),np.arange(2),1)
def test_s2_eligibility_does_not_reuse_gate_filter(self):
s=dict(valid=np.array([True,True,False,True]),neff_pass=np.array([True,False,True,True]),g=np.ones((4,2)),gate_direction_valid=np.zeros(4,bool))
np.testing.assert_array_equal(s2_eligibility(np.ones(4),s,np.array([True,True,True,False]),np.ones((4,4))),[True,False,False,False])
def test_all_incident_edges_and_anchor_tensor(self):
x=np.array([[0.,0,0],[1.,0,0],[0.,1,0]]);edges=np.array([[0,1],[0,2],[1,2]])
b=np.zeros((1,2,3,3));b[0,0,0,0]=1
s=dict(B=b,mu=np.ones((1,3))/3,valid=np.ones(1,bool))
summary,complete,e,degree,good=edge_summary(x,edges,np.array([0]),s,1.)
np.testing.assert_allclose(summary,[[.5,1.,0,0]])
self.assertTrue(complete[0]);self.assertEqual(degree[0],2);self.assertEqual(good[0],2)
def test_stage_deadline_and_supervisor_cpu_count(self):
self.assertEqual(remaining_limits(10799,10,5)['job_wall_seconds'],1)
self.assertEqual(remaining_limits(100,10790,5)['job_cpu_seconds'],5)
self.assertIsNone(remaining_limits(10800,0,0))
self.assertIsNone(remaining_limits(0,10799,1))
if __name__=='__main__':unittest.main(verbosity=2)