构建DeepSeekMath语料库的迭代流程:本文概述了从 Common Crawl 构建 DeepSeekMath 语料库的过程。如图2所示,这是一个迭代流程,展示了如何从一个种子语料库(例如,一个小型但高质量的数学相关数据集)开始,系统地从 Common Crawl 中收集大规模数学语料库。该方法同样适用于编码等其他领域。
图2 | 从 Common Crawl 收集数学网页的迭代流程。
初始数据收集与分类器训练:首先,选择高质量数学网络文本集合 OpenWebMath 【【35】,OpenWebMath: An open dataset of high-quality mathematical web text,2023】作为初始种子语料库。利用这个语料库,训练一个 fastText 模型【【22】,Fasttext.zip: Compressing text classification models,2016】来召回更多类似 OpenWebMath 的数学网页。具体地,从种子语料库中随机选择50万个数据点作为正向训练样本,并从 Common Crawl 中选择另外50万个网页作为负向样本。使用一个开源库进行训练,配置向量维度为256,学习率为0.1,词n-gram的最大长度为3,词出现的最小次数为3,训练轮数为3。为了减小原始 Common Crawl 的规模,采用了基于URL的去重和近去重技术,最终得到400亿个HTML网页。然后,使用 fastText 模型从去重后的 Common Crawl 中召回数学网页。为了过滤掉低质量的数学内容,根据 fastText 模型预测的分数对收集到的页面进行排序,并只保留排名靠前的页面。保留的数据量通过对前40B、80B、120B和160B词元进行预训练实验来评估。在第一次迭代中,选择保留前40B个词元。
迭代优化与语料库扩充:在第一次数据收集迭代后,由于 fastText 模型训练所用的正样本集多样性不足,许多数学网页仍未被收集。因此,通过识别更多的数学网页来源来丰富种子语料库,从而优化 fastText 模型。具体操作是,首先将整个 Common Crawl 组织成不相交的域(domain),一个域定义为共享相同基础URL的网页。对于每个域,计算在第一次迭代中被收集的网页百分比。网页收集比例超过10%的域被归类为数学相关域(例如,mathoverflow.net)。随后,在这些已识别的域中手动标注与数学内容相关的URL(例如,mathoverflow.net/questions)。与这些URL相关但未被收集的网页将被添加到种子语料库中。这种方法能够收集更多的正样本,从而训练出更优的 fastText 模型,在后续迭代中召回更多的数学数据。经过四轮数据收集迭代,最终得到3550万个数学网页,总计1200亿词元。在第四次迭代中,注意到近98%的数据已在第三次迭代中被收集,因此决定停止数据收集。
基准测试污染清理:为避免基准测试污染,遵循 Guo 等人【【15】,Deepseek-coder: When the large language model meets programming – the rise of code intelligence,2024】的方法,过滤掉包含英文数学基准(如GSM8K【【9】,Training verifiers to solve math word problems,2021】和MATH【【17】,Measuring mathematical problem solving with the math dataset,2021】)以及中文基准(如CMATH【【57】,Cmath: Can your language model pass chinese elementary school math test?,2023】和AGIEval【【64】,AGIEval: A human-centric benchmark for evaluating foundation models,2023】)中的问题或答案的网页。过滤标准如下:任何包含与评估基准中任意子字符串完全匹配的10-gram字符串的文本段都将从数学训练语料库中移除。对于长度小于10-gram但至少有3-gram的基准文本,采用精确匹配来过滤受污染的网页。
MathPile【【55】,Generative AI for math: Part I - mathpile: A billion-token-scale pretraining corpus for math,2023c】:一个多源语料库(89亿词元),整合了教科书、维基百科、ProofWiki、CommonCrawl、StackExchange和arXiv的内容,其中超过85%来自arXiv。
OpenWebMath【【35】,OpenWebMath: An open dataset of high-quality mathematical web text,2023】:从CommonCrawl筛选的数学内容数据,总计136亿词元。
Proof-Pile-2【【3】,Llemma: An open language model for mathematics,2023】:一个由OpenWebMath、AlgebraicStack(103亿词元的数学代码)和arXiv论文(280亿词元)组成的数学语料库。实验中遵循其论文建议,使用arXiv:Web:Code为2:4:1的混合比例。
2.2.1. 训练设置
实验模型与框架:在一个与DeepSeek LLM【【11】,Deepseek LLM: scaling open-source language models with longtermism,2024】框架相同的13亿参数通用预训练语言模型(表示为DeepSeekLLM 1.3B)上进行数学训练。针对每种数学语料库,分别训练一个模型,训练量为1500亿词元。所有实验均使用高效轻量的HAI-LLM【【18】,Hai-llm: 效且轻 的 型训练工具,2023】训练框架。
高质量:使用少样本思维链提示【【56】,Chain-of-thought prompting elicits reasoning in large language models,2022】在8个数学基准上评估下游性能。如表1所示,在DeepSeekMath语料库上训练的模型性能明显领先。图3显示,在处理了500亿词元(相当于Proof-Pile-2的1个完整epoch)时,该模型表现优于在Proof-Pile-2上训练的模型,表明DeepSeekMath语料库的平均质量更高。
模型训练设置:本节介绍DeepSeekMath-Base 7B,一个具有强大推理能力(尤其在数学方面)的基础模型。该模型使用DeepSeek-Coder-Base-v1.5 7B【【15】,Deepseek-coder: When the large language model meets programming – the rise of code intelligence,2024】进行初始化,并训练了5000亿词元。数据分布如下:56%来自DeepSeekMath语料库,4%来自AlgebraicStack,10%来自arXiv,20%是Github代码,其余10%是来自Common Crawl的中英文自然语言数据。主要采用2.2.1节中指定的训练设置,但将学习率最大值设为4.2e-4,批处理大小设为1000万词元。
逐步推理的数学问题解决:使用少样本思维链提示【【56】,Chain-of-thought prompting elicits reasoning in large language models,2022】评估了DeepSeekMath-Base在八个中英文基准上的数学解题性能。这些基准涵盖了定量推理(如GSM8K【【9】,Training verifiers to solve math word problems,2021】、MATH【【17】,Measuring mathematical problem solving with the math dataset,2021】和CMATH【【57】,Cmath: Can your language model pass chinese elementary school math test?,2023】)和多项选择题(如MMLU-STEM【【16】,Measuring massive multitask language understanding,2020】和Gaokao-MathQA【【64】,AGIEval: A human-centric benchmark for evaluating foundation models,2023】),覆盖了从小学到大学水平的多个数学领域。
逐步推理评估结果:如表2所示,DeepSeekMath-Base 7B在所有八个基准上均领先于开源基础模型(包括广泛使用的通用模型Mistral 7B【【21】,Mistral 7b,2023】和近期发布的、在Proof-Pile-2上进行数学训练的Llemma 34B【【3】,Llemma: An open language model for mathematics,2023】)。值得注意的是,在竞赛级别的MATH数据集上,DeepSeekMath-Base比现有开源基础模型高出超过10个绝对百分点,并超过了规模大77倍的闭源基础模型Minerva 540B【【25】,Solving quantitative reasoning problems with language models,2022a】,后者基于PaLM【【26】,Solving quantitative reasoning problems with language models,2022b】并在数学文本上进行了进一步训练。
使用工具的数学问题解决:在GSM8K和MATH上,使用少样本思路编程提示(program-of-thought prompting)【【8】,Program of thoughts prompting: Disentangling computation from reasoning for numerical reasoning tasks,2022;【13】,PAL: programaided language models,2023】评估了程序辅助的数学推理能力。模型被提示通过编写Python程序来解决每个问题,其中可以利用math和sympy等库进行复杂计算。程序的执行结果被评估为答案。如表3所示,DeepSeekMath-Base 7B的性能超过了之前的最先进模型Llemma 34B。
形式化数学:形式化证明自动化有助于确保数学证明的准确性和可靠性,并提高效率,近年来受到越来越多的关注。我们在非正式到正式的证明任务【【20】,Draft, sketch, and prove: Guiding formal theorem provers with informal proofs,2022】上评估了DeepSeekMath-Base 7B,该任务是根据一个非正式陈述、该陈述的形式化对应物以及一个非正式证明来生成一个形式化证明。我们在miniF2F【【63】,Minif2f: a cross-system benchmark for formal olympiad-level mathematics,2021】(一个奥林匹克级别的形式化数学基准)上进行评估,并使用少样本提示为每个问题生成Isabelle中的形式化证明。遵循Jiang等人【【20】,Draft, sketch, and prove: Guiding formal theorem provers with informal proofs,2022】的方法,我们利用模型生成证明草图,并执行现成的自动证明器Sledgehammer【【36】,Three years of experience with sledgehammer, a practical link between automatic and interactive theorem provers,2010】来填补缺失的细节。如表3所示,DeepSeekMath-Base 7B在证明自动形式化方面表现出强大的性能。
自然语言理解、推理和代码能力:在MMLU【【16】,Measuring massive multitask language understanding,22】上评估模型的自然语言理解能力,在BBH【【46】,Challenging big-bench tasks and whether chain-of-thought can solve them,2022】上评估推理能力,在HumanEval【【7】,Evaluating large language models trained on code,2021】和MBPP【【2】,Program synthesis with large language models,2021】上评估编码能力。如表4所示,DeepSeekMath-Base 7B在其前身DeepSeek-Coder-Base-v1.5【【15】,Deepseek-coder: When the large language model meets programming – the rise of code intelligence,2024】的基础上,在MMLU和BBH上的性能有显著提升,表明数学训练对语言理解和推理有积极影响。此外,通过在持续训练中加入代码词元,DeepSeekMath-Base 7B有效地保持了DeepSeek-Coder-Base-v1.5在两个编码基准上的性能。总体而言,DeepSeekMath-Base 7B在三个推理和编码基准上显著优于通用模型Mistral 7B【【21】,Mistral 7b,2023】。
数据集构成:构建了一个数学指令微调数据集,涵盖了不同数学领域和不同复杂程度的中英文问题。这些问题与思维链(CoT)【【56】,Chain-of-thought prompting elicits reasoning in large language models,2022】、思路编程(PoT)【【8】,Program of thoughts prompting: Disentangling computation from reasoning for numerical reasoning tasks,2022;【13】,PAL: programaided language models,2023】以及工具集成推理格式【【14】,Tora: A toolintegrated reasoning agent for mathematical problem solving,2023】的解决方案配对。训练样本总数为77.6万。
英文数学数据集:为GSM8K和MATH问题标注了工具集成的解决方案,并采用了MathInstruct【【62】,Mammoth: Building math generalist models through hybrid instruction tuning,2023】的一个子集以及Lila-OOD【【30】,LILA: A unified benchmark for mathematical reasoning,2022】的训练集,其中问题通过CoT或PoT解决。英文数据集涵盖了代数、概率、数论、微积分和几何等多个数学领域。
强化学习的有效性:强化学习(RL)已被证明在监督微调(SFT)阶段之后能有效进一步提升大语言模型(LLM)的数学推理能力【【29】,Wizardmath: Empowering mathematical reasoning for large language models via reinforced evol-instruct,2023;【54】,Math-shepherd: Verify and reinforce llms step-by-step without human annotations,2023b】。本节介绍了我们高效且有效的RL算法——组相对策略优化(Group Relative Policy Optimization, GRPO)。
4.1.1. 从PPO到GRPO
PPO算法回顾:近端策略优化(PPO)【【42】,Proximal policy optimization algorithms,2017】是一种在LLM的RL微调阶段广泛使用的演员-评论家(actor-critic)RL算法【【34】,Training language models to follow instructions with human feedback,2022】。它通过最大化以下代理目标来优化LLM:
公式1
其中 $\pi_{\theta}$ 和 $\pi_{\text{old}}$ 分别是当前和旧的策略模型,$x, y$ 分别是从问题数据集和旧策略 $\pi_{\text{old}}$ 中采样的问题和输出。$\epsilon$ 是PPO中为稳定训练引入的与裁剪相关的超参数。$A_{t}$ 是优势(advantage),通过广义优势估计(GAE)【【41】,High-dimensional continuous control using generalized advantage estimation,2015】计算得出,基于奖励 {$r_{\ge t}$} 和一个学习到的价值函数 $V_{\phi}$。因此,在PPO中,需要与策略模型一同训练一个价值函数,并且为了减轻对奖励模型的过度优化,标准方法是在每个词元(token)的奖励中加入一个来自参考模型的逐词元KL惩罚项【【34】,Training language models to follow instructions with human feedback,2022】,即:
训练设置:我们基于DeepSeekMath-Instruct 7B进行RL。RL的训练数据是来自SFT数据中与GSM8K和MATH相关的思维链格式问题,共约14.4万个问题。我们排除了其他SFT问题,以研究RL对在RL阶段缺乏数据的基准的影响。我们遵循Wang等人【【54】,Math-shepherd: Verify and reinforce llms step-by-step without human annotations,2023b】的方法构建奖励模型的训练集。我们在DeepSeekMath-Base 7B的基础上以2e-5的学习率训练初始奖励模型。对于GRPO,策略模型的学习率设为1e-6,KL系数为0.04。对每个问题,我们采样64个输出。最大长度设为1024,训练批次大小为1024。策略模型在每个探索阶段后仅进行单次更新。
背景:ArXiv论文通常被用作数学预训练数据的一部分【【3】,Llemma: An open language model for mathematics,2023;【25】,Solving quantitative reasoning problems with language models,2022a;【37】,Generative language modeling for automated theorem proving,2020;【55】,Generative AI for math: Part I - mathpile: A billion-token-scale pretraining corpus for math,2023c】,但关于它们对数学推理影响的详细分析尚未广泛进行。
实验设计:根据我们的实验,ArXiv论文在提升数学推理方面似乎无效。我们使用不同规模的模型,包括DeepSeek-LLM 1.3B和DeepSeek-Coder-Base-v1.5 7B【【15】,Deepseek-coder: When the large language model meets programming – the rise of code intelligence,2024】,并使用了经过不同处理流程的ArXiv语料库:
MathPile【【55】,Generative AI for math: Part I - mathpile: A billion-token-scale pretraining corpus for math,2023c】:一个89亿词元的语料库,通过清洗和过滤启发式规则开发,其中超过85%是科学ArXiv论文。
ArXiv-RedPajama【【10】,Redpajama: an open dataset for training large language models,2023】:全部ArXiv LaTeX文件,移除了序言、注释、宏和参考文献,总计280亿词元。