TL;DR
MegaMath is a newly released, fully open-source mathematical pre-training corpus containing a massive 371 billion tokens across web text, code, and synthetic data. By introducing math-optimized extraction pipelines and rigorous filtering, the dataset delivers both unprecedented scale and top-tier quality, yielding a 15% to 20% absolute performance boost when training state-of-the-art models like Llama-3.2.
What Problem Does This Paper Address?
While frontier models like OpenAI's o1 and DeepSeek-R1 have shown that language models can excel at complex mathematical reasoning, their success heavily relies on massive, high-quality math pre-training data. Unfortunately, the open-source community faces a severe data bottleneck.
Existing general-purpose web scraping pipelines often inadvertently ruin mathematical content. They either over-filter documents by discarding pages with math tags, or their text extractors strip out crucial equations, symbols, and LaTeX formatting entirely. Furthermore, existing open math datasets are either limited in scale or kept partially proprietary. MegaMath addresses this gap by engineering a reproducible, high-fidelity pipeline to extract and synthesize math data at a multi-hundred-billion-token scale.
Methods
The authors developed a multi-pronged curation strategy spanning three data domains:
- MegaMath-Web (279B tokens): Instead of relying on pre-extracted text, the team re-processed raw HTML from 99 Common Crawl snapshots. They introduced custom DOM-parsing rules to explicitly convert MathML and KaTeX into standardized LaTeX. To balance computational cost and quality, they used a two-stage extraction pipeline: a fast but noisy extractor (Resiliparse) paired with a fastText classifier for initial filtering, followed by a slower, high-quality extractor (Trafilatura) and a refined fastText model for the final pass. A premium subset, MegaMath-Web-Pro, was further refined using LLM-based rewriting.
- MegaMath-Code (28.1B tokens): Code is vital for teaching models logical step-by-step reasoning. The authors filtered the massive Stack-V2 corpus down to 11 math- and science-heavy programming languages. They used a powerful LLM to score a subset of the data for educational value and math relevance, and then distilled this knowledge into a Small Language Model (SLM) to filter the rest of the corpus at scale.
- MegaMath-Synthetic (64.5B tokens): To augment the dataset, they generated three synthetic formats: refined Q&A pairs using an "Explain Like I'm 5" (ELI5) prompting style, non-Python code translated into Python, and interleaved text-code blocks. Crucially, synthetic code blocks were validated via actual execution, protected by an Abstract Syntax Tree (AST) pre-filter to prevent malicious or broken code from halting the pipeline.
Datasets
The paper introduces the MegaMath Collection, totaling 371.6B tokens (counted via the Llama-2 tokenizer):
- MegaMath-Web: 263.9B tokens
- MegaMath-Web-Pro: 15.1B tokens (high-quality subset)
- MegaMath-Code: 28.1B tokens
- MegaMath-Synthetic: 64.5B tokens (comprising Translated Code, Q&A, and Text&Code Blocks)
For evaluation, the authors utilized a robust suite of 10 benchmarks split into two categories:
- Core Set (Chain-of-Thought & Program-Aided Language modeling): GSM8K, MATH, ASDiv, SVAMP, and MAWPS.
- Extended Set: MMLU-STEM, TabMWP, MathQA, SAT, and OCW Courses.
Key Insights and Ideas
- Preserving Math Structure is Paramount: Standard text extractors degrade math performance. Explicitly parsing HTML DOM trees to preserve subscripts, superscripts, and LaTeX formatting provides a massive boost in downstream Chain-of-Thought (CoT) performance.
- The Code Mixing Sweet Spot: The authors found that keeping code data to less than 20% of the total mixture maximizes program-aided problem-solving (PAL) capabilities without degrading natural language reasoning.
- Quality Beats Raw Web Text: Token-for-token, synthetic data and LLM-refined text (like MegaMath-Web-Pro) drastically outperform raw web text. Refining QA pairs with an ELI5 prompt combined with an "information completeness" constraint yielded the highest evaluation scores.
Key Results
- State-of-the-Art Corpus Quality: In controlled 55B-token pre-training experiments, MegaMath-Web-Pro outperformed other leading open math corpora, including FineMath-4+ and InfiMM-WebMath, by over 4% on core benchmarks.
- Massive Downstream Gains: When continually pre-trained on a blended mixture of MegaMath, standard base models saw dramatic improvements. For instance, Llama-3.2-3B's performance surged by 15% to 20% across multiple benchmarks, achieving 56.2% on GSM8K and 25.1% on the notoriously difficult MATH benchmark.
Model | GSM8K | MATH | Core Avg.
-----------------------|--------|-------|-----------
Llama-3.2-3B (Base) | 30.1% | 9.2% | 46.6%
MegaMath-Llama-3.2-3B | 56.2% | 25.1% | 64.4%
Limitations and Future Work
- Granular Deduplication Challenges: The authors experimented with aggressive sentence-level and exact substring deduplication but found that it frequently disrupted text consistency and degraded model performance. They note that more sophisticated deduplication strategies are needed.
- Language Constraints: The current iteration of MegaMath is strictly focused on English documents, leaving multilingual mathematical optimization as an open direction for future work.
Summary updated