20/30
Part 5 of 6 · Generation, and the whole roundLearn and do
Every new token must match a fresh pass on its window
Generation has bugs of its own, separate from training.
def check_generate(model: nn.Module, generate: Callable, prompt: torch.Tensor, n: int) -> None:
"""Fails if a new token isn't the pick from its last block_size tokens."""
out = generate(model, prompt, n)
with torch.no_grad():
for t in range(prompt.size(1), out.size(1)):
window = out[:, max(0, t - model.block_size):t]
want = model(window)[0][:, -1].argmax(dim=-1)
assert torch.equal(out[:, t], want), f"token {t} is off"
generate has its own contract: every new token is the model's pick given the most recent block_size tokens. This check reruns the model on exactly that window, for each new token:
t0t1t2t3t4t5t6t7
block_size = 4. The dashed cell is the token being picked.
t4 comes from the last 4 tokens, t0 t1 t2 t3: idx[:, -block_size:].
Run it past block_size, on a trained model
Run it past block_size: a window bug can't show before the window fills. Turn on crop from the front in the figure above: from t5 on, the wrong crop reads the same first window every step.
Greedy decoding always picks the top-scoring token. On an untrained model, does a wrong crop show in the text?
Pick an answer above to see the rest