Skip to content

Change NamedTupleVariable implementation to subclass UserDefinedTupleVariable#167468

Closed
morrison-turnansky wants to merge 28 commits intopytorch:mainfrom
morrison-turnansky:named-tuple
Closed

Change NamedTupleVariable implementation to subclass UserDefinedTupleVariable#167468
morrison-turnansky wants to merge 28 commits intopytorch:mainfrom
morrison-turnansky:named-tuple

Conversation

@morrison-turnansky
Copy link
Collaborator

@morrison-turnansky morrison-turnansky commented Nov 10, 2025

Continuation of work from previous PR, see link for context #161645 (comment)

I think this PR is a step in that direction. There is probably some room for simplification.

At a high level, the new class NamedTupleVariable handles methods that branch on structseq or the more dynamic subclasses of namedtuple, and falls back to UserDefinedTupleVariable otherwise.

Please let me know what you think. @StrongerXi

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @Lucaskabela @jataylo @chenyang78

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 10, 2025

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit 992fe52 with merge base d2237eb (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@morrison-turnansky
Copy link
Collaborator Author

morrison-turnansky commented Nov 10, 2025

I went through the repo and grabbed namedtuple tests. The following pass locally with python 3.13 (required for collections tests)
test/dynamo/test_misc.py::MiscTests::test_namedtuple1
test/dynamo/test_misc.py::MiscTests::test_namedtuple2
test/dynamo/test_misc.py::MiscTests::test_namedtuple3
test/dynamo/test_misc.py::MiscTests::test_namedtuple_class
test/dynamo/test_misc.py::MiscTests::test_namedtuple_with_custom_getitem
test/dynamo/test_misc.py::MiscTests::test_namedtuple_source_dynamic_attributes
test/dynamo/test_misc.py::MiscTests::test_namedtuple_sourceless_dynamic_attributes
test/dynamo/test_misc.py::MiscTests::test_sourceless_namedtuple
test/dynamo/test_misc.py::MiscTests::test_structseq1
test/dynamo/test_misc.py::MiscTests::test_structseq2
test/dynamo/test_functions.py::FunctionTests::test_namedtuple
test/dynamo/test_functions.py::FunctionTests::test_namedtuple_defaults
test/dynamo/test_functions.py::FunctionTests::test_namedtuple_replace
test/dynamo/test_functions.py::FunctionTests::test_namedtuple_fields
test/dynamo/test_functions.py::FunctionTests::test_namedtuple_user_methods
test/dynamo/test_functions.py::FunctionTests::test_generic_namedtuple_user_methods
test/dynamo/test_functions.py::FunctionTests::test_namedtuple_hasattr
test/dynamo/test_functions.py::FunctionTests::test_generic_namedtuple_hasattr
test/dynamo/test_functions.py::FunctionTests::test_namedtuple_subclass
test/dynamo/test_functions.py::FunctionTests::test_generic_namedtuple_subclass
test/dynamo/test_functions.py::DefaultsTests::test_udf_namedtuple
test/dynamo/test_dicts.py::DictTests::test_dict_namedtuple
test/dynamo/test_dicts.py::DictTests::test_lazy_key_non_const_guarding
test/dynamo/test_modules.py::NNModuleTests::test_lazy_module7
test/dynamo/test_subclasses.py::SubclassTests::test_subclass_constructor_proxying
PYTORCH_TEST_WITH_DYNAMO=1 test/dynamo/cpython/3_13/test_collections.py::TestNamedTuple

@morrison-turnansky morrison-turnansky force-pushed the named-tuple branch 2 times, most recently from 597d115 to 768d30d Compare November 11, 2025 12:51
@morrison-turnansky morrison-turnansky changed the title Named tuple Change NamedTupleVariable implementation to subclass UserDefinedTupleVariable Nov 12, 2025
@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Nov 13, 2025
@morrison-turnansky
Copy link
Collaborator Author

@isuruf addressed failing tests, we should be good now

Copy link
Contributor

@StrongerXi StrongerXi left a comment

Choose a reason for hiding this comment

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

This looks great! Could you please keep NamedTupleVariable inside lists.py though, for 2 reasons:

  1. To minimize changes and make the diff clearer.
  2. We already have more non-primitive container classes like deque and torch.Size emulated inside lists.py, so it makes sense to keep named tuple there as well.

I think you can avoid circular dependency by moving this to within where it's used:

from .lists import SizeVariable

elif self.value is torch.Size:
# This simulates `THPSize_pynew`, the C impl for `Size.__new__`.
tup = variables.BuiltinVariable(tuple).call_function(tx, args, kwargs)
return SizeVariable(tup.items)

@morrison-turnansky
Copy link
Collaborator Author

@StrongerXi I moved NamedTupleVariable back to list. You'll see some additional changes to fix circular dependencies besides what you listed. In addition, since typing errors in user_defined.py is ignored, I had to make some linting changes. Everything else is more or less the same.

Copy link
Collaborator

@guilhermeleobas guilhermeleobas left a comment

Choose a reason for hiding this comment

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

One a few minor comments but the code looks good to me. Thanks for the contribution @morrison-turnansky

StrongerXi
StrongerXi previously approved these changes Nov 18, 2025
Copy link
Contributor

@StrongerXi StrongerXi left a comment

Choose a reason for hiding this comment

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

Dropped a nit on docs, otherwise LGTM! Please address Guilherme's comments too. Thank you for this clean up!

mlazos
mlazos previously approved these changes Nov 19, 2025
@morrison-turnansky
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 19, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@StrongerXi
Copy link
Contributor

@pytorchbot merge -f "unrelated build error"

@pytorch-bot pytorch-bot bot removed the ciflow/trunk Trigger trunk jobs on your pull request label Jan 12, 2026
@morrison-turnansky
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot
Copy link

pytorch-bot bot commented Jan 12, 2026

Pull workflow has not been scheduled for the PR yet. It could be because author doesn't have permissions to run those or skip-checks keywords were added to PR/commits, aborting merge. Please get/give approval for the workflows and/or remove skip ci decorators before next merge attempt. If you think this is a mistake, please contact PyTorch Dev Infra.

@morrison-turnansky
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot
Copy link

pytorch-bot bot commented Jan 12, 2026

Pull workflow has not been scheduled for the PR yet. It could be because author doesn't have permissions to run those or skip-checks keywords were added to PR/commits, aborting merge. Please get/give approval for the workflows and/or remove skip ci decorators before next merge attempt. If you think this is a mistake, please contact PyTorch Dev Infra.

@morrison-turnansky
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot
Copy link

pytorch-bot bot commented Jan 12, 2026

Pull workflow has not been scheduled for the PR yet. It could be because author doesn't have permissions to run those or skip-checks keywords were added to PR/commits, aborting merge. Please get/give approval for the workflows and/or remove skip ci decorators before next merge attempt. If you think this is a mistake, please contact PyTorch Dev Infra.

@morrison-turnansky
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot
Copy link

pytorch-bot bot commented Jan 12, 2026

Pull workflow has not been scheduled for the PR yet. It could be because author doesn't have permissions to run those or skip-checks keywords were added to PR/commits, aborting merge. Please get/give approval for the workflows and/or remove skip ci decorators before next merge attempt. If you think this is a mistake, please contact PyTorch Dev Infra.

@skpark-rh
Copy link
Collaborator

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jan 12, 2026
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-jammy-rocm-py3.10 / test (distributed, 3, 3, linux.rocm.gpu.gfx942.4)

Details for Dev Infra team Raised by workflow job

@skpark-rh
Copy link
Collaborator

@pytorch merge -i

@skpark-rh
Copy link
Collaborator

@pytorchbot merge -i

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: trunk / linux-jammy-rocm-py3.10 / test (distributed, 2, 3, linux.rocm.gpu.gfx942.4)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

mattteochen pushed a commit to mattteochen/pytorch that referenced this pull request Jan 15, 2026
…Variable (pytorch#167468)

Continuation of work from previous PR, see link for context pytorch#161645 (comment)

I think this PR is a step in that direction. There is probably some room for simplification.

At a high level, the new class NamedTupleVariable handles methods that branch on structseq or the more dynamic subclasses of namedtuple, and falls back to UserDefinedTupleVariable otherwise.

Please let me know what you think. @StrongerXi

Pull Request resolved: pytorch#167468
Approved by: https://github.com/Lucaskabela
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/trunk Trigger trunk jobs on your pull request Merged module: dynamo open source Reverted topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants