Fix B7 rank and B6 HKS gates behind existing B1 recovery
This commit is contained in:
1 parent
bccf1fee9f
commit
086c56dc36
27 files changed
+61726
-46
No files matched your search
@@ -0,0 +1,148 @@
|
||||
"""B6 learning-only geometric HKS gate, after B1 recovery and serial B7."""
|
||||
import argparse
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import resource
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from collections import Counter
|
||||
import joblib
|
||||
import numpy as np
|
||||
import scipy
|
||||
from phase2_fixed_io import BASE,freeze,verify
|
||||
from phase2_h1_worker_v2 import checked,sha,save
|
||||
from phase2_b6_math import hks,partial_from_triangular,tsqr_update
|
||||
from r4bc_guard import supervise
|
||||
from s2_recovery_monitoring import disk_usage
|
||||
|
||||
ROOT=BASE/'artifacts/phase2-b6-gate-2026-11-17'
|
||||
def read(p):return json.loads(Path(p).read_text())
|
||||
|
||||
|
||||
def prepare(acceptance):
|
||||
a=read(acceptance)
|
||||
if a['status']!='PASS':raise ValueError('B6 acceptance missing')
|
||||
for p,h in a['code_sha256'].items():checked(BASE/p,h)
|
||||
b3=BASE/'artifacts/phase2-b3-gate-2026-11-17/contract.json';old=read(b3)
|
||||
spec=BASE/'references/2026-11-17/phase2-design/mold-b6-spec-8f94be12.md'
|
||||
checked(spec,'8f94be1294b9b8a020aabaf2f434658b593a3fc78504815c38751e546015ae16')
|
||||
names=['phase2_b6_gate','phase2_b6_math','phase2_fixed_io','phase2_h1_worker_v2','phase2_residual_math','r4bc_guard','s2_recovery_supervisor','s2_recovery_monitoring']
|
||||
ROOT.mkdir(exist_ok=False)
|
||||
save(ROOT/'contract.json',dict(version='B6-volume-Neumann-HKS-v1',approvals=['1794892472.421479','1794893702.921009'],math_review='1794893856.072239',
|
||||
parts=old['parts'],baseline=old['baseline_model'],source_sha256={str(b3):sha(b3),str(spec):sha(spec),str(acceptance):sha(acceptance)},
|
||||
code_sha256={f'tools/{n}.py':sha(BASE/f'tools/{n}.py') for n in names},numpy=np.__version__,scipy=scipy.__version__,
|
||||
operator='volume tet P1 K; lumped mass; all boundaries homogeneous natural Neumann; centered coordinates/Rg; FP64',
|
||||
spectrum='all exact physical-component zero modes; first8 positive + full adjacent relative1e-6 boundary cluster; require next gap or exhaustion',
|
||||
solver='component-wise dense eigh for n<=256; otherwise projected shift-invert eigsh16 positive Ritz pairs, tol1e-10/maxiter2000/ncv48/fixedseed2026111706; no fallback or retry; unconfirmed boundary missing',
|
||||
shift='median whitened stiffness diagonal * 1e-6; invert K+shift*M only on exact zero-mode complement; Rayleigh-Ritz eigenvalues',
|
||||
residual_tolerance=1e-5,orthogonality_tolerance=1e-7,zero_tolerance=1e-10,cluster_tolerance=1e-6,times=[.1,1.,10.],
|
||||
weights='1/(G * parts_in_component * nodes_in_part); full retained tet node rows',
|
||||
partial_correlation='weighted TSQR then SVD projection of y=t_star-A and all3HKS on same original19 columns+intercept; signed Pearson residual r; no refit',
|
||||
null_variance='residual norm<=1e-10*max(original_norm,1) is undefined, never filled zero',
|
||||
gate='coverage>=40/50 and any abs(r)>=.10 => RETAIN_LEARNING_DESCRIPTION; undefined all => PARK; otherwise PRUNE_THIS_FIXED_HKS; no out-of-sample claim',
|
||||
total_cpu_seconds=7200,setup_seconds=60,cpu=24,part_cpu_seconds=110,part_wall_seconds=180,score_cpu_seconds=900,
|
||||
rss_bytes=8*1024**3,disk_bytes=5*1024**3,new_models=0,new_decodes=0,gpu=False,retry=False))
|
||||
|
||||
|
||||
def config(root):
|
||||
c=read(root/'contract.json')
|
||||
for p,h in {**c['code_sha256'],**c['source_sha256']}.items():checked(p,h)
|
||||
if (np.__version__,scipy.__version__)!=(c['numpy'],c['scipy']):raise ValueError('Library drift')
|
||||
if len(c['parts'])!=50 or len({r['component_id'] for r in c['parts']})!=50:raise ValueError('Original B3 panel differs')
|
||||
for r in c['parts']:
|
||||
a=next(a for a in r['assignments'] if a['fold']==0)
|
||||
if a['role']!='learning' or '100' not in a['learning_pools']:raise PermissionError('Not fold0 learning100')
|
||||
return c
|
||||
|
||||
|
||||
def field(root,c,index):
|
||||
row=c['parts'][index];dest=root/'parts'/f'{index:02d}';p=row['preparation'];gp=Path(p['preparation_root'])/'geometry.npz'
|
||||
checked(gp,p['output_sha256']['geometry.npz'])
|
||||
metadata=read(checked(Path(p['preparation_root'])/'result.json',p['result_sha256']))
|
||||
try:
|
||||
with np.load(gp,allow_pickle=False) as z:
|
||||
x=z['points_raw'].copy();tet=z['tetra'].copy();raw=z['raw_node_ids'].copy();mass=z['node_volume_raw'].copy()
|
||||
if x.shape!=(len(raw),3) or np.any(np.diff(raw)<=0) or not np.isfinite(mass).all() or np.any(mass<=0):raise ValueError('GEOMETRY_IDENTITY')
|
||||
center=np.average(x,axis=0,weights=mass);rg=float(metadata['scales']['Rg'])
|
||||
if not np.isfinite(rg) or rg<=0:raise ValueError('INVALID_RG')
|
||||
H,report=hks((x-center)/rg,tet)
|
||||
np.save(dest/'H.npy',H,allow_pickle=False);np.save(dest/'raw_ids.npy',raw,allow_pickle=False)
|
||||
save(dest/'result.json',dict(status='PASS',case=row['case'],component_id=row['component_id'],rg=rg,center=center.tolist(),nodes=len(raw),
|
||||
geometry_sha256=p['output_sha256']['geometry.npz'],H_sha256=sha(dest/'H.npy'),raw_ids_sha256=sha(dest/'raw_ids.npy'),labels_read=False,**report))
|
||||
except BaseException as exc:
|
||||
save(dest/'failure.json',dict(status='MISSING',type=type(exc).__name__,reason=str(exc),traceback=traceback.format_exc(),labels_read=False));raise
|
||||
|
||||
|
||||
def score(root,c):
|
||||
verify(root,'fields');manifest=read(root/'fields-manifest.json');dest=root/'score';R=None;access=[]
|
||||
entries=manifest['entries'];coverage=len(entries)/50
|
||||
if coverage<.8:
|
||||
save(dest/'result.json',dict(status='COMPLETE',decision='PARK_COVERAGE',parts=len(entries),coverage=coverage,signed_r=[None]*3,labels_read=False));return
|
||||
model=joblib.load(checked(c['baseline']['model_path'],c['baseline']['model_sha256']));groups=Counter(c['parts'][e['index']]['component_id'] for e in entries)
|
||||
for e in entries:
|
||||
row=c['parts'][e['index']];folder=root/'parts'/f"{e['index']:02d}";result=read(checked(folder/'result.json',e['result_sha256']));cache=row['v5_cache']
|
||||
H=np.load(checked(folder/'H.npy',result['H_sha256']),mmap_mode='r',allow_pickle=False)
|
||||
raw=np.load(checked(folder/'raw_ids.npy',result['raw_ids_sha256']),allow_pickle=False)
|
||||
original=np.load(checked(Path(cache['output'])/'raw_ids.npy',cache['output_sha256']['raw_ids.npy']),allow_pickle=False)
|
||||
if not np.array_equal(raw,original):raise ValueError('HKS and A node order differ')
|
||||
x=np.load(checked(Path(cache['output'])/'features.npy',cache['output_sha256']['features.npy']),mmap_mode='r',allow_pickle=False)
|
||||
access.append(dict(case=row['case'],role='fold0_learning',target_sha256=cache['output_sha256']['target.npy'],nodes=len(raw)));save(dest/'target-access.json',access)
|
||||
y=np.load(checked(Path(cache['output'])/'target.npy',cache['output_sha256']['target.npy']),mmap_mode='r',allow_pickle=False)
|
||||
if x.shape!=(len(raw),19) or y.shape!=(len(raw),) or H.shape!=(len(raw),3):raise ValueError('HKS residual row mismatch')
|
||||
weight=1/(len(groups)*groups[row['component_id']]*len(raw))
|
||||
for start in range(0,len(raw),32768):
|
||||
stop=min(len(raw),start+32768);xx=np.asarray(x[start:stop],np.float64);pred=np.asarray(model.predict(xx.astype(np.float32)),np.float64)
|
||||
block=np.column_stack([np.ones(stop-start),xx,y[start:stop]-pred,H[start:stop]])*np.sqrt(weight)
|
||||
if not np.isfinite(block).all():raise ValueError('Nonfinite residual input')
|
||||
R=tsqr_update(R,block)
|
||||
save(dest/'progress.json',dict(parts=len(access),nodes=sum(a['nodes'] for a in access)))
|
||||
result=partial_from_triangular(R);valid=[r for r in result['signed_r'] if r is not None]
|
||||
decision='PARK_UNDEFINED' if not valid else 'RETAIN_LEARNING_DESCRIPTION' if any(abs(r)>=.10 for r in valid) else 'PRUNE_THIS_FIXED_HKS'
|
||||
np.save(dest/'weighted-tsqr.npy',R,allow_pickle=False)
|
||||
save(dest/'result.json',dict(status='COMPLETE',decision=decision,parts=len(entries),components=len(groups),coverage=coverage,**result,
|
||||
interpretation='learning fit residual partial correlation; truncated low-frequency geometric heat signature; no out-of-sample or physical thermal claim'))
|
||||
|
||||
|
||||
def run(root):
|
||||
c=config(root);os.sched_setaffinity(0,[c['cpu']]);lock=(root/'run.lock').open('a');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
||||
for folder in ['phase2-h1-labelled-recovery-2026-11-17','phase2-b7-gate-2026-11-17']:
|
||||
p=BASE/'artifacts'/folder/'complete.json'
|
||||
if not p.exists() or (folder.startswith('phase2-h1') and read(p)['status']!='COMPLETE'):raise RuntimeError('Serial dependency incomplete')
|
||||
if (root/'started.json').exists():raise FileExistsError('B6 zero retries')
|
||||
start=time.process_time();u0=resource.getrusage(resource.RUSAGE_CHILDREN);charged=0.;records=[];entries=[]
|
||||
def total():
|
||||
u=resource.getrusage(resource.RUSAGE_CHILDREN);return 60+max(charged,u.ru_utime+u.ru_stime-u0.ru_utime-u0.ru_stime)+time.process_time()-start
|
||||
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json')))
|
||||
try:
|
||||
for i,row in enumerate(c['parts']):
|
||||
if total()+c['part_cpu_seconds']+1050>7200:raise RuntimeError('B6 remaining CPU cannot reserve next part and postfit')
|
||||
dest=root/'parts'/f'{i:02d}';dest.mkdir(parents=True,exist_ok=False)
|
||||
remaining=c['disk_bytes']-disk_usage(root)
|
||||
if remaining<=0:raise RuntimeError('B6 disk cap')
|
||||
save(root/'live-progress.json',dict(stage='GEOMETRY_ONLY',index=i,parts=len(records),unix=time.time(),cumulative_cpu_seconds=total()))
|
||||
g=supervise([sys.executable,'tools/phase2_b6_gate.py','field',str(root),str(i)],dest,dict(cpu_seconds=c['part_cpu_seconds'],wall_seconds=c['part_wall_seconds'],rss_bytes=c['rss_bytes'],disk_bytes=remaining))
|
||||
charged+=g['cpu_seconds'];record=dict(index=i,case=row['case'],guard=g);records.append(record)
|
||||
if g['monitor_errors']:raise RuntimeError('B6 monitor failure')
|
||||
if g['status']!='PASS' and not (dest/'failure.json').exists() and not g['stop_reason']:raise RuntimeError('B6 input/configuration failure before field diagnostics')
|
||||
if g['status']=='PASS':entries.append(dict(index=i,case=row['case'],result_sha256=sha(dest/'result.json')))
|
||||
save(root/'phase-ledger.json',dict(records=records,cumulative_cpu_seconds=total()))
|
||||
save(root/'fields-manifest.json',dict(entries=entries,records=records,all50_terminal=True,labels_read=False))
|
||||
freeze(root,'fields',[root/'contract.json',root/'fields-manifest.json',*[BASE/p for p in c['code_sha256']]])
|
||||
dest=root/'score';dest.mkdir(exist_ok=False)
|
||||
g=supervise([sys.executable,'tools/phase2_b6_gate.py','score',str(root)],dest,dict(cpu_seconds=min(900,7200-total()-120),wall_seconds=1200,rss_bytes=c['rss_bytes'],disk_bytes=c['disk_bytes']-disk_usage(root)))
|
||||
charged+=g['cpu_seconds'];records.append(dict(phase='score',guard=g))
|
||||
if g['status']!='PASS' or total()>7200 or disk_usage(root)>c['disk_bytes']:raise RuntimeError('B6 scoring/resource failure')
|
||||
save(root/'complete.json',dict(status='COMPLETE',cumulative_cpu_seconds=total(),records=records,scientific_gate=read(dest/'result.json')['decision'],retry=False))
|
||||
except BaseException as exc:
|
||||
save(root/'complete.json',dict(status='STOPPED',reason=type(exc).__name__+': '+str(exc),cumulative_cpu_seconds=total(),records=records,retry=False));raise
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
p=argparse.ArgumentParser();p.add_argument('mode',choices=['prepare','run','field','score']);p.add_argument('path',type=Path);p.add_argument('index',nargs='?',type=int);a=p.parse_args()
|
||||
if a.mode=='prepare':prepare(a.path)
|
||||
elif a.mode=='run':run(a.path)
|
||||
elif a.mode=='field':field(a.path,config(a.path),a.index)
|
||||
else:score(a.path,config(a.path))
|
||||
@@ -0,0 +1,107 @@
|
||||
"""FP64 volume-P1 Neumann truncated HKS and weighted partial correlation."""
|
||||
import numpy as np
|
||||
from scipy import linalg,sparse
|
||||
from scipy.sparse.csgraph import connected_components
|
||||
from scipy.sparse.linalg import LinearOperator,eigsh,splu
|
||||
|
||||
CLUSTER_TOL=1e-6
|
||||
RESIDUAL_TOL=1e-5
|
||||
ORTHOGONAL_TOL=1e-7
|
||||
|
||||
|
||||
def assemble(points,tetra):
|
||||
x=np.asarray(points,dtype=np.float64);t=np.asarray(tetra,dtype=np.int64);n=len(x)
|
||||
if x.ndim!=2 or x.shape[1]!=3 or t.ndim!=2 or t.shape[1]!=4 or not len(t) or not np.isfinite(x).all():raise ValueError('INVALID_MESH')
|
||||
if t.min()<0 or t.max()>=n:raise ValueError('MESH_INDEX')
|
||||
K=sparse.csr_matrix((n,n));mass=np.zeros(n)
|
||||
for start in range(0,len(t),16384):
|
||||
ids=t[start:start+16384];v=x[ids];J=np.transpose(v[:,1:]-v[:,0,None],(0,2,1));det=np.linalg.det(J)
|
||||
if not np.isfinite(det).all() or np.any(det==0):raise ValueError('DEGENERATE_TET')
|
||||
volume=np.abs(det)/6;inv=np.linalg.inv(J);grad=np.concatenate([-inv.sum(axis=1)[:,None,:],inv],axis=1)
|
||||
ke=volume[:,None,None]*np.einsum('eik,ejk->eij',grad,grad)
|
||||
K+=sparse.coo_matrix((ke.ravel(),(np.repeat(ids,4,axis=1).ravel(),np.tile(ids,(1,4)).ravel())),shape=(n,n)).tocsr()
|
||||
np.add.at(mass,ids.ravel(),np.repeat(volume/4,4))
|
||||
if not np.isfinite(mass).all() or np.any(mass<=0):raise ValueError('NONPOSITIVE_MASS')
|
||||
# Topology from original tetra membership, never sparsity cancellation in K.
|
||||
edge=np.concatenate([t[:,[0,1]],t[:,[0,2]],t[:,[0,3]]])
|
||||
adj=sparse.coo_matrix((np.ones(len(edge)),(edge[:,0],edge[:,1])),shape=(n,n)).tocsr()
|
||||
nc,labels=connected_components(adj,directed=False)
|
||||
return K,mass,labels,nc
|
||||
|
||||
|
||||
def truncate(values,exhausted):
|
||||
values=np.asarray(values)
|
||||
if not len(values) or np.any(values<=0) or np.any(np.diff(values)<0):raise ValueError('INVALID_POSITIVE_SPECTRUM')
|
||||
end=min(8,len(values))
|
||||
while end<len(values) and (values[end]-values[end-1])/max(values[end],values[end-1])<=CLUSTER_TOL:end+=1
|
||||
if end==len(values) and not exhausted:raise ValueError('UNCONFIRMED_CLUSTER_BOUNDARY')
|
||||
return end
|
||||
|
||||
|
||||
def component_modes(K,mass):
|
||||
n=len(mass);d=1/np.sqrt(mass);A=sparse.diags(d)@K@sparse.diags(d)
|
||||
z=np.sqrt(mass/mass.sum());available=n-1
|
||||
if available<=0:raise ValueError('NO_POSITIVE_MODES')
|
||||
if n<=256:
|
||||
vals,q=linalg.eigh(A.toarray());zero=int(np.argmax(np.abs(q.T@z)))
|
||||
keep=np.arange(n)!=zero;q=q[:,keep];vals=vals[keep];exhausted=True
|
||||
else:
|
||||
# The inverse acts only in the exact physical zero mode's complement.
|
||||
# No small-positive eigenvalue is removed by an absolute cutoff.
|
||||
shift=float(np.median(A.diagonal()))*1e-6
|
||||
if shift<=0:raise ValueError('INVALID_OPERATOR_SCALE')
|
||||
factor=splu((K+sparse.diags(shift*mass)).tocsc());sqrtm=np.sqrt(mass)
|
||||
def project(v):return v-z*(z@v)
|
||||
def mv(v):return project(sqrtm*factor.solve(sqrtm*project(v)))
|
||||
op=LinearOperator((n,n),matvec=mv,dtype=np.float64)
|
||||
initial=project(np.random.default_rng(2026111706).normal(size=n));initial/=np.linalg.norm(initial)
|
||||
_,q=eigsh(op,k=16,which='LA',v0=initial,tol=1e-10,maxiter=2000,ncv=48)
|
||||
q=q-z[:,None]*(z@q)[None,:]
|
||||
q,_=np.linalg.qr(q,mode='reduced')
|
||||
vals,rotation=linalg.eigh(q.T@(A@q));q=q@rotation;exhausted=False
|
||||
order=np.argsort(vals);vals=vals[order];q=q[:,order];phi=d[:,None]*q
|
||||
kp=K@phi;mp=mass[:,None]*phi;err=np.linalg.norm(kp-mp*vals,axis=0)/(np.linalg.norm(kp,axis=0)+np.abs(vals)*np.linalg.norm(mp,axis=0))
|
||||
orth=float(np.linalg.norm(phi.T@mp-np.eye(len(vals))))
|
||||
zero_error=float(np.linalg.norm(K@np.ones(n))/(np.linalg.norm(K.data)*np.sqrt(n)))
|
||||
if not np.isfinite(vals).all() or np.any(vals<=0) or not np.isfinite(err).all() or np.max(err)>RESIDUAL_TOL or orth>ORTHOGONAL_TOL or zero_error>1e-10:raise ValueError('SPECTRAL_ACCEPTANCE_FAILURE')
|
||||
return vals,phi,dict(exhausted=exhausted,residuals=err.tolist(),orthogonality_error=orth,zero_error=zero_error,positive_modes=len(vals))
|
||||
|
||||
|
||||
def hks(points,tetra):
|
||||
K,mass,labels,nc=assemble(points,tetra);modes=[];diagnostics=[];zero=np.zeros(len(mass));all_complete=True
|
||||
for component in range(nc):
|
||||
ids=np.flatnonzero(labels==component);m=mass[ids];zero[ids]=1/m.sum()
|
||||
values,phi,report=component_modes(K[ids][:,ids],m)
|
||||
diagnostics.append(dict(component=component,nodes=len(ids),mass=float(m.sum()),**report))
|
||||
all_complete=all_complete and report['exhausted']
|
||||
for j,v in enumerate(values):modes.append((float(v),component,j))
|
||||
diagnostics[-1]['_ids']=ids;diagnostics[-1]['_phi']=phi
|
||||
modes.sort();values=np.array([v[0] for v in modes]);end=truncate(values,all_complete)
|
||||
# Each unexhausted component must have a witnessed root beyond the global cut.
|
||||
for g,r in enumerate(diagnostics):
|
||||
if not r['exhausted'] and max(v[0] for v in modes if v[1]==g)<=values[end-1]:raise ValueError('UNCONFIRMED_COMPONENT_SPECTRUM')
|
||||
times=np.array([.1,1.,10.])/values[0];H=np.repeat(zero[:,None],3,axis=1)
|
||||
for value,g,j in modes[:end]:
|
||||
r=diagnostics[g];H[r['_ids']]+=r['_phi'][:,j,None]**2*np.exp(-value*times)[None,:]
|
||||
for r in diagnostics:del r['_ids'],r['_phi']
|
||||
if not np.isfinite(H).all():raise ValueError('NONFINITE_HKS')
|
||||
return H,dict(physical_components=nc,components=diagnostics,positive_modes_retained=end,positive_values=values[:end].tolist(),
|
||||
next_eigenvalue=float(values[end]) if end<len(values) else None,spectrum_exhausted=all_complete,times=times.tolist(),zero_modes=nc,
|
||||
mass_sum=float(mass.sum()),cluster_relative_tolerance=CLUSTER_TOL,normalization='Phi^T M Phi=I; exact physical component zero modes')
|
||||
|
||||
|
||||
def partial_from_triangular(R):
|
||||
# R is the TSQR factor of sqrt(w)*[1,X19,y,H3]. Orthogonal left transforms
|
||||
# preserve all projections and inner products, avoiding a dense full corpus.
|
||||
X=R[:,:20];Z=R[:,20:];u,s,v=linalg.svd(X,full_matrices=False)
|
||||
cutoff=np.finfo(float).eps*max(X.shape)*s[0];rank=int(np.sum(s>cutoff));res=Z-u[:,:rank]@(u[:,:rank].T@Z)
|
||||
norms=np.linalg.norm(res,axis=0);original=np.linalg.norm(Z,axis=0);out=[]
|
||||
for j in range(1,4):
|
||||
if norms[0]<=1e-10*max(original[0],1.) or norms[j]<=1e-10*max(original[j],1.):out.append(None)
|
||||
else:out.append(float(np.clip(res[:,0]@res[:,j]/(norms[0]*norms[j]),-1,1)))
|
||||
return dict(signed_r=out,design_rank=rank,design_singular_values=s.tolist(),residual_norms=norms.tolist(),undefined=[j for j,r in enumerate(out) if r is None])
|
||||
|
||||
|
||||
def tsqr_update(R,block):
|
||||
local=np.linalg.qr(block,mode='r')
|
||||
return local if R is None else np.linalg.qr(np.vstack([R,local]),mode='r')
|
||||
@@ -0,0 +1,52 @@
|
||||
"""One dependency waiter: existing B1-v2 terminal, then B7 then B6 once."""
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import psutil
|
||||
from phase2_h1_worker_v2 import save,sha,checked
|
||||
|
||||
BASE=Path('/home/frank14f/optane/MeltMind')
|
||||
ROOT=BASE/'artifacts/phase2-b76-serial-2026-11-17'
|
||||
DEPENDENCY=BASE/'artifacts/phase2-h1-labelled-recovery-2026-11-17'
|
||||
def read(p):return json.loads(Path(p).read_text())
|
||||
|
||||
|
||||
def live(root):
|
||||
return [p.pid for p in psutil.process_iter(['pid','cmdline']) if p.pid!=os.getpid() and str(root) in (p.info['cmdline'] or [])]
|
||||
|
||||
|
||||
def run():
|
||||
c=read(ROOT/'contract.json')
|
||||
for p,h in c['source_sha256'].items():checked(BASE/p,h)
|
||||
os.sched_setaffinity(0,[24]);start=time.process_time()
|
||||
with (ROOT/'run.lock').open('a') as lock:
|
||||
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
||||
if (ROOT/'started.json').exists():raise FileExistsError('Unique existing chain; no restart')
|
||||
save(ROOT/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(ROOT/'contract.json')))
|
||||
results=[]
|
||||
try:
|
||||
wall=time.monotonic()
|
||||
while not (DEPENDENCY/'complete.json').exists() or live(DEPENDENCY):
|
||||
save(ROOT/'live-progress.json',dict(stage='WAIT_EXISTING_B1_V2_TERMINAL_AND_EXIT',unix=time.time(),coordinator_cpu_seconds=time.process_time()-start))
|
||||
if time.monotonic()-wall>43200 or time.process_time()-start>30:raise RuntimeError('Bounded waiter limit')
|
||||
time.sleep(15)
|
||||
if read(DEPENDENCY/'complete.json')['status']!='COMPLETE':raise RuntimeError('B1 recovery stopped; no downstream launch')
|
||||
for name,module in [('phase2-b7-gate-2026-11-17','phase2_b7_gate'),('phase2-b6-gate-2026-11-17','phase2_b6_gate')]:
|
||||
root=BASE/'artifacts'/name
|
||||
for p,h in c['source_sha256'].items():checked(BASE/p,h)
|
||||
if live(root) or (root/'started.json').exists():raise RuntimeError('Dependent gate already attempted; do not duplicate')
|
||||
save(ROOT/'live-progress.json',dict(stage='RUN_'+module,unix=time.time(),coordinator_cpu_seconds=time.process_time()-start))
|
||||
with (ROOT/(module+'.log')).open('x') as log:
|
||||
child=subprocess.run([sys.executable,'tools/'+module+'.py','run',str(root)],stdout=log,stderr=subprocess.STDOUT)
|
||||
if not (root/'complete.json').exists() or live(root):raise RuntimeError('Missing terminal or live children')
|
||||
result=read(root/'complete.json');results.append(dict(gate=name,returncode=child.returncode,complete_sha256=sha(root/'complete.json'),status=result['status']))
|
||||
save(ROOT/'gate-terminals.json',results)
|
||||
save(ROOT/'complete.json',dict(status='BOTH_GATES_TERMINAL',results=results,coordinator_cpu_seconds=time.process_time()-start,retry=False))
|
||||
except BaseException as exc:
|
||||
save(ROOT/'complete.json',dict(status='STOPPED',reason=type(exc).__name__+': '+str(exc),results=results,coordinator_cpu_seconds=time.process_time()-start,retry=False));raise
|
||||
|
||||
if __name__=='__main__':run()
|
||||
@@ -0,0 +1,177 @@
|
||||
"""B7 single-fold validation ranker gate; rank scores are not seconds."""
|
||||
import argparse
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import resource
|
||||
import sys
|
||||
import time
|
||||
import joblib
|
||||
import numpy as np
|
||||
import xgboost as xgb
|
||||
from phase2_h1_worker_v2 import sha,checked,save
|
||||
from phase2_fixed_io import freeze,verify,BASE
|
||||
from s2_custody_common import prediction_inputs
|
||||
from s2_custody_statistics import part_metrics
|
||||
from r4bc_common import paired_difference
|
||||
from r4bc_guard import supervise
|
||||
from s2_recovery_monitoring import disk_usage
|
||||
|
||||
ROOT=BASE/'artifacts/phase2-b7-gate-2026-11-17'
|
||||
PARAMS=dict(objective='rank:pairwise',tree_method='hist',device='cpu',n_estimators=100,max_depth=8,learning_rate=.1,random_state=0,n_jobs=1,lambdarank_pair_method='mean',lambdarank_num_pair_per_sample=1)
|
||||
def read(p):return json.loads(Path(p).read_text())
|
||||
|
||||
|
||||
def sample_ids(raw,cap=2048):
|
||||
raw=np.asarray(raw)
|
||||
if not len(raw) or np.any(np.diff(raw)<=0):raise ValueError('Expected sorted unique raw IDs')
|
||||
return np.linspace(0,len(raw)-1,min(cap,len(raw)),dtype=np.int64)
|
||||
|
||||
|
||||
def dense_rank(y):
|
||||
y=np.asarray(y)
|
||||
if y.ndim!=1 or not np.isfinite(y).all():raise ValueError('Invalid pairwise labels')
|
||||
return np.unique(y,return_inverse=True)[1].astype(np.float32)
|
||||
|
||||
|
||||
def gate(tau,last,undefined):
|
||||
if undefined:return 'PARK_UNDEFINED_OR_COVERAGE'
|
||||
return 'RETAIN_SINGLE_FOLD_FEASIBILITY' if (tau['low'] is not None and tau['low']>0) or (last['value'] is not None and last['value']>=.010) else 'PRUNE_THIS_FIXED_RANKER_RECIPE'
|
||||
|
||||
|
||||
def check_ranker(model):
|
||||
cfg=json.loads(model.get_booster().save_config());obj=cfg['learner']['objective']
|
||||
if obj['name']!='rank:pairwise':raise ValueError('Unexpected ranking objective')
|
||||
params=obj['lambdarank_param']
|
||||
if params['lambdarank_pair_method']!='mean' or int(params['lambdarank_num_pair_per_sample'])!=1:raise ValueError('Unsupported pair method/number; no silent fallback')
|
||||
return cfg
|
||||
|
||||
|
||||
def prepare(acceptance):
|
||||
tests=read(acceptance)
|
||||
if tests['status']!='PASS':raise ValueError('B7 acceptance missing')
|
||||
for p,h in tests['code_sha256'].items():checked(BASE/p,h)
|
||||
p=BASE/'artifacts/r4bc-execution-2026-11-16/contract.json';old=read(p);rows={}
|
||||
for role in ['learning','validation']:
|
||||
rows[role]=sorted([r for r in old['parts'] if any(a['fold']==0 and a['role']==role and (role!='learning' or '100' in a['learning_pools']) for a in r['assignments'])],key=lambda r:r['case'])
|
||||
if set(r['component_id'] for r in rows['learning']) & set(r['component_id'] for r in rows['validation']):raise ValueError('Role component leakage')
|
||||
b3=BASE/'artifacts/phase2-b3-gate-2026-11-17/contract.json';baseline=read(b3)['baseline_model']
|
||||
names=['phase2_b7_gate','phase2_fixed_io','phase2_h1_worker_v2','phase2_residual_math','s2_custody_common','s2_custody_statistics','r4bc_common','r4bc_guard','s2_recovery_supervisor','s2_recovery_monitoring']
|
||||
ROOT.mkdir(exist_ok=False)
|
||||
save(ROOT/'contract.json',dict(version='B7-one-fold-validation-v1',approval='1794892472.421479',proposal_sha256='66d5801e49470eb9465301b1b855ad8dcddfb5bbbca71c26b90ed7f2fb26f7fb',
|
||||
rows=rows,baseline=baseline,parameters=PARAMS,source_sha256={str(p):sha(p),str(b3):sha(b3),str(acceptance):sha(acceptance)},
|
||||
code_sha256={f'tools/{n}.py':sha(BASE/f'tools/{n}.py') for n in names},cpu=24,total_cpu_seconds=7200,setup_seconds=60,rss_bytes=8*1024**3,disk_bytes=5*1024**3,
|
||||
phase_limits=dict(train=3600,predict=900,score=900),sample_policy='<=2048 uniformly spaced ascending original IDs per part, endpoint inclusive; no labels used for selection',
|
||||
qid_policy='one learning part per qid, ascending part order; dense within-part target ranks, exact ties retained',
|
||||
score_role='fold0 validation only; no test label access',gate='tau-b candidate-minus-A paired-component95CI lower>0 OR A-minus-candidate mean last-distance>=.010Rg; otherwise prune; incomplete/undefined park',
|
||||
rank_scale='raw arbitrary rank score compared with T/t_set only as uncalibrated diagnostic L2, never seconds or a gate',new_models=1,extra_control_models=0,new_decodes=0,gpu=False,retry=False))
|
||||
|
||||
|
||||
def config(root):
|
||||
c=read(root/'contract.json')
|
||||
if xgb.__version__!='2.1.4':raise ValueError('Pinned XGBoost version differs')
|
||||
for p,h in {**c['code_sha256'],**c['source_sha256']}.items():checked(p,h)
|
||||
for role,rows in c['rows'].items():
|
||||
for r in rows:
|
||||
a=next(x for x in r['assignments'] if x['fold']==0)
|
||||
if a['role']!=role or (role=='learning' and '100' not in a['learning_pools']):raise PermissionError('B7 role mismatch')
|
||||
return c
|
||||
|
||||
|
||||
def array(row,name):
|
||||
d=row['v5_cache'];return np.load(checked(Path(d['output'])/(name+'.npy'),d['output_sha256'][name+'.npy']),allow_pickle=False)
|
||||
|
||||
|
||||
def train(root,c):
|
||||
dest=root/'phases/train';xs=[];ys=[];qids=[];records=[];start=time.process_time()
|
||||
for qid,row in enumerate(c['rows']['learning']):
|
||||
raw=array(row,'raw_ids');ix=sample_ids(raw);x=array(row,'features');y=array(row,'target')
|
||||
if x.shape!=(len(raw),19) or y.shape!=raw.shape:raise ValueError('B7 full cache identity mismatch')
|
||||
xs.append(np.asarray(x[ix],np.float32));ys.append(dense_rank(y[ix]));qids.append(np.full(len(ix),qid,np.int64))
|
||||
records.append(dict(case=row['case'],component_id=row['component_id'],qid=qid,raw_ids=list(map(int,raw[ix])),target_sha256=row['v5_cache']['output_sha256']['target.npy'],role='learning'))
|
||||
save(dest/'training-access.json',records)
|
||||
x=np.concatenate(xs);y=np.concatenate(ys);qid=np.concatenate(qids)
|
||||
model=xgb.XGBRanker(**c['parameters']);model.fit(x,y,qid=qid,verbose=False);cfg=check_ranker(model)
|
||||
model.save_model(dest/'model.ubj');save(dest/'xgboost-config.json',cfg)
|
||||
save(dest/'result.json',dict(status='FIT_COMPLETE',checkpoint_sha256=sha(dest/'model.ubj'),rows=len(y),parts=len(records),cpu_seconds=time.process_time()-start,validation_labels_read=False))
|
||||
|
||||
|
||||
def predict(root,c):
|
||||
verify(root,'model');dest=root/'phases/predict';model=xgb.XGBRanker();model.load_model(root/'phases/train/model.ubj');baseline=joblib.load(checked(c['baseline']['model_path'],c['baseline']['model_sha256']));entries=[]
|
||||
for row in c['rows']['validation']:
|
||||
q=prediction_inputs(row);raw=array(row,'raw_ids');ix=np.searchsorted(raw,q['raw_ids'])
|
||||
if np.any(ix>=len(raw)) or not np.array_equal(raw[ix],q['raw_ids']):raise ValueError('Validation query IDs differ')
|
||||
x=np.asarray(array(row,'features')[ix],np.float32);rank=np.asarray(model.predict(x),np.float64);a=np.asarray(baseline.predict(x),np.float64)
|
||||
if not np.isfinite(rank).all() or not np.isfinite(a).all():raise ValueError('Nonfinite validation prediction')
|
||||
p=dest/(row['case']+'.npz');np.savez_compressed(p,rank=rank,A=a,raw_ids=q['raw_ids'],counts=q['counts'],near_wall=q['near_wall'],coordinates_over_Rg=q['coordinates_over_Rg'])
|
||||
entries.append(dict(case=row['case'],path=str(p),sha256=sha(p),component_id=row['component_id']))
|
||||
save(dest/'progress.json',dict(parts=len(entries),validation_labels_read=False))
|
||||
save(dest/'manifest.json',dict(entries=entries,validation_labels_read=False))
|
||||
|
||||
|
||||
def score(root,c):
|
||||
verify(root,'predictions');dest=root/'phases/score';entries=read(root/'phases/predict/manifest.json')['entries'];by={r['case']:r for r in c['rows']['validation']};rows=[];undefined=[]
|
||||
if {e['case'] for e in entries}!=set(by):raise ValueError('Incomplete validation roster')
|
||||
for e in entries:
|
||||
row=by[e['case']]
|
||||
save(dest/'next-target-access.json',dict(case=row['case'],role='fold0_validation',target_sha256=row['v5_cache']['output_sha256']['target.npy']))
|
||||
y=array(row,'target');raw=array(row,'raw_ids')
|
||||
with np.load(checked(e['path'],e['sha256']),allow_pickle=False) as z:
|
||||
ix=np.searchsorted(raw,z['raw_ids'])
|
||||
if not np.array_equal(raw[ix],z['raw_ids']):raise ValueError('Scoring query identity changed')
|
||||
result=dict(case=e['case'],component_id=row['component_id'])
|
||||
for arm in ['rank','A']:
|
||||
metrics=part_metrics(z[arm],y[ix],z['counts'],z['near_wall'],z['raw_ids'],z['coordinates_over_Rg'],int(e['case'].split('_')[0]));result[arm]=metrics
|
||||
for name in ['last_location','kendall_tau_b']:
|
||||
if metrics[name]['value'] is None:undefined.append(dict(case=e['case'],arm=arm,metric=name,reason=metrics[name]['reason']))
|
||||
rows.append(result)
|
||||
groups={}
|
||||
for r in rows:groups.setdefault(r['component_id'],[]).append(r)
|
||||
metrics={}
|
||||
for name in ['last_location','kendall_tau_b','relative_l2']:
|
||||
values={a:{} for a in ['rank','A']}
|
||||
for g,parts in groups.items():
|
||||
for a in values:
|
||||
v=[r[a][name]['value'] for r in parts]
|
||||
if all(x is not None for x in v):values[a][g]=float(np.mean(v))
|
||||
metrics[name]=dict(means={a:float(np.mean(list(v.values()))) if v else None for a,v in values.items()},components={a:len(v) for a,v in values.items()},
|
||||
rank_minus_A=paired_difference(values['rank'],values['A']),A_minus_rank=paired_difference(values['A'],values['rank']))
|
||||
tau=metrics['kendall_tau_b']['rank_minus_A'];last=metrics['last_location']['A_minus_rank']
|
||||
decision=gate(tau,last,undefined)
|
||||
save(dest/'target-access.json',[dict(case=r['case'],role='fold0_validation',target_sha256=r['v5_cache']['output_sha256']['target.npy']) for r in c['rows']['validation']])
|
||||
save(dest/'result.json',dict(status='COMPLETE',decision=decision,parts=len(rows),components=len(groups),metrics=metrics,undefined=undefined,rows=rows,
|
||||
interpretation='validation-only one-fold feasibility; capped ranker vs full-tet A does not isolate loss; rank raw score has no physical-time calibration'))
|
||||
|
||||
|
||||
def run(root):
|
||||
c=config(root);os.sched_setaffinity(0,[24]);start=time.process_time();u0=resource.getrusage(resource.RUSAGE_CHILDREN);charged=0.;records=[]
|
||||
lock=(root/'run.lock').open('a');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
||||
dependency=BASE/'artifacts/phase2-h1-labelled-recovery-2026-11-17/complete.json'
|
||||
if not dependency.exists() or read(dependency)['status']!='COMPLETE':raise RuntimeError('B1 recovery must complete before B7')
|
||||
if (root/'started.json').exists():raise FileExistsError('B7 no retry')
|
||||
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json')))
|
||||
def total():
|
||||
u=resource.getrusage(resource.RUSAGE_CHILDREN);return 60+max(charged,u.ru_utime+u.ru_stime-u0.ru_utime-u0.ru_stime)+time.process_time()-start
|
||||
try:
|
||||
for mode in ['train','predict','score']:
|
||||
if mode=='predict':freeze(root,'model',[root/'contract.json',root/'phases/train/model.ubj',root/'phases/train/result.json',*[BASE/p for p in c['code_sha256']]])
|
||||
if mode=='score':freeze(root,'predictions',[root/'contract.json',root/'phases/predict/manifest.json',*[BASE/p for p in c['code_sha256']]])
|
||||
dest=root/'phases'/mode;dest.mkdir(parents=True,exist_ok=False);cap=min(c['phase_limits'][mode],7200-total()-120)
|
||||
disk_remaining=c['disk_bytes']-disk_usage(root)
|
||||
if disk_remaining<=0:raise RuntimeError('B7 cumulative disk cap')
|
||||
if cap<=0:raise RuntimeError('B7 cumulative CPU cap')
|
||||
save(root/'live-progress.json',dict(stage=mode,unix=time.time(),cumulative_cpu_seconds=total()))
|
||||
g=supervise([sys.executable,'tools/phase2_b7_gate.py',mode,str(root)],dest,dict(cpu_seconds=cap,wall_seconds=c['phase_limits'][mode],rss_bytes=c['rss_bytes'],disk_bytes=disk_remaining))
|
||||
charged+=g['cpu_seconds'];records.append(dict(phase=mode,guard=g));save(root/'phase-ledger.json',dict(records=records,cumulative_cpu_seconds=total()))
|
||||
if g['status']!='PASS' or total()>7200 or disk_usage(root)>c['disk_bytes']:raise RuntimeError('B7 phase/resource stop')
|
||||
save(root/'complete.json',dict(status='COMPLETE',cumulative_cpu_seconds=total(),records=records,scientific_gate=read(root/'phases/score/result.json')['decision'],retry=False))
|
||||
except BaseException as exc:
|
||||
save(root/'complete.json',dict(status='STOPPED',reason=type(exc).__name__+': '+str(exc),cumulative_cpu_seconds=total(),records=records,retry=False));raise
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
p=argparse.ArgumentParser();p.add_argument('mode',choices=['prepare','run','train','predict','score']);p.add_argument('path',type=Path);a=p.parse_args()
|
||||
if a.mode=='prepare':prepare(a.path)
|
||||
elif a.mode=='run':run(a.path)
|
||||
else:dict(train=train,predict=predict,score=score)[a.mode](a.path,config(a.path))
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Single-writer Git barriers; worker verification never spawns a process."""
|
||||
import fcntl
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
from phase2_h1_worker_v2 import sha,checked,save
|
||||
BASE=Path('/home/frank14f/optane/MeltMind')
|
||||
WRITER=Path('/tmp/meltmind-git-review-20261114-v1-29ebglg8')
|
||||
|
||||
|
||||
def freeze(root,name,files):
|
||||
with Path('/tmp/meltmind-single-writer.lock').open('a') as lock:
|
||||
fcntl.flock(lock,fcntl.LOCK_EX)
|
||||
def git(*args):return subprocess.check_output(['git','-C',str(WRITER),*args],text=True).strip()
|
||||
if git('branch','--show-current')!='codex/s2-execution-diff-20261114' or git('status','--porcelain'):raise RuntimeError('Single writer not clean')
|
||||
hashes={}
|
||||
for path in files:
|
||||
path=Path(path);rel=str(path.resolve().relative_to(BASE));dest=WRITER/rel;dest.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(path,dest);hashes[rel]=sha(path)
|
||||
subprocess.run(['git','-C',str(WRITER),'add','--',*hashes],check=True)
|
||||
if git('diff','--cached','--name-only'):subprocess.run(['git','-C',str(WRITER),'commit','-m','Phase2 '+root.name+' '+name+' immutable barrier'],check=True)
|
||||
commit=git('rev-parse','HEAD')
|
||||
for rel,h in hashes.items():
|
||||
if hashlib.sha256(subprocess.check_output(['git','-C',str(WRITER),'show',commit+':'+rel])).hexdigest()!=h:raise ValueError('Git byte mismatch')
|
||||
save(root/(name+'-git-freeze.json'),dict(commit=commit,sha256=hashes,remote_published=False))
|
||||
save(root/(name+'-authorization.json'),dict(proof_sha256=sha(root/(name+'-git-freeze.json')),contract_sha256=sha(root/'contract.json')))
|
||||
|
||||
|
||||
def verify(root,name):
|
||||
auth=json.loads((root/(name+'-authorization.json')).read_text());checked(root/'contract.json',auth['contract_sha256'])
|
||||
p=checked(root/(name+'-git-freeze.json'),auth['proof_sha256']);proof=json.loads(p.read_text())
|
||||
for rel,h in proof['sha256'].items():checked(BASE/rel,h)
|
||||
return proof
|
||||
@@ -0,0 +1,64 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from scipy.linalg import expm
|
||||
from phase2_b6_math import assemble,hks,truncate,tsqr_update,partial_from_triangular
|
||||
|
||||
TET=np.array([[0.,0,0],[1,0,0],[0,1,0],[0,0,1]])
|
||||
|
||||
class Acceptance(unittest.TestCase):
|
||||
def test_exact_stiffness_mass_disconnected(self):
|
||||
K,m,_,nc=assemble(TET,[[0,1,2,3]])
|
||||
expected=np.array([[3,-1,-1,-1],[-1,1,0,0],[-1,0,1,0],[-1,0,0,1]])/6
|
||||
np.testing.assert_allclose(K.toarray(),expected,atol=1e-15);np.testing.assert_allclose(m,np.full(4,1/24))
|
||||
x=np.vstack([TET,TET+[3,0,0],TET+[6,0,0]])
|
||||
H,r=hks(x,np.arange(12).reshape(3,4))
|
||||
self.assertEqual(r['zero_modes'],3);self.assertEqual(r['positive_modes_retained'],9)
|
||||
self.assertEqual(r['next_eigenvalue'],None)
|
||||
np.testing.assert_allclose(H[:4],H[4:8],rtol=1e-12)
|
||||
np.testing.assert_allclose(H[:4],H[8:],rtol=1e-12)
|
||||
self.assertTrue(np.all(H>=6))
|
||||
def test_node_permutation(self):
|
||||
original,_=hks(TET,[[0,1,2,3]])
|
||||
p=np.array([2,0,3,1]);inverse=np.argsort(p)
|
||||
reordered,_=hks(TET[p],inverse[np.array([[0,1,2,3]])])
|
||||
np.testing.assert_allclose(reordered[inverse],original,rtol=1e-11,atol=1e-12)
|
||||
def test_cluster_boundary(self):
|
||||
vals=np.array([1,2,3,4,5,6,7,8,8+1e-7,9])
|
||||
self.assertEqual(truncate(vals,False),9)
|
||||
with self.assertRaises(ValueError):truncate(vals[:9],False)
|
||||
self.assertEqual(truncate([1,2,3],True),3)
|
||||
def test_exact_and_near_rotations(self):
|
||||
rng=np.random.default_rng(6);phi=rng.normal(size=(9,2));Q=np.linalg.qr(rng.normal(size=(2,2)))[0]
|
||||
direct=lambda P,B:np.einsum('ij,jk,ik->i',P,expm(-.7*B),P)
|
||||
for vals in [[2,2],[2,2.000001]]:
|
||||
B=np.diag(vals);a=direct(phi,B);b=direct(phi@Q,Q.T@B@Q)
|
||||
np.testing.assert_allclose(a,b,rtol=1e-13,atol=1e-13)
|
||||
np.testing.assert_allclose(a,direct(phi*np.array([-1,1]),B),rtol=1e-13)
|
||||
self.assertGreater(np.max(np.abs(direct(phi@Q,B)-direct(phi,B))),1e-9)
|
||||
def test_sparse_projected_solver(self):
|
||||
n=7;x=np.array([(i,j,k) for i in range(n) for j in range(n) for k in range(n)],float)
|
||||
ix=lambda i,j,k:(i*n+j)*n+k
|
||||
tet=[]
|
||||
for i in range(n-1):
|
||||
for j in range(n-1):
|
||||
for k in range(n-1):
|
||||
corners=[ix(i,j,k),ix(i+1,j,k),ix(i+1,j+1,k),ix(i,j+1,k),ix(i,j,k+1),ix(i+1,j,k+1),ix(i+1,j+1,k+1),ix(i,j+1,k+1)]
|
||||
tet.extend([[corners[a] for a in t] for t in [(0,1,2,6),(0,2,3,6),(0,3,7,6),(0,7,4,6),(0,4,5,6),(0,5,1,6)]])
|
||||
H,r=hks(x,np.array(tet));self.assertEqual(H.shape,(343,3));self.assertFalse(r['spectrum_exhausted'])
|
||||
self.assertGreater(r['next_eigenvalue'],r['positive_values'][-1])
|
||||
def test_degenerate_and_mass_failure(self):
|
||||
with self.assertRaises(ValueError):assemble(TET,[[0,1,2,2]])
|
||||
with self.assertRaises(ValueError):assemble(np.vstack([TET,[8,9,10]]),[[0,1,2,3]])
|
||||
def test_weighted_partial_qr(self):
|
||||
rng=np.random.default_rng(7);x=rng.normal(size=(200,19));z=rng.normal(size=200)
|
||||
y=3*x[:,0]+z;H=np.column_stack([2*x[:,1]+z,np.ones(200),rng.normal(size=200)])
|
||||
w=np.r_[np.full(50,.5/50),np.full(150,.5/150)]
|
||||
block=np.column_stack([np.ones(200),x,y,H])*np.sqrt(w[:,None]);R=None
|
||||
for chunk in np.array_split(block,7):R=tsqr_update(R,chunk)
|
||||
actual=partial_from_triangular(R)
|
||||
self.assertAlmostEqual(actual['signed_r'][0],1.,places=12);self.assertIsNone(actual['signed_r'][1])
|
||||
residual=block[:,20:]-block[:,:20]@np.linalg.lstsq(block[:,:20],block[:,20:],rcond=None)[0]
|
||||
expected=np.dot(residual[:,0],residual[:,3])/np.linalg.norm(residual[:,0])/np.linalg.norm(residual[:,3])
|
||||
self.assertAlmostEqual(actual['signed_r'][2],expected,places=12)
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
@@ -0,0 +1,28 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
import xgboost as xgb
|
||||
from phase2_b7_gate import sample_ids,dense_rank,check_ranker,PARAMS,gate
|
||||
|
||||
class Acceptance(unittest.TestCase):
|
||||
def test_sampling_and_ties(self):
|
||||
raw=np.arange(10000)*3
|
||||
ids=sample_ids(raw)
|
||||
self.assertEqual(len(ids),2048);self.assertEqual(ids[0],0);self.assertEqual(ids[-1],9999)
|
||||
self.assertTrue(np.all(np.diff(ids)>0))
|
||||
np.testing.assert_array_equal(dense_rank([4,2,4,1]),[2,1,2,0])
|
||||
with self.assertRaises(ValueError):sample_ids([2,1])
|
||||
with self.assertRaises(ValueError):dense_rank([1,np.nan])
|
||||
def test_actual_pairwise_support(self):
|
||||
x=np.arange(60,dtype=np.float32).reshape(20,3)
|
||||
y=np.tile(np.arange(10),2);qid=np.repeat([0,1],10)
|
||||
m=xgb.XGBRanker(**PARAMS);m.fit(x,y,qid=qid,verbose=False)
|
||||
check_ranker(m)
|
||||
self.assertEqual(m.get_booster().num_boosted_rounds(),100)
|
||||
self.assertTrue(np.isfinite(m.predict(x)).all())
|
||||
def test_final_or_gate(self):
|
||||
self.assertEqual(gate({'low':0},{'value':.009},[]),'PRUNE_THIS_FIXED_RANKER_RECIPE')
|
||||
self.assertEqual(gate({'low':-.1},{'value':.010},[]),'RETAIN_SINGLE_FOLD_FEASIBILITY')
|
||||
self.assertEqual(gate({'low':.001},{'value':-.1},[]),'RETAIN_SINGLE_FOLD_FEASIBILITY')
|
||||
self.assertEqual(gate({'low':.001},{'value':.1},['constant']),'PARK_UNDEFINED_OR_COVERAGE')
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
Reference in new issue
Block a user