Skip to content

Mixture of Depths: adaptive computation spending

Briefly

Mixture of Depths (MoD) — an architectural approach where each token passes through a subset of transformer layers, selected by a router; this reduces the inference computational cost while maintaining quality.

Idea

In a standard transformer, each token passes through all layers equally. MoD introduces a router at each layer that decides whether to process the token with that layer or skip it (pass it forward unchanged).

Key difference from MoE: MoE selects an expert within a layer, MoD — the layer itself.

Routing Mechanism

For each token at a layer, a scalar score is calculated, then a top-k selection is applied per batch.

# Pseudocode for MoD routing
scores = router(x)              # [batch, seq]
threshold = topk_threshold(scores, capacity)
mask = scores >= threshold      # boolean tensor
x = x + mask * layer(x)         # layer is applied only to selected tokens

Tokens not selected are passed through residual connections without computation.

Approach What is Selected Savings
MoE Expert within layer Partially active parameters
MoD Layer entirely FLOPs per token
Early exit Exit moment Tail layers
Pruning Weights Fixed structure

Diagram

flowchart LR
    T[Token] --> R{Router}
    R -->|selected| L[Layer]
    R -->|skipped| S[Skip]
    L --> N[Next layer]
    S --> N

Practical Limitations

  • Routing adds overhead and complicates batching.
  • Savings are more apparent on long sequences; on short ones, gains are minimal.
  • Requires training from scratch or fine-tuning — transferring weights from a dense model is non-trivial.
  • Compatibility with KV-cache requires careful implementation.

When to Use

MoD makes sense under strict FLOPs constraints and long context. For short prompts, the complexity does not justify the gain. Related topics: quantization, decoding strategies.