Shifted targets and parallel training
Build targets by shifting the input one token left. Explain how causal masking lets one forward pass train all T predictions in parallel, with the true prefix always fed in (teacher forcing).
Builds on: Causal masking, The next-token cross-entropy objective
What the animation says
Four problems in one chunk
Here is a training chunk: five token ids. Five, nine, two, seven, three. Hidden inside are four next-token problems. From five, predict nine. From five and nine, predict two. And so on, up to predicting three. Running the model once per prefix means four passes, redoing the same prefix work every time. So try a single pass over the text, with nothing hiding the future. What would position two learn to do?
Mask it, shift it
It would copy. Seeing the answer makes the guess free, and useless when generating new text. The causal mask fixes this. Each position sees only itself and the tokens before it. Now the output at every position is an honest next-token predictor. Feed in the first four tokens. Each position's correct answer is simply the next token in the text. Position one's target is nine. Position two's target is two. Position three reads the token two. Tap its target.
One pass, one loss
Seven. So the targets are nine, two, seven and three: the text shifted one step left. One forward pass gives a row of scores at every position, turned into probabilities. From each row, read the probability of its target: point two, point five, point one, point four. Take minus the log of each, then average. The loss is about one point three eight nats. Position three gave seven only point one. Suppose it bet on token four instead. When position four makes its prediction, which prefix does it see?
Teacher forcing vs generation
Position four sees five, nine, two, seven: the true text, never the model's guess. Every position is conditioned on the real prefix. This is called teacher forcing. Because no prediction waits on another, all four are scored together in one parallel pass. Generation is different. There, the next input is the model's own output. So generation must run one token at a time, each step waiting for the last.
Shift and score in numpy
Now in numpy. The chunk holds the five ids. This line does the shift: inputs drop the last token, targets drop the first. Z stands in for one forward pass: four rows of ten logits. Next, these three lines turn each row into log probabilities, safely. Finally each row needs just one entry: the log probability of its own target. Tap the line that picks those entries.
Shift the text, let the mask hide the future
Inputs, targets and the loss
Five tokens, four predictions
Shift and score in numpy
import numpy as np
rng = np.random.default_rng(0)
V = 10
chunk = np.array([5, 9, 2, 7, 3]) # T+1 token ids
inp, tgt = chunk[:-1], chunk[1:] # shift left by one
T = len(inp)
Z = rng.normal(size=(T, V)) # stand-in logits [T, V] from one pass
Z = Z - Z.max(axis=1, keepdims=True) # log-sum-exp stability
logp = Z - np.log(np.exp(Z).sum(axis=1, keepdims=True))
loss = -logp[np.arange(T), tgt].mean()
print(inp, tgt, round(loss, 3))
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses