Transformer Block
Compose Pre-LayerNorm, Multi-Head Attention, residual identity highways, and 2-layer FeedForward MLPs.
A transformer block is a modular sandwich: attention (so tokens communicate with each other) + feed-forward network (so each token processes information independently) + skip connections (so gradients flow freely without vanishing) + normalization (so numbers remain stable). Stack 4 of these and you have our full model.
Modern LLMs simply stack dozens of these identical blocks. GPT-3 stacks 96 blocks; LLaMA-70B stacks 80 blocks. Pre-LayerNorm (normalizing before the sublayer) is now universal because it guarantees stable training without fragile warmup schedules.
Pre-Norm Transformer Block
Layer Normalization
Position-wise Feed-Forward Network
Dataflow through a Single Transformer Block (Layer l)
The Residual Highway & Gradient Flow
In our bilingual model with 4 blocks ($L=4$), this residual connection is vital. It guarantees that the early token representations retain their character identities while deeper layers compose complex syntactic and philosophical relationships.
- Transformer block formula: x + Attention(LN(x)), followed by x + FFN(LN(x)).
- Residual connections (x + f(x)) create an identity gradient highway preventing vanishing gradients.
- LayerNorm stabilizes activations across the 64 embedding dimensions at each sublayer.
- FeedForward network expands 4x into 256 dimensions with GELU, providing non-linear memorization capacity.
Trace how tensor activations travel through the residual stream without losing their base token features.