Implement bounded O1 geometry preparation with METIS and synthetic acceptance

This commit is contained in:
Frank14f committed 2026-11-17 04:52:02 +08:00
1 parent 7f820b76ea
commit afec2dce6a
34 files changed
+124863

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
"""Synthetic partition fingerprint from one fresh process, never real data."""
import argparse
import hashlib
import json
import os
from pathlib import Path
from test_o1_partition import box
from o1_partition import canonical_mesh, graph, Metis, partition_metrics
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('output',type=Path);a=p.parse_args()
mesh=canonical_mesh(*box());g=graph(mesh);labels,call=Metis().partition(g,mesh['raw_cells'],0)
record=dict(pid=os.getpid(),seed=0,synthetic_only=True,labels_sha256=hashlib.sha256(labels.tobytes()).hexdigest(),
graph_sha256={k:hashlib.sha256(g[k].tobytes()).hexdigest() for k in ['xadj','adjncy','vwgt','adjwgt']},
options=call['options'],metrics=partition_metrics(mesh,g,labels))
a.output.write_text(json.dumps(record,indent=2)+'\n')
+292
View File
@@ -0,0 +1,292 @@
"""O1 fixed METIS geometry library and label-free regional readout.
No label loader, model, geometry-dependent seed search or partition repair.
"""
import ctypes
import hashlib
from pathlib import Path
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components
from scipy.spatial import ConvexHull, distance
LIBRARY = '/usr/lib/x86_64-linux-gnu/libmetis.so.5.1.0'
LIBRARY_SHA = 'd0ad52903eee2ae1f79540a9de989acd7ddccff1b7499bbcb466184c15cbc4f7'
HEADER_SHA = '20826dae4a5ff3fce9ad5fd0cf28ed0f8899e1cbdab1e318dfb33c8123840616'
K = 8
Q = 2**26
# metis.h 5.1.0 enumeration indices; all unspecified options retain -1.
FIXED_OPTIONS = {1: 0, 2: 1, 3: 4, 4: 1, 5: 0, 6: 10, 7: 1,
9: 0, 10: 0, 11: 1, 16: 50, 17: 0}
class InputViolation(ValueError):
"""Frozen identity/hash/role mismatch: stop the whole queue."""
class GeometryFailure(ValueError):
"""A preserved local preparation or scope failure."""
def sha(path):
h = hashlib.sha256()
with Path(path).open('rb') as f:
for chunk in iter(lambda: f.read(4*2**20), b''):
h.update(chunk)
return h.hexdigest()
def checked(path, digest):
if sha(path) != digest:
raise InputViolation('Pinned hash changed: '+str(path))
return Path(path)
def quantize(a):
a = np.asarray(a, dtype=np.float64)
if a.ndim != 1 or not 0 < len(a) <= Q or not np.isfinite(a).all() or np.any(a <= 0):
raise GeometryFailure('INT_WEIGHT_RANGE')
total = a.sum(dtype=np.float64)
if not np.isfinite(total) or total <= 0:
raise GeometryFailure('INT_WEIGHT_RANGE')
fraction = a/total
weights = 1 + np.floor(Q*fraction).astype(np.int64)
if weights.sum() > 2*Q or weights.max() > np.iinfo(np.int32).max:
raise GeometryFailure('INT_WEIGHT_RANGE')
error = np.abs(weights/weights.sum() - fraction)
return weights.astype(np.int32), dict(L1=float(error.sum()), max_absolute=float(error.max()))
def canonical_mesh(points, tets, raw_nodes, raw_cells):
x = np.asarray(points, dtype=np.float64)
t = np.asarray(tets)
rn, rc = np.asarray(raw_nodes), np.asarray(raw_cells)
if (x.ndim != 2 or x.shape[1] != 3 or not np.isfinite(x).all() or
t.ndim != 2 or t.shape[1] != 4 or t.dtype.kind not in 'iu' or
rn.shape != (len(x),) or rc.shape != (len(t),) or
rn.dtype.kind not in 'iu' or rc.dtype.kind not in 'iu' or
len(t) < K or len(np.unique(rn)) != len(rn) or
len(np.unique(rc)) != len(rc) or np.any(rn < 0) or np.any(rc < 0)):
raise GeometryFailure('INVALID_MESH_IDENTITY')
if t.min() < 0 or t.max() >= len(x):
raise GeometryFailure('TET_INDEX_RANGE')
node_order, cell_order = np.argsort(rn, kind='stable'), np.argsort(rc, kind='stable')
inverse = np.empty(len(x), dtype=np.int64); inverse[node_order] = np.arange(len(x))
t = np.sort(inverse[t[cell_order]], axis=1)
x, rn, rc = x[node_order], rn[node_order], rc[cell_order]
if np.any(np.diff(t, axis=1) == 0) or len(np.unique(t, axis=0)) != len(t):
raise GeometryFailure('DUPLICATE_OR_INVALID_TET')
if not np.array_equal(np.unique(t), np.arange(len(x))):
raise GeometryFailure('UNREFERENCED_NODE')
cells = x[t]
volume = np.abs(np.linalg.det(cells[:, 1:]-cells[:, :1]))/6
if not np.isfinite(volume).all() or np.any(volume <= 0):
raise GeometryFailure('DEGENERATE_VOLUME')
return dict(points=x, tets=t, raw_nodes=rn, raw_cells=rc, volume=volume,
node_order=node_order, cell_order=cell_order)
def graph(mesh):
x, t = mesh['points'], mesh['tets']
face_pattern = np.array([[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]])
faces = t[:, face_pattern].reshape(-1, 3) # canonical t already sorted
owners = np.repeat(np.arange(len(t)), 4)
order = np.lexsort((faces[:, 2], faces[:, 1], faces[:, 0]))
faces, owners = faces[order], owners[order]
starts = np.r_[0, np.flatnonzero(np.any(faces[1:] != faces[:-1], axis=1))+1]
counts = np.diff(np.r_[starts, len(faces)])
if np.any(counts > 2):
raise GeometryFailure('NONMANIFOLD_FACE')
idx = starts[counts == 2]
face = faces[idx]
edges = np.sort(np.column_stack((owners[idx], owners[idx+1])), axis=1)
if not len(edges):
raise GeometryFailure('MULTICOMPONENT_SCOPE')
vertices = x[face]
area = np.linalg.norm(np.cross(vertices[:, 1]-vertices[:, 0],
vertices[:, 2]-vertices[:, 0]), axis=1)/2
if not np.isfinite(area).all() or np.any(area <= 0):
raise GeometryFailure('DEGENERATE_FACE')
vwgt, ve = quantize(mesh['volume']); ewgt, ee = quantize(area)
directed = np.concatenate((edges, edges[:, ::-1]))
weights = np.tile(ewgt, 2)
order = np.lexsort((directed[:, 1], directed[:, 0]))
directed, weights = directed[order], weights[order]
offsets = np.r_[0, np.cumsum(np.bincount(directed[:, 0], minlength=len(t)))]
if len(t) >= 2**31 or offsets[-1] >= 2**31 or weights.astype(np.int64).sum() > 4*Q:
raise GeometryFailure('CSR_OR_WEIGHT_OVERFLOW')
adjacency = csr_matrix((np.ones(len(directed)), directed[:, 1], offsets), shape=(len(t), len(t)))
nc = connected_components(adjacency, directed=False, return_labels=False)
if nc != 1:
raise GeometryFailure('MULTICOMPONENT_SCOPE')
return dict(xadj=offsets.astype(np.int32), adjncy=directed[:, 1].astype(np.int32),
vwgt=vwgt, adjwgt=weights.astype(np.int32), edges=edges,
faces_raw=mesh['raw_nodes'][face], area=area,
quantization=dict(volume=ve, face_area=ee), adjacency=adjacency)
def canonical_labels(labels, raw_cells):
labels = np.asarray(labels)
if labels.shape != np.asarray(raw_cells).shape or labels.dtype.kind not in 'iu':
raise GeometryFailure('PARTITION_SHAPE')
unique = np.unique(labels)
regions = sorted(unique, key=lambda r: int(np.min(raw_cells[labels == r])))
out = np.empty(len(labels), dtype=np.int32)
for i, r in enumerate(regions):
out[labels == r] = i
return out
class Metis:
def __init__(self):
checked(LIBRARY, LIBRARY_SHA); checked('/usr/include/metis.h', HEADER_SHA)
self.lib = ctypes.CDLL(LIBRARY)
self.i = ctypes.c_int32; self.r = ctypes.c_float
self.ip = ctypes.POINTER(self.i); self.rp = ctypes.POINTER(self.r)
self.lib.METIS_SetDefaultOptions.argtypes = [self.ip]
self.lib.METIS_SetDefaultOptions.restype = ctypes.c_int
self.lib.METIS_PartGraphKway.argtypes = [self.ip]*8+[self.rp]*2+[self.ip]*3
self.lib.METIS_PartGraphKway.restype = ctypes.c_int
def options(self, seed):
if seed not in range(32):
raise ValueError('Only fixed seeds 0..31')
options = np.empty(40, dtype=np.int32)
if self.lib.METIS_SetDefaultOptions(options.ctypes.data_as(self.ip)) != 1:
raise InputViolation('Default options initialization failed')
for k, v in FIXED_OPTIONS.items(): options[k] = v
options[8] = seed
return options
def partition(self, g, raw_cells, seed):
n, ncon, k, cut = self.i(len(raw_cells)), self.i(1), self.i(K), self.i()
options = self.options(seed)
part = np.empty(n.value, dtype=np.int32)
target = np.full(K, 1/K, dtype=np.float32)
arrays = [np.ascontiguousarray(g[key], dtype=np.int32) for key in ['xadj', 'adjncy', 'vwgt', 'adjwgt']]
ptr = [a.ctypes.data_as(self.ip) for a in arrays]
code = self.lib.METIS_PartGraphKway(ctypes.byref(n), ctypes.byref(ncon),
ptr[0], ptr[1], ptr[2], None, ptr[3], ctypes.byref(k),
target.ctypes.data_as(self.rp), None, options.ctypes.data_as(self.ip),
ctypes.byref(cut), part.ctypes.data_as(self.ip))
if code != 1:
raise GeometryFailure('METIS_RETURN_'+str(code))
if np.any(part < 0) or np.any(part >= K):
raise GeometryFailure('METIS_LABEL_RANGE')
return canonical_labels(part, raw_cells), dict(returncode=code, integer_cut=cut.value, options=options.tolist())
def diameter(points, block=256):
x = np.asarray(points, dtype=np.float64)
if len(x) < 4:
raise GeometryFailure('DEGENERATE_REGION')
# No QJ/random perturbation. Region includes positive-volume tets -> 3D.
hull = ConvexHull(x)
vertices = x[hull.vertices]
best = 0.
for i in range(0, len(vertices), block):
for j in range(i, len(vertices), block):
best = max(best, float(distance.cdist(vertices[i:i+block], vertices[j:j+block], 'sqeuclidean').max()))
return float(np.sqrt(best)), len(vertices)
def partition_metrics(mesh, g, labels):
if not np.array_equal(np.unique(labels), np.arange(K)):
raise GeometryFailure('NOT_EIGHT_NONEMPTY')
volume = np.bincount(labels, weights=mesh['volume'], minlength=K)
fractions = volume/mesh['volume'].sum()
if np.any(fractions < .95/K) or np.any(fractions > 1.05/K):
raise GeometryFailure('PHYSICAL_VOLUME_BALANCE')
diameters, hull_counts = [], []
for r in range(K):
ids = np.flatnonzero(labels == r)
if connected_components(g['adjacency'][ids][:, ids], directed=False, return_labels=False) != 1:
raise GeometryFailure('REGION_DISCONNECTED')
nodes = np.unique(mesh['tets'][ids])
d, h = diameter(mesh['points'][nodes]); diameters.append(d); hull_counts.append(h)
interface = labels[g['edges'][:, 0]] != labels[g['edges'][:, 1]]
cut = float(g['area'][interface].sum())
if cut <= 0 or not np.isfinite(cut):
raise GeometryFailure('DEGENERATE_CUT')
return dict(fractions=sorted(fractions.tolist()), cut_area=cut,
diameters=sorted(diameters), hull_vertices=hull_counts,
fraction_margin_min=float(min(fractions.min()-.95/K, 1.05/K-fractions.max())))
def match(a, b, mutual=False):
av, bv = np.array(a['fractions']), np.array(b['fractions'])
ad, bd = np.array(a['diameters']), np.array(b['diameters'])
area_den = min(a['cut_area'], b['cut_area']) if mutual else b['cut_area']
diam_den = np.minimum(ad, bd) if mutual else bd
dv = float(K*np.abs(av-bv).max())
da = float(abs(a['cut_area']-b['cut_area'])/area_den)
dd = float((np.abs(ad-bd)/diam_den).max())
return dict(passed=bool(dv <= .025 and da <= .05 and dd <= .05),
scaled_volume_max=dv, cut_area_relative=da, diameter_relative_max=dd)
def type_fields(features, tets):
f = np.asarray(features, dtype=np.float64)
if f.ndim != 2 or f.shape[1] != 19 or not np.isfinite(f).all():
raise InputViolation('Invalid frozen 19-column features')
s = np.empty((len(f), 3, 3))
for col, i, j in [(13,0,0),(14,0,1),(15,0,2),(16,1,1),(17,1,2),(18,2,2)]:
s[:, i, j] = s[:, j, i] = f[:, col]
eig = np.linalg.eigvalsh(s)[:, ::-1]
z = np.column_stack((f[:, 9], eig[:, 0]-eig[:, 1], eig[:, 1]-eig[:, 2]))
return z[tets].mean(axis=1), f[:, [0, 9]][tets].mean(axis=1)
def shuffle_type(z, volume, raw_cells, numeric_case):
if not isinstance(numeric_case, int) or numeric_case < 0:
raise InputViolation('Numeric original case prefix required')
order = np.lexsort((raw_cells, volume))
rng = np.random.Generator(np.random.PCG64(np.random.SeedSequence([2026111701, numeric_case])))
perm = np.arange(len(z))
for ids in np.array_split(order, 10):
perm[ids] = rng.permutation(ids)
return z[perm], perm
def regional_mean(values, volume, labels):
result = np.zeros((K, values.shape[1]))
sums = np.bincount(labels, weights=volume, minlength=K)
if np.any(sums <= 0): raise GeometryFailure('EMPTY_REGION')
for j in range(values.shape[1]):
result[:, j] = np.bincount(labels, weights=volume*values[:, j], minlength=K)/sums
return result
def objective(z, volume, labels):
mean = regional_mean(z, volume, labels)
value = float(np.sum(volume*np.sum((z-mean[labels])**2, axis=1))/volume.sum())
if not np.isfinite(value): raise GeometryFailure('NONFINITE_OBJECTIVE')
return value
def readout(tets, volume, tet_values, labels, node_count):
means = regional_mean(tet_values, volume, labels)
denominator = np.bincount(tets.ravel(), weights=np.repeat(volume, 4), minlength=node_count)
if np.any(denominator <= 0): raise GeometryFailure('UNSUPPORTED_NODE')
out = np.empty((node_count, tet_values.shape[1]))
for j in range(out.shape[1]):
out[:, j] = np.bincount(tets.ravel(), weights=np.repeat(volume*means[labels, j], 4),
minlength=node_count)/denominator
return out
def interface_difference(a, b, g):
edges = g['edges']
ai, bi = a[edges[:, 0]] != a[edges[:, 1]], b[edges[:, 0]] != b[edges[:, 1]]
union = float(g['area'][ai | bi].sum())
return dict(value=float(g['area'][ai ^ bi].sum()/union) if union else 0., empty_union=not bool(union))
def common_roster(parts, results):
bad = {p['component_id'] for p in parts if results.get(p['case'], {}).get('status') != 'PASS'}
return [p for p in parts if p['component_id'] not in bad], sorted(bad)
def can_reserve(spent, active, cap=43200., per_job=1800.):
return bool(np.isfinite(spent) and spent >= 0 and spent+(active+1)*per_job <= cap)
+209
View File
@@ -0,0 +1,209 @@
"""One O1 preparation attempt: immutable geometry/features only, no labels."""
import argparse
import json
import os
from pathlib import Path
import time
import traceback
from xml.etree import ElementTree as ET
from zipfile import ZipFile
import numpy as np
from scipy.spatial import QhullError
from audit_samples import decode_array
from gre24_features import COLUMNS
from o1_partition import (Metis, InputViolation, GeometryFailure, checked, sha,
canonical_mesh, graph, partition_metrics, match, type_fields, shuffle_type,
objective, readout, interface_difference)
from s2_recovery_supervisor import save
def read(path):
return json.loads(Path(path).read_text())
def contract(root):
c = read(Path(root)/'contract.json')
for p, h in c['source_sha256'].items(): checked(p, h)
for p, h in c['code_sha256'].items(): checked(p, h)
stat = Path(c['archive']).stat(); old = c['archive_stat']
if (stat.st_size, stat.st_mtime_ns, stat.st_ino) != (old['bytes'], old['mtime_ns'], old['inode']):
raise InputViolation('Frozen source archive stat changed')
return c
def source_geometry(archive, case):
"""Stop before field arrays; keep actual mixed-VTU cell IDs."""
stack = []; cells = {}; points = None; decoded = []
with ZipFile(archive) as z, z.open(case+'_sim.vtu') as stream:
for event, element in ET.iterparse(stream, events=('start', 'end')):
if event == 'start':
stack.append(element.tag)
if element.tag == 'VTKFile':
if element.get('byte_order') != 'LittleEndian' or element.get('compressor') != 'vtkZLibDataCompressor':
raise InputViolation('Unsupported source encoding')
header = element.get('header_type', 'UInt32')
if element.tag in ('PointData', 'CellData'):
raise InputViolation('Refuse source field section')
else:
if element.tag == 'DataArray':
parent = stack[-2]
if parent == 'Points':
points = decode_array(element, header).copy(); decoded.append('Points')
elif parent == 'Cells':
name = element.get('Name')
if name not in ('connectivity', 'offsets', 'types'):
raise InputViolation('Unexpected Cells array')
cells[name] = decode_array(element, header).copy(); decoded.append(name)
element.clear()
if points is not None and all(k in cells for k in ('connectivity', 'offsets', 'types')):
break
stack.pop()
if points is None or set(cells) != {'connectivity', 'offsets', 'types'}:
raise InputViolation('Missing geometry before field section')
offsets = np.r_[0, cells['offsets']]
ids = np.flatnonzero(cells['types'] == 10)
if not len(ids) or np.any(np.diff(offsets)[ids] != 4):
raise GeometryFailure('INVALID_SOURCE_TET')
raw_tets = cells['connectivity'][offsets[ids, None]+np.arange(4)]
raw_nodes = np.unique(raw_tets)
t = np.searchsorted(raw_nodes, raw_tets)
return canonical_mesh(points[raw_nodes], t, raw_nodes, ids), decoded
def load_inputs(c, row):
v, p = row['v5_cache'], row['preparation']
vr, pr = Path(v['output']), Path(p['preparation_root'])
files = {}
def check(path, digest):
checked(path, digest); files[str(path)] = digest; return path
old = read(check(pr/'result.json', p['result_sha256']))
new = read(check(vr/'result.json', v['result_sha256']))
if old['case'] != row['case'] or new['case'] != row['case'] or new['status'] != 'PASS':
raise InputViolation('Preparation identity/status')
if new['features']['columns'] != list(COLUMNS) or new['t_scale_seconds'] != p['t_set'] or v['t_scale_seconds'] != p['t_set']:
raise InputViolation('Frozen feature columns/clock')
mesh, decoded = source_geometry(c['archive'], row['case'])
with np.load(check(pr/'geometry.npz', p['output_sha256']['geometry.npz']), allow_pickle=False) as z:
if not np.array_equal(z['raw_node_ids'], mesh['raw_nodes']) or not np.array_equal(z['points_raw'], mesh['points']):
raise InputViolation('Source/cache raw node coordinates')
t = np.sort(z['tetra'], axis=1); expected = mesh['tets']
order = lambda a: np.lexsort(tuple(a[:, i] for i in (3, 2, 1, 0)))
if not np.array_equal(t[order(t)], expected[order(expected)]):
raise InputViolation('Source/cache tetrahedra differ')
raw = np.load(check(vr/'raw_ids.npy', v['output_sha256']['raw_ids.npy']), allow_pickle=False)
if not np.array_equal(raw, mesh['raw_nodes']): raise InputViolation('Feature raw IDs differ')
features = np.load(check(vr/'features.npy', v['output_sha256']['features.npy']), allow_pickle=False)
if features.shape != (len(raw), 19) or not np.isfinite(features).all():
raise InputViolation('Invalid feature coverage/values')
with np.load(check(pr/'queries.npz', p['output_sha256']['queries.npz']), allow_pickle=False) as z:
keep = z['eligible_s2']; queries = z['raw_node_ids'][keep]; counts = z['multiplicity'][keep]
qi = np.searchsorted(raw, queries)
if (not len(qi) or np.any(qi >= len(raw)) or np.any(np.diff(queries) <= 0) or
np.any(counts <= 0) or not np.array_equal(raw[qi], queries)):
raise InputViolation('Query identity/multiplicity')
return mesh, features, dict(indices=qi, raw_ids=queries, counts=counts), dict(files=files, decoded_geometry_arrays=decoded,
labels_decoded=[], label_cache_opened=False, raw_cells_are_original_VTU_indices=True)
def difference(a, b):
d = np.abs(a-b)
return [dict(median=float(np.median(d[:, j])), p95=float(np.quantile(d[:, j], .95)),
nonzero_fraction=float(np.count_nonzero(d[:, j])/len(d))) for j in range(2)]
def prepare(root, index):
root = Path(root); c = contract(root); row = c['parts'][index]
dest = root/'parts'/row['case']/'output'; dest.mkdir(parents=True, exist_ok=False)
start = time.process_time(); wall = time.monotonic(); stage = 'INPUTS'; candidates = []
def progress(**kw):
save(dest/'progress.json', dict(stage=stage, case=row['case'], pid=os.getpid(),
cpu_seconds=time.process_time()-start, wall_seconds=time.monotonic()-wall, **kw))
try:
progress(); mesh, features, queries, provenance = load_inputs(c, row)
save(dest/'input-provenance.json', provenance)
stage = 'GEOMETRY_LIBRARY'; progress(tets=len(mesh['tets']))
g = graph(mesh); engine = Metis()
np.savez_compressed(dest/'graph.npz', **{k:g[k] for k in ('xadj','adjncy','vwgt','adjwgt','edges','faces_raw','area')},
raw_nodes=mesh['raw_nodes'], raw_cells=mesh['raw_cells'], volume=mesh['volume'])
save(dest/'graph.json', dict(nodes=len(mesh['points']), tets=len(mesh['tets']), faces=len(g['area']),
quantization=g['quantization'], graph_sha256=sha(dest/'graph.npz')))
reference = None; seen = {}; legal = []
for seed in range(32):
before = time.process_time(); rec = dict(seed=seed)
progress(seed=seed, tets=len(mesh['tets']), candidates_terminal=len(candidates))
try:
labels, call = engine.partition(g, mesh['raw_cells'], seed); rec.update(call)
path = dest/f'partition-{seed:02d}.npz'
np.savez_compressed(path, labels=labels)
rec.update(path=str(path), sha256=sha(path))
digest = __import__('hashlib').sha256(labels.tobytes()).hexdigest()
rec['canonical_label_sha256'] = digest
if digest in seen:
rec.update(status='DUPLICATE', earliest_seed=seen[digest])
else:
seen[digest] = seed
rec['physical_fractions'] = (np.bincount(labels, weights=mesh['volume'], minlength=8)/mesh['volume'].sum()).tolist()
metrics = partition_metrics(mesh, g, labels); rec['metrics'] = metrics
if seed == 0: reference = metrics
rec['matching_G'] = match(metrics, reference)
rec['status'] = 'LEGAL' if rec['matching_G']['passed'] else 'MATCH_FAIL'
if rec['status'] == 'LEGAL': legal.append(rec)
except (GeometryFailure, QhullError) as error:
rec.update(status='CANDIDATE_FAILED', cause=type(error).__name__, message=str(error))
rec['cpu_seconds'] = time.process_time()-before
candidates.append(rec); save(dest/'candidate-records.json', candidates)
if seed == 0 and rec['status'] != 'LEGAL':
raise GeometryFailure('G_SEED0_INVALID')
stage = 'TYPE_SELECTION'; progress(tets=len(mesh['tets']), candidates_terminal=32)
z, b = type_fields(features, mesh['tets'])
shuffled, permutation = shuffle_type(z, mesh['volume'], mesh['raw_cells'], row['numeric_case'])
np.save(dest/'shuffle-permutation.npy', permutation)
for rec in legal:
with np.load(rec['path'], allow_pickle=False) as source: labels = source['labels']
rec['J_true'] = objective(z, mesh['volume'], labels)
rec['J_shuffled'] = objective(shuffled, mesh['volume'], labels)
selected = dict(G=legal[0], T=min(legal, key=lambda r:(r['J_true'],r['seed'])),
C=min(legal, key=lambda r:(r['J_shuffled'],r['seed'])))
mutual = match(selected['T']['metrics'], selected['C']['metrics'], mutual=True)
save(dest/'selection.json', dict(seeds={a:r['seed'] for a,r in selected.items()}, mutual=mutual,
legal_candidates=legal, shuffle_sha256=sha(dest/'shuffle-permutation.npy'),
z_min=z.min(axis=0).tolist(), z_max=z.max(axis=0).tolist()))
if not mutual['passed']: raise GeometryFailure('T_C_MUTUAL_MATCH_FAIL')
stage = 'READOUT'; progress(tets=len(mesh['tets']), candidates_terminal=32)
fields = {}; labels_by_arm = {}
for arm, rec in selected.items():
with np.load(rec['path'], allow_pickle=False) as source: labels = source['labels']
labels_by_arm[arm] = labels
fields[arm] = readout(mesh['tets'], mesh['volume'], b, labels, len(features))
if not np.isfinite(fields[arm]).all(): raise GeometryFailure('NONFINITE_READOUT')
np.save(dest/(arm+'-extra.npy'), fields[arm])
qi = queries['indices']; diagnostics = {}
for arm in ('G', 'C'):
diagnostics['T/'+arm] = dict(interface=interface_difference(labels_by_arm['T'], labels_by_arm[arm], g),
float64=difference(fields['T'][qi], fields[arm][qi]),
float32=difference(fields['T'][qi].astype(np.float32), fields[arm][qi].astype(np.float32)))
np.savez_compressed(dest/'queries.npz', **queries)
save(dest/'result.json', dict(status='PASS',case=row['case'],nodes=len(features),tets=len(mesh['tets']),
faces=len(g['area']),candidate_calls=32,unique_candidates=len(seen),legal_candidates=len(legal),
seeds={a:r['seed'] for a,r in selected.items()},diagnostics=diagnostics,
output_sha256={p.name:sha(p) for p in dest.iterdir() if p.suffix in ('.npz','.npy')},
input_provenance_sha256=sha(dest/'input-provenance.json'), selection_sha256=sha(dest/'selection.json'),
cpu_seconds=time.process_time()-start,wall_seconds=time.monotonic()-wall,
original_features_unchanged=True,labels_decoded=[],label_cache_opened=False,model_fit=False))
stage = 'COMPLETE'; progress(tets=len(mesh['tets']), candidates_terminal=32); return 0
except GeometryFailure as error:
save(dest/'result.json', dict(status='INELIGIBLE',case=row['case'],reason=str(error),stage=stage,
candidate_calls=len(candidates),tets=row['tets'],cpu_seconds=time.process_time()-start,
labels_decoded=[],label_cache_opened=False,model_fit=False))
return 0 # completed eligibility measurement, not a valid part
except Exception as error:
save(dest/'failure.json', dict(case=row['case'],type=type(error).__name__,message=str(error),
stage=stage,global_stop=True,traceback=traceback.format_exc(),labels_decoded=[]))
return 1
if __name__ == '__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);p.add_argument('index',type=int)
a=p.parse_args();raise SystemExit(prepare(a.root,a.index))
+63
View File
@@ -0,0 +1,63 @@
"""Metadata-only O1 contract creation after synthetic acceptance; never launch."""
import argparse
import json
from pathlib import Path
import time
from o1_partition import sha, checked, InputViolation
from o1_prepare import read
from s2_recovery_supervisor import save
CODE=['o1_partition.py','o1_prepare.py','run_o1_prepare.py','prepare_o1_queue.py',
'test_o1_partition.py','test_o1_preparation.py','o1_determinism_probe.py','audit_samples.py','gre24_features.py',
'boundary_distance.py','r4bc_guard.py','s2_recovery_supervisor.py','s2_recovery_monitoring.py']
def prepare(root):
start=time.process_time()
if root.exists():raise FileExistsError('Existing O1 execution root; inspect instead of replacing')
planpath=Path('artifacts/o1-planning-2026-11-17/contract-plan.json');plan=read(planpath)
checked(plan['authoritative_contract'],plan['contract_sha256'])
sources=dict(plan['sources_sha256']);sources[str(planpath)]=sha(planpath)
sources[plan['authoritative_contract']]=plan['contract_sha256']
for p,h in sources.items():checked(p,h)
roster=read('artifacts/s2-main-preparation-2026-11-15/final-roster.json')
successful=read('artifacts/gre24-reproduction-2026-11-15/v5-models-continuation-2026-11-16/preparation-freeze.json')['successful']
original=Path('artifacts/gre24-reproduction-2026-11-15/v5-protocol/contract.json')
sources[str(original)]=sha(original);archive=read(original)
parts=[];metadata_cpu=[]
for r in sorted((r for r in roster['parts'] if r['included']),key=lambda r:int(r['case'].split('_',1)[0])):
row=dict(r); row['numeric_case']=int(row['case'].split('_',1)[0]);p=row['preparation']
resultpath=Path(p['preparation_root'])/'result.json';old=read(checked(resultpath,p['result_sha256']))
row['tets']=old['tets']; row['v5_cache']=successful.get(row['case'])
row['metadata_failure']=None if row['v5_cache'] else 'PREEXISTING_V5_FULL_FEATURE_UNAVAILABLE'
parts.append(row)
if len(parts)!=617 or len({p['component_id'] for p in parts})!=209 or len({p['numeric_case'] for p in parts})!=617:
raise InputViolation('Original roster identity changed')
receipts=sorted(Path('artifacts/o1-acceptance-2026-11-17').glob('attempt-*/worker.receipt.json'))
acceptance=[dict(path=str(p),sha256=sha(p),**read(p)) for p in receipts]
if not acceptance or acceptance[-1]['status']!='PASS':raise ValueError('No final synthetic acceptance')
accepted=read(receipts[-1].parent/'accepted-source.json')
for p,h in accepted.items():checked(p,h)
code={str(Path('tools')/n):sha(Path('tools')/n) for n in CODE}
if accepted!=code:raise ValueError('Accepted source manifest incomplete')
spent=60+sum(a['cpu_seconds'] for a in acceptance)
if time.process_time()-start>60:raise ValueError('Metadata engineering reserve exceeded')
dispatch=[i for i,p in enumerate(parts) if not p['metadata_failure']]
c=dict(version='O1-GEOMETRY-PREPARATION-v1',parts=parts,dispatch=dispatch,first_ten=dispatch[:10],
source_sha256=sources,code_sha256=code,archive=archive['archive'],archive_stat=archive['archive_stat'],
main_review=['1794860233.507079','1794861463.486809'],contract_commit='7f820b76ea6d64b3a9f8a595bc04c8434ec00ae9',
worker_cpus=[0,2,4,6],coordinator_cpu=1,limits=plan['limits'],
part_limits=dict(cpu_seconds=1800,wall_seconds=1800,rss_bytes=8*2**30,disk_bytes=100*2**30),
initial_preparation_cpu_seconds=spent,acceptance=acceptance,
prepared_without_label_values=True,fit_launch=False)
root.mkdir(parents=True)
save(root/'contract.json',c)
save(root/'prepared.json',dict(status='READY_FOR_GEOMETRY_PREPARATION_NO_FITS',parts=617,dispatch=len(dispatch),
metadata_excluded=617-len(dispatch),contract_sha256=sha(root/'contract.json'),
initial_preparation_cpu_seconds=spent,metadata_cpu_seconds_within_60_reserve=time.process_time()-start))
print(str(root),sha(root/'contract.json'))
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('root',type=Path);a=p.parse_args();prepare(a.root)
+210
View File
@@ -0,0 +1,210 @@
"""Single O1 geometry queue with first-ten gate and bounded, non-retry attempts."""
import argparse
import fcntl
import json
import math
import os
from pathlib import Path
import signal
import subprocess
import sys
import time
import psutil
from o1_partition import checked, sha, common_roster, can_reserve, InputViolation
from o1_prepare import contract, read
from r4bc_guard import supervise
from s2_recovery_supervisor import save, tree_usage
from s2_recovery_monitoring import disk_usage
def part_dir(root, c, index):
return Path(root)/'parts'/c['parts'][index]['case']
def guard(root, index, slot):
c = contract(root); os.sched_setaffinity(0, [c['worker_cpus'][slot]])
dest = part_dir(root, c, index)
receipt = supervise([sys.executable, 'tools/o1_prepare.py', str(root), str(index)], dest,
c['part_limits'], global_stop=root/'global-stop.json')
output = dest/'output/result.json'; failure = dest/'output/failure.json'
failed = read(failure) if failure.exists() else None
unexplained = receipt['status'] != 'PASS' and not receipt['stop_reason'] and failed is None
global_failure = bool(receipt['monitor_errors']) or bool(failed and failed['global_stop']) or unexplained
if receipt['status'] == 'PASS' and not output.exists():
receipt.update(status='FAILED', stop_reason='MISSING_RESULT'); global_failure = True
record = dict(index=index,case=c['parts'][index]['case'],worker=receipt,
result_path=str(output) if output.exists() else None,
result_sha256=sha(output) if output.exists() else None,
global_failure=global_failure,guard_cpu_seconds=time.process_time())
save(dest/'guard-result.json', record)
return 0 if receipt['status'] == 'PASS' else 1
def cost_check(c, records):
by = {r['index']:r for r in records}
samples = []; rows = []
for i in c['first_ten']:
r = by.get(i)
if r is None:
return dict(passed=False, reason='FIRST_TEN_INCOMPLETE')
result = read(checked(r['result_path'],r['result_sha256'])) if r['result_path'] else {}
rows.append(dict(case=r['case'],worker_status=r['worker']['status'],
part_status=result.get('status'),reason=result.get('reason'),
cpu_seconds=r['worker']['cpu_seconds'],tets=c['parts'][i]['tets'],
candidate_calls=result.get('candidate_calls',0)))
if r['worker']['status']=='PASS' and result.get('candidate_calls')==32:
samples.append(r['worker']['cpu_seconds']/c['parts'][i]['tets'])
spent = c['initial_preparation_cpu_seconds']+sum(r['worker']['cpu_seconds'] for r in records)
if not samples:
return dict(passed=False,reason='NO_COMPLETE_32_SEED_COST_SAMPLE',samples=rows,spent_cpu_seconds=spent)
rate = max(samples)
remaining = sum(c['parts'][i]['tets'] for i in c['dispatch'] if i not in by)*rate
return dict(passed=bool(spent+remaining <= c['limits']['preparation_cpu_seconds']),
reason='PROJECTED_WITHIN_CAP' if spent+remaining <= c['limits']['preparation_cpu_seconds'] else 'COST_PROJECTION_STOP',
samples=rows,measured_max_cpu_per_tet=rate,remaining_projected_cpu_seconds=remaining,
spent_cpu_seconds=spent,total_projected_cpu_seconds=spent+remaining,
method='Maximum measured complete-32-candidate CPU/tet times remaining tet counts; heuristic only; hard guards remain authoritative.')
def freeze_results(root, c, records, complete):
results = {p['case']:dict(status='INELIGIBLE',reason=p['metadata_failure']) for p in c['parts'] if p.get('metadata_failure')}
for r in records:
if r['worker']['status']=='PASS':
result = read(checked(r['result_path'],r['result_sha256']))
else:
result = dict(status='WORKER_FAILED',reason=r['worker']['stop_reason'])
results[r['case']] = result
for p in c['parts']:
results.setdefault(p['case'],dict(status='NEVER_ATTEMPTED'))
save(root/'all-part-results.json',results)
if not complete:
return None
kept, bad = common_roster(c['parts'],results)
groups = sorted({p['component_id'] for p in kept})
strengths = {}
for group in groups:
rows = [results[p['case']] for p in kept if p['component_id']==group]
strengths[group] = {key:sum(r['diagnostics'][key]['interface']['value'] for r in rows)/len(rows) for key in ['T/G','T/C']}
strong = sum(v['T/G']>=.1 and v['T/C']>=.1 for v in strengths.values())
output = dict(status='GEOMETRY_COMMON_ROSTER_FROZEN_NO_FITS',original_parts=617,original_components=209,
parts=kept,paired_components=groups,excluded_components=bad,
component_interface_strength=strengths,strong_components=strong,
coverage_gate=len(groups)>=168,intervention_gate=strong>=math.ceil(.7*len(groups)) if groups else False,
retained_parts=len(kept),retained_nodes=sum(p['nodes'] for p in kept),
all_part_results_sha256=sha(root/'all-part-results.json'),
results={r['case']:dict(path=r['result_path'],sha256=r['result_sha256']) for r in records},
fit_started=False,labels_read=False)
save(root/'common-roster.json',output)
return sha(root/'common-roster.json')
def run(root):
root = Path(root)
with (root/'queue.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
return locked(root)
def locked(root):
c=contract(root); launch=read(root/'launch-freeze.json')
checked(root/'contract.json',launch['contract_sha256'])
if (root/'started.json').exists(): raise FileExistsError('O1 queue already attempted; no restart')
os.sched_setaffinity(0,[1]); start=time.monotonic()
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),contract_sha256=sha(root/'contract.json'),
implementation_commit=launch['implementation_commit'],cpu_affinity=sorted(os.sched_getaffinity(0)),
mode='GEOMETRY_PREPARATION_ONLY',fit_started=False))
active={};records=[];cursor=0;gate=False;cut=None;reason=None;errors=[];accounting=True;requested=[None]
def interrupt(sig,frame): requested[0]='COORDINATOR_SIGNAL_'+str(sig)
for s in (signal.SIGTERM,signal.SIGINT): signal.signal(s,interrupt)
env=dict(os.environ,OMP_NUM_THREADS='1',OPENBLAS_NUM_THREADS='1',MKL_NUM_THREADS='1',NUMEXPR_NUM_THREADS='1',CUDA_VISIBLE_DEVICES='')
try:
while True:
for slot,item in list(active.items()):
if item['process'].poll() is None: continue
path=part_dir(root,c,item['index'])/'guard-result.json'
if not path.exists():
accounting=False; reason='MISSING_GUARD_RECEIPT'
else:
r=read(path)
if not math.isfinite(r['worker']['cpu_seconds']) or r['worker']['cpu_seconds']<0:
accounting=False;reason='INVALID_ACCOUNTING'
records.append(r)
if r['global_failure']: reason='WORKER_GLOBAL_FAILURE'
item['log'].close();del active[slot]
spent=c['initial_preparation_cpu_seconds']+sum(r['worker']['cpu_seconds'] for r in records)
reason=reason or requested[0]
try:
usage=[tree_usage(item['process'].pid) for item in active.values()]
live=sum(u[0] for u in usage);rss=sum(u[1] for u in usage)+psutil.Process().memory_info().rss
disk=disk_usage(root);available=psutil.virtual_memory().available
if live+spent>c['limits']['preparation_cpu_seconds']: reason='GLOBAL_PREPARATION_CPU_LIMIT'
if rss>c['limits']['total_rss_bytes']: reason='GLOBAL_RSS_LIMIT'
if disk>=c['limits']['disk_bytes']: reason='GLOBAL_DISK_LIMIT'
except Exception as exc:
errors.append(dict(type=type(exc).__name__,message=str(exc)));reason='GLOBAL_MONITOR_ERROR'
live=rss=disk=available=0
if not gate and not active and len(records)==len(c['first_ten']) and not reason and not cut:
check=cost_check(c,records);save(root/'first-ten-cost-check.json',check)
gate=check['passed']
if not gate: cut=check['reason']
waiting=None
if not reason and not cut:
while cursor<len(c['dispatch']) and len(active)<(4 if gate else 1):
if not gate and cursor>=len(c['first_ten']):break
if not can_reserve(spent,len(active),c['limits']['preparation_cpu_seconds']):
if not active: cut='NOT_RUN_BUDGET'
else: waiting='WAIT_ACTIVE_BUDGET_SETTLEMENT'
break
if available<10*2**30: waiting='WAIT_AVAILABLE_MEMORY';break
contract(root)
index=c['dispatch'][cursor];slot=next(i for i in range(4) if i not in active)
dest=part_dir(root,c,index);dest.mkdir(parents=True,exist_ok=True)
if (dest/'worker.started.json').exists(): raise InputViolation('Attempt already exists')
log=(dest/'guard.log').open('x')
proc=subprocess.Popen([sys.executable,'tools/run_o1_prepare.py','guard',str(root),str(index),str(slot)],
stdout=log,stderr=subprocess.STDOUT,env=env,start_new_session=True)
active[slot]=dict(process=proc,index=index,log=log);cursor+=1;available-=8*2**30
if reason:
if not (root/'global-stop.json').exists():save(root/'global-stop.json',dict(reason=reason,unix=time.time()))
for item in active.values():
if not item.get('stop_sent'):
try:item['process'].send_signal(signal.SIGTERM)
except ProcessLookupError:pass
item['stop_sent']=True
save(root/'progress.json',dict(terminal_attempts=len(records),metadata_excluded=sum(bool(p.get('metadata_failure')) for p in c['parts']),
active={str(s):dict(index=v['index'],case=c['parts'][v['index']]['case'],guard_pid=v['process'].pid) for s,v in active.items()},
preparation_cpu_seconds=spent,live_guard_and_worker_cpu_seconds=live,reserved_active_cpu_seconds=len(active)*1800,
total_rss_bytes=rss,disk_bytes=disk,first_ten_gate_passed=gate,cut=cut,global_stop=reason,waiting=waiting,
monitor_errors=errors,wall_seconds=time.monotonic()-start,fit_started=False))
if not active and (reason or cut or cursor==len(c['dispatch'])):break
time.sleep(.2)
except Exception as exc:
reason='COORDINATOR_EXCEPTION'
save(root/'coordinator-failure.json',dict(type=type(exc).__name__,message=str(exc)))
save(root/'global-stop.json',dict(reason=reason))
finally:
for item in active.values():
try:item['process'].send_signal(signal.SIGTERM)
except ProcessLookupError:pass
for item in active.values():
item['process'].wait();item['log'].close();p=part_dir(root,c,item['index'])/'guard-result.json'
if p.exists():records.append(read(p))
else:accounting=False
complete=not reason and not cut and accounting and len(records)==len(c['dispatch'])
roster_hash=freeze_results(root,c,records,complete)
save(root/'complete.json',dict(status='PREPARATION_COMPLETE' if complete else 'PREPARATION_STOPPED',
global_stop=reason,cut=cut,accounting_complete=accounting,records=records,
never_attempted=[c['parts'][i]['case'] for i in c['dispatch'][cursor:]],
preparation_cpu_seconds=c['initial_preparation_cpu_seconds']+sum(r['worker']['cpu_seconds'] for r in records),
guard_cpu_seconds=sum(r.get('guard_cpu_seconds',0) for r in records),coordinator_cpu_seconds=time.process_time(),
common_roster_sha256=roster_hash,monitor_errors=errors,fit_started=False,new_label_decodes=0,
next='No auto fit launch; R4bc terminal dependency and model implementation still required'))
return 0 if complete else 1
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('mode',choices=['run','guard']);p.add_argument('root',type=Path)
p.add_argument('index',type=int,nargs='?');p.add_argument('slot',type=int,nargs='?');a=p.parse_args()
raise SystemExit(run(a.root) if a.mode=='run' else guard(a.root,a.index,a.slot))
+136
View File
@@ -0,0 +1,136 @@
"""Synthetic acceptance for the fixed O1 geometry intervention; no real data."""
import itertools
import unittest
import numpy as np
from scipy.spatial.distance import pdist
from o1_partition import (Metis, canonical_mesh, graph, canonical_labels, quantize,
partition_metrics, match, diameter, shuffle_type, readout, objective,
interface_difference, common_roster, can_reserve, GeometryFailure, InputViolation)
def box(nx=8, ny=4, nz=4):
points = np.array(list(itertools.product(range(nx+1), range(ny+1), range(nz+1))), dtype=float)
lookup = {tuple(p): i for i, p in enumerate(points)}
tets = []
for low in itertools.product(range(nx), range(ny), range(nz)):
for perm in itertools.permutations(range(3)):
p = np.array(low); cell = [lookup[tuple(p)]]
for axis in perm:
p = p.copy(); p[axis] += 1; cell.append(lookup[tuple(p)])
tets.append(cell)
return points, np.array(tets), np.arange(len(points))*3+11, np.arange(len(tets))*7+5
class O1Acceptance(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.source = box(); cls.mesh = canonical_mesh(*cls.source)
cls.g = graph(cls.mesh); cls.engine = Metis()
cls.labels, cls.call = cls.engine.partition(cls.g, cls.mesh['raw_cells'], 0)
def test_same_seed_same_input_twice_exact(self):
again, receipt = self.engine.partition(self.g, self.mesh['raw_cells'], 0)
np.testing.assert_array_equal(self.labels, again)
self.assertEqual(self.call, receipt)
def test_fixed_ufactor_and_physical_volume_reachable(self):
self.assertEqual(self.call['options'][16], 50)
self.assertEqual(self.call['options'][11], 1)
m = partition_metrics(self.mesh, self.g, self.labels)
self.assertGreaterEqual(min(m['fractions']), .95/8)
self.assertLessEqual(max(m['fractions']), 1.05/8)
self.assertEqual(len(m['diameters']), 8)
def test_csr_and_raw_identity_ignore_traversal_order(self):
x, t, rn, rc = self.source
rng = np.random.default_rng(31)
no, co = rng.permutation(len(x)), rng.permutation(len(t))
inverse = np.empty(len(x), dtype=int); inverse[no] = np.arange(len(x))
moved = canonical_mesh(x[no], inverse[t[co, ::-1]], rn[no], rc[co])
changed = graph(moved)
for key in ['points', 'tets', 'raw_nodes', 'raw_cells', 'volume']:
np.testing.assert_array_equal(self.mesh[key], moved[key])
for key in ['xadj', 'adjncy', 'vwgt', 'adjwgt', 'edges', 'area', 'faces_raw']:
np.testing.assert_array_equal(self.g[key], changed[key])
labels, _ = self.engine.partition(changed, moved['raw_cells'], 0)
np.testing.assert_array_equal(self.labels, labels)
def test_numbering_invariance_and_interface_identity(self):
relabeled = np.array([4, 6, 0, 7, 1, 3, 2, 5])[self.labels]
np.testing.assert_array_equal(canonical_labels(relabeled, self.mesh['raw_cells']), self.labels)
self.assertEqual(interface_difference(self.labels, relabeled, self.g)['value'], 0)
b = self.mesh['points'][self.mesh['tets']].mean(axis=1)[:, :2]
a = readout(self.mesh['tets'], self.mesh['volume'], b, self.labels, len(self.mesh['points']))
c = readout(self.mesh['tets'], self.mesh['volume'], b, relabeled, len(self.mesh['points']))
np.testing.assert_array_equal(a, c)
def test_boundary_blending_matches_independent_sums(self):
t, v, labels = self.mesh['tets'], self.mesh['volume'], self.labels
b = np.column_stack((np.arange(len(t))*.01, np.arange(len(t))*.02))
out = readout(t, v, b, labels, len(self.mesh['points']))
for node in [0, 34, 100, len(out)-1]:
incident = np.flatnonzero(np.any(t == node, axis=1)); expected = np.zeros(2)
for k in incident:
r = np.flatnonzero(labels == labels[k])
expected += v[k]*np.average(b[r], weights=v[r], axis=0)
expected /= v[incident].sum()
np.testing.assert_allclose(out[node], expected, rtol=1e-14, atol=1e-14)
def test_quantization_positive_bounded_and_invalid_rejected(self):
a = np.array([1e-20, 1., 20.]); w, e = quantize(a)
self.assertGreater(w.min(), 0); self.assertLessEqual(int(w.astype(np.int64).sum()), 2*2**26)
self.assertGreaterEqual(e['L1'], 0)
with self.assertRaises(GeometryFailure): quantize(np.array([0., 1.]))
def test_exact_diameter_not_bounding_box(self):
points = np.array([[0,0,0],[1,0,0],[0,1,0],[0,0,1],[.1,.1,.1]], dtype=float)
d, n = diameter(points, block=2)
self.assertEqual(d, float(pdist(points).max()))
self.assertNotEqual(d, np.sqrt(3)); self.assertEqual(n, 4)
def test_whole_triples_shuffle_only_within_volume_strata(self):
n = 103; z = np.arange(n*3).reshape(n, 3); volume = np.repeat(np.arange(11)+1, 10)[:n]
raw = np.arange(n)[::-1]
a, perm = shuffle_type(z, volume, raw, 3)
b, p2 = shuffle_type(z, volume, raw, 3)
np.testing.assert_array_equal(a, z[perm]); np.testing.assert_array_equal(a, b)
np.testing.assert_array_equal(perm, p2)
for ids in np.array_split(np.lexsort((raw, volume)), 10):
self.assertEqual(set(ids), set(perm[ids]))
with self.assertRaises(InputViolation): shuffle_type(z, volume, raw, '3')
def test_true_constant_field_objective_and_readout(self):
z = np.ones((len(self.labels), 3))*.125
self.assertAlmostEqual(objective(z, self.mesh['volume'], self.labels), 0., places=25)
out = readout(self.mesh['tets'], self.mesh['volume'], z, self.labels, len(self.mesh['points']))
np.testing.assert_allclose(out, .125, atol=1e-15)
def test_physical_and_pair_matching_hard_limits(self):
m = dict(fractions=[.125]*8, cut_area=10., diameters=[2.]*8)
self.assertTrue(match(m, m)['passed'])
self.assertFalse(match(dict(m, cut_area=10.51), m)['passed'])
self.assertFalse(match(dict(m, diameters=[2.101]*8), m)['passed'])
self.assertFalse(match(dict(m, fractions=[.12]*8), m)['passed'])
a, b = dict(m, cut_area=10.49), dict(m, cut_area=9.51)
self.assertTrue(match(a, m)['passed']); self.assertTrue(match(b, m)['passed'])
self.assertFalse(match(a, b, mutual=True)['passed'])
def test_multicomponent_and_duplicate_tets_fail(self):
x, t, rn, rc = self.source
with self.assertRaises(GeometryFailure):
canonical_mesh(x, np.vstack([t, t[:1]]), rn, np.r_[rc, rc.max()+1])
x2 = np.vstack([x, x+100]); t2 = np.vstack([t, t+len(x)])
with self.assertRaisesRegex(GeometryFailure, 'MULTICOMPONENT_SCOPE'):
graph(canonical_mesh(x2, t2, np.arange(len(x2)), np.arange(len(t2))))
def test_full_component_intersection_and_budget_reservation(self):
parts = [{'case':'a','component_id':'X'},{'case':'b','component_id':'X'},{'case':'c','component_id':'Y'}]
rows, excluded = common_roster(parts, {'a':{'status':'PASS'},'b':{'status':'FAIL'},'c':{'status':'PASS'}})
self.assertEqual(rows, parts[2:]); self.assertEqual(excluded, ['X'])
self.assertTrue(can_reserve(36000, 3)); self.assertFalse(can_reserve(36000.1, 3))
self.assertFalse(can_reserve(float('nan'), 0))
if __name__ == '__main__': unittest.main(verbosity=2)
+130
View File
@@ -0,0 +1,130 @@
"""Synthetic mixed-VTU ingestion and complete 32-seed preparation acceptance."""
import base64
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from xml.etree import ElementTree as ET
from zipfile import ZipFile
import zlib
import numpy as np
from test_o1_partition import box
from o1_partition import sha, InputViolation
from o1_prepare import prepare, source_geometry, load_inputs
from run_o1_prepare import cost_check
from gre24_features import COLUMNS
def save(path, value):
Path(path).write_text(json.dumps(value))
def array(parent, name, values, dtype, components=1):
a = np.asarray(values)
data = a.tobytes(); compressed = zlib.compress(data)
header = np.array([1,len(data),len(data),len(compressed)],dtype='<u4').tobytes()
e=ET.SubElement(parent,'DataArray',Name=name,type=dtype,format='binary',NumberOfComponents=str(components))
e.text=(base64.b64encode(header)+base64.b64encode(compressed)).decode()
def fixture(root):
x,t,_,_=box();case='00000003_synthetic_step_002'
tree=ET.Element('VTKFile',byte_order='LittleEndian',compressor='vtkZLibDataCompressor',header_type='UInt32')
piece=ET.SubElement(ET.SubElement(tree,'UnstructuredGrid'),'Piece',NumberOfPoints=str(len(x)),NumberOfCells=str(len(t)+1))
array(ET.SubElement(piece,'Points'),'Points',x.astype('<f8'),'Float64',3)
cells=ET.SubElement(piece,'Cells')
# A non-tet first cell forces actual VTU tet IDs to start at 1.
array(cells,'connectivity',np.r_[t[0,:3],t.ravel()].astype('<i8'),'Int64')
array(cells,'offsets',np.r_[3,3+4*np.arange(1,len(t)+1)].astype('<i8'),'Int64')
array(cells,'types',np.r_[5,np.full(len(t),10)].astype('u1'),'UInt8')
poison=ET.SubElement(ET.SubElement(piece,'PointData'),'DataArray',Name='Fill_time',type='Float64',format='binary')
poison.text='THIS MUST NEVER BE DECODED'
archive=root/'source.zip'
with ZipFile(archive,'w') as z:z.writestr(case+'_sim.vtu',ET.tostring(tree))
pr=root/'old';vr=root/'v5';pr.mkdir();vr.mkdir()
np.savez(pr/'geometry.npz',points_raw=x,tetra=t[::-1,::-1],raw_node_ids=np.arange(len(x)))
queries=np.array([1,3,8]);counts=np.array([4,2,1])
np.savez(pr/'queries.npz',eligible_s2=np.ones(3,dtype=bool),raw_node_ids=queries,multiplicity=counts)
features=np.zeros((len(x),19));features[:,0]=x[:,0]/8
np.save(vr/'features.npy',features);np.save(vr/'raw_ids.npy',np.arange(len(x)))
save(pr/'result.json',dict(case=case,status='PASS'))
save(vr/'result.json',dict(case=case,status='PASS',t_scale_seconds=.4,features=dict(columns=list(COLUMNS))))
row=dict(case=case,numeric_case=3,tets=len(t),nodes=len(x),component_id='synthetic',
preparation=dict(preparation_root=str(pr),t_set=.4,result_sha256=sha(pr/'result.json'),
output_sha256={p.name:sha(p) for p in pr.glob('*.npz')}),
v5_cache=dict(output=str(vr),t_scale_seconds=.4,result_sha256=sha(vr/'result.json'),
output_sha256={p.name:sha(p) for p in vr.glob('*.npy')}))
st=archive.stat();c=dict(parts=[row],archive=str(archive),archive_stat=dict(bytes=st.st_size,mtime_ns=st.st_mtime_ns,inode=st.st_ino),
source_sha256={},code_sha256={})
save(root/'contract.json',c)
return c,row,x,t
class PreparationAcceptance(unittest.TestCase):
def setUp(self):
self.temp=tempfile.TemporaryDirectory();self.root=Path(self.temp.name)
self.c,self.row,self.x,self.t=fixture(self.root)
def tearDown(self):self.temp.cleanup()
def test_mixed_cell_ids_and_no_pointdata_decode(self):
mesh,decoded=source_geometry(self.c['archive'],self.row['case'])
np.testing.assert_array_equal(mesh['raw_cells'],np.arange(1,len(self.t)+1))
self.assertEqual(decoded,['Points','connectivity','offsets','types'])
np.testing.assert_array_equal(mesh['points'],self.x)
def test_field_section_before_geometry_refused(self):
p=self.root/'bad.zip'
xml='<VTKFile byte_order="LittleEndian" compressor="vtkZLibDataCompressor"><PointData/></VTKFile>'
with ZipFile(p,'w') as z:z.writestr(self.row['case']+'_sim.vtu',xml)
with self.assertRaises(InputViolation):source_geometry(p,self.row['case'])
def test_full_32_seed_prepare_without_any_labels_or_model(self):
before=sha(Path(self.row['v5_cache']['output'])/'features.npy')
original=np.load;opened=[]
def tracked(path,*a,**kw):
opened.append(str(path))
if any(name in str(path) for name in ['target.npy','seconds.npy','label-index']):
raise AssertionError('Label read forbidden')
return original(path,*a,**kw)
with patch('numpy.load',side_effect=tracked):
self.assertEqual(prepare(self.root,0),0)
dest=self.root/'parts'/self.row['case']/'output'
result=json.loads((dest/'result.json').read_text())
self.assertEqual(result['status'],'PASS');self.assertEqual(result['candidate_calls'],32)
self.assertFalse(result['model_fit']);self.assertFalse(result['label_cache_opened'])
self.assertEqual(result['labels_decoded'],[])
self.assertEqual(sha(Path(self.row['v5_cache']['output'])/'features.npy'),before)
self.assertEqual(len(list(dest.glob('partition-*.npz'))),32)
self.assertEqual(result['seeds'],dict(G=0,T=0,C=0))
with self.assertRaises(FileExistsError):prepare(self.root,0)
def test_frozen_feature_mutation_is_global_input_failure(self):
p=Path(self.row['v5_cache']['output'])/'features.npy'
with p.open('ab') as f:f.write(b'changed')
self.assertEqual(prepare(self.root,0),1)
failure=json.loads((self.root/'parts'/self.row['case']/'output/failure.json').read_text())
self.assertTrue(failure['global_stop']);self.assertEqual(failure['type'],'InputViolation')
def test_input_alignment_preserves_queries_and_whole_features(self):
mesh,f,q,provenance=load_inputs(self.c,self.row)
np.testing.assert_array_equal(q['raw_ids'],[1,3,8]);np.testing.assert_array_equal(q['counts'],[4,2,1])
np.testing.assert_array_equal(f[:,0],self.x[:,0]/8)
self.assertEqual(provenance['labels_decoded'],[])
def test_cost_checkpoint_no_complete_library_is_not_extrapolated(self):
p=self.root/'measurement.json';save(p,dict(status='INELIGIBLE',candidate_calls=1))
c=dict(first_ten=[0],dispatch=[0,1],parts=[{'tets':100},{'tets':1000}],
initial_preparation_cpu_seconds=60,limits=dict(preparation_cpu_seconds=43200))
records=[dict(index=0,case='synthetic',result_path=str(p),result_sha256=sha(p),worker=dict(status='PASS',cpu_seconds=5))]
self.assertFalse(cost_check(c,records)['passed'])
save(p,dict(status='PASS',candidate_calls=32));records[0]['result_sha256']=sha(p)
result=cost_check(c,records);self.assertTrue(result['passed'])
self.assertEqual(result['total_projected_cpu_seconds'],115)
c['limits']['preparation_cpu_seconds']=100
self.assertFalse(cost_check(c,records)['passed'])
if __name__=='__main__':unittest.main(verbosity=2)