Skip to content

[Quality] Reject stateful transforms placed after a key-dropping inverse - #4571

Open
itwasabhi wants to merge 3 commits into
pytorch:mainfrom
itwasabhi:transform_safety
Open

itwasabhi wants to merge 3 commits into
pytorch:mainfrom
itwasabhi:transform_safety

Conversation

@itwasabhi

Copy link
Copy Markdown
Contributor

Usability improvement with transforms.

Description

TransformedEnv._step hands every transform the input data after the whole transform chain is inverted. Some transforms ---Stack, RenameTransform(create_copy=False) and ExcludeTransform(inverse=True)--- remove entries in their inverse, so a stateful transform placed after them (e.g. RewardSum under the stacked key) silently read its state as missing at every step.

Here we add an opt-in Transform._inv_drops_key hook and make Compose raise a ValueError at spec-building time when a transform registers a state entry that an earlier transform's inverse drops. This is primarily a usability improvement to help the user avoid an error-prone transform chain.

Stack, RenameTransform and ExcludeTransform are updated to implement inv_drops_key.

Motivation and Context

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of examples)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

TransformedEnv._step hands every transform the input data after the whole
chain was inverted. Stack, RenameTransform(create_copy=False) and
ExcludeTransform(inverse=True) remove entries in their inverse, so a stateful
transform placed after them (e.g. RewardSum under the stacked key) silently
read its state as missing at every step.

Here we add an opt-in Transform._inv_drops_key hook and make Compose raise a
ValueError at spec-building time when a transform registers a state entry
that an earlier transform's inverse drops. This is primarily a usability improvement
to help the user avoid an error-prone transform chain.

Stack, RenameTransform and ExcludeTransform are updated to implement inv_drops_key.

This commit was co-authored by claude.
@pytorch-bot

pytorch-bot Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4571

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 16 Awaiting Approval

As of commit 6f69164 with merge base fd33226 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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 Oct 14, 2026
@itwasabhi

Copy link
Copy Markdown
Contributor Author

@theap06 can i request a review here?

@theap06
theap06 self-requested a review October 23, 2026 05:23
Comment thread torchrl/envs/transforms/_base.py Outdated
# in ``_step``. If the inverse of a previous transform removed it,
# ``t`` would silently read it as missing at every step.
for key in self._state_keys(input_spec) - state_keys:
for previous in self.transforms[:i]:

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.

wont this only check transforms before t? the _step runs after the full inverse chain. the check needs to look at the whole chain.

Comment thread torchrl/envs/transforms/_base.py Outdated
for key in self._state_keys(input_spec) - state_keys:
for previous in self.transforms[:i]:
if previous._inv_drops_key(key):
raise ValueError(

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.

this needs a warning naming the removal version first before it becomes a raised error.

@itwasabhi

Copy link
Copy Markdown
Contributor Author

@theap06 ready for review

This branch has not been deployed

No deployments
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. quality code quality Transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants