← Questions
Implement multi-head attention and LLM sampling
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Scale AI's coding round, last asked Feb 2026. It tests transformers, attention and sampling.
Implement multi-head self-attention from scratch in NumPy or PyTorch (no built-in attention layer), then the sampling step of a language model: given the logits for the next token, choose a token with temperature, top-k and top-p (nucleus) sampling.
Part 1: multi-head attention
Unless you agree on a different interface with the interviewer, assume:
-
multi_head_attention(x, w_q, w_k, w_v, w_o, num_heads, mask=None)takesxof shape(batch, seq, d_model)and four(d_model, d_model)weight matrices, and returns(batch, seq, d_model). - Split into
num_headsheads of sized_model / num_heads, computesoftmax(Q Kᵀ / sqrt(d_head) + mask) Vper head, concatenate the heads and project withw_o. -
maskis boolean,Truewhere attention is allowed; a causal mask lets positiontsee only positions<= t.
Part 2: sampling
-
sample(logits, temperature=1.0, top_k=None, top_p=None, rng=None) -> intdivides the logits bytemperature, keeps only thetop_khighest, then keeps the smallest set of most likely tokens whose probability adds up to at leasttop_p, renormalizes and draws one token.temperature = 0means greedy (argmax).
Example
logits = [2.0, 1.0, 0.0, -1.0]
softmax(logits) -> [0.644, 0.237, 0.087, 0.032]
temperature 0.5 -> [0.865, 0.117, 0.016, 0.002] # sharper
temperature 2.0 -> [0.455, 0.276, 0.167, 0.102] # flatter
top_k = 2 -> draw from tokens {0, 1} with [0.731, 0.269]
top_p = 0.8 -> 0.644 < 0.8, 0.644 + 0.237 = 0.881 >= 0.8: same two tokens
top_p = 0.6 -> token 0 only
temperature 0 -> 0
Constraints
- Attention must be vectorized over batch and heads (no Python loop over positions).
- Sampling must be reproducible given a seeded
rng.
Clarifying questions
- NumPy or PyTorch? Are biases in the projections needed?
- Is the mask boolean or additive, and what is its shape?
- In which order are temperature, top-k and top-p applied?
- How should ties at the top-k boundary be handled?
What a strong answer covers
- Correct reshapes:
(B, T, C)to(B, H, T, d_head)with a transpose, scores(B, H, T, T), and back. - Scaling by
sqrt(d_head), and applying the mask before the softmax by filling disallowed scores with a large negative value (and the care needed in half precision). - A numerically stable softmax (subtract the maximum); a row with every position masked.
- Complexity: O(B · H · T² · d_head) time and O(B · H · T²) memory for the scores.
- Sampling with sorting and a cumulative sum for top-p, always keeping at least one token; tests on fixed logits with a seeded generator.
Follow-ups
- Add a key-value cache for generation. What shapes change?
- Make attention memory-efficient for long sequences (chunking, the idea behind FlashAttention).
- Implement grouped-query attention, where several query heads share one key/value head.
Where it's asked
What it tests
transformers · attention · sampling · pytorch
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
