Skip to content

skip_reference_policy_logprobs_calculation=true crashes training with RuntimeError / NameError #1968

Closed
ShriyaRishab/RL
#4

Description

@dmvevents

Bug Description

Setting grpo.skip_reference_policy_logprobs_calculation=true with loss_fn.reference_policy_kl_penalty=0 crashes training in NeMo RL v0.5.0. There are two distinct bugs:

Bug 1: use_reference_model() generator crash (sync GRPO)

When skip_reference_policy_logprobs_calculation=true, the reference model is never initialized (init_reference_model is not called, so self.reference_state_dict does not exist on the worker). However, the sync GRPO code path at grpo.py L1684-L1692 correctly guards the call:

if not master_config["grpo"].get("skip_reference_policy_logprobs_calculation"):
    train_data["reference_policy_logprobs"] = (
        policy.get_reference_policy_logprobs(logprob_data, timer=timer)["reference_logprobs"]
    )

But base_policy_worker.py L125-149 implements get_reference_policy_logprobs() using with self.use_reference_model():, and use_reference_model() in megatron_policy_worker.py L524-573 is a @contextmanager generator that accesses self.reference_state_dict (L546). When the reference model was never initialized, this attribute does not exist, causing AttributeError. Even if reference_state_dict is set to None, the generator fails because load_state_dict(None) raises, and the @contextmanager decorator converts this to:

RuntimeError: generator didn't yield

Bug 2: Async GRPO path ignores the skip flag entirely

The async GRPO code path at grpo.py L2699-L2704 unconditionally calls get_reference_policy_logprobs() without checking skip_reference_policy_logprobs_calculation:

reference_logprobs = policy.get_reference_policy_logprobs(
    train_data,
    timer=timer,
)["reference_logprobs"]
train_data["reference_policy_logprobs"] = reference_logprobs

This always crashes when reference model was not initialized.

Bug 3: Missing reference_policy_logprobs in train_data causes shape mismatch

Even when Bug 1 is worked around (by catching the exception), train_data["reference_policy_logprobs"] is never set when the call is skipped. Downstream, policy.train(train_data) expects this key to exist and have the same shape as other logprob tensors (e.g., [batch_size, seq_len]). Its absence or None value causes assertion failures or shape mismatches in the loss computation.

Steps to Reproduce

python examples/run_grpo.py \
    +grpo.skip_reference_policy_logprobs_calculation=true \
    ++loss_fn.reference_policy_kl_penalty=0 \
    # ... other standard GRPO config

Error Messages

RuntimeError: generator didn't yield

from base_policy_worker.py:143 (with self.use_reference_model():)

or:

AttributeError: 'MegatronPolicyWorker' object has no attribute 'reference_state_dict'

Environment

  • NeMo RL v0.5.0 (nvcr.io/nvidia/nemo-rl:v0.5.0)
  • Tested on both P5.48xlarge (H100) and P5en.48xlarge (H200)
  • PyTorch 2.x, CUDA 12.x

Suggested Fix

1. Guard use_reference_model() in megatron_policy_worker.py

Add an early return when no reference model is initialized:

@contextmanager
def use_reference_model(self):
    if not hasattr(self, "reference_state_dict") or self.reference_state_dict is None:
        yield  # no-op context manager
        return
    # ... existing implementation

2. Guard get_reference_policy_logprobs() in base_policy_worker.py

Return zeros when no reference model exists:

def get_reference_policy_logprobs(self, *, data, micro_batch_size=None):
    if not hasattr(self, "reference_state_dict") or self.reference_state_dict is None:
        # Return zeros matching expected shape when reference model is skipped
        logprobs = self.get_logprobs(data=data, micro_batch_size=micro_batch_size)
        return_data = BatchedDataDict[ReferenceLogprobOutputSpec]()
        return_data["reference_logprobs"] = torch.zeros_like(logprobs["logprobs"]).cpu()
        return return_data
    # ... existing implementation

3. Add skip check in async GRPO path (grpo.py L2699)

Mirror the sync path's guard:

if not master_config["grpo"].get("skip_reference_policy_logprobs_calculation"):
    reference_logprobs = policy.get_reference_policy_logprobs(
        train_data, timer=timer,
    )["reference_logprobs"]
    train_data["reference_policy_logprobs"] = reference_logprobs
else:
    # Set zeros to maintain expected tensor shape for downstream loss computation
    train_data["reference_policy_logprobs"] = torch.zeros_like(fprop_logprobs)

Workaround

We are currently using runtime monkey-patches to work around this (two scripts applied at container startup):

  1. Worker patch: Injects early-return guards into use_reference_model(), use_training_model(), and get_reference_policy_logprobs() when self.reference_state_dict is None
  2. GRPO patch: Wraps get_reference_policy_logprobs() in try/except, and injects zeros_like for reference_policy_logprobs before policy.train() when the value is None or has wrong shape

Activity

  1. added
    needs-follow-up
    on Feb 19, 2026
  2. dmvevents commented on Mar 18, 2026

    @dmvevents
    ContributorAuthor

    Ping on this — labeled needs-follow-up, open ~30 days. There are two distinct bugs here: one in the sync GRPO generator path and one NameError in the reference model path. If it helps triage, I can split this into two separate issues.

  3. added
    needs-follow-up
    and removed
    needs-follow-up
    on Mar 18, 2026
  4. yuki-97 commented on Mar 25, 2026

    @yuki-97
    Contributor

    hi @dmvevents , sorry for waiting, I noticed @ShriyaRishab had a fix at ShriyaRishab#4.

    @ShriyaRishab it seems the PR is at your fork repo, do you mind file a PR to NVIDIA-NeMo/RL:main?

  5. removed
    needs-follow-up
    on Mar 25, 2026
  6. dmvevents commented on Apr 11, 2026

    @dmvevents
    ContributorAuthor

    @yuki-97 — Thanks for pointing to @ShriyaRishab's fix. I see the PR is still at their fork (ShriyaRishab#4). If it hasn't been filed upstream yet, I'm happy to submit it to NVIDIA-NeMo/RL:main — or if @ShriyaRishab plans to file it, I can wait.

    Just want to make sure the fix covers both bugs we identified: (1) the sync GRPO generator path RuntimeError and (2) the NameError in the reference policy path. Let me know how you'd like to proceed.

  7. dmvevents commented on Apr 12, 2026

    @dmvevents
    ContributorAuthor

    @yuki-97 — Noting that @ShriyaRishab filed PR #2174 on Mar 30 addressing this. It's open and awaiting review. Happy to help test or review if useful.

  8. yuki-97 commented on Apr 13, 2026

    @yuki-97
    Contributor

    hi @dmvevents thanks for reminding, and @ShriyaRishab thanks for helping on this!

    I noticed that #2174 seems part of #2177, @yfw @terrykong could you help to decide which one we should move forward with?

  9. added
    waiting-on-maintainers
    and removed
    needs-follow-up
    on Apr 21, 2026
  10. dmvevents commented on Apr 28, 2026

    @dmvevents
    ContributorAuthor

    @yuki-97 — Thanks for the update. Just checking in on this since it's been a couple weeks: has there been a decision between PR #2174 and PR #2177? Happy to help test either one once there's a path forward.

    Want to make sure both bugs are covered in whichever PR moves forward — the sync GRPO RuntimeError from the generator path and the reference policy path NameError.

  11. added
    waiting-on-maintainers
    and removed
    waiting-on-maintainers
    on Apr 28, 2026
  12. yuki-97 commented on May 8, 2026

    @yuki-97
    Contributor

    hi @dmvevents , thanks for raising this!

    We had some discussions and decide to move forward based on #2177 + #2178. @jinglinglingling will help on this.

  13. dmvevents commented on May 8, 2026

    @dmvevents
    ContributorAuthor

    @yuki-97 thanks for the update — tracking #2177 (skip prev_logprobs when force_on_policy_ratio) and #2178 (skip reference model when KL=0), both currently open. Happy to test either once they land on main.

    @jinglinglingling feel free to ping if you want validation against specific configurations — we're running NeMo RL v0.5.0rc0 on p5en.48xlarge (H200) with EFA.

  14. removed
    waiting-on-maintainers
    on May 8, 2026
  15. added 3 commits that reference this issue on May 9, 2026
    2566326
    c31332e
    b430912
  16. added
    waiting-on-maintainers
    on May 10, 2026
  17. yuki-97 commented on May 12, 2026

    @yuki-97
    Contributor

    hi @dmvevents , #2443 is merged into main and should solve this. you can take a try and feel free to reopen the issue if you still meet some errors.

    and thanks @ShriyaRishab for the contribution! #2443 includes the changes of all the three PRs: #2174, #2177, #2178.

  18. removed
    waiting-on-maintainers
    on May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions