Skip to content

[BugFix] Add strict_shape parameter to QValueModule for action shape enforcement#3593

Merged
vmoens merged 2 commits intopytorch:mainfrom
Lidang-Jiang:fix/qvalue-actor-action-spec-shape
Apr 5, 2026
Merged

[BugFix] Add strict_shape parameter to QValueModule for action shape enforcement#3593
vmoens merged 2 commits intopytorch:mainfrom
Lidang-Jiang:fix/qvalue-actor-action-spec-shape

Conversation

@Lidang-Jiang
Copy link
Copy Markdown
Contributor

Summary

Fixes #3059 — QValueActor now respects action_spec shape for singleton dimensions.

When using Categorical specs with singleton dimensions (e.g., shape=(1, 1)), argmax(dim=-1) drops the trailing dimension, causing the action shape to not match the spec. This adds a strict_shape parameter to QValueModule and QValueActor following the approach suggested by @vmoens in #3059.

strict_shape parameter

Value Behavior
None (default) FutureWarning on shape mismatch (backward compatible)
"auto" Automatically reshape action to match spec
True RuntimeError on shape mismatch
False Silently allow mismatch
Before
action_spec = Categorical(4, shape=torch.Size((1, 1)), dtype=torch.int64)
module = TensorDictModule(module=nn.Linear(3, 1), in_keys=("observation",), out_keys=("action_value",))
qvalue_actor = QValueActor(module=module, in_keys=["observation"], spec=action_spec)
td = TensorDict({"observation": torch.randn(12, 3)})
qvalue_actor(td)
print(td["action"].shape)  # torch.Size([12])  ← should be [12, 1]
After
qvalue_actor = QValueActor(module=module, in_keys=["observation"], spec=action_spec, strict_shape="auto")
td = TensorDict({"observation": torch.randn(12, 3)})
qvalue_actor(td)
print(td["action"].shape)  # torch.Size([12, 1])  ← correct!
Test results (55 passed)
test/test_actors.py::TestQValue (55 tests including 4 new)
test_qvalue_actor_strict_shape_auto PASSED
test_qvalue_actor_strict_shape_true_raises PASSED
test_qvalue_actor_strict_shape_none_warns PASSED
test_qvalue_actor_strict_shape_normal_no_warning PASSED
======================= 55 passed, 24 warnings in 1.45s ========================

Test plan

  • 4 new tests: auto reshape, strict raise, default warning, no false positive
  • All 51 existing QValue tests pass (no regression)

…enforcement

When using Categorical specs with singleton dimensions, argmax drops
the trailing dim causing action shape to not match the spec.

Add strict_shape parameter to QValueModule and QValueActor:
- None (default): FutureWarning on shape mismatch
- 'auto': automatically reshape action to match spec
- True: raise RuntimeError on mismatch
- False: silently allow mismatch

Fixes pytorch#3059

Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot bot commented Apr 4, 2026

🔗 Helpful Links

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

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

❌ 28 New Failures, 1 Cancelled Job, 2 Pending

As of commit e6fcc31 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
Copy link
Copy Markdown

meta-cla bot commented Apr 4, 2026

Hi @Lidang-Jiang!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Apr 4, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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

Thanks for this!
The linter needs to be fixed, and I would mention the version where the change of behavior will occur (0.14)

warnings.warn(
f"Action shape {action.shape} does not match expected shape {target_shape} "
f"(per-sample spec shape: {per_sample_shape}). "
f"In a future version, this will raise an error. "
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.

2 versions from now, which is in v0.14

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in e6fcc31. Updated the warning message to specify v0.14 as the deprecation version. Also fixed linter formatting issues.

- Fix linter formatting (line length) in actors.py and test_actors.py
- Specify deprecation version (v0.14) in FutureWarning message

Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
@github-actions github-actions bot added the BugFix label Apr 5, 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.

LGTM thanks

@vmoens vmoens merged commit d1917f2 into pytorch:main Apr 5, 2026
93 of 122 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. Modules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] QValueActor does not respect action_spec

2 participants