From 451f96dd09f7b17173a6827c4d7fe21a38582f24 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Fri, 4 Dec 2026 13:04:21 -0800 Subject: [PATCH 1/2] skip SPIR-V kernel bundle tests on other backends Tests that loaded SPIR-V on the default queue failed when it was a CUDA queue. They now get it from get_spirv_queue_or_skip, which skips unless the backend is OpenCL or Level Zero. --- dpctl/tests/helper/__init__.py | 2 ++ dpctl/tests/helper/_helper.py | 13 +++++++++++++ dpctl/tests/test_sycl_compiler.py | 17 +++++------------ dpctl/tests/test_sycl_kernel_submit.py | 12 ++++-------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/dpctl/tests/helper/__init__.py b/dpctl/tests/helper/__init__.py index d776ddab50..ff1c39644b 100644 --- a/dpctl/tests/helper/__init__.py +++ b/dpctl/tests/helper/__init__.py @@ -19,6 +19,7 @@ from ._helper import ( create_invalid_capsule, get_queue_or_skip, + get_spirv_queue_or_skip, get_sub_device_or_skip, has_cpu, has_gpu, @@ -34,6 +35,7 @@ "has_gpu", "has_sycl_platforms", "get_queue_or_skip", + "get_spirv_queue_or_skip", "get_sub_device_or_skip", "is_adaptivecpp", "is_wsl_or_windows", diff --git a/dpctl/tests/helper/_helper.py b/dpctl/tests/helper/_helper.py index ed85530a79..8c742e9b55 100644 --- a/dpctl/tests/helper/_helper.py +++ b/dpctl/tests/helper/_helper.py @@ -62,6 +62,19 @@ def get_queue_or_skip(args=()): return q +def get_spirv_queue_or_skip(): + """Returns a default queue that accepts SPIR-V kernel bundles, or skips + the test. + """ + q = get_queue_or_skip() + if q.backend not in ( + dpctl.backend_type.opencl, + dpctl.backend_type.level_zero, + ): + pytest.skip(f"SPIR-V kernel bundles are not supported on {q.backend}") + return q + + def get_sub_device_or_skip(): for dev in dpctl.get_devices(): for partition in ("next_partitionable", [1, 1]): diff --git a/dpctl/tests/test_sycl_compiler.py b/dpctl/tests/test_sycl_compiler.py index 99d257104f..2591d62eff 100644 --- a/dpctl/tests/test_sycl_compiler.py +++ b/dpctl/tests/test_sycl_compiler.py @@ -26,6 +26,8 @@ import dpctl.memory as dpm from dpctl.compiler.utils import parse_spirv_specializations +from .helper import get_spirv_queue_or_skip + def _get_opencl_queue_or_skip(): try: @@ -234,10 +236,7 @@ def test_create_kernel_bundle_from_source_ocl(): @pytest.mark.unsupported_on_acpp def test_get_sycl_kernel_undefined_name(): - try: - q = dpctl.SyclQueue() - except dpctl.SyclQueueCreationError: - pytest.skip("Could not create default queue") + q = get_spirv_queue_or_skip() spirv_file = get_spirv_abspath("multi_kernel.spv") with open(spirv_file, "rb") as fin: spirv = fin.read() @@ -297,10 +296,7 @@ def test_create_kernel_bundle_from_invalid_src_ocl(): @pytest.mark.unsupported_on_acpp def test_create_kernel_bundle_with_spec_const(): - try: - q = dpctl.SyclQueue() - except dpctl.SyclQueueCreationError: - pytest.skip("Could not create default queue") + q = get_spirv_queue_or_skip() spec_id = 0 sp = dpc.SpecializationConstant(spec_id, "i4", 42) @@ -332,10 +328,7 @@ def test_create_kernel_bundle_with_spec_const(): @pytest.mark.unsupported_on_acpp def test_create_kernel_bundle_with_composite_spec_const(): - try: - q = dpctl.SyclQueue() - except dpctl.SyclQueueCreationError: - pytest.skip("Could not create default queue") + q = get_spirv_queue_or_skip() # composite specialization constants are separated into individual # specialization constants with unique spec_ids diff --git a/dpctl/tests/test_sycl_kernel_submit.py b/dpctl/tests/test_sycl_kernel_submit.py index 28c9af8ac0..2776663d57 100644 --- a/dpctl/tests/test_sycl_kernel_submit.py +++ b/dpctl/tests/test_sycl_kernel_submit.py @@ -27,6 +27,8 @@ import dpctl.memory as dpm from dpctl._sycl_queue import kernel_arg_type +from .helper import get_spirv_queue_or_skip + @pytest.mark.parametrize( "ctype_str,dtype,ctypes_ctor", @@ -459,10 +461,7 @@ def test_submit_wrong_number_of_args_sycl_source(backend): @pytest.mark.unsupported_on_acpp def test_submit_wrong_number_of_args(): - try: - q = dpctl.SyclQueue() - except dpctl.SyclQueueCreationError: - pytest.skip("Could not create default queue") + q = get_spirv_queue_or_skip() fn = get_spirv_abspath("multi_kernel.spv") with open(fn, "br") as f: spirv_bytes = f.read() @@ -505,10 +504,7 @@ def test_submit_wrong_number_of_args(): # int main() { return 0; } @pytest.mark.unsupported_on_acpp def test_submit_local_accessor_arg_spirv_from_sycl(): - try: - q = dpctl.SyclQueue() - except dpctl.SyclQueueCreationError: - pytest.skip("Could not create default queue") + q = get_spirv_queue_or_skip() fn = get_spirv_abspath("local-accessor-free-function-kernel.spv") with open(fn, "br") as f: spirv_bytes = f.read() From b86d4913a2608c1a0b67299f9c60d85990a7ec42 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Fri, 4 Dec 2026 13:04:25 -0800 Subject: [PATCH 2/2] test opening IPC handles from another process CUDA cannot open an IPC handle in the process that exported it, so the tests that do skip there. New tests open a handle exported by a child process, which works on CUDA and on Level Zero. --- dpctl/tests/test_sycl_ipc_memory.py | 98 +++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 11 deletions(-) diff --git a/dpctl/tests/test_sycl_ipc_memory.py b/dpctl/tests/test_sycl_ipc_memory.py index 773022ac4f..0e5f8347a4 100644 --- a/dpctl/tests/test_sycl_ipc_memory.py +++ b/dpctl/tests/test_sycl_ipc_memory.py @@ -17,6 +17,8 @@ """Tests for IPC memory handle functionality.""" import gc +import subprocess +import sys import pytest @@ -57,6 +59,14 @@ def _get_ipc_queue(): return dpctl.SyclQueue(dev) +def _get_local_open_ipc_queue(): + """Return an IPC queue whose handles this process can open itself.""" + q = _get_ipc_queue() + if q.backend == dpctl.backend_type.cuda: + pytest.skip("CUDA cannot open an IPC handle in the exporting process") + return q + + # ─── Handle creation tests ───────────────────────────────────────────── @@ -88,7 +98,7 @@ def test_get_handle_rejects_host_memory(self): class TestIPCOpenClose: def test_open_returns_ipc_device_memory(self): - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -99,7 +109,7 @@ def test_open_returns_ipc_device_memory(self): assert mapped.nbytes == 4096 def test_close_mapping_nulls_pointer(self): - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -112,7 +122,7 @@ def test_close_mapping_nulls_pointer(self): assert mapped.nbytes == 0 def test_close_mapping_idempotent(self): - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -151,7 +161,7 @@ def test_host_memory_is_not_ipc(self): assert not isinstance(mem, MemoryIPCDevice) def test_ipc_mapped_memory_is_ipc_type(self): - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -160,7 +170,7 @@ def test_ipc_mapped_memory_is_ipc_type(self): assert isinstance(mapped, MemoryIPCDevice) def test_type_persists_after_close_mapping(self): - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -170,7 +180,7 @@ def test_type_persists_after_close_mapping(self): assert isinstance(mapped, MemoryIPCDevice) def test_is_closed_property(self): - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -188,7 +198,7 @@ def test_is_closed_property(self): class TestIPCLifetime: def test_source_memory_outlives_mapped(self): """Source memory must stay alive while receiver uses the mapping.""" - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -204,7 +214,7 @@ def test_source_memory_outlives_mapped(self): def test_del_mapped_memory_does_not_crash(self): """Deleting IPC-mapped memory should call CloseHandle, not sycl::free, and not crash.""" - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -219,7 +229,7 @@ def test_del_mapped_memory_does_not_crash(self): def test_del_mapped_memory_after_explicit_close(self): """If close_mapping was already called, __dealloc__ should be a no-op (ptr is NULL).""" - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -234,7 +244,7 @@ def test_del_mapped_memory_after_explicit_close(self): def test_multiple_opens_from_same_handle(self): """Each open() produces an independent mapping that must be closed independently.""" - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(4096, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -252,7 +262,7 @@ def test_multiple_opens_from_same_handle(self): def test_del_mapped_via_gc(self): """Full lifecycle: get handle, open, use, let GC collect mapped.""" - q = _get_ipc_queue() + q = _get_local_open_ipc_queue() dev = q.sycl_device mem = MemoryUSMDevice(2048, queue=q) handle_bytes = SyclIPCGetMemHandle(mem) @@ -267,3 +277,69 @@ def test_del_mapped_via_gc(self): # Source is still valid after receiver is gone assert mem.nbytes == 2048 + + +# ─── Cross-process tests ────────────────────────────────────────────── + +# The child exports and the parent opens: Level Zero handles carry a file +# descriptor that only a process allowed to ptrace the exporter can import. +_EXPORTER_SRC = """ +import sys + +import dpctl +from dpctl.memory import MemoryUSMDevice, SyclIPCGetMemHandle + +nbytes = int(sys.argv[2]) +mem = MemoryUSMDevice(nbytes, queue=dpctl.SyclQueue(sys.argv[1])) +mem.copy_from_host(bytes(i % 256 for i in range(nbytes))) +print(SyclIPCGetMemHandle(mem).hex(), flush=True) +sys.stdin.readline() +assert bytes(mem.copy_to_host()) == bytes(255 - i % 256 for i in range(nbytes)) +""" + + +class TestIPCAcrossProcesses: + @pytest.mark.parametrize("close", ["explicit", "dealloc"]) + def test_open_handle_from_another_process(self, close, tmp_path): + dev = _get_ipc_device() + nbytes = 4096 + with ( + open(tmp_path / "stderr", "w+") as err, + subprocess.Popen( + [ + sys.executable, + "-c", + _EXPORTER_SRC, + dev.filter_string, + str(nbytes), + ], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=err, + text=True, + ) as child, + ): + try: + handle_hex = child.stdout.readline().strip() + assert handle_hex, "exporter exited without a handle" + mapped = SyclIPCOpenMemHandle( + bytes.fromhex(handle_hex), dev, nbytes + ) + assert isinstance(mapped, MemoryIPCDevice) + assert mapped.nbytes == nbytes + assert bytes(mapped.copy_to_host()) == bytes( + i % 256 for i in range(nbytes) + ) + mapped.copy_from_host( + bytes(255 - i % 256 for i in range(nbytes)) + ) + if close == "explicit": + SyclIPCCloseMemHandle(mapped) + assert mapped.is_closed + else: + del mapped + gc.collect() + finally: + child.communicate("\n", timeout=120) + err.seek(0) + assert child.returncode == 0, err.read()