pytorch - ✅(Solved) Fix [XPU] `TypeError` raised when passing `device="xpu"` to `torch.from_dlpack` [1 pull requests, 1 comments, 2 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#181140Fetched 2026-04-23 07:22:25
View on GitHub
Comments
1
Participants
2
Timeline
18
Reactions
0
Author
Participants
Assignees
Timeline (top)
mentioned ×5subscribed ×5labeled ×4added_to_project_v2 ×1

Error Message

import torch, numpy as np torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu:0") tensor([1., 1., 1.], device='xpu:0') torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu") Traceback (most recent call last): Cell In[6], line 1 torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu") File ~/miniforge3/envs/dev/lib/python3.14/site-packages/torch/utils/dlpack.py:149 in from_dlpack target_dl_device = torch._C._torchDeviceToDLDevice(device) RuntimeError: The device index is out of range. It must be in [0, 1), but got -1.

Fix Action

Fix / Workaround

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 16 On-line CPU(s) list: 0-15 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) Ultra X7 358H CPU family: 6 Model: 204 Thread(s) per core: 1 Core(s) per socket: 16 Socket(s): 1 Stepping: 2 CPU(s) scaling MHz: 50% CPU max MHz: 4800.0000 CPU min MHz: 400.0000 BogoMIPS: 7372.80 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni lam wbnoinvd dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid bus_lock_detect movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 576 KiB (16 instances) L1i cache: 1 MiB (16 instances) L2 cache: 24 MiB (7 instances) L3 cache: 18 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-15 Vulnerability Gather data sampling: Not affected Vulnerability Ghostwrite: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS Not affected; BHI BHI_DIS_S Vulnerability Srbds: Not affected Vulnerability Tsa: Not affected Vulnerability Tsx async abort: Not affected Vulnerability Vmscape: Not affected

PR fix notes

PR #32460: Register dpnp as a new array API namespace to test against

Description (problem / solution / changelog)

Here is tentative PR to experiment with array API support in dpnp: Intel's hardware optimized implementation of the array API spec with both CPU and GPU support.

Note that when installing dpnp from pip you need to also set the OCL_ICD_FILENAMES env name to point to the location of the "libintelocl.so" file. See: https://github.com/IntelPython/dpnp/issues/2617.

We could setup an extra CI entry for this, but this requires some multiple hundreds of MB of dependencies, an extra 90 s of runtime (measured on a google colab notebook) and that would come with extra maintenance cost, but since my initial experiment only found a single failure, maybe this is bearable?

EDIT: I can now test this PR locally on a machine with an Intel Core Ultra X7 chipset that includes an integrated Arc B390 GPU.

I took the opportunity to update our test harness for PyTorch with the XPU backend because it runs on the same hardware as DPNP.

Status

  • Workaround https://github.com/pytorch/pytorch/issues/181140
  • All float32 tests now pass \o/
  • There are a few float64 failures to investigate (both with DPNP and PyTorch XPU)
  • On my local machine, pytest is deadlocked after running the tests (even when all green). I need to find the time to investigate with faulthandler.
  • Investigate DPNP CPU failures reported in https://github.com/scikit-learn/scikit-learn/pull/32460#issuecomment-4296393854
  • Find out why I cannot run the DPNP CPU tests locally...
  • We probably need to register a dedicated self-hosted Intel GPU CI runner if we want the Intel GPU tests to be easy to run by anybody.
  • Remove the DPNP dependency from the main CI triggered at each PR to avoid adding overhead and instead move it to a dedicated CI config as we do for CUDA.
  • We will also need to update the doc to point users to the relevant installation instructions.

Changed files

  • build_tools/github/pylatest_conda_forge_mkl_linux-64_conda.lock (modified, +23/-5)
  • build_tools/github/pylatest_conda_forge_mkl_linux-64_environment.yml (modified, +2/-0)
  • build_tools/update_environments_and_lock_files.py (modified, +2/-1)
  • sklearn/metrics/pairwise.py (modified, +1/-1)
  • sklearn/utils/_array_api.py (modified, +47/-4)
  • sklearn/utils/_testing.py (modified, +11/-0)

Code Example

>>> import torch, numpy as np
>>> torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu:0")
tensor([1., 1., 1.], device='xpu:0')
>>> torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu")
Traceback (most recent call last):
  Cell In[6], line 1
    torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu")
  File ~/miniforge3/envs/dev/lib/python3.14/site-packages/torch/utils/dlpack.py:149 in from_dlpack
    target_dl_device = torch._C._torchDeviceToDLDevice(device)
RuntimeError: The device index is out of range. It must be in [0, 1), but got -1.
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Passing device="xpu" (as a shorthand for device="xpu:0") raises a TypeError while this shorthand is valid for other device types ("cuda", "mps"`...).

>>> import torch, numpy as np
>>> torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu:0")
tensor([1., 1., 1.], device='xpu:0')
>>> torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu")
Traceback (most recent call last):
  Cell In[6], line 1
    torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu")
  File ~/miniforge3/envs/dev/lib/python3.14/site-packages/torch/utils/dlpack.py:149 in from_dlpack
    target_dl_device = torch._C._torchDeviceToDLDevice(device)
RuntimeError: The device index is out of range. It must be in [0, 1), but got -1.

Note that using device="xpu" works fine for other tensor-allocating torch functions such as torch.ones for instance.

Versions

Collecting environment information... PyTorch version: 2.11.0+xpu Is debug build: False CUDA used to build PyTorch: None ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.4 LTS (x86_64) GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 Clang version: Could not collect CMake version: Could not collect Libc version: glibc-2.39

Python version: 3.14.4 | packaged by conda-forge | (main, Apr 8 2026, 01:59:35) [GCC 14.3.0] (64-bit runtime) Python platform: Linux-6.17.0-22-generic-x86_64-with-glibc2.39 Is CUDA available: False CUDA runtime version: No CUDA CUDA_MODULE_LOADING set to: N/A GPU models and configuration: No CUDA Nvidia driver version: No CUDA cuDNN version: No CUDA Is XPU available: True XPU used to build PyTorch: 20250302 Intel GPU driver version:

  • libze1: 1.27.0-124.04ppa2
  • intel-opencl-icd: 26.05.37020.3-124.04ppa3 Intel GPU models onboard: N/A Intel GPU models detected:
  • [0] _XpuDeviceProperties(name='Intel(R) Graphics [0xb080]', platform_name='Intel(R) oneAPI Unified Runtime over Level-Zero V2', type='gpu', device_id=0xB080, uuid=868080b0-0400-0000-0002-000000000000, driver_version='1.14.37020+3', total_memory=29307MB, local_mem_size=128KB, max_compute_units=96, gpu_eu_count=96, gpu_subslice_count=12, max_work_group_size=1024, max_num_sub_groups=64, sub_group_sizes=[16 32], has_fp16=1, has_fp64=1, has_atomic64=1) HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True Caching allocator config: N/A

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 16 On-line CPU(s) list: 0-15 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) Ultra X7 358H CPU family: 6 Model: 204 Thread(s) per core: 1 Core(s) per socket: 16 Socket(s): 1 Stepping: 2 CPU(s) scaling MHz: 50% CPU max MHz: 4800.0000 CPU min MHz: 400.0000 BogoMIPS: 7372.80 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni lam wbnoinvd dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid bus_lock_detect movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 576 KiB (16 instances) L1i cache: 1 MiB (16 instances) L2 cache: 24 MiB (7 instances) L3 cache: 18 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-15 Vulnerability Gather data sampling: Not affected Vulnerability Ghostwrite: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS Not affected; BHI BHI_DIS_S Vulnerability Srbds: Not affected Vulnerability Tsa: Not affected Vulnerability Tsx async abort: Not affected Vulnerability Vmscape: Not affected

Versions of relevant libraries: [pip3] dpcpp-cpp-rt==2025.3.2 [pip3] impi-rt==2021.17.2 [pip3] intel-cmplr-lib-rt==2025.3.2 [pip3] intel-cmplr-lib-ur==2025.3.2 [pip3] intel-cmplr-lic-rt==2025.3.2 [pip3] intel-opencl-rt==2025.3.2 [pip3] intel-openmp==2025.3.2 [pip3] intel-pti==0.16.0 [pip3] intel-sycl-rt==2025.3.2 [pip3] mkl==2025.3.1 [pip3] numpy==2.4.3 [pip3] oneccl==2021.17.2 [pip3] oneccl-devel==2021.17.2 [pip3] onemkl-license==2025.3.1 [pip3] onemkl-sycl-blas==2025.3.1 [pip3] onemkl-sycl-dft==2025.3.1 [pip3] onemkl-sycl-lapack==2025.3.1 [pip3] onemkl-sycl-rng==2025.3.1 [pip3] onemkl-sycl-sparse==2025.3.1 [pip3] onemkl-sycl-stats==2025.3.0 [pip3] onemkl-sycl-vm==2025.3.0 [pip3] tbb==2022.3.1 [pip3] tcmlib==1.4.1 [pip3] torch==2.11.0+xpu [pip3] torchaudio==2.11.0+xpu [pip3] torchvision==0.26.0+xpu [pip3] triton-xpu==3.7.0 [pip3] umf==1.0.3 [conda] dpcpp-cpp-rt 2025.3.2 pypi_0 pypi [conda] impi-rt 2021.17.2 pypi_0 pypi [conda] intel-cmplr-lib-rt 2025.3.2 pypi_0 pypi [conda] intel-cmplr-lib-ur 2025.3.2 pypi_0 pypi [conda] intel-cmplr-lic-rt 2025.3.2 pypi_0 pypi [conda] intel-opencl-rt 2025.3.2 pypi_0 pypi [conda] intel-openmp 2025.3.2 pypi_0 pypi [conda] intel-pti 0.16.0 pypi_0 pypi [conda] intel-sycl-rt 2025.3.2 pypi_0 pypi [conda] libblas 3.11.0 6_h5875eb1_mkl conda-forge [conda] libcblas 3.11.0 6_hfef963f_mkl conda-forge [conda] liblapack 3.11.0 6_h5e43f62_mkl conda-forge [conda] mkl 2025.3.1 pypi_0 pypi [conda] numpy 2.4.3 py314h2b28147_0 conda-forge [conda] oneccl 2021.17.2 pypi_0 pypi [conda] oneccl-devel 2021.17.2 pypi_0 pypi [conda] onemkl-license 2025.3.1 pypi_0 pypi [conda] onemkl-sycl-blas 2025.3.1 pypi_0 pypi [conda] onemkl-sycl-dft 2025.3.1 pypi_0 pypi [conda] onemkl-sycl-lapack 2025.3.1 pypi_0 pypi [conda] onemkl-sycl-rng 2025.3.1 pypi_0 pypi [conda] onemkl-sycl-sparse 2025.3.1 pypi_0 pypi [conda] onemkl-sycl-stats 2025.3.0 pypi_0 pypi [conda] onemkl-sycl-vm 2025.3.0 pypi_0 pypi [conda] tbb 2022.3.1 pypi_0 pypi [conda] tcmlib 1.4.1 pypi_0 pypi [conda] torch 2.11.0+xpu pypi_0 pypi [conda] torchaudio 2.11.0+xpu pypi_0 pypi [conda] torchvision 0.26.0+xpu pypi_0 pypi [conda] triton-xpu 3.7.0 pypi_0 pypi [conda] umf 1.0.3 pypi_0 pypi

cc @gujinghui @EikanWang @fengyuan14 @guangyey

extent analysis

TL;DR

The issue can be fixed by specifying the full device name, including the index, such as device="xpu:0" instead of relying on the shorthand device="xpu".

Guidance

  • The error occurs because the shorthand device="xpu" is not properly handled when converting to a DLDevice, resulting in an incorrect device index of -1.
  • To mitigate this issue, always specify the full device name, including the index, when using torch.from_dlpack.
  • Verify that the fix works by running the same code with the modified device specification and checking that it no longer raises a TypeError.
  • Consider filing a bug report or feature request to add support for the shorthand device="xpu" in torch.from_dlpack, as it is already supported in other tensor-allocating functions.

Example

import torch
import numpy as np

# Correct usage
torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu:0")

# Incorrect usage (raises TypeError)
torch.from_dlpack(np.ones(3, dtype=np.float32), device="xpu")

Notes

The issue seems to be specific to the torch.from_dlpack function and may not affect other parts of the PyTorch library. The provided workaround should resolve the issue, but it may be worth investigating further to determine the root cause and potentially add support for the shorthand device specification.

Recommendation

Apply the workaround by always specifying the full device name, including the index, when using torch.from_dlpack. This should resolve the issue and allow the code to run without errors.

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

pytorch - ✅(Solved) Fix [XPU] `TypeError` raised when passing `device="xpu"` to `torch.from_dlpack` [1 pull requests, 1 comments, 2 participants]