A weather report reads rain stops. Count each word as one token, append an end marker (EOS), and leave out the start marker. After rain, the target is stops; after rain stops, the target is EOS. Three positions give us only two predictions to score.

Why do training and generation run at different rhythms?

In one lineTraining scores both targets in parallel. If generation picks falls instead of stops, the next prediction has to follow rain falls.

Training and generation use the same parameterized distribution:

$$p_\theta(x_{1:T})=\prod_{t=1}^{T}p_\theta(x_t\mid x_{<t})$$

Give the model the same prefix, rain. In training we have the rest of the report, so we can score both predictions in one pass. A causal mask keeps each position from seeing its answer. In generation there is no answer key: if the model picks falls second, the third word has to follow rain falls.

Stage What's known? What happens?
Training The whole report Predict in parallel. Each target is one place to the right of its input.
Inference: prefill The supplied prompt Process the prompt in parallel and build the KV cache. The last position gives the next-token distribution.
Inference: decode The prefix and the token just chosen Read the cached keys and values to predict and pick the next token.

Training saves or recomputes activations for the backward pass. A long prefill is often compute-bound; small-batch decode often waits on memory reads, and each step depends on the last choice. A roofline estimate helps tell which limit matters for a particular device, length and batch.

Cross-entropy does not decide “what matters” for you

In one lineAveraging decides sample weight: by token, a 200-token answer counts ten times a 20-token one. Across tokenizers, compare bits per byte, not perplexity.

The loss at a single position:

$$\ell_t=-\log p_\theta(y_t\mid x_{\le t})$$

Here $y_t=x_{t+1}$ is the true token after position $t$. The gradient with respect to the logits is $p-e_{y_t}$, where $e_{y_t}$ is the one-hot vector for $y_t$. When losses are summed over all positions, frequent tokens and long samples contribute more terms. Data mixing, sample weighting, and loss masking therefore shape the training objective; they are not merely implementation details.

How much more say does a long answer get than a short one? Suppose a 20-token answer has a mean loss of 1.0 and a 200-token answer has a mean loss of 2.0:

How you average Calculation Result
Over all tokens together $(20\times1.0+200\times2.0)/220=420/220$ about 1.91
Within each answer, then over samples $(1.0+2.0)/2$ 1.5

Under the token mean, the long answer carries $200/220\approx91\%$ of the weight, so the result is almost its own 2.0; under the per-sample mean, each answer counts for half. Which one fits depends on what you want the model to care about; the formula does not choose for you. Gradient accumulation across microbatches raises the same question; see one training update.

Perplexity is the exponential of the average token negative log-likelihood:

$$\text{PPL}=\exp\!\left(\frac{1}{N}\sum_t \ell_t\right)$$

Plug in two numbers. With a mean NLL of 2 nats per token, PPL is $e^2\approx7.39$, roughly like choosing among 7.39 equally likely tokens at every step. If the model gives every one of the $V$ vocabulary tokens probability $1/V$, each step's NLL is $\ln V$ and PPL is exactly $V$.

Different tokenizers have different token units, so perplexity cannot be compared directly across them. Take a teaching example (not a measurement): the same 100-byte text, and two models that both assign it a total NLL of 100 nats, differing only in how their tokenizers split it:

Tokenizer splits it into NLL per token PPL Bits per byte (bpb)
25 tokens 4 nats $e^4\approx54.6$ $100/(100\ln2)\approx1.44$
50 tokens 2 nats $e^2\approx7.39$ $100/(100\ln2)\approx1.44$

Both models give the text the same probability, yet their PPL differs by more than 7 times: finer splits make each token easier to guess. Converting the total NLL to bits and dividing by the byte count fixes the unit to the same bytes; the GPT-2 report §3.1 likewise reports results per fixed unit such as a character, byte, or word. Bpb has limits too: under UTF-8's encoding rules, an ASCII character takes 1 byte and most CJK characters take 3. The same content written in another language has a different byte count, so cross-language bpb comparisons still need care.

Training uses the true prefix; generation depends on the model's own output

In one lineTraining sees the true prefix; generation continues its own output and can drift. Before RL, rule out data, mask, template and sampling problems.

In training the model always predicts on top of the true prefix (teacher forcing); in generation it must continue from its own output. This mismatch is commonly called exposure bias; see Seq2Seq for a worked example. A low-probability error at one step can carry the subsequent context into a region that the training data rarely covers.

Before considering RL to improve generation, check where the problem comes from:

  1. the training data does not cover the target behavior;
  2. the loss mask or the template is wrong;
  3. the sampling strategy is unsuitable;
  4. a long-horizon objective cannot be expressed by per-token likelihood.

One diagnostic is to score a reference answer. Low log-likelihood means that this model is unlikely to generate that answer under the given input; it does not establish a gap in the training data. Check the template, training objective, and model capability as well. If the reference has reasonable likelihood but sampled responses are often poor, inspect temperature, top-p, and related settings more closely. This comparison helps guide the investigation, but a single score cannot identify the cause.

The fourth category points most directly to a sequence-level preference or an RL objective; if the problem is exactly that the model's own outputs carry the context away from the training distribution, training on the model's own prefixes (on-policy data, RL, and similar) is also worth considering.

A cache must match the full forward pass

In one lineIncremental decoding must match a full forward pass. A wrong position, mask or K/V order can still read fluently, so compare logits step by step.

Incremental decoding must agree with a one-shot forward pass within numerical tolerance. A small error in positions, the mask, or K/V order can still produce fluent sentences while the logits have already changed, so compare logits step by step rather than judging whether the text looks right. The item-by-item checklist is in the KV cache note, and ../code/test_model.py has the matching equivalence test.

What post-training actually changes

In one lineSFT, preference learning and RL keep the same model; they change the data source, the scored tokens, how outputs compare and how return is assigned.

SFT, preference learning, and RL do not switch to a different kind of model. On the same autoregressive distribution they change: where the training samples come from, which tokens count toward the loss, how different outputs are compared, and how the return is assigned across a whole trajectory.

Next, pack this report next to a second one. For instruction and preference data instead, see post-training.