Skip to content

[BugFix] Fix MultiOneHot.to_numpy() returning scalar instead of array#3589

Merged
vmoens merged 1 commit intopytorch:mainfrom
jashshah999:fix/multionehot-to-numpy
Apr 4, 2026
Merged

[BugFix] Fix MultiOneHot.to_numpy() returning scalar instead of array#3589
vmoens merged 1 commit intopytorch:mainfrom
jashshah999:fix/multionehot-to-numpy

Conversation

@jashshah999
Copy link
Copy Markdown
Contributor

Summary

MultiOneHot.to_numpy() inherits from OneHot.to_numpy(), which calls argmax(-1) over the entire concatenated one-hot vector. For a MultiOneHot((11, 3)) spec with a 14-element vector like [0,0,1,0,0,0,0,0,0,0,0,1,0,0], this returns a single scalar 2 instead of the correct array [2, 0].

This also breaks gym MultiDiscrete environment interaction since GymWrapper maps those to MultiOneHot.

Changes

  • Add to_numpy override to MultiOneHot that delegates to to_categorical (which already correctly splits by nvec and applies per-segment argmax)
  • Add parametrized tests covering multiple nvec configs and batch shapes

Fixes #2633

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Apr 3, 2026

🔗 Helpful Links

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

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

❌ 2 New Failures, 1 Cancelled Job

As of commit 5c52391 with merge base e2c8a8d (image):

NEW FAILURES - The following jobs have failed:

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

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 Apr 3, 2026
@github-actions github-actions Bot added the BugFix label Apr 3, 2026
Copy link
Copy Markdown
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.

Wondering if getting a categorical value out of a one-hot when calling to_numpy() is the expected behavior?
Is it documented as such?
What happens for OneHot for instance, do we get a numpy scalar?

Copy link
Copy Markdown
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.

Ok read your comment on the PR, LGTM then!

@vmoens vmoens merged commit 91c4087 into pytorch:main Apr 4, 2026
139 of 145 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BugFix 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

Development

Successfully merging this pull request may close these issues.

[BUG] The class MultiOneHot(OneHot) uses the "to_numpy" method from the OneHot class which do not support multionehot vectors.

2 participants