Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
(bug) sanitize masked position
  • Loading branch information
KJLdefeated committed Jun 9, 2026
commit a4ca4535cf7868dafe3e8a5f6de50642618c47ce
5 changes: 3 additions & 2 deletions rl_engine/kernels/ops/pytorch/loss/grpo_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ def apply(
"""Compute ``(loss, policy_loss, kl)`` from per-token log-probabilities."""
bool_mask = completion_mask.bool()

ratio = torch.exp(current_logps.float() - old_logps.float())
delta = (current_logps.float() - old_logps.float()).masked_fill(~bool_mask, 0.0)
diff = (ref_logps.float() - current_logps.float()).masked_fill(~bool_mask, 0.0)
ratio = torch.exp(delta)
adv = advantages.float()
unclipped = ratio * adv
clipped = torch.clamp(ratio, 1.0 - clip_eps, 1.0 + clip_eps) * adv
policy_loss_terms = -torch.minimum(unclipped, clipped)

# k3 reference-KL estimator: exp(d) - d - 1, with d = ref - current.
diff = ref_logps.float() - current_logps.float()
kl_terms = torch.exp(diff) - diff - 1.0

policy_loss = self._masked_mean(policy_loss_terms, bool_mask)
Expand Down
Loading