实现声明,本次记录是在1.5B/3B模型上的实验踩坑记录,比如出现各种奇奇怪怪的情况。。。。。
总体分为两个大节,踩坑和效果对比
实验配置:
LLMRL框架:OpenRLHF
模型:Qwen2.5-1.5/3B-Base/Instruct
训练数据:MATH(7.5k)
指令的格式是参考orz的think\answer格式:
"A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant first thinks about the reasoning process in the mind and then provides the user with the answer. The reasoning process and answer are enclosed within and tags, respectively, i.e., reasoning process here answer here "
以及加上,方便answer提取答案
"Please put your final answer within \boxed{}."
OpenRLHF里面修改是prompts_dataset.py文件
奖励函数:
accuracy_reward: 使用 math_verify 做答案抽取和匹配
format_reward: 借鉴openr1的,pattern =r"^<think>.*?</think><answer>.*?</answer>$"
踩坑
format格式奖励问题
模型:Qwen2.5-1.5B-base
数据:MATH数据集取十分之一(较为简单,先看一下什么情况)
基础超参:
train_batch_size=16,32,64
rollout_size=16
n_generate=6
lr=1e-6
kl_coef=1e-3
accuracy_rewar和response在增长,形式还是很不错的。但引出了我们的第一个问题,我的format去哪里了? 太低了,怎么还降低了?这是怎么回事。
先调取一下模型训练过程中的response,其中部分是没有think和answer的,但也有部分是拥有和的过程,
To find the number of zeros in the expansion of $999,999,999,998^2$, we first need to understand the structure of $999,999,999,998^2$.
Let's denote $n \= 999,999,999,998$. Then, we need to find $n^2$.
By squaring $n \= 999,999,999,998$, we arrive at the expression $n^2 \= (10^{11} \- 2\)^2$, which expands to $10^{22} \- 20 \\cdot 10^{11} \+ 4$.
We need to determine how many zeros appear in this expansion. Notice that the term $20 \\cdot 10^{11}$ contributes to the formation of 11 zeros in the resulting number, because each '20' term creates a '0' followed by a '0'. The remaining term '4' contributes a single zero.
Therefore, the total number of zeros is $(\\text{number of zeros in } 10^{11}) \+ 1 \= 11 \+ 1$.
The number of zeros in the expansion of $999,999,999,998^2$ is 12\.
To determine how many cubic feet are in one cubic yard, we need to consider the conversion factors between yards and feet.\\n\\n 1 yard is equal to 3 feet.\\n\\n To create a cubic yard, we need dimensions that are each 3 feet long, wide, and high.\\n\\n The volume of one cubic yard is calculated by multiplying the dimensions:\\n\\\[ \\text{Volume of one cubic yard} \= \\text{length} \\times \\text{width} \\times \\text{height} \\]\\n\\\[ \\text{Volume of one cubic yard} \= 3 \\, \\text{feet} \\times 3 \\, \\text{feet} \\times 3 \\, \\text{feet} \\]\\n\\n Therefore, the volume of one cubic yard is \\( 27 \\times 1 \\) cubic feet. Thus, there are 27 cubic feet in one cubic yard.
可以看出,确实可以生成出和的过程,但是由于我们的reward_func中pattern = r"^<think>.*?</think><answer>.*?</answer>$"的严格限制:要求response必须开始(^)结尾是,和之间没有其他的内容。很难在探索或者采样的时候获取反馈。接下来我们进行修改一下pattern,不断格式的限制。
(1)逐步,或者说tag_count。 正确放置 前x个tag 得到 0.25 *x 的奖励 (注: 正确放置指多个tag顺序正确,且每个tag 只出现一次)
(2)宽松。pattern =r"<think>.*?</think><answer>.*?</answer>",允许response不是开始(^)结尾是,可以多次思考
均可以相对改善format_reward,但仍各有侧重,接下来选择逐步进行优化
效果还是不错
那如果我们换成Instruct模型?直接format的效果就很好,不需要适配。
想必3B-Instruct模型也是如此吧?。。。小寄,怎么3B的-Instruct又坏掉了?
format仍然出现问题,检查输出,发现Instruct模型可能是由于对齐的原因,喜欢将之间加一个\n,导致奖励获取失败。修改pattern, pattern = r"^<think>.*?</think>\n<answer>.*?</answer>$"
3B-Base模型 Basemodel没有经过之前的对齐操作,所以正常的format就可以,加上\n的反而不正常。。
return 在0附近
之前我们提到过,使用的是GRPO的reward—normalization,因此, 因此,return的平均也是0左右
policy loss有正有负,呈现上升趋势
在这片文章中进行了解释,https://zhuanlan.zhihu.com/p/1887453157099037961
呈现上升趋势是因为KL散度作为loss的原因,训练模型,因此KL_loss就变大了。policy loss有负是因为OpenRLHF框架下的误差,以及token-level-loss的使用。
训练分阶段问题
小模型训练中因为一些超参原因总会导致一些奇奇怪怪的情况,比如我们的下图所示length长度出现非正常暴增
我们将训练过程大概可以分为3个阶段。
(一)Base model 学习 形式,原有的输出格式变成特定格式。追求format奖励,而不是我们希望的深度思考。长度下降,一般在长度的最低点处format奖励达到极值。同时我们可以看到KL散度区域出现波动,学习到的format形式的原因
(二)Base model 正常发展阶段,适应在format形式下问题回答,长度回归正常,并逐渐提高回答能力。
(三)Base model (重复,一味的追求长度的输出??重复一些句子,但也不是重复,format奖励也受阻)
通过调整超参能够减弱上述的问题,只能说小模型各有各的蛋疼之处吧
效果对比
模型:Qwen-3B-Base
训练数据:MATH(7.5k)
测试数据:MATH500,GAOKAO23,AIME,AMC
train_batch=512
rollout_batch=64
n_generate=8
num_episode=1
结果表格如下,我们提供了Qwen2.5-3B-Base的效果表格(顺便说一下,Simple-R1的基线模型效果感觉是有问题的,根本没那低吧)
感觉可能step太少的原因,不是很明显,但还行吧。
发现没有强制的format奖励下,模型效果表现更好-也可能是训练步骤原因。
之后进行 仍然存在一些问题:
KL散度经常爆炸
token-level-loss下不同长度下梯度计算不稳定。