raymin0223/mixture_of_recursions

Mixture-of-Recursions: Learning Dynamic Recursive Depths for Adaptive Token-Level Computation (NeurIPS 2025)

📚 What is Mixture‑of‑Recursions (MoR)?

MoR is a research codebase that implements the method described in the NeurIPS‑2025 paper “Mixture‑of‑Recursions: Learning Dynamic Recursive Depths for Adaptive Token‑Level Computation”. The goal is to make large language models (LLMs) run faster by letting each token decide how many transformer layers it really needs during inference. It solves two long‑standing problems of early‑exiting techniques:

  1. Missing KV‑cache – when a token exits early, later layers never produce the key‑value pairs that the next tokens need. MoR’s recursion‑wise KV caching stores exactly the needed KV pairs so downstream tokens can still attend correctly.
  2. Inefficient batched inference – tokens that finish early would otherwise block the whole batch. MoR’s routing mechanism assigns a recursion depth per token and processes all tokens together, keeping the batch dense.

The paper reports up to 2× higher throughput at comparable accuracy while also cutting training FLOPs and memory.


🚀 Core ideas

Component What it does
Routing mechanism A learned router (expert‑choice or token‑choice) predicts, for each token, the number of recursive passes (i.e., how deep it should go).
Recursion‑wise KV cache KV pairs are stored only for the recursions a token actually runs, eliminating the “missing cache” issue and saving memory.
Parameter sharing All recursions share the same transformer block weights (built on Llama‑style LlamaForCausalLM), enabling tokens at different depths to be processed together in a single batch.
Training utilities Scripts to generate Hydra/YAML configs, launch distributed training with DeepSpeed or Accelerate, and evaluate few‑shot performance via the EleutherAI LM‑Eval‑Harness.

🛠️ Getting started

  1. Create a conda env (Python 3.12) and install the exact versions used by the authors (torch 2.6.0+cu124, flash‑attn 2.7.4.post1, transformers 4.52.4) – this avoids subtle bugs in the custom attention code.
  2. Download the pre‑training corpus – a deduplicated slice of FineWeb‑Edu, accessed through the HuggingFace smollm-corpus dataset. The repo provides a shell script (download_fineweb-edu-dedup.sh) that pulls the data and places it where lm_dataset/load_dataset.py expects it.
  3. Run training – the authors used 4 × H100/A100 GPUs. Distributed launch can be done with either:
    deepspeed --include localhost:0,1,2,3 pretrain.py --config-name example
    # or
    accelerate launch --config_file acc_configs/default_config.yaml pretrain.py --config-name example
    
    Config files are auto‑generated by util/generate_pretrain_eval_fewshot_configs.py from the template YAMLs under conf/.
  4. Evaluate – few‑shot accuracy is measured with the standard LM‑Eval‑Harness. The same launch commands work with eval_fewshot.py.
  5. Explore routing – a Jupyter notebook (notebooks/250727_get_mor_routing_decision.ipynb) visualises which tokens take which recursion depth.

📦 Pre‑trained models

Checkpoints for three 360 M‑parameter variants (Vanilla, Recursive, MoR) are hosted on Google Drive. After downloading, place them in ./checkpoints or use the provided gdown command.


📖 Where to look in the repo

Directory / File Purpose
model/mor_model/ Implements the two router variants (expert_choice_router.py, token_choice_router.py) and the modified LlamaForCausalLM that supports recursion‑wise KV caching.
lm_dataset/ Custom dataset class and download scripts for FineWeb‑Edu.
conf/ Hydra configuration templates for pre‑training and few‑shot evaluation.
util/generate_pretrain_eval_fewshot_configs.py Helper to create concrete YAML configs from the templates.
scripts/pretrain_eval_fewshot.sh One‑liner to launch training and evaluation together.
evaluate_fineweb_test.py Computes validation loss for scaling‑law analysis.

🙏 Citation

If you use MoR or its checkpoints, cite the arXiv paper (see the README’s BibTeX). The authors also list their affiliations (KAIST, Mila, Google DeepMind/Research, etc.) and note equal contribution.


Bottom line: Mixture‑of‑Recursions is a full‑featured research implementation that lets LLMs adaptively choose how many transformer layers each token runs, addressing both KV‑cache and batching inefficiencies and delivering measurable speed‑ups without sacrificing accuracy.

Related

  • Project
  • Project
  • Project
  • Project