← Questions

Implement multi-head attention and LLM sampling

0
Scale AICodingLast asked Feb 2026

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) takes x of shape (batch, seq, d_model) and four (d_model, d_model) weight matrices, and returns (batch, seq, d_model).
  • Split into num_heads heads of size d_model / num_heads, compute softmax(Q Kᵀ / sqrt(d_head) + mask) V per head, concatenate the heads and project with w_o.
  • mask is boolean, True where attention is allowed; a causal mask lets position t see only positions <= t.

Part 2: sampling

  • sample(logits, temperature=1.0, top_k=None, top_p=None, rng=None) -> int divides the logits by temperature, keeps only the top_k highest, then keeps the smallest set of most likely tokens whose probability adds up to at least top_p, renormalizes and draws one token. temperature = 0 means 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

  • Scale AI · Coding · Last asked Feb 2026Live coding, one or two rounds, Python

What it tests

transformers · attention · sampling · pytorch

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