Skip to content

Mixture-of-Depths

At a glance

Mixture-of-Depths: a router per routed layer sends only a fraction of the tokens through the layer.

Registered name Family Saves Lossless Trainable Configuration
mod dynamic-depth compute or latency no routers MoDConfig

Backends (default configuration): transformers: real · gguf: no · mlx: no · onnx: no · sglang: no · trtllm: no · vllm: no

Mixture-of-Depths (Raposo et al., 2024) puts a router in front of a subset of the layers. Router l scores every token with a logit r = w . x + b of the layer's input x. A token that is routed through the layer leaves it as x + r (layer(x) - x), and a token that is routed around it passes unchanged. In training, the capacity fraction of each sequence's tokens with the largest r runs the layer. That top-k choice looks at the whole sequence, which autoregressive decoding cannot do, so an auxiliary binary cross-entropy teaches sigmoid(r) to predict top-k membership, the first of the paper's two remedies, and at inference a token runs the layer when r > 0. The saving is the layer applications of the routed-around tokens.

Mixture-of-Depths is lossy. The paper trains the routers together with the model during pretraining. economize trains only the routers, on the frozen base model. In the paper a routed-around token writes no K/V at that layer. Here it writes the K/V that the layer computes from its unchanged state (kv="fill"), the convention of the other dynamic-depth techniques, and the paper's variant kv="routed" is rejected because the runtime does not support it.

Setup

Every tutorial starts from the same model and prompt:

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

import economize

device = "cuda" if torch.cuda.is_available() else "cpu"
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B", dtype=torch.bfloat16, device_map=device)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B")
ids = tokenizer("Natalia sold clips to 48 of her friends in April, and then she sold half as many clips in May. "
                "How many clips did Natalia sell altogether?", return_tensors="pt").input_ids.to(device)

Training data is a flat file of uint32 token ids, the format of nanoGPT-style pretraining shards, and calibration data is a [N, S] tensor of token ids. Both are made here from GSM8K:

import numpy as np
from datasets import load_dataset

texts = [f"{row['question']}\n{row['answer']}" for row in load_dataset("openai/gsm8k", "main", split="train")]
tokens = np.concatenate([tokenizer(t + tokenizer.eos_token).input_ids for t in texts]).astype(np.uint32)
tokens[: len(tokens) * 9 // 10].tofile("train.bin")
tokens[len(tokens) * 9 // 10 :].tofile("val.bin")

calibration = torch.from_numpy(tokens[: 128 * 2048].astype(np.int64)).view(128, 2048)

Quick start

The base model's greedy output is the reference for quality, so generate it before the technique is applied:

reference = base.generate(ids, max_new_tokens=64, do_sample=False)[0, ids.shape[1]:]

The routers start with zero weights and a bias of 1, which routes every token through every layer with weight exactly 1, so the untrained model is the base model. Training teaches them which tokens to route around:

from economize.techniques.dynamic_depth import MoDConfig, token_mismatch
from economize.train import fit

model = economize.apply(base, MoDConfig(capacity=0.5))
fit(model, "train.bin", steps=2000, batch_size=8, seq_len=1024, seed=0)

result = model.decode(ids[0], 64)
print(tokenizer.decode(result.tokens))
summary = model.techniques["mod"].cost_summary(result)
print(token_mismatch(result.tokens, reference))      # fraction of positions that differ from the base model
print(summary["relative_effective_layer_tokens"])    # layer applications relative to plain decoding
print(summary["executed_fraction_per_layer"])        # fraction of the tokens that ran each layer

The routed layers default to every second layer, 2, 4, ..., 24, as in the paper. capacity=0.5 lets half of each sequence's tokens run a routed layer in training, and the paper's 12.5% is the default.

Measuring quality and saving

token_mismatch is CALM's token-level consistency loss: the fraction of the positions of the longer generation at which the two generations differ, with a missing token counted as a difference. Greedy decoding compounds every early difference, so it is worth averaging it over prompts from the target workload. relative_effective_layer_tokens divides the layer applications that ran by those of plain decoding of the same output, prompt included, and phases["decode"] holds the counts of the decoding steps alone. At inference no capacity is enforced, so the routed fraction follows the trained routers and can differ from capacity.

Training

fit without an objective uses MoDObjective(): the language-model loss under top-k routing plus aux_weight times the router cross-entropy. kl_weight adds a KL divergence to the base model's distribution, and ce_weight scales the language-model loss:

from economize.techniques.dynamic_depth import MoDObjective

model.detach()
model = economize.apply(base, MoDConfig(layers=tuple(range(13, 25)), capacity=0.5, router_input="rms"))
fit(model, "train.bin", objective=MoDObjective(aux_weight=0.5, kl_weight=1.0),
    steps=2000, batch_size=8, seq_len=1024, seed=0)

layers names the routed layers, here the upper half. router_input="rms" scores the input scaled to unit root mean square, which does not grow with the depth of the layer. router_init_std draws random initial router weights. skip_prompt=False runs the prompt through every layer.

Saving and loading

save_pretrained writes the configuration and the router weights:

model.save_pretrained("mod_model")
model.detach()

model = economize.load(base, "mod_model")

Combining with other techniques

Only one dynamic-depth technique is used per model. Mixture-of-Depths composes with drafters and with KV-cache techniques. The exact pass that verifies a draft follows the routers, so greedy verification reproduces the output of Mixture-of-Depths alone. Here the drafter is an untrained EAGLE-3 head (train it as in EAGLE-3 before use) and the cache is quantized to 4 bits:

from economize.techniques.eagle3 import Eagle3Config
from economize.techniques.kv_quant.kivi import KIVIConfig

model.detach()
model = economize.apply(base, MoDConfig(), KIVIConfig(bits=4), Eagle3Config(),
                        decoding=economize.DecodeConfig(cycle_length=6))
combined = model.decode(ids[0], 64)

Serving

Mixture-of-Depths runs on the Hugging Face path only, and a model with it decodes without CUDA graphs:

print(economize.backend_support(MoDConfig()))

Configuration

MoDConfig dataclass

MoDConfig(layers: tuple[int, ...] = (), skip_prompt: bool = True, decision_group: int = 1, capacity: float = 0.125, router_bias: float = 1.0, router_init_std: float = 0.0, router_input: str = 'raw', kv: str = 'fill', threshold: float = 0.0)

Mixture-of-Depths.

Attributes:

Name Type Description
layers tuple[int, ...]

Routed layers (empty: every second layer, 2, 4, ..., as in the paper).

capacity float

Fraction of a sequence's tokens that run a routed layer in training (the paper's 12.5%).

router_bias float

Initial router bias. With zero weights, 1 routes every token with weight exactly 1, which leaves the model unchanged.

router_init_std float

Standard deviation of the initial router weights.

router_input str

:data:MOD_ROUTER_INPUTS.

kv str

:data:MOD_KV_MODES (see the module docstring).

threshold float

Router logit above which a token runs a routed layer at inference (the paper's rule: 0).

skip_prompt bool

Route the prompt too (False: the prompt runs every layer).

routed_layers

routed_layers(num_layers: int) -> tuple[int, ...]

The routed layers (:meth:layer_set).

References

  • Raposo, D., Ritter, S., Richards, B., Lillicrap, T., Humphreys, P. C., & Santoro, A. (2024). Mixture-of-depths: Dynamically allocating compute in transformer-based language models (arXiv:2404.02258). arXiv. https://doi.org/10.48550/arXiv.2404.02258