397. Split and merge attention heads
Multi-head attention runs several smaller attentions in parallel. The trick is all reshapes: split the model dimension into heads, attend per head, glue the heads back.
Write multihead_attention(x, Wq, Wk, Wv, Wo, n_heads) for x of shape (T, d) and square (d, d) weights. Steps:
1. Q = x Wq, K = x Wk, V = x Wv.
2. Reshape each to (T, n_heads, d_head) and move heads first: (n_heads, T, d_head).
3. Per head: softmax(Q K^T / sqrt(d_head)) V, no mask.
4. Move back to (T, n_heads, d_head), reshape to (T, d), multiply by Wo.
def multihead_attention(x, Wq, Wk, Wv, Wo, n_heads)Example 1
Input: multihead_attention([[1.0, 0.0], [0.0, 1.0]], [[1.0, 0.0], [0.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 1.0]], [[1.0, 0.0], [0.0, 1.0]], 2)
Output: [[0.5, 0.5], [0.5, 0.5]]
Constraints
xhas shape(T, d)- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 3 tests to be accepted.
Hint 1
reshape(T, h, dh).transpose(1, 0, 2) to split; transpose(1, 0, 2).reshape(T, d) to merge.
Hint 2
Use a stable softmax over the last axis.
Common mistakes
- Reshaping straight to
(h, T, dh), as the buggy version does, mixes features from different tokens into one head.
Understand the idea
Multi-head attention runs several smaller attentions side by side, each on its own slice of the features. The tricky part is only reshaping: each token's features must be cut into head-sized slices without mixing tokens. Reshape (T, d) to (T, heads, d_head) first, then move the head axis to the front.
T = 2, d = 4, 2 heads, d_head = 2. Q = [[0, 1, 2, 3], [4, 5, 6, 7]]. Correct split: head 0 gets [[0, 1], [4, 5]] and head 1 gets [[2, 3], [6, 7]], so each head sees both tokens. Reshaping straight to (2, 2, 2) would give head 0 [[0, 1], [2, 3]], treating halves of token 0 as two tokens, which is wrong. Each head then computes softmax(Q K^T / sqrt(2)) V with no mask; afterwards move heads back to (T, heads, d_head), reshape to (T, d) and multiply by Wo. Every transformer layer in GPT, BERT and Llama does this split and merge.
Companies11
- Amazon a candidate reported this question Multi-Head Attention From Scratch in NumPy — Amazon MLE Phone Screen | Screna AI
- Anthropic a candidate reported this question Anthropic Senior SWE phone screen experience (Screna)
- Apple a candidate reported this question Apple Machine Learning Engineer Interview | Screna AI
- ByteDance a candidate reported this question TikTok Machine Learning Engineer Interview (questions, process, prep)
- DeepMind the topic appears in interview reports AI Engineering Interview Questions Company Wise (Outcome School)
- Google a candidate reported this question Google Interview Prep (Deep-ML)
- Meta a candidate reported this question Meta Research Engineer Interview Guide (Dataford)
- Mistral the topic appears in interview reports Mistral AI Interview Guide 2026: European Frontier Lab, Open-Weight Strategy, MoE, and La Plateforme
- NVIDIA the topic appears in interview reports NVIDIA Interview Prep: Questions, Process & Study Plan
- OpenAI the topic appears in interview reports OpenAI Research Engineer Interview Guide (Dataford)
- Uber a candidate reported this question Uber Interview Questions — Page 13 | Screna AI
From public interview reports and guides. Not affiliated with or endorsed by these companies.