-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[torch.library] Change Library.__del__ into weakref.finalize #101829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
`__del__` is a bit difficult to use, because when it is called, it is not guaranteed that anything it uses has not been cleaned up. Ed tells me he got the following exception one day, which is what prompted this PR. ``` Exception ignored in: <function Library.__del__ at 0x7fa36d211e50> Traceback (most recent call last): File "/data/users/ezyang/a/pytorch/torch/library.py", line 139, in __del__ AttributeError: 'NoneType' object has no attribute 'remove' ``` One solution is to use weakref.finalize, which lets one define a function to be run when the object is deleted that can hold references to specific things it needs. Another solution is to just check if the object is None, but I like the weakref solution better. Test Plan: - new test [ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/101829
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New FailureAs of commit d162f3a: NEW FAILURE - The following job has failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
`__del__` is a bit difficult to use, because when it is called, it is not guaranteed that anything it uses has not been cleaned up. Ed tells me he got the following exception one day, which is what prompted this PR. ``` Exception ignored in: <function Library.__del__ at 0x7fa36d211e50> Traceback (most recent call last): File "/data/users/ezyang/a/pytorch/torch/library.py", line 139, in __del__ AttributeError: 'NoneType' object has no attribute 'remove' ``` One solution is to use weakref.finalize, which lets one define a function to be run when the object is deleted that can hold references to specific things it needs. Another solution is to just check if the object is None, but I like the weakref solution better. Test Plan: - new test ghstack-source-id: 809ac42 Pull Request resolved: #101829
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems OK, but please double check that you are not creating a refcycle through the finalizer. If so, you may want to pass in weakrefs to the finalizer and skip finalization if the weakrefs have gone dead.
`__del__` is a bit difficult to use, because when it is called, it is not guaranteed that anything it uses has not been cleaned up. Ed tells me he got the following exception one day, which is what prompted this PR. ``` Exception ignored in: <function Library.__del__ at 0x7fa36d211e50> Traceback (most recent call last): File "/data/users/ezyang/a/pytorch/torch/library.py", line 139, in __del__ AttributeError: 'NoneType' object has no attribute 'remove' ``` One solution is to use weakref.finalize, which lets one define a function to be run when the object is deleted that can hold references to specific things it needs. Another solution is to just check if the object is None, but I like the weakref solution better. Test Plan: - new test [ghstack-poisoned]
`__del__` is a bit difficult to use, because when it is called, it is not guaranteed that anything it uses has not been cleaned up. Ed tells me he got the following exception one day, which is what prompted this PR. ``` Exception ignored in: <function Library.__del__ at 0x7fa36d211e50> Traceback (most recent call last): File "/data/users/ezyang/a/pytorch/torch/library.py", line 139, in __del__ AttributeError: 'NoneType' object has no attribute 'remove' ``` One solution is to use weakref.finalize, which lets one define a function to be run when the object is deleted that can hold references to specific things it needs. Another solution is to just check if the object is None, but I like the weakref solution better. Test Plan: - new test ghstack-source-id: 7690b2f Pull Request resolved: #101829
Verified, and also added new tests to check that the refcounts are what we expect (and that there is no reference cycle as a result) |
@pytorchbot merge -f "distributed test timed out" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Stack from ghstack:
__del__
is a bit difficult to use, because when it is called, it isnot guaranteed that anything it uses has not been cleaned up.
Ed tells me he got the following exception one day, which is what
prompted this PR.
One solution is to use weakref.finalize, which lets one define a
function to be run when the object is deleted that can hold references
to specific things it needs.
Another solution is to just check if the object is None, but I like the
weakref solution better.
Test Plan: