QKV attention programming with einsum fluency
I was asked this
No reports yet. Sign in to say where you were asked it.
An AI/ML design interview question asked in Anthropic's live technical screen, last asked Feb 2026. It tests attention, transformers and pytorch.
A short programming screen around sequence models and scaled dot-product attention, plus quick ML knowledge questions. Reading equations and expressing the math with einsum-style ops is the point.
Use NumPy. Shapes: b batch, h heads, t query positions, s key positions, d head width, m model width.
import numpy as np
def softmax(x, axis=-1): ...
def attention(q, k, v, mask=None): ... # mask: True = blocked
def causal_mask(t, s=None): ...
def multi_head_attention(x, wq, wk, wv, wo, causal=True): ... # x: (b, t, m); w*: (m, h, d); wo: (h, d, m)
def grouped_query_attention(q, k, v, mask=None): ... # q: (b, h, t, d); k, v: (b, g, s, d)
def decode_step(x_new, cache, wq, wk, wv, wo): ... # one token with a KV cache dict
Requirements
- Functional: a stable softmax, scaled dot-product attention with an optional boolean mask, a causal mask that works when
s > t, multi-head attention from the given weight shapes, grouped-query attention, and a single decode step that appends to a KV cache. - Non-functional: written with
np.einsumand broadcasting, no loops over batch or heads, and shapes stated at each step.
Clarifying questions
- Is the mask
Truefor blocked positions everywhere, and what should a row that is fully blocked return? - For a causal mask with
s > t, are the queries the lasttpositions of the key sequence? - In grouped-query attention, does query head
iuse KV groupi // (h // g)? - What does the cache hold, keys and values per layer of shape
(b, h, s, d), and doesdecode_stepreturn the updated cache?
What a strong answer covers
- Softmax with the row max subtracted, and masked scores set to a large negative number (or
-infwith a guard for fully masked rows). - Scores as
einsum("bhtd,bhsd->bhts", q, k) / sqrt(d), output aseinsum("bhts,bhsd->bhtd", p, v), and the projections aseinsum("btm,mhd->bhtd", x, w)andeinsum("bhtd,hdm->btm", o, wo). - A causal mask offset by
s - t, so a new query only sees keys up to its own position. - Grouped-query attention by reshaping queries to
(b, g, h/g, t, d)or repeating K and V along the head axis, and the memory saving that motivates it. - The decode step projecting one token, concatenating its K and V onto the cache, and attending without a mask, then a check that it matches full causal attention on the same sequence.
- Tests: rows of the attention weights sum to one, a causal output at position 0 depends only on token 0, GQA with
g == hequals multi-head attention.
Follow-ups
- Why divide by
sqrt(d)? What happens to the softmax without it? - Cost in time and memory of attention over a length-
nsequence, and how FlashAttention avoids storing the score matrix. - How the KV cache size grows with batch, length, layers and heads, and what GQA or MQA saves.
- Add rotary position embeddings to
qandk.
Where it's asked
- Anthropic · Live technical screen · Last asked Feb 202655 min · One engineer on video with screen share, usually in CodeSignal (Google Colab for research roles), Python by default; one practical problem that grows in parts · No AI; looking up documentation and searching the web is fine
What it tests
attention · transformers · pytorch
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
