Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

kshitij12345
Copy link
Collaborator

Fixes #131328

Copy link

pytorch-bot bot commented Mar 5, 2025

🔗 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 Failures

As of commit 001abe1 with merge base 690fc2c (image):
💚 Looks good so far! There are no failures yet. 💚

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

@kshitij12345 kshitij12345 added the release notes: python_frontend python frontend release notes category label Mar 5, 2025
@kshitij12345 kshitij12345 marked this pull request as ready for review March 5, 2025 23:45
@kshitij12345 kshitij12345 requested a review from ezyang March 5, 2025 23:45
@colesbury colesbury requested a review from albanD March 6, 2025 18:41
@colesbury colesbury added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Mar 6, 2025
@kshitij12345
Copy link
Collaborator Author

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
Copy link
Collaborator

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?

Copy link
Collaborator Author

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)

pytorch/torch/__init__.py

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gentle ping :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open source release notes: python_frontend python frontend release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

torch.device context manager change doesn't show in torch.get_default_device
4 participants