Skip to content

[RLHF] DAPO/CISPO/FIPO 的 loss 在梯度累积下被额外除以 gradient_accumulation_steps #10117

Description

@yhb643

Checklist / 检查清单

  • I have searched existing issues, and this is a new bug report. / 我已经搜索过现有的 issues,确认这是一个新的 bug report。

Bug Description / Bug 描述

loss_typedapocispo/fipo 同样受影响)时,策略 loss 已按整个梯度累积窗口的 completion token 数归一化:

normalizer = grpo_batch.num_items_in_batch / self.accelerator.num_processes
loss = (per_token_loss * completion_mask).sum() / normalizer

num_items_in_batchga_batch_encoded_inputs 汇总(swift/rlhf_trainers/grpo_trainer.py:803-809),覆盖所有 micro-batch × 所有进程,所以单个 micro-batch 只是全局 token 均值的一部分。

transformers.Trainer.training_step 会在 backward 前再除以 current_gradient_accumulation_stepsAcceleratornum_steps 被强制为 1,没有二次缩放),于是 loss 和梯度都被额外缩小 G 倍:期望梯度 g,gradient_accumulation_steps=4 时实际只有 g/4。

影响:DAPO 的有效学习率随 gradient_accumulation_steps 变化(G 越大更新越弱),日志里的 loss 也被除以 G。

How to Reproduce / 如何复现

环境:ms-swift main @ 0673cf7、transformers 5.12.1、DeepSpeed ZeRO-3

  1. --loss_type dapo --gradient_accumulation_steps 4 起 GRPO 训练;
  2. _compute_loss_and_metrics 打印同一个 micro-batch 的 loss;
  3. 参数更新量约为 gradient_accumulation_steps=1 时的 1/4。

最小验证:

loss = torch.tensor(2.0, requires_grad=True)
(loss / 4).backward()   # 复现 Trainer 的行为:梯度只有目标值的 1/4

Additional Information / 补充信息

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions