B3 B4 approved learning-panel gates and synthetic acceptance

This commit is contained in:
Frank14f committed 2026-11-17 12:37:02 +08:00
1 parent ef7fe68833
commit 4d19c03080
12 files changed
+13106

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
"""B3 quantization/birth records and B4 weighted monotone projection."""
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components
def quantize_lower_tie(values,step=.001,offset=0.):
y=np.asarray(values,dtype=np.float64)
if not np.isfinite(y).all() or not np.isfinite([step,offset]).all() or step<=0:raise ValueError('Invalid quantization input')
q=(y-offset)/step;lo=np.floor(q)
return (lo+((q-lo)>.5))*step+offset
def monotone_projection(potential,target,weights):
s=np.asarray(potential,dtype=np.float64);y=np.asarray(target,dtype=np.float64);w=np.asarray(weights,dtype=np.float64)
if s.ndim!=1 or not len(s) or s.shape!=y.shape or s.shape!=w.shape or not np.isfinite([s,y,w]).all() or np.any(w<=0):raise ValueError('Projection domain mismatch')
distinct,inverse=np.unique(s,return_inverse=True);W=np.bincount(inverse,weights=w);means=np.bincount(inverse,weights=w*y)/W
block_means=[];block_weight=[];block_length=[]
for mean,weight in zip(means,W):
block_means.append(float(mean));block_weight.append(float(weight));block_length.append(1)
while len(block_means)>1 and block_means[-2]>block_means[-1]:
weight=block_weight[-2]+block_weight[-1]
mean=(block_weight[-2]*block_means[-2]+block_weight[-1]*block_means[-1])/weight
length=block_length[-2]+block_length[-1]
block_means[-2:]=[mean];block_weight[-2:]=[weight];block_length[-2:]=[length]
fitted=np.repeat(block_means,block_length);prediction=fitted[inverse]
within=float(np.sum(w*(y-means[inverse])**2));between=float(np.sum(W*(means-fitted)**2))
direct=float(np.sum(w*(y-prediction)**2));denom=float(np.sum(w*y*y))
if not np.isclose(within+between,direct,rtol=1e-10,atol=1e-12*max(denom,1.)):raise ValueError('PAVA decomposition identity failed')
info=dict(nodes=len(y),potential_groups=len(distinct),fitted_blocks=len(block_means),within_tie_SSE=within,
between_order_SSE=between,total_SSE=direct,denominator=denom,
normalized_tie=within/denom if denom>0 else None,normalized_order=between/denom if denom>0 else None,
relative_l2=float(np.sqrt(direct/denom)) if denom>0 else None,
tie_share=within/direct if direct>0 else None,deployable=False)
return info,dict(potential=distinct,group_weights=W,group_mean=means,fitted=fitted)
def birth_platforms(field,mass,raw,points,adjacency):
s=np.asarray(field,dtype=np.float64);m=np.asarray(mass,dtype=np.float64);n=len(s)
src=np.repeat(np.arange(n),np.diff(adjacency.indptr));dst=adjacency.indices
equal=s[src]==s[dst]
ties=csr_matrix((np.ones(np.count_nonzero(equal)),(src[equal],dst[equal])),shape=(n,n))
count,labels=connected_components(ties,directed=False)
rejected=np.zeros(count,bool);rejected[labels[src[s[dst]>s[src]]]]=True
order=np.argsort(labels,kind='stable');bounds=np.r_[0,np.flatnonzero(np.diff(labels[order]))+1,n];out=[]
for lo,hi in zip(bounds[:-1],bounds[1:]):
ids=order[lo:hi];g=labels[ids[0]]
if rejected[g]:continue
center=np.sum(points[ids]*m[ids,None],axis=0)/m[ids].sum();d=np.linalg.norm(points[ids]-center,axis=1)
rep=int(ids[np.lexsort((raw[ids],d))[0]])
out.append(dict(birth_min_raw_id=int(raw[ids].min()),representative_raw_id=int(raw[rep]),nodes=len(ids),
volume_mass=float(m[ids].sum()),scalar=float(s[rep]),position=list(map(float,points[rep]))))
return sorted(out,key=lambda r:r['birth_min_raw_id'])
+161
View File
@@ -0,0 +1,161 @@
"""Approved B3/B4 learning-only gates, distinct components, no new models."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import sys
import time
import numpy as np
import joblib
from scipy.sparse import csr_matrix
from threadpoolctl import threadpool_limits
from phase2_h1_worker_v2 import sha,checked,save
from phase2_h1_topology import graph,events,match
from phase2_gate_math import monotone_projection,quantize_lower_tie,birth_platforms
from r4bc_guard import supervise
BASE=Path('/home/frank14f/optane/MeltMind')
def read(p):return json.loads(Path(p).read_text())
def prepare(acceptance):
a=read(acceptance)
if a['status']!='PASS':raise ValueError('Gate synthetic acceptance missing')
old=BASE/'artifacts/r4bc-execution-2026-11-16/contract.json';c=read(old)
candidates={}
for r in c['parts']:
role=next(x for x in r['assignments'] if x['fold']==0)
if role['role']=='learning' and '100' in role['learning_pools']:candidates.setdefault(r['component_id'],[]).append(r)
components=sorted(candidates,key=lambda x:(hashlib.sha256(x.encode()).hexdigest(),x))[:50]
rows=[min(candidates[g],key=lambda r:r['case']) for g in components]
if len(rows)!=50 or len({r['component_id'] for r in rows})!=50:raise ValueError('Expected 50 distinct learning components')
modelpath=BASE/'artifacts/r4bc-execution-2026-11-16/models-terminal.json'
model=next(m for m in read(modelpath)['models'] if m['arm']=='A' and m['fold']==0 and m['pool']==100)
source={str(old):sha(old),str(modelpath):sha(modelpath),str(acceptance):sha(acceptance)}
for p,h in a['code_sha256'].items():checked(p,h)
code={f'tools/{n}.py':sha(BASE/f'tools/{n}.py') for n in ['phase2_gates_34','phase2_gate_math','phase2_h1_topology','phase2_h1_worker_v2','phase2_residual_math','r4bc_guard','s2_recovery_supervisor','s2_recovery_monitoring']}
for name,cpu in [('B3',16),('B4',18)]:
root=BASE/f'artifacts/phase2-{name.lower()}-gate-2026-11-17';root.mkdir(exist_ok=False)
save(root/'contract.json',dict(version='phase2-'+name+'-gate-v1',gate=name,authorizing_ruling='1794887072.921849',
b1_main_review='1794888554.440209',parts=rows,code_sha256=code,source_sha256=source,baseline_model=model,
selection='fold0/pool100 learning; SHA256(component_id UTF8) ascending; lexicographically first case in each; first50',
worker_cpu=cpu,cpu_cap_seconds=7200,worker_cap_seconds=7080,reserved_setup_guard_seconds=120,
rss_bytes=8*1024**3,disk_bytes=5*1024**3,new_models=0,new_decodes=0,gpu=False,retry=False,
thresholds=dict(grid_step=.001,half_grid_offset=.0005,min_precision=.80,min_recall=.80,min_eventful_parts=10),
B4_domain='full retained tet nodes; unit positive weights exactly as A training; existing fold0/pool100 A inference on the same learning parts',
failure_policy='retain each exception; missing panel parts prevent PASS/pruning conclusions; no replacement',
scope='diagnostic learning-side gate; no test targets, no deployable calibration'))
print(root,flush=True)
def config(root):
c=read(root/'contract.json')
for p,h in {**c['source_sha256'],**c['code_sha256']}.items():checked(p,h)
for r in c['parts']:
role=next(a for a in r['assignments'] if a['fold']==0)
if role['role']!='learning' or '100' not in role['learning_pools']:raise PermissionError('Gate attempted held-out part')
return c
def array(row,name):
c=row['v5_cache'];return np.load(checked(Path(c['output'])/(name+'.npy'),c['output_sha256'][name+'.npy']),allow_pickle=False)
def geometry(row):
p=row['preparation'];d=Path(p['preparation_root']);meta=read(checked(d/'result.json',p['result_sha256']))
with np.load(checked(d/'geometry.npz',p['output_sha256']['geometry.npz']),allow_pickle=False) as z:
x=z['points_raw'].copy();tet=z['tetra'].copy();raw=z['raw_node_ids'].copy()
if not np.array_equal(raw,array(row,'raw_ids')):raise ValueError('Gate raw IDs differ')
return x,raw,array(row,'mass'),graph(x,tet),float(meta['scales']['Rg'])
def totals(rows,key):
items=[r[key] for r in rows];tp=sum(x['TP'] for x in items);fp=sum(x['FP'] for x in items);fn=sum(x['FN'] for x in items)
distances=[d for x in items for d in x['matched_distance_over_Rg']];empty={}
for item in items:empty[item['empty_class']]=empty.get(item['empty_class'],0)+1
return dict(TP=tp,FP=fp,FN=fn,precision=tp/(tp+fp) if tp+fp else None,recall=tp/(tp+fn) if tp+fn else None,
median_distance_over_Rg=float(np.median(distances)) if distances else None,matched=len(distances),empty_classes=empty)
def run_b3(root,c):
rows=[];failures=[];start=time.process_time();dest=root/'parts';dest.mkdir(exist_ok=False)
for r in c['parts']:
out=dest/r['case'];out.mkdir()
save(out/'access.json',dict(role='fold0_learning100',seconds_sha256=r['v5_cache']['output_sha256']['seconds.npy']))
try:
x,raw,m,a,rg=geometry(r);truth=array(r,'seconds');scale=r['preparation']['t_set'];ref=events(truth,m,raw,x,a)
save(out/'truth-events.json',ref);save(out/'birth-platforms.json',birth_platforms(truth,m,raw,x,a))
info=dict(case=r['case'],component_id=r['component_id'],nodes=len(raw),edges=a.nnz//2,volume=float(m.sum()),Rg=rg,true_events=len(ref['events']))
for label,offset in [('grid',0.),('half_grid',.0005)]:
field=quantize_lower_tie(truth/scale,.001,offset)*scale;pred=events(field,m,raw,x,a)
info[label]=match(ref,pred,a,rg);save(out/(label+'-events.json'),pred)
np.savez_compressed(out/'geometry.npz',points=x,raw_ids=raw,mass=m,indices=a.indices,indptr=a.indptr,data=a.data,rg=rg)
save(out/'result.json',info);rows.append(info)
except Exception as exc:
failure=dict(case=r['case'],type=type(exc).__name__,message=str(exc));failures.append(failure);save(out/'failure.json',failure)
save(root/'progress.json',dict(phase='TRUE_FIELD_STABILITY',complete=len(rows),failures=len(failures),cpu_seconds=time.process_time()-start))
stability={key:totals(rows,key) for key in ('grid','half_grid')};eventful=sum(r['true_events']>0 for r in rows)
good=not failures and len(rows)==50 and eventful>=10 and all(t[k] is not None and t[k]>=.8 for t in stability.values() for k in ('precision','recall'))
save(root/'stability-gate.json',dict(pass_gate=good,eventful_parts=eventful,planned=50,complete=len(rows),failures=failures,metrics=stability))
eik=[]
if good:
for r in c['parts']:
out=dest/r['case']
with np.load(out/'geometry.npz',allow_pickle=False) as z:
raw=z['raw_ids'];x=z['points'];m=z['mass'];rg=float(z['rg']);a=csr_matrix((z['data'],z['indices'],z['indptr']),shape=(len(raw),len(raw)))
pred=events(array(r,'Vf')*r['preparation']['t_set'],m,raw,x,a)
score=match(read(out/'truth-events.json'),pred,a,rg)
save(out/'eik-events.json',dict(events=pred,match=score));eik.append(dict(case=r['case'],eik=score))
save(root/'result.json',dict(status='COMPLETE',gate='B3',decision='METRIC_STABLE_ON_THIS_PANEL' if good else 'PARK_UNIVERSAL_MODEL_JUDGEMENT',
eventful_parts=eventful,stability=stability,rows=rows,failures=failures,EIK=totals(eik,'eik') if eik else None,
EIK_attempted=good,reopen='independent temporal resolution or remesh reference',cpu_seconds=time.process_time()-start,
limitations=['learning panel only','precision perturbation not remesh convergence','no weld/airtrap truth']))
def run_b4(root,c):
model=c['baseline_model'];baseline=joblib.load(checked(model['model_path'],model['model_sha256']))
rows=[];failures=[];start=time.process_time();dest=root/'parts';dest.mkdir(exist_ok=False)
with threadpool_limits(1):
for r in c['parts']:
out=dest/r['case'];out.mkdir();save(out/'access.json',dict(role='fold0_learning100',target_sha256=r['v5_cache']['output_sha256']['target.npy']))
try:
y=array(r,'target');s=array(r,'Df');x=np.asarray(array(r,'features'),dtype=np.float32)
if len(y)!=r['nodes'] or x.shape!=(len(y),19):raise ValueError('A full node domain mismatch')
info,curve=monotone_projection(s,y,np.ones(len(y)));pred=np.asarray(baseline.predict(x),dtype=np.float64)
denominator=float(np.sum(y*y));ae=float(np.sqrt(np.sum((pred-y)**2)/denominator)) if denominator>0 else None
result=dict(case=r['case'],component_id=r['component_id'],A_relative_l2=ae,**info)
np.savez_compressed(out/'monotone-map.npz',**curve);save(out/'result.json',result);rows.append(result)
except Exception as exc:
failure=dict(case=r['case'],type=type(exc).__name__,message=str(exc));failures.append(failure);save(out/'failure.json',failure)
save(root/'progress.json',dict(complete=len(rows),failures=len(failures),cpu_seconds=time.process_time()-start))
defined=[r for r in rows if r['relative_l2'] is not None and r['A_relative_l2'] is not None]
oracle=float(np.mean([r['relative_l2'] for r in defined])) if defined else None;aa=float(np.mean([r['A_relative_l2'] for r in defined])) if defined else None
decision='PARK_COVERAGE' if len(defined)!=50 or failures else 'PRUNE_ONLY_CLOCK_CAN_BEAT_A' if oracle>=aa else 'RETAIN_POSSIBILITY_ONLY'
save(root/'result.json',dict(status='COMPLETE',gate='B4',decision=decision,component_equal_oracle_l2=oracle,
component_equal_A_l2=aa,rows=rows,failures=failures,cpu_seconds=time.process_time()-start,
interpretation='posthoc lower bound for this fixed potential/domain/positive weights/monotone map class; not causal ordering share or deployable calibration',
reopen='new potential, different control domain, or additional branch information'))
def supervise_gate(root):
c=config(root);os.sched_setaffinity(0,[c['worker_cpu']]);start=time.process_time()
if (root/'actual-start.json').exists():raise FileExistsError('No duplicate gate')
save(root/'actual-start.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json'),main_review=c['authorizing_ruling']))
result=supervise([sys.executable,'tools/phase2_gates_34.py','worker',str(root)],root/'guard',
dict(cpu_seconds=c['worker_cap_seconds'],wall_seconds=c['worker_cap_seconds'],rss_bytes=c['rss_bytes'],disk_bytes=c['disk_bytes']))
guard_cpu=time.process_time()-start;total=result['cpu_seconds']+guard_cpu
status='PASS' if result['status']=='PASS' and total+60<=7200 and (root/'result.json').exists() else 'STOPPED'
save(root/'complete.json',dict(status=status,worker=result,guard_cpu_seconds=guard_cpu,setup_charge_seconds=60,
cumulative_charge_seconds=total+60,scientific_gate_pass=None if status!='PASS' else read(root/'result.json')['decision'],retry=False))
return 0 if status=='PASS' else 1
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('mode',choices=['prepare','supervise','worker']);p.add_argument('path',type=Path);a=p.parse_args()
if a.mode=='prepare':prepare(a.path)
elif a.mode=='supervise':raise SystemExit(supervise_gate(a.path))
else:
c=config(a.path);(run_b3 if c['gate']=='B3' else run_b4)(a.path,c)
+25
View File
@@ -0,0 +1,25 @@
import unittest
import numpy as np
from scipy.sparse import csr_matrix
from phase2_gate_math import monotone_projection,quantize_lower_tie,birth_platforms
class GateMathTests(unittest.TestCase):
def test_perfect_order(self):
r,_=monotone_projection([1,2,3],[2,4,7],[1,1,1]);self.assertEqual(r['total_SSE'],0)
def test_all_ties_are_within_group_error(self):
r,z=monotone_projection([1,1,1],[1,2,5],[1,2,1]);self.assertEqual(r['between_order_SSE'],0)
self.assertAlmostEqual(r['within_tie_SSE'],9.);self.assertAlmostEqual(z['fitted'][0],2.5)
def test_two_point_reversal(self):
r,z=monotone_projection([0,1],[3,1],[1,3]);self.assertEqual(r['within_tie_SSE'],0)
self.assertAlmostEqual(r['between_order_SSE'],3.);np.testing.assert_array_equal(z['fitted'],[1.5,1.5])
def test_midpoint_rounds_lower_including_shifted_origin(self):
np.testing.assert_array_equal(quantize_lower_tie(np.array([.0005,.0015]),.001,0),[0.,.001])
self.assertEqual(float(quantize_lower_tie(np.array([0.]),.001,.0005)[0]),-.0005)
def test_births_preserve_disconnected_equal_maxima(self):
a=csr_matrix((np.ones(4),([0,1,1,2],[1,0,2,1])),shape=(3,3));x=np.c_[np.arange(3),np.zeros((3,2))]
r=birth_platforms(np.array([2,1,2]),np.ones(3),np.arange(10,13),x,a)
self.assertEqual([v['birth_min_raw_id'] for v in r],[10,12])
if __name__=='__main__':unittest.main()