Skip to content

Pass tensor data pointer to dlDeviceToTorchDevice from maybeCopyTensor in DLConvertor.cpp - #178768

Closed
AKloniecki wants to merge 1 commit into
pytorch:mainfrom
AKloniecki:pass-data-to-dlDeviceToTorchDevice
Closed

AKloniecki wants to merge 1 commit into
pytorch:mainfrom
AKloniecki:pass-data-to-dlDeviceToTorchDevice

Conversation

@AKloniecki

@AKloniecki AKloniecki commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

The optional data pointer argument of dlDeviceToTorchDevice is non-optional for XPU, and must point to correct tensor data, in order for XPU to correctly name the device.

Previously this optional data pointer was omitted in few places, triggering explicit assert in XPU path:

TORCH_CHECK(data != nullptr, "Can't get ATen device for XPU without XPU data.");
.
Now that the argument is passed along, the device can be correctly determined.

Fixes intel/torch-xpu-ops#3077

@pytorch-bot

pytorch-bot Bot commented Mar 30, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

🟡 AUTOMATED REVIEW - Changes requested:

The PR enables same-device XPU DLPack export by passing the source tensor's data
pointer to dlDeviceToTorchDevice. For kDLOneAPI that function returns the device
that owns the pointer and ignores the requested device_id. As a result,
maybeCopyTensor now gets cross-device __dlpack__(dl_device=...) requests wrong
instead of failing loudly as before, and the Module.cpp allocator change does
nothing because the DLPack prototype's data field is unused. Tests cover only
the same-device case.

Findings (3):

[major] aten/src/ATen/DLConvertor.cpp:547
For kDLOneAPI, passing data.data_ptr() makes the resolved device the SOURCE
tensor's device, so the requested device_id is ignored. A direct
x.__dlpack__(dl_device=(kDLOneAPI, 1)) on an xpu:0 tensor now silently stays on
xpu:0, and with copy=False it skips the 'cannot move' error. A CPU source throws
'ptr is not a device type pointer.' instead of copying. Before this PR both
cases failed loudly. Map device_id to a torch XPU index (the inverse of
getGlobalIdxFromDevice) instead of reading the source pointer. Note: data_ptr()
is also now evaluated for every device type, which materializes COW te

[minor] torch/csrc/Module.cpp:785
The DLPack spec (dlpack.h:388-389) says only dtype/ndim/shape/device of the
allocator prototype are used, so prototype->data is normally NULL
(test_dlpack.py:675) or uninitialized. XPU allocation still fails with 'without
XPU data', or getDeviceFromPtr runs on a garbage pointer. Derive the device from
prototype->device.device_id.

[minor] test/test_xpu.py:1832
The test covers only same-device device=x.device, which the pointer lookup
handles by construction. Add coverage for direct __dlpack__(dl_device=...) with
an XPU target that differs from the source (CPU->XPU, and xpu:0->xpu:1 where
available) and for the copy=False error path.

Reviewed commit: 185d0db

Review run

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/178768

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 185d0db with merge base 1a70a6e (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@AKloniecki

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Mar 30, 2026
@EikanWang

Copy link
Copy Markdown
Collaborator

@AKloniecki thanks for the fix. Please help add a regression test to test_xpu.py.

@AKloniecki

Copy link
Copy Markdown
Contributor Author

@EikanWang there is a test case in the original PR that enables the test_dlpack.py test suite, that is currently skipped, but should be reenabled once this PR is merged (https://github.com/shangerxin/pytorch/blob/6f74d5d9c987a7c687bf980c883d1a9738856fe6/test/test_dlpack.py#L527). Should we add a separate TC in test_xpu.py anyway?

@EikanWang

Copy link
Copy Markdown
Collaborator

@AKloniecki , I suggest adding a UT to test_xpu.py first to verify the feature. Regarding test_dlpack.py, we can position it as a thorough test scope of dlpack.

@AKloniecki

Copy link
Copy Markdown
Contributor Author

@EikanWang test added.

@tszulist-hbn

tszulist-hbn commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Found two things below:

  1. Enable the skipped XPU test in test_dlpack.py:527
  2. Verify: Confirm that the Module.cpp:769 call site doesn't need the same fix (appears safe, but worth explicit verification)

@tszulist-hbn tszulist-hbn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found two things below:

Enable the skipped XPU test in test_dlpack.py:527
Verify: Confirm that the Module.cpp:769 call site doesn't need the same fix (appears safe, but worth explicit verification)

@AKloniecki
AKloniecki force-pushed the pass-data-to-dlDeviceToTorchDevice branch from ecd6137 to 79855ae Compare April 22, 2026 12:35
@linux-foundation-easycla

linux-foundation-easycla Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@AKloniecki
AKloniecki force-pushed the pass-data-to-dlDeviceToTorchDevice branch from 79855ae to 1a1c444 Compare April 23, 2026 05:51
@AKloniecki

Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase -b main

@AKloniecki
AKloniecki requested a review from tszulist-hbn April 27, 2026 12:06
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/main. Check the current status here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased pass-data-to-dlDeviceToTorchDevice onto refs/remotes/origin/main, please pull locally before adding more changes (for example, via git checkout pass-data-to-dlDeviceToTorchDevice && git pull --rebase)

@pytorchmergebot
pytorchmergebot force-pushed the pass-data-to-dlDeviceToTorchDevice branch from 1a1c444 to fad7e96 Compare April 27, 2026 12:08
Comment thread aten/src/ATen/DLConvertor.cpp Resolved
Comment thread torch/csrc/Module.cpp Resolved
@EikanWang
EikanWang requested a review from guangyey April 28, 2026 12:41
@AKloniecki
AKloniecki marked this pull request as ready for review May 18, 2026 09:59
@AKloniecki
AKloniecki requested a review from gujinghui as a code owner May 18, 2026 09:59
@AKloniecki
AKloniecki force-pushed the pass-data-to-dlDeviceToTorchDevice branch from fad7e96 to 660e7b6 Compare May 25, 2026 16:48
@ZhaoqiongZ ZhaoqiongZ moved this to Aged Pending Review in PyTorch Intel Jun 29, 2026
@AKloniecki
AKloniecki force-pushed the pass-data-to-dlDeviceToTorchDevice branch from 660e7b6 to 42c4101 Compare July 6, 2026 06:21
@guangyey
guangyey requested a review from albanD July 7, 2026 06:36
@AKloniecki

Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase -b main

tszulist-hbn added a commit to tszulist-hbn/pytorch that referenced this pull request Sep 25, 2026
…template

DLPack stream exchange is only specified for CUDA and ROCm. The seven tests
moved here encode that numbering directly -- stream=1 is the legacy default
stream on CUDA and stream=0 on ROCm, stream=2 is the per-thread default
stream PyTorch rejects -- and reach for torch.cuda.Stream and
torch.cuda._sleep by name. Cloning them into every backend so they can skip
at runtime tells the reader nothing and costs 23 skipped clones per
non-CUDA backend, since two of the seven are dtype-parametrized.

They move to TestTorchDlPackCUDA, instantiated with only_for="cuda",
which makes @skipMeta and @onlyCUDA redundant. The executed set is
unchanged at {cuda}, with test_dlpack_invalid_rocm_streams still gated by
@skipCUDAIfNotRocm. Part of pytorch#142029.

Test Plan:

```
python test/test_dlpack.py
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
python -m pytest test/test_dlpack.py --collect-only -q -p no:randomly
```

281 tests, 237 passed / 44 skipped / 0 failed, against 327 tests and 237/90/0
on the parent commit. The per-test outcome map loses 46 lines and gains none,
and every one of the 46 was SKIPPED: the 23 cpu and 23 xpu clones of the seven
moved tests. The passed set is untouched, so no coverage was traded away.

only_for="cuda" generates no class on this box, so collection lists only
TestTorchDlPackDeviceCPU, TestTorchDlPackDeviceXPU and TestReadOnlyDLPack; the
seven tests need a CUDA runner to execute, exactly as before the move.

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
tszulist-hbn added a commit to tszulist-hbn/pytorch that referenced this pull request Sep 25, 2026
Eight tests in test/test_dlpack.py do not depend on where the tensor lives.
They cover export rejections -- requires_grad, conjugate views, non-strided
layouts, a stream argument on CPU -- plus stride normalization, the copy
fallback and the unsupported-dtype error. Six take no device argument at
all and the other two ignore theirs, so the device template was cloning
each of them into every backend to run byte-identical work. They move to a
plain TestTorchDlPack that is never passed to
instantiate_device_type_tests, so each runs once. The @skipMeta that seven
of the eight carried is dropped rather than moved, because skipIf reads
slf.device_type and a plain TestCase has no such attribute.

Two of them lose a restriction outright rather than trading it for an
only_for. test_from_dlpack_negative_strides and
test_dlpack_unsupported_dtype_error were @onlyCPU, so they did not run at
all wherever the CPU base is filtered out, as on GPU-only shards or under
PYTORCH_TESTING_DEVICE_ONLY_FOR=cuda. Both bodies build only CPU tensors,
so each still runs once and runs the same code it ran as the _cpu clone,
but the restriction is now gone everywhere rather than on XPU alone, which
is worth stating rather than burying. Their unused device parameters go
with the decorator.

test_numpy_dlpack_protocol_conversion joins them. It consumes a capsule
produced by NumPy, which only allocates on the host, so it was @onlyCPU as
well and its xpu clones existed only to skip. Like the two above, it now
also runs where the CPU base is filtered out, on the same CPU tensors. It
takes its dtypes from @parametrize, since @dtypes needs the device
template, and its @skipMeta is dropped for the same reason as the others'.

Part of pytorch#142029.

Test Plan:

```
python test/test_dlpack.py
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
python -m pytest test/test_dlpack.py --collect-only -q -p no:randomly
```

260 tests, 231 passed / 29 skipped / 0 failed, against 281 tests and 237/44/0
on the parent commit. The per-test outcome map loses 42 lines and gains 21: for
each of the eight tests the cpu and xpu clones are replaced by one unsuffixed
entry, and the 13 cpu and 13 xpu dtype clones of
test_numpy_dlpack_protocol_conversion by 13 entries without a device suffix.
All 21 pass. Six of the eight were PASSED on both bases, so they account for
six fewer executions of identical work; the two former @onlyCPU tests and the
NumPy test had SKIPPED xpu clones, 15 in all, and those are gone.

Collection confirms TestTorchDlPack appears once with 21 tests and no
per-device suffix, alongside TestTorchDlPackDeviceCPU (115),
TestTorchDlPackDeviceXPU (115) and TestReadOnlyDLPack (9).

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
tszulist-hbn added a commit to tszulist-hbn/pytorch that referenced this pull request Sep 25, 2026
Adds hw_classification to the four test classes in test/test_dlpack.py.
Part of pytorch#142029.

The classification follows the agreed rule: ACCELERATOR when a class is
instantiated via instantiate_device_type_tests, GENERIC when it is not.
ACCELERATOR covers behavior that must hold consistently across every device
including CPU, so TestTorchDlPackDevice is ACCELERATOR even though most of
its tests also run on CPU -- see TestTorchAutocastDevice in
test/test_autocast.py, which is ACCELERATOR and instantiated with no device
restriction. TestTorchDlPack and TestReadOnlyDLPack are not instantiated,
so they are GENERIC. TestTorchDlPackCUDA, whose instantiation is pinned
to one backend, takes that backend's label, matching
TestUnaryUfuncsCUDADevice in test/test_unary_ufuncs.py.

The attribute is set on the generic classes and inherited by the per-device
classes that instantiate_device_type_tests generates, so it does not need
to be applied to the generated names.

Test Plan:

```
python test/test_dlpack.py
python test/test_dlpack.py --hw-classification GENERIC
python test/test_dlpack.py --hw-classification ACCELERATOR
python test/test_dlpack.py --hw-classification CUDA
python test/test_dlpack.py --hw-classification MPS
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
```

260 tests, 231 passed / 29 skipped / 0 failed, and the per-test outcome map is
byte-identical to the parent commit; setting a class attribute cannot change
what runs unfiltered.

The four selectors partition the file exactly, with unclassified=0 on every
one: GENERIC 30, ACCELERATOR 230, CUDA 0, MPS 0, summing to 260. CUDA and MPS
are empty because neither device exists on this box.

The 29 skips are 24 cpu clones of the @OnlyOn(["xpu", "cuda"]) tests, 2 needing
a second device, 2 needing a newer NumPy, and test_no_copy_xpu, whose pytorch#3077
skip is removed by pytorch#178768.

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
@pbielak
pbielak requested review from EikanWang and jansel September 29, 2026 10:49
tszulist-hbn added a commit to tszulist-hbn/pytorch that referenced this pull request Sep 30, 2026
Review feedback on pytorch#195722: the seven tests in TestTorchDlPackDevice that
were @OnlyOn(["xpu", "cuda"]) exercise the generic DLPack device contract,
so they now use @onlyAccelerator and opt MPS out only where it actually
fails. The class is already instantiated with allow_mps=True, and the
macos-m1 default shard collects its MPS clones (test_copy_mps,
test_no_copy_mps, ...), so this enables them there.

The two stream tests (test_dlpack_conversion_with_streams and
test_dlpack_conversion_with_streams_narrow_precision) get @skipMPS. Their
helper calls stream.synchronize(), and MPSGuardImpl does not override
synchronizeStream, so the base DeviceGuardImplInterface raises "Backend
doesn't support synchronizing streams." The float64/complex128 variants
would also fail earlier in make_tensor, since the test has no dtypesIfMPS.
test_numpy_cross_device_multi_gpu skips itself on MPS through
deviceCountAtLeast(2). The remaining four (test_copy, test_no_copy,
test_needs_copy_error, test_numpy_cross_device_transfer) are expected to
pass on MPS from reading DLConvertor.cpp and torch/utils/dlpack.py.

On CUDA and XPU nothing changes: onlyAccelerator only skips cpu and meta,
and skipMPS is a no-op off MPS.

Test Plan:

Per-test outcomes before and after, on a single-Battlemage XPU box with no
CUDA and no MPS device:

```
cd test && python -m pytest test_dlpack.py -p no:randomly -v --tb=no
lintrunner -a -- test/test_dlpack.py
```

262 tests, identical outcome map before and after. The one failure on
both sides is test_copy_xpu, which depends on the unmerged
pytorch#178768 (intel/torch-xpu-ops#3077). The MPS predictions
above were not run locally; the macos-m1 default shard is the first
place they execute.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@dvrogozh dvrogozh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this optional data pointer was not passed to dlDeviceToTorchDevice, which is not supported on XPU. Now that is being passed along, the device can be correctly determined.

Please, polish PR description which will go to the commit message. I am confused reading the first sentence: if dlDeviceToTorchDevice is not supported on XPU, then why we should care and pass some optional data pointer? This does not make sense. You likely wanted to say that you've added support for XXX by doing YYY.

pytorchmergebot pushed a commit to tszulist-hbn/pytorch that referenced this pull request Oct 2, 2026
…device under test

test_automatically_select_in_creation in test/test_dlpack.py is a device
template test, but it allocated its input with a bare torch.rand(10), so
every non-CPU clone handed torch.tensor a CPU producer. The property the
test is named for, that torch.tensor detects __dlpack__ on the wrapper and
imports the buffer instead of iterating element by element, was therefore
only ever exercised against the CPU export path.

Allocating on the injected device makes each clone drive its own backend's
export path. The assertion is left as it was: assertEqual defaults to
exact_device=False, so this commit widens what the test runs, not what it
checks.
Part of pytorch#142029.

Test Plan:

```
python test/test_dlpack.py
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
```

327 tests, 234 passed / 93 skipped / 0 failed, and the per-test outcome map is
byte-identical to the parent commit. That is the expected result: the xpu clone
already passed, and allocating on the injected device changes which backend it
exercises, not whether it succeeds.

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
pytorchmergebot pushed a commit to tszulist-hbn/pytorch that referenced this pull request Oct 2, 2026
…lpack.py

test_dlpack_exchange_api carried a skip for intel/torch-xpu-ops#3074, which
is closed. The test passes on Intel GPU, so the skip only hides coverage.
Part of pytorch#142029.

The skipXPUIf import stays: test_no_copy still carries a skip for
intel/torch-xpu-ops#3077, which pytorch#178768 removes.

Test Plan:

```
python test/test_dlpack.py
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
```

327 tests, 235 passed / 92 skipped / 0 failed, against 234/93/0 on the parent
commit. Exactly one line moves in the per-test outcome map:
test_dlpack_exchange_api_xpu, SKIPPED -> PASSED.

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
pytorchmergebot pushed a commit to tszulist-hbn/pytorch that referenced this pull request Oct 2, 2026
test_copy and test_needs_copy_error in test/test_dlpack.py were @onlyCUDA,
but neither body is CUDA-specific: they exercise the copy=True and
copy=False contract of torch.from_dlpack across a device boundary. Both now
carry @OnlyOn(["xpu", "cuda"]), matching test_no_copy immediately below
them, so both run on Intel GPU. Part of pytorch#142029.

test_copy on XPU depends on pytorch#178768, so this PR has to land
after it. Its out_device=device sub-case reaches dlDeviceToTorchDevice through
maybeCopyTensor, which without that fix raises "Can't get ATen device for XPU
without XPU data" (intel/torch-xpu-ops#3077).

@onlyAccelerator would have been the broader choice and was rejected: it
also selects MPS, where neither test has ever run, and enabling a backend
speculatively either goes green by luck or lands a failure with no issue
behind it.

Test Plan:

```
python test/test_dlpack.py
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
python -m pytest test/test_dlpack.py -v -p no:randomly -k copy
```

327 tests, 237 passed / 90 skipped / 0 failed, against 235/92/0 on the parent
commit. Exactly the two newly enabled clones move in the per-test outcome map:
test_copy_xpu and test_needs_copy_error_xpu, both SKIPPED -> PASSED.

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
pytorchmergebot pushed a commit to tszulist-hbn/pytorch that referenced this pull request Oct 2, 2026
test/test_dlpack.py has one test class instantiated through
instantiate_device_type_tests. The Device suffix marks it as the
device-instantiated template, an established convention in the test suite:
TestTypePromotionDevice in test/test_type_promotion.py (pytorch#190990),
TestScatterGatherDevice, TestHOPDevice and others. Part of pytorch#142029.

Renaming a test class changes every lookup key of the form
f"{class_name}.{test_name}", so the three name-keyed mechanisms were swept
first. No marker file under test/dynamo_expected_failures, test/dynamo_skips,
test/compiled_autograd_skips, test/inductor_expected_failures or
test/inductor_skips names a TestTorchDlPack class -- the TestDLPack markers
there belong to test/torch_np -- the test-infra disabled-tests list has no
DLPack entry, and nothing outside this file refers to the generated names.

A later commit reuses the bare name TestTorchDlPack for the tests that are
not device-dependent, so the rename goes first to keep the two steps
separately reviewable.

Test Plan:

```
python test/test_dlpack.py
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
python -m pytest test/test_dlpack.py --collect-only -q -p no:randomly
```

327 tests, 237 passed / 90 skipped / 0 failed, unchanged from the parent
commit. The per-test outcome map, which is keyed on the method name, is
identical apart from two skip-reason strings that pytest truncates one
character earlier now that the class name is longer. Collection confirms the
generated classes are TestTorchDlPackDeviceCPU and TestTorchDlPackDeviceXPU.

The name sweep above was three checks: a filename grep over the five marker
directories, a lookup in the downloaded disabled-tests-condensed.json (193
entries, no DLPack match), and a repo-wide grep for TestTorchDlPack outside
this file (no hits).

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
pytorchmergebot pushed a commit to tszulist-hbn/pytorch that referenced this pull request Oct 2, 2026
…template

DLPack stream exchange is only specified for CUDA and ROCm. The seven tests
moved here encode that numbering directly -- stream=1 is the legacy default
stream on CUDA and stream=0 on ROCm, stream=2 is the per-thread default
stream PyTorch rejects -- and reach for torch.cuda.Stream and
torch.cuda._sleep by name. Cloning them into every backend so they can skip
at runtime tells the reader nothing and costs 23 skipped clones per
non-CUDA backend, since two of the seven are dtype-parametrized.

They move to TestTorchDlPackCUDA, instantiated with only_for="cuda",
which makes @skipMeta and @onlyCUDA redundant. The executed set is
unchanged at {cuda}, with test_dlpack_invalid_rocm_streams still gated by
@skipCUDAIfNotRocm. Part of pytorch#142029.

Test Plan:

```
python test/test_dlpack.py
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
python -m pytest test/test_dlpack.py --collect-only -q -p no:randomly
```

281 tests, 237 passed / 44 skipped / 0 failed, against 327 tests and 237/90/0
on the parent commit. The per-test outcome map loses 46 lines and gains none,
and every one of the 46 was SKIPPED: the 23 cpu and 23 xpu clones of the seven
moved tests. The passed set is untouched, so no coverage was traded away.

only_for="cuda" generates no class on this box, so collection lists only
TestTorchDlPackDeviceCPU, TestTorchDlPackDeviceXPU and TestReadOnlyDLPack; the
seven tests need a CUDA runner to execute, exactly as before the move.

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
pytorchmergebot pushed a commit to tszulist-hbn/pytorch that referenced this pull request Oct 2, 2026
Eight tests in test/test_dlpack.py do not depend on where the tensor lives.
They cover export rejections -- requires_grad, conjugate views, non-strided
layouts, a stream argument on CPU -- plus stride normalization, the copy
fallback and the unsupported-dtype error. Six take no device argument at
all and the other two ignore theirs, so the device template was cloning
each of them into every backend to run byte-identical work. They move to a
plain TestTorchDlPack that is never passed to
instantiate_device_type_tests, so each runs once. The @skipMeta that seven
of the eight carried is dropped rather than moved, because skipIf reads
slf.device_type and a plain TestCase has no such attribute.

Two of them lose a restriction outright rather than trading it for an
only_for. test_from_dlpack_negative_strides and
test_dlpack_unsupported_dtype_error were @onlyCPU, so they did not run at
all wherever the CPU base is filtered out, as on GPU-only shards or under
PYTORCH_TESTING_DEVICE_ONLY_FOR=cuda. Both bodies build only CPU tensors,
so each still runs once and runs the same code it ran as the _cpu clone,
but the restriction is now gone everywhere rather than on XPU alone, which
is worth stating rather than burying. Their unused device parameters go
with the decorator.

test_numpy_dlpack_protocol_conversion joins them. It consumes a capsule
produced by NumPy, which only allocates on the host, so it was @onlyCPU as
well and its xpu clones existed only to skip. Like the two above, it now
also runs where the CPU base is filtered out, on the same CPU tensors. It
takes its dtypes from @parametrize, since @dtypes needs the device
template, and its @skipMeta is dropped for the same reason as the others'.

Part of pytorch#142029.

Test Plan:

```
python test/test_dlpack.py
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
python -m pytest test/test_dlpack.py --collect-only -q -p no:randomly
```

260 tests, 231 passed / 29 skipped / 0 failed, against 281 tests and 237/44/0
on the parent commit. The per-test outcome map loses 42 lines and gains 21: for
each of the eight tests the cpu and xpu clones are replaced by one unsuffixed
entry, and the 13 cpu and 13 xpu dtype clones of
test_numpy_dlpack_protocol_conversion by 13 entries without a device suffix.
All 21 pass. Six of the eight were PASSED on both bases, so they account for
six fewer executions of identical work; the two former @onlyCPU tests and the
NumPy test had SKIPPED xpu clones, 15 in all, and those are gone.

Collection confirms TestTorchDlPack appears once with 21 tests and no
per-device suffix, alongside TestTorchDlPackDeviceCPU (115),
TestTorchDlPackDeviceXPU (115) and TestReadOnlyDLPack (9).

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
pytorchmergebot pushed a commit to tszulist-hbn/pytorch that referenced this pull request Oct 2, 2026
Adds hw_classification to the four test classes in test/test_dlpack.py.
Part of pytorch#142029.

The classification follows the agreed rule: ACCELERATOR when a class is
instantiated via instantiate_device_type_tests, GENERIC when it is not.
ACCELERATOR covers behavior that must hold consistently across every device
including CPU, so TestTorchDlPackDevice is ACCELERATOR even though most of
its tests also run on CPU -- see TestTorchAutocastDevice in
test/test_autocast.py, which is ACCELERATOR and instantiated with no device
restriction. TestTorchDlPack and TestReadOnlyDLPack are not instantiated,
so they are GENERIC. TestTorchDlPackCUDA, whose instantiation is pinned
to one backend, takes that backend's label, matching
TestUnaryUfuncsCUDADevice in test/test_unary_ufuncs.py.

The attribute is set on the generic classes and inherited by the per-device
classes that instantiate_device_type_tests generates, so it does not need
to be applied to the generated names.

Test Plan:

```
python test/test_dlpack.py
python test/test_dlpack.py --hw-classification GENERIC
python test/test_dlpack.py --hw-classification ACCELERATOR
python test/test_dlpack.py --hw-classification CUDA
python test/test_dlpack.py --hw-classification MPS
python -m pytest test/test_dlpack.py -v --tb=no -p no:randomly
```

260 tests, 231 passed / 29 skipped / 0 failed, and the per-test outcome map is
byte-identical to the parent commit; setting a class attribute cannot change
what runs unfiltered.

The four selectors partition the file exactly, with unclassified=0 on every
one: GENERIC 30, ACCELERATOR 230, CUDA 0, MPS 0, summing to 260. CUDA and MPS
are empty because neither device exists on this box.

The 29 skips are 24 cpu clones of the @OnlyOn(["xpu", "cuda"]) tests, 2 needing
a second device, 2 needing a newer NumPy, and test_no_copy_xpu, whose pytorch#3077
skip is removed by pytorch#178768.

Measured on a single-Battlemage box with no CUDA and no MPS device, so the
cuda and mps bases are never generated here. pytorch#178768 was
applied locally, since test_copy on XPU depends on it.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5 <[email protected]>
pytorchmergebot pushed a commit to tszulist-hbn/pytorch that referenced this pull request Oct 2, 2026
Review feedback on pytorch#195722: the seven tests in TestTorchDlPackDevice that
were @OnlyOn(["xpu", "cuda"]) exercise the generic DLPack device contract,
so they now use @onlyAccelerator and opt MPS out only where it actually
fails. The class is already instantiated with allow_mps=True, and the
macos-m1 default shard collects its MPS clones (test_copy_mps,
test_no_copy_mps, ...), so this enables them there.

The two stream tests (test_dlpack_conversion_with_streams and
test_dlpack_conversion_with_streams_narrow_precision) get @skipMPS. Their
helper calls stream.synchronize(), and MPSGuardImpl does not override
synchronizeStream, so the base DeviceGuardImplInterface raises "Backend
doesn't support synchronizing streams." The float64/complex128 variants
would also fail earlier in make_tensor, since the test has no dtypesIfMPS.
test_numpy_cross_device_multi_gpu skips itself on MPS through
deviceCountAtLeast(2). The remaining four (test_copy, test_no_copy,
test_needs_copy_error, test_numpy_cross_device_transfer) are expected to
pass on MPS from reading DLConvertor.cpp and torch/utils/dlpack.py.

On CUDA and XPU nothing changes: onlyAccelerator only skips cpu and meta,
and skipMPS is a no-op off MPS.

Test Plan:

Per-test outcomes before and after, on a single-Battlemage XPU box with no
CUDA and no MPS device:

```
cd test && python -m pytest test_dlpack.py -p no:randomly -v --tb=no
lintrunner -a -- test/test_dlpack.py
```

262 tests, identical outcome map before and after. The one failure on
both sides is test_copy_xpu, which depends on the unmerged
pytorch#178768 (intel/torch-xpu-ops#3077). The MPS predictions
above were not run locally; the macos-m1 default shard is the first
place they execute.

Authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@AKloniecki

AKloniecki commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Please, polish PR description which will go to the commit message. I am confused reading the first sentence: if dlDeviceToTorchDevice is not supported on XPU, then why we should care and pass some optional data pointer? This does not make sense. You likely wanted to say that you've added support for XXX by doing YYY.

@dvrogozh You're right, that was some really broken english in there. What I meant is that 'not passing the optional data pointer <- is not supported', but obviously the straightforward way of saying that is, that it is required. I've updated the description.

@AKloniecki
AKloniecki requested a review from dvrogozh October 2, 2026 13:24

@dvrogozh dvrogozh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, fix a typo in the PR description (you are pointing to your fork copy):

Previously this optional data pointer was omitted in few places, triggering explicit assert in XPU path: https://github.com/AKloniecki/pytorch/blob/60786e267c6c8b74bceaf0c11f463b355028b67f/aten/src/ATen/DLConvertor.cpp#L184.

Make sure to point to the assert in the upstream pytorch with the valid commit id belonging to the main branch.

@AKloniecki

Copy link
Copy Markdown
Contributor Author

@dvrogozh Corrected PR description.

@AKloniecki
AKloniecki requested a review from dvrogozh October 5, 2026 09:52

@dvrogozh dvrogozh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AKloniecki

Copy link
Copy Markdown
Contributor Author

@jansel Could you please review these changes and help to move them along?
Thanks in advance!

@jansel jansel added the in progress PR is being worked on by the author, until the automated review accepts it label Oct 6, 2026
@AKloniecki

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 ciflow/trunk Trigger trunk jobs on your pull request ciflow/xpu Run XPU CI tasks in progress PR is being worked on by the author, until the automated review accepts it Merged open source topic: not user facing topic category

Projects

Status: Aged Pending Review

Development

Successfully merging this pull request may close these issues.

[upstream ut] test_dlpack.py::TestTorchDlPackXPU::test_no_copy_xpu - RuntimeError: Can't get ATen device for XPU without XPU data.

10 participants