Self-attention and transformer blocks
Understand queries, keys, values and masking.
Inspect attention weights while remembering they are not guaranteed causal explanations.
Interview questions
Choose a question to reveal its answer.
01EasyCoreWhat does the query vector represent in self-attention?
Answer
What does the query vector represent in self-attention?
Vector representing what a token seeks from other positions.
Compare a token's request against candidate keys.
02EasyCoreWhat is the role of keys in attention?
Answer
What is the role of keys in attention?
Vector used to determine relevance to a query.
Provide a lookup representation for each source token.
03EasyCoreHow are value vectors combined in attention?
Answer
How are value vectors combined in attention?
Vector combined using attention weights to form the output.
Aggregate contextual information after scoring keys.
04MediumHigh priorityVisualWhy divide attention logits by the square root of the key dimension?
Answer
Why divide attention logits by the square root of the key dimension?
Softmax of query-key dot products divided by square root of key dimension, applied to values.
The standard operation is softmax(QKᵀ/√dₖ + mask)V. If independent query and key coordinates have unit variance, their dot product variance grows with dₖ. Scaling helps prevent overly large logits and saturated softmax.
Compute contextual output while preventing large logits from saturating softmax.
import torch
q, k, v = [torch.randn(4, 8) for _ in range(3)]
weights = (q @ k.T / 8**.5).softmax(dim=-1)
out = weights @ v
print(out.shape)A single unmasked attention head with four tokens.
05MediumHigh priorityWhy use multiple attention heads?
Answer
Why use multiple attention heads?
Runs multiple learned attention projections in parallel and combines their outputs.
Each head has separate learned projections of queries, keys and values. Outputs are concatenated and projected to model width. Multiple heads can learn different relationships but the attention weights are not guaranteed faithful causal explanations.
Let different heads capture different relations.
06MediumHow does causal masking prevent future-token leakage?
Answer
How does causal masking prevent future-token leakage?
Blocks attention to future sequence positions.
Train an autoregressive decoder without future-token leakage.
07MediumWhy must transformers represent token position?
Answer
Why must transformers represent token position?
Represents sequence order because content-only attention is permutation equivariant.
Distinguish the ordering of words in a sentence.
08MediumWhy are residual connections important in transformers?
Answer
Why are residual connections important in transformers?
Adds a block input to its transformed output.
Improve gradient flow through deep layers.
09HardHow does layer normalization differ from batch normalization?
Answer
How does layer normalization differ from batch normalization?
Normalizes feature activations within each token or example as configured.
Stabilize transformer block computations.
10HardWhat does the transformer feed-forward block do?
Answer
What does the transformer feed-forward block do?
Position-wise nonlinear transformation after attention in a standard transformer block.
Expand and project token features independently.
11MediumHigh priorityWhat does a transformer encoder do?
Answer
What does a transformer encoder do?
Transforms an input sequence into contextual representations, commonly using bidirectional attention when appropriate.
Encode a text document for classification.
12MediumHigh priorityHow does a decoder-only transformer generate text?
Answer
How does a decoder-only transformer generate text?
Uses autoregressive transformer blocks to predict next tokens with causal attention.
Generate a response conditioned on a preceding prompt.
13MediumHigh priorityHow does cross-attention differ from self-attention?
Answer
How does cross-attention differ from self-attention?
Computes queries from one representation and keys and values from another.
Let a translation decoder attend to encoded source tokens.
14MediumHigh priorityWhy is long-context self-attention expensive?
Answer
Why is long-context self-attention expensive?
Standard dense self-attention computes pairwise token interactions, requiring quadratic work in sequence length for those interactions.
Double sequence length and observe a fourfold increase in attention score entries.
15MediumHigh priorityHow does RoPE encode position in attention?
Answer
How does RoPE encode position in attention?
Rotates query and key coordinate pairs as a function of position to incorporate relative position relationships in dot products.
Use position-dependent rotations in a decoder attention layer.
Priority and difficulty are editorial study recommendations.
Technical reference: Documentation / paper ↗
Try a few practice questions
Explore the questions before creating a free account.
1. In scaled dot-product attention, which operation converts scores into weights?
2. Why does a causal decoder mask later token positions?
3. If sequence length doubles, what happens to dense attention score matrix entries?
Transformer architectures and training
Continue to the next set of interview questions.