[wip] fix(metric): honor torch.device context manager when setting default device - #3448
Open
bhimrazy wants to merge 2 commits into
Open
[wip] fix(metric): honor torch.device context manager when setting default device#3448bhimrazy wants to merge 2 commits into
torch.device context manager when setting default device#3448bhimrazy wants to merge 2 commits into
Conversation
…t device
`Metric.__init__` gated the use of `torch.get_default_device()` on torch >= 2.3.
However, until torch 2.8 that function only reflected `set_default_device()` and
ignored an active `with torch.device(...)` context manager, so metrics created
inside such a block were assigned the CPU instead of the context device.
torch 2.8 (pytorch/pytorch v2.8.0) added the `DeviceContext` lookup to
`get_default_device`, so gate on 2.8 and keep the `torch.empty(0).device` probe
for older versions, which does respect the context manager.
Verified on torch 2.6.0: inside `with torch.device("meta")`,
`get_default_device()` returns cpu while `torch.empty(0).device` returns meta.
Adds a CPU-runnable regression test using the meta device; the existing coverage
for this path was GPU-only.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3448 +/- ##
======================================
- Coverage 37% 36% -0%
======================================
Files 349 349
Lines 19901 19910 +9
======================================
+ Hits 7264 7265 +1
- Misses 12637 12645 +8 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Metric.__init__gatestorch.get_default_device()on torch >= 2.3, but that function only started reflecting an activewith torch.device(...)context manager in 2.8 — before that it tracked onlyset_default_device(). So on torch 2.3–2.7, a metric built inside such a block gets CPU instead of the context device.Gates on 2.8 and keeps the
torch.empty(0).deviceprobe below it, which does respect the context manager.Verified on torch 2.6.0 inside
with torch.device("meta"):get_default_device()→cpu,torch.empty(0).device→meta.The existing coverage for this path is GPU-only and passes today only because the GPU images are torch 2.0 and 2.8; any image in 2.3–2.7 turns it red. Adds a CPU-runnable regression test using the
metadevice.Prerequisite for #3449 (floor moves to 2.6).