NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

MathCoder2: Better Math Reasoning from Continued Pretraining on Model-translated Mathematical Code

Zimu Lu, Aojun Zhou, Ke Wang, Houxing Ren, Weikang Shi, Junting Pan, Mingjie Zhan, Hongsheng Li

TL;DR

MathCoder2 introduces a novel "model-translation" method that converts natural language mathematical reasoning into verified, executable Python code. By pretraining on the resulting 19.2B-token MathCode-Pile, researchers created a family of models that significantly outperform their base versions (like Llama-3 and Mistral) on complex reasoning benchmarks.

What Problem Does This Paper Address?

While Large Language Models (LLMs) have improved at math, they often struggle with the precision required for complex, multi-step problems. Researchers have found that training on code helps because code is inherently logical and rigorous.

However, existing math-related code (largely scraped from GitHub) is often designed for engineering, machine learning, or signal processing rather than pure mathematical reasoning. Furthermore, this code usually lacks the natural language "connective tissue"—the explanations and context—that help a model understand why a specific computation is being performed. The authors aim to bridge this gap by creating a dataset where natural language reasoning and executable code are tightly coupled.

Methods

The core innovation is a two-stage pipeline for data curation and "model-translation":

MathCoder2 Data Generation Pipeline
  1. Data Curation & Filtering: The team gathered data from web pages (OpenWebMath, Common Crawl), textbooks, and synthetic sources. They used a "finer filtering" process involving fastText classifiers and LLM-based annotation (using Mixtral-8x7B) to prune irrelevant content, resulting in a high-quality 16.5B-token base.
  2. Model-Translation: This is the "secret sauce." Using Llama-3.1-70B-Instruct, the authors processed the base text to:
  3. Extract LaTeX expressions and the conditions required for them.
  4. Identify the expected result of the computation.
  5. Translate that reasoning step into a Python code snippet.
  6. Execution-Based Verification: To ensure quality, they didn't just take the model's word for it. They executed the generated Python code. Only snippets that ran without errors and produced the correct mathematical result were kept. This resulted in 2.7B tokens of high-fidelity, paired reasoning-and-code data.
  7. Continued Pretraining: They used this combined 19.2B-token corpus (MathCode-Pile) to perform continued pretraining on several base models (Llama-3-8B, DeepSeekMath-7B, Mistral-7B, and Code-Llama-7B).

Datasets

  • MathCode-Pile: A newly introduced 19.2B-token corpus. It consists of:
  • Filtered Web Data: 11.2B tokens (OpenWebMath and CC-En-math).
  • Synthetic Data: 2.2B tokens.
  • Math Textbooks: 1.4B tokens.
  • Existing Math Code: 1.7B tokens (from StarCoderData).
  • Translated Math Code: 2.7B tokens (the novel verified code-reasoning pairs).
  • Evaluation Benchmarks: The models were tested on GSM8K (school math), MATH (competition level), SAT-Math, OCW (college level), and MMLU-Math.

Key Insights and Ideas

  • Code as Structured Reasoning: The authors argue that code shouldn't just be a tool used at inference time; it should be part of the model's "thought process" during pretraining. By seeing how a LaTeX formula translates into a Python function, the model learns a more robust internal representation of logic.
  • The Power of Verification: By filtering out code that didn't execute correctly, the authors ensured the pretraining data was "grounded." This prevents the model from learning the "hallucinated" logic often found in raw synthetic datasets.
  • Synergy of Text and Code: Ablation studies showed that concatenating natural language reasoning steps with their corresponding code snippets performed better than using either alone. This suggests that the "bilingual" nature of the data (Math + Python) is key to the performance boost.

Key Results

The MathCoder2 family showed dramatic improvements over their base models across multiple benchmarks:

Performance comparison of MathCoder2 against base models
  • MathCoder2-Llama-3-8B: Achieved 38.4% on MATH (a massive +17% jump from the base model) and 69.9% on GSM8K (+15.1% improvement).
  • MathCoder2-Mistral-7B: Saw its MATH score rocket from 13.1% to 36.7%.
  • SAT-Math: The Llama-3-8B variant improved its SAT-Math score by 28.1%, reaching an impressive 84.4%.
  • Generalization: Interestingly, despite the math focus, the models also improved on general coding benchmarks like HumanEval and MBPP, proving that mathematical rigor translates well to general programming.

Limitations and Future Work

  • Domain Specificity: The current corpus is focused strictly on mathematics. The authors acknowledge that extending this to other STEM subjects (Physics, Chemistry, Engineering) is a logical next step.
  • Language Barrier: The dataset is currently entirely in English. Future iterations could incorporate multi-lingual math data.
  • Model Scale: The study focused on 7B and 8B parameter models. Testing this pipeline on much larger models (e.g., 70B+) remains for future research.
  • Formal Verification: While Python execution is a great sanity check, the authors suggest that integrating formal theorem-proving languages like Lean or Coq could provide even higher levels of mathematical certainty.

Summary updated

BibTeX
@inproceedings{lu2025mathcoder,
  author = {Zimu Lu and Aojun Zhou and Ke Wang and Houxing Ren and Weikang Shi and Junting Pan and Mingjie Zhan and Hongsheng Li},
  title = {{MathCoder2: Better Math Reasoning from Continued Pretraining on Model-translated Mathematical Code}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=1Iuw1jcIrf},
}
Figure