Skip to content

[BugFix] Convert Mapping values for TensorDict fields in tensor-only tensorclasses - #1753

Merged
vmoens merged 3 commits into
pytorch:mainfrom
peterdsharpe:codex/tensorclass-mapping
Aug 14, 2026
Merged

[BugFix] Convert Mapping values for TensorDict fields in tensor-only tensorclasses#1753
vmoens merged 3 commits into
pytorch:mainfrom
peterdsharpe:codex/tensorclass-mapping

Conversation

@peterdsharpe

Copy link
Copy Markdown
Contributor

Description

This PR converts arbitrary collections.abc.Mapping values assigned to
TensorDict-annotated fields on tensor_only=True tensorclasses.

The annotation-aware conversion applies consistently during construction,
attribute assignment, .set(), and from_dataclass. It also:

  • recursively normalizes nested mappings;
  • supports generic and optional/union TensorDict annotations;
  • preserves existing TensorDict instances, including identity and lock state;
  • leaves mappings annotated as Any or NonTensorData unchanged;
  • preserves the direct constructor fast path when no relevant fields exist.

Motivation and Context

Closes #1752.

Non-dict Mapping implementations were previously wrapped as NonTensorData
even when the destination field was annotated as TensorDict. The fix is
scoped using resolved field annotations so that it does not globally reinterpret
mappings intended to remain non-tensor data.

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

Test plan

Regression tests cover:

  • dict and UserDict inputs;
  • nested mappings;
  • construction, attribute assignment, and .set();
  • generic and optional TensorDict annotations;
  • class and instance forms of from_dataclass;
  • preservation of existing and nested locked TensorDict instances;
  • Any and NonTensorData mapping fields remaining non-tensor.

Local validation:

  • pytest test/tensorclass/test_tensorclass.py -k "TestTensorOnly":
    16 passed
  • pytest test/tensorclass/test_tensorclass.py -k "not multiprocessing":
    160 passed, 1 skipped, 1 deselected
  • pytest test/tensorclass/test_compatibility.py test/tensorclass/test_typedtensordict.py:
    174 passed, 14 skipped
  • Ruff and git diff --check pass

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

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

@meta-cla

meta-cla Bot commented Jul 21, 2026

Copy link
Copy Markdown

Hi @peterdsharpe!

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!

@github-actions github-actions Bot added bug Something isn't working Test tensorclass labels Jul 21, 2026
@peterdsharpe
peterdsharpe marked this pull request as ready for review July 21, 2026 13:55
@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 Jul 28, 2026
@meta-cla

meta-cla Bot commented Jul 28, 2026

Copy link
Copy Markdown

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

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

Made a small edit in 849271b, otherwise lgtm!

@vmoens
vmoens merged commit 54a5107 into pytorch:main Aug 14, 2026
3 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. tensorclass Test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] tensor_only tensorclasses wrap non-dict Mapping values assigned to TensorDict fields

2 participants