Skip to content

fix(grpo): clamp log-ratio and k3 KL for numerical stability#9

Open
dongseokmotif wants to merge 2 commits into
mainfrom
feat/grpo-clamp-numerical-stability
Open

fix(grpo): clamp log-ratio and k3 KL for numerical stability#9
dongseokmotif wants to merge 2 commits into
mainfrom
feat/grpo-clamp-numerical-stability

Conversation

@dongseokmotif

Copy link
Copy Markdown
Member

Summary

main 기준으로 GRPO 수치 안정화(clamp) 커밋만 담은 PR입니다.

Numerical-stability clamping for the chunked GRPO loss, matching NeMo-RL behavior.

Details

  • Clamp policy/old log-ratio before exp (log_ratio_clamp_value).
  • Clamp (ref - policy) log-ratio before exp inside k3, and clamp the resulting k3 KL output (kl_input_clamp_value, kl_output_clamp_value).
  • nan_to_num guards on the relevant exponentials.
  • Default clamp values turned on for NeMo-RL parity.

Commits

  1. fix(grpo): clamp log-ratio and k3 KL inputs/outputs for numerical stability
  2. default clamp values on (NeMo-RL parity)

WyldeCat and others added 2 commits June 14, 2026 05:22
…bility

Unbounded ``exp`` over the policy/old log-ratio and over the k3 KL
``log_p - log_q`` can overflow fp32 to ``inf`` at masked / low-
probability positions. Because the subsequent reduction is a plain
``per_token_loss * attention_mask``, ``inf * 0 == nan`` then contami-
nates the entire batch, producing NaN losses on the very first step.

Add three optional clamps (defaults ``None`` = no behavior change):

- ``log_ratio_clamp_value`` clamps ``policy - old`` before ``exp``,
  protecting both the token- and sequence-level importance-weight
  paths (and the PG ``coef_1``).
- ``kl_input_clamp_value`` clamps ``ref - policy`` inside
  ``k3_loss_fn`` before ``exp``.
- ``kl_output_clamp_value`` clamps the resulting k3 KL value.

This mirrors the guard used by NeMo-RL's ``calculate_kl``
(``input_clamp_value`` / ``output_clamp_value``) and the broader
RL-framework convention. Callers that don't set the new arguments
get the prior behavior unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Set the new clamp arguments to NeMo-RL's ``calculate_kl`` defaults
(``log_ratio_clamp_value=20.0``, ``kl_input_clamp_value=20.0``,
``kl_output_clamp_value=10.0``) instead of ``None`` so the NaN guard
is active out of the box. Existing callers automatically get the
safety; users that want the prior unbounded behavior can pass
``None`` explicitly.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants