The flow shows one generation step of a decoder-only LLM (GPT- or Llama-style). What happens at the highlighted step?
Show the answer and explanation
Correct answer: D, The LM head: a linear layer that maps the last position's hidden state to one logit per vocabulary token, which softmax turns into probabilities.
Explanation LLM Fundamentals
A decoder-only LLM generates text one token at a time:
- Tokenize the text into token IDs.
- Look up an embedding vector per ID; position information is added (learned positions) or applied inside attention (RoPE).
- Run the vectors through N decoder blocks, each with causal (masked) self-attention and an MLP, plus residual connections and normalisation.
- Apply a final normalisation.
- LM head: multiply the last position's hidden vector (size d_model) by a d_model × vocab matrix to get one logit per vocabulary token. Many models tie this matrix to the input embedding matrix.
- Softmax turns the logits into probabilities and a decoding rule (temperature, top-p) picks the next token; greedy decoding can simply take the argmax.
- Append it and repeat. With a KV cache, later steps push only the new token through the blocks and reuse the cached keys and values of earlier tokens.
Only the last position's logits are needed to generate the next token. In training, the logits at every position are used, so one forward pass yields a next-token loss for every position.
Why the other steps are wrong: cross-attention to an encoder exists in encoder-decoder models such as T5, not in decoder-only models. Choosing a position to copy from describes pointer networks, not a standard LM. Detokenization happens only after a token ID has been chosen, to show text to the user.
Interview tip: be ready to size the LM head: with d_model = 4,096 and a 128k vocabulary it holds 4,096 × 128,000 ≈ 0.5B parameters. In small models this matrix is a large share of all weights, which is why they often tie it to the input embeddings.