"""Metadata, synthetic H2-P geometry, and real-guard CPU replay for both entries.""" from pathlib import Path import sys from tempfile import TemporaryDirectory import time import unittest from unittest.mock import patch import numpy as np import torch import track_b_gino_formal_guard as guard import track_b_gino_greif_prepare as greif import track_b_h2_pressure_prepare as pressure from s2_gpu_idle_gate import wait_idle from track_b_gino_shapenet_entry import read, sha REPLAYS = {} class FollowupPreparation(unittest.TestCase): @classmethod def setUpClass(cls): cls.pressure = pressure.binding() cls.greif = greif.binding() def replay_guard(self, connection, candidate, busy): with TemporaryDirectory() as folder: root = Path(folder) path = root / 'worker-records/result.json' path.parent.mkdir() commands = [] fake_elapsed = 0. uuid = 'GPU-cpu-fixture' samples = [] def sample(card): self.assertEqual(card, uuid) samples.append(card) return dict(idle=not busy, occupants=[], unix=10., memory_MiB=0) def advance(seconds): nonlocal fake_elapsed fake_elapsed += seconds def idle_with_test_clock(card, limit, record, *, sample): return wait_idle(card, limit, record, sample=sample, clock=lambda: fake_elapsed, sleep=advance) def ordinal(card, env): commands.append(card) return dict(env, CUDA_VISIBLE_DEVICES=''), [] c = candidate['production_guard_limits'] worker = [sys.executable, '-c', "from pathlib import Path;import sys;" "p=Path(sys.argv[1]);q=p.with_suffix('.tmp');" "q.write_text('{\"peak_allocated_bytes\":1,\"peak_reserved_bytes\":2}');q.replace(p)", str(path)] with (patch.object(guard, 'snapshot', side_effect=sample), patch.object(guard, 'wait_idle', side_effect=idle_with_test_clock), patch.object(guard, 'dispatch_environment', side_effect=ordinal)): began = time.monotonic() row = guard.guard(root, worker, uuid=uuid, limits=dict(c, cpu_seconds=60), deadline=began + 120, ticket=dict(ordinal='', guard_started_monotonic=began, dispatch_before_unix=time.time()+120), validate_result=lambda: sha(path)) if busy: self.assertEqual(row['trigger'], 'IDLE_DEADLINE') self.assertEqual(commands, []) self.assertFalse((root / 'actual-worker-spawn.json').exists()) else: self.assertEqual(row['status'], 'COMPLETE', row) self.assertEqual(commands, [uuid]) self.assertEqual(read(root / 'idle-gate.json')['polls'], 3) self.assertGreaterEqual(len(samples), 4) self.assertEqual([r['event'] for r in row['lifecycle']], ['leader_reaped']) REPLAYS.setdefault(connection, {})['busy' if busy else 'idle'] = dict( status=row['status'], trigger=row['trigger'], launches=len(commands), samples=len(samples), synthetic_gpu_elapsed=fake_elapsed, lifecycle=row['lifecycle']) def test_pressure_entry_through_real_guard_idle_and_busy(self): for busy in (False, True): self.replay_guard('H2-P', self.pressure, busy) def test_greif_entry_through_real_guard_idle_and_busy(self): for busy in (False, True): self.replay_guard('GINO-Greif', self.greif, busy) def test_pressure_coordinates_true_edges_and_direct_readout(self): torch.set_num_threads(1) p = np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], [2., 0., 0.], [3., 0., 0.], [2., 1., 0.]]) f = np.array([[0, 1, 2], [3, 4, 5]]) x, mass, receiver, sender, attributes, components = args = pressure.surface(p, f, [0]*3, [4]*3) self.assertTrue(torch.allclose(x[1], torch.tensor([-.5, -1., -1.]))) self.assertTrue(torch.allclose(mass, torch.full((6,), 1/6))) self.assertEqual(len(receiver), 12) self.assertTrue(bool(torch.all(components[receiver] == components[sender]))) self.assertEqual(len(torch.unique(components)), 2) with torch.no_grad(): output = pressure.PressureH2().eval()(*args) self.assertEqual(output.shape, (6,)) self.assertTrue(bool(torch.isfinite(output).all())) def test_pressure_uses_original_part_counts_and_cost_margin(self): self.assertEqual(len(self.pressure['split']['train']), 500) self.assertEqual(len(self.pressure['split']['test']), 111) self.assertEqual(self.pressure['custody']['final_unique_target_reads'], 111) self.assertTrue(pressure.project(100, 2500)['within_matched_cap']) self.assertFalse(pressure.project(100, 2501)['within_matched_cap']) def test_greif_grouped_support_and_full_phase_boundary(self): self.assertEqual(self.greif['folds']['0']['counts'], dict(learning=320, validation=111, test=121)) self.assertEqual(self.greif['folds']['1']['counts'], dict(learning=374, validation=87, test=91)) prior = 500000 other = dict(gino_shapenet=40000, h2_pressure=40000, other_unsettled=0) self.assertTrue(greif.project(prior, other, {'0': 100, '1': 100}, 200, 40000)['within_phase_cap']) self.assertFalse(greif.project(prior, other, {'0': 100, '1': 100}, 200, 40001)['within_phase_cap']) with self.assertRaises(ValueError): greif.project(prior, other, {'0': 100}, 200, 40000)