Back Home

GitHub Repo

PyTorch Proposes CUDA Initialization Fix After Single-GPU Test Reveals Device-Switching Shortcut Bug

On November 14, the PyTorch community reported that an internal device-switching function may return early without creating the CUDA primary context as promised. A draft fix has been proposed but not merged; the impact on released versions remains unclear.

Pytorch Deepdream (https://github.com/gordicaleksa/pytorch-deepdream) by gordicaleksa (https://github.com/gordicaleksa/pytorch-deepdream/commits?author=gordicaleksa) · MIT · Image source
zh-Hant

On November 14, PyTorch received a report of a CUDA initialization bug: the internal function c10::cuda::ExchangeDevice promises to initialize the target device’s primary context, but may return early when that device is already current. The issue surfaced in an out-of-tree CI test on Windows, and a contributor proposed a draft fix the same day. It had not been merged at the time of review. Issue report

The key point is that having the correct current device number does not prove that its context has been created. NVIDIA’s documentation says cudaSetDevice both sets the device used by the calling thread and binds the primary context, immediately initializing its runtime state. Skipping this call may therefore bypass an initialization step that higher-level code relies on. CUDA device management documentation

According to the reporter’s analysis, an earlier ROCm lazy-setting optimization changed the internal state of MaybeSetDevice when the target was already the current device, causing an existing test to take this early-return path. In a single-GPU environment, device 0 consequently did not have its primary context created. The test’s multi-GPU branch did not trigger the same path because it switched to another card first. This means multi-GPU tests can pass while missing a single-GPU initialization issue; the early return itself had already existed. Trigger conditions and analysis

The proposed fix removes the shortcut in ExchangeDevice while preserving the existing ROCm optimization and the behavior of another function that does not promise to create a context. The submitter ran an independent reproduction program on an RTX 5090 with CUDA 13.2 and reported that it failed before the change and passed with the fix applied. This is the submitter’s verification, not a complete CI result. Draft fix and verification

For engineers maintaining C++ extensions or device-management code, the lesson is to verify “device selection” and “context existence” separately, and to add single-GPU tests in a fresh process. The fix review, test coverage, and release attribution remain to be tracked; the available evidence is not enough to conclude that typical model training or inference is broadly affected.

Sources

  1. ExchangeDevice does not create the primary context when to_device is already the current device
  2. Fix ExchangeDevice to always create the primary context on to_device
  3. CUDA Runtime API — Device Management