Inferensys

Glossary

Transformer Block

The fundamental building block of the Transformer architecture, typically consisting of a multi-head self-attention sub-layer and a position-wise feed-forward network, each wrapped with residual connections and layer normalization.
Developer demonstrating multi-agent tool use, agent tool selection interface on laptop, casual tech demo moment.
FUNDAMENTAL ARCHITECTURAL UNIT

What is a Transformer Block?

The Transformer block is the core computational module replicated to form the depth of a Transformer model, standardizing how sequence data is transformed through attention and feed-forward mechanisms.

A Transformer block is the fundamental building block of the Transformer architecture, consisting of a multi-head self-attention sub-layer and a position-wise feed-forward network (FFN) , each wrapped with residual connections and layer normalization. This standardized module processes an input sequence of vectors and outputs a transformed sequence of the same dimensionality, enabling the stacking of many identical blocks to form deep models.

Within the block, the self-attention sub-layer allows each token to aggregate context from the entire sequence, while the subsequent FFN applies a non-linear transformation independently to each position. The residual connections add the sub-layer's input directly to its output, mitigating the vanishing gradient problem, and layer normalization stabilizes training by normalizing activations across the feature dimension.

THE TRANSFORMER BLOCK

Key Architectural Features

The fundamental building block of the Transformer architecture, consisting of a multi-head self-attention sub-layer and a position-wise feed-forward network, each wrapped with residual connections and layer normalization.

01

Multi-Head Self-Attention

The core sub-layer that allows the model to jointly attend to information from different representation subspaces at different positions.

  • Mechanism: Projects the input into multiple sets of Query, Key, and Value (QKV) vectors, computes scaled dot-product attention in parallel, and concatenates the results.
  • Purpose: Enables each token to dynamically weigh the importance of every other token in the sequence, capturing long-range dependencies.
  • Example: In the sentence 'The cat sat on the mat because it was tired,' the head learns to link 'it' back to 'cat' with a high attention weight.
02

Position-Wise Feed-Forward Network (FFN)

A fully connected network applied identically and independently to each token's representation after the attention sub-layer.

  • Structure: Typically consists of two linear transformations with a non-linear activation function like GELU or ReLU in between.
  • Function: Introduces non-linearity and processes the information gathered by the attention heads, expanding the hidden dimension to a larger intermediate size before projecting it back.
  • Analogy: If attention is the 'communication' step, the FFN is the 'computation' step where each token processes what it has learned.
03

Residual Connections

Pathways that add the original input of a sub-layer directly to its output, bypassing the transformation.

  • Formula: Output = LayerNorm(Input + Sublayer(Input))
  • Critical Role: Mitigates the vanishing gradient problem, enabling the successful training of very deep networks by allowing unimpeded gradient flow.
  • Impact: Without residual connections, deep Transformers would be nearly impossible to train effectively, as the signal would degrade with depth.
04

Layer Normalization

A normalization technique applied across the feature dimension of a single token's representation, not across the batch.

  • Operation: Computes the mean and variance of a token's embedding vector and normalizes it, followed by a learned affine transformation.
  • Placement: The original 'Post-LN' places it after the residual addition, while modern 'Pre-LN' architectures apply it before the sub-layer for improved training stability.
  • Benefit: Reduces internal covariate shift and accelerates convergence during training.
05

Quadratic Complexity Bottleneck

The inherent computational limitation of standard self-attention, where both memory and time complexity scale quadratically with the input sequence length.

  • Complexity: O(n²) for an input sequence of length n, as every token attends to every other token.
  • Consequence: Processing a 32k-token document requires 1,024 times more compute than a 1k-token document, making it prohibitive for very long contexts.
  • Mitigations: Techniques like Sparse Attention, Sliding Window Attention, and FlashAttention are designed to overcome this bottleneck.
06

Pre-Norm vs. Post-Norm Configuration

A critical architectural choice defining the order of operations within the Transformer block, significantly impacting training stability.

  • Post-Norm (Original): x + Sublayer(LayerNorm(x)). Prone to gradient instability in deep models, requiring careful learning rate warm-up.
  • Pre-Norm (Modern): x + Sublayer(LayerNorm(x)) is incorrect; the standard Pre-Norm is LayerNorm(x + Sublayer(x)) is also incorrect. The correct Pre-Norm formula is x + Sublayer(LayerNorm(x)). Actually, the standard Pre-Norm is LayerNorm(x + Sublayer(x)) is Post-Norm. Let's be precise: Pre-Norm applies normalization before the sub-layer: x + Sublayer(LayerNorm(x)). Post-Norm applies it after: LayerNorm(x + Sublayer(x)).
  • Adoption: Pre-Norm is the de facto standard in modern models like GPT-3 and Llama because it allows for more stable training without extensive warm-up.
TRANSFORMER BLOCK DEEP DIVE

Frequently Asked Questions

Clear, technical answers to the most common questions about the fundamental building block of modern large language models.

A Transformer Block is the fundamental, repeatable computational unit that constitutes a Transformer architecture. It operates by processing an input sequence of vectors through two primary sub-layers: a multi-head self-attention mechanism and a position-wise feed-forward network (FFN). The self-attention sub-layer allows each token to dynamically weigh the influence of every other token in the sequence, generating a context-aware representation. The FFN then applies an identical non-linear transformation to each token's representation independently. Crucially, both sub-layers are wrapped with residual connections and followed by layer normalization to ensure stable gradient flow during training. By stacking multiple identical blocks, the model can iteratively refine its contextual understanding of the input.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.