Skip to content

fix: skip prev_logprobs computation when force_on_policy_ratio is true#2177

Closed
yfw wants to merge 1 commit into
mainfrom
yifu/skip-prev-logprobs-force-onpolicy
Closed

fix: skip prev_logprobs computation when force_on_policy_ratio is true#2177
yfw wants to merge 1 commit into
mainfrom
yifu/skip-prev-logprobs-force-onpolicy

Conversation

@yfw
Copy link
Copy Markdown
Contributor

@yfw yfw commented Mar 30, 2026

When force_on_policy_ratio=True, the importance sampling ratio is forced to 1.0, so prev_logprobs are unnecessary. Skip the expensive prepare_for_lp_inference() and get_logprobs() calls in both sync and async GRPO paths. In the loss function, use curr_logprobs.detach() as prev_logprobs instead of loading placeholder zeros from data.

Also guards against incompatible use of seq_logprob_error_threshold with force_on_policy_ratio (the threshold requires real prev_logprobs).

Part of #1906

What does this PR do ?

Add a one line overview of what this PR aims to accomplish.

Issues

List issues that this PR closes (syntax):

Usage

  • You can potentially add a usage example below
# Add a code snippet demonstrating how to use this

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

  • ...

When force_on_policy_ratio=True, the importance sampling ratio is
forced to 1.0, so prev_logprobs are unnecessary. Skip the expensive
prepare_for_lp_inference() and get_logprobs() calls in both sync and
async GRPO paths. In the loss function, use curr_logprobs.detach() as
prev_logprobs instead of loading placeholder zeros from data.

Also guards against incompatible use of seq_logprob_error_threshold
with force_on_policy_ratio (the threshold requires real prev_logprobs).

Part of #1906

Co-Authored-By: Jiaqi Zeng <jiaqiz@nvidia.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Signed-off-by: Yi-Fu Wu <yifu.wu@gmail.com>
@copy-pr-bot
Copy link
Copy Markdown

copy-pr-bot Bot commented Mar 30, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

jinglinglingling added a commit to jinglinglingling/RL that referenced this pull request May 9, 2026
…ratio

Adds two parametrized unit tests in tests/unit/algorithms/test_grpo.py
that cover both grpo_train and async_grpo_train:

- test_grpo_train_skips_reference_policy_logprobs_when_configured:
  guards issue NVIDIA-NeMo#1968 / PRs NVIDIA-NeMo#2174, NVIDIA-NeMo#2178 by asserting that
  policy.get_reference_policy_logprobs is never called when
  grpo.skip_reference_policy_logprobs_calculation=True.

- test_grpo_train_skips_prev_logprobs_when_force_on_policy_ratio:
  guards PR NVIDIA-NeMo#2177 by asserting that policy.get_logprobs is never
  called when loss_fn.force_on_policy_ratio=True.

Both tests reuse the existing mock_grpo_components fixture and the
mock_async_grpo_infrastructure helper so they require no GPU / Ray
cluster and run in CI in milliseconds (modulo cold-start import cost).

Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: Linglin Jing <linglinj@nvidia.com>
jinglinglingling added a commit to jinglinglingling/RL that referenced this pull request May 9, 2026
The two regression tests added in this PR drive `grpo_train` /
`async_grpo_train` through code paths that call
`torch.zeros_like(prev_logprobs)` (PRs NVIDIA-NeMo#2174 / NVIDIA-NeMo#2178) and
`torch.zeros_like(generation_logprobs)` (PR NVIDIA-NeMo#2177). Under the bare
`mock_grpo_components` fixture those inputs are `MagicMock` objects, so
CI failed with `TypeError: zeros_like(): argument 'input' (position 1)
must be Tensor, not MagicMock` at `nemo_rl/algorithms/grpo.py:1801`.

Add a `_patched_logprob_phase` context manager that swaps in real
tensors for `policy.get_logprobs`, `policy.get_reference_policy_logprobs`,
and `batched_message_log_to_flat_message`, and use it in both the sync
and async branches of the two new tests.

Signed-off-by: Linglin Jing <linglinj@nvidia.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@yuki-97
Copy link
Copy Markdown
Contributor

yuki-97 commented May 12, 2026

closes since included in #2443

@yuki-97 yuki-97 closed this May 12, 2026
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