What are the two core sublayers inside a standard Transformer encoder block?
Last updated
An encoder block has two sublayers: multi-head self-attention and a position-wise feed-forward network. Each is wrapped in a residual connection plus layer normalization.
Multi-head self-attention:
Every token attends to every other token (bidirectional, no causal mask). This is the only place where information moves between positions.
Multiple heads let the model attend to different relationships in parallel subspaces.
Position-wise feed-forward network:
The same two-layer MLP is applied independently to each token (d → 4d → d with ReLU or GELU).
It transforms each token's representation and holds most of the block's parameters.
The wrapping around each sublayer:
Original post-norm form:
LayerNorm(x + Sublayer(x)). Modern pre-norm form:x + Sublayer(LayerNorm(x)).Residuals preserve gradient flow through deep stacks, and normalization stabilizes activation scale.
- https://github.com/purepisces/Wenqing-Machine_Learning_Blog/blob/main/Machine-Learning-Interview-Questions/Transformer-Interview-Question.md
- https://medium.com/@tejpal.abhyuday/part-1-transformers-working-of-encoder-block-67655545081f
- https://www.sundeepteki.org/advice/the-transformer-revolution-the-ultimate-guide-for-ai-interviews