Transformer Block Assembly › The Standard Transformer Block

Michael BrenndoerferUpdated June 13, 20252 min read
Language AI HandbookAll chapters
Part 15 of 67Transformer Blocks
Chapter 110Transformer Block Assembly › The Standard Transformer Block
Reading section 2 of 16 · The Standard Transformer Block · 2 minSection 2 of 16 · 2 min

Book contents

Section 2 of 16 · 2 min read

Transformer Block Assembly › The Standard Transformer BlockOne section · Sections in this chapter2 / 16 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

A transformer block is a modular unit that can be stacked to build arbitrarily deep models. Each block takes a sequence of token representations as input and produces a refined sequence as output. The refinement happens through two complementary operations: attention (which mixes information across positions) and the feed-forward network (which transforms each position independently).

The modern pre-norm block, used in GPT and LLaMA as well as most contemporary language models, follows a specific structure. The computation proceeds in two stages (first attention, then feed-forward), with each stage wrapped in a residual connection:

h=x+Attention(Norm(x))y=h+FFN(Norm(h))\begin{aligned} \mathbf{h} &= \mathbf{x} + \text{Attention}(\text{Norm}(\mathbf{x})) \\ \mathbf{y} &= \mathbf{h} + \text{FFN}(\text{Norm}(\mathbf{h})) \end{aligned}

where:

  • x∈Rn×d\mathbf{x} \in \mathbb{R}^{n \times d}: the input to the block, a matrix containing nn tokens (one per row) with dd-dimensional representations
  • Norm(⋅)\text{Norm}(\cdot): normalization function, typically RMSNorm in modern architectures, which stabilizes activations before each sublayer
  • Attention(⋅)\text{Attention}(\cdot): multi-head self-attention, which allows each token to gather information from other positions in the sequence
  • FFN(⋅)\text{FFN}(\cdot): the feed-forward network, which transforms each token independently through nonlinear layers (potentially with gating like SwiGLU)
  • h∈Rn×d\mathbf{h} \in \mathbb{R}^{n \times d}: intermediate representation after the attention sublayer, combining the original input with attention's contribution
  • y∈Rn×d\mathbf{y} \in \mathbb{R}^{n \times d}: the block output, passed to the next block in the stack

The structure of each sublayer follows the same pattern: (1) normalize the input, (2) apply the transformation (attention or FFN), and (3) add the result back to the original input via a residual connection. This "normalize → transform → add" pattern repeats for both sublayers.

Placing normalization before each sublayer (pre-norm) lets gradients flow directly through the residual path without being distorted by normalization. This ordering, which we analyzed in the pre-norm vs post-norm chapter, is important for training deep transformer models (50+ layers).

The Two-Sublayer Pattern

Every transformer block contains exactly two sublayers: attention and feed-forward. Each sublayer follows the same pattern: normalize, transform, add residual. This uniformity simplifies implementation and enables modular stacking of blocks.