AI Fundamentals

Transformers Guide

Engineering guide to the transformer architecture: self-attention, encoder/decoder/decoder-only stacks, positional encodings, scaling laws, and production serving implications.

55 min readIntermediateLast reviewed: 21 July 2026

Quick Summary

A transformer is a neural sequence model that mixes information across tokens with parallel self-attention instead of recurrence.

One Analogy

Self-attention is a meeting where every token can read every other token's notes in one round — not a phone chain that relays messages step by step.

Engineering Rule

Treat context length, KV-cache memory, and decode latency as first-class architecture constraints, not serving afterthoughts.

TL;DR

  • A transformer processes sequences with self-attention — every position can attend to every other position in parallel. That design replaced RNNs for large-scale language modeling.
  • Modern chat LLMs are decoder-only transformers (Llama, Mistral, GPT-style APIs): causal attention plus stacked blocks that predict the next token.
  • Each block is multi-head attention + a feed-forward network, with residuals and normalization. Most parameters live in the FFN, not in the attention score computation itself.
  • Positional encodings (today usually RoPE) inject order because attention alone treats inputs as a set.
  • Production cost is dominated by prefill vs decode and KV cache: attention is (O(N^2)) in sequence length; serving systems batch, cache keys/values, and parallelize across GPUs.
  • You rarely implement a transformer from scratch — you choose models, context limits, quantization, and serving stacks that expose those architectural constraints.

Quick Decision Guide

If you want to... Read
Understand transformers Transformers
Learn the attention math Attention Mechanism
See how LLMs use this Large Language Models
Manage context cost Context Windows
Adapt a model cheaply PEFT · LoRA
Train with limited VRAM QLoRA

Who this guide is for

  • Best for: AI engineers · ML engineers · backend engineers · architects
  • Difficulty: Intermediate
  • Estimated time: 55 min

Learning Path

EmbeddingsAttention MechanismTransformersLarge Language ModelsContext WindowsPrompt EngineeringFine-tuning

On this page

Why This Matters

Every production LLM you call — whether a hosted API or an open checkpoint such as Llama 3.x / Llama 4-class models or Mistral — is a transformer (or a close variant such as Mixture-of-Experts transformers). Embedding models, rerankers, and many vision backbones use the same family. The architecture determines hard product limits: context windows, time-to-first-token, tokens-per-second, GPU memory for the KV cache, and why fine-tuning changes behavior without changing the block layout.

Understanding transformers is not about reimplementing multi-head attention. It is about making correct engineering decisions: why longer prompts cost more, why generation latency scales with output length, why "lost in the middle" appears, why KV cache sizing dominates serving capacity, and why RAG and tools exist when parametric memory is insufficient. See large language models for the training/inference product view and attention mechanism for the math of queries, keys, and values.

Engineering Insight

Longer context windows increase cost and latency — attention is (O(N^2)) and the KV cache grows with sequence length. More context is not automatically better.

The Problem Transformers Solve

Before 2017, sequence modeling at scale leaned on recurrent networks (RNNs, LSTMs, GRUs). Those designs process tokens in order. Three structural problems blocked industrial language modeling:

  1. Sequential training. A 512-token sequence requires 512 dependent steps. GPU throughput collapses because most cores wait.
  2. Long-range degradation. Signals from early tokens dilute through many recurrent updates. Distant dependencies are hard to learn.
  3. Fixed-state bottleneck. An LSTM compresses history into a fixed hidden vector. Detail is lost as sequences grow.

Self-attention removes the sequential dependency for training: every token can form a direct connection to every other token in one parallel matmul-heavy step. Depth comes from stacking blocks, not from unrolling time. That change unlocked the scaling regime that produced modern LLMs.

Constraint RNN / LSTM Transformer
Parallelism over sequence Poor Excellent (training / prefill)
Path length for long-range links Linear in distance Constant (one attention hop)
Dominant memory at long context Hidden state size Attention + KV cache (~(N^2) / (N \times L \times d))
Generation Still sequential Still sequential (decode), but trained in parallel

How We Got Here

Diagram: From recurrence to modern decoder-only LLMs

timeline
    title Sequence modeling → production LLMs
    2014-2016 : Seq2seq RNNs + attention
              : Attention as an add-on
    2017 : Attention Is All You Need
         : Full transformer encoder-decoder
    2018-2019 : BERT vs GPT split
              : Bidirectional vs causal
    2020-2022 : Scale + decoder-only wins
              : GPT-3, scaling laws
    2023-2026 : Open families + long context
              : Llama, Mistral, RoPE, MoE, KV serving

The industry converged on decoder-only transformers for generation; encoders remain common for embeddings and classification.

Era Dominant idea Production consequence
RNN + soft attention Attention helps the decoder look at encoder states Still sequential training limits
Original transformer Drop recurrence; stack attention + FFN Parallel training at scale
BERT / GPT split Bidirectional MLM vs causal LM Two product lines: understand vs generate
LLM era Decoder-only + huge data/compute Chat APIs, open weights, PEFT adaptation
Serving era KV cache, continuous batching, FlashAttention Throughput and memory engineering

Kaplan / Chinchilla-style scaling laws (high level): for a fixed compute budget, model size and data volume trade off; under-training a huge model wastes parameters. Practitioners treat this as planning guidance — pick a size you can train or license, then invest in data quality, evaluation, and serving — not as a guarantee that "bigger always wins" on your task.

What Is a Transformer?

A transformer is a neural architecture introduced by Vaswani et al. (2017) in Attention Is All You Need. It builds representations by alternating:

  • Multi-head self-attention — mixes information across positions
  • Position-wise feed-forward networks — transforms each position independently
  • Residual connections and normalization — stabilize deep stacks

The original paper used an encoder–decoder for machine translation. Three families matter in production:

Family Attention pattern Typical use
Encoder-only Bidirectional Embeddings, classification (BERT-style)
Decoder-only Causal (left-to-right) Chat / code / agents (Llama, Mistral, GPT-style)
Encoder–decoder Encode bidirectionally; decode with cross-attention Translation, some summarization (T5/BART lineage)

As an application engineer shipping generation, you almost always consume decoder-only models. Encoder-only stacks still power many embedding pipelines.

How Transformers Work

Inside a block

Each block roughly does:

  1. Normalize (pre-norm in most modern LLMs)
  2. Multi-head self-attention with residuals
  3. Normalize
  4. Feed-forward (often SwiGLU-style MLP in Llama/Mistral families)
  5. Residual add

Multi-head self-attention projects hidden states into queries, keys, and values. Attention weights decide how much each position reads from others. Multiple heads learn different relationship patterns in parallel. Full math lives in attention mechanism.

Feed-forward layers hold most parameters (~two-thirds in many GPT-style layouts). Capacity for "knowledge" and non-linear transforms concentrates here; attention routes information.

Residuals (x + sublayer(x)) keep gradients usable across dozens or hundreds of layers.

Autoregressive generation

Decoder-only models generate one token at a time: sample (yt) from (p(y_t \mid y{<t}, x)), append it, repeat. Prefill processes the prompt in parallel; decode is inherently serial per sequence (though batching and speculative decoding mitigate wall-clock cost).

Autoregressive next-token generation loop

Source: Jay Alammar — Illustrated GPT-2

Positional encodings

Without positions, attention is permutation-invariant. Early transformers added sinusoidal or learned absolute embeddings. Most modern open LLMs use RoPE (Rotary Position Embeddings): rotate query/key pairs by an angle that depends on position so relative distance is encoded geometrically. RoPE also underpins many long-context extension schemes (frequency scaling / NTK-aware adjustments) used when models move from 4K–8K training contexts toward 128K+ serving contexts — with quality caveats that require evaluation, not slogans.

# Conceptual RoPE sketch (not a drop-in kernel)
import torch

def apply_rope_pair(x_even: torch.Tensor, x_odd: torch.Tensor, position: int, theta: float = 10000.0):
    dim = x_even.shape[-1]
    freqs = 1.0 / (theta ** (torch.arange(0, dim).float() / dim))
    angles = position * freqs
    cos, sin = angles.cos(), angles.sin()
    return x_even * cos - x_odd * sin, x_even * sin + x_odd * cos

Architecture

Diagram: Decoder-only stack used by modern LLMs

flowchart TB
  subgraph input [Input]
    T[Tokens]
    E[Embedding + RoPE]
  end
  subgraph block [Repeated N times]
    A[Causal multi-head attention]
    F[Feed-forward / MLP]
  end
  subgraph out [Output]
    L[LM head → logits]
    S[Sample next token]
  end
  T --> E --> A --> F
  F -->|stack| A
  F --> L --> S
  S -->|append| T

Causal attention prevents attending to future tokens; the LM head maps the final hidden state to vocabulary logits.

Variant comparison

Variant Examples (illustrative) Strength
Dense decoder-only Llama 3.x / 4-class dense, Mistral dense Predictable latency, broad tooling
MoE decoder-only Mixtral-style, large MoE APIs More capacity per active FLOP; routing complexity
Encoder-only BERT-lineage embedders Bidirectional context for retrieval features
Encoder–decoder T5-lineage Explicit seq2seq; less common for chat

Scale intuition (order-of-magnitude)

Class Layers (typical) Hidden size (typical) Role
~7–8B open instruct ~32 ~4096 Local / cheap serving, strong with PEFT
~70B open instruct ~80 ~8192 Higher quality; heavier KV + weights
Frontier APIs Proprietary Proprietary Capability ceiling; black-box serving

Exact layer counts change by release. Treat vendor cards and Hugging Face model configs as source of truth for a checkpoint.

Serving-shaped architecture

At inference time the "architecture" you operate includes:

  • Weights — static parameters (possibly quantized)
  • Activations — transient per forward
  • KV cache — per-layer keys/values for tokens already seen (grows with context × layers × heads × dim)
  • Scheduler — continuous batching, paged attention (vLLM), tensor/pipeline parallel

Step-by-Step Flow

Diagram: Prefill vs decode for one request

sequenceDiagram
  participant C as Client
  participant S as Inference server
  participant G as GPU kernels
  C->>S: Prompt tokens
  S->>G: Prefill (parallel over prompt)
  G-->>S: Logits + initial KV cache
  loop Until stop / max tokens
    S->>G: Decode 1 token (use KV cache)
    G-->>S: Next token + append KV
    S-->>C: Stream token (optional)
  end

Prefill is compute-heavy and parallelizable; decode is memory-bandwidth heavy and serial per sequence.

  1. Tokenize text with the model tokenizer (tokens).
  2. Embed token IDs; apply RoPE (or equivalent) inside attention.
  3. Prefill the prompt through all blocks; materialize KV cache.
  4. Project final hidden state through the LM head → logits.
  5. Decode with temperature / top-p / constraints; append token.
  6. Repeat decode until EOS, stop string, or max length.
  7. Detokenize for the client; retain KV only as long as the session policy allows.

Real Production Example

You configure serving, not hand-written attention. Example: vLLM-style deployment for an open instruct model (Llama 3.1 8B Instruct shown; swap IDs for your approved checkpoint):

from vllm import LLM, SamplingParams

llm = LLM(
    model="meta-llama/Llama-3.1-8B-Instruct",
    tensor_parallel_size=1,
    max_model_len=8192,
    gpu_memory_utilization=0.90,
    dtype="bfloat16",
    enforce_eager=False,
)

sampling = SamplingParams(
    temperature=0.2,
    max_tokens=1024,
    top_p=0.95,
)

outputs = llm.generate(
    ["Explain causal attention vs bidirectional attention in two paragraphs."],
    sampling,
)
print(outputs[0].outputs[0].text)

Map knobs to architecture:

Knob Architectural meaning
max_model_len Positional / trained context ceiling + memory budget
gpu_memory_utilization Room left for KV cache and batching
tensor_parallel_size Split large matrices across GPUs
dtype / quant Weight precision vs quality / VRAM
Continuous batching Hide decode latency across users

For adaptation rather than serving, the same architecture is frozen or lightly updated via PEFT / LoRA / QLoRA without redesigning blocks.

Design Decisions

Decision Options Guidance
Family Decoder-only vs encoder–decoder vs encoder-only Generation → decoder-only; embeddings → encoder-only
Size 8B vs 70B+ vs API frontier Fit latency/cost first; measure task quality
Density Dense vs MoE MoE for capacity/cost; watch tail latency and routing
Context 8K vs 128K+ Longer ≠ always better; retrieval often beats stuffing
Precision BF16 vs INT8/INT4 Quantize after task eval; separate train vs serve precision
Serving API vs vLLM/TGI/TensorRT-LLM Privacy, steady load, and customization drive self-host
Adaptation Prompt / RAG / fine-tuning Architecture stays fixed; change context or adapters

Common patterns

  • Chat template + stop tokens aligned with training
  • RAG to extend effective knowledge without growing weights
  • Speculative decoding / Medusa-style heads for decode speedups (engine-dependent)
  • Prefix caching when many requests share a system prompt

Comparisons

Dimension RNN/LSTM Transformer (decoder-only LLM)
Train parallelism Weak Strong
Long-range modeling Hard Direct attention paths
Inference generation Sequential Sequential decode + KV cache
Ecosystem (2026) Niche / legacy Default for LLMs
Dimension Encoder-only Decoder-only Encoder–decoder
Mask Bidirectional Causal Mixed + cross-attn
Best fit Embeddings, NLU Chat, agents, code Classic seq2seq
Chat fine-tunes Rare Standard (PEFT) Less common
Serving concern Why transformers force it
KV cache RAM Stores per-token K/V across layers
(O(N^2)) attention Long context taxes compute/memory
Batching Needed to utilize GPUs during decode
FlashAttention-class kernels Avoid materializing full attention matrices

Common Mistakes

  1. Confusing the architecture with the Hugging Face transformers library. The library loads models; the architecture is the network design.
  2. Assuming chat needs an encoder–decoder. Production chat is almost always decoder-only.
  3. Ignoring KV cache when sizing GPUs. Long context × concurrency OOMs even when weights fit.
  4. Expecting bidirectional "understanding" from causal models. Future tokens are invisible during generation; prompt order matters.
  5. Equating prefill cost with decode cost. One long answer is many full-stack forwards.
  6. Treating context extension as free. RoPE scaling can degrade retrieval/reasoning — evaluate.
  7. Blaming "the transformer" for factual errors. Fluency ≠ truth; see hallucinations and grounding.
  8. Changing chat templates between train and serve after fine-tuning adapters.

Where It Breaks Down

Quadratic attention. Doubling sequence length roughly quadruples dense attention cost. Streaming, retrieval, hierarchical memory, and sparse/linear attention variants exist, but most production LLMs still pay a steep long-context bill.

Statelessness. The network has no durable memory outside the context you pass (and any external store). Multi-turn products must resend or cache history deliberately.

Error compounding. Teacher-forced training differs from autoregressive freerunning; mistakes in early tokens pollute later attention.

Capability ceiling. Adapters and prompts cannot invent abilities absent from the base model’s scale and training mix. Pick a stronger base before heroic fine-tunes.

Multimodal and MoE complexity. Shared transformer ideas remain, but routing, vision encoders, and tool loops add failure modes outside the vanilla stack.

When NOT to Obsess Over Transformer Internals

Skip deep architecture work when:

  • you only call a hosted API and your bottleneck is evaluation, prompts, or retrieval;
  • a smaller specialized classifier or rules engine meets the contract;
  • the problem is factual freshness — use tools/RAG, not a different attention variant;
  • you lack measurement — architecture debates without golden sets waste time.

You still need enough transformer literacy to set context limits, interpret latency profiles, and review serving configs. You do not need to re-derive attention softmax to ship.

Running in Production

Important

Capacity planning is usually KV-cache and decode bandwidth, not "can the weights load."

Dimension Practice
Latency Track TTFT (prefill) and inter-token latency (decode) separately
Throughput Continuous batching; watch queue depth and preemptible long jobs
Memory Model weights + KV × concurrent sequences × context
Reliability Hard caps on max_tokens and context; backpressure
Quality Task evals beat perplexity; slice by length and language
Security Prompt injection exploits the lack of instruction/data separation in attention — defend in the app
Adaptation Version base revision + tokenizer + optional LoRA adapter as one artifact

Batching and the KV cache in practice

Continuous batching packs decode steps from many users into the same GPU kernels so expensive matrix multiplies stay saturated while each sequence still advances one token at a time. Paged attention (as popularized by vLLM) allocates KV cache in blocks so fragmentation does not strand memory when requests finish at different lengths. Operators should alert on KV fragmentation, preempted long generations, and cache hit rate for shared system prefixes.

Tensor parallelism shards large matmuls across GPUs for models that do not fit on one device; pipeline parallelism stages layers across devices at the cost of bubble time. Choose based on model size and interconnect — do not enable both “because more parallelism sounds faster” without a benchmark on your traffic mix.

Quantization (INT8/INT4/FP8) reduces weight memory and can raise concurrency, but it changes numerics. Treat quantized weights as a new artifact: rerun task and safety evals. Training-time schemes such as QLoRA are not the same decision as inference quantization after a merge.

What application engineers should log

For each request, retain model ID/revision, tokenizer revision, max context used, prompt token count, completion token count, TTFT, and whether an adapter was active. Those fields turn “the model got worse” into a bisectable incident. Architectural literacy without telemetry still leaves you debugging anecdotes.

Diagram: Decision tree for using transformer knowledge in a product

flowchart TD
  A[Need generation quality?] -->|No| B[Classifier / embeddings / rules]
  A -->|Yes| C[Hosted API OK?]
  C -->|Yes| D[Prompt + eval + RAG/tools]
  C -->|No| E[Self-host open LLM]
  E --> F{VRAM tight?}
  F -->|Yes| G[Quantize + smaller model]
  F -->|No| H[BF16/FP8 serve]
  D --> I{Stable behavior gap?}
  I -->|Yes| J[Fine-tune via PEFT]
  I -->|No| K[Keep prompt/RAG]

Architecture literacy informs serving and adaptation; it does not replace evaluation.

Continue Learning

Production Checklist

  • Context window selected and enforced with hard caps
  • KV cache sized for peak concurrent sequences × context
  • Batching strategy tested (continuous batching / paged attention)
  • Throughput and TTFT / inter-token latency benchmarked
  • Quantization evaluated as a separate artifact (task + safety evals)
  • Model revision and tokenizer revision logged per request
  • max_tokens and context backpressure configured
  • Tensor / pipeline parallelism choice validated on traffic mix
  • Adapter (if any) versioned with base as one artifact
  • Queue depth and KV fragmentation alerts configured
  • Rollback path for model / quant / serving config documented

Prerequisites

Core Concepts

Implementation

Optimization

Advanced Topics

  • RLHF · DPO — preference training on the same stacks

Diagram: Learning path around transformers

flowchart LR
    T[Tokens] --> LLM[LLMs]
    LLM --> TR[Transformers]
    TR --> AT[Attention]
    TR --> CW[Context windows]
    LLM --> FT[Fine-tuning]
    FT --> PF[PEFT]
    PF --> LR[LoRA]
    LR --> QL[QLoRA]

Read architecture first, then adaptation methods that freeze most transformer weights.

Interview Questions

Why did transformers replace RNNs for large-scale LM?

Parallel training over the sequence, direct long-range paths via attention, and better hardware utilization. Generation remains sequential, but training throughput unlocked scale.

What is the difference between BERT-style and GPT-style transformers?

BERT-style encoders use bidirectional masks for understanding/embeddings. GPT-style decoders use causal masks for next-token generation. Most chat LLMs are decoder-only.

Where do most parameters sit in a decoder-only LLM?

Typically in the feed-forward / MLP matrices, not in the attention score function itself. Attention projections still matter and are common LoRA targets.

What is the KV cache and why does it matter?

Cached key/value tensors from prior tokens so decode does not recompute them. Memory scales with layers × heads × dimension × sequence length × batch, often dominating serving VRAM.

How does RoPE differ from absolute positional embeddings?

RoPE encodes position by rotating Q/K, emphasizing relative offsets and enabling many long-context extension methods. Absolute embeddings add a position vector to each token embedding.

Prefill vs decode — which dominates latency?

Short answers: often prefill (TTFT). Long answers: decode dominates wall clock because each token is a serial step. Optimize them differently.

Do scaling laws mean you should always train the biggest model?

No. They describe compute–data–size trade-offs. Under-trained large models waste budget; for products, evaluate quality per dollar and per millisecond on your task.

Key Takeaways

  • Transformers mix tokens with parallel self-attention and per-position MLPs; they are the substrate of modern LLMs.
  • Decoder-only causal stacks dominate generation; encoders remain central for embeddings.
  • Positional schemes (RoPE) and KV caching are as operationally important as "knowing attention exists."
  • Production pain is memory, batching, and long context — not implementing softmax.
  • Adaptation (PEFT, LoRA, QLoRA) changes weights or adapters, not the block diagram.

FAQs

Do I need to implement a transformer to use LLMs?

No. You need enough literacy to reason about context, latency, memory, and adaptation. Libraries and engines implement the kernels.

Why is generation slower than embedding a document?

Embeddings are typically one forward pass (encoder). Autoregressive generation runs a forward per output token (decode), plus growing KV state.

What is FlashAttention?

A family of IO-aware attention kernels that avoid materializing full (N \times N) matrices in HBM, cutting memory and often speeding training/inference.

How do MoE transformers change the picture?

Experts activate sparsely so total parameters can exceed dense compute per token. Serving must handle expert placement and load balance; latency variance can increase.

Can I change layer count at inference?

No. Depth and width are fixed by the checkpoint. Choose another model or distill/quantize; do not expect runtime architecture morphing.

How do vision transformers relate?

Same attention + FFN pattern on patch tokens. Multimodal systems couple vision encoders with text decoders; product failures often sit at the interface, not in "attention itself."

Why do open models still matter if APIs are stronger?

Privacy, cost at volume, customization via fine-tuning, and offline control. Architecture knowledge transfers across both.

References

Further Reading

Next Topics

Learning Path

Continue Learning

Related Guides

Related companies

  • Hugging Face

    Open-source AI collaboration platform for models, datasets, and tooling.

Related Tools

ToolCategoryPurposeWebsiteBest For
Hugging Face Transformers
Python SDK
frameworksLibrary for using pretrained transformers in Python and beyond.huggingface.coModel fine-tuning