做亚马逊外国网站需要语言好吗,h5制作模板免费下载,wordpress固定连接重,福州公司网站建设一定要用主流程序php语言目录 1 配置环境2 训练2.1 命令和配置参数2.2 num_generations2.2.1 参数定义2.2.2 参数含义2.2.3 示例2.2.4 使用场景2.2.5 示例代码 2.3 显存占用和耗时 3 结果 1 配置环境
关于环境配置#xff0c;可以参考这篇博文#xff1a;【复现DeepSeek-R1之Open R1实战】系列1可以参考这篇博文【复现DeepSeek-R1之Open R1实战】系列1跑通SFT一步步操作手把手教学
关于flash-attention依赖库的安装问题运行以下命令等待一小时左右依赖库就安装成功了
pip install flash-attn --no-cache-dir2 训练
2.1 命令和配置参数
训练的命令如下和SFT差不多
ACCELERATE_LOG_LEVELinfo accelerate launch --config_file recipes/accelerate_configs/zero2.yaml \--num_processes7 src/open_r1/grpo.py \--config /nfs/ofs-902-1/fusion/zhongyudong/open-r1/recipes/Qwen2.5-1.5B-Instruct/grpo/config_demo.yaml我们需要修改config配置文件recipes/Qwen2.5-1.5B-Instruct/grpo/config_demo.yaml主要是将和Huggingface的链接关掉修改模型路径、数据集路径、GPU个数num_processesGPU个数-1因为vLLM使用了一张卡。
在训练过程中我发现torch的DDP不稳定容易接收不到Worker的信号导致训练失败所以保存策略改成了每步都保存save_strategy: “steps”。
完整的配置如下
# Model arguments
model_name_or_path: /nfs/ofs-902-1/pnc/huggingface_hub/Qwen/Qwen2.5-1.5B-Instruct
# model_revision: main
torch_dtype: bfloat16
attn_implementation: flash_attention_2# Data training arguments
dataset_name: /nfs/ofs-902-1/fusion/zhongyudong/open-r1/datas/NuminaMath-TIR/data
dataset_configs:
- all
# Num processes is less by 1 as vLLM is using 1 GPU
num_processes: 7# GRPO trainer config
bf16: true
use_vllm: true
vllm_device: auto
vllm_gpu_memory_utilization: 0.7
do_eval: true
eval_strategy: steps
eval_steps: 100
gradient_accumulation_steps: 16
gradient_checkpointing: true
gradient_checkpointing_kwargs:use_reentrant: false
# hub_model_id: Qwen2.5-1.5B-Open-R1-GRPO
# hub_strategy: every_save
learning_rate: 2.0e-05
log_level: info
logging_steps: 5
logging_strategy: steps
lr_scheduler_type: cosine
max_prompt_length: 512
max_completion_length: 1024
max_steps: -1
num_generations: 7
num_train_epochs: 1
output_dir: /nfs/ofs-902-1/fusion/zhongyudong/open-r1/outputs/Qwen2.5-1.5B-Open-R1-GRPO
overwrite_output_dir: true
per_device_eval_batch_size: 32
per_device_train_batch_size: 16
push_to_hub: false
# report_to:
# - wandb
save_strategy: steps
seed: 42
warmup_ratio: 0.1
重点解释一下num_generations这个参数主要是控制每个提示Prompt生成的样本数量。
2.2 num_generations
参数 num_generations 用于指定每个提示prompt生成的样本数量。这个参数在生成模型中非常常见特别是在文本生成、对话系统或其他需要从模型中采样多个输出的任务中。以下是对该参数及其使用场景的详细解释
2.2.1 参数定义
num_generations (int or None, *optional*, defaults to 8):Number of generations per prompt to sample. The global batch size (num_processes * per_device_batch_size)must be divisible by this value.类型: 可以是整数int或 None。默认值: 默认为 8。可选性: 是一个可选参数。
2.2.2 参数含义 生成数量: num_generations 指定了对于每一个输入的提示prompt模型将生成多少个不同的输出样本。例如如果你设置 num_generations3那么对于每一个输入提示模型会生成3个不同的输出。 全局批处理大小的约束: 全局批处理大小global batch size是指所有进程和设备上批处理大小的总和通常计算为 num_processes * per_device_batch_size。这个全局批处理大小必须能够被 num_generations 整除。也就是说global_batch_size % num_generations 0 必须成立。这个约束确保了在分布式训练或多设备环境中每个设备上的生成任务可以均匀分配。
2.2.3 示例
假设你有以下配置
per_device_batch_size 4num_processes 2即你在使用两个GPU或其他并行计算单元num_generations 8
在这种情况下
全局批处理大小为 global_batch_size num_processes * per_device_batch_size 2 * 4 8因为 global_batch_size 等于 num_generations所以条件满足。
如果我们将 num_generations 改为 6则
全局批处理大小仍然是 8但 8 % 6 ! 0这会导致错误因为无法均匀分配生成任务。
2.2.4 使用场景
文本生成
在文本生成任务中你可能希望从一个提示生成多个不同的输出以便选择最好的结果或者展示多样性。例如在故事生成或对话系统中生成多个候选答案可以让用户有更多的选择。
对话系统
在对话系统中生成多个回复可以帮助系统提供更丰富的互动体验。通过生成多个回复系统可以选择最合适的回答或者让用户选择他们喜欢的回答。
多模态生成
在多模态生成任务如图像字幕生成、视频描述等中生成多个输出可以提高生成内容的多样性和准确性。
2.2.5 示例代码
以下是一个简单的示例展示了如何使用 num_generations 参数
from transformers import AutoModelForCausalLM, AutoTokenizer# 加载预训练模型和分词器
model_name your-pretrained-model
model AutoModelForCausalLM.from_pretrained(model_name)
tokenizer AutoTokenizer.from_pretrained(model_name)# 定义输入提示
prompt Once upon a time# 将提示编码为模型输入格式
input_ids tokenizer(prompt, return_tensorspt).input_ids# 设置生成参数
num_generations 5 # 每个提示生成5个样本# 生成多个样本
outputs model.generate(input_ids,num_return_sequencesnum_generations, # 设置num_generationsmax_length50,do_sampleTrue
)# 解码生成的样本
for i, output in enumerate(outputs):print(fGenerated text {i1}:)print(tokenizer.decode(output, skip_special_tokensTrue))print()在这个例子中num_return_sequences 参数对应于 num_generations它指定了要生成的样本数量。
2.3 显存占用和耗时
8卡H20每张卡占用40~50G。
要跑将15个多小时。
3 结果
一些中间结果