Mastering Text, Code and Math Simultaneously via Fusing Highly Specialized Language Models

Ning Ding, Yulin Chen, Ganqu Cui, Xingtai Lv, Weilin Zhao, Ruobing Xie, Bowen Zhou, Zhiyuan Liu, Maosong SunarXiv 2024 · DOI

Abstract

Large Language Models (LLMs) often face a "mutual weakening" effect when trained across diverse domains like natural language, programming, and mathematics. This paper introduces ULTRAFUSER, a novel framework designed to master these three domains simultaneously by fusing highly specialized models. Utilizing a token-level gating mechanism and a high-quality synthetic dataset, ULTRACHAT 2, ULTRAFUSER achieves state-of-the-art performance without the typical "alignment tax" associated with multi-domain training. Our results demonstrate that the fused model not only maintains but often exceeds the performance of its individual specialist components.

Introduction

The current landscape of Large Language Models (LLMs) is characterized by a tension between general-purpose versatility and domain-specific expertise. While models can be fine-tuned to excel in specific areas such as Python programming or advanced calculus, this specialization often comes at the cost of general conversational ability—a phenomenon known as the "alignment tax." The distinct symbol systems and logical structures of text, code, and math frequently clash during standard joint training, leading to degraded performance in one or more areas.

To address this, we present ULTRAFUSER, a framework that moves beyond simple model merging or sample-level routing. Instead, it employs a sophisticated "conductor" strategy to blend the strengths of multiple specialists at the token level, creating a unified model that is truly a master of all trades.

Methodology: The ULTRAFUSER Framework

The core of ULTRAFUSER is its ability to dynamically weigh the contributions of different expert models for every token generated.

The Specialists

We utilize three state-of-the-art 13B parameter models as our base specialists:

  • UltraLM-13B for natural language text.
  • CodeLlama-13B for programming and code generation.
  • WizardMath-13B for mathematical reasoning.

Token-Level Gating

Unlike traditional Mixture-of-Experts (MoE) approaches that might route an entire prompt to a single model, ULTRAFUSER implements a gating layer that calculates weights for each specialist at every single token. This allows the model to shift its focus seamlessly. For instance, when transitioning from a textual explanation to a mathematical formula, the gating mechanism automatically increases the weight of the Math specialist.

Two-Stage Training Strategy

To ensure stability and synergy, we employ a two-stage training process:

  1. Stage 1: Gating Layer Training. The specialist models are frozen, and only the gating layer is trained. This allows the "conductor" to learn the optimal expert for various contexts without altering the underlying knowledge of the specialists.
  2. Stage 2: Joint Fine-Tuning. Both the gating layer and the specialists are fine-tuned together. This stage encourages the models to adapt to one another, fostering synergy across domains.

To prevent domain bias, we utilize a "class-balance sampler," ensuring that every training batch contains an equal representation of text, code, and math data.

Data and Evaluation

A critical component of our success is ULTRACHAT 2, a massive instruction-tuning dataset comprising approximately 300,000 high-quality instructions (100,000 per domain). Generated via a multi-stage pipeline using GPT-4, it covers 72 meta-topics and nearly 1,600 sub-topics.

We evaluated ULTRAFUSER across a comprehensive suite of benchmarks:

  • Text: TruthfulQA, AlpacaEval, and MT-Bench.
  • Code: HumanEval (Pass@1).
  • Math: GSM8K, MATH, SAT-Math, and AQuA-RAT.

Key Results

ULTRAFUSER demonstrated significant performance gains over both individual specialists and general-purpose fine-tuned models.

Performance comparison across Text, Code, and Math domains for specialized models versus the fused UltraFuser model.

As shown in Figure 2, ULTRAFUSER achieves an average score of 47.48 across all benchmarks, outperforming UltraLM (35.46), CodeLlama (36.78), and WizardMath (29.91). Notably, the fused model exhibits a "synergy effect":

  • Coding: It reached 53.03% on HumanEval, surpassing the standalone CodeLlama-13B (48.78%). This suggests that mathematical reasoning capabilities may enhance logical consistency in code generation.
  • Truthfulness: On TruthfulQA, it achieved 64.67%, a nearly 6% improvement over the text-specialized baseline.
  • Conversation: It scored 7.02 on MT-Bench, the highest in its class, proving that it retains superior conversational skills while mastering technical domains.

Discussion and Conclusion

The success of ULTRAFUSER highlights the potential of token-level fusion as a strategy for building versatile LLMs. By treating specialized models as modular components, we can avoid the catastrophic forgetting often seen in multi-task fine-tuning.

While the current implementation requires more computational resources during inference (as all three specialists are active), the use of frameworks like vLLM helps mitigate this overhead. Future work will explore expanding this framework to additional domains, such as multilingual support or biological sequence modeling, and investigating ways to reduce the inference footprint without sacrificing performance.

Summary updated

BibTeX
@misc{ding2024mastering,
  author = {Ning Ding and Yulin Chen and Ganqu Cui and Xingtai Lv and Weilin Zhao and Ruobing Xie and Bowen Zhou and Zhiyuan Liu and Maosong Sun},
  title = {{Mastering Text, Code and Math Simultaneously via Fusing Highly Specialized Language Models}},
  year = {2024},
  eprint = {2403.08281},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2403.08281},
}
Figure