test_adapter.py
python · 62 lines · Exact bytes
Version & provenance · evaluation-cm7
Recorded SHA-256 a96103b5f2b7588801f27e4034f9746ce5b84c668677711de515cd7233a40ca0Rendered SHA-256 a96103b5f2b7588801f27e4034f9746ce5b84c668677711de515cd7233a40ca0
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 sys,unittest,math
2from pathlib import Path
3sys.path.insert(0,str(Path(__file__).parent))
4import adapter
5c=adapter.c
6class Tests(unittest.TestCase):
7 def test_cm7_checkpoint_lineage(self):
8 identity={'candidate_name':'CM-7','parent_sha256':'7b63bab559741893b632f456e5f9a8e48b15e48fc9c47c302734da9d613dfe85'}
9 value={'format':'cm6-curriculum-checkpoint-v1','identity':identity,'identity_sha256':adapter.digest(identity)}
10 adapter.validate_identity(value)
11 for field,bad in (('candidate_name','CM-6'),('parent_sha256','wrong')):
12 altered=dict(identity);altered[field]=bad
13 with self.assertRaises(ValueError):adapter.validate_identity(dict(value,identity=altered,identity_sha256=adapter.digest(altered)))
14 with self.assertRaises(ValueError):adapter.validate_identity(dict(value,format='chess-state-train-v2'))
15 def test_joint_and_raw_reserved(self):
16 b=c.chess.Board();calls=[]
17 def logits(xs,masks):
18 calls.append((xs,masks))
19 out=[]
20 for x in xs:
21 row=[-9.]*512;row[511]=2
22 row[[12,28,64][len(x)-120]]=1
23 out.append(row)
24 return out
25 m,metrics=adapter.decide(b,logits)
26 self.assertEqual(m.uci(),'e2e4');self.assertEqual(metrics['raw_greedy_tokens'],[511]*3)
27 self.assertFalse(metrics['raw_greedy_legal']);self.assertEqual(metrics['forward_calls'],3)
28 self.assertLess(metrics['legal_joint_probability'],1)
29 for phase,(xs,ms) in enumerate(calls):
30 self.assertTrue(all(len(x)==120+phase for x in xs))
31 self.assertTrue(all(x[:120]==c.prompt(b)['input_ids'] for x in xs))
32 self.assertTrue(all(m[:120]==c.prompt(b)['attention_mask'] for m in ms))
33 def test_followup_requires_exact_stage_identity(self):
34 identity={'candidate_name':'CM-7 theory follow-up','parent_sha256':'bound-by-final-config'}
35 value={'format':'cm7-theory-followup-v1','identity':identity,'identity_sha256':adapter.digest(identity)}
36 with self.assertRaises(ValueError):adapter.validate_identity(value)
37 adapter.validate_identity(value,identity)
38 with self.assertRaises(ValueError):adapter.validate_identity(value,dict(identity,parent_sha256='other'))
39 def test_history_fen_and_special(self):
40 b=c.chess.Board()
41 for s in ['e4','a6','e5','d5']:b.push_san(s)
42 fixtures=[b,c.chess.Board('r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1'),c.chess.Board('7k/P7/8/8/8/8/8/7K w - - 0 1')]
43 for b in fixtures:
44 for history in (0,16):
45 def logits(xs,masks):
46 self.assertEqual(xs[0][:120],c.prompt(b,history)['input_ids'])
47 return [[0.]*512 for _ in xs]
48 m,info=adapter.decide(b,logits,history)
49 self.assertIn(m,b.legal_moves)
50 expected=len(list(b.legal_moves))/(512**3)
51 self.assertAlmostEqual(info['legal_joint_probability'],expected)
52 def test_invalid_logits_terminal(self):
53 for row in ([0.]*128,[float('nan')]*512):
54 with self.assertRaises(ValueError):adapter.decide(c.chess.Board(),lambda x,m:[row]*len(x))
55 with self.assertRaises(ValueError):adapter.decide(c.chess.Board('7k/6Q1/5K2/8/8/8/8/8 b - - 0 1'),lambda x,m:[])
56 def test_history_tail_matches_training(self):
57 b=c.chess.Board()
58 for s in 'e4 e5 Nf3 Nc6 Bb5 a6 Ba4 Nf6 O-O Be7 Re1 b5 Bb3 d6 c3 O-O h3 Nb8 d4 Nbd7'.split():b.push_san(s)
59 move=next(iter(b.legal_moves));ex=c.example(b,move,16);p=c.prompt(b,16)
60 self.assertEqual(ex['input_ids'][:120],p['input_ids'])
61if __name__=='__main__':unittest.main()
62