Files
MeltMind/tools/o1_partition.py
T

293 lines
13 KiB
Python

"""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)