LittlePlyResearch notebook

test_trainer.py

python · 44 lines · Exact bytes

Line 1 ↗
Version & provenance · curriculum-cm7

Recorded SHA-256 a5ea09447757f9d99bcb5b5f6fbf0c998a0a24798a64e117aea3a9030cec5ee1Rendered SHA-256 a5ea09447757f9d99bcb5b5f6fbf0c998a0a24798a64e117aea3a9030cec5ee1

Apache-2.0 original project code; dependencies retain their own licenses. License notes. Line links are stable within this snapshot. CM-7 completed implementation source; original and public rendered hashes recorded. Host-specific paths use /work placeholders; inspect reproduction instructions before running. Separate Rust helper retains its GPL license.

1import importlib.util,json,math,tempfile,unittest
2from pathlib import Path
3from unittest.mock import patch
4import numpy as np
5import torch
6spec=importlib.util.spec_from_file_location('curriculum_trainer',Path(__file__).with_name('train.py'))
7t=importlib.util.module_from_spec(spec);spec.loader.exec_module(t)
8
9class Reader:
10    def __init__(self,root,source,split,verify=True):
11        from records import DTYPE
12        self.rows=np.zeros(2,dtype=DTYPE);self.rows['attention']=1;self.rows['ply']=[0,20]
13        if root=='theory':self.rows['x'][:,51:115]=128
14    def __len__(self):return len(self.rows)
15    def take(self,indices):return self.rows[list(indices)]
16
17class Net(torch.nn.Module):
18    def forward(self,x,m):return torch.zeros(len(x),3,512)
19    def next_logits(self,x,m):return torch.zeros(len(x),512)
20
21class Tests(unittest.TestCase):
22    def test_paired_cluster_uncertainty(self):
23        from exact_metrics import summarize
24        a=[{'key':str(i),'game':str(i//2),'correct':0} for i in range(10)]
25        b=[dict(r,correct=1) for r in a];v=summarize(b,a)
26        self.assertEqual(v['source_game_clusters'],5);self.assertEqual(v['estimate'],1);self.assertEqual(v['percentile95'],[1.,1.])
27        with self.assertRaises(ValueError):summarize(b[:-1],a)
28    def test_validation_targets_phases_and_ties(self):
29        with tempfile.TemporaryDirectory() as folder:
30            path=Path(folder)/'ties.jsonl';path.write_text(json.dumps({'tied_target_triples':[[0,0,0],[1,2,64]],'position_key':'fixture','source_sequence_sha256':'game'})+'\n')
31            cfg={'validation_groups':[{'name':'main','kind':0,'source':0,'split':1,'root':'ordinary'},
32                {'name':'exact','kind':1,'source':1,'split':1,'root':'theory','tie_metadata':str(path)}]}
33            with patch.object(t,'Records',Reader),patch.object(t,'SmallRecords',Reader):value=t.evaluate(Net(),cfg,torch.device('cpu'))
34            self.assertEqual(value['decisions'],4);self.assertAlmostEqual(value['nll'],math.log(512),places=5)
35            self.assertEqual(value['groups']['main']['phase_counts'],[1,1,0]);self.assertIsNone(value['groups']['main']['phase_nll'][2])
36            self.assertEqual(value['groups']['exact']['phase_counts'],[0,0,2]);self.assertEqual(value['groups']['exact']['raw_tie_preserving_fraction'],1.)
37            self.assertEqual(value['groups']['main']['phase_accuracy'],[1.,1.,1.])
38    def test_reject_changed_runtime_before_launch(self):
39        with tempfile.TemporaryDirectory() as folder:
40            path=Path(folder)/'runtime';path.write_text('changed')
41            with self.assertRaisesRegex(ValueError,'runtime changed'):t.verify({'runtime_sha256':{str(path):'wrong'}},None)
42
43if __name__=='__main__':unittest.main(verbosity=2)
44