Mixture-of-Depths (MoD): Mathematical Foundations, Dynamic Compute Routing, Capacity-Constrained Tensors, and IsoFLOP Scaling
In standard autoregressive Transformer architectures, computational effort is distributed uniformly across all tokens in a sequence. Every token position passes through every layer , executing identical matrix multiplications across multi-head self-attention and feed-forward networks (FFN). This architectural constraint ignores the wide disparity in information density across natural language and formal reasoning: trivial grammatical connectors, punctuation, and predictable tokens require minimal processing, whereas complex multi-step reasoning tokens demand substantial non-linear transformation.
Prior attempts to introduce conditional computation into deep neural networks—such as Adaptive Computation Time (Graves, 2016), Universal Transformers (Dehghani et al., 2018), and early-exiting mechanisms like Confident Adaptive Language Modeling (Schuster et al., 2022)—often created dynamic computation graphs, variable sequence lengths per batch, and ragged tensor shapes. These non-uniform execution profiles conflict directly with modern hardware accelerators (TPUs and GPUs), which require static memory allocations and predetermined matrix dimensions to maximize systolic array utilization.
The Mixture-of-Depths (MoD) framework (Raposo et al., Google DeepMind, 2024) resolves this tension by enforcing a static computational budget per layer through capacity-constrained top- routing. By capping the number of tokens that participate in self-attention and MLP blocks at a predefined constant , MoD preserves static tensor shapes while allowing the network to dynamically assign depth across sequence positions.

1. The Mechanical Inefficiency of Uniform Compute
In a standard decoder Transformer with layers, hidden dimension , sequence length , and intermediate MLP dimension , the computational cost per token sequence per layer is dominated by matrix multiplications:
Across all layers, total floating-point operations scale as . Standard Transformers evaluate this full computational graph regardless of the empirical loss gradient or Shannon entropy at token step .
When a model processes boilerplate syntax or repetitive boilerplate patterns, passing representations through dense FFN projections and full attention context updates yields negligible predictive improvement while consuming identical energy and memory bandwidth. Mixture-of-Depths replaces this static allocation with a dynamic policy that routes only a critical subset of tokens through computation blocks while streaming the remaining tokens through identity residual pathways.
2. Mathematical Formulation of Mixture-of-Depths
Let denote the input matrix of token representations at layer .
2.1 Linear Router Projections
Each MoD block incorporates a lightweight routing mechanism parameterized by a weight vector . The router projects each token embedding into a scalar routing logit :
The vector of all sequence routing logits at layer is denoted as .
2.2 Capacity Factor and Quantile Selection
Unlike token-choice routing in Mixture of Experts (Shazeer et al., 2017), which allows variable numbers of tokens per expert and requires auxiliary load-balancing losses, MoD employs expert-choice routing across the sequence dimension.
The system defines a sequence capacity , governed by a user-specified capacity factor :
To select the top- tokens, the router determines the dynamic selection threshold corresponding to the -th percentile of , where:
The subset of tokens chosen to participate in layer computation is defined by the index set:
Let represent the gathered matrix containing solely the representations of tokens in .
2.3 Layer Output Update and Residual Bypass
Let denote the composite block transformation comprising multi-head self-attention followed by an MLP:
For tokens outside , computation is skipped entirely, passing the input directly to the output via the residual stream. For selected tokens, the output of is scaled by the router weight to maintain gradient flow into the router parameters:
Input Sequence X^l (S tokens)
│
├───> Router: r_i = w^T * x_i ───> Top-k Selection (Capacity C = α * S)
│ │
│ ├── Selected Tokens (C tokens) ───> [ Attention & MLP ] ───> (*) Scale by r_i ───┐
│ │ │
│ └── Bypassed Tokens (S - C tokens) ───────────────────────── Identity Bypass ───(+)
│ │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
│
Output Sequence X^{l+1} (S tokens) <───────────────────────────────────────────────────────────────────────────────────────────┘2.4 Differentiability and Gradient Routing
A primary challenge in conditional routing is ensuring differentiability across discrete token selection. In MoD, multiplying the block transformation by the continuous scalar places directly on the computational graph.
The loss gradient with respect to router weights is:
This formulation eliminates the need for score function estimators (REINFORCE) or straight-through gradient estimators (STE). The gradient magnitude received by is proportional to the contribution of the block computation toward reducing the overall language modeling loss .
3. The Non-Causal Top-k Dilemma and Causal Autoregressive Sampling
During pre-training on complete sequences of length , calculating the top- tokens is executed across the entire sequence. However, in autoregressive decoding, tokens are generated sequentially one by one (). At step , the router cannot determine if ranks in the top -fraction of the future sequence , because tokens do not yet exist.
To solve this causal barrier, Raposo et al. (2024) evaluated two mechanisms during training:
3.1 Auxiliary Sigmoid Centering Loss
The first approach enforces an auxiliary binary classification objective directly onto the routing logits . Let denote ground-truth participation based on sequence-level top- selection:
An auxiliary binary cross-entropy loss is applied over the sigmoid of router logits:
This loss pressures the router to output values () for tokens that belong to the top- set, and () for bypassed tokens. During autoregressive decoding, the model routes a token into the computation block if , operating independently of future context.
3.2 Auxiliary MLP Predictor Head
The second approach trains a distinct lightweight classifier in parallel with the main network. The router receives representation with a stop-gradient operator:
Because isolates the backbone from , the main language modeling representations remain unconstrained by auxiliary objectives.
Empirical evaluations in the DeepMind study demonstrated that the auxiliary predictor reaches greater than 97% routing agreement within initial training steps and exceeds 99% accuracy at convergence. When switching from sequence-level top- to causal predictor routing at inference, language model perplexity degradation is negligible (less than 0.2%).
4. IsoFLOP Analysis and Pareto Optimal Frontiers
To evaluate efficiency rigorously, conditional computation architectures must be assessed under compute-equivalent regimes (Chinchilla scaling laws, Hoffmann et al., 2022).
Validation Loss (Lower is Better)
│
│ Vanilla Transformer IsoFLOP Frontier
│ \
│ \ MoD IsoFLOP Frontier (Shifted Down & Right)
│ \ \
│ ● \
│ \ ● (Matches Baseline Loss with 50-66% Fewer FLOPs/Step)
│ \ \
│ ● ● (IsoFLOP Optimal: Lower Loss, Larger Parameter Count)
│ \
│
└──────────────────────────────────────────────────────── Step Time / FLOPs4.1 Optimal Capacity Factor and Block Interleaving
DeepMind swept capacity factors and structural routing patterns across training budgets ranging from to total FLOPs.
Key empirical findings include:
- Aggressive Capacity Reduction (): Restricting computation blocks to process only of tokens per sequence () yielded superior loss-to-FLOP trade-offs compared to moderate reductions ( or ).
- Alternating Block Interleaving: Models that route every single layer suffered from representation drift. In contrast, interleaved routing—alternating between one full-capacity dense Transformer block () and one MoD routing block ()—achieved optimal stability. The dense blocks ensure frequent global attention mixing across all tokens, while the MoD blocks execute sparse, high-impact transformations.
- Down-and-to-the-Right Frontier Shift: For a fixed training FLOP budget, the optimal MoD configuration contains more total parameters than the optimal dense baseline, yet executes a single forward pass in 50% to 66% of the wall-clock time.
5. Mixture-of-Depths-and-Experts (MoDE)
The routing mechanism of Mixture-of-Depths operates along the depth dimension (determining whether to compute), whereas Mixture-of-Experts (Switch Transformers, Fedus et al., 2022; ST-MoE, Zoph et al., 2022) operates along the width dimension (determining which expert to compute).
These two paradigms integrate into Mixture-of-Depths-and-Experts (MoDE) through two architectures:
5.1 Staged MoDE
In Staged MoDE, a binary MoD router first determines whether token participates in the block. If selected, the token passes to self-attention and subsequently enters a standard top- MoE router that distributes it among FFN experts:
5.2 Integrated MoDE
In Integrated MoDE, the MoD residual bypass is incorporated directly into the MoE routing matrix as an explicit -th "No-Op" expert:
A unified router outputs a softmax distribution over all choices. DeepMind's empirical comparisons revealed that Integrated MoDE outperforms standard MoE with reduced capacity factors, because tokens explicitly learn to route to the identity operation rather than suffering arbitrary dropping when expert capacities saturate.
6. Structural Comparison Across Sparsity Paradigms
- Standard Dense Transformer: Dense computation across all layers; static computation graph; no routing mechanism; full KV cache (); baseline (1.0x) execution.
- Mixture-of-Experts (MoE): Width-level sparsity across FFN experts; static computation graph; top- routing over experts; full KV cache (); reduces active parameter footprint per token while preserving total model capacity.
- Early Exit (CALM / ACT): Depth-level sparsity via prefix halting; dynamic / ragged computation graphs; confidence thresholding routing; reduced KV cache for halted tokens; token halts execution permanently at early layer .
- LayerSkip: Depth-level sparsity via layer dropout during training; static / speculative computation graphs; early-exit draft verifier; full multi-layer verification; speculative drafting with early layer exits followed by full-model verification.
- Mixture-of-Depths (MoD): Depth-level sparsity across selective intermediate blocks; static computation graph with fixed top- tensor capacity; top- quantile routing with causal auxiliary prediction; reduced KV cache for bypassed layers; reduces FLOPs per forward step by up to 50% to 66% while allowing tokens to rejoin later layers.
7. Reference PyTorch Implementation
Below is a complete, standalone PyTorch module demonstrating the capacity-constrained top- routing mechanism, the auxiliary sigmoid loss, and the residual bypass of a Mixture-of-Depths layer:
import torch
import torch.nn as nn
import torch.nn.functional as F
class MixtureOfDepthsBlock(nn.Module):
"""
Mixture-of-Depths (MoD) Transformer Block.
Routes a fixed capacity fraction (alpha) of tokens through Self-Attention & MLP,
bypassing the remainder via a direct residual connection.
"""
def __init__(self, d_model: int, n_heads: int, d_ffn: int, alpha: float = 0.125):
super().__init__()
self.d_model = d_model
self.alpha = alpha
# Router projection
self.router = nn.Linear(d_model, 1, bias=False)
# Core computation block (Self-Attention + MLP)
self.norm1 = nn.LayerNorm(d_model)
self.attn = nn.MultiheadAttention(d_model, n_heads, batch_first=True)
self.norm2 = nn.LayerNorm(d_model)
self.mlp = nn.Sequential(
nn.Linear(d_model, d_ffn),
nn.GELU(),
nn.Linear(d_ffn, d_model)
)
def forward(self, x: torch.Tensor, causal: bool = False):
"""
Args:
x: Tensor of shape [batch_size, seq_len, d_model]
causal: If True, uses causal thresholding (inference mode)
Returns:
out: Tensor of shape [batch_size, seq_len, d_model]
aux_loss: Auxiliary classification loss for router training
"""
B, S, D = x.shape
capacity = max(1, int(self.alpha * S))
# 1. Compute scalar routing logits: [B, S]
router_logits = self.router(x).squeeze(-1)
if not causal:
# Training Mode: Sequence-level Top-k (non-causal selection)
# Find top-C routing weights across sequence dimension
topk_vals, topk_indices = torch.topk(router_logits, k=capacity, dim=-1)
# Binary mask for active tokens: [B, S]
mask = torch.zeros(B, S, device=x.device, dtype=torch.bool)
mask.scatter_(dim=1, index=topk_indices, value=True)
# Ground-truth targets for auxiliary router loss: 1 if in top-k, 0 otherwise
targets = mask.float()
aux_loss = F.binary_cross_entropy_with_logits(router_logits, targets)
else:
# Inference Mode: Causal thresholding (sigma(logit) > 0.5 <=> logit > 0)
mask = router_logits > 0.0
aux_loss = torch.tensor(0.0, device=x.device)
# 2. Extract selected tokens: [B, capacity, D]
# For batch implementations, gather active tokens into fixed-size capacity buffer
out = x.clone()
for b in range(B):
active_idx = torch.nonzero(mask[b]).squeeze(-1)
if len(active_idx) == 0:
continue
# If active tokens exceed capacity during causal inference, truncate to capacity
if len(active_idx) > capacity:
active_idx = active_idx[:capacity]
selected_x = x[b:b+1, active_idx, :] # [1, C, D]
# Compute Self-Attention on active tokens
norm_x = self.norm1(selected_x)
attn_out, _ = self.attn(norm_x, norm_x, norm_x)
attn_res = selected_x + attn_out
# Compute MLP on active tokens
mlp_out = self.mlp(self.norm2(attn_res))
block_out = attn_res + mlp_out # [1, C, D]
# Scale block output by router weights (places router in gradient path)
routing_weights = router_logits[b, active_idx].unsqueeze(-1) # [C, 1]
scaled_out = block_out * routing_weights
# Residual addition
out[b, active_idx, :] = x[b, active_idx, :] + scaled_out.squeeze(0)
return out, aux_loss8. Summary and Architectural Takeaways
The Mixture-of-Depths paradigm establishes several foundational principles for efficient autoregressive model design:
- Decoupling Parameters from FLOPs: MoD demonstrates that scaling parameter count without increasing per-step FLOPs improves convergence rates and downstream task accuracy on an isoFLOP basis.
- Hardware-Harmonious Conditional Compute: Unlike early-exit and adaptive recurrence algorithms that produce ragged tensor executions, MoD preserves fixed capacity factors , maintaining static memory graphs and high accelerator compute density.
- Interleaved Depth Sparsity: Optimal execution is achieved by interleaving dense global-attention layers with sparse 12.5% capacity MoD layers, ensuring robust long-range context integration while bypassing over 85% of redundant FFN and attention calculations in intermediate stages.
- Causal Autoregressive Decoupling: Non-causal top- pre-training is converted to causal step-by-step inference via auxiliary sigmoid centering losses or stop-gradient classifier predictors with greater than 99% routing fidelity.
Sources
- Raposo et al. (2024). Mixture-of-Depths: Dynamically allocating compute in transformer-based language models. Google DeepMind. arXiv:2404.02258
- Shazeer et al. (2017). Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer. arXiv:1701.06538
- Fedus et al. (2022). Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity. Journal of Machine Learning Research. arXiv:2101.03961
- Zoph et al. (2022). ST-MoE: Designing Stable and Transferable Sparse Expert Models. arXiv:2202.08906
- Hoffmann et al. (2022). Training Compute-Optimal Large Language Models (Chinchilla). DeepMind. arXiv:2203.15556
- Schuster et al. (2022). Confident Adaptive Language Modeling (CALM). Google Research. arXiv:2207.07061
- Ainslie et al. (2023). CoLT5: Faster Long-Range Transformers with Conditional Computation. Google Research. arXiv:2303.09752
- Graves (2016). Adaptive Computation Time for Recurrent Neural Networks. arXiv:1603.08983



