-
Notifications
You must be signed in to change notification settings - Fork 24.1k
update get_default_device to also respect torch.device ctx manager #148621
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
base: main
Are you sure you want to change the base?
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/148621
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 001abe1 with merge base 690fc2c ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Ping @albanD |
if hasattr(_GLOBAL_DEVICE_CONTEXT, "device_context"): | ||
device = _GLOBAL_DEVICE_CONTEXT.device_context.device | ||
from torch.overrides import _get_current_function_mode_stack | ||
from torch.utils._device import DeviceContext |
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'm confused now what's the difference between torch._GLOBAL_DEVICE_CONTEXT and torch.utils._device.CURRENT_DEVICE ?
And if we change all access to respect the later, should we just delete the first one?
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.
Currently, I think _GLOBAL_DEVICE_CONTEXT
is required to correctly implement the set_default_device
with DeviceContext
(it holds the DeviceContext object on which enter and exit is called)
Lines 1205 to 1218 in ece1658
global _GLOBAL_DEVICE_CONTEXT | |
if hasattr(_GLOBAL_DEVICE_CONTEXT, "device_context"): | |
device_context = _GLOBAL_DEVICE_CONTEXT.device_context | |
if device_context is not None: | |
device_context.__exit__(None, None, None) | |
if device is None: | |
device_context = None | |
else: | |
from torch.utils._device import DeviceContext | |
device_context = DeviceContext(device) | |
device_context.__enter__() | |
_GLOBAL_DEVICE_CONTEXT.device_context = device_context |
Actually looking at _device.py, I am not sure if CURRENT_DEVICE
is needed for implementation.
But I think this (or above) refactoring can be addressed in a separate PR, wdyt?
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.
Gentle ping :)
Fixes #131328