Unlocking Emergent Modularity in Large Language Models

Zihan Qiu, Zeyu Huang, Jie Fu

Abstract

Modular Neural Networks (MNNs) demonstrate various advantages over monolithic models. Existing MNNs are generally explicit: their modular architectures are pre-defined, with individual modules expected to implement distinct functions. Recent works reveal that there exists implicit modularity in standard pre-trained transformers, namely Emergent Modularity. They indicate that such modular structures spontaneously exhibit during the early pre-training phase. Despite the benefits of modularity, most Language Models (LMs) are still treated as monolithic models in the pre-train and fine-tune paradigm, with their emergent modularity locked and underutilized. In this work, focusing on unlocking the emergent modularity in LMs, we showcase that standard LMs could be fine-tuned as their Mixture-of-Expert (MoEs) counterparts without introducing any extra parameters. Such MoEs are derived from emergent modularity and are referred to as Emergent MoEs (EMoE). Our experiments demonstrate that fine-tuning EMoE effectively improves downstream in-domain and out-of-domain generalization compared with vanilla fine-tuning. Our analysis and ablation studies further illustrate that it is robust to various configurations and can scale up to Large Language Models (i.e., Llama2-7B and Llama-30B).

Introduction: The Hidden Structure of LLMs

Large Language Models (LLMs) are typically treated as monolithic blocks, but they actually possess "emergent modularity" where neurons spontaneously group by function during pre-training. Despite their success, standard transformers are usually fine-tuned as "monolithic" models, meaning every neuron is updated and used for every input. However, research shows that neurons in the Feed-Forward Network (FFN) layers of pre-trained models are naturally sparse and specialized.

Visualization of raw activation patterns in a pre-trained LLM, showing emergent sparsity and modularity.

Standard fine-tuning "locks" this emergent modularity, leading to sub-optimal performance and poor out-of-domain (OOD) generalization. The challenge is: how can we leverage this internal modularity to improve fine-tuning without the massive computational overhead or architectural complexity typically associated with Mixture-of-Experts (MoE) models?

Methodology: Emergent Mixture-of-Experts (EMoE)

The authors propose a three-step process to transform a standard transformer into an Emergent MoE (EMoE), reconfiguring standard FFNs into sparse MoE layers during fine-tuning to improve generalization without adding new parameters.

Conceptual overview of EMoE: (a) viewing FFNs as key-value memories, (b) clustering neurons into explicit experts, and (c) implementing avg-k gating for routing.

The transformation involves:

  1. FFNs as Key-Value Memories: The first linear layer of an FFN is viewed as "keys" (detecting patterns) and the second as "values" (producing responses).
  2. Clustering-based Expert Construction: Instead of adding new experts, constrained clustering groups existing neurons based on the similarity of their "key" vectors. Neurons detecting similar patterns are grouped into the same "expert."
  3. Avg-k Gating: To route inputs without adding new trainable parameters, the average of the key vectors within an expert group serves as the router. For any given input, only the "top-k" most relevant experts are activated.
  4. Parameter-Efficient Fine-tuning: Using LoRA (Low-Rank Adaptation), the model is fine-tuned. Since experts are partitions of the original FFN, the model can be "collapsed" back into its monolithic architecture post-training, ensuring zero inference overhead.

Experimental Evaluation

The researchers evaluated EMoE across diverse benchmarks including GLUE (in-domain), GLUE-X (out-of-domain), MMLU (scaling for Llama-2 7B/30B), Alpaca (instruction-tuning), and Domainbed (vision-based ViT).

Key Results

  • Performance Gains: On GPT2-XL (1.5B), EMoE improved the average GLUE score by 0.84 points and significantly outperformed vanilla LoRA on OOD tasks.
  • Scaling to LLMs: On Llama2-7B, EMoE improved MMLU scores by 0.62 points compared to standard LoRA.
  • OOD Robustness: EMoE consistently showed lower error rates on OOD benchmarks (like GLUE-X) compared to both vanilla tuning and GMoE.
  • Efficiency: Training time and FLOPs remained nearly identical to standard LoRA tuning, with memory usage increasing by less than 5%.

Discussion and Insights

  • Modularity is a "Free Lunch": EMoE achieves better results than vanilla fine-tuning without adding extra parameters or significant training time.
  • Masking "Negative Transfer": By activating only specific experts, EMoE effectively masks out neurons that might have a "negative transfer" effect on a specific task. Selecting the bottom-k experts significantly degrades performance compared to the standard monolithic baseline.
  • Stability over Learning: "Avg-k" gating based on pre-trained weights was more stable and converged better than "learned" gating, suggesting pre-trained models already "know" how to route information.
  • Deployment Practicality: The final weights can be merged back into a standard FFN, making it highly practical for production environments.

Conclusion and Future Work

While validated on general benchmarks, EMoE hasn't been tested on highly complex reasoning tasks or "ultra-scale" models (100B+). Future work could explore more sophisticated clustering algorithms for identifying functional groups and a more principled way to select which layers to modularize.

Summary updated

BibTeX
@inproceedings{qiu2024unlocking,
  author = {Zihan Qiu and Zeyu Huang and Jie Fu},
  title = {{Unlocking Emergent Modularity in Large Language Models}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=qDNwsuPX9k},
}
Figure