Four Parts, One Pass

TransformersHard+30 XP+30

Four Parts, One Pass

Self-attention, add and normalize, feed-forward, add and normalize. Four parts, in that order, and that is the entire block — the thing GPT stacks ninety-six times without changing anything about it.

Task: write transformer_block(x, Wq, Wk, Wv, W1, b1, W2, b2) for a single masked attention head, returning the block's output, rounded to 4 decimal places. Both normalizations have their learned scale and shift at the neutral values 111 and 000, so there is nothing extra to apply.

argumentshapewhat it is
xnnn vectors of length dddthe tokens entering the block
Wq, Wkd×dkd \times d_kd×dk​queries and keys
Wvd×dd \times dd×dvalues, the same width as x
W1, b1d×hd \times hd×h, length hhhwiden each position to the hidden width hhh
W2, b2h×dh \times dh×d, length dddsqueeze it back to ddd

1 — Masked self-attention. With Q=xWqQ = x W_qQ=xWq​, K=xWkK = x W_kK=xWk​ and V=xWvV = x W_vV=xWv​,

scoreij=Qi⋅Kjdkfor j≤i\text{score}_{ij} = \frac{Q_i \cdot K_j}{\sqrt{d_k}} \qquad \text{for } j \le iscoreij​=dk​​Qi​⋅Kj​​for j≤i

Row iii may use positions 000 through iii only; everything to its right is blocked. Softmax each row over the positions it is allowed to see, then blend the values: ai=∑j≤iwijVja_i = \sum_{j \le i} w_{ij} V_jai​=∑j≤i​wij​Vj​.

2 — Add and normalize. h=LN⁡(x+a)h = \operatorname{LN}(x + a)h=LN(x+a).

3 — Feed-forward, each position on its own. f=ReLU⁡(hW1+b1) W2+b2f = \operatorname{ReLU}(h W_1 + b_1)\, W_2 + b_2f=ReLU(hW1​+b1​)W2​+b2​, where ReLU⁡\operatorname{ReLU}ReLU keeps positive entries and sets negative ones to 000.

4 — Add and normalize again. The block returns LN⁡(h+f)\operatorname{LN}(h + f)LN(h+f).

LN⁡\operatorname{LN}LN works on each position's vector separately, using that vector's own ddd slots:

LN⁡(v)j=vj−m1d∑l=1d(vl−m)2+10−5,m=1d∑l=1dvl\operatorname{LN}(v)_j = \frac{v_j - m}{\sqrt{\dfrac{1}{d}\sum_{l=1}^{d}(v_l - m)^2 + 10^{-5}}}, \qquad m = \frac{1}{d}\sum_{l=1}^{d} v_lLN(v)j​=d1​∑l=1d​(vl​−m)2+10−5​vj​−m​,m=d1​∑l=1d​vl​

Round only the numbers you return.

Two things are worth noticing once it runs. The output has exactly the shape of the input, which is the property that lets you stack this ninety-six times. And the second residual adds hhh, not xxx: every sublayer adds to whatever it was handed, so one running workspace travels all the way up the stack.

tokens arrive as vectors
attention mixes information across positions
add the input back and normalize
feed-forward refines each position alone
add and normalize once more
vectors leave, same shape as they arrived

Notice the last line: the block gives back exactly the shape it was given. That is what makes stacking possible.

Examples

x = [[1.0, 0.0, -1.0], [0.5, 1.0, 0.0]]
Wq = [[1.0, 0.0], [0.0, 1.0], [0.5, -0.5]]
Wk = [[0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]
Wv = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
W1 = [[1.0, 0.0, -1.0, 0.5], [0.0, 1.0, 1.0, -0.5], [0.5, -1.0, 0.0, 1.0]]
b1 = [0.0, 0.1, -0.1, 0.0]
W2 = [[1.0, 0.0, 0.5], [0.0, 1.0, -1.0], [-0.5, 0.5, 0.0], [1.0, 0.0, 1.0]]
b2 = [0.0, 0.0, 0.0]
returns [[0.8436, 0.5612, -1.4048], [-0.0031, 1.2263, -1.2232]]
x = [[0.5, -1.0, 2.0], [1.0, 1.0, 1.0], [-0.5, 0.0, 1.5]]
Wq = [[1.0, 0.5, 0.0], [0.0, 1.0, 0.5], [0.5, 0.0, 1.0]]
Wk = [[0.0, 1.0, 1.0], [1.0, 0.0, 0.5], [0.5, 0.5, 0.0]]
Wv = [[1.0, 0.0, 0.5], [0.5, 1.0, 0.0], [0.0, 0.5, 1.0]]
W1 = [[1.0, -1.0, 0.5, 0.0], [0.5, 1.0, 0.0, -1.0], [0.0, 0.5, 1.0, 1.0]]
b1 = [0.2, -0.2, 0.0, 0.1]
W2 = [[1.0, 0.0, -0.5], [0.0, 1.0, 0.5], [0.5, -0.5, 1.0], [-1.0, 0.0, 0.0]]
b2 = [0.1, -0.1, 0.0]
returns [[-0.8018, -0.6079, 1.4098], [-1.0257, -0.3303, 1.356], [-1.2138, -0.0216, 1.2354]]

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