pytorch - ✅(Solved) Fix [CUDA] `test_dlpack_tensor_on_different_device` fails with `torch.device` instead of `torch.cuda.device` [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#181024Fetched 2026-04-22 07:43:01
View on GitHub
Comments
0
Participants
1
Timeline
21
Reactions
2
Author
Participants
Timeline (top)
mentioned ×7subscribed ×7labeled ×6cross-referenced ×1

Error Message

====================================================================== FAIL: test_dlpack_tensor_on_different_device_cuda (main.TestTorchDlPackCUDA.test_dlpack_tensor_on_different_device_cuda)

Traceback (most recent call last): File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_utils.py", line 3448, in wrapper method(*args, **kwargs) File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_utils.py", line 3448, in wrapper method(*args, **kwargs) File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 439, in instantiated_test result = test(self, **param_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 1255, in dep_fn return fn(slf, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 1468, in only_fn return fn(slf, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 1498, in multi_fn return fn(slf, devices, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/pytorch/pytorch/test/test_dlpack.py", line 374, in test_dlpack_tensor_on_different_device with self.assertRaisesRegex( AssertionError: BufferError not raised

To execute this test, run the following from the base repo dir: python test/test_dlpack.py TestTorchDlPackCUDA.test_dlpack_tensor_on_different_device_cuda

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0


Ran 1 test in 0.435s

FAILED (failures=1)

Fix Action

Fixed

PR fix notes

PR #181025: [CUDA] Use torch.cuda.device in dlpack test

Description (problem / solution / changelog)

Fixes #181024 by using torch.cuda.device on CUDA and retaining torch.device for XPU. @shangerxin can you or somebody else please confirm that this change doesn't break this test on XPU?

cc @eqy

Changed files

  • test/test_dlpack.py (modified, +6/-3)

Code Example

- with torch.cuda.device(dev1):
+ with torch.device(dev1):

---

======================================================================
FAIL: test_dlpack_tensor_on_different_device_cuda (__main__.TestTorchDlPackCUDA.test_dlpack_tensor_on_different_device_cuda)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_utils.py", line 3448, in wrapper
    method(*args, **kwargs)
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_utils.py", line 3448, in wrapper
    method(*args, **kwargs)
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 439, in instantiated_test
    result = test(self, **param_kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 1255, in dep_fn
    return fn(slf, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 1468, in only_fn
    return fn(slf, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 1498, in multi_fn
    return fn(slf, devices, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/pytorch/test/test_dlpack.py", line 374, in test_dlpack_tensor_on_different_device
    with self.assertRaisesRegex(
AssertionError: BufferError not raised

To execute this test, run the following from the base repo dir:
    python test/test_dlpack.py TestTorchDlPackCUDA.test_dlpack_tensor_on_different_device_cuda

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0

----------------------------------------------------------------------
Ran 1 test in 0.435s

FAILED (failures=1)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

#173760 made the following change to test_dlpack_tensor_on_different_device:

- with torch.cuda.device(dev1):
+ with torch.device(dev1):

On CUDA, torch.device does not update the current CUDA device in the same way that torch.cuda.device does, causing the test to fail.

Test log:

======================================================================
FAIL: test_dlpack_tensor_on_different_device_cuda (__main__.TestTorchDlPackCUDA.test_dlpack_tensor_on_different_device_cuda)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_utils.py", line 3448, in wrapper
    method(*args, **kwargs)
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_utils.py", line 3448, in wrapper
    method(*args, **kwargs)
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 439, in instantiated_test
    result = test(self, **param_kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 1255, in dep_fn
    return fn(slf, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 1468, in only_fn
    return fn(slf, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/testing/_internal/common_device_type.py", line 1498, in multi_fn
    return fn(slf, devices, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/pytorch/test/test_dlpack.py", line 374, in test_dlpack_tensor_on_different_device
    with self.assertRaisesRegex(
AssertionError: BufferError not raised

To execute this test, run the following from the base repo dir:
    python test/test_dlpack.py TestTorchDlPackCUDA.test_dlpack_tensor_on_different_device_cuda

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0

----------------------------------------------------------------------
Ran 1 test in 0.435s

FAILED (failures=1)

Versions

GB200 on current main

cc @ptrblck @msaroufim @eqy @jerryzh168 @tinglvv @nWEIdia @mruberry

extent analysis

TL;DR

Revert the change in test_dlpack_tensor_on_different_device to use torch.cuda.device instead of torch.device to update the current CUDA device.

Guidance

  • The issue is caused by the difference in behavior between torch.device and torch.cuda.device when updating the current CUDA device.
  • To fix the test failure, revert the change made in test_dlpack_tensor_on_different_device to use torch.cuda.device instead of torch.device.
  • Verify the fix by re-running the test test_dlpack_tensor_on_different_device_cuda after making the change.
  • Consider adding a comment or documentation to explain the reason for using torch.cuda.device in this specific test case.

Example

# Revert the change to use torch.cuda.device
with torch.cuda.device(dev1):
    # test code here

Notes

The fix assumes that the test is intended to run on a CUDA device and that torch.cuda.device is the correct way to update the current CUDA device in this context.

Recommendation

Apply workaround: Revert the change to use torch.cuda.device to ensure the test runs correctly on CUDA devices.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING