Attention
Attention combines token representations using weights computed from queries and keys. It helps a token use information from other allowed positions.
Before you start: Review Embeddings first. Python-like examples use variables, lists and functions.
Follow the mechanism
Queries and keys
Compute compatibility scores for allowed positions.
Start with an everyday explanation
When reading a story, you look back to connect a pronoun to a character. This analogy explains selecting context, but a neural attention layer uses matrix operations rather than human understanding.
See a concrete example
For one head with two allowed positions and normalized attention weights 0.75 and 0.25, value vectors [2,0] and [0,4] combine to [1.5,1].
A claim to question
A bright attention connection does not prove causal reasoning or factual support.
Try before revealing the answer
Compute the weighted sum above, then mask out the second position and renormalize.
Show the worked answer
Without the second position, the first receives weight 1 and the result is [2,0]. Mask before softmax so disallowed positions receive no probability.
Now explain the idea to someone else using your own example. If you cannot explain a stage, revisit it before moving on.
Engineering details and trade-offs
Scaled dot-product attention computes softmax(QKᵀ / √d) V. A causal mask prevents a decoder position from using future tokens.
Multiple heads can learn different relationships. Attention weights alone are not a reliable explanation of why the whole model produced an answer.
Reason through the example
For one head with two allowed positions and normalized attention weights 0.75 and 0.25, value vectors [2,0] and [0,4] combine to [1.5,1].
Implement and test
Compute the weighted sum above, then mask out the second position and renormalize.
Inspect the reasoning and expected result
Without the second position, the first receives weight 1 and the result is [2,0]. Mask before softmax so disallowed positions receive no probability.
What to measure
State the expected result before changing a control. Record your inputs, compare the observed result and explain one limit of this model. In a real implementation, test the failure case as well as the normal path.
Answer before reading the reference
Why does a decoder need a causal mask during training?
A defensible answer
Training predicts each next token from its preceding context. Without the mask, positions could inspect the target or future tokens and learn an invalid shortcut that is unavailable at generation time.
Prepare the follow-up
How do KV caching and grouped-query attention change inference memory?
Show follow-up reasoning
Caching avoids recomputing previous keys and values during decoding. Memory depends on KV heads, layers, head dimension, context, batch and element precision. Grouped-query attention uses fewer KV heads than query heads, so its cache can be smaller than a full multi-head attention cache at comparable dimensions.
Use the engineering explanation and worked task above to support your response. Identify which assumptions you would need to verify in production.
Choose a practice depth
- New to GenAI
- Define attention with the everyday example and name its key limitation.
- Applied experience
- Explain the example, implementation choice and a measured failure case. Use the exercise as a small design review.
- Senior or systems depth
- Answer the follow-up, compare alternatives and justify an evaluation or rollout plan with operational constraints.
Self-review your answer
0/4 checks selected. This is self-assessment, not an interview score.
Your explanation and revision notes
Loading local notes…