Practice in Lab: Project: build a pre-norm decoder block and audit its gradients →
Project: build a pre-norm decoder block and audit its gradients
A decoder block composes normalization, projection layout, multiple attention heads, causal masking, residual connections and a feed-forward network. Plausible shapes do not establish the correct computation. This project tests future-token isolation, the identity residual when both branches are zero and finite gradients through every parameter family. It also checks the causal backward boundary. The block is small and executes real tensor operations on CPU; it is a mathematical building block rather than a pretrained model or a fused-kernel performance claim.
import torch
x = torch.arange(8.).reshape(1, 2, 4)
heads = x.reshape(1, 2, 2, 2)
print(heads.transpose(1, 2).shape)Review the design
Audit QKV packing, head transposes, residual ordering and normalization convention. Loading a checkpoint requires the same conventions, not merely matching tensor sizes.
Your task
Implement one causal pre-norm block on nonempty x[B,T,D]. parameters has exactly n1/n2 [D], qkv [3D,D], out [D,D], ff1 [2D,D], ff2 [D,2D]. Use layer_norm eps=1e-5 with these weights and no biases; split contiguous Q/K/V projections into heads, scaled causal softmax attention, concatenate heads and add the output projection residual. Then add ff2(GELU(ff1(layer_norm(residual,n2)))). Dropout is zero; heads must divide D. Return the differentiable output and reject a wrong schema.
The explanation opens here once every check passes.