Transformer Architecture Explained
A practical guide to the Transformer architecture, explaining how transformer blocks, self-attention, multi-head attention, embeddings, positional information, normalization, and feed-forward networks work together.
The Transformer architecture is one of the most important developments in modern artificial intelligence. Introduced in 2017, transformers became the foundation for many large language models, generative AI systems, and other neural networks that process sequential or multimodal data.
Before transformers, sequence-processing systems commonly relied on recurrent neural networks and related architectures. These models processed sequences step by step, which made long-range dependencies and large-scale parallel training difficult. Transformers introduced a different approach based primarily on attention, allowing the model to relate different positions in a sequence without requiring the entire sequence to be processed strictly one token at a time during training.
Modern systems such as large language models use transformer architectures in forms that can differ substantially from the original design. Some use decoder-only transformers, some use encoder-only architectures, and some use encoder-decoder structures. Despite these differences, concepts such as attention, embeddings, feed-forward networks, residual connections, and normalization remain central to understanding how transformers work.
What Is the Transformer Architecture?
A Transformer is a neural network architecture designed to process sequences using attention mechanisms rather than relying primarily on recurrence. The architecture transforms input representations through a series of layers, allowing information from different positions to interact.
The original Transformer introduced an encoder-decoder architecture. The encoder processes the input sequence, while the decoder generates an output sequence. Modern language models often use variations of this architecture rather than the original encoder-decoder structure.
| Architecture | Typical use |
|---|---|
| Encoder-only | Understanding or representing input text, such as classification and embedding tasks. |
| Decoder-only | Autoregressive text generation and many modern LLM applications. |
| Encoder-decoder | Sequence-to-sequence tasks such as translation and text transformation. |
The Main Components of a Transformer
Although implementations vary, a transformer can be understood as a collection of major components that work together to transform token representations.
- Token embeddings
- Positional information
- Self-attention
- Multi-head attention
- Feed-forward neural networks
- Residual connections
- Normalization layers
- Output projection
- A stack of repeated transformer blocks
The exact ordering and implementation of these components can vary between architectures. For example, modern models may use different normalization placement, positional encoding methods, attention variants, activation functions, or architectural optimizations.
High-Level Transformer Flow
Input text
↓
Tokenization
↓
Token IDs
↓
Token embeddings
↓
Positional information
↓
Transformer block
├── Self-attention
├── Residual connection
├── Normalization
├── Feed-forward network
└── Residual connection
↓
Repeated transformer blocks
↓
Output representations
↓
Task-specific outputFor a decoder-only language model, the final output is typically projected into vocabulary logits that represent scores for possible next tokens. Other transformer architectures can use the resulting representations for different tasks.
Step 1: Tokenization
Transformers do not normally receive raw human-readable text. An input string is first converted into tokens using a tokenizer. Tokens can represent complete words, parts of words, punctuation, whitespace, or other frequently occurring sequences.
Input:
Transformers process language.
Conceptual tokens:
[Transformers] [ process] [ language] [.]
Token IDs:
[1024] [481] [2937] [13]The exact tokenization depends on the tokenizer. After tokenization, each token is represented by an integer ID from the model's vocabulary. Those IDs are then converted into vectors through an embedding layer.
Step 2: Token Embeddings
A token ID is only an index. The neural network needs a numerical representation containing many dimensions. An embedding layer maps each token ID to a learned vector.
Token ID
↓
Embedding table
↓
[0.21, -0.47, 0.83, 0.14, ...]The embedding vectors are learned during training. They provide the initial numerical representation of tokens before the information is transformed by the deeper layers of the network.
Step 3: Positional Information
Attention by itself does not inherently tell the model where a token appears in a sequence. Because word order matters, transformers need a mechanism for representing positional information.
The original Transformer used positional encodings based on sinusoidal functions. Other transformer implementations use learned positional embeddings or relative-position techniques. Modern language models also commonly use rotary positional embeddings, often abbreviated as RoPE.
The exact technique is architecture-dependent, but the goal is the same: give the network information that allows it to distinguish different positions and model relationships between them.
Why Position Matters
Consider two sentences: "The dog chased the cat" and "The cat chased the dog." The same basic words are present, but changing their positions changes the meaning.
A transformer therefore needs to represent not only which tokens are present but also how they are arranged and how positions relate to one another.
Step 4: Self-Attention
Self-attention is the defining mechanism of the Transformer architecture. It allows each token representation to incorporate information from other positions in the same sequence.
For example, in the sentence "The developer opened the file because it contained an error," the representation of "it" can benefit from information about other tokens in the sequence. Attention provides a mathematical mechanism for calculating which positions should contribute information.
Self-attention uses three learned projections commonly described as queries, keys, and values. These are derived from the input representations.
| Component | Intuition |
|---|---|
| Query | Represents what the current position is looking for. |
| Key | Represents information that can be matched against queries. |
| Value | Contains information that can be incorporated into the output. |
The Attention Calculation
In the standard scaled dot-product attention formulation, queries are compared with keys using a dot product. The scores are scaled and passed through softmax to produce attention weights. Those weights are then used to calculate a weighted combination of the value vectors.
Q = queries
K = keys
V = values
a = softmax(QKᵀ / √dₖ)
Attention(Q, K, V) = aVHere, dₖ represents the dimensionality of the key vectors. Scaling by the square root of the key dimension helps control the magnitude of the dot products before the softmax operation.
The result is a new representation in which each position contains information gathered from other positions according to the calculated attention weights.
Attention as Information Mixing
It is useful to think of attention as a learned information-mixing mechanism. Each position can determine which other positions are useful for its current representation.
Token A ───────┐
Token B ───────┤
Token C ───────┼──→ Attention → Updated token representations
Token D ───────┤
Token E ───────┘The attention weights are not manually programmed. They are produced by learned transformations and depend on the current input. This allows the same network to use different relationships for different sentences and contexts.
What Is Multi-Head Attention?
Instead of calculating a single attention operation, transformers commonly use multiple attention heads. Each head operates on a different learned projection of the representations.
This gives the model multiple opportunities to represent relationships within the same sequence. Different heads can learn different patterns, although their exact roles are not explicitly assigned by the model developer.
Input representations
↓
├── Head 1
├── Head 2
├── Head 3
└── ...
↓
Each head performs attention
↓
Concatenate outputs
↓
Linear projection
↓
Updated representationThe outputs of the attention heads are concatenated and passed through a learned linear projection. The result becomes the output of the multi-head attention sublayer.
Why Use Multiple Attention Heads?
A single attention mechanism has a limited representation space. Multiple heads allow the model to perform several attention calculations in parallel using different projections of the same underlying representations.
This can help the network represent different relationships between tokens. For example, different heads may become sensitive to syntactic relationships, nearby context, long-range dependencies, or other learned patterns.
Causal Attention
Decoder-only language models generally use causal or masked self-attention. When predicting the next token, a position cannot attend to tokens that occur later in the sequence.
Tokens:
The developer fixed the bug
When processing "developer":
The ✓
developer ✓
fixed ✗
the ✗
bug ✗A causal mask prevents information from future positions from leaking into the prediction. This makes the model's training objective consistent with autoregressive generation.
Bidirectional Attention
Encoder-only transformer models can use bidirectional self-attention, allowing a token representation to incorporate information from both earlier and later positions in the input.
This is useful for tasks where the entire input is available and the goal is to understand or represent it rather than generate it strictly from left to right.
| Attention type | Future tokens visible? | Typical use |
|---|---|---|
| Causal | No | Autoregressive generation |
| Bidirectional | Yes | Input understanding and representation |
| Cross-attention | Depends on architecture | Connecting one sequence or representation to another |
Step 5: Feed-Forward Networks
After attention has mixed information between positions, transformer layers apply feed-forward neural networks. A feed-forward network transforms each position independently using learned weights and nonlinear activation functions.
Input representation
↓
Linear transformation
↓
Nonlinear activation
↓
Linear transformation
↓
Output representationA simplified feed-forward network can be represented as a linear transformation followed by an activation function and another linear transformation. Modern architectures can use different activation functions and additional optimizations.
The important distinction is that attention mixes information across positions, while the feed-forward network applies a learned transformation to the resulting representation at each position.
Attention vs Feed-Forward Networks
| Component | Primary role |
|---|---|
| Attention | Mixes information between different positions. |
| Feed-forward network | Transforms information within each position. |
Step 6: Residual Connections
Transformer blocks use residual connections, also called skip connections. Instead of replacing the input with only the output of a sublayer, the input can be added back to the transformed result.
Input
│
├──────────────┐
↓ │
Sublayer │
↓ │
Transformed │
output │
│ │
└──── + input ─┘
↓
ResultResidual connections help information and gradients move through deep networks. Without appropriate pathways for information and gradients, optimization of very deep neural networks can become more difficult.
Step 7: Normalization
Normalization helps stabilize the activations flowing through the network. Transformer implementations commonly use Layer Normalization or related normalization techniques.
The exact placement of normalization differs between architectures. The original Transformer and many later models use different arrangements such as post-normalization or pre-normalization. Modern large language models frequently use a pre-normalization design.
What Is a Transformer Block?
A transformer block is a repeated unit containing the major operations used by the architecture. The exact structure varies, but a simplified decoder-style block can be represented as attention followed by a feed-forward network, with residual connections and normalization around those operations.
Input
↓
Normalization
↓
Causal self-attention
↓
Residual connection
↓
Normalization
↓
Feed-forward network
↓
Residual connection
↓
OutputA large language model stacks many transformer blocks. Each block transforms the representations produced by the previous block, allowing the network to build increasingly complex representations.
Why Stack Many Transformer Blocks?
A single transformer block can perform useful transformations, but deep networks can build progressively richer representations. Earlier layers may capture relatively basic patterns, while later layers can combine information into more abstract representations.
It is important not to interpret individual layers as having rigid, universal responsibilities. Neural networks distribute computation across many layers, and the behavior of one layer can depend heavily on the architecture and training process.
Encoder, Decoder, and Encoder-Decoder Transformers
The original Transformer consisted of an encoder stack and a decoder stack. This structure remains useful for understanding the architecture, even though many modern LLMs use only the decoder portion.
Encoder Architecture
An encoder transforms an input sequence into contextual representations. Encoder layers generally use self-attention that can access the full input sequence, followed by feed-forward processing.
Encoder-style transformers are useful for understanding tasks such as classification, similarity, and representation learning. Models designed primarily for embeddings or language understanding can use this general architecture.
Decoder Architecture
A decoder-only transformer processes a sequence using causal self-attention and is commonly used for autoregressive language generation. At each position, the model can use the current and previous tokens but not future tokens.
Many modern LLMs use decoder-only transformer architectures because they are well suited to next-token prediction and large-scale generative applications.
Encoder-Decoder Architecture
An encoder-decoder transformer contains both components. The encoder processes the source sequence, and the decoder generates the target sequence while using information from the encoder.
The decoder can therefore combine information from its own generated sequence with representations produced by the encoder. This is useful for sequence-to-sequence tasks such as translation and text transformation.
What Is Cross-Attention?
Cross-attention allows one sequence to attend to representations produced by another sequence. In the original encoder-decoder Transformer, the decoder uses cross-attention to incorporate information from the encoder output.
Encoder input
↓
Encoder stack
↓
Encoder representations
↓
Cross-attention
↑
Decoder sequence
↓
Decoder stack
↓
OutputCross-attention is conceptually different from self-attention. Self-attention relates positions within the same sequence, while cross-attention allows one representation stream to retrieve information from another.
How Does a Decoder-Only LLM Use a Transformer?
For a decoder-only LLM, the input prompt is tokenized and transformed into embeddings with positional information. The sequence then passes through a stack of decoder-style transformer blocks.
Prompt tokens
↓
Embeddings + position information
↓
Transformer block 1
↓
Transformer block 2
↓
Transformer block 3
↓
...
↓
Transformer block N
↓
Vocabulary projection
↓
Logits
↓
Next-token probabilitiesDuring generation, the selected token is added to the sequence and the model continues predicting subsequent tokens. This creates the autoregressive generation process used by many language models.
What Are Logits?
The final transformer representation is projected into a vector whose size corresponds to the model's vocabulary. The resulting values are called logits. Each logit represents an unnormalized score for a possible token.
Final hidden state
↓
Vocabulary projection
↓
Logits
↓
Token A: 3.8
Token B: 2.4
Token C: 0.9
Token D: -0.7
↓
Softmax
↓
ProbabilitiesA decoding algorithm then uses the resulting probability distribution to select the next token. Depending on the configuration, the system can use greedy decoding, sampling, temperature, top-k, top-p, or other strategies.
How Does a Transformer Learn?
The transformer architecture itself does not contain manually written rules describing language. Its parameters are learned through training. During pretraining of an autoregressive language model, the network repeatedly attempts to predict target tokens from their preceding context.
When predictions are incorrect, a loss function measures the error. Backpropagation computes gradients, and an optimizer updates the model's parameters. Repeating this process across huge amounts of training data gradually changes the parameters so the network becomes better at its training objective.
Training data
↓
Tokenization
↓
Transformer forward pass
↓
Next-token predictions
↓
Loss
↓
Backpropagation
↓
Gradient-based parameter update
↓
RepeatWhy Is Attention So Important?
Attention allows a transformer to model relationships between different positions without relying on a recurrent hidden state that must carry information sequentially through the entire sequence.
This is particularly useful when important information is separated by many tokens. The model can directly calculate relationships between positions rather than requiring information to pass through a long chain of recurrent steps.
Attention also works well with parallel computation during training. Since many token positions can be processed together, transformer training can take advantage of highly parallel hardware such as GPUs and other accelerators.
Transformers vs Recurrent Neural Networks
| Transformer | Recurrent neural network |
|---|---|
| Uses attention as a central mechanism | Uses recurrent state updates |
| Can process training positions in parallel with appropriate masking | Sequence processing is inherently sequential |
| Efficient for large-scale parallel training | More difficult to parallelize across sequence positions |
| Can directly model relationships between positions through attention | Information is passed through recurrent states |
| Foundation of many modern LLMs | Historically important for sequence modeling |
Transformers did not make recurrent networks mathematically useless. Instead, they provided a highly scalable architecture that proved particularly effective for large-scale sequence modeling.
Why Are Transformers Computationally Expensive?
The standard self-attention operation compares token representations across positions. As sequence length increases, the number of pairwise relationships can grow rapidly. This creates computational and memory challenges for very long contexts.
For a sequence of length n, the standard attention mechanism has a quadratic relationship with sequence length for its attention score matrix. This means that doubling the sequence length can increase the size of that matrix by roughly four times.
Sequence length: n
Attention score matrix:
n × n
If n doubles:
2n × 2n = 4n²This quadratic scaling is one of the reasons researchers have developed optimized attention implementations and alternative attention mechanisms for long-context workloads.
What Is the KV Cache?
During autoregressive inference, a decoder repeatedly generates new tokens. Previously computed key and value representations can be stored in a key-value cache, commonly called the KV cache.
The cache allows the system to reuse information from earlier tokens rather than recalculating all previous key and value representations at every generation step. This can substantially improve inference efficiency, although the cache consumes memory and grows with the amount of context.
What Is FlashAttention?
FlashAttention is an optimized approach to computing attention that focuses on reducing memory movement and improving hardware utilization. It does not change the fundamental mathematical attention operation; instead, it implements the computation more efficiently on modern accelerators.
Efficient attention implementations are important because attention can become a major computational and memory bottleneck as sequence lengths and model sizes increase.
What Is Rotary Positional Embedding?
Rotary Positional Embedding, commonly abbreviated as RoPE, is a positional representation technique used by many modern transformer architectures. It incorporates positional information by applying position-dependent rotations to parts of the query and key representations used by attention.
One useful property of this approach is that positional relationships can naturally influence attention calculations. RoPE is one of several approaches to representing position, and the exact implementation varies between models.
What Is a Transformer Parameter?
Transformer parameters are learned numerical values that determine how the network transforms its inputs. They include values in embedding layers, attention projections, feed-forward networks, normalization components, and output projections.
When a model is described as having billions of parameters, this refers to the total number of learned numerical values in the model. Parameter count is useful for describing model scale, but it is not a complete measure of quality or capability.
How Do Transformer Layers Build Representations?
The representation of a token changes as it passes through the transformer stack. Early processing begins with embeddings and positional information. Attention then allows information from other positions to influence the representation, while feed-forward layers apply nonlinear transformations.
Repeating these operations across many layers allows the network to construct increasingly sophisticated representations. The model does not need a manually defined rule saying which layer should represent a particular concept. These representations emerge from optimization during training.
Transformer Architecture in Modern LLMs
Modern LLMs are not identical copies of the original 2017 Transformer. Researchers and engineers have introduced many architectural changes and optimizations, including different normalization strategies, positional encoding methods, attention implementations, activation functions, parameter sharing approaches, and inference optimizations.
Some models also use techniques such as grouped-query attention or multi-query attention to reduce memory requirements during inference. Mixture-of-experts architectures can activate only a subset of parameters for a particular token, allowing the total model to be very large while controlling the amount of computation used for each input.
What Is Grouped-Query Attention?
Grouped-Query Attention, or GQA, is an attention architecture in which multiple query heads share key and value heads. It provides a compromise between standard multi-head attention and multi-query attention.
The main practical motivation is inference efficiency. Reducing the number of key and value heads can reduce the size of the KV cache and therefore lower memory requirements while retaining multiple query heads.
What Is Multi-Query Attention?
Multi-Query Attention, or MQA, uses multiple query heads but shares a single key and value head across them. This can substantially reduce the memory required for the KV cache during autoregressive generation.
The trade-off is that sharing keys and values changes the representation capacity of attention compared with standard multi-head attention. Grouped-query attention provides an intermediate design by using multiple groups of query heads that share key and value heads.
What Is Mixture of Experts?
Mixture-of-Experts, or MoE, architectures divide parts of a neural network into multiple expert networks and use a routing mechanism to select which experts process a particular token or input.
An MoE model can contain a very large total number of parameters while activating only a subset for each token. This can increase model capacity without requiring every parameter to be used for every computation, although it introduces additional complexity in training and serving.
Why Transformers Became Dominant
Transformers became widely adopted because they combine strong sequence modeling capabilities with highly parallelizable training. Their attention mechanism can represent relationships across a sequence, while modern hardware is well suited to the large matrix operations used by transformer networks.
Scaling the architecture with more data, parameters, and compute led to increasingly capable models. This helped transformers become the foundation for modern language models and influenced architectures used in vision, audio, multimodal AI, and other areas.
Transformers Beyond Text
Although transformers became famous through language models, the architecture is not limited to text. A transformer can process any data that can be represented as a sequence or set of tokens.
- Natural language
- Source code
- Images represented as patches or tokens
- Audio representations
- Video representations
- Multimodal inputs
- Time-series data
- Structured sequences
This flexibility comes from the general attention mechanism. The network does not fundamentally require its tokens to represent words. Tokens can represent many types of learned or engineered representations.
Transformer Architecture and Multimodal AI
Multimodal systems can use transformer-based components to combine information from different types of inputs. For example, image information can be converted into representations that a transformer processes alongside text tokens.
The exact design varies considerably between systems. Some architectures use separate encoders for different modalities and connect them through attention, while others convert multiple modalities into compatible token-like representations.
A Simplified Transformer Block
Input
↓
Normalization
↓
Self-Attention
↓
Residual Connection
↓
Normalization
↓
Feed-Forward Network
↓
Residual Connection
↓
OutputThis diagram is intentionally simplified. Real transformer implementations can contain additional operations, different normalization placement, gated feed-forward networks, specialized positional mechanisms, attention optimizations, and other architectural components.
The Complete Process in a Language Model
Putting the pieces together, a decoder-only language model can be understood as a pipeline that starts with text and ends with a probability distribution over possible next tokens.
User text
↓
Tokenizer
↓
Token IDs
↓
Token embeddings
↓
Positional information
↓
┌──────────────────────────┐
│ Transformer block │
│ │
│ Self-attention │
│ Residual connection │
│ Normalization │
│ Feed-forward network │
│ Residual connection │
└──────────────────────────┘
↓
Repeated many times
↓
Final representation
↓
Vocabulary projection
↓
Logits
↓
Probabilities
↓
Next token
↓
RepeatThis process explains how a relatively simple objective such as next-token prediction can be implemented using a very deep neural network containing many layers and billions of learned parameters.
Common Misconceptions About Transformers
Transformers are sometimes described as if attention simply "looks up" the correct answer. That is not how the architecture works. Attention performs learned numerical transformations and weighted combinations of representations.
Another misconception is that every attention head has a fixed human-readable role. Some heads can exhibit interpretable patterns, but neural networks distribute information across many components, and the behavior of individual heads can vary by model and task.
It is also incorrect to assume that a larger context window automatically means the model can perfectly remember and use every token in a long input. Context length, retrieval quality, attention behavior, and application design all influence how effectively information is used.
Why Transformer Architecture Matters for Developers
Developers building AI applications do not usually need to implement a transformer from scratch. However, understanding its architecture helps explain many practical behaviors of LLM APIs and local models.
- Token count affects input and output costs.
- Longer contexts can increase latency and memory requirements.
- Generation is autoregressive in decoder-only language models.
- Attention allows the model to use information from different parts of the context.
- The KV cache affects memory usage during generation.
- Different model architectures can have very different performance characteristics.
- Model size does not alone determine quality.
- Prompt structure influences the information available to the transformer.
- External retrieval and tools can provide information outside the model's learned parameters.
Transformer Architecture vs LLM
A Transformer is an architecture, while an LLM is a trained model. The architecture defines the general computational structure, whereas the trained model contains specific learned parameters.
| Transformer | LLM |
|---|---|
| Neural network architecture | A trained language model |
| Defines computational components | Contains learned parameter values |
| Can be used for many data types | Primarily designed around language in the traditional LLM sense |
| Can be encoder, decoder, or encoder-decoder | Often uses a decoder-only architecture for generation |
| Describes how computation is organized | Represents a specific trained system |
Key Concepts to Remember
- Transformers are neural network architectures based heavily on attention.
- Input text is converted into tokens before being processed.
- Token IDs are mapped to learned embeddings.
- Positional mechanisms provide information about token order.
- Self-attention allows positions to exchange information.
- Multi-head attention performs several attention operations using different learned projections.
- Causal attention prevents autoregressive models from using future tokens.
- Feed-forward networks apply nonlinear transformations to token representations.
- Residual connections help information and gradients flow through deep networks.
- Normalization helps stabilize neural network computation.
- Multiple transformer blocks are stacked to create deep models.
- Decoder-only transformers are widely used for autoregressive LLMs.
- Cross-attention connects one representation stream with another.
- KV caching improves autoregressive inference efficiency.
- Modern transformers include many architectural optimizations beyond the original 2017 design.
Frequently Asked Questions
What is a Transformer in AI?
A Transformer is a neural network architecture that uses attention mechanisms to process relationships between positions in a sequence. It is the foundation of many modern language models and is also used for vision, audio, multimodal AI, and other tasks.
What is the main idea behind the Transformer architecture?
The main idea is to use attention to allow different positions in a sequence to exchange information. This provides an efficient way to model relationships across a sequence and enables highly parallel training.
What is the difference between a Transformer and an LLM?
A Transformer is an architecture, while an LLM is a trained model. Many LLMs use transformer architectures, but transformers can also be used for tasks that are not language generation.
Why is attention important in Transformers?
Attention allows each position to incorporate information from other relevant positions. This helps the model represent relationships between tokens and is particularly useful for handling long-range dependencies.
Why do modern LLMs use decoder-only Transformers?
Decoder-only Transformers are well suited to autoregressive next-token prediction. They can process a prompt and generate a response by repeatedly predicting the next token while using causal attention to prevent access to future tokens.
Helpful AI Tools
AI and developer tools can help you experiment with language models, inspect tokenization, analyze prompts, work with structured outputs, estimate token usage, and understand how changes in context affect model behavior. These tools are useful when moving from the theory of transformers to practical LLM development.
Conclusion
The Transformer architecture provides the foundation for many of today's most capable AI systems. Its central idea is attention: instead of relying primarily on recurrent processing, the network can dynamically relate different positions in a sequence and combine information from them.
A typical transformer processes token embeddings together with positional information through repeated blocks containing attention, feed-forward networks, residual connections, and normalization. In decoder-only language models, causal attention and a vocabulary projection allow these representations to be used for autoregressive next-token prediction.
The original Transformer was only the beginning. Modern architectures have introduced techniques such as rotary positional embeddings, grouped-query attention, optimized attention implementations, KV caching, and mixture-of-experts designs. Nevertheless, the fundamental concepts of attention and deep representation transformation remain at the center of the architecture.
Understanding these concepts makes it much easier to understand how LLMs work, why context length and tokenization matter, how inference is optimized, and how modern AI applications are built around transformer-based models.