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

Open Beginner friendly
#10,690 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@newcloud123 is already working on this.

Since Dec 5, 2026.

  • #10691 by @newcloud123 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Read swift/rlhf_trainers/grpo_trainer.py, especially the loss_type='real' branch around line 1127 and the existing KL guard around line 1067. Reproduce with beta>0 and kl_in_reward=True, then check that the real-loss path handles the absent per_token_kl without crashing; confirm the kl_in_reward=False control still works.

Written by the indexing model from the issue text.

Description

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
Dominant language
Python
Stars
16.2k
Forks
1.8k
Avg merge
1d 14h
Merged PRs (30d)
68

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelscope/ms-swift

All issues in modelscope/ms-swift

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.