[Feature] Mask-aware BCLoss; LossModule._reduce_loss honors ("collect…#3850
Open
theap06 wants to merge 1 commit into
Open
[Feature] Mask-aware BCLoss; LossModule._reduce_loss honors ("collect…#3850theap06 wants to merge 1 commit into
theap06 wants to merge 1 commit into
Conversation
…or", "mask") Closes the loss-side gap from the sequence-RL composability work landed in pytorch#3695: SliceSampler(pad_output=True) writes ("collector", "mask") alongside the padded batch, but no loss in the repo was reading that mask. Padded positions were silently averaged into the gradient. This PR: - Extends LossModule._reduce_loss (torchrl/objectives/common.py) to look up ("collector", "mask") first, falling back to the legacy "shifted_valid" key so the existing PPO / A2C / Reinforce callers retain their behavior exactly. - Migrates BCLoss.forward() from `_reduce(loss, reduction=self.reduction)` to `self._reduce_loss(loss, tensordict=tensordict)` as the reference adoption. When the mask key is absent the output is byte-identical to the old path; when present, padded positions are excluded from the time-averaging. - Adds 5 mask-aware tests in test/objectives/test_bc.py: * back-compat without mask key * all-True mask is a no-op * partial mask matches the loss computed on the real subset * gradient through masked loss matches gradient through subset * all-False mask reduces to 0 without NaN (denominator clamp) Follow-up PRs can adopt the same one-line change in PPO, A2C, IQL, CQL, SAC, DDPG, TD3, GAIL, DT, and Reinforce. Each is reviewable in minutes once this base pattern lands. The reduction infrastructure on _reduce(..., mask=, weights=) already existed; this PR just wires the input-side tensordict lookup into the common helper that losses already inherit.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3850
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
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.
##Summary
Closes the loss-side gap from the sequence-RL composability work landed in #3695: SliceSampler(pad_output=True) writes ("collector", "mask") alongside the padded batch, but no loss in the repo was reading that mask. Padded positions were silently averaged into the gradient.
Extends LossModule._reduce_loss (torchrl/objectives/common.py) to look up ("collector", "mask") first, falling back to the legacy "shifted_valid" key so the existing PPO / A2C / Reinforce callers retain their behavior exactly.
Migrates BCLoss.forward() from
_reduce(loss, reduction=self.reduction)toself._reduce_loss(loss, tensordict=tensordict)as the reference adoption. When the mask key is absent the output is byte-identical to the old path; when present, padded positions are excluded from the time-averaging.Adds 5 mask-aware tests in test/objectives/test_bc.py: