Skip to content
Merged
Show file tree
Hide file tree
Changes from 97 commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
4316425
wip
hjh0119 Aug 29, 2025
5d46eae
init wip
hjh0119 Sep 1, 2025
5828229
args wip
hjh0119 Sep 1, 2025
a82cec4
Merge remote-tracking branch 'origin/main' into mega-grpo
hjh0119 Sep 2, 2025
0689b76
reuse _prepare_rollout_engine
hjh0119 Sep 3, 2025
46593cf
merge main
hjh0119 Sep 11, 2025
3da8756
mega wip
hjh0119 Sep 12, 2025
2ca7ac1
Merge remote-tracking branch 'origin' into mega-grpo
hjh0119 Sep 17, 2025
d9ec029
wip
hjh0119 Sep 17, 2025
7c56f9f
override train_step wip
hjh0119 Sep 17, 2025
686fc74
remove override train_step to grpo
hjh0119 Sep 18, 2025
095bcbd
Merge remote-tracking branch 'origin' into mega-grpo
hjh0119 Sep 18, 2025
4d9457b
sync weight wip
hjh0119 Sep 18, 2025
f52d5e1
rollout wip
hjh0119 Sep 19, 2025
155d4fb
Merge remote-tracking branch 'origin' into mega-grpo
hjh0119 Sep 22, 2025
3c69c39
modify mini_batch_size to generation batch size
hjh0119 Sep 22, 2025
eebdd47
wip
hjh0119 Sep 24, 2025
de6ecfe
loss wip
hjh0119 Sep 28, 2025
4569e54
fix repeat n
hjh0119 Sep 28, 2025
f118935
Merge remote-tracking branch 'origin' into mega-grpo
hjh0119 Sep 29, 2025
9cb84e3
fix padding to multiple of tp_size
hjh0119 Sep 29, 2025
8627aa3
compute loss
hjh0119 Sep 29, 2025
2292cf8
fix logps
hjh0119 Sep 30, 2025
bbe5f39
logging & patch VL
hjh0119 Sep 30, 2025
6a2940c
fix rollout_group & rollout judgement
hjh0119 Oct 1, 2025
486c3d4
fix step
hjh0119 Oct 6, 2025
7e8e6b0
merge main
hjh0119 Oct 6, 2025
c68d976
move old base trainer to newer
hjh0119 Oct 7, 2025
6b1653c
fix
hjh0119 Oct 8, 2025
d4a9dcc
offload utils
hjh0119 Oct 8, 2025
9dc92a0
offload context
hjh0119 Oct 9, 2025
7bc3d61
Resolve merge conflict in megatron_args.py by removing duplicate fiel…
hjh0119 Oct 9, 2025
91f97ca
fix resolve
hjh0119 Oct 9, 2025
59f436c
fix logps
hjh0119 Oct 9, 2025
8dea6d7
fix old logps
hjh0119 Oct 9, 2025
abac696
reduce redundancy
hjh0119 Oct 9, 2025
3a3ff37
replace token
hjh0119 Oct 10, 2025
2cd89dc
fix offload model
hjh0119 Oct 10, 2025
50d5e6f
offload optimizer & ref
hjh0119 Oct 11, 2025
e1a06c6
support cp
hjh0119 Oct 11, 2025
ff9b667
fix pp+cp
hjh0119 Oct 11, 2025
ba4bfbf
lora wip
hjh0119 Oct 11, 2025
e5a6252
Merge remote-tracking branch 'origin' into mega-grpo
hjh0119 Oct 13, 2025
e22c790
arguments document
hjh0119 Oct 13, 2025
b3de262
wip lora&cp
hjh0119 Oct 14, 2025
d5bd92c
merge origin
hjh0119 Oct 14, 2025
fe3270f
remove unused patch
hjh0119 Oct 14, 2025
137704e
merge main
hjh0119 Oct 29, 2025
ca9c9bc
wip server
hjh0119 Oct 29, 2025
f258202
wip
hjh0119 Oct 29, 2025
85a035e
Merge remote-tracking branch 'origin' into mega-grpo
hjh0119 Oct 29, 2025
0a38c0c
server rollout wip
hjh0119 Oct 30, 2025
e0fc2e9
Merge remote-tracking branch 'origin' into mega-grpo
hjh0119 Nov 4, 2025
5f2f349
move vllm client init out of args
hjh0119 Nov 4, 2025
416feb2
server mode
hjh0119 Nov 4, 2025
85135bb
merge main
hjh0119 Nov 4, 2025
b93c031
remove old func
hjh0119 Nov 4, 2025
2f5d7b5
mcore bridge
hjh0119 Nov 4, 2025
edf3378
Merge remote-tracking branch 'origin' into mega-grpo
hjh0119 Nov 4, 2025
b3b37ce
merge main & flatten weight sync
hjh0119 Nov 5, 2025
1d930d8
dynamic sample
hjh0119 Nov 5, 2025
5f9e14a
fix dynamic sampling
hjh0119 Nov 5, 2025
b753911
merge main
hjh0119 Nov 6, 2025
d1460c2
fix cp: compute part of seq loss
hjh0119 Nov 7, 2025
db7000d
optimize weight sync & fix vllm_tp
hjh0119 Nov 7, 2025
bee6925
fix vllm tp
hjh0119 Nov 7, 2025
f92739a
fix vllm tp distribute outputs twice
hjh0119 Nov 7, 2025
bd3f9c9
length context for template encode
hjh0119 Nov 7, 2025
3f9b11d
fix sequence is wip"
hjh0119 Nov 9, 2025
e34d02d
merge main
hjh0119 Nov 9, 2025
1b4fd80
fix padding loss calculate
hjh0119 Nov 10, 2025
e9a307d
log completions
hjh0119 Nov 10, 2025
8b1e407
bug todo
hjh0119 Nov 10, 2025
b6c10eb
fix wip
hjh0119 Nov 10, 2025
b25c7d2
merge main
hjh0119 Nov 11, 2025
06b3693
Merge remote-tracking branch 'origin' into mega-grpo
hjh0119 Nov 11, 2025
46b8e2a
fix sp padding
hjh0119 Nov 12, 2025
87bbad7
fix pp
hjh0119 Nov 12, 2025
1927712
revert to full seq loss for cp
hjh0119 Nov 12, 2025
44920c0
fix server client init in first rank instead of last rank
hjh0119 Nov 13, 2025
7ebfdda
fix server mode
hjh0119 Nov 13, 2025
43fc27d
fix server pass prompt
hjh0119 Nov 13, 2025
1696ea9
dense script
hjh0119 Nov 13, 2025
a629fbd
check batch size params
hjh0119 Nov 13, 2025
852d0f0
dense server script
hjh0119 Nov 13, 2025
2f98eba
moe script
hjh0119 Nov 14, 2025
1936a83
docs
hjh0119 Nov 14, 2025
500408a
merge main
hjh0119 Nov 14, 2025
bdcaa51
update doc
hjh0119 Nov 14, 2025
027ca57
update doc & args check
hjh0119 Nov 14, 2025
360da42
clean up
hjh0119 Nov 14, 2025
6259a34
clean up
hjh0119 Nov 14, 2025
5fdde02
clean up
hjh0119 Nov 14, 2025
b5be0ce
clean up
hjh0119 Nov 14, 2025
d8c7c3b
clean up
hjh0119 Nov 14, 2025
2aaa1e5
align scale_rewards
hjh0119 Nov 14, 2025
7540743
merge main
hjh0119 Nov 14, 2025
29ecb32
aggressive_empty_cache before wake up weights
hjh0119 Nov 14, 2025
ad00c5c
docs
hjh0119 Nov 14, 2025
5977fe5
sleep level doc
hjh0119 Nov 14, 2025
5ab6d37
fix kl metrics
hjh0119 Nov 14, 2025
2841fb9
fix arxiv link & fix kl metric
hjh0119 Nov 14, 2025
2a97c64
revert script
hjh0119 Nov 14, 2025
40706d7
revert server_base_url doc
hjh0119 Nov 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/Instruction/Command-line-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ reward模型参数将在PPO、GRPO中使用。
- top_entropy_quantile: 仅对熵值处于前指定分位的 token 参与损失计算,默认为1.0,即不过滤低熵 token,具体参考[文档](./GRPO/AdvancedResearch/entropy_mask.md)
- log_entropy: 记录训练中的熵值变化动态,默认为False,具体参考[文档](./GRPO/GetStarted/GRPO.md#logged-metrics)

##### 奖励函数参数
内置的奖励函数参考[文档](./GRPO/DeveloperGuide/reward_function.md)
cosine 奖励参数
- cosine_min_len_value_wrong:cosine 奖励函数参数,生成错误答案时,最小长度对应的奖励值。默认值为-0.5。
- cosine_max_len_value_wrong:生成错误答案时,最大长度对应的奖励值。默认值为0.0。
Expand Down
2 changes: 1 addition & 1 deletion docs/source/Instruction/GRPO/AdvancedResearch/GSPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ importance_weights = torch.exp(log_importance_weights)
- `importance_sampling_level sequence` (GSPO)
- `importance_sampling_level sequence_token` (GSPO-token)

其中 sequence_token 要求 ms-swift > 3.7 (源码安装)
其中 sequence_token 要求 ms-swift >= 3.8

论文其他超参
```bash
Expand Down
2 changes: 1 addition & 1 deletion docs/source/Instruction/Use-tuners.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tuner是指附加在模型上的额外结构部分,用于减少训练参数量
- Adapter: [Parameter-Efficient Transfer Learning for NLP](http://arxiv.org/abs/1902.00751)
- Vision Prompt Tuning: [Visual Prompt Tuning](https://arxiv.org/abs/2203.12119)
- Side: [Side-Tuning: A Baseline for Network Adaptation via Additive Side Networks](https://arxiv.org/abs/1912.13503)
- Res-Tuning: [Res-Tuning: A Flexible and Efficient Tuning Paradigm via Unbinding Tuner from Backbone](https://arxiv.org/abs/2310.19859) < [arXiv](https://arxiv.org/abs/2310.19859) | [Project Page](https://res-tuning.github.io/) | [Usage](ResTuning.md) >
- Res-Tuning: [Res-Tuning: A Flexible and Efficient Tuning Paradigm via Unbinding Tuner from Backbone](https://arxiv.org/abs/2310.19859) < [arXiv](https://arxiv.org/abs/2310.19859) | [Project Page](https://res-tuning.github.io/) >
- [PEFT](https://github.com/huggingface/peft)提供的tuners, 如AdaLoRA、DoRA、Fourierft等

## 接口列表
Expand Down
91 changes: 68 additions & 23 deletions docs/source/Megatron-SWIFT/Command-line-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,29 +246,6 @@ lora训练:
- lora_bias: 默认为`'none'`,可以选择的值: 'none'、'all'。如果你要将bias全都设置为可训练,你可以设置为`'all'`。
- use_rslora: 默认为`False`,是否使用`RS-LoRA`。


**DPO参数**:
- ref_load: ref_model的加载路径。采用DPO/KTO算法且使用全参数训练时需要传入。默认为None,即设置为`load`。
- ref_adapter_load: 加载ref_adapter的权重路径,默认为None。若你要使用SFT产生的LoRA权重进行DPO,请使用"ms-swift>=3.8",并在训练时设置`--adapter_load sft_ckpt --ref_adapter_load sft_ckpt --finetune true`。若是此场景的断点续训,则设置`--adapter_load rlhf_ckpt --ref_adapter_load sft_ckpt --finetune false`。
- beta: 含义与[TRL](https://huggingface.co/docs/trl/main/en/dpo_trainer#trl.DPOConfig)相同。控制与参考模型偏差程度的参数。beta值越高,表示与参考模型的偏差越小。对于 IPO 损失函数 (loss_type="ipo"),beta是[论文](https://huggingface.co/papers/2310.12036)中所指的正则化参数。默认为0.1。
- 🔥rpo_alpha: 来自[RPO 论文](https://huggingface.co/papers/2404.19733)中的参数,用于控制损失函数中NLL项的权重(即SFT损失),`loss = dpo_loss + rpo_alpha * sft_loss`,论文中推荐设置为`1.`。默认为`None`,即默认不引入sft_loss。
- **注意**:在"ms-swift<3.8",其默认值为`1.`。在"ms-swift>=3.8"该默认值修改为`None`。
- reference_free: 是否忽略提供的参考模型,并隐式地使用一个对所有响应赋予相等概率的参考模型。默认为False。
- label_smoothing: 默认为0.。
- f_divergence_type: 默认为`reverse_kl`。可选值参考[TRL文档](https://huggingface.co/docs/trl/main/en/dpo_trainer)。
- loss_type: 默认为'sigmoid'。可选值参考[TRL文档](https://huggingface.co/docs/trl/main/en/dpo_trainer#loss-functions)。

**KTO参数**:
- ref_load: 含义同DPO。
- ref_adapter_load: 含义同DPO。
- beta: 控制与 ref_model 偏离程度的参数。较高的 beta 表示与 ref_model 偏离更小。默认为`0.1`。
- loss_type: 默认为'kto'。可选值参考[TRL文档](https://huggingface.co/docs/trl/main/en/kto_trainer#trl.KTOConfig.loss_type)。
- desirable_weight: 抵消 desirable 和 undesirable 数量不均衡的影响,对 desirable 损失按该系数进行加权,默认为`1.`。
- undesirable_weight: 抵消 desirable 和 undesirable 数量不均衡的影响,对 undesirable 损失按该系数进行加权,默认为`1.`。

**RM参数**:
- center_rewards_coefficient: 用于激励奖励模型输出均值为零的奖励的系数,具体查看这篇[论文](https://huggingface.co/papers/2312.09244)。推荐值:0.01。

**Mcore-Bridge参数**
- 🔥load_safetensors: 默认为False,是否直接从safetensors加载权重。
- 🔥save_safetensors: 默认为False,是否直接保存成safetensors权重。注意,若该参数设置为True,则不会存储优化器权重、随机数状态等断点续训内容。
Expand Down Expand Up @@ -323,6 +300,74 @@ Megatron训练参数继承自Megatron参数和基本参数(**与ms-swift共用
- calculate_per_token_loss: 覆盖Megatron参数,默认为False。


### DPO参数
- ref_load: ref_model的加载路径。采用DPO/GRPO/KTO算法且使用全参数训练时需要传入。默认为None,即设置为`load`。
- ref_adapter_load: 加载ref_adapter的权重路径,默认为None。若你要使用SFT产生的LoRA权重进行DPO,请使用"ms-swift>=3.8",并在训练时设置`--adapter_load sft_ckpt --ref_adapter_load sft_ckpt --finetune true`。若是此场景的断点续训,则设置`--adapter_load rlhf_ckpt --ref_adapter_load sft_ckpt --finetune false`。
- beta: 含义与[TRL](https://huggingface.co/docs/trl/main/en/dpo_trainer#trl.DPOConfig)相同。控制与参考模型偏差程度的参数。beta值越高,表示与参考模型的偏差越小。对于 IPO 损失函数 (loss_type="ipo"),beta是[论文](https://huggingface.co/papers/2310.12036)中所指的正则化参数。默认为0.1。
- 🔥rpo_alpha: 来自[RPO 论文](https://huggingface.co/papers/2404.19733)中的参数,用于控制损失函数中NLL项的权重(即SFT损失),`loss = dpo_loss + rpo_alpha * sft_loss`,论文中推荐设置为`1.`。默认为`None`,即默认不引入sft_loss。
- **注意**:在"ms-swift<3.8",其默认值为`1.`。在"ms-swift>=3.8"该默认值修改为`None`。
- reference_free: 是否忽略提供的参考模型,并隐式地使用一个对所有响应赋予相等概率的参考模型。默认为False。
- label_smoothing: 默认为0.。
- f_divergence_type: 默认为`reverse_kl`。可选值参考[TRL文档](https://huggingface.co/docs/trl/main/en/dpo_trainer)。
- loss_type: 默认为'sigmoid'。可选值参考[TRL文档](https://huggingface.co/docs/trl/main/en/dpo_trainer#loss-functions)。

### KTO参数
- ref_load: 含义同DPO。
- ref_adapter_load: 含义同DPO。
- beta: 控制与 ref_model 偏离程度的参数。较高的 beta 表示与 ref_model 偏离更小。默认为`0.1`。
- loss_type: 默认为'kto'。可选值参考[TRL文档](https://huggingface.co/docs/trl/main/en/kto_trainer#trl.KTOConfig.loss_type)。
- desirable_weight: 抵消 desirable 和 undesirable 数量不均衡的影响,对 desirable 损失按该系数进行加权,默认为`1.`。
- undesirable_weight: 抵消 desirable 和 undesirable 数量不均衡的影响,对 undesirable 损失按该系数进行加权,默认为`1.`。

### RM参数
- center_rewards_coefficient: 用于激励奖励模型输出均值为零的奖励的系数,具体查看这篇[论文](https://huggingface.co/papers/2312.09244)。推荐值:0.01。

### GRPO参数
- ref_load: 含义同DPO。
- ref_adapter_load: 含义同DPO。
- beta: KL正则系数,默认为0.04,设置为0时不加载ref model。
- micro_batch_size: 每个device的批次大小,默认为1。
- global_batch_size: 总批次大小,等价于`micro_batch_size*数据并行大小*梯度累加步数`。默认为16。
- steps_per_generation:每轮生成的优化步数,即采样批量大小相对global_batch_size的倍数,默认为1。
- generation_batch_size: 采样批量大小,需要是global_batch_size的倍数,默认等于global_batch_size*steps_per_generation。
- num_generations: 每个prompt采样的数量,论文中的G值,默认为8。
- reward_funcs: GRPO算法奖励函数,可选项为`accuracy`、`format`、`cosine`、`repetition`和`soft_overlong`,见swift/plugin/orm.py。你也可以在plugin中自定义自己的奖励函数。默认为`[]`。
- reward_weights: 每个奖励函数的权重。必须与奖励函数和奖励模型的总数量匹配。默认为 None,即所有奖励的权重都相等,为`1.0`。
- 提示:如果GRPO训练中包含`--reward_model`,则其加在奖励函数的最后位置。
- loss_type: loss 归一化的类型,可选项为['grpo', 'bnpo', 'dr_grpo'], 默认为'grpo', 具体查看该[pr](https://github.com/huggingface/trl/pull/3256#discussion_r2033213348)。
- log_completions: 是否记录训练中的模型生成内容,默认为False。
- vllm_mode: vLLM 集成模式,可选项为 `server` 和 `colocate`。server 模式使用 `swift rollout` 拉起的 vLLM 服务器进行采样,colocate 模式在程序内部署 vLLM。使用server端时,
- vllm_mode server 参数
- vllm_server_base_url: vLLM server的Base URL(比如 http://local_host:8000), 默认为None。设置后,忽略host和port设置。
- vllm_server_host:vLLM server host地址,默认为None。
- vllm_server_port vLLM server 服务端口,默认为8000。
- vllm_server_timeout 连接vLLM server的超时时间,默认为 240s。
- vllm_server_pass_dataset: 透传额外的数据集信息到vLLM server,用于多轮训练。
- async_generate: 异步rollout以提高训练速度,注意开启时采样会使用上一轮更新的模型进行采样,不支持多轮场景。默认`false`.
- SWIFT_UPDATE_WEIGHTS_BUCKET_SIZE:环境变量,用于控制权重同步时的传输桶大小(bucket size),适用于 Server Mode 下的全参数训练,单位为 MB,默认值为 512 MB。
- vllm_mode colocate 参数(更多参数支持参考[vLLM参数](#vLLM参数)。)
- vllm_gpu_memory_utilization: vllm透传参数,默认为0.9。
- vllm_max_model_len: vllm透传参数,默认为None。
- vllm_enforce_eager: vllm透传参数,默认为False。
- vllm_limit_mm_per_prompt: vllm透传参数,默认为None。
- vllm_enable_prefix_caching: vllm透传参数,默认为True。
- vllm_tensor_parallel_size: tp并行数,默认为`1`。
- vllm_enable_lora: 支持vLLM Engine 加载 LoRA adapter,默认为False。用于加速LoRA训练的权重同步,具体参考[文档](../Instruction/GRPO/GetStarted/GRPO.md#权重同步加速)。
- sleep_level: 训练时释放 vLLM 显存,可选项为[0, 1, 2], 默认为0,不释放。
- offload_optimizer: 是否在vLLM推理时offload optimizer参数,默认为False。
- offload_model: 是否在vLLM推理时 offload 模型,默认为False。
- num_iterations: 每条数据的更新次数,[GRPO论文](https://arxiv.org/abs/2402.03300)中的 $\mu$ 值,默认为1。
- epsilon: clip 系数,默认为0.2。
- epsilon_high: upper clip 系数,默认为None,设置后与epsilon共同构成[epsilon, epsilon_high]裁剪范围。
- dynamic_sample:筛除group内奖励标准差为0的数据,额外采样新数据,默认为False。
- max_resample_times:dynamic_sample设置下限制重采样次数,默认3次。
- overlong_filter:跳过超长截断的样本,不参与loss计算,默认为False。
- delta: [INTELLECT-2 tech report](https://huggingface.co/papers/2505.07291)中双侧 GRPO 上界裁剪值。若设置,建议大于 1 + epsilon。默认为None。
- importance_sampling_level: 控制重要性采样比计算,可选项为 `token` 和 `sequence`,`token` 模式下保留原始的每个 token 的对数概率比,`sequence` 模式下则会对序列中所有有效 token 的对数概率比进行平均。[GSPO论文](https://www.arxiv.org/abs/2507.18071)中使用sequence级别计算来稳定训练,默认为`token`。
- scale_rewards:指定奖励的缩放策略。可选值包括 `group`(按组内标准差缩放)、`batch`(按整个批次的标准差缩放)、`none`(不进行缩放)。在 ms-swift < 3.10 版本中,该参数为布尔类型,`true` 对应 `group`,`false` 对应 `none`。默认值与 `advantage_estimator` 绑定:`grpo` 对应 `group`,`rloo` 对应 `none`,`reinforce_plus_plus` 对应 `batch`。

内置奖励函数参数参考[文档](../Instruction/Command-line-parameters.md#奖励函数参数)

## 导出参数
这里介绍`megatron export`的参数(需"ms-swift>=3.10"),若要使用`swift export`导出命令,请参考[ms-swift命令行参数文档](../Instruction/Command-line-parameters.md#导出参数)。`megatron export`相比`swift export`,支持分布式和多机导出。Megatron导出参数继承自Megatron参数和基本参数。
- 🔥to_mcore: HF格式权重转成Megatron格式。默认为False。
Expand Down
61 changes: 61 additions & 0 deletions docs/source/Megatron-SWIFT/GRPO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# GRPO

**版本依赖**:ms-swift >= 3.11

如果你是首次使用 GRPO,请先参考 [GRPO文档](../Instruction/GRPO/GetStarted/GRPO.md)。

Megatron GRPO 当前已支持以下功能:

- **训练模式**:全参数训练与 LoRA 微调
- **并行策略**:支持上下文并行(CP)、流水线并行(PP)、张量并行(TP)和专家并行(EP)
- **推理加速**:支持 vLLM 的 colocate 模式和 server 模式
- **模型支持**:兼容 Megatron Swift 中的 LLM 及 MLLM(多模态大模型)
- **算法支持**:涵盖 swift GRPO 的大部分功能

以下参数或功能将在后续版本中逐步支持:

- **Entropy 相关配置**:如 `top_entropy_quantile`、`log_entropy`
- **Reward Model / Reward Model Plugin**
- **多轮 Rollout 调度机制**(`multi_turn_scheduler`):实现多轮对话策略优化
- **优势估计器**(`advantage_estimator`):支持更复杂的策略梯度估计方法
- **KL 散度计入奖励**(`kl_in_reward`)
- **虚拟流水线并行**(VPP)
- **参考模型同步更新**(`sync_ref_model`)
- **Async Generate** (`async_generate`)
- **num_iterations**
- **日志同步 SwanLab**

⚠️ 注意:以下参数在 Megatron GRPO 中不生效:

- **`use_vllm`**:Megatron GRPO 暂不支持使用 PTEngine 进行 Rollout 推理。
- **`move_model_batches`**:该参数专用于 DeepSpeed ZeRO-3 优化,在 Megatron 架构下无效。

与 ms-swift GRPO 相同,Megatron GRPO batch size 相关的参数均以 **completion-level** 为单位,即表示模型生成的 completion 数量,而非 prompt 数量。

#### 参数对比

下表对比了 ms-swift 和 Megatron-SWIFT 中批量相关参数的对应关系:

| ms-swift 参数 | Megatron-SWIFT 参数 | 说明 |
|---------------|---------------------|------|
| `per_device_train_batch_size` | `micro_batch_size` | 每张 GPU 的训练批次大小(completion-level) |
| `gradient_accumulation_steps` | - | 梯度累积步数,在 Megatron-SWIFT 中已包含在 `global_batch_size` 的计算中 |
| - | `global_batch_size` | 全局批次大小(completion-level)<br/>**Megatron-SWIFT**: `micro_batch_size × dp_size × gradient_accumulation_steps`<br/>**ms-swift**: `per_device_train_batch_size × world_size × gradient_accumulation_steps` |
| `num_generations` | `num_generations` | 每个 prompt 生成的 completion 数量 |
| `steps_per_generation` | `steps_per_generation` | Rollout 批次大小相对于训练批次大小的倍数<br/>**注意**:在 ms-swift 中需为 `gradient_accumulation_steps` 的整数倍 |
| `generation_batch_size` | `generation_batch_size` | Rollout 阶段的批次大小(completion-level),需为 `global_batch_size` 的整数倍 |

以下公式用于计算 Megatron GRPO 中的批量:

- **数据并行大小**:`dp_size = world_size / (TP × PP × CP)`
- **全局批次大小**:`global_batch_size = micro_batch_size × dp_size × gradient_accumulation_steps`
- **生成批次大小**:`generation_batch_size = global_batch_size × steps_per_generation`
- **Rollout Prompt 数量**:`num_rollout_prompts = generation_batch_size / num_generations`
- **训练 Prompt 数量**:`num_train_prompts = global_batch_size / num_generations`
- **每个 DP group 的训练 Prompt 数量**:`num_prompts_per_dp_group = global_batch_size / num_generations / dp_size`

注意:在 Megatron GRPO 中,每个 DP group 的训练 Prompt 数量须满足 `num_prompts_per_dp_group` 是 `micro_batch_size`的整数倍,以确保训练批次能够正确分配。

更多参数请参考[命令行文档](./Command-line-parameters.md#grpo参数)

训练脚本请参考[Megatron GRPO 脚本](https://github.com/modelscope/ms-swift/blob/main/examples/megatron/grpo)
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Swift DOCUMENTATION
Megatron-SWIFT/LoRA-Training.md
Megatron-SWIFT/Multimodal-Model.md
Megatron-SWIFT/Mcore-Bridge.md
Megatron-SWIFT/GRPO.md


.. toctree::
Expand Down
2 changes: 2 additions & 0 deletions docs/source_en/Instruction/Command-line-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ The hyperparameters for the reward function can be found in the [Built-in Reward
- log_entropy: Logs the entropy values during training. The default is False. For more information, refer to the [documentation](./GRPO/GetStarted/GRPO.md#logged-metrics).


##### Reward function parameters
Refer to the [documentation](./GRPO/DeveloperGuide/reward_function.md) for built-in reward functions.

cosine reward function arguments
- cosine_min_len_value_wrong (default: -0.5): Reward value corresponding to the minimum length when the answer is incorrect.
Expand Down
2 changes: 1 addition & 1 deletion docs/source_en/Instruction/GRPO/AdvancedResearch/GSPO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Group Sequence Policy Optimization

**Version Requirement**: ms-swift>=3.7
**Version Requirement**: ms-swift>=3.8

In [Group Sequence Policy Optimization](https://www.arxiv.org/abs/2507.18071), it is pointed out that GRPO computes importance sampling weights at the token level. However, this approach is problematic: since each token is only sampled once, it cannot realize effective distribution correction, and instead introduces high-variance noise during training, which can easily lead to unstable gradient estimates and even training collapse. Therefore, the paper argues that the unit of the objective function should be consistent with that of the reward. Since the reward is typically given at the sequence level (i.e., for the entire generated response), it is more reasonable to perform off-policy correction and optimization at the sequence level rather than the token level.

Expand Down
2 changes: 1 addition & 1 deletion docs/source_en/Instruction/Use-tuners.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Tuners refer to additional structural components attached to a model, aimed at r
- Adapter: [Parameter-Efficient Transfer Learning for NLP](http://arxiv.org/abs/1902.00751)
- Vision Prompt Tuning: [Visual Prompt Tuning](https://arxiv.org/abs/2203.12119)
- Side: [Side-Tuning: A Baseline for Network Adaptation via Additive Side Networks](https://arxiv.org/abs/1912.13503)
- Res-Tuning: [Res-Tuning: A Flexible and Efficient Tuning Paradigm via Unbinding Tuner from Backbone](https://arxiv.org/abs/2310.19859) < [arXiv](https://arxiv.org/abs/2310.19859) | [Project Page](https://res-tuning.github.io/) | [Usage](ResTuning.md) >
- Res-Tuning: [Res-Tuning: A Flexible and Efficient Tuning Paradigm via Unbinding Tuner from Backbone](https://arxiv.org/abs/2310.19859) < [arXiv](https://arxiv.org/abs/2310.19859) | [Project Page](https://res-tuning.github.io/) >
- Tuners provided by [PEFT](https://github.com/huggingface/peft), such as AdaLoRA, DoRA, Fourierft, etc.

## Interface List
Expand Down
Loading
Loading