LittlePlyResearch notebook

training_adapter.py

python · 31 lines · Exact bytes

Line 1 ↗
Version & provenance · match-20261028

Recorded SHA-256 1a0271cad107870345ef839378692d0d4036de44910e3e187c52c4e4d8244637Rendered SHA-256 1a0271cad107870345ef839378692d0d4036de44910e3e187c52c4e4d8244637

Apache-2.0 for original LittlePly code, declared by the project owner on 2026-10-28. Historical source bytes and hashes are unchanged. Third-party components retain their own licenses; Lichess data remain CC0-1.0. License notes. Line links are stable within this snapshot. SHA-256 compared to persisted run identity/manifest. Unavailable files are never replaced with newer versions. Rendered derivatives preserve line numbers and redact private paths; original and rendered hashes are distinct.

1"""Read-only Sunfish harness adapter. No matches or training run on import."""
2import os
3from pathlib import Path
4import random
5import torch
6from training_core import load_model_checkpoint
7from chessdata import BOS
8from chessdata.convert import file_hash
9
10
11def load(checkpoint:Path, *, seed:int):
12    os.environ.setdefault('CUBLAS_WORKSPACE_CONFIG',':4096:8')
13    random.seed(seed);torch.manual_seed(seed);torch.set_num_threads(4)
14    torch.use_deterministic_algorithms(True)
15    device=os.environ.get('CHESS_INFERENCE_DEVICE','cpu')
16    net,metadata=load_model_checkpoint(checkpoint,device)
17    if metadata['identity']['tokenizer']['sha256']!=file_hash(Path(__file__).parent/'chessdata'/'tokenizer.py'):
18        raise ValueError('checkpoint tokenizer source hash mismatch')
19    class Model:
20        context_length=1024
21        checkpoint_metadata=metadata
22        @torch.inference_mode()
23        def next_logits(self,prefix):
24            if not 1<=len(prefix)<=self.context_length or prefix[0]!=BOS or any(type(t) is not int or not 0<=t<128 for t in prefix):
25                raise ValueError('invalid full token prefix; cropping is forbidden')
26            x=torch.tensor([prefix],dtype=torch.long,device=device)
27            logits=net(x,attention_mask=torch.ones_like(x,dtype=torch.bool),use_cache=False).logits[0,-1].float()
28            if not torch.isfinite(logits).all():raise ValueError('nonfinite model logits')
29            return logits.cpu().tolist()
30    return Model()
31