← Questions

QKV attention programming with einsum fluency

0
AnthropicAI/ML designLast asked Feb 2026

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.einsum and broadcasting, no loops over batch or heads, and shapes stated at each step.

Clarifying questions

  • Is the mask True for blocked positions everywhere, and what should a row that is fully blocked return?
  • For a causal mask with s > t, are the queries the last t positions of the key sequence?
  • In grouped-query attention, does query head i use KV group i // (h // g)?
  • What does the cache hold, keys and values per layer of shape (b, h, s, d), and does decode_step return the updated cache?

What a strong answer covers

  • Softmax with the row max subtracted, and masked scores set to a large negative number (or -inf with a guard for fully masked rows).
  • Scores as einsum("bhtd,bhsd->bhts", q, k) / sqrt(d), output as einsum("bhts,bhsd->bhtd", p, v), and the projections as einsum("btm,mhd->bhtd", x, w) and einsum("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 == h equals 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-n sequence, 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 q and k.

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

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing