LittlePlyResearch notebook

cm7_scorer.py

python · 27 lines · Exact bytes

Line 1 ↗
Version & provenance · service-cm7-v1

Recorded SHA-256 04d49ecb4b9affdcccedf739cb59f1d1a2c67b76621397b1a454c83445c28101Rendered SHA-256 04d49ecb4b9affdcccedf739cb59f1d1a2c67b76621397b1a454c83445c28101

Apache-2.0 original project code. License notes. Line links are stable within this snapshot. Live CM-7 release 29e68a8e8533-39446d6ca608. Selected checkpoint 29e68a8e85336cbb1f56efdad36bd76217728b6a376d1e5f74e89887d21383da. Explicit source allowlist; private configuration, credentials, state and logs excluded. Shared service dependencies remain in historical service snapshots.

1"""Pinned CM-7 state-v2 serving; exact evaluated joint scoring, genuine history16."""
2import json
3from pathlib import Path
4from cm7_joint import decide,validate_identity,sha
5
6class Scorer:
7    def __init__(self,checkpoint,device):
8        import torch
9        from model import StateModel
10        root=Path(__file__).resolve().parent.parent
11        contract=json.loads((root/'cm7-contract.json').read_text())
12        if sha(checkpoint)!=contract['checkpoint_sha256']:raise ValueError('CM-7 checkpoint hash')
13        saved=torch.load(checkpoint,map_location='cpu',weights_only=True)
14        validate_identity(saved,contract['identity'])
15        if json.loads(json.dumps(saved['model_config']))!=contract['model_config']:raise ValueError('CM-7 architecture')
16        self.model=StateModel(saved['model_config'])
17        self.model.load_state_dict(saved['model_state_dict'],strict=True)
18        self.model.to(device).eval();self.device=device
19    def score(self,board):
20        import torch
21        def logits(xs,masks):
22            with torch.inference_mode(),torch.autocast('cuda',dtype=torch.bfloat16,enabled=str(self.device).startswith('cuda')):
23                return self.model.next_logits(torch.tensor(xs,device=self.device),torch.tensor(masks,device=self.device)).float().cpu().tolist()
24        move,metrics=decide(board,logits,history_plies=16)
25        metrics.update(input_representation='chess-state-history16-v2',model_input_limit=122)
26        return move,metrics
27