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.
Glossary
Transformer Block

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.
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.
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.
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.
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.
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.
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.
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.
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 isLayerNorm(x + Sublayer(x))is also incorrect. The correct Pre-Norm formula isx + Sublayer(LayerNorm(x)). Actually, the standard Pre-Norm isLayerNorm(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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
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.
Related Terms
The Transformer block is a composite structure. Understanding its sub-components and surrounding optimization techniques is essential for engineering performant models.
Multi-Head Attention
The core mixing component of the block. It runs multiple scaled dot-product attention operations in parallel, allowing the model to jointly attend to information from different representation subspaces. Each head projects the input into distinct Query, Key, and Value vectors before computing attention weights and aggregating the results.
Feed-Forward Network (FFN)
A position-wise, fully connected network applied identically to each token's representation independently. It typically consists of two linear transformations with a non-linear activation function (like GELU or SwiGLU) in between. The FFN introduces the non-linearity necessary for the model to learn complex functions, often expanding the hidden dimension to 4x the model dimension before projecting back down.
Residual Connection
A critical pathway that adds the original input of a sub-layer directly to its output. This skip connection mitigates the vanishing gradient problem, enabling the successful training of very deep Transformer architectures. It ensures that the network can easily learn identity functions, making the optimization landscape much smoother.
Layer Normalization
A normalization technique applied across the feature dimension of a single token's representation. It stabilizes the training dynamics by re-centering and re-scaling the activations. The placement of Layer Normalization is a key architectural choice, with Pre-LN (applied before the sub-layer) being the standard in modern architectures for improved training stability over the original Post-LN design.
KV Cache
An inference optimization that stores the previously computed Key and Value vectors for all generated tokens. During autoregressive decoding, the model only needs to compute the Query for the new token, preventing the redundant re-computation of past Keys and Values. This trades memory capacity for a massive reduction in computational latency.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us