ModuleFormer: Learning Modular Large Language Models From Uncurated Data

Yikang Shen, Zheyu Aqa Zhang, Tianyou Cao, Shawn Tan, Zhenfang Chen, Chuang Gan

TL;DR

ModuleFormer is a modular Large Language Model (LLM) architecture that uses Sparse Mixture-of-Experts (SMoE) to achieve the performance of dense models with more than twice the throughput. By introducing novel load-balancing and concentration losses, it induces specialized modularity from uncurated data, enabling efficient deployment, easy knowledge extension without catastrophic forgetting, and task-specific pruning.

What Problem Does This Paper Address?

Modern LLMs are remarkably capable but suffer from three major "inflexibilities":

  1. Efficiency: Most models are "dense," meaning every parameter is activated for every token during inference, making them expensive to deploy.
  2. Extendability: LLMs are typically "frozen in time." Updating them with new knowledge often leads to "catastrophic forgetting," where the model loses its original capabilities.
  3. Specialization: Deploying a massive model for a narrow task (like Python coding) is wasteful, yet there are few effective ways to "prune" these models without significant performance degradation.

While modular architectures like Sparse Mixture-of-Experts (SMoE) exist, they often require carefully curated, domain-labeled data to train specialized "experts." The authors of ModuleFormer aimed to create a system where this modularity emerges naturally from raw, uncurated data.

Methods

ModuleFormer reimagines the standard Transformer block by introducing two types of modules:

  • Feedforward (FFD) Experts: Replacing the standard feedforward layer with a Mixture-of-Experts (MoE) layer.
  • Stick-breaking Attention Heads: A novel Mixture-of-Attention (MoA) mechanism. Unlike standard attention, this uses a "stick-breaking" process (derived from the Dirichlet process) to model attention weights. This implicitly encodes positional information, eliminating the need for traditional positional embeddings (like RoPE) and simplifying length extrapolation for long contexts.

To make modularity "emerge," the authors introduced two key loss functions:

  1. Mutual Information (MI) Loss (Pretraining): This balances the load across experts. It encourages the model to use a diverse set of experts across a batch of data while ensuring that for any single token, the gating probability is concentrated on just a few experts.
  2. Load Concentration Loss (Finetuning): When adapting the model to a specific task, this loss encourages the model to rely on an even smaller subset of modules, making the rest redundant and safe to prune.
Figure 1: The architecture of ModuleFormer showing sparse activation, adding new modules for extension, and pruning for specialization.

Datasets

The authors evaluated ModuleFormer (dubbed MoLM) across a wide array of benchmarks:

  • Pretraining: The Pile (300 billion tokens).
  • General Benchmarks: Hellaswag, PIQA, ARC, and OpenBookQA for zero-shot reasoning; TriviaQA for few-shot QA.
  • Code Generation: HumanEval (Python programming tasks).
  • Continual Learning: CC-100 (multilingual corpus) to test the model's ability to learn new languages (German and Vietnamese) while retaining English.
  • Finetuning/Pruning: A 15B token subset of GitHub-code-clean.

Key Insights and Ideas

  • Emergent Specialization: The MI loss successfully forces experts to specialize in different domains even without explicit labels. As shown in Figure 2, similar domains (like different book datasets) share similar expert activation patterns, while distinct domains (like math vs. subtitles) use very different experts.
  • Sparsity is a Feature, Not a Bug: By only activating ~25% of its parameters per token, ModuleFormer achieves the performance of a 1.3B dense model while running at the speed of a 350M model.
  • Knowledge Isolation: Because knowledge is stored in discrete modules, adding new knowledge (like a new language) can be done by simply inserting new modules and training them while freezing the old ones. This effectively bypasses the stability-plasticity dilemma that plagues dense models.
Figure 2: KL-divergence of expert distributions across different domains, illustrating how the model naturally clusters similar data types.

Key Results

  • Efficiency: MoLM-4B-K2 (4 billion total parameters, but only 350M active per token) achieved 2x the throughput of comparable dense models like Pythia-1.4B, with lower latency and memory footprint.
  • Performance: MoLM-8B-K2 (700M active parameters) matched the performance of dense models with 2.7B–2.8B parameters across standard NLP benchmarks.
  • Pruning: After finetuning with the load concentration loss, the authors could prune 40% to 50% of the parameters without sacrificing performance on the target task. This significantly outperformed standard layer-wise pruning methods.
  • Continual Learning: When learning new languages, ModuleFormer maintained its English proficiency significantly better than LoRA or full finetuning, demonstrating superior "immunity" to catastrophic forgetting.

Limitations and Future Work

  • Total Parameter Count: Although active parameters are low, the total parameter count is higher than dense models for the same performance level. This means that while inference is fast, the model still requires significant disk space and VRAM to store the inactive weights.
  • Optimization Complexity: The authors note that discrete gating decisions in SMoE are inherently difficult to optimize. They suggest that finding more optimal ways to learn these gating functions remains an open research question.
  • Scaling: While the results at 4B and 8B parameters are promising, testing the architecture at the 70B+ scale remains for future work.

Summary updated

BibTeX
@misc{shen2023moduleformer,
  author = {Yikang Shen and Zheyu Aqa Zhang and Tianyou Cao and Shawn Tan and Zhenfang Chen and Chuang Gan},
  title = {{ModuleFormer: Learning Modular Large Language Models From Uncurated Data}},
  year = {2023},
  eprint = {2306.04640},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2306.04640},
}
Figure