Files
MeltMind/tools/test_phase2_h2_s1.py

39 lines
2.8 KiB
Python

"""Engineering regressions for the independent S1 scope, no real data/GPU."""
import ast,json,tempfile,unittest
from pathlib import Path
from unittest.mock import patch
import phase2_h2_s1_common as common
from phase2_h2_model import lr_epoch
from s2_main_memory_guard import PeakRecords
class S1Tests(unittest.TestCase):
def test_gate_strict_and_or(self):
self.assertFalse(common.gate(.005,0));self.assertTrue(common.gate(.00499,1));self.assertTrue(common.gate(.1,-1e-10));self.assertFalse(common.gate(None,float('nan')))
def test_final_only(self):
for e in [1,42,199,201]:
with self.assertRaises(ValueError):common.selected_checkpoint(e)
self.assertEqual(common.selected_checkpoint(200),'last-epoch200.pt')
def test_normalization_rejects_validation(self):
with tempfile.TemporaryDirectory() as d:
p=Path(d)/'s.json';p.write_text(json.dumps(dict(parts=['validation'],nodes=1,mean=[0]*19,scale=[1]*19)))
c=dict(standardization_path=str(p),standardization_sha256='dummy')
with patch.object(common,'checked',side_effect=lambda p,h:Path(p)):
with self.assertRaises(ValueError):common.stats_from_pilot(c,dict(folds=[dict(learning=['learning'],learning_nodes=1)]))
def test_schedule_index(self):
self.assertAlmostEqual(lr_epoch(1),.0001);self.assertAlmostEqual(lr_epoch(10),.001);self.assertAlmostEqual(lr_epoch(200),.00001);self.assertLess(lr_epoch(11),lr_epoch(10))
def test_history_peak_cannot_be_hidden(self):
with tempfile.TemporaryDirectory() as d:
p=Path(d);(p/'history').mkdir();(p/'history/001.json').write_text(json.dumps(dict(peak_reserved_bytes=16*2**30,peak_allocated_bytes=1)))
(p/'result.json').write_text(json.dumps(dict(status='FAILED',peak_reserved_bytes=1)))
tracker=PeakRecords();self.assertTrue(tracker.scan(p));(p/'history/001.json').unlink();self.assertTrue(tracker.scan(p))
def test_train_does_not_open_test_or_pilot_weights(self):
tree=ast.parse(Path('tools/phase2_h2_s1_worker.py').read_text())
calls=[n for n in ast.walk(tree) if isinstance(n,ast.Call)]
self.assertFalse(any(isinstance(n.func,ast.Attribute) and n.func.attr=='load' and isinstance(n.func.value,ast.Name) and n.func.value.id=='torch' for n in calls))
roles=[n.args[3].value for n in calls if isinstance(n.func,ast.Name) and n.func.id=='load_part']
self.assertEqual(sorted(roles),['learning','validation'])
def test_unapproved_scope_rejected_before_sources(self):
with tempfile.TemporaryDirectory() as d:
p=Path(d);(p/'contract.json').write_text(json.dumps(dict(approval='wrong',mode='H2_S1_FOLD0_LAST200')))
with self.assertRaises(PermissionError):common.config(p)
if __name__=='__main__':unittest.main()