Redhanuman's picture
Pocket Storyteller: 4.6 MB story model, browser demo, weights and training code
ebf8f81 verified
Raw History Blame Contribute Delete
1.39 kB
"""Measure the loss on 1.02 million word-pieces of stories the model never saw.
python tests/bigval.py ckpt.pt web/storyteller.bin
Prints one line for the full-precision checkpoint and one for the packed file. These are the
numbers quoted in the README. It uses the first 4,000 windows of the validation file, no overlap.
"""
import sys, numpy as np, torch
sys.path.insert(0, ".")
from model import Config, Storyteller
src = open("tests/check_export.py").read().split("h, sd = load_packed")[0] # reuse its reader
ns = {}; exec(src, ns)
ck = torch.load(sys.argv[1], map_location="cpu"); cfg = Config(**ck["config"])
_, packed = ns["load_packed"](sys.argv[2])
val = np.memmap("data/val.bin", dtype=np.uint16, mode="r")
N, B = 4000, 50
res = {}
for name, sd in (("float32", ck["model"]), ("packed", packed)):
m = Storyteller(cfg); m.load_state_dict(sd); m.eval(); tot = 0.0
with torch.no_grad():
for b in range(0, N, B):
idx = np.arange(b, b + B) * cfg.ctx
x = torch.from_numpy(np.stack([val[i:i + cfg.ctx].astype(np.int64) for i in idx]))
y = torch.from_numpy(np.stack([val[i + 1:i + 1 + cfg.ctx].astype(np.int64) for i in idx]))
tot += m(x, y).item() * B
res[name] = tot / N
print(name, f"{tot / N:.4f}", "perplexity", f"{np.exp(tot / N):.2f}", flush=True)
print("difference", f"{res['packed'] - res['float32']:.4f}")