[BUG] GRPO REAL loss crashes with beta>0 and kl_in_reward=True (NoneType * Tensor)
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 85/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- machine-learning
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from modelscope/ms-swift
-
get_multimodal_target_regex(..., exclude_router=True) does not exclude shared_expert_gatePossibly taken @KonnyakuMatcha claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
modelscope/ms-swift#10696 ·
Maintainers usually reply within 1 day
-
[BUG] GRPO REAL loss divides by all groups instead of valid groups, silently scaling gradientsPossibly taken @newcloud123 claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
modelscope/ms-swift#10688 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 Half a day Newbie friendliness 62/100
modelscope/ms-swift#10621 ·
Maintainers usually reply within 1 day
-
[Bug] `get_seed` consumes a draw from the shared RandomState even when decision augmentation is disabled, changing the train/val split and dataset shufflePossibly taken @ryznren claimed this 38 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
modelscope/ms-swift#10466 · 1 comment ·
Maintainers usually reply within 1 day
-
[Bug] `swift --help` raises KeyError instead of showing CLI helpPossibly taken @haahy claimed this 64 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
modelscope/ms-swift#10287 ·
Maintainers usually reply within 1 day
All issues in modelscope/ms-swift
Similar issues
-
Test: to_hijri() accepts datetime.datetime and uses only the date partPossibly taken A pull request linked to this issue is open or already merged. Opengood first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 · 3 comments ·
Maintainers usually reply within 1 day
-
polish
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
521xueweihan/HelloGitHub#4729 ·
-
[Bug]: Score parser reads parts of scientific notation as valid scoresPossibly taken @AshutoshSingh840 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
UKGovernmentBEIS/control-arena#992 ·
Maintainers usually reply within 3 days
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
mvalentsev/awesome-free-ai-coding#70 ·
Maintainers usually reply within 1 day