1. Pre-norm block
Pre-norm applies layer normalization before each sublayer, which is the standard for stable deep transformers.
// pre-norm residual blockTyporithm · hard
Step 1 of 8 · intro card
Attention & Transformers
A transformer block stacks an attention sublayer and a feed-forward network, each wrapped in a residual connection and layer normalization (pre-norm is standard). Stacking N such blocks builds depth, and a causal mask keeps decoder tokens from attending to future positions.
The transformer block is the repeating unit of the model. It has two sublayers: a multi-head attention layer and a position-wise feed-forward network (FFN) applied to each token independently.
Each sublayer is wrapped in a residual connection — its output is added back to its input — and a layer normalization. In the now-standard pre-norm arrangement, layerNorm is applied before the sublayer, which keeps gradients stable in very deep stacks.
Stacking N identical blocks gives the model its depth. In a decoder, attention uses a causal mask so that each position can only attend to itself and earlier tokens, never to future ones, which is what makes left-to-right generation possible.
Two sublayers: attention then a feed-forward network.
Residual + layer norm wrap each sublayer (pre-norm).
Stack N blocks; a causal mask hides future tokens.
// pre-norm residual block
x = x.add(attention(layerNorm(x)));
x = x.add(feedForward(layerNorm(x)));
// stack N blocks; causal mask for decodingConnect each part of the code to the concept before you type.
Pre-norm applies layer normalization before each sublayer, which is the standard for stable deep transformers.
// pre-norm residual blockThe attention output is added back to x through a residual connection, with layerNorm applied to the input.
x = x.add(attention(layerNorm(x)));A position-wise feed-forward network follows attention, again wrapped in a residual and layer norm.
x = x.add(feedForward(layerNorm(x)));Layer norm rescales each token's features to stabilize training across deep stacks.
layerNorm(x)Many blocks are stacked for depth, and a causal mask stops decoder tokens from attending to the future.
// stack N blocks; causal mask for decodingRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.