备案 网站语言,网站网站做员工犯法吗,组织建设一百年心得体会,外包加工官网1. 引入
TinyZero#xff08;参考1#xff09;是伯克利的博士生复现DeepSeek-R1-Zero的代码参仓库#xff0c;他使用veRL来运行RL强化学习方法#xff0c;对qwen2.5的0.5B、1.5B、3B等模型进行训练#xff0c;在一个数字游戏数据集上#xff0c;达到了较好的推理效果。 …1. 引入
TinyZero参考1是伯克利的博士生复现DeepSeek-R1-Zero的代码参仓库他使用veRL来运行RL强化学习方法对qwen2.5的0.5B、1.5B、3B等模型进行训练在一个数字游戏数据集上达到了较好的推理效果。
下面解读源码中的关键训练逻辑细节。
2. 训练过程
原始数据
原始数据来自参考2一共490k条数据数据中只有两个字段格式如下
{nums: [ 95, 11, 56 ],target:28
}这是一个数字游戏要求对nums中的数据进行基础数学运算(, -, *, /)每个数字只能用一次最终结果等于target的值。比如上例子95-11-5628。
数据处理
具体源码见参考3下文仅仅解析关键步骤
1训练集和测试集大小
默认值如下
parser.add_argument(--train_size, typeint, default327680)
parser.add_argument(--test_size, typeint, default1024)2对原始数据添加提示词
下面的dp就是一条原始数据参考2.1例子
def make_prefix(dp, template_type):target dp[target]# 取出目标numbers dp[nums]# 取出数字# 对于默认模型加的提示词如下if template_type base:This works for any base modelprefix fA 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.
User: Using the numbers {numbers}, create an equation that equals {target}. You can use basic arithmetic operations (, -, *, /) and each number can only be used once. Show your work in think /think tags. And return the final answer in answer /answer tags, for example answer (1 2) / 3 /answer.
Assistant: Let me solve this step by step.
think# 对于qwen-instruct模型加的提示词如下elif template_type qwen-instruct:This works for Qwen Instruct Modelsprefix f|im_start|system\nYou are a helpful assistant. You first thinks about the reasoning process in the mind and then provides the user with the answer.|im_end|\n|im_start|user\n Using the numbers {numbers}, create an equation that equals {target}. You can use basic arithmetic operations (, -, *, /) and each number can only be used once. Show your work in think /think tags. And return the final answer in answer /answer tags, for example answer (1 2) / 3 /answer.|im_end|\n|im_start|assistant\nLet me solve this step by step.\nthinkreturn prefix3对数据进行完整的处理增加提示词与reward等数据
如下函数中的example就是一条原始数据参考2.1例子。 def process_fn(example, idx):question make_prefix(example, template_typeargs.template_type) # 增加提示词见2.2.2solution {target: example[target],numbers: example[nums]}data {data_source: data_source, # 任务名称默认为countdownprompt: [{role: user,content: question, # 带有提示词的问题}],ability: math,reward_model: {style: rule,ground_truth: solution # 含有nums和target},extra_info: {split: split,index: idx,}}return data最终数据为含有prompt和reward_model等字段的json结构。
训练
从参考4的训练代码中摘取部分配置如下
python3 -m verl.trainer.main_ppo \
data.train_files$DATA_DIR/train.parquet \
data.val_files$DATA_DIR/test.parquet \
data.train_batch_size256 \
data.val_batch_size1312 \
data.max_prompt_length256 \
data.max_response_length1024 \
actor_rollout_ref.model.path$BASE_MODEL \
actor_rollout_ref.actor.optim.lr1e-6 \
actor_rollout_ref.actor.ppo_mini_batch_size128 \
actor_rollout_ref.actor.ppo_micro_batch_size8 \
actor_rollout_ref.rollout.log_prob_micro_batch_size8 \
actor_rollout_ref.rollout.tensor_model_parallel_size$ROLLOUT_TP_SIZE \
actor_rollout_ref.rollout.gpu_memory_utilization0.4 \
actor_rollout_ref.ref.log_prob_micro_batch_size4 \
critic.optim.lr1e-5 \
critic.model.path$BASE_MODEL \
critic.ppo_micro_batch_size8 \
algorithm.kl_ctrl.kl_coef0.001 \
trainer.logger[wandb] \
trainer.val_before_trainFalse \
trainer.default_hdfs_dirnull \
trainer.n_gpus_per_node$N_GPUS \
trainer.nnodes1 \
trainer.save_freq100 \
trainer.test_freq100 \
trainer.project_nameTinyZero \
trainer.experiment_name$EXPERIMENT_NAME \
trainer.total_epochs15 21 | tee verl_demo.log这条命令是一个典型的 Python 脚本调用用于训练一个基于 PPOProximal Policy Optimization 算法的模型。
用veRL进行训练参考5需要指定 数据、模型、超参数
1数据相关配置
data.train_files$DATA_DIR/train.parquet指定训练数据文件路径Parquet 格式。data.val_files$DATA_DIR/test.parquet指定验证数据文件路径。data.train_batch_size256训练时的批量大小batch size。data.val_batch_size1312验证时的批量大小。data.max_prompt_length256输入提示prompt的最大长度。data.max_response_length1024生成响应response的最大长度。2Actor 模型配置
actor_rollout_ref.model.path$BASE_MODEL指定 Actor 模型的预训练权重路径。actor_rollout_ref.actor.optim.lr1e-6Actor 模型的学习率。actor_rollout_ref.actor.ppo_mini_batch_size128PPO 算法中 Actor 的 mini-batch 大小。actor_rollout_ref.actor.ppo_micro_batch_size8PPO 算法中 Actor 的 micro-batch 大小。actor_rollout_ref.rollout.log_prob_micro_batch_size8Rollout 阶段计算 log probability 的 micro-batch 大小。actor_rollout_ref.rollout.tensor_model_parallel_size$ROLLOUT_TP_SIZERollout 阶段的张量并行大小用于分布式训练。actor_rollout_ref.rollout.gpu_memory_utilization0.4Rollout 阶段的 GPU 内存利用率。actor_rollout_ref.ref.log_prob_micro_batch_size4参考模型ref model计算 log probability 的 micro-batch 大小。3Critic 模型配置
critic.optim.lr1e-5Critic 模型的学习率。critic.model.path$BASE_MODEL指定 Critic 模型的预训练权重路径。critic.ppo_micro_batch_size8PPO 算法中 Critic 的 micro-batch 大小。4算法配置
algorithm.kl_ctrl.kl_coef0.001KL 散度Kullback-Leibler divergence的系数用于控制策略更新的稳定性。5训练器配置
trainer.logger[wandb]使用 Weights BiasesWandB作为日志记录工具。trainer.val_before_trainFalse在训练开始前不进行验证。trainer.default_hdfs_dirnullHDFS 目录未设置HDFS 是分布式文件系统。trainer.n_gpus_per_node$N_GPUS每个节点使用的 GPU 数量。trainer.nnodes1使用的节点数量单节点训练。trainer.save_freq100每 100 步保存一次模型。trainer.test_freq100每 100 步进行一次测试。trainer.project_nameTinyZeroWandB 项目名称。trainer.experiment_name$EXPERIMENT_NAME实验名称。trainer.total_epochs15总训练轮数epochs。训练效果
用强化学习的方法训练后能如下所示输出字段推理过程并给出最终结果字段。
3. 总结
通过具体的数据与处理训练过程来更好的理解DeepSeek-R1-Zero的强化学习训练方法。
4. 参考
项目https://github.com/Jiayi-Pan/TinyZero数据https://huggingface.co/datasets/Jiayi-Pan/Countdown-Tasks-3to4数据处理源码https://github.com/Jiayi-Pan/TinyZero/blob/main/examples/data_preprocess/countdown.py训练源码https://github.com/Jiayi-Pan/TinyZero/blob/main/scripts/train_tiny_zero.shveRLhttps://verl.readthedocs.io/en/latest/start/quickstart.html