[Refactor] Migrate remaining LossModules to mask-aware reduction (#3866) - #3888
Merged
vmoens merged 2 commits intoJun 22, 2026
Merged
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3888
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
gtnv
added a commit
to gtnv/rl
that referenced
this pull request
Aug 2, 2026
PR pytorch#3888 routed objectives through _reduce_loss, but the helper still ignored ("collector", "mask") because the corresponding change in pytorch#3850 never merged. This fix excludes padded timesteps from loss reduction so they do not affect the loss or gradients.
vmoens
added a commit
to coder-jayp/rl
that referenced
this pull request
Aug 4, 2026
…se-batch support Follow-ups to review, on top of the rebase onto main (which drops the objectives migration already merged as pytorch#3888): - Loss fields are discovered by iterating the TensorClass loss output instead of a hardcoded 11-name allowlist, so a new loss term is logged automatically. Plain objects still work via their attribute dict. - Metric-computation failures warn once per site at WARNING level instead of five blanket except-Exception blocks logging at debug: an observability component that silently emits nothing is worse than one that raises. - training/grad_norm is omitted on accumulation steps rather than logged as a literal 0.0, which polluted any aggregate over the series. - batch/reward_std is omitted for single-element batches (std would be NaN). - as_list=True returns a plain tensor for dense (padded) batches; all three read sites now handle both the ragged and the dense form. The dense form previously made every batch/* metric silently disappear. - Deprecations follow CLAUDE.md 12: the legacy GRPO keys are emitted again during the window (previously they were warned about but not emitted, and three keys plus two renames had no deprecation at all), the category is FutureWarning (DeprecationWarning is hidden by default), and the removal target is v0.16.0 (two minors after v0.14; version.txt is 0.13). - Runnable Examples block on the class docstring, verified by execution. test/test_loggers.py: 50 passed. test/objectives: 7491 passed post-rebase. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
vmoens
added a commit
to coder-jayp/rl
that referenced
this pull request
Aug 5, 2026
…se-batch support Follow-ups to review, on top of the rebase onto main (which drops the objectives migration already merged as pytorch#3888): - Loss fields are discovered by iterating the TensorClass loss output instead of a hardcoded 11-name allowlist, so a new loss term is logged automatically. Plain objects still work via their attribute dict. - Metric-computation failures warn once per site at WARNING level instead of five blanket except-Exception blocks logging at debug: an observability component that silently emits nothing is worse than one that raises. - training/grad_norm is omitted on accumulation steps rather than logged as a literal 0.0, which polluted any aggregate over the series. - batch/reward_std is omitted for single-element batches (std would be NaN). - as_list=True returns a plain tensor for dense (padded) batches; all three read sites now handle both the ragged and the dense form. The dense form previously made every batch/* metric silently disappear. - Deprecations follow CLAUDE.md 12: the legacy GRPO keys are emitted again during the window (previously they were warned about but not emitted, and three keys plus two renames had no deprecation at all), the category is FutureWarning (DeprecationWarning is hidden by default), and the removal target is v0.16.0 (two minors after v0.14; version.txt is 0.13). - Runnable Examples block on the class docstring, verified by execution. test/test_loggers.py: 50 passed. test/objectives: 7491 passed post-rebase. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
vmoens
pushed a commit
to gtnv/rl
that referenced
this pull request
Aug 5, 2026
PR pytorch#3888 routed objectives through _reduce_loss, but the helper still ignored ("collector", "mask") because the corresponding change in pytorch#3850 never merged. This fix excludes padded timesteps from loss reduction so they do not affect the loss or gradients.
vmoens
pushed a commit
to gtnv/rl
that referenced
this pull request
Aug 7, 2026
PR pytorch#3888 routed objectives through _reduce_loss, but the helper still ignored ("collector", "mask") because the corresponding change in pytorch#3850 never merged. This fix excludes padded timesteps from loss reduction so they do not affect the loss or gradients.
vmoens
added a commit
to coder-jayp/rl
that referenced
this pull request
Aug 8, 2026
…se-batch support Follow-ups to review, on top of the rebase onto main (which drops the objectives migration already merged as pytorch#3888): - Loss fields are discovered by iterating the TensorClass loss output instead of a hardcoded 11-name allowlist, so a new loss term is logged automatically. Plain objects still work via their attribute dict. - Metric-computation failures warn once per site at WARNING level instead of five blanket except-Exception blocks logging at debug: an observability component that silently emits nothing is worse than one that raises. - training/grad_norm is omitted on accumulation steps rather than logged as a literal 0.0, which polluted any aggregate over the series. - batch/reward_std is omitted for single-element batches (std would be NaN). - as_list=True returns a plain tensor for dense (padded) batches; all three read sites now handle both the ragged and the dense form. The dense form previously made every batch/* metric silently disappear. - Deprecations follow CLAUDE.md 12: the legacy GRPO keys are emitted again during the window (previously they were warned about but not emitted, and three keys plus two renames had no deprecation at all), the category is FutureWarning (DeprecationWarning is hidden by default), and the removal target is v0.16.0 (two minors after v0.14; version.txt is 0.13). - Runnable Examples block on the class docstring, verified by execution. test/test_loggers.py: 50 passed. test/objectives: 7491 passed post-rebase. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Migrates remaining
LossModulesubclasses to use the mask-awareself._reduce_loss(loss, tensordict=tensordict)helper instead of direct_reduce(loss, reduction=self.reduction)calls, as requested in #3866.Modules migrated:
cql.py(CQLLoss, DiscreteCQLLoss)crossq.py(CrossQLoss)ddpg.py(DDPGLoss)decision_transformer.py(DTLoss, OnlineDTLoss)deprecated.pydqn.py(DQNLoss, DistributionalDQNLoss)gail.py(GAILLoss)iql.py(IQLLoss, DiscreteIQLLoss)llm/grpo.py(GRPOLoss)ppo.py(PPOLoss, ClipPPOLoss, KLPENPPOLoss)redq.py(REDQLoss)sac.py(SACLoss, DiscreteSACLoss)td3.py(TD3Loss)td3_bc.py(TD3BCLoss)bc.pywas intentionally left untouched (already migrated in #3850 as the reference case).Motivation and Context
Closes the loss-side gap from the sequence-RL composability work.
SliceSampler(pad_output=True)which produces batches with a("collector", "mask")key.LossModule._reduce_lossand migrated BCLoss.Behaviour is byte-identical when no mask is present. When the mask exists, padded positions are correctly excluded from reduction.
Types of changes
Checklist
pytest test/objectives/)