-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Support with statement on torch.Stream #140138
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
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/140138
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 1 New Failure, 1 Unrelated FailureAs of commit fb3f110 with merge base 3beb700 ( NEW FAILURE - The following job has failed:
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. |
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 would still leak if the second raises an error, here is an updated version.
torch/csrc/Stream.cpp
Outdated
| PyObject* ctx_stream = nullptr; | ||
| if (PyDict_GetItemStringRef(self->context, "_ctx_stream", &ctx_stream) < 0) { | ||
| throw python_error(); | ||
| } | ||
| TORCH_CHECK(ctx_stream, "ctx_stream should be initialized."); | ||
| PyObject* ctx_device_index = nullptr; | ||
| if (PyDict_GetItemStringRef( | ||
| self->context, "_ctx_device_index", &ctx_device_index) < 0) { | ||
| throw python_error(); | ||
| } | ||
| TORCH_CHECK(ctx_device_index, "ctx_device_index should be initialized."); | ||
| auto prev_stream = (THPStream*)(THPObjectPtr(ctx_stream).get()); | ||
| auto prev_device_index = | ||
| THPUtils_unpackDeviceIndex(THPObjectPtr(ctx_device_index).get()); |
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.
| PyObject* ctx_stream = nullptr; | |
| if (PyDict_GetItemStringRef(self->context, "_ctx_stream", &ctx_stream) < 0) { | |
| throw python_error(); | |
| } | |
| TORCH_CHECK(ctx_stream, "ctx_stream should be initialized."); | |
| PyObject* ctx_device_index = nullptr; | |
| if (PyDict_GetItemStringRef( | |
| self->context, "_ctx_device_index", &ctx_device_index) < 0) { | |
| throw python_error(); | |
| } | |
| TORCH_CHECK(ctx_device_index, "ctx_device_index should be initialized."); | |
| auto prev_stream = (THPStream*)(THPObjectPtr(ctx_stream).get()); | |
| auto prev_device_index = | |
| THPUtils_unpackDeviceIndex(THPObjectPtr(ctx_device_index).get()); | |
| THPObjectPtr ctx_stream; | |
| if (PyDict_GetItemStringRef(self->context, "_ctx_stream", &ctx_stream.get()) < 0) { | |
| throw python_error(); | |
| } | |
| TORCH_INTERNAL_ERROR(ctx_stream.get(), "ctx_stream should be present on the context dict."); | |
| THPObjectPtr ctx_device_index; | |
| if (PyDict_GetItemStringRef( | |
| self->context, "_ctx_device_index", &ctx_device_index.get()) < 0) { | |
| throw python_error(); | |
| } | |
| TORCH_CHECK(ctx_device_index.get(), "ctx_device_index should be present on the context dict."); | |
| auto prev_stream = (THPStream*)(ctx_stream.get()); | |
| auto prev_device_index = | |
| THPUtils_unpackDeviceIndex(ctx_device_index.get()); |
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.
Good catch! Thanks~
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.
ctx_stream.get() will raise an error lvalue required as unary & operand because it returns a rvalue. So I changed a minor code to use a lvalue py_stream and passed its reference to PyDict_GetItemStringRef.
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.
One minor nit on the clearing, sounds good otherwise!
Thanks for your patience working through cpython api fun!
torch/csrc/Stream.cpp
Outdated
| Py_DECREF(self->context); | ||
| self->context = nullptr; |
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.
| Py_DECREF(self->context); | |
| self->context = nullptr; | |
| Py_CLEAR(self->context); |
Very interesting read about that in https://github.com/python/cpython/blob/ea39c8b08d8f025273bfa5b7a95f7b5984dc1e86/Include/refcount.h#L416
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.
Thanks for your suggestions. I am now more familiar with CPython:)
|
"So happy to try to land this PR, the failure is irrelevant." |
Merge startedYour change will be merged while ignoring the following 2 checks: xpu / linux-jammy-xpu-2025.0-py3.9 / test (default, 3, 4, linux.idc.xpu), xpu / linux-jammy-xpu-2025.0-py3.9 / test (default, 4, 4, linux.idc.xpu) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
# Motivation In #137678, we help use the device-agnostic APIs to generalize distributed module. As this [comment](#137678 (comment)) said, we will use the with statement of `torch.Stream` once #140138 is landed. Pull Request resolved: #144951 Approved by: https://github.com/kwen2501, https://github.com/albanD
Stack from ghstack (oldest at bottom):
Motivation
We propose to support Python with statement on
torch.Stream. This is a benefit for all accelerators when writing device-agnostic code. The device-specific stream will also be supported because they are generally derived fromtorch.Stream.With this PR, we can do like this
cc @albanD @EikanWang