Skip to content

NamedTuple: Allow side effects for dynamic attributes#161645

Closed
morrison-turnansky wants to merge 16 commits intopytorch:mainfrom
morrison-turnansky:issue-161610-dynamo-namedtuple-dynamic-var
Closed

NamedTuple: Allow side effects for dynamic attributes#161645
morrison-turnansky wants to merge 16 commits intopytorch:mainfrom
morrison-turnansky:issue-161610-dynamo-namedtuple-dynamic-var

Conversation

@morrison-turnansky
Copy link
Collaborator

@morrison-turnansky morrison-turnansky commented Aug 27, 2025

I confirmed that the tracing was correct i.e. NamedTupleVariable had the correct dynamic attribute added to it.

The problem was that NamedTupleVariable was always marked as immutable. This does not reflect the behavior of namedtuple.

Subclasses of namedtuple may be mutable, so when a NamedTupleVariable is derived from a subclass that is mutable, I made NamedTupleVariable mutable as well. Then side_effects correctly updates the returned object.

Fixes #161610

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

@pytorch-bot
Copy link

pytorch-bot bot commented Aug 27, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit 7068799 with merge base 6737e2c (image):
💚 Looks good so far! There are no failures yet. 💚

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

@morrison-turnansky
Copy link
Collaborator Author

@pytorchbot label "topic: not user facing"

@anijain2305 anijain2305 requested a review from StrongerXi August 28, 2025 00:14
@morrison-turnansky morrison-turnansky marked this pull request as ready for review August 28, 2025 17:09
Copy link
Contributor

@anijain2305 anijain2305 left a comment

Choose a reason for hiding this comment

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

If supporting NamedTuple creation inside the compiled frame and then returning is hard, we can do a graph break and not support it in this PR.

Let me know - this PR is little more complicated than I anticipated because most of the code around tuples is written with immutability in mind, and named tuple creation in CPython is not a standard process (CPython generates some string, and then eval it on the fly), so using a UserDefinedObjectVariable is also not readily available.

@morrison-turnansky
Copy link
Collaborator Author

morrison-turnansky commented Aug 28, 2025

If these changes are close to what you wanted, I am good to keep going. Otherwise, we can introduce a graph break.

Copy link
Contributor

@anijain2305 anijain2305 left a comment

Choose a reason for hiding this comment

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

Very close to be done. Thanks for sending the PR.

@morrison-turnansky morrison-turnansky force-pushed the issue-161610-dynamo-namedtuple-dynamic-var branch from b81dfd5 to fed34a7 Compare September 3, 2025 16:08
@zou3519 zou3519 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Sep 3, 2025
…ned variable call function. raised exception for converting dynamic attribute of named tuple variable to python constant when variable does not have to python constant method
@morrison-turnansky morrison-turnansky force-pushed the issue-161610-dynamo-namedtuple-dynamic-var branch from a37a275 to e9b7abd Compare September 8, 2025 17:06
@StrongerXi
Copy link
Contributor

Trigger the CI workflows.

@morrison-turnansky
Copy link
Collaborator Author

@anijain2305 Please review and/or resolve requested change.

@StrongerXi
Copy link
Contributor

Looks great, merging, thank you!

@StrongerXi
Copy link
Contributor

@pytorchbot merge

@pytorch-bot
Copy link

pytorch-bot bot commented Sep 9, 2025

This PR has pending changes requested. Please address the comments and update the PR before merging.

@anijain2305
Copy link
Contributor

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 9, 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

markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
I confirmed that the tracing was correct i.e. NamedTupleVariable had the correct dynamic attribute added to it.

The problem was that NamedTupleVariable was always marked as immutable. This does not reflect the behavior of namedtuple.

Subclasses of namedtuple may be mutable, so when a NamedTupleVariable is derived from a subclass that is mutable, I made NamedTupleVariable mutable as well. Then side_effects correctly updates the returned object.

Fixes pytorch#161610

Pull Request resolved: pytorch#161645
Approved by: https://github.com/anijain2305, https://github.com/StrongerXi
mansiag05 pushed a commit to mansiag05/pytorch that referenced this pull request Sep 22, 2025
I confirmed that the tracing was correct i.e. NamedTupleVariable had the correct dynamic attribute added to it.

The problem was that NamedTupleVariable was always marked as immutable. This does not reflect the behavior of namedtuple.

Subclasses of namedtuple may be mutable, so when a NamedTupleVariable is derived from a subclass that is mutable, I made NamedTupleVariable mutable as well. Then side_effects correctly updates the returned object.

Fixes pytorch#161610

Pull Request resolved: pytorch#161645
Approved by: https://github.com/anijain2305, https://github.com/StrongerXi
cleonard530 pushed a commit to cleonard530/pytorch that referenced this pull request Sep 22, 2025
I confirmed that the tracing was correct i.e. NamedTupleVariable had the correct dynamic attribute added to it.

The problem was that NamedTupleVariable was always marked as immutable. This does not reflect the behavior of namedtuple.

Subclasses of namedtuple may be mutable, so when a NamedTupleVariable is derived from a subclass that is mutable, I made NamedTupleVariable mutable as well. Then side_effects correctly updates the returned object.

Fixes pytorch#161610

Pull Request resolved: pytorch#161645
Approved by: https://github.com/anijain2305, https://github.com/StrongerXi
dsashidh pushed a commit to dsashidh/pytorch that referenced this pull request Sep 26, 2025
I confirmed that the tracing was correct i.e. NamedTupleVariable had the correct dynamic attribute added to it.

The problem was that NamedTupleVariable was always marked as immutable. This does not reflect the behavior of namedtuple.

Subclasses of namedtuple may be mutable, so when a NamedTupleVariable is derived from a subclass that is mutable, I made NamedTupleVariable mutable as well. Then side_effects correctly updates the returned object.

Fixes pytorch#161610

Pull Request resolved: pytorch#161645
Approved by: https://github.com/anijain2305, https://github.com/StrongerXi
@morrison-turnansky morrison-turnansky deleted the issue-161610-dynamo-namedtuple-dynamic-var branch October 3, 2025 17:43
pytorchmergebot pushed a commit that referenced this pull request Nov 20, 2025
…Variable (#167468)

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

Pull Request resolved: #167468
Approved by: https://github.com/guilhermeleobas, https://github.com/StrongerXi, https://github.com/mlazos
JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
…Variable (#167468)

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

Pull Request resolved: #167468
Approved by: https://github.com/guilhermeleobas, https://github.com/StrongerXi, https://github.com/mlazos
pytorchmergebot pushed a commit that referenced this pull request Dec 12, 2025
…Variable (#167468)

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

Pull Request resolved: #167468
Approved by: https://github.com/guilhermeleobas, https://github.com/Lucaskabela
vishalgoyal316 pushed a commit to vishalgoyal316/pytorch that referenced this pull request Dec 17, 2025
…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/guilhermeleobas, https://github.com/Lucaskabela
krastogi-in pushed a commit to krastogi-in/pytorch that referenced this pull request Jan 9, 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/guilhermeleobas, https://github.com/Lucaskabela
pytorchmergebot pushed a commit that referenced this pull request Jan 13, 2026
…Variable (#167468)

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

Pull Request resolved: #167468
Approved by: https://github.com/Lucaskabela
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

ciflow/trunk Trigger trunk jobs on your pull request Merged module: dynamo open source 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.

NamedTuple Dynamic Objects do not persist via torch compile and eager backend

6 participants