Skip to content

[BugFix]treat 1‑D MultiDiscrete as MultiCategorical and accept flattened masks#3342

Merged
vmoens merged 7 commits into
pytorch:mainfrom
ParamThakkar123:fix/multdiscrete
Jan 19, 2026
Merged

[BugFix]treat 1‑D MultiDiscrete as MultiCategorical and accept flattened masks#3342
vmoens merged 7 commits into
pytorch:mainfrom
ParamThakkar123:fix/multdiscrete

Conversation

@ParamThakkar123

@ParamThakkar123 ParamThakkar123 commented Jan 17, 2026

Copy link
Copy Markdown
Contributor

Description

Describe your changes in detail.

  • Treat any 1‑D MultiDiscrete.nvec as a single MultiCategorical / MultiOneHot instead of stacking per-dimension Categoricals.
  • Build the observation spec first in the Gym wrapper and, when an env-provided action_mask shape exactly matches the MultiDiscrete nvec, prefer a flattened Categorical / OneHot action spec so masks align.
  • Make Categorical.update_mask accept flattened/grid masks (reshape when product == n) and coerce them to boolean on the spec device before expansion.

Motivation and Context

Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.
You can use the syntax close #15213 if this solves the issue #15213

Fixes #3242

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

@pytorch-bot

pytorch-bot Bot commented Jan 17, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 4 New Failures, 1 Cancelled Job, 3 Unrelated Failures

As of commit c94424b with merge base 9ee9e90 (image):

NEW FAILURES - The following jobs have failed:

CANCELLED JOB - The following job was cancelled. Please retry:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Jan 17, 2026
@ParamThakkar123 ParamThakkar123 changed the title fix(gym):treat 1‑D MultiDiscrete as MultiCategorical and accept flattened masks [BugFix]:treat 1‑D MultiDiscrete as MultiCategorical and accept flattened masks Jan 17, 2026
@ParamThakkar123 ParamThakkar123 changed the title [BugFix]:treat 1‑D MultiDiscrete as MultiCategorical and accept flattened masks [BugFix]treat 1‑D MultiDiscrete as MultiCategorical and accept flattened masks Jan 17, 2026
@vmoens vmoens added the bug Something isn't working label Jan 18, 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.


✅ Approved — Thank you for this contribution!

@ParamThakkar123 Thanks so much for tackling issue #3242! This is a real pain point for board game environments and similar use cases with 2D action masks.

I took the liberty of making a few small patches on my end — apologies for not waiting for a revision cycle:

  1. Simplified update_mask in tensor_specs.py — Removed the try/except blocks and getattr pattern in favor of direct access (our codebase convention is to fail loudly rather than silently swallow errors)

  2. Removed blanket exception handling in gym.py — Same reasoning; added an explanatory comment instead

  3. Added a testtest_multidiscrete_action_mask_gym that reproduces the original bug scenario

  4. Added documentation — Note in the ActionMask docstring explaining the MultiDiscrete flattening behavior

The core logic of your fix is spot-on. Thanks again for the contribution! 🙏

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


✅ Approved — Thank you for this contribution!

@ParamThakkar123 Thanks so much for tackling issue #3242! This is a real pain point for board game environments and similar use cases with 2D action masks.

I took the liberty of making a few small patches on my end — apologies for not waiting for a revision cycle:

  1. Simplified update_mask in tensor_specs.py — Removed the try/except blocks and getattr pattern in favor of direct access (our codebase convention is to fail loudly rather than silently swallow errors)

  2. Removed blanket exception handling in gym.py — Same reasoning; added an explanatory comment instead

  3. Added a testtest_multidiscrete_action_mask_gym that reproduces the original bug scenario

  4. Added documentation — Note in the ActionMask docstring explaining the MultiDiscrete flattening behavior

The core logic of your fix is spot-on. Thanks again for the contribution! 🙏

@ParamThakkar123

Copy link
Copy Markdown
Contributor Author

Thank you so much @vmoens 🫡

@ParamThakkar123

Copy link
Copy Markdown
Contributor Author

I think it fixes this issue too ? #3197

@vmoens vmoens linked an issue Jan 19, 2026 that may be closed by this pull request
@vmoens vmoens merged commit be56457 into pytorch:main Jan 19, 2026
101 of 109 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.

Projects

None yet

2 participants