TL;DR
This paper investigates whether code data helps Large Language Models (LLMs) reason better depending on when it is introduced: during initial pre-training or later during instruction-tuning. The researchers found that mixing code into pre-training significantly boosts general logic and reasoning across all domains, while adding it during instruction-tuning primarily enhances task-specific coding abilities.
What Problem Does This Paper Address?
It is widely recognized that LLMs trained on code (like OpenAI’s Codex or Meta’s Code Llama) are not just better at programming—they also exhibit superior general reasoning capabilities. However, the exact "recipe" for this success remains somewhat of a black box.
The authors tackle a fundamental architectural question: At which training stage does code data provide the most significant boost to reasoning? Is it better to bake the logic in from the start during pre-training, or can we "teach" a model to be logical later on via instruction-tuning? Understanding this dynamic helps researchers build more efficient models without wasting massive computational resources on suboptimal data mixtures.
Methods
To systematically explore the impact of code data, the researchers used the PanGu architecture (a 32-layer Transformer decoder) as their primary testbed.
They compared three primary configurations to isolate the effects of code data:
- NL (2.6B/13B): Models pre-trained exclusively on natural language text.
- CODE (2.6B): A model pre-trained from scratch on a mixture of text and code.
- Instruction-Tuning Variants: The team took these pre-trained models and fine-tuned them using different ratios of natural language instructions versus code-related instructions.
Crucially, they experimented with Dynamic Mixing Strategies during the instruction-tuning phase to see how the timing of code exposure affects learning:
- Uniform Sampling: Keeping the code-to-text ratio constant throughout tuning.
- Stepwise Increase: Gradually increasing the proportion of code data.
- Stepwise Decrease: Starting with a high concentration of code and tapering it off.
Datasets
The study utilized a massive breadth of data for both training and evaluation to ensure robust results.
- Pre-training: The corpus included 100GB of cleaned natural language text (Common Crawl, e-books, news, encyclopedia) and 50GB of Python code sourced from CodeParrot.
- Instruction-Tuning: The team curated a 400K-sample dataset. Half (200K samples) came from natural language sources like Alpaca-GPT-4 and PromptCLUE, while the remaining 150K samples were code-specific, drawn from CodeAlpaca, CosQA, and MBPP.
- Evaluation Benchmarks: To measure reasoning comprehensively, they evaluated the models on C-Eval (Logic), JEC-QA (Legal), ScienceQA (Scientific), E-KAR (Analogy), and MBPP (Code Generation).
Key Insights and Ideas
- Code as a "Logic Primer": Code is inherently structured, rigorous, and unambiguous compared to messy human language. The authors argue that pre-training on code "primes" the model’s internal weights to handle long-range dependencies and step-by-step logic.
- The Pre-training Advantage: Adding code during pre-training improves reasoning across the board—even in non-coding fields like law and science—with almost no "negative transfer" (harm) to standard language tasks.
- The "Stepwise Decrease" Secret: A surprising finding was that during instruction-tuning, starting with a higher ratio of code and gradually reducing it yielded the best results. This suggests that code acts as a "catalyst" that activates the model's reasoning capabilities early in the tuning process.
- Synergy with Chain-of-Thought (CoT): Models pre-trained with code were significantly better at utilizing CoT prompting. They didn't just arrive at the right answer more often; they were demonstrably better at following the "thinking chain" provided in the prompt.
Key Results
- Efficiency: The CODE (2.6B) model, despite being five times smaller, outperformed the NL (13B) text-only model on several complex reasoning tasks, including Legal QA and Scientific reasoning.
- Generalization: On the MBPP code generation task, the mixed pre-trained model achieved a 5.06 BLEU score, while the text-only model barely registered a 0.52.
- Instruction Tuning: Using the "Stepwise Decrease" strategy during tuning led to the highest scores in Logical Reasoning (40.90) and Code QA (52.48).
- Minimal Trade-offs: While adding code can slightly degrade performance on simple reading comprehension (like DuReader), the substantial gains in complex reasoning far outweigh these minor losses.
Limitations and Future Work
The authors acknowledge a few hurdles and areas for future exploration:
- Model Scale: The study focused on 2.6B and 13B parameter models. While substantial, the "emergent properties" of reasoning often shift dramatically at the 70B+ parameter scale.
- Language Diversity: The code data was primarily Python. Future work could explore if different programming paradigms (e.g., functional vs. object-oriented languages) impact reasoning differently.
- Task Interference: There remains some slight confusion in the model when generating answers for simple reading tasks after being heavily tuned on code, suggesting a need for more refined data balancing techniques to prevent "forgetting" how to handle simple prose.
Summary updated