Skip to content

[BugFix] ActionMask is compatible with composite action specs #3022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025

Conversation

louisfaury
Copy link
Contributor

Description

ActionMask was not compatible with composite action specs because of the lines:

action_spec = self.container.full_action_spec
        keys = self.container.action_keys
        if len(keys) == 1:
            action_spec = action_spec[keys[0]]

that afaiu do not serve a real purpose.

This PR removes this check, and allows tu use ActionMask to mask one factor of a composite action spec.

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.

Copy link

pytorch-bot bot commented Jun 20, 2025

🔗 Helpful Links

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

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

❌ 5 New Failures, 21 Pending, 22 Unrelated Failures

As of commit 32c62e4 with merge base 5a13341 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-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 20, 2025
Comment on lines -8776 to -8777
>>> env = TransformedEnv(base_env, ActionMask())
>>> r = env.rollout(10)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Already in the lines above.

Comment on lines +8729 to +8732
This transform is useful to ensure that randomly generated actions
respect legal actions, by masking the action specs.
It reads the mask from the input tensordict after the step is executed,
and adapts the mask of the finite action spec.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Took the occasion to refine a bit the docstring.

Comment on lines -8814 to -8817
action_spec = self.container.full_action_spec
keys = self.container.action_keys
if len(keys) == 1:
action_spec = action_spec[keys[0]]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

tbh I could not really see why this check was in place. I hope I did not miss any chesterton fence!

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks good, there is some confusion in the existing code about in_keys and action_keys but this seems to be aligned with what we want.

We should maybe have a validation step when we register a transform in an env. For example here, when we set the container to the transform, we should check that the action_key arg is part of the action_keys of the parent env.

Comment on lines -8842 to -8851
action_spec = self.action_spec
mask = tensordict.get(self.in_keys[1], None)
if mask is not None:
mask = mask.to(action_spec.device)
action_spec.update_mask(mask)

# TODO: Check that this makes sense
with _set_missing_tolerance(self, True):
tensordict_reset = self._call(tensordict_reset)
return tensordict_reset
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this entire logic is already handled by _call

@vmoens vmoens added bug Something isn't working Environments Adds or modifies an environment wrapper labels Jun 20, 2025
@vmoens vmoens force-pushed the lf/action-mask-many-actions branch from 630b39d to 32c62e4 Compare June 20, 2025 13:52
Copy link
Collaborator

@vmoens vmoens left a comment

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 92b52a0 into pytorch:main Jun 20, 2025
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Environments Adds or modifies an environment wrapper
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants