"""Read a packed model file back in Python and write reference answers for the browser engine. python tests/check_export.py web/storyteller.bin [ckpt.pt] Writes tests/expected.json (token ids and next-piece scores for some test sentences). If a checkpoint is given, also compares validation loss: float32 checkpoint vs the packed file. """ import sys, json, struct, numpy as np, torch sys.path.insert(0, ".") from model import Config, Storyteller from tokenizer import Tokenizer, split_stories def load_packed(path): buf = open(path, "rb").read() assert buf[:4] == b"STRY" hlen = struct.unpack(" 2: ck = torch.load(sys.argv[2], map_location="cpu") ref = Storyteller(cfg); ref.load_state_dict(ck["model"]); ref.eval() val = np.memmap("data/val.bin", dtype=np.uint16, mode="r") rng = np.random.default_rng(7); tot = [0.0, 0.0]; NB = 12 with torch.no_grad(): for _ in range(NB): ix = rng.integers(0, len(val) - cfg.ctx - 1, 32) x = torch.from_numpy(np.stack([val[i:i + cfg.ctx].astype(np.int64) for i in ix])) y = torch.from_numpy(np.stack([val[i + 1:i + 1 + cfg.ctx].astype(np.int64) for i in ix])) tot[0] += ref(x, y).item() / NB; tot[1] += m(x, y).item() / NB print(f"validation loss: float32 checkpoint {tot[0]:.4f}, packed file {tot[1]:.4f} ({tot[1]-tot[0]:+.4f})")