Many Heads, One Vector

AttentionHard+30 XP+30

Many Heads, One Vector

A single attention pattern has to choose. Its weights sum to 111, so "slept" attending to its subject means "slept" attending less to its adverb — even though both relationships are real. Multi-head attention buys its way out: several attention mechanisms run side by side, each with its own budget, and their outputs are joined back into one vector.

The projections are already done. queries, keys and values are each an n×dmodeln \times d_{model}n×dmodel​ table, and the heads are carved out of the width:

  • dhead=dmodel/hd_{head} = d_{model} / hdhead​=dmodel​/h, where hhh is num_heads and divides dmodeld_{model}dmodel​ exactly.
  • Head ttt owns the contiguous column slice t⋅dheadt \cdot d_{head}t⋅dhead​ through (t+1)⋅dhead−1(t+1) \cdot d_{head} - 1(t+1)⋅dhead​−1 of all three tables, and never looks outside it.
  • Within its slice, head ttt runs ordinary self-attention over the whole sequence: score each query against every key, divide by dhead\sqrt{d_{head}}dhead​​, softmax across the row, then blend that head's values by those weights.
  • Every head produces an n×dheadn \times d_{head}n×dhead​ output. Concatenate them back in head order into an n×dmodeln \times d_{model}n×dmodel​ table, then multiply that table on the right by the output matrix WOW_OWO​, of shape dmodel×dmodeld_{model} \times d_{model}dmodel​×dmodel​: entry [i][c][i][c][i][c] of the result is row iii of the joined table dotted with column ccc of WOW_OWO​.

Task: write multi_head_attention(queries, keys, values, num_heads, w_out) returning the final n×dmodeln \times d_{model}n×dmodel​ table, every entry rounded to 4 decimal places. Round once, at the very end.

  • Row i of the result is the output for token i.
  • hhh may be 111 (plain single-head attention) and may be as large as dmodeld_{model}dmodel​ (every head one column wide, so dhead=1\sqrt{d_{head}} = 1dhead​​=1).

The heads are narrow, which is why eight of them cost roughly what one wide one costs. It is also why the scale is dhead\sqrt{d_{head}}dhead​​: a dot product inside a head sums dheadd_{head}dhead​ terms, and that count is what decides how large the raw scores grow.

Examples

queries = [[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 1.0, 0.0]]
keys = [[1.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 1.0]]
values = [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]
num_heads = 2
w_out = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
returns [[2.321, 3.321, 5.679, 6.679], [3.679, 4.679, 4.321, 5.321]]
queries = [[1.0, 1.0, 0.0, 2.0], [0.0, 2.0, 1.0, 0.0], [2.0, 0.0, -1.0, 1.0]]
keys = [[1.0, 0.0, 2.0, 0.0], [0.0, 1.0, 0.0, 1.0], [1.0, 1.0, 1.0, -1.0]]
values = [[1.0, 0.0, 2.0, 1.0], [0.0, 3.0, 1.0, 0.0], [2.0, 1.0, 0.0, 4.0]]
num_heads = 2
w_out = [[0.5, 0.0, 1.0, 0.0], [0.0, 0.5, 0.0, 1.0], [1.0, 0.0, 0.5, 0.0], [2.0, 1.0, 0.0, 0.5]]
returns [[2.5054, 0.9924, 1.8259, 1.4324], [5.2159, 2.6036, 1.646, 2.6392], [2.6356, 0.8689, 1.8374, 1.0127]]
queries = [[1.0, 0.0, 2.0, 1.0], [0.0, 1.0, -1.0, 2.0]]
keys = [[2.0, 1.0, 0.0, 1.0], [1.0, -1.0, 1.0, 1.0]]
values = [[1.0, 2.0, 0.0, 3.0], [4.0, 0.0, 2.0, 1.0]]
num_heads = 1
w_out = [[1.0, 0.0, 0.0, 0.5], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 2.0, 0.0], [0.5, 0.0, 0.0, 1.0]]
returns [[3.7449, 0.7551, 2.4898, 3.1888], [2.8649, 1.6351, 0.7297, 3.4088]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run