Skip to content

fix(metric): honor torch.device context manager when setting default device - #3448

Merged
bhimrazy merged 5 commits into
Lightning-AI:masterfrom
bhimrazy:fix/metric-default-device-torch-2.8
Aug 20, 2026
Merged

fix(metric): honor torch.device context manager when setting default device#3448
bhimrazy merged 5 commits into
Lightning-AI:masterfrom
bhimrazy:fix/metric-default-device-torch-2.8

Conversation

@bhimrazy

@bhimrazy bhimrazy commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Fixes Metric landing on CPU instead of the context device when constructed inside a with torch.device(...) block on torch 2.3–2.7.

Metric.__init__ gated its default-device lookup on torch >= 2.3: torch.get_default_device() above, torch.empty(0).device below. But get_default_device() only started consulting the active DeviceContext — what the torch.device context manager pushes — in torch 2.8 (pytorch/pytorch#148621, fixing pytorch/pytorch#148874). Before that it read only the global slot set_default_device() populates, so on 2.3–2.7 the newer branch is strictly worse than the fallback it replaced. Moving the gate to 2.8 respects both the context manager and set_default_device on every supported version.

Verified on torch 2.6.0 inside with torch.device("meta"): get_default_device()cpu, torch.empty(0).devicemeta. The metric's state still lands on the context device, so the bug leaves metric.device disagreeing with its own state — the mismatch #3316 set out to fix.

Existing coverage for this path is GPU-only and passes today only because the GPU workflow runs the torch 2.0 and 2.8 images. Added a CPU-runnable regression test on the meta device that exercises both sides of the version gate.

Prerequisite for #3449.

…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-commenter

codecov-commenter commented Aug 9, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 36%. Comparing base (d184220) to head (8c518c3).
⚠️ Report is 11 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Force the pre-2.8 `torch.empty(0).device` fallback via monkeypatch so that branch
stays covered on torch >= 2.8, where CI would otherwise never execute it, and assert
the state tensor lands on the context device too.
@bhimrazy bhimrazy changed the title [wip] fix(metric): honor torch.device context manager when setting default device fix(metric): honor torch.device context manager when setting default device Aug 20, 2026
bhimrazy added a commit to bhimrazy/torchmetrics that referenced this pull request Aug 20, 2026
@mergify mergify Bot added the ready label Aug 20, 2026
@mergify
mergify Bot requested a review from a team August 20, 2026 12:25
@bhimrazy
bhimrazy enabled auto-merge (squash) August 20, 2026 13:30
@bhimrazy
bhimrazy merged commit dab0333 into Lightning-AI:master Aug 20, 2026
64 of 65 checks passed
@bhimrazy
bhimrazy deleted the fix/metric-default-device-torch-2.8 branch August 20, 2026 18:23
bhimrazy added a commit that referenced this pull request Aug 31, 2026
….6 (#3449)

* fix(metric): honor `torch.device` context manager when setting default 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.

* docs: add CHANGELOG entry for #3448

* ci: drop PyTorch 2.0-2.5 and raise minimum to 2.6

Drops the end-of-life PyTorch minors and raises the floor to 2.6, keeping the
latest five supported minors (2.6-2.10), matching what Lightning did in #21851.

- CPU matrix: drop the 2.0-2.5 rows and retarget the oldest/macOS/Windows
  entries to 2.6.0 (23 -> 17 jobs).
- Docker: drop the 2.0.1-2.5.1 CUDA images (14 -> 8 builds); 2.6.0/cu12.4.1 is
  the new oldest. Dockerfile ARG defaults follow it.
- GPU lanes: move the oldest image to torch 2.6 (cu12.4.1 for unittests,
  pytorch/pytorch:2.6.0-cuda12.4 for integrations).
- Requirements: torch >=2.6.0, torchaudio >=2.6.0, torchvision >=0.21.0.
- pytorch-lightning floor 1.9.0 -> 2.5.1, the first release CI-tested against
  torch 2.6.

The GPU lanes previously hardcoded `TORCH_VER == "2.0"` to decide whether to
apply the oldest requirements. That silently goes stale whenever the floor moves
on the CPU side, so derive it from requirements/base.txt instead and fail fast
if an image is below the declared floor.

* docs: add CHANGELOG entry for #3449

* ci(gpu): read torch floor before requirements are rewritten and mirror PL cuda images

* docs: refresh dockers README build example to a supported image combo

* chore: drop the metric default-device fix, tracked in #3448

* ci: update Docker image to use torch 2.10 and CUDA 12.8.1

* ci: allow pip in PEP 668 pytorch images and raise onnxruntime floor for py3.11

* ci: raise scikit-image and mecab-ko floors to versions with cp311 wheels

* refactor(ci): tidy torch-floor guard and trim its comments

- state the version check in positive polarity instead of relying on
  bool -> exit-code coercion, so it reads the same way as the error below it
- shorten the guard comments to the one-liner style used in these files, and
  note the adjust-torch-versions ordering in both workflows, not just one
- drop a redundant `pip install -q packaging`, already installed at the top
- keep the _integrate.txt note about our own floor rather than upstream's
  test matrix, which we cannot verify here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants