Efficiency Frontiers › Efficient Architectures
Language AI HandbookAll chapters Efficiency Frontiers › Efficient ArchitecturesOne section · Sections in this chapter2 / 10 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The original transformer architecture was designed for correctness and flexibility, not for computational efficiency. Its self-attention mechanism computes relationships between every pair of tokens, which scales quadratically with sequence length. For a 1,000-token sequence, this means one million attention computations per layer. For a 100,000-token sequence, it means ten billion. The architecture also allocates compute uniformly across all positions and all layers, even when most positions are not informative and most layers are doing similar work.
Efficient architecture research attacks these inefficiencies from several directions.
Sparse and Linear Attention
Standard attention computes a full attention matrix for a sequence of tokens. The quadratic cost arises because every query token attends to every key token:
where , , are the query, key, and value matrices, and is the head dimension. This is in time and in memory just to store the attention matrix.
Sparse attention replaces the full matrix with a structured pattern. Instead of every token attending to every other token, each token attends to a limited neighborhood. The two most influential patterns are:
- Local window attention: each token attends only to the tokens to its left and right. This reduces complexity to , which is linear in sequence length for fixed window size.
- Strided attention: tokens attend locally plus to tokens at fixed stride intervals, allowing global information to propagate over multiple layers.
Longformer and BigBird combine local attention for most tokens with global attention for special tokens like [CLS]. The global tokens can see all positions, giving the model a way to aggregate information across the entire sequence without paying the full cost everywhere.
Linear attention takes a different approach. Recall that the softmax in standard attention creates a non-linear function that cannot be factored:
If we replace softmax with a kernel function such that approximates the original attention score, we can rewrite attention as:
where:
- is the feature map applied to the query vector
- is the feature map applied to the -th key
- is the -th value row
The key insight is that can be computed once and reused for every query, reducing the total cost to , which is linear in sequence length. Performers and Linear Transformers use this idea with random Fourier features as the kernel approximation.
The tradeoff is that linear attention loses some expressiveness. The softmax non-linearity helps attention sharply focus on a few relevant tokens, while kernel approximations tend to produce softer distributions. In practice, linear attention works well on tasks that do not require sharp token selection, but lags on tasks requiring precise retrieval.
Mixture-of-Experts
A standard transformer layer applies the same feed-forward network to every input token. This means the model uses the same parameters regardless of whether it is processing a Python variable name or a French sentence. Mixture-of-Experts (MoE) architectures replace the single feed-forward network with many expert sub-networks and route each token to only a few of them.
A mixture-of-experts layer consists of expert networks and a gating network. For each token, the gating network scores all experts and activates the top- (typically or ). Only the activated experts process the token, so the computation per token stays constant even as the total number of parameters grows.
The gating mechanism uses a learned linear transformation followed by softmax:
where:
- is the token embedding
- is the gating weight matrix
- keeps only the top- logits and sets the rest to
- is optional noise for load balancing during training
The output of the MoE layer is:
The efficiency gain is striking. A model with 8 experts and top-2 routing has the same computational cost per token as a model with one feed-forward network, but four times the total parameter count. Mixtral 8x7B, for example, has 46.7B total parameters but only uses about 12.9B active parameters per token, giving it performance comparable to 70B dense models at a fraction of the inference cost.
MoE architectures introduce two key challenges. The first is load balancing: if the gating network routes all tokens to the same few experts, the unused experts contribute nothing. Training typically includes an auxiliary load-balancing loss that encourages uniform expert utilization. The second is communication overhead: in distributed training, different experts may live on different devices. Routing tokens to remote experts requires network communication, which can become a bottleneck at scale.
Sub-Quadratic Sequence Models
A different approach to the quadratic attention problem is to abandon the attention mechanism entirely and replace it with architectures that process sequences with linear or sub-linear complexity.
State Space Models (SSMs) model the sequence transformation as a linear dynamical system. The input sequence is processed through a recurrence:
where:
- is the hidden state
- , , , are learned matrices
- The bars indicate that and are discretized from continuous-time parameters
The recurrent form processes each token in time with memory (constant in sequence length). SSMs can also be unrolled into a convolutional form for efficient parallel training:
where is a learned convolution kernel derived from the SSM parameters. The convolutional form enables parallel training on GPUs while the recurrent form enables efficient autoregressive inference.
Mamba is the most influential SSM architecture for language modeling. Its key innovation is selective state spaces: the transition matrices and depend on the input, making the model input-dependent rather than linear and time-invariant. This selective mechanism allows Mamba to focus its hidden state on the most relevant parts of the input history, similar to how attention focuses on relevant tokens. Mamba achieves competitive performance with transformers on many language tasks at complexity, though its performance on tasks requiring precise token retrieval still lags behind full attention.
RetNet takes a hybrid approach, showing that transformers with decay-based attention (rather than softmax) can be computed in three equivalent ways: recurrently for inference (linear complexity), in parallel for training (quadratic complexity), and in chunks for a middle ground. This "train in parallel, infer recurrently" duality was an important insight because it separated the training computation graph from the inference computation graph.
Depth Efficiency and Early Exit
Transformers typically apply the same number of layers to every input token. A trivial question, like "What is 2 + 2?", receives the same number of forward passes as a complex reasoning problem. This is wasteful.
Early exit architectures add classifier heads after intermediate layers. During inference, if the model's confidence at layer exceeds a threshold , it exits early without computing the remaining layers:
This reduces average inference depth on easy inputs while preserving full-depth computation for hard ones. DeeBERT and FastBERT demonstrated that classification tasks can often exit by layer 3-4 out of 12, reducing computation by 50-70% with minimal accuracy loss.
A related idea is mixture-of-depths: routing tokens to skip certain layers. If a token is already well-represented after 6 layers, it may not need to pass through layers 7-12. This allows the model to dynamically allocate depth to the tokens that need it.
Both early exit and mixture-of-depths challenge a core assumption of the standard transformer: that all computation should be homogeneous. In practice, language is heterogeneous. Some tokens are rare and semantically loaded; others are grammatical connectives that carry little information. Some sentences require careful multi-step reasoning; others are simple pattern completions. Depth efficiency exploits this heterogeneity by allocating computation adaptively. The catch is that training these models is harder: the model must learn useful representations and reliable policies for when to stop. Distillation from a full-depth teacher helps, as does using differentiable threshold mechanisms that allow the exit decision to be trained end-to-end.