Skip to content

Conversation

@az0uz
Copy link

@az0uz az0uz commented Oct 30, 2025

Description

This PR introduces TensorClassModuleBase, a new base class that provides a type-safe way to define PyTorch modules that operate on TensorClass instances. This implementation offers compile-time type checking and seamless integration with TensorDict-based workflows.

Key Features:

  • Type-safe module definition: Generic base class TensorClassModuleBase[InputClass, OutputClass] with automatic type extraction
  • TensorDict integration: TensorClassModuleWrapper enables conversion to TensorDictModule via as_td_module() method
  • Nested TensorClass support: Automatically handles nested TensorClass structures and extracts all keys
  • ONNX export compatibility: Full support for ONNX export with proper input/output handling

Implementation Details:

  • New module: tensordict/nn/tensorclass_module.py with TensorClassModuleBase and TensorClassModuleWrapper
  • Automatic key extraction from TensorClass type annotations using _tensor_class_keys()
  • Generic type parameter extraction via __init_subclass__ for clean API
  • Comprehensive test suite in test/test_tensorclass_module.py covering forward pass, TensorDict conversion, ONNX export, and edge cases

Additional Changes:

  • Exported new classes in tensordict/__init__.py and tensordict/nn/__init__.py

Motivation and Context

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

implements #1355

This change addresses the need for type-safe module definitions when working with TensorClass instances. Previously, developers had to work directly with TensorDict or manually handle type conversions, which was error-prone and lacked compile-time type checking.

With TensorClassModuleBase, developers can:

  1. Define modules with explicit input/output TensorClass types
  2. Get IDE autocomplete and type checking for TensorClass fields
  3. Seamlessly integrate with existing TensorDict-based workflows
  4. Export to ONNX while maintaining type safety

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 (via comprehensive docstrings and examples).

@meta-cla
Copy link

meta-cla bot commented Oct 30, 2025

Hi @az0uz!

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!

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.

Thanks for this!
Overall great feature!
I left some comments here and there but they're pretty minor.
Can you sign the CLA? It's required to run the tests and merge

Copy link
Collaborator

Choose a reason for hiding this comment

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

new classes need to be added to the doc

Copy link
Author

@az0uz az0uz Nov 17, 2025

Choose a reason for hiding this comment

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

Do I need to write anything else than adding them to docs/source/reference/nn.rst like above?

I've added a section with description and examples. let me know if that works for you.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's really cool thx

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.

Thanks for this!
Overall great feature!
I left some comments here and there but they're pretty minor.
Can you sign the CLA? It's required to run the tests and merge

@az0uz
Copy link
Author

az0uz commented Nov 5, 2025

The person responsible for the existing cla in my company has asked cla@ to add me, but haven't got any answer yet, I'll ping them again. Do I need to also sign it personally?

@vmoens
Copy link
Collaborator

vmoens commented Nov 5, 2025

The person responsible for the existing cla in my company has asked cla@ to add me, but haven't got any answer yet, I'll ping them again. Do I need to also sign it personally?

No the company one should be fine. If we need to ping anyone on the Meta side LMK!

@az0uz
Copy link
Author

az0uz commented Nov 7, 2025

If we need to ping anyone on the Meta side LMK!

Yes that would be great, no answer for a week, we've tried to ping cla@meta.com again yesterday but still no news.

@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 Nov 12, 2025
@az0uz az0uz requested a review from vmoens November 17, 2025 17:34
@vmoens vmoens changed the title feat: Add TensorClassModuleBase for type-safe TensorClass modules [Feature] Add TensorClassModuleBase for type-safe TensorClass modules Nov 25, 2025
@vmoens vmoens added the enhancement New feature or request label Nov 25, 2025
@vmoens
Copy link
Collaborator

vmoens commented Nov 25, 2025

GHA seems down -- we'll need to wait till tests run for this

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.

looking good!

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's really cool thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants