Skip to content

Resolve "cuda" to the caller's current GPU - #4

Merged
wrignj08 merged 1 commit into
mainfrom
fix/cuda-current-device
Nov 11, 2026
Merged

wrignj08 merged 1 commit into
mainfrom
fix/cuda-current-device

Conversation

@wrignj08

Copy link
Copy Markdown
Contributor

Fixes a multi-GPU issue found in the pre-release sweep.

Bug. torch.cuda.set_device is per thread, and ManyPred's prefetch and saving threads start on GPU 0. After torch.cuda.set_device(1) with device="cuda" (or no device), the prefetch thread read "cuda" as GPU 0. Inputs already on cuda:1 were stacked on GPU 0, copied back, and record_stream was given a stream from the other GPU. Results were never wrong, but it used memory on GPU 0 (possibly another job's) and could crash. Inputs from numpy or files weren't affected.

Fix. resolve_device turns an unindexed "cuda" into cuda:<current device> on the calling thread, before any worker thread starts. Every device path (inference, mosaic_device, program .device, ensembles) already goes through it. Explicit "cuda:N", CPU, MPS and machines without CUDA behave as before.

Tests.

  • Unit tests mock the current device.
  • A two-GPU test runs predict_batch on cuda:1 with device="cuda" and checks that GPU 0's peak memory doesn't move. It skips with fewer than two GPUs, so it hasn't run yet.
  • Locally (CPU): ruff passes, and the suite has 692 passed, 153 skipped.

Review. Codex reviewed the fix and found no issues. It checked that every device path into worker threads goes through resolve_device, and that same_device, device_lock, ensembles, ROCm and torch.compile are unaffected.

Also adds an [Unreleased] section to the changelog.

🤖 Generated with Claude Code

torch.cuda.set_device is per thread, and ManyPred's prefetch and saving threads start on GPU 0. With set_device(1) and device="cuda", inputs already on cuda:1 were stacked on cuda:0 by the prefetch thread, then copied back, with record_stream given a stream from the other GPU. resolve_device now turns an unindexed "cuda" into cuda:<current device> on the calling thread, so every thread gets an explicit index.

Tests: unit tests with a mocked current device, and a two-GPU test (skips with fewer). Codex reviewed the fix: no issues.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wrignj08
wrignj08 merged commit 870b5d9 into main Nov 11, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant