Exercises

Practice head-axis shapes, two-head calculations, output projection, parameter and score counts, ablation claims, head permutation, and silent implementation errors.

These exercises move from head dimensions and numerical calculations to cost, ablation, and implementation audits. Each prompt states the convention it uses.

Trace Head Dimensions

Q1. Count the attention distributions

A multi-head layer receives a batch with B=3B=3 sequences, uses h=8h=8 heads, and has sequence length T=20T=20. How many separate attention rows are formed for one query position across the complete batch?

Compute it first, then check your number.

Hint
Do not multiply by TT again; the question fixes one query position.
Solution
There are Bh=3(8)=24Bh=3(8)=24 independently normalized rows for the selected query position.
Not attempted
Review

Not marked done.

Q2. Derive a valid output projection

A layer uses dmodel=12d_{model}=12, h=3h=3, and dv=5d_v=5. The implementation does not require hdv=dmodelhd_v=d_{model}. What shape must WOW_O have?

Choose one

Select one choice, then check.

Hint
Complete (B,T,hdv)(?,?)=(B,T,dmodel)(B,T,hd_v)(?,?)=(B,T,d_{model}).
Solution
WOW_O has shape (hdv,dmodel)=(15,12)(hd_v,d_{model})=(15,12).
Not attempted
Review

Not marked done.

Calculate Two Heads

Q3. Calculate one head result

For the final query, a head has attention weights [0.25,0.25,0.50][0.25,0.25,0.50] and value vectors [1,0][1,0], [0,1][0,1], and [1,1][1,1]. What is the first coordinate of the head result?

Compute it first, then check your number.

Hint
Apply the same three weights to the first coordinate of every value.
Solution
The result is 0.25[1,0]+0.25[0,1]+0.50[1,1]=[0.75,0.75]0.25[1,0]+0.25[0,1]+0.50[1,1]=[0.75,0.75]. Its first coordinate is 0.750.75.
Not attempted
Review

Not marked done.

Q4. Place the head axis correctly

Combined query projection produces a tensor with shape (B,T,hdk)=(2,7,12)(B,T,hd_k)=(2,7,12). The layer uses h=3h=3 equal-width heads. Which shape should enter the batched query-key product?

Choose one

Select one choice, then check.

Hint
Trace (B,T,hdk)(B,T,h,dk)(B,h,T,dk)(B,T,hd_k)\to(B,T,h,d_k)\to(B,h,T,d_k).
Solution
The required shape is (2,3,7,4)(2,3,7,4).
Not attempted
Review

Not marked done.

Q5. Apply the shared output projection

Two heads produce z(1)=[1,2]z^{(1)}=[1,2] and z(2)=[3,4]z^{(2)}=[3,4]. They are concatenated in that order. Let

WO=[10011111].W_O= \begin{bmatrix} 1&0\\ 0&1\\ 1&1\\ 1&-1 \end{bmatrix}.

What is the second coordinate of [z(1);z(2)]WO[z^{(1)};z^{(2)}]W_O?

Compute it first, then check your number.

Hint
The concatenated row is [1,2,3,4][1,2,3,4].
Solution
The projected row is [8,1][8,1], so the requested coordinate is 11.
Not attempted
Review

Not marked done.

Count What the Layer Stores and Learns

Q6. Count projection parameters

A bias-free layer uses dmodel=16d_{model}=16, h=4h=4, and dk=dv=4d_k=d_v=4. Count all entries in WQW_Q, WKW_K, WVW_V, and WOW_O.

Compute it first, then check your number.

Hint
Under hdk=hdv=dmodelhd_k=hd_v=d_{model}, every combined projection is a dmodel×dmodeld_{model}\times d_{model} matrix.
Solution
The three input projections and one output projection contain 4dmodel2=4(162)=10244d_{model}^2=4(16^2)=1024 entries.
Not attempted
Review

Not marked done.

Q7. Count dense score entries

A dense attention layer has B=2B=2, h=6h=6, and T=50T=50. How many score entries does its full score tensor contain before masking?

Compute it first, then check your number.

Hint
The score shape is (B,h,T,T)(B,h,T,T).
Solution
2(6)(50)(50)=30,0002(6)(50)(50)=30{,}000 entries.
Not attempted
Review

Not marked done.

Interpret Interventions Carefully

Q8. Interpret a head-zeroing result

Zeroing head 4 before concatenation reduces validation accuracy by 0.2 percentage points on one task. Which conclusion is supported?

Choose the supported claim

Select one choice, then check.

Hint
Zeroing at inference is not the same experiment as pruning and retraining.
Solution
The bounded claim is supported. The result does not establish a universal role or explain how later components compensate.
Not attempted
Review

Not marked done.

Q9. Preserve a layer while permuting heads

All head-result blocks are permuted before concatenation. Which additional change preserves the same layer function?

Choose one

Select one choice, then check.

Hint
For row-vector notation, ZWOZW_O contracts the coordinates of ZZ with the rows of WOW_O.
Solution
Apply the matching inverse permutation to the row blocks of WOW_O. Head indices are coordinate labels, not fixed semantic identities.
Not attempted
Review

Not marked done.

Integrated Audit

Q10. Locate a silent head-order error

A combined implementation and a verified separate-head implementation agree on each Z(r)Z^{(r)}. Both final outputs have shape (B,T,dmodel)(B,T,d_{model}), but their values differ. The combined implementation concatenates heads in the order [2,1][2,1] while using the original WOW_O. What is the smallest complete repair?

Choose the repair

Select one choice, then check.

Hint
The error preserves shape and appears only after correct head results are joined.
Solution
Either concatenate in the reference order or permute the input row blocks of WOW_O consistently. Changing softmax or merely reshaping does not repair the semantic coordinate mismatch.
Not attempted
Review

Not marked done.

Pause and reflect

Which exercises were difficult, what mistake pattern did you notice, and what should you practice again? The note stays with this exercise set.

0 of 10 exercises marked done

Review

Not marked done.