Audit a Two-Head Attention Layer
Implement the same two-head layer in separate and combined forms, then compare every intermediate. The audit checks masks, row sums, split widths, axis order, concatenation, W_O, head zeroing, and failures that retain valid shapes.
Multi-head attention adds transformations around a familiar single-head core. The audit must therefore check two kinds of correctness:
- each head performs valid attention;
- split, transpose, concatenate, and output projection preserve the intended axes and head order.
A final tensor with shape does not prove either one.
Compare Two Equivalent Implementations
The clearest equivalence test calculates the same layer in two forms:
- separate form: project and run each head with its own matrices;
- combined form: concatenate projection matrices, project once, split the final feature axis into heads, and run those heads.
Before , the corresponding tensors must agree. After the same concatenation order and , the final outputs must agree.
Audit separate and combined two-head attention
The program reconstructs the chapter trace in both forms and checks head outputs, row sums, causal zeroes, joined width, and the output projection.
Ready to run.
The expected final rows are approximately
Failures to Introduce Deliberately
Once the verified implementation passes, a useful audit changes one operation at a time:
| Broken change | Expected evidence |
|---|---|
| omit transpose | score axes contract or broadcast incorrectly |
| split with the wrong widths | head projections disagree with the separate form |
| concatenate along position instead of features | changes or token records interleave |
| give both heads the same projected slice | their Q/K/V and weights become identical in this trace |
| swap head results without swapping row blocks | joined shape remains valid but changes |
The separate-head implementation acts as an oracle only because it has already been checked against the hand calculation. Two implementations can agree and still share the same conceptual error.
Head Zeroing Needs a Defined Location
To test head 2's current contribution, set before concatenation and keep fixed. Zeroing an attention map before multiplying values is a different intervention. Removing projection parameters and retraining is also different. Record the intervention point beside every ablation result.
Q1. Diagnose a valid-shape head-order bug
Two head results are concatenated in reverse order, but the row blocks of the trained remain unchanged. The final output still has the expected shape. What is the most direct test?
Select one choice, then check.
Hint
Solution
Audit in Two Nested Loops
First audit projections, masks, row sums, and values inside every head. Then audit the transformations across heads: split, transpose, concatenate, head order, and . This nested order localizes a failure before it is hidden by the final projection.