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.
qis(..., T_q, d),kis(..., T_k, d)andvis(..., T_k, d_v).
softmax(x, axis=-1): weights that are positive and sum to 1 alongaxis, finite on logits around 10,000.attention(q, k, v):softmax(q kᵀ / √d) v, shape(..., T_q, d_v).attention(q, k, v, causal=True): positioniattends only to positionsj <= i.attention_blocks(q, k, v, block): the level 2 output, reading the keysblockat a time, without ever building the(T_q, T_k)score matrix.
Call Returns softmax([1000, 1001, 1002])[0.09, 0.24, 0.67]attention(Q, K, V), withQ=[[1, 0], [0, 1]],K=[[1, 0], [1, 1]]andV=[[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 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.