Transformer Architecture
Subject: AI-ML Chapter: Transformer Architecture Time limit: 30 minutes Total marks: 40 Level 2 — Recall: definitions, standard textbook problems, short derivations
Answer all questions. Show working where required.
Q1. (4 marks) State two limitations of RNNs that motivated the transformer architecture. Briefly explain how self-attention addresses each.
Q2. (4 marks) Write the formula for scaled dot-product attention given query matrix , key matrix , and value matrix . Explain the role of the scaling factor .
Q3. (5 marks) In a self-attention layer the model dimension is and the number of heads is . Assuming , compute: (a) the per-head key/value dimension ; (2) (b) the total number of parameters in the three projection matrices (ignore biases) across all heads. (3)
Q4. (4 marks) Define multi-head attention. Why is using multiple heads beneficial compared to a single attention head?
Q5. (5 marks) The sinusoidal positional encoding is defined as For and , compute the 4-dimensional positional encoding vector . Give values to 4 decimal places.
Q6. (4 marks) Explain the purpose of masked (causal) attention in the decoder. What values are placed in the masked positions before the softmax, and why?
Q7. (4 marks) Compare the computational and memory complexity of self-attention with respect to sequence length . State the complexity and explain briefly why.
Q8. (3 marks) Distinguish between encoder-only, decoder-only, and encoder–decoder transformer architectures, giving one example task each.
Q9. (4 marks) Describe the two sublayers of a standard transformer encoder block. State where residual connections and layer normalization are applied in the original "Attention is All You Need" design.
Q10. (3 marks) What is the key idea behind FlashAttention? State one advantage it provides over the naive attention implementation.
End of paper
Answer keyMark scheme & solutions
Q1. (4 marks)
- Sequential computation prevents parallelization over time steps (slow training) — self-attention processes all positions in parallel. (2)
- Difficulty modelling long-range dependencies / vanishing gradients over long sequences — self-attention gives constant path length between any two tokens. (2) Why: attention connects every pair directly, removing recurrence.
Q2. (4 marks) Scaling by counteracts the growth of dot-product magnitude with dimension; without it large values push softmax into regions of tiny gradients (saturation). (2)
Q3. (5 marks) (a) . (2) (b) Each of maps (all heads combined) params. Total . (3) Why: full projection matrix is .
Q4. (4 marks) Multi-head attention runs attention functions in parallel on different learned linear projections of Q,K,V, concatenates outputs, and projects with : (2) Benefit: allows the model to jointly attend to information from different representation subspaces / positions, capturing multiple relationship types a single head would average out. (2)
Q5. (5 marks) For , :
- : , . (1.25)
- : . (1.25)
- : , . (1.25)
- : . (1.25) Vector .
Q6. (4 marks) Masked attention prevents a position from attending to future positions, preserving the autoregressive property so predictions depend only on known (past) tokens. (2) Masked positions are set to (large negative) before softmax, so their softmax weights become 0. (2)
Q7. (4 marks) Self-attention is time and memory in sequence length . (2) Because computing produces an attention matrix — every token attends to every other token. (2)
Q8. (3 marks)
- Encoder-only (e.g., BERT) — classification/understanding. (1)
- Decoder-only (e.g., GPT) — autoregressive text generation. (1)
- Encoder–decoder (e.g., original Transformer/T5) — machine translation / seq2seq. (1)
Q9. (4 marks) Two sublayers: (1) multi-head self-attention, (2) position-wise feed-forward network. (2) In the original design each sublayer uses a residual connection followed by layer norm: (post-norm). (2)
Q10. (3 marks) FlashAttention computes exact attention in a tiled/block-wise manner using online softmax, keeping intermediate blocks in fast SRAM and never materializing the full matrix in HBM. (2) Advantage: reduced memory (linear in ) and faster wall-clock time via fewer memory reads/writes (IO-awareness). (1)
[
{"claim":"d_k = 512/8 = 64","code":"result = (512//8 == 64)"},
{"claim":"Total QKV projection params = 786432","code":"result = (3*512*512 == 786432)"},
{"claim":"PE(1,0)=sin(1)~0.8415, PE(1,1)=cos(1)~0.5403","code":"import sympy as sp; s=sp.N(sp.sin(1)); c=sp.N(sp.cos(1)); result = (abs(s-0.8415)<1e-3 and abs(c-0.5403)<1e-3)"},
{"claim":"PE(1,2)=sin(0.01)~0.0100, PE(1,3)=cos(0.01)~0.99995","code":"import sympy as sp; s=sp.N(sp.sin(sp.Rational(1,100))); c=sp.N(sp.cos(sp.Rational(1,100))); result = (abs(s-0.0100)<1e-3 and abs(c-0.99995)<1e-4)"}
]