156 lines
7.6 KiB
Python
156 lines
7.6 KiB
Python
"""CPU-only synthetic tests for track_b_transolver_greif_adapter (no real data, no GPU)."""
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import torch
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
import phase2_h2_model as h2
|
|
import track_b_gino_greif_adapter as gino
|
|
import track_b_transolver_greif_adapter as tv
|
|
|
|
|
|
def synthetic_tet_part(seed, nx=4, ny=4, nz=3, second_component=False):
|
|
"""Build a synthetic 3D tet mesh with lumped positive nodal masses and 19 features."""
|
|
rng = np.random.default_rng(seed)
|
|
xs, ys, zs = np.meshgrid(np.linspace(0.0, 1.0, nx), np.linspace(0.0, 0.8, ny), np.linspace(0.0, 0.4, nz), indexing='ij')
|
|
pts = np.column_stack([xs.ravel(), ys.ravel(), zs.ravel()])
|
|
pts += rng.normal(0.0, 0.01, size=pts.shape)
|
|
n0 = len(pts)
|
|
idx = np.arange(n0).reshape(nx, ny, nz)
|
|
tets = []
|
|
for i in range(nx - 1):
|
|
for j in range(ny - 1):
|
|
for k in range(nz - 1):
|
|
c = [idx[i + a, j + b, k + c_] for a in (0, 1) for b in (0, 1) for c_ in (0, 1)]
|
|
for t in ((0, 1, 3, 7), (0, 1, 5, 7), (0, 4, 5, 7), (0, 2, 3, 7), (0, 2, 6, 7), (0, 4, 6, 7)):
|
|
tets.append([c[p] for p in t])
|
|
tets = np.asarray(tets, dtype=np.int64)
|
|
comp = np.zeros(n0, dtype=np.int64)
|
|
if second_component:
|
|
pts2 = pts[:12] + np.array([2.0, 0.0, 0.0])
|
|
pts = np.vstack([pts, pts2])
|
|
extra = np.array([[n0 + i, n0 + i + 1, n0 + i + 2, n0 + i + 3] for i in range(0, 9, 3)], dtype=np.int64)
|
|
tets = np.vstack([tets, extra])
|
|
comp = np.concatenate([comp, np.ones(len(pts2), dtype=np.int64)])
|
|
n = len(pts)
|
|
mass = np.zeros(n, dtype=np.float64)
|
|
v0, v1, v2, v3 = pts[tets[:, 0]], pts[tets[:, 1]], pts[tets[:, 2]], pts[tets[:, 3]]
|
|
vol = np.abs(np.einsum('ij,ij->i', v1 - v0, np.cross(v2 - v0, v3 - v0))) / 6.0
|
|
vol = np.maximum(vol, 1e-6)
|
|
for corner in range(4):
|
|
np.add.at(mass, tets[:, corner], vol / 4.0)
|
|
mass = np.maximum(mass, 1e-7)
|
|
vf = np.clip(np.argsort(np.argsort(pts[:, 0])).astype(np.float64) / max(n - 1, 1), 0.0, 1.0)
|
|
feats = rng.normal(0.0, 1.0, size=(n, len(tv.COLUMNS))).astype(np.float64)
|
|
feats[:, 0] = vf
|
|
target_star = np.clip(0.95 * vf + 0.05 * rng.normal(size=n), 0.0, None)
|
|
return dict(points=pts, tets=tets, mass=mass, features=feats, Vf=vf, target_star=target_star, component_ids=comp)
|
|
|
|
|
|
class TestTransolverGreifAdapter(unittest.TestCase):
|
|
def setUp(self):
|
|
torch.set_num_threads(1)
|
|
self.parts = [synthetic_tet_part(10 + i, second_component=(i == 1)) for i in range(2)]
|
|
self.stats = tv.standardize_learning_arrays([p['features'] for p in self.parts])
|
|
self.clock = tv.learning_clock([0.96, 1.02], ['fam_a', 'fam_b'])
|
|
|
|
def test_interface_and_parameter_counts(self):
|
|
self.assertEqual(len(tv.COLUMNS), 19)
|
|
self.assertEqual(tv.COLUMNS, gino.COLUMNS)
|
|
h2_params = tv.count_parameters(h2.H2Geo())
|
|
default_params = tv.count_parameters(tv.make_greif_transolver('default'))
|
|
wide_params = tv.count_parameters(tv.make_greif_transolver('wide'))
|
|
unweighted_params = tv.count_parameters(tv.make_greif_transolver('default', mass_weighted=False))
|
|
self.assertEqual(h2_params, 195077)
|
|
self.assertEqual(default_params, 197913)
|
|
self.assertEqual(wide_params, 293937)
|
|
self.assertEqual(unweighted_params, default_params)
|
|
|
|
def test_zero_init_starts_at_exact_volume_clock_prior(self):
|
|
torch.manual_seed(0)
|
|
model = tv.make_greif_transolver('default')
|
|
p = self.parts[0]
|
|
x, m, comp = tv.prepare_inputs(p['features'], p['mass'], self.stats, p['component_ids'])
|
|
pred = model(x, m, comp, checkpoint_blocks=False)
|
|
self.assertEqual(pred.shape, (len(p['mass']),))
|
|
self.assertEqual(pred.dtype, torch.float32)
|
|
self.assertTrue(torch.allclose(pred, torch.zeros_like(pred), atol=0.0))
|
|
decoded = tv.decode_seconds(pred.detach().numpy(), p['Vf'], self.clock, t_set=2.5)
|
|
expected = tv.prior(p['Vf'], self.clock) * 2.5
|
|
np.testing.assert_allclose(decoded, expected, rtol=0.0, atol=1e-12)
|
|
|
|
def test_forward_backward_step_and_multicomponent(self):
|
|
torch.manual_seed(7)
|
|
model = tv.make_greif_transolver('default')
|
|
opt = tv.optimizer(model)
|
|
total_nodes = sum(len(p['mass']) for p in self.parts)
|
|
for p in self.parts:
|
|
x, m, comp = tv.prepare_inputs(p['features'], p['mass'], self.stats, p['component_ids'])
|
|
target = torch.from_numpy(tv.residual_target(p['target_star'], p['Vf'], self.clock, fit_dtype='float32'))
|
|
opt.zero_grad(set_to_none=True)
|
|
pred = model(x, m, comp, node_chunk=17, checkpoint_blocks=True)
|
|
loss = tv.weighted_loss(pred, target, len(self.parts), len(m), total_nodes)
|
|
self.assertTrue(bool(torch.isfinite(loss)))
|
|
loss.backward()
|
|
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0, error_if_nonfinite=True)
|
|
opt.step()
|
|
self.assertTrue(all(bool(torch.isfinite(v).all()) for v in model.parameters()))
|
|
# After 1 step, head output moves away from zero and gradients reached encoder + physics slices.
|
|
self.assertGreater(float(model.head[-1].weight.detach().abs().max()), 0.0)
|
|
x, m, comp = tv.prepare_inputs(self.parts[1]['features'], self.parts[1]['mass'], self.stats, self.parts[1]['component_ids'])
|
|
out = model(x, m, comp, checkpoint_blocks=False)
|
|
out.sum().backward()
|
|
self.assertGreater(float(model.encoder[0].weight.grad.abs().max()), 0.0)
|
|
self.assertGreater(float(model.blocks[0].attn.in_project_slice.weight.grad.abs().max()), 0.0)
|
|
|
|
def test_determinism_chunk_invariance_and_mass_scale_invariance(self):
|
|
p = self.parts[1]
|
|
x, m, comp = tv.prepare_inputs(p['features'], p['mass'], self.stats, p['component_ids'])
|
|
target = torch.from_numpy(tv.residual_target(p['target_star'], p['Vf'], self.clock, fit_dtype='float32'))
|
|
|
|
def run_once(chunk, mass_scale=1.0):
|
|
torch.manual_seed(1234)
|
|
model = tv.make_greif_transolver('default')
|
|
opt = tv.optimizer(model)
|
|
for _ in range(2):
|
|
opt.zero_grad(set_to_none=True)
|
|
pred = model(x, m * mass_scale, comp, node_chunk=chunk, checkpoint_blocks=False)
|
|
loss = tv.weighted_loss(pred, target, 1, len(m), len(m))
|
|
loss.backward()
|
|
opt.step()
|
|
model.eval()
|
|
with torch.no_grad():
|
|
return model(x, m * mass_scale, comp, node_chunk=chunk, checkpoint_blocks=False)
|
|
|
|
a = run_once(chunk=tv.NODE_CHUNK)
|
|
b = run_once(chunk=tv.NODE_CHUNK)
|
|
c = run_once(chunk=13)
|
|
d = run_once(chunk=tv.NODE_CHUNK, mass_scale=1e-4)
|
|
self.assertTrue(torch.equal(a, b))
|
|
self.assertTrue(torch.allclose(a, c, atol=1e-6, rtol=1e-5))
|
|
self.assertTrue(torch.allclose(a, d, atol=1e-6, rtol=1e-5))
|
|
|
|
def test_input_guards(self):
|
|
model = tv.make_greif_transolver('default')
|
|
p = self.parts[0]
|
|
x, m, comp = tv.prepare_inputs(p['features'], p['mass'], self.stats, p['component_ids'])
|
|
with self.assertRaises(ValueError):
|
|
model(x[:, :18], m, comp)
|
|
with self.assertRaises(ValueError):
|
|
model(x.double(), m, comp)
|
|
bad_m = m.clone()
|
|
bad_m[0] = 0.0
|
|
with self.assertRaises(ValueError):
|
|
model(x, bad_m, comp)
|
|
bad_x = x.clone()
|
|
bad_x[0, 0] = float('nan')
|
|
with self.assertRaises(ValueError):
|
|
model(bad_x, m, comp)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main(verbosity=2)
|