Find the planted bugs in a small transformer, then add KV caching

HardConcept45 minRERSPro outline

The question

Here is part of a NumPy transformer that trains but gives poor results. Find the bugs, explain how you found each one, then add a KV cache for generation.

def attention(x, Wq, Wk, Wv, n_heads): T, D = x.shape; h = D // n_heads q = (x @ Wq).reshape(T, n_heads, h) k = (x @ Wk).reshape(T, n_heads, h) v = (x @ Wq).reshape(T, n_heads, h) s = np.einsum('thd,shd->hts', q, k) / h mask = np.tril(np.ones((T, T)), k=-1) == 1 s = np.where(mask, -1e9, s) w = np.exp(s) / np.exp(s).sum(axis=1, keepdims=True) return np.einsum('hts,shd->thd', w, v).reshape(T, D)

def block(x, p): x = layernorm(x) + attention(x, p.Wq, p.Wk, p.Wv, p.n_heads) return x + mlp(layernorm(x), p)

What the interviewer is looking for

  • Finds the bugs by reasoning and by small tests, not guessing
  • Explains the symptom each bug causes
  • Fixes the residual and mask logic correctly
  • Adds a KV cache with correct positions
  • Checks cached and uncached outputs match

Your answer

Saved in this browser only. Nobody else sees it.

Why this question

Written by BlogLLM in the style of a publicly reported OpenAI question type: find planted bugs in a few hundred lines of transformer code, then add KV caching.

Reported in: aced.io, careerservices.fas.harvard.edu

Practice question written by BlogLLM based on publicly reported interview formats. Not an official OpenAI question.