Files
MeltMind/tests/test_track_b_gino_greif_adapter.py
T

110 lines
5.7 KiB
Python

"""CPU geometry, pinned spectral implementation and production guard replay."""
import ast
from pathlib import Path
import unittest
import numpy as np
import torch
import test_track_b_followup_preparation as followup
import track_b_gino_greif_prepare as prep
from track_b_gino_greif_adapter import COLUMNS, GRID, GreifGINO, deposit, gather, stencil
from track_b_gino_shapenet_entry import read, sha
class GreifAdapter(unittest.TestCase):
@classmethod
def setUpClass(cls):
torch.set_num_threads(1)
cls.candidate = read(prep.REVIEW_ROOT/'preparation-binding.json')
if cls.candidate != prep.binding():
raise ValueError('Candidate differs from the exact on-disk binding')
def test_all_19_columns_contribute_and_mass_is_preserved(self):
ids, weights = stencil([[0.,0.,0.],[.12,.29,.35],[.2,.41,.46],[1.,1.,1.]], [1,4,8,21], 8)
mass = torch.tensor([.1,1.,2.,3.], dtype=torch.float32)
x = torch.arange(76, dtype=torch.float32).reshape(4,19) / 11
grid, volume = deposit(x, mass, ids, weights, 8)
self.assertEqual(grid.shape, (20,8,8,8))
self.assertTrue(torch.allclose(volume.sum(), torch.tensor(1.)))
expected = (mass/mass.sum()) @ x
self.assertTrue(torch.allclose(grid[:19].reshape(19,-1) @ volume, expected))
self.assertTrue(torch.equal(grid[-1].reshape(-1).bool(), volume>0))
self.assertFalse(bool(torch.any(grid[:19].reshape(19,-1)[:, volume==0])))
# At the outer corner distinct from others, all channels round trip exactly.
self.assertTrue(torch.allclose(gather(grid[:19], ids, weights)[0], x[0]))
def test_exact_gather_on_affine_field_and_isotropic_translation(self):
xyz = np.array([[0.,0.,0.],[.23,.6,.4],[1.,.7,.8]])
ids = [23,25,58]
corners, weights = stencil(xyz, ids, 8)
c2, w2 = stencil(xyz + np.array([2.,-4.,3.]), ids, 8)
self.assertTrue(torch.equal(c2,corners))
self.assertTrue(torch.allclose(weights,w2,atol=1e-7))
axes = torch.meshgrid(*(torch.arange(8,dtype=torch.float32) for _ in range(3)),indexing='ij')
full = (3*axes[0] + 2*axes[1] - axes[2] + 10)[None]
lattice = (xyz - (xyz.min(0)+xyz.max(0))/2)*5 + 3.5
expected = torch.from_numpy(lattice @ np.array([3,2,-1]) + 10).float()
self.assertTrue(torch.allclose(gather(full,corners,weights)[:,0],expected))
with self.assertRaises(ValueError):
stencil(xyz, [23,58,25], 8)
def test_pinned_model_full_64_cpu_forward_and_small_backward(self):
xyz = [[0.,0.,0.],[.12,.29,.35],[.2,.41,.46],[1.,1.,1.]]
raw = [2,4,8,12]
x = torch.arange(76, dtype=torch.float32).reshape(4,19) / 100
mass = torch.ones(4)
with torch.no_grad():
v, w = stencil(xyz, raw)
core = GreifGINO().eval()
y = core(x,mass,v,w)
self.assertEqual(y.shape, (4,))
self.assertTrue(torch.equal(y,torch.zeros(4)))
self.assertEqual(core.size, GRID)
v, w = stencil(xyz, raw, 8)
core = GreifGINO(size=8,modes=(4,4,4))
with torch.no_grad():
core.readout[-1].weight.fill_(.01)
core(x,mass,v,w).sum().backward()
self.assertTrue(any(p.grad is not None and p.grad.abs().sum() > 0 for p in core.fno.parameters()))
def test_source_columns_and_h2_paired_domain(self):
pilot = prep.read(prep.PILOT)
source = Path(__file__).resolve().parents[1] / 'tools/gre24_features_v2.py'
layout = next(ast.literal_eval(s.value) for s in ast.parse(source.read_text()).body if
isinstance(s, ast.Assign) and any(isinstance(t, ast.Name) and t.id == 'COLUMNS' for t in s.targets))
self.assertEqual(layout, COLUMNS)
self.assertEqual(self.candidate['adapter_candidate']['columns'], list(layout))
self.assertEqual([self.candidate['folds'][k]['counts']['test'] for k in ('0','1')],[121,91])
self.assertEqual(self.candidate['h2_reference_support']['status'],'REFERENCE_MANIFESTS_ALIGNED')
self.assertEqual(self.candidate['epochs'], pilot['full_epochs'])
self.assertFalse(set(self.candidate['folds']['0']['parts']['test']) &
set(self.candidate['folds']['1']['parts']['test']))
def test_real_production_guard_idle_and_busy_with_frozen_limits(self):
for busy in (False, True):
followup.FollowupPreparation().replay_guard('GINO-Greif', self.candidate, busy)
def test_release_gate_requires_both_full_folds_and_respects_phase_cap(self):
c = self.candidate
rows = {f:dict(fold=int(f),seed=0,epoch=1,learning_cases=d['counts']['learning'],
learning_nodes=d['learning_nodes'],heldout_target_reads=0,
epoch_seconds=50.+5*int(f),booked_gpu_seconds=100.) for f,d in c['folds'].items()}
k = dict(other_unsettled=0.,approved_greif_cap=80000.,remaining_overhead=500.)
result = prep.first_epoch_gate(c, rows, **k)
self.assertTrue(result['release_epoch2'])
self.assertEqual(result['charged_first_epochs_and_setup_gpu_seconds'],200.)
self.assertEqual(result['remaining_train_gpu_seconds'],399*105.)
self.assertFalse(prep.first_epoch_gate(c, {'0':rows['0']}, **k)['release_epoch2'])
remainder=c['projection']['upper_remaining_before_other_bookings_gpu_seconds']
blocked=prep.first_epoch_gate(c, rows, **dict(k,other_unsettled=remainder-10000))
self.assertEqual(blocked['status'],'STOP_PROJECTED_CAP')
self.assertFalse(blocked['release_epoch2'])
with self.assertRaises(ValueError):
prep.first_epoch_gate(c, dict(rows, **{'1':dict(rows['1'],learning_cases=373)}), **k)
if __name__ == '__main__':
unittest.main()