Skip to content

[Refactor] Migrate remaining LossModules to mask-aware reduction (#3866) - #3888

Merged
vmoens merged 2 commits into
pytorch:mainfrom
coder-jayp:feat/mask-aware-reduction-all-losses
Jun 22, 2026
Merged

[Refactor] Migrate remaining LossModules to mask-aware reduction (#3866)#3888
vmoens merged 2 commits into
pytorch:mainfrom
coder-jayp:feat/mask-aware-reduction-all-losses

Conversation

@coder-jayp

Copy link
Copy Markdown
Contributor

Description

Migrates remaining LossModule subclasses to use the mask-aware self._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.py
  • dqn.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.py was 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.

Behaviour is byte-identical when no mask is present. When the mask exists, padded positions are correctly excluded from reduction.

Types of changes

  • New feature / improvement (non-breaking change which adds core functionality)

Checklist

  • I have read the CONTRIBUTING guide
  • Tests pass (pytest test/objectives/)
  • Documentation updated (if needed)

@pytorch-bot

pytorch-bot Bot commented Jun 21, 2026

Copy link
Copy Markdown

🔗 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 SEVs

There 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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 21, 2026
@github-actions github-actions Bot added Objectives llm/ LLM-related PR, triggers LLM CI tests Refactoring Refactoring of an existing feature labels Jun 21, 2026

@vmoens vmoens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks

@vmoens
vmoens merged commit 8d4e228 into pytorch:main Jun 22, 2026
3 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. llm/ LLM-related PR, triggers LLM CI tests Objectives Refactoring Refactoring of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants