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

Aperta Adatta ai principianti
#10,690 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@newcloud123 ci sta già lavorando.

Dal 5/12/2026.

  • #10691 di @newcloud123 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Leggi swift/rlhf_trainers/grpo_trainer.py, in particolare il ramo loss_type='real' intorno alla riga 1127 e il controllo KL esistente intorno alla riga 1067. Riproduci il problema con beta>0 e kl_in_reward=True, poi verifica che il percorso real-loss gestisca l’assenza di per_token_kl senza arrestarsi; conferma che il controllo con kl_in_reward=False continui a funzionare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Python
Stelle
16.2k
Fork
1.8k
Merge medio
1g 14h
PR unite (30g)
68

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelscope/ms-swift

Tutte le issue di modelscope/ms-swift

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.