Skip to main content

Exercises — GPT and Transformers: architecture and data flow

Self-check questions, exercises, and worked answers

← Back to the lesson: GPT and Transformers: architecture and data flow

For extended practice, see the Transformer exercise collection, which brings together progressive problems, worked solutions, and transfer questions.

Check the essential idea

This check focuses on the network computation, not on decoding, which is covered in the generation lesson.

  1. Which operation lets a position incorporate information from other positions, and which one transforms each position independently?
  2. Why does a residual connection not replace the transformation computed by a sublayer?
  3. What role does normalization play in a deep stack of blocks?
  4. Which dimensions does a representation traverse in a block’s MLP, and in what order?
  5. During generation, which logits are used to choose the next token?
Show answers and reasoning
  1. Self-attention mixes information across permitted positions; the MLP applies a position-wise transformation with shared weights.
  2. The residual connection adds the sublayer output to its input: \(x_{out}=x_{in}+F(x_{in})\). It preserves a direct path through the block while allowing the sublayer to contribute a learned transformation.
  3. Normalization helps control activation scale and gradient flow. Its effect depends on placement: Xiong et al. (2020), for example, show that the original Post-LN design can have large gradients near the output at initialization, while the Pre-LN variant they study has better-behaved gradients. Do not conclude that every deep architecture fails without one particular normalization arrangement.
  4. d_model to d_ff (the MLP’s intermediate dimension, often about 4 × d_model), a nonlinear activation on the intermediate representation, then back to d_model.
  5. For an unpadded prefix, decoding uses the logits at its last position. The causal mask limits their computation to tokens already known. During training, the model can compute logits at all positions in parallel. The loss uses those whose next token is a valid target.

Suggested answers

Read the original 2017 diagram

Use the guided reading of the original diagram. It helps to have studied Q, K, and V projections and causal masking first.

  1. Locate: where does information enter, which stacks are repeated, where do encoder and decoder streams separate, and where are output scores produced?

  2. Explain: where do Q, K and V come from for each attention operation? Which positions are allowed by the mask? Why is encoder–decoder attention not a third form of self-attention?

  3. Compare: what does Add & Norm mean? In what order are addition and normalization applied? Why does the historical post-LN diagram differ from the pre-LN block studied in the deep dive?

  4. Transfer: which boxes disappear in decoder-only GPT? Which remain in an adapted form? Why does causal masking change the permitted computation?

  5. Reconstruct: explain the path of a representation from input to logits, separating network computation, conversion to probabilities, and generation of the next token.

  6. Locate: the encoder receives embeddings and positional information, then repeats self-attention and a feed-forward network. The decoder receives shifted target-sequence embeddings, then repeats masked self-attention, encoder–decoder attention, and a feed-forward network. A linear projection and softmax produce the output distribution.

  7. Explain: in encoder or decoder self-attention, Q, K and V come from the same stream. In encoder–decoder attention, Q comes from the decoder while K and V come from encoder outputs: it connects two distinct streams. The decoder’s causal mask allows previous and current positions, not future tokens.

  8. Compare: Add & Norm denotes residual addition followed by normalization in the original diagram (Post-LN). The pre-LN block in the deep dive places normalization before each sublayer; the two orders are not interchangeable.

  9. Transfer: decoder-only GPT has neither an encoder stack nor encoder–decoder attention. It retains causal self-attention, an MLP, residual connections and normalization, followed by a vocabulary projection.

  10. Reconstruct: input representations → repeated decoder blocks → projection to logits → softmax and distribution → token selection and context append. The last two operations belong to decoding, after the logits have been computed.

Language: Français · English