Repository navigation
Description
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_logprobsThis 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 configError 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 implementation2. 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 implementation3. 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):
- Worker patch: Injects early-return guards into
use_reference_model(),use_training_model(), andget_reference_policy_logprobs()whenself.reference_state_dict is None - GRPO patch: Wraps
get_reference_policy_logprobs()in try/except, and injectszeros_likeforreference_policy_logprobsbeforepolicy.train()when the value is None or has wrong shape
Activity
dmvevents commentedon Mar 18, 2026
Ping on this — labeled
needs-follow-up, open ~30 days. There are two distinct bugs here: one in the sync GRPO generator path and oneNameErrorin the reference model path. If it helps triage, I can split this into two separate issues.yuki-97 commentedon Mar 25, 2026
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?dmvevents commentedon Apr 11, 2026
@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.
dmvevents commentedon Apr 12, 2026
@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.
yuki-97 commentedon Apr 13, 2026
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?
dmvevents commentedon Apr 28, 2026
@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
RuntimeErrorfrom the generator path and the reference policy pathNameError.yuki-97 commentedon May 8, 2026
hi @dmvevents , thanks for raising this!
We had some discussions and decide to move forward based on #2177 + #2178. @jinglinglingling will help on this.
dmvevents commentedon May 8, 2026
@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.
fix: skip_reference_policy_logprobs_calculation=true crashes training
feat(grpo): auto-enable skip_reference_policy_logprobs when KL=0
test(grpo): add unit tests for skip-ref-logprobs and force-on-policy-…
yuki-97 commentedon May 12, 2026
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.