Skip to content

[dynamo] Move _empty_create_subclass to be method helper to avoid closure refcycle#175660

Closed
azahed98 wants to merge 6 commits intogh/azahed98/7/basefrom
gh/azahed98/7/head
Closed

[dynamo] Move _empty_create_subclass to be method helper to avoid closure refcycle#175660
azahed98 wants to merge 6 commits intogh/azahed98/7/basefrom
gh/azahed98/7/head

Conversation

@azahed98
Copy link
Copy Markdown
Contributor

@azahed98 azahed98 commented Feb 24, 2026

Stack from ghstack (oldest at bottom):

Fixes a potential reference cycle that can block swap_tensors during or after compile. This reference cycle comes from a closure of a MetaConverter object within the _empty_create_subclass defined in MetaConverter.empty_create_subclass.

This PR moves _empty_create_subclass to be a method of MetaConverter instead, adding additional arguments and moving imports as needed.

Test Plan: The original error can be reproduced with this script (if ran on the previous commit from this stack). This PR adds a unit test that checks that weakrefs created by MetaConverter are cleaned up when it is manually deleted even if garbage collection is disabled.

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

[ghstack-poisoned]
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Feb 24, 2026

🔗 Helpful Links

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

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

✅ You can merge normally! (2 Unrelated Failures)

As of commit 783f38c with merge base f72a552 (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

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

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Feb 24, 2026

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

[ghstack-poisoned]
azahed98 added a commit that referenced this pull request Feb 24, 2026
[ghstack-poisoned]
azahed98 added a commit that referenced this pull request Feb 24, 2026
azahed98 added a commit that referenced this pull request Feb 24, 2026
[ghstack-poisoned]
azahed98 added a commit that referenced this pull request Feb 25, 2026
@miladm
Copy link
Copy Markdown
Collaborator

miladm commented Mar 2, 2026

@azahed98 do we have clarity on what code patterns / behaviors lead to this failure scenario? I wonder how we can generalize the pattern of failure beyond the specific use case you've discovered.

…o avoid closure refcycle"


Fixes a potential reference cycle that can block `swap_tensors` during or after compile. This reference cycle comes from a closure of a `MetaConverter` object within the `_empty_create_subclass` defined in `MetaConverter.empty_create_subclass`.

This PR moves `_empty_create_subclass` to be a method of `MetaConverter` instead, adding additional arguments and moving imports as needed.

**Test Plan:** The original error can be reproduced with [this script](https://gist.github.com/sayakpaul/929678132809874c5dbf9c5215460d33) (if ran on the previous commit from this stack). This PR adds a unit test that checks that weakrefs created by `MetaConverter` are cleaned up when it is manually deleted even if garbage collection is disabled.


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

[ghstack-poisoned]
azahed98 added a commit that referenced this pull request Mar 9, 2026
azahed98 added a commit that referenced this pull request Mar 12, 2026
…sure refcycle

ghstack-source-id: 185f47b
Pull-Request: #175660

[ghstack-poisoned]
azahed98 added a commit that referenced this pull request Mar 12, 2026
…sure refcycle

ghstack-source-id: 185f47b
Pull-Request: #175660

ghstack-source-id: 0495197
Pull Request resolved: #177293
azahed98 added a commit that referenced this pull request Mar 12, 2026
…sure refcycle

ghstack-source-id: 185f47b
Pull-Request: #175660

[ghstack-poisoned]
azahed98 added a commit that referenced this pull request Mar 12, 2026
…sure refcycle

ghstack-source-id: 185f47b
Pull-Request: #175660

ghstack-source-id: bcb6699
Pull Request resolved: #177295
…o avoid closure refcycle"


Fixes a potential reference cycle that can block `swap_tensors` during or after compile. This reference cycle comes from a closure of a `MetaConverter` object within the `_empty_create_subclass` defined in `MetaConverter.empty_create_subclass`.

This PR moves `_empty_create_subclass` to be a method of `MetaConverter` instead, adding additional arguments and moving imports as needed.

**Test Plan:** The original error can be reproduced with [this script](https://gist.github.com/sayakpaul/929678132809874c5dbf9c5215460d33) (if ran on the previous commit from this stack). This PR adds a unit test that checks that weakrefs created by `MetaConverter` are cleaned up when it is manually deleted even if garbage collection is disabled.


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

[ghstack-poisoned]
azahed98 added a commit that referenced this pull request Mar 12, 2026
…sure refcycle

ghstack-source-id: eeebcfd
Pull Request resolved: #175660
@azahed98 azahed98 added the topic: not user facing topic category label Mar 13, 2026
@azahed98
Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Mar 13, 2026
@pytorchmergebot
Copy link
Copy Markdown
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

EmanueleCoradin pushed a commit to EmanueleCoradin/pytorch that referenced this pull request Mar 30, 2026
…sure refcycle (pytorch#175660)

Fixes a potential reference cycle that can block `swap_tensors` during or after compile. This reference cycle comes from a closure of a `MetaConverter` object within the `_empty_create_subclass` defined in `MetaConverter.empty_create_subclass`.

This PR moves `_empty_create_subclass` to be a method of `MetaConverter` instead, adding additional arguments and moving imports as needed.

**Test Plan:** The original error can be reproduced with [this script](https://gist.github.com/sayakpaul/929678132809874c5dbf9c5215460d33) (if ran on the previous commit from this stack). This PR adds a unit test that checks that weakrefs created by `MetaConverter` are cleaned up when it is manually deleted even if garbage collection is disabled.

Pull Request resolved: pytorch#175660
Approved by: https://github.com/anijain2305
ghstack dependencies: pytorch#175397, pytorch#175596
AaronWang04 pushed a commit to AaronWang04/pytorch that referenced this pull request Mar 31, 2026
…sure refcycle (pytorch#175660)

Fixes a potential reference cycle that can block `swap_tensors` during or after compile. This reference cycle comes from a closure of a `MetaConverter` object within the `_empty_create_subclass` defined in `MetaConverter.empty_create_subclass`.

This PR moves `_empty_create_subclass` to be a method of `MetaConverter` instead, adding additional arguments and moving imports as needed.

**Test Plan:** The original error can be reproduced with [this script](https://gist.github.com/sayakpaul/929678132809874c5dbf9c5215460d33) (if ran on the previous commit from this stack). This PR adds a unit test that checks that weakrefs created by `MetaConverter` are cleaned up when it is manually deleted even if garbage collection is disabled.

Pull Request resolved: pytorch#175660
Approved by: https://github.com/anijain2305
ghstack dependencies: pytorch#175397, pytorch#175596
@github-actions github-actions Bot deleted the gh/azahed98/7/head branch April 13, 2026 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants