[BUG] GRPO REAL loss crashes with beta>0 and kl_in_reward=True (NoneType * Tensor)

オープン 初心者向け
#10,690 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@newcloud123 がすでに取り組んでいます。

2026年12月5日 から。

  • #10691 @newcloud123 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
85/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch

調査の方向性

swift/rlhf_trainers/grpo_trainer.pyを読み、特に1127行目付近のloss_type='real'分岐と1067行目付近の既存のKLガードを確認してください。beta>0とkl_in_reward=Trueで再現し、real-lossパスがper_token_klがない場合もクラッシュせずに処理できることを確認してください。また、kl_in_reward=Falseの対照ケースも引き続き動作することを確認してください。

索引モデルが issue の本文から書いたものです。

説明

Describe the bug

In swift/rlhf_trainers/grpo_trainer.py (main @ ece3bcc6), per_token_kl is only computed when beta != 0.0 and not self.kl_in_reward:

976: per_token_kl = None
977: if self.beta != 0.0 and not self.kl_in_reward:
978:     ref_per_token_logps = grpo_batch.ref_per_token_logps
980:     per_token_kl = torch.clamp(...)

The main per-token loss path correctly guards it at line 1067:

1067: if per_token_kl is not None:
1068:     per_token_loss = per_token_loss + self.beta * per_token_kl

But the loss_type='real' branch only checks beta != 0.0 and unconditionally multiplies per_token_kl:

1127: if self.beta != 0.0:
1128:     kl_loss = (per_token_kl * completion_mask).sum() / completion_mask.sum().clamp(min=1.0)
1129:     loss = loss + kl_loss * self.beta

With beta > 0 and kl_in_reward=True (a common setup: fold reference-model KL into the reward), per_token_kl stays None and line 1128 raises TypeError: unsupported operand type(s) for *: 'NoneType' and 'Tensor'. Training cannot start at all.

Reproduction

Configure GRPO training with:

loss_type='real'
beta=0.01
kl_in_reward=True       # KL folded into reward -> per_token_kl is None

Construct any batch with mixed-advantage groups and call the loss computation. The real branch reaches line 1128 with per_token_kl=None and crashes. The control case kl_in_reward=False runs normally (per_token_kl is computed).

Expected behavior

Guard the KL term the same way as line 1067:

if self.beta != 0.0 and per_token_kl is not None:
    ...

Environment

  • ms-swift main @ ece3bcc6
  • loss_type='real', beta>0, kl_in_reward=True
  • torch 2.7.0+cu128, CUDA / RTX 5090
主要言語
Python
スター
16.2k
フォーク
1.8k
平均マージ
1日 14時間
マージ済み PR(30日)
68

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelscope/ms-swift のほかの issue

modelscope/ms-swift の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。