1/25
Learn

Attention, the way the coding round asks for it

Model coding rounds ask for attention from scratch. One OpenAI ML engineer's phone screen mainly tested how well they knew the Transformer (1 report), and the same candidate's onsite needed NumPy cold (1 report). A Netflix screen asked what attention is and exactly how Q, K and V are computed (1 report). This lesson's practice problem is that round, in four levels:

Write scaled dot-product attention in NumPy, with no Python loops over batches or heads. q is (..., T_q, d), k is (..., T_k, d) and v is (..., T_k, d_v).

  1. softmax(x, axis=-1): weights that are positive and sum to 1 along axis, finite on logits around 10,000.
  2. attention(q, k, v): softmax(q kᵀ / √d) v, shape (..., T_q, d_v).
  3. attention(q, k, v, causal=True): position i attends only to positions j <= i.
  4. attention_blocks(q, k, v, block): the level 2 output, reading the keys block at a time, without ever building the (T_q, T_k) score matrix.
CallReturns
softmax([1000, 1001, 1002])[0.09, 0.24, 0.67]
attention(Q, K, V), with Q = [[1, 0], [0, 1]], K = [[1, 0], [1, 1]] and V = [[10, 0], [0, 10]][[5, 5], [3.3, 6.7]]
attention(Q, K, V, causal=True)[[10, 0], [3.3, 6.7]]
attention_blocks(Q, K, V, 1)[[5, 5], [3.3, 6.7]], the level 2 answer

The lesson builds attention one piece at a time: the lookup, the scores, the d\sqrt{d} scale, the mask and a stable softmax, then the long-input follow-up that level 4 asks for. Nine steps end with a small task in the editor. It builds on the NumPy lesson: axes, keepdims, broadcasting and @ on stacks.