Encoder, decoder and hybrid model architectures
easy 6 minModel architecture determines which positions can interact and how information survives generation.
Video lesson · Chapter 1 of 4
The repair manual before the question
Theory
Read or listen first. About 2 min.
What it is
Model architecture determines which positions can interact and how information survives generation. A bidirectional encoder can read both sides of an observed token and output a representation for classification or retrieval. A causal decoder predicts the next token using only its available prefix. An encoder–decoder processes a source once and uses a separate decoder with cross-attention to generate a target sequence.
Why it exists
Select the architecture by the interface the task needs. Search may demand millions of precomputable document vectors, while summarization must generate an output of variable length. At long contexts, compare attention memory and compute, recurrent state, and hybrid designs empirically. An expert router changes which network weights are active per token; it does not by itself change every attention or caching cost.
Choose a backbone for search and summarization
Inside this chapter
Chapter sources- Encoder and encoder–decoder
- A dual-encoder system precomputes document vectors and encodes queries independently. An encoder–decoder instead keeps source representations available to an autoregressive decoder. Both can use encoders while exposing very different serving contracts.
In a dual encoder the document side and the query side never meet inside the network; they only meet at the similarity score. That is what lets you encode millions of documents once and reuse them. An encoder-decoder does the opposite: the decoder reads the encoded source at every output step through cross-attention, so it can produce a new sequence of any length but cannot precompute answers.
Example. A search service scores a new query against a stored catalog. A translation service must emit a new target sequence conditioned on a particular source.
Another example. A legal search tool embeds 2 million clauses overnight and answers each query with one short encoder pass plus a vector lookup. A contract translation service must run its decoder once per output token for every document.
Two serving contracts built on encoders
Top: a dual encoder for search. Bottom: an encoder-decoder for translation. - Latent attention and recurrent state
- Multi-head latent attention compresses information retained for keys and values using learned low-dimensional representations. State-space models maintain recurrent state across positions. Hybrid architectures combine mechanisms. Costs depend on implementation, state dimensions and sequence lengths.
Standard attention keeps every past key and value, so memory grows linearly with context and every new token reads all of it. Latent attention stores a smaller learned summary per position and reconstructs what attention needs. A state-space model folds the past into a fixed-size state, which is cheap at any length but must decide what to forget. Hybrids interleave both kinds of layer to trade memory against recall.
Example. At the same input length, compare cache bytes, retrieval of a distant fact and sustained token latency before choosing a smaller-state design.
What each design keeps between tokens
Design Stored per sequence Grows with length? Watch for Standard multi-head attention keys and values for every position and head yes cache memory at long contexts Multi-head latent attention a compressed latent per position yes, more slowly extra compute to expand latents State-space model a fixed-size recurrent state no recall of a distant exact fact Hybrid a mix of both partly depends on the layer mix Measure on your own lengths: implementation details decide the real costs. - Expert routing and load
- An MoE layer selects a subset of experts per token. Total resident parameters and active computation differ. If expert placement spans devices, token dispatch, load imbalance and communication can dominate a sparse layer’s speed.
A mixture-of-experts layer replaces one large feed-forward block with many smaller experts and a router that sends each token to a few of them. Compute per token falls, but all experts must stay in memory. When experts live on different devices, tokens must be shipped across and results gathered back, and the layer finishes only when the busiest device does. Training usually adds a balancing loss, but traffic at serving time can still be skewed.
Example. A popular expert receives half the tokens while other devices wait. The active parameter count alone does not predict wall time.
Another example. A support model's routing sends most billing-related tokens to one expert. On a billing-heavy day that device becomes the bottleneck while the others idle.
One hot expert holds everyone up
Devices 2 and 3 finish early and idle while device 1 works through its queue. - Latent attention and expert imbalance
- A latent KV design caches a compressed representation from which the model computes attention. An MoE router chooses experts per token, which creates a different bottleneck when many tokens select the same expert. Measure cache bytes and expert dispatch imbalance independently.
These two techniques solve different problems and fail in different ways. Latent KV attacks memory: you see its benefit as more concurrent sequences or longer contexts on the same hardware. Expert routing attacks compute per token: you see its weakness as idle devices waiting at a collective step. Profile them separately so a memory win does not hide a throughput loss.
Example. Two variants fit the same context in memory. The expert-routed one can still slow down if a majority of tokens land on one device and other workers wait at the collective.
Two bottlenecks, two measurements
Cache bytes
Latent KV shrinks memory per cached position. Measure bytes at your context length.
Dispatch imbalance
Routing can pile tokens onto one expert. Measure each device's share of tokens.
Fits is not fast
Two variants can fit the same context and still differ in wall time.
- Causal decoder-only models
- A decoder-only model uses one stack of masked self-attention layers for both the prompt and the output, predicting each token from everything before it. The prompt is processed in one parallel pass and its keys and values are cached; output tokens are then generated one at a time.
Because prompt and answer share one sequence, a decoder-only model is simple to train on raw text and to prompt for many tasks. The trade-off is that a long source document shares the context window with the output, whereas an encoder-decoder keeps the encoded source separate. In practice the choice is made by measuring quality and latency on the task.
Example. A chat assistant receives instructions and a document as one prefix and continues it with the answer, with no separate encoder.
The causal mask
Added to attention scores before softmax, it gives every later position a weight of zero.
How it works
Step through the idea, or press Play.
Document encoder. Index a catalog ahead of time
Understand it
Worked example
Choose a backbone for search and summarization
- 1
Index a catalog ahead of time
Bidirectional source representations yield vectors that can be reused across many queries.
- 2
Retrieve the relevant manual
Encode the query in the aligned representation space and select a bounded set of permitted documents.
- 3
Encode the selected manual
An encoder–decoder can compute source states once and expose them to its decoder’s cross-attention.
- 4
Generate a variable-length summary
Decode each output token conditioned on the source and prior output. Compare against a decoder-only alternative on actual latency and quality.
Follow the handoffs
When each part of the pipeline runs
| Component | Runs | Reused across queries? | Cost grows with |
|---|---|---|---|
| Document encoder | offline, when the catalogue changes | yes | catalogue size |
| Query encoder | once per query | no | query count |
| Source encoder | once per selected manual | within one summary | manual length |
| Causal decoder | once per output token | no | summary length |
Pick the backbone from the interface
Summary
The whole topic in one minute.
Document embeddings are independent of the future query in this dual-encoder design. The generated summary depends on the selected source and request.
Select the architecture by the interface the task needs. Search may demand millions of precomputable document vectors, while summarization must generate an output of variable length. At long contexts, compare attention memory and compute, recurrent state, and hybrid designs empirically. An expert router changes which network weights are active per token; it does not by itself change every attention or caching cost.
Sources
- BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
- Exploring Transfer Learning with T5: the Text-To-Text Transfer Transformer
- Transformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality
- DeepSeek-V3 Technical Report
- Parallelism Strategies Guide — Megatron Core