Skip to content

Commit 6aae524

Browse files
authored
add geoqa grpo experiment (#3344)
1 parent a2459b8 commit 6aae524

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed
655 KB
Loading

docs/resources/grpo_geoqa.png

2.13 MB
Loading

docs/source/BestPractices/GRPO多模态训练.md

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# GRPO完整实验流程
2-
本文介绍如何使用swift GRPO进行多模态模型和任务的训练。目标是对多个多模态任务进行训练,提升任务精度,任务定义和训练参数等参考了 [R1-V](https://github.com/Deep-Agent/R1-V.git)[open-r1-multimodal](https://github.com/EvolvingLMMs-Lab/open-r1-multimodal.git)
2+
本文介绍如何使用SWIFT GRPO进行多模态模型和任务的训练。目标是对多个多模态任务进行训练,提升任务精度,任务定义和训练参数等参考了 [R1-V](https://github.com/Deep-Agent/R1-V.git)[open-r1-multimodal](https://github.com/EvolvingLMMs-Lab/open-r1-multimodal.git)
33

44

55

@@ -45,7 +45,7 @@ register_dataset(
4545
## 奖励函数定义:
4646
本任务使用的奖励函数有两个,一个是 Deepseek-R1 中提到的格式奖励函数,另一是 ClevrCount 的准确性奖励函数。前者已经在swift中内置,通过 `--reward_funcs format` 可以直接使用,而后者需要我们自己定义,在这里我们使用 external_plugin 的方式定义准确性奖励函数,将代码放在`swift/examples/train/grpo/plugin/plugin.py`中。
4747

48-
在这里,奖励函数的输入包括 completions和solution两个字段,分别表示模型生成的文本和真值。每个都是list,支持多个 completion 同时计算。注意,在这里,solution字段是数据集中定义的字段透传而来,如果有任务上的变动,可以分别对数据集和奖励函数做对应的改变即可。
48+
在这里,奖励函数的输入包括completions和solution两个字段,分别表示模型生成的文本和真值。每个都是list,支持多个completion同时计算。注意,在这里,solution字段是数据集中定义的字段透传而来,如果有任务上的变动,可以分别对数据集和奖励函数做对应的改变即可。
4949
```python
5050

5151
class MultiModalAccuracyORM(ORM):
@@ -93,9 +93,9 @@ class MultiModalAccuracyORM(ORM):
9393
orms['external_r1v_acc'] = MultiModalAccuracyORM
9494
```
9595

96-
## GRPO训练实验记录
97-
### 训练参数:
98-
我们选取 Qwen2.5-VL-3B-Instruct 作为基础模型进行训练,选取 Instruct 而不是基模的主要原因是可以更快地获取 format reward。我们在八卡 GPU 上进行实验。swift GRPO训练已支持多卡部署模型以加速rollout,因此我们设置num_infer_workers为2,进程数为6,即2卡部署,6卡训练。如果遇到vllm部署qwen2.5-vl报错,可以参考[issue](https://github.com/vllm-project/vllm/issues/13285)
96+
### GRPO训练实验记录
97+
#### 训练参数:
98+
我们选取 Qwen2.5-VL-3B-Instruct 作为基础模型进行训练,选取 Instruct 而不是基模的主要原因是可以更快地获取 format reward。我们在八卡 GPU 上进行实验。SWIFT GRPO训练已支持多卡部署模型以加速rollout,因此我们设置num_infer_workers为2,进程数为6,即2卡部署,6卡训练。如果遇到vllm部署qwen2.5-vl报错,可以参考[issue](https://github.com/vllm-project/vllm/issues/13285)
9999

100100
由于任务简单,我们设置max_completion_length为1024,奖励函数选择external_r1v_acc和format,学习率和beta分别设置为1e-6和0.001。其他设置如下所示,batch_size和num_generations的设置原则可以参考[GRPO完整流程](./GRPO完整流程.md)
101101

@@ -142,10 +142,10 @@ swift rlhf \
142142
--beta 0.001 \
143143

144144
```
145-
### 实验现象
145+
#### 实验现象
146146
![image.png](../../resources/grpo_clevr_count.png)
147147
由于数据集和任务比较简单,模型训练了500个epoch已经基本收敛,有以下观察。
148-
1. 自定义的ClevrORM在不断增加,证明模型学习到了如何完成这一任务,最终任务成功率从出事的0.4攀升到1左右。
148+
1. 自定义的ClevrORM在不断增加,证明模型学习到了如何完成这一任务,最终任务成功率从初始的0.4攀升到1左右。
149149
2. 由于是Instruct模型,Format Reward基本稳定在1,这可能也与数据集所有样本都是一样的query有一定关系。
150150
3. reward_std 稳定到0.1以下。
151151
4. completion length最终稳定在60-80,模型学习到的固定输出范式是逐个物体列举数数。
@@ -162,3 +162,73 @@ step 400:
162162
"<think> Counting the objects in the image:\n1. Large yellow metallic sphere\n2. Small green rubber sphere\n3. Small brown metallic cube\n\nThere are three distinct items in the image.\n</think>\n<answer>3</answer>"
163163
```
164164
总体来讲,这一任务比较简单,reward的收敛也比较典型。
165+
166+
## Geometric QA任务
167+
### 任务与数据集定义
168+
本任务为Geometric QA任务,任务描述为:给定一个几何图形,回答有关几何图形的数学问题。原始数据来自于[论文](https://arxiv.org/pdf/2312.11370)[R1-V](https://github.com/Deep-Agent/R1-V.git)对数据进行了预处理,将所有数据全部处理成了problem-solution的格式,而图像则保留在image字段中,因此,我们不需要额外定义数据集,直接使用`--dataset AI-ModelScope/GEOQA_R1V_Train_8K`即可。
169+
### 奖励函数
170+
由于也是数学题,同时,答案也处理成了最终结果,因此,我们直接使用以上定义过的`MultiModalAccuracyORM`奖励函数。
171+
### GRPO训练实验记录
172+
#### 训练参数:
173+
选取的模型和大部分超参数与上一个实验相似,主要有两点不同:
174+
1. SWIFT 已支持`--num_iteration`参数,单次rollout可以进行多次更新,这里设置为2。
175+
2. 在实验时发现,在数学问题中,训练可能会出现不稳定现象,导致模型训崩,具体表现为所有rewar迅速降低,loss、grad_norm和kl都迅速增大,后续也难以恢复正常状态。因此,这里设置`--max_grad_norm 0.5`,保证稳定训练,当然,这种现象的出现也有一定的随机性。
176+
177+
```shell
178+
WANDB_API_KEY=your_wandb_api_key \
179+
MAX_PIXELS=401408 \
180+
NPROC_PER_NODE=6 \
181+
swift rlhf \
182+
--rlhf_type grpo \
183+
--model Qwen/Qwen2.5-VL-3B-Instruct \
184+
--external_plugins examples/train/grpo/plugin/plugin.py \
185+
--reward_funcs external_r1v_acc format \
186+
--use_vllm true \
187+
--vllm_device auto \
188+
--vllm_gpu_memory_utilization 0.6 \
189+
--train_type full \
190+
--torch_dtype bfloat16 \
191+
--dataset 'AI-ModelScope/GEOQA_R1V_Train_8K' \
192+
--max_length 8192 \
193+
--max_completion_length 1024 \
194+
--num_train_epochs 1 \
195+
--per_device_train_batch_size 8 \
196+
--per_device_eval_batch_size 8 \
197+
--learning_rate 1e-6 \
198+
--gradient_accumulation_steps 2 \
199+
--save_strategy 'steps' \
200+
--eval_strategy 'steps' \
201+
--eval_steps 400 \
202+
--save_steps 400 \
203+
--save_total_limit 10 \
204+
--logging_steps 1 \
205+
--output_dir output/GRPO_GEOQA \
206+
--warmup_ratio 0.05 \
207+
--dataloader_num_workers 4 \
208+
--num_generations 8 \
209+
--temperature 1.0 \
210+
--repetition_penalty 1.1 \
211+
--system 'examples/train/grpo/prompt.txt' \
212+
--deepspeed zero3 \
213+
--log_completions true \
214+
--report_to wandb \
215+
--num_iterations 2 \
216+
--num_infer_workers 2 \
217+
--async_generate false \
218+
--beta 0.001 \
219+
--max_grad_norm 0.5 \
220+
```
221+
222+
#### 实验现象
223+
![image.png](../../resources/grpo_geoqa.png)
224+
训练曲线如上图所示。
225+
1. 相比于Count任务,Geometric QA任务的难度明显更大,因此,Accuracy Reward收敛明显更慢,同时震荡幅度也明显更大。
226+
2. completion_length的变化趋势比较明显,最终收敛到250 token左右。
227+
3. reward基本收敛,标准差在一共1300个step时稳定在0.2左右。
228+
以下是训练后的模型completition示例:
229+
```
230+
User:
231+
"If AB is the tangent to circle O with the tangent point A, and BO intersects circle O at point C, and point D is located on circle O, what is the measure of angle ADC if the degree of angle ABO is 32.0?"
232+
Assistant:
233+
"<think>\nTo find the measure of angle ADC, we first need to understand the properties of the tangent and the circle. Since AB is a tangent to the circle at point A, the line segment OA (which is perpendicular to the tangent AB) is perpendicular to AB. This means that angle OAB is 90 degrees.\n\nGiven that angle ABO is 32 degrees, we can find angle BAO by subtracting angle ABO from 90 degrees:\n∠BAO = 90° - ∠ABO = 90° - 32° = 58°\n\nSince angle BAO is an angle in the minor arc AD, the angle ADC, which is an angle in the minor arc AD, is half the angle BAO. Therefore, angle ADC is:\n∠ADC = 1/2 × ∠BAO = 1/2 × 58° = 29°\n\nSo, the measure of angle ADC is 29 degrees.\n</think>\n<answer>\nThe measure of angle ADC is 29 degrees.\n</answer>"
234+
```

0 commit comments

Comments
 (0)