"""Evaluate the exported PSSA-300M model on held-out code, text/math, and mix. Usage on the box: cd /marimo/pssa_300m && python3 eval_300m.py artifacts/export data \ artifacts/eval_300m.json Protocol matches the 100M/4B cards: 1M unseen tokens per corpus, batch 8, sequence length 512. code/textmath are post-quota splits of the training sources; mixed is the training run's own valid split. """ import json import math import sys from pathlib import Path import numpy as np import torch import torch.nn.functional as F from pssa_model import from_pretrained def evaluate(model, arr, seq=512, batch=8, tokens=1_000_000, seed=1234, device="cuda"): n_batches = max(1, tokens // (seq * batch)) rng = np.random.default_rng(seed) ix = rng.integers(0, len(arr) - seq - 1, size=(n_batches, batch)) total_loss = 0.0 correct = 0 count = 0 with torch.no_grad(), torch.autocast("cuda", dtype=torch.bfloat16): for b in range(n_batches): rows = ix[b][:, None] + np.arange(seq + 1)[None, :] chunk = np.asarray(arr[rows], dtype=np.int64) x = torch.from_numpy(chunk[:, :-1]).to(device) y = torch.from_numpy(chunk[:, 1:]).to(device) logits = model(x) loss = F.cross_entropy(logits.float().view(-1, logits.shape[-1]), y.view(-1), reduction="sum") total_loss += loss.item() correct += (logits.argmax(-1) == y).sum().item() count += y.numel() mean = total_loss / count return {"tokens": count, "cross_entropy": mean, "perplexity": math.exp(mean), "next_token_accuracy": correct / count} def main(): export_dir, data_dir, out_path = sys.argv[1], Path(sys.argv[2]), Path(sys.argv[3]) device = "cuda" if torch.cuda.is_available() else "cpu" model, _ = from_pretrained(export_dir, device=device) results = {} for name, key, seed in ( ("code", "code_eval.u16", 1234), ("textmath", "textmath_eval.u16", 4321), ("mixed", "valid.u16", 2468), ): arr = np.memmap(data_dir / key, dtype=np.uint16, mode="r") results[name] = evaluate(model, arr, seed=seed, device=device) print(name, results[name], flush=True) out_path.write_text(json.dumps(results, indent=1)) print("wrote", out_path, flush=True) if __name__ == "__main__": main()