Skip to content

[Test] Refactor test_dlpack.py and enable on XPU - #195722

Closed
tszulist-hbn wants to merge 8 commits into
pytorch:mainfrom
tszulist-hbn:dlpack-xpu-enablement
Closed

tszulist-hbn wants to merge 8 commits into
pytorch:mainfrom
tszulist-hbn:dlpack-xpu-enablement

Conversation

@tszulist-hbn

@tszulist-hbn tszulist-hbn commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue or supporting maintainer

Part of #142029

BC-breaking?

No

Summary

This PR updates test/test_dlpack.py to reduce repeated device-specific test coverage and enable XPU coverage where tests are backend-agnostic.

Agent details

What changed

  • Moved the device-independent tests out of the device template into a plain TestTorchDlPack, so each runs once instead of once per backend; the device-generic tests stay in TestTorchDlPackDevice.
  • Switched the copy, NumPy cross-device, stream and read-only wrapper tests from onlyCUDA / onlyOn(["xpu", "cuda"]) to @onlyAccelerator, with @skipMPS on the four stream tests MPS cannot run.
  • Dropped the stale XPU skips for [Bug Skip] test_dlpack_exchange_api expect current_work_stream is NOT null intel/torch-xpu-ops#3074.
  • Ported the two cross-stream exchange tests (test_dlpack_conversion_with_diff_streams and its narrow-precision variant) to torch.Stream and Stream.native_handle, and skipped them on XPU as a feature gap (DLPack stream exchange is not implemented on XPU intel/torch-xpu-ops#5641); the five that test the CUDA/ROCm stream numbers stay @onlyCUDA.
  • Added hardware classification metadata to all test classes.
  • Test-only change; no product code was modified.

Test coverage delta

Metric Before After
XPU tests collected 161 140
XPU passed / skipped 117 / 44 115 / 25
CPU tests collected 161 140
CPU passed / skipped 113 / 48 92 / 48
Device-independent tests collected 9 30
Device-independent passed / skipped 7 / 2 28 / 2
Total tests collected 331 310
Total passed / skipped 237 / 94 235 / 75

No tests failed. The 21 tests moved to TestTorchDlPack now run once instead of once per device, which accounts for the drop in collected tests. On XPU, test_copy, test_needs_copy_error, test_dlpack_exchange_api and test_dlpack_exchange_api_sliced now pass, and the 18 XPU skips of the two cross-stream exchange tests point at intel/torch-xpu-ops#5641 instead of onlyCUDA.

@pytorch-bot

pytorch-bot Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

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

✅ AUTOMATED REVIEW - Ready for human review:

The PR splits test/test_dlpack.py into device-independent TestTorchDlPack and
per-device TestTorchDlPackDevice (using @​ onlyAccelerator/@​ skipMPS, dropping
stale XPU skips, porting cross-stream tests to torch.Stream) and updates a
docstring in test_cutedsl_smoketest.py. Recommendation: Needs Discussion  etaf
(maintainer) should confirm the author's split of keeping five stream tests @​
onlyCUDA while two skip on XPU against intel/torch-xpu-ops# 5641; the author's
question to etaf is still unanswered. Separately, the exact_device check in
test_automatically_select_in_creation should be fixed.

Findings (1):

[minor] test/test_dlpack.py:513
The device check in `test_automatically_select_in_creation` is too weak. The PR
now builds the source tensor on `device` (`torch.rand(10, device=device)`), so
on accelerators it exercises the `__dlpack__` path in `torch.tensor(...)`
(`torch/csrc/utils/tensor_new.cpp:343-358`). But `self.assertEqual(tensor,
new_tensor)` compares with `exact_device=False` by default
(`common_utils.py:5022`), so it only checks values, not the device. If
`torch.tensor(wrapper)` silently put the result on CPU instead of keeping the
producer's device (`tensor.device()` at `tensor_new.cpp:350`), this assertion
would still pass. Fix: use `self.assertEqual(tensor, new_tensor,
exact_device=True)` or assert `new_tensor.device == tensor.device`.

Reviewed commit: 974ed2f

Review run

🔗 Helpful Links

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

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

✅ You can merge normally! (9 Unrelated Failures)

As of commit 974ed2f with merge base 26650ac (image):

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

BROKEN TRUNK - The following jobs failed but was present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@pytorch-bot

pytorch-bot Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@chuanqi129 chuanqi129 added the ci-no-td Do not run TD on this PR label Sep 3, 2026
@chuanqi129 chuanqi129 added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 3, 2026
@daisyden

daisyden commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator

@newtdms could you please help review this test port / refactor PR? Thanks!

@tszulist-hbn tszulist-hbn changed the title [Test] Enable test_dlpack.py on Intel GPU and classify its test classes [Test] Enable test_dlpack.py on XPU Sep 10, 2026

@pbielak pbielak left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please also update the PR title, e.g., "[Test] Refactor test_dlpack.py and enable on XPU"

Comment thread test/test_dlpack.py Resolved
Comment thread test/test_dlpack.py Resolved
@tszulist-hbn tszulist-hbn changed the title [Test] Enable test_dlpack.py on XPU [Test] Refactor test_dlpack.py and enable on XPU Sep 25, 2026
@tszulist-hbn
tszulist-hbn force-pushed the dlpack-xpu-enablement branch 3 times, most recently from 2a699ee to 947a669 Compare September 25, 2026 14:38
Comment thread test/test_dlpack.py Resolved
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]>
Comment thread test/test_dlpack.py Resolved
@tszulist-hbn

Copy link
Copy Markdown
Contributor Author

@pytorchbot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Successfully rebased dlpack-xpu-enablement onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout dlpack-xpu-enablement && git pull --rebase)

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]>
@pbielak
pbielak requested a review from etaf October 7, 2026 08:48
Comment thread test/test_dlpack.py Resolved
Comment thread test/test_dlpack.py Resolved
@tszulist-hbn
tszulist-hbn force-pushed the dlpack-xpu-enablement branch from 78875a3 to ae1d5eb Compare October 8, 2026 13:10
tszulist-hbn added a commit to tszulist-hbn/pytorch that referenced this pull request Oct 26, 2026
Review feedback on pytorch#195722: the stream tests in TestTorchDlPackDevice were
@onlyCUDA, so their XPU instances skipped as "Only runs on cuda" with
nothing pointing at the gap. The two that exchange a tensor across streams,
test_dlpack_conversion_with_diff_streams and its _narrow_precision variant,
now use the device-generic stream API (torch.Stream, `with stream:` and
Stream.native_handle) and the gating of their siblings
test_dlpack_conversion_with_streams*, @onlyAccelerator plus @skipMPS, and
skip on XPU against intel/torch-xpu-ops#5641. Part of pytorch#142029.

The other five stay @onlyCUDA, as on main: test_dlpack_default_stream,
test_dlpack_convert_default_stream, test_dlpack_cuda_per_thread_stream,
test_dlpack_invalid_rocm_streams and test_dlpack_invalid_cuda_streams.
They check the array API's CUDA and ROCm stream numbers (1 is CUDA's legacy
default stream and 0 ROCm's, 2 is CUDA's per-thread default stream, 0 is
invalid on CUDA and 1 and 2 on ROCm). SYCL has no such numbering, so these
need XPU counterparts rather than a port, and a pytorch#5641 skip on them could
never be lifted.

The gap is in Tensor.__dlpack__, which only honors the stream argument for
CUDA tensors. On XPU any integer is accepted and ignored, so no ordering is
set up between the producer's and the consumer's queue, and
torch.from_dlpack never passes a stream for a kDLOneAPI producer. On XPU
native_handle is the sycl::queue pointer, the value pytorch#5641 suggests a
consumer pass. With the skip removed, the XPU instances pass today, but
only because the stream is ignored: the test synchronizes both streams
before comparing, so it cannot observe the ordering on any backend, and a
pass on XPU is not evidence of the feature. The skip stays until pytorch#5641
defines what XPU accepts.

On CUDA and ROCm nothing changes: native_handle returns the same
cudaStream_t as torch.cuda.Stream.cuda_stream (both are
CUDAStream::stream(), in CUDAGuardImpl::getStreamNativeHandle and
torch/csrc/cuda/Stream.cpp),
torch.Stream() takes a stream from the same pool as torch.cuda.Stream(),
and skipXPUIf and skipMPS are no-ops there. MPS is excluded for the same
reason as the siblings: MPSGuardImpl does not implement synchronizeStream.

Test Plan:

```
python test/test_dlpack.py -v
```

Run on this commit and on its parent: 310 tests, 235 passed / 75 skipped /
0 failed on both, with an identical per-test outcome map. Only skip reasons
change, on 36 instances of the two diff_streams tests (12 + 6 dtypes): the
18 xpu instances go from "Only runs on cuda" to the issue URL, and the 18
cpu instances from "Only runs on cuda" to
"onlyAccelerator: doesn't run on CPU or meta devices". The five @onlyCUDA
tests skip the same way on both sides. The same file with the two
skipXPUIf lines removed, run with -k diff_streams, passes all 18 xpu
instances.

Measured on an Intel Arc B580 box with one XPU device and no CUDA or MPS
device, so the cuda and mps bases are never generated here, against a
2.16.0.dev20261016+xpu nightly torch.

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@tszulist-hbn
tszulist-hbn force-pushed the dlpack-xpu-enablement branch from d4a8d01 to 2f44354 Compare October 26, 2026 12:04
github-actions[bot]

This comment was marked as outdated.

@tszulist-hbn

Copy link
Copy Markdown
Contributor Author

@pytorchmergebot rebase

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

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

tszulist-hbn and others added 8 commits October 26, 2026 12:18
…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:

```
cd test
python -m pytest test_dlpack.py -p no:randomly -v --tb=no -rs \
    --junitxml=dlpack.xml
```

331 tests, 237 passed / 93 skipped / 1 failed, and the per-test outcome map
is 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 an Intel Data Center GPU Max 1550 box with two XPU devices and
no CUDA or MPS device, so the cuda and mps bases are never generated here.
The one failure on both sides, test_no_copy_xpu, comes from the local torch
build: it predates pytorch#178768, which the base contains, and CI
builds include the fix.

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

Co-Authored-By: Claude Opus 5 <[email protected]>
…dlpack.py

test_dlpack_exchange_api and test_dlpack_exchange_api_sliced carried a skip
for intel/torch-xpu-ops#3074, which is closed. Both tests pass on Intel
GPU, so the skips only hide coverage. They were the last users of skipXPUIf
in the file, so the import goes too. Part of pytorch#142029.

Test Plan:

```
cd test
python -m pytest test_dlpack.py -p no:randomly -v --tb=no -rs \
    --junitxml=dlpack.xml
```

331 tests, 239 passed / 91 skipped / 1 failed, against 237/93/1 on the
parent commit. Exactly two lines move in the per-test outcome map:
test_dlpack_exchange_api_xpu and test_dlpack_exchange_api_sliced_xpu, both
SKIPPED -> PASSED.

Measured on an Intel Data Center GPU Max 1550 box with two XPU devices and
no CUDA or MPS device, so the cuda and mps bases are never generated here.
The one failure on both sides, test_no_copy_xpu, comes from the local torch
build: it predates pytorch#178768, which the base contains, and CI
builds include the fix.

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

Co-Authored-By: Claude Opus 5 <[email protected]>
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 between them, so
both run on Intel GPU. Part of pytorch#142029.

test_copy on XPU depends on pytorch#178768, which the base
includes. 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, which also selects MPS, is a separate step later in this
stack.

Test Plan:

```
cd test
python -m pytest test_dlpack.py -p no:randomly -v --tb=no -rs \
    --junitxml=dlpack.xml
```

331 tests, 240 passed / 89 skipped / 2 failed, against 239/91/1 on the
parent commit. Exactly the two newly enabled xpu clones move in the
per-test outcome map: test_needs_copy_error_xpu goes SKIPPED -> PASSED,
and test_copy_xpu SKIPPED -> FAILED with the pytorch#3077 error above, because
the local torch build predates pytorch#178768. With that fix
applied, test_copy_xpu passed in an earlier run on a Battlemage box. Their
cpu clones still skip, now as "Only runs on ['xpu', 'cuda']".

Measured on an Intel Data Center GPU Max 1550 box with two XPU devices and
no CUDA or MPS device, so the cuda and mps bases are never generated here.
The other failure, test_no_copy_xpu on both sides, has the same cause; CI
builds include the fix.

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

Co-Authored-By: Claude Opus 5 <[email protected]>
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.
The one reference to the class itself, a docstring that pytorch#195575 added to
test/python_native/test_cutedsl_smoketest.py, is updated here.

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:

```
cd test
python -m pytest test_dlpack.py -p no:randomly -v --tb=no -rs \
    --junitxml=dlpack.xml
```

331 tests, 240 passed / 89 skipped / 2 failed, unchanged from the parent
commit. The per-test outcome map, keyed on the method name, is identical,
and so is every skip reason; only the repro hint in the two failure
messages names the new class. The run generates 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 (230
entries, no DLPack match), and a repo-wide grep for TestTorchDlPack outside
this file (one hit, that docstring).

Measured on an Intel Data Center GPU Max 1550 box with two XPU devices and
no CUDA or MPS device, so the cuda and mps bases are never generated here.
The local torch build predates pytorch#178768, which the base
contains, so test_copy_xpu and test_no_copy_xpu fail on both sides; CI
builds include the fix.

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

Co-Authored-By: Claude Opus 5 <[email protected]>
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, negative
strides, 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:

```
cd test
python -m pytest test_dlpack.py -p no:randomly -v --tb=no -rs \
    --junitxml=dlpack.xml
```

310 tests, 234 passed / 74 skipped / 2 failed, against 331 tests and
240/89/2 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, and no test present on both sides changes outcome.
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.

The run collects TestTorchDlPack once with 21 tests and no per-device
suffix, alongside TestTorchDlPackDeviceCPU (140), TestTorchDlPackDeviceXPU
(140) and TestReadOnlyDLPack (9).

Measured on an Intel Data Center GPU Max 1550 box with two XPU devices and
no CUDA or MPS device, so the cuda and mps bases are never generated here.
The local torch build predates pytorch#178768, which the base
contains, so test_copy_xpu and test_no_copy_xpu fail on both sides; CI
builds include the fix.

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

Co-Authored-By: Claude Opus 5 <[email protected]>
Adds hw_classification to the three 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 many 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 passed to
instantiate_device_type_tests, so they are GENERIC.

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:

```
cd test
python test_dlpack.py --hw-classification GENERIC
python test_dlpack.py --hw-classification ACCELERATOR
python test_dlpack.py --hw-classification CUDA
python -m pytest test_dlpack.py -p no:randomly -v --tb=no -rs \
    --junitxml=dlpack.xml
```

310 tests, 234 passed / 74 skipped / 2 failed, and the per-test outcome map
is identical to the parent commit; setting a class attribute cannot change
what runs unfiltered.

The selectors partition the file exactly, with unclassified=0 on each:
GENERIC 30 and ACCELERATOR 280 sum to 310, and CUDA selects nothing.

The 74 skips are 48 cpu and 24 xpu clones of tests gated by
@OnlyOn(["xpu", "cuda"]) or @onlyCUDA, and 2 needing a newer NumPy.

Measured on an Intel Data Center GPU Max 1550 box with two XPU devices and
no CUDA or MPS device, so the cuda and mps bases are never generated here.
The local torch build predates pytorch#178768, which the base
contains, so test_copy_xpu and test_no_copy_xpu fail on both sides; CI
builds include the fix.

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

Co-Authored-By: Claude Opus 5 <[email protected]>
Review feedback on pytorch#195722: the eight 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, and the onlyOn import goes. One of them,
test_readonly_wrapper_bare_capsule_is_valid, came in with pytorch#195575 after
that review and follows the same rule. 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.
Part of pytorch#142029.

The same eight tests drop @skipMeta, which onlyAccelerator makes dead code
because it already skips cpu and meta. The docstrings and comments of
test_numpy_cross_device_transfer and test_numpy_cross_device_multi_gpu now
say accelerator instead of CUDA/XPU and cuda:0/cuda:1.

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 five are expected to pass on MPS from
reading the code: test_copy, test_no_copy, test_needs_copy_error and
test_numpy_cross_device_transfer through DLConvertor.cpp and
torch/utils/dlpack.py, and test_readonly_wrapper_bare_capsule_is_valid
because the MPS allocator hands out simple data pointers, so _lazy_clone
can wrap the storage as copy-on-write, and the read-only export reads
storage().data() without materializing it. That test is the first to run
_lazy_clone on MPS.

On CUDA and XPU nothing changes: onlyAccelerator only skips cpu and meta,
and skipMPS is a no-op off MPS. Out-of-tree backends that instantiate the
template, such as PrivateUse1 and HPU, pick these tests up as well.

Test Plan:

Per-test outcomes before and after, on an Intel Data Center GPU Max 1550
box with two XPU devices and no CUDA or MPS device:

```
cd test
python -m pytest test_dlpack.py -p no:randomly -v --tb=no -rs \
    --junitxml=dlpack.xml
```

310 tests, 234 passed / 74 skipped / 2 failed, with an identical outcome
map before and after. Only the skip reason of the 24 cpu instances changes,
from "Only runs on ['xpu', 'cuda']" to
"onlyAccelerator: doesn't run on CPU or meta devices". No meta instances
are generated, so dropping @skipMeta changes no outcome. The two failures on
both sides are test_copy_xpu and test_no_copy_xpu: the local torch build
predates pytorch#178768, which the base contains; CI builds include
the fix. 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]>
Review feedback on pytorch#195722: the stream tests in TestTorchDlPackDevice were
@onlyCUDA, so their XPU instances skipped as "Only runs on cuda" with
nothing pointing at the gap. The two that exchange a tensor across streams,
test_dlpack_conversion_with_diff_streams and its _narrow_precision variant,
now use the device-generic stream API (torch.Stream, `with stream:` and
Stream.native_handle) and the gating of their siblings
test_dlpack_conversion_with_streams*, @onlyAccelerator plus @skipMPS, and
skip on XPU against intel/torch-xpu-ops#5641. Part of pytorch#142029.

The other five stay @onlyCUDA, as on main: test_dlpack_default_stream,
test_dlpack_convert_default_stream, test_dlpack_cuda_per_thread_stream,
test_dlpack_invalid_rocm_streams and test_dlpack_invalid_cuda_streams.
They check the array API's CUDA and ROCm stream numbers (1 is CUDA's legacy
default stream and 0 ROCm's, 2 is CUDA's per-thread default stream, 0 is
invalid on CUDA and 1 and 2 on ROCm). SYCL has no such numbering, so these
need XPU counterparts rather than a port, and a pytorch#5641 skip on them could
never be lifted.

The gap is in Tensor.__dlpack__, which only honors the stream argument for
CUDA tensors. On XPU any integer is accepted and ignored, so no ordering is
set up between the producer's and the consumer's queue, and
torch.from_dlpack never passes a stream for a kDLOneAPI producer. On XPU
native_handle is the sycl::queue pointer, the value pytorch#5641 suggests a
consumer pass. With the skip removed, the XPU instances pass today, but
only because the stream is ignored: the test synchronizes both streams
before comparing, so it cannot observe the ordering on any backend, and a
pass on XPU is not evidence of the feature. The skip stays until pytorch#5641
defines what XPU accepts.

On CUDA and ROCm nothing changes: native_handle returns the same
cudaStream_t as torch.cuda.Stream.cuda_stream (both are
CUDAStream::stream(), in CUDAGuardImpl::getStreamNativeHandle and
torch/csrc/cuda/Stream.cpp),
torch.Stream() takes a stream from the same pool as torch.cuda.Stream(),
and skipXPUIf and skipMPS are no-ops there. MPS is excluded for the same
reason as the siblings: MPSGuardImpl does not implement synchronizeStream.

Test Plan:

```
python test/test_dlpack.py -v
```

Run on this commit and on its parent: 310 tests, 235 passed / 75 skipped /
0 failed on both, with an identical per-test outcome map. Only skip reasons
change, on 36 instances of the two diff_streams tests (12 + 6 dtypes): the
18 xpu instances go from "Only runs on cuda" to the issue URL, and the 18
cpu instances from "Only runs on cuda" to
"onlyAccelerator: doesn't run on CPU or meta devices". The five @onlyCUDA
tests skip the same way on both sides. The same file with the two
skipXPUIf lines removed, run with -k diff_streams, passes all 18 xpu
instances.

Measured on an Intel Arc B580 box with one XPU device and no CUDA or MPS
device, so the cuda and mps bases are never generated here, against a
2.16.0.dev20261016+xpu nightly torch.

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

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

Copy link
Copy Markdown
Collaborator

Successfully rebased dlpack-xpu-enablement onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout dlpack-xpu-enablement && git pull --rebase)

@github-actions github-actions Bot 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.

Automated review of 974ed2f: nothing blocking; ready for human review.

The PR splits test/test_dlpack.py into device-independent TestTorchDlPack and per-device TestTorchDlPackDevice (using @ onlyAccelerator/@ skipMPS, dropping stale XPU skips, porting cross-stream tests to torch.Stream) and updates a docstring in test_cutedsl_smoketest.py. Recommendation: Needs Discussion etaf (maintainer) should confirm the author's split of keeping five stream tests @ onlyCUDA while two skip on XPU against intel/torch-xpu-ops# 5641; the author's question to etaf is still unanswered. Separately, the exact_device check in test_automatically_select_in_creation should be fixed.

⚪ 1 non-blocking finding

tensor = torch.rand(10, device=device)

The device check in test_automatically_select_in_creation is too weak. The PR now builds the source tensor on device (torch.rand(10, device=device)), so on accelerators it exercises the __dlpack__ path in torch.tensor(...) (torch/csrc/utils/tensor_new.cpp:343-358). But self.assertEqual(tensor, new_tensor) compares with exact_device=False by default (common_utils.py:5022), so it only checks values, not the device. If torch.tensor(wrapper) silently put the result on CPU instead of keeping the producer's device (tensor.device() at tensor_new.cpp:350), this assertion would still pass. Fix: use self.assertEqual(tensor, new_tensor, exact_device=True) or assert new_tensor.device == tensor.device.

This review is AI-generated and advisory. Each new automated review replaces the previous one.

@github-actions
github-actions Bot dismissed stale reviews from themself October 26, 2026 12:24

Withdrawn: the automated review of 974ed2f found nothing blocking.

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Oct 26, 2026
@tszulist-hbn

Copy link
Copy Markdown
Contributor Author

@pytorchmergebot 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

dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
## Linked issue or supporting maintainer

Part of pytorch#142029

## BC-breaking?

No

## Summary

This PR updates `test/test_dlpack.py` to reduce repeated device-specific test coverage and enable XPU coverage where tests are backend-agnostic.

## Agent details

### What changed

- Moved the device-independent tests out of the device template into a plain `TestTorchDlPack`, so each runs once instead of once per backend; the device-generic tests stay in `TestTorchDlPackDevice`.
- Switched the copy, NumPy cross-device, stream and read-only wrapper tests from `onlyCUDA` / `onlyOn(["xpu", "cuda"])` to `@onlyAccelerator`, with `@skipMPS` on the four stream tests MPS cannot run.
- Dropped the stale XPU skips for intel/torch-xpu-ops#3074.
- Ported the two cross-stream exchange tests (`test_dlpack_conversion_with_diff_streams` and its narrow-precision variant) to `torch.Stream` and `Stream.native_handle`, and skipped them on XPU as a feature gap (intel/torch-xpu-ops#5641); the five that test the CUDA/ROCm stream numbers stay `@onlyCUDA`.
- Added hardware classification metadata to all test classes.
- Test-only change; no product code was modified.

### Test coverage delta

| Metric | Before | After |
|---|---:|---:|
| XPU tests collected | 161 | 140 |
| XPU passed / skipped | 117 / 44 | 115 / 25 |
| CPU tests collected | 161 | 140 |
| CPU passed / skipped | 113 / 48 | 92 / 48 |
| Device-independent tests collected | 9 | 30 |
| Device-independent passed / skipped | 7 / 2 | 28 / 2 |
| Total tests collected | 331 | 310 |
| Total passed / skipped | 237 / 94 | 235 / 75 |

No tests failed. The 21 tests moved to `TestTorchDlPack` now run once instead of once per device, which accounts for the drop in collected tests. On XPU, `test_copy`, `test_needs_copy_error`, `test_dlpack_exchange_api` and `test_dlpack_exchange_api_sliced` now pass, and the 18 XPU skips of the two cross-stream exchange tests point at intel/torch-xpu-ops#5641 instead of `onlyCUDA`.
Pull Request resolved: pytorch#195722
Approved by: https://github.com/pbielak, https://github.com/etaf, https://github.com/jansel

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ci-no-td Do not run TD on this PR ciflow/b200 ciflow/h100 ciflow/trunk Trigger trunk jobs on your pull request ciflow/xpu Run XPU CI tasks Merged open source ready for review Automated review passed, ready for a final review by a maintainer. topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

8 participants