-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[PyTorch] RFC: ExclusivelyOwned #59419
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
This introduces ExclusivelyOwned, which allows isolated pieces of code that can make ownership guarantees to opt out of reference counting operations on `intrusive_ptr` and `Tensor` entirely. To elaborate, if you know you are the exclusive owner of an `intrusive_ptr` or `Tensor`, moving it into an `ExclusivelyOwned` will avoid performing atomic reference counting operations at destruction time. The documentation comment should provide sufficient explanation; please request changes if not. Differential Revision: [D28885314](https://our.internmc.facebook.com/intern/diff/D28885314/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D28885314/)! [ghstack-poisoned]
💊 CI failures summary and remediationsAs of commit 770a4fe (more details on the Dr. CI page):
🕵️ 1 new failure recognized by patternsThe following CI failures do not appear to be due to upstream breakages:
|
|
an open question: would it be nice if Tensor factory functions could optionally return this? We can't simply change the return type because that would make this opt-out instead of opt-in, but perhaps we could have a template wrapper |
aten/src/ATen/templates/TensorBody.h
Outdated
|
|
||
| static repr_type nullRepr() { | ||
| // REVIEW: do we need to be able to distinguish an empty | ||
| // ExclusivelyOwned<Tensor> from an undefined Tensor? |
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.
seems unlikely
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.
Alternately, we could delete the default constructor from ExclusivelyOwned (I am not sure how painful that will be for consumers)
| #ifndef NDEBUG | ||
| // Needed to pass the debug assertions in ~intrusive_ptr_target. | ||
| toDestroy->refcount_ = 0; | ||
| toDestroy->weakcount_ = 0; |
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.
Hmm, this is extra invariant on release_resources that the refcount/weakcount may not be accurate
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.
if release_resources cares about the refcount/weakcount....why?
We can do the stores in all builds if you think it will make a difference to anyone, I would just rather not have the code size and memory traffic.
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.
oh, wait, it can't care about the refcount & weakcount because they're private to intrusive_ptr_target!
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.
I don't think release_resources ever relies on it, but it's a near miss, as some PyObject binding stuff uses use_count to decide if it has exclusive ownership or not. Just need to doc and socialize this, no action needed.
c10/util/ExclusivelyOwned.h
Outdated
| // End the lifetime of repr_ without executing its dtor, since we | ||
| // already did specialized destruction for the exclusively-owned | ||
| // case in destroyOwned! | ||
| dummy_ = '\0'; |
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.
C unions never destruct anyway, though?
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.
normally, we would have to write an explicit destructor call for exactly that reason.
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.
I guess my Q is, couldn't we just omit nulling it out here entirely?
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.
yes, I think you're right. the lifetime of the union is going to end anyway. will fix.
This introduces ExclusivelyOwned, which allows isolated pieces of code that can make ownership guarantees to opt out of reference counting operations on `intrusive_ptr` and `Tensor` entirely. To elaborate, if you know you are the exclusive owner of an `intrusive_ptr` or `Tensor`, moving it into an `ExclusivelyOwned` will avoid performing atomic reference counting operations at destruction time. The documentation comment should provide sufficient explanation; please request changes if not. Differential Revision: [D28885314](https://our.internmc.facebook.com/intern/diff/D28885314/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D28885314/)! [ghstack-poisoned]
This introduces ExclusivelyOwned, which allows isolated pieces of code that can make ownership guarantees to opt out of reference counting operations on `intrusive_ptr` and `Tensor` entirely. To elaborate, if you know you are the exclusive owner of an `intrusive_ptr` or `Tensor`, moving it into an `ExclusivelyOwned` will avoid performing atomic reference counting operations at destruction time. The documentation comment should provide sufficient explanation; please request changes if not. Differential Revision: [D28885314](https://our.internmc.facebook.com/intern/diff/D28885314/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D28885314/)! [ghstack-poisoned]
c10/util/ExclusivelyOwned.h
Outdated
| // have this traits class, and instead we should just have two | ||
| // explicit specializations of ExclusivelyOwned? There would be some | ||
| // small amount of code duplication, but perhaps it would be easier to | ||
| // understand? |
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.
I don't have an opinion here, you should do what you feel is best.
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.
Speculatively approving this. I don't know how this is going to pan out overall in the codebase but it seems like a useful tool to have in the toolbox
This introduces ExclusivelyOwned, which allows isolated pieces of code that can make ownership guarantees to opt out of reference counting operations on `intrusive_ptr` and `Tensor` entirely. To elaborate, if you know you are the exclusive owner of an `intrusive_ptr` or `Tensor`, moving it into an `ExclusivelyOwned` will avoid performing atomic reference counting operations at destruction time. The documentation comment should provide sufficient explanation; please request changes if not. Differential Revision: [D28885314](https://our.internmc.facebook.com/intern/diff/D28885314/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D28885314/)! [ghstack-poisoned]
This introduces ExclusivelyOwned, which allows isolated pieces of code that can make ownership guarantees to opt out of reference counting operations on `intrusive_ptr` and `Tensor` entirely. To elaborate, if you know you are the exclusive owner of an `intrusive_ptr` or `Tensor`, moving it into an `ExclusivelyOwned` will avoid performing atomic reference counting operations at destruction time. The documentation comment should provide sufficient explanation; please request changes if not. Differential Revision: [D28885314](https://our.internmc.facebook.com/intern/diff/D28885314/) **NOTE FOR REVIEWERS**: This PR has internal Facebook specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D28885314/)! [ghstack-poisoned]
|
This pull request has been merged in 79d7c15. |
Stack from ghstack:
This introduces ExclusivelyOwned, which allows isolated
pieces of code that can make ownership guarantees to opt out of
reference counting operations on
intrusive_ptrandTensorentirely. To elaborate, if you know you are the exclusive owner of an
intrusive_ptrorTensor, moving it into anExclusivelyOwnedwillavoid performing atomic reference counting operations at destruction
time. The documentation comment should provide sufficient explanation; please request changes if not.
Differential Revision: D28885314
NOTE FOR REVIEWERS: This PR has internal Facebook specific changes or comments, please review them on Phabricator!