Skip to content

Commit 7028804

Browse files
rparolincpcloudleofangpre-commit-ci[bot]
authored
Skip IPC mempool tests on WSL (#1045)
* Skip IPC mempool tests on WSL * Update cuda_core/tests/conftest.py Co-authored-by: Phillip Cloud <417981+cpcloud@users.noreply.github.com> * Update cuda_core/tests/test_ipc_mempool.py Co-authored-by: Phillip Cloud <417981+cpcloud@users.noreply.github.com> * Apply suggestion from @cpcloud Co-authored-by: Phillip Cloud <417981+cpcloud@users.noreply.github.com> * addressing feedback * Making utils globally accessible * working * wip * wip * formatting * removing deleted files * removing skip helper * wip * feedback * wip * sorting imports * Update cuda_core/tests/conftest.py Co-authored-by: Leo Fang <leof@nvidia.com> * Checking if CU_DEVICE_ATTRIBUTE_MEMPOOL_SUPPORTED_HANDLE_TYPES is supported to skip IPC tests on WSL * Update cuda_python_test_helpers/__init__.py Co-authored-by: Leo Fang <leof@nvidia.com> * wip * [pre-commit.ci] auto code formatting * Creating the cuda_python_test_helpers as a package * [pre-commit.ci] auto code formatting * pre-commit changes * removing cuda_python_test_helpers/__init__.py * install cuda_python_test_helpers as editable * do not require cuda_python_test_helpers to be pre-installed for now * Revert "install cuda_python_test_helpers as editable" This reverts commit baac405. --------- Co-authored-by: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Co-authored-by: Leo Fang <leof@nvidia.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 7335c05 commit 7028804

File tree

10 files changed

+155
-10
lines changed

10 files changed

+155
-10
lines changed

cuda_core/tests/conftest.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4+
import multiprocessing
5+
46
import helpers
7+
import pytest
58

69
try:
710
from cuda.bindings import driver
811
except ImportError:
912
from cuda import cuda as driver
10-
import multiprocessing
1113

12-
import pytest
1314
from cuda.core.experimental import Device, DeviceMemoryResource, DeviceMemoryResourceOptions, _device
1415
from cuda.core.experimental._utils.cuda_utils import handle_return
1516

@@ -85,6 +86,10 @@ def ipc_device():
8586
if not device.properties.handle_type_posix_file_descriptor_supported:
8687
pytest.skip("Device does not support IPC")
8788

89+
# Skip on WSL or if driver rejects IPC-enabled mempool creation on this platform/device
90+
if helpers.IS_WSL or not helpers.supports_ipc_mempool(device):
91+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
92+
8893
return device
8994

9095

cuda_core/tests/helpers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import os
5+
import pathlib
6+
import sys
57

68
CUDA_PATH = os.environ.get("CUDA_PATH")
79
CUDA_INCLUDE_PATH = None
@@ -14,3 +16,18 @@
1416
path = os.path.join(path, "cccl")
1517
if os.path.isdir(path):
1618
CCCL_INCLUDE_PATHS = (path,) + CCCL_INCLUDE_PATHS
19+
20+
21+
try:
22+
import cuda_python_test_helpers
23+
except ImportError:
24+
# Import shared platform helpers for tests across repos
25+
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2] / "cuda_python_test_helpers"))
26+
import cuda_python_test_helpers
27+
28+
29+
IS_WSL = cuda_python_test_helpers.IS_WSL
30+
supports_ipc_mempool = cuda_python_test_helpers.supports_ipc_mempool
31+
32+
33+
del cuda_python_test_helpers

cuda_core/tests/memory_ipc/test_errors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from cuda.core.experimental import Buffer, Device, DeviceMemoryResource, DeviceMemoryResourceOptions
99
from cuda.core.experimental._utils.cuda_utils import CUDAError
1010

11+
from cuda_python_test_helpers import supports_ipc_mempool
12+
1113
CHILD_TIMEOUT_SEC = 20
1214
NBYTES = 64
1315
POOL_SIZE = 2097152
@@ -18,6 +20,10 @@ class ChildErrorHarness:
1820
PARENT_ACTION, CHILD_ACTION, and ASSERT (see below for examples)."""
1921

2022
def test_main(self, ipc_device, ipc_memory_resource):
23+
if not supports_ipc_mempool(ipc_device):
24+
import pytest
25+
26+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
2127
"""Parent process that checks child errors."""
2228
# Attach fixtures to this object for convenience. These can be accessed
2329
# from PARENT_ACTION.

cuda_core/tests/memory_ipc/test_memory_ipc.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from cuda.core.experimental import Buffer, DeviceMemoryResource
77
from utility import IPCBufferTestHelper
88

9+
from cuda_python_test_helpers import supports_ipc_mempool
10+
911
CHILD_TIMEOUT_SEC = 20
1012
NBYTES = 64
1113
NWORKERS = 2
@@ -14,6 +16,10 @@
1416

1517
class TestIpcMempool:
1618
def test_main(self, ipc_device, ipc_memory_resource):
19+
if not supports_ipc_mempool(ipc_device):
20+
import pytest
21+
22+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
1723
"""Test IPC with memory pools."""
1824
# Set up the IPC-enabled memory pool and share it.
1925
device = ipc_device
@@ -51,6 +57,10 @@ def child_main(self, device, mr, queue):
5157

5258
class TestIPCMempoolMultiple:
5359
def test_main(self, ipc_device, ipc_memory_resource):
60+
if not supports_ipc_mempool(ipc_device):
61+
import pytest
62+
63+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
5464
"""Test IPC with memory pools using multiple processes."""
5565
# Construct an IPC-enabled memory resource and share it with two children.
5666
device = ipc_device
@@ -99,6 +109,10 @@ def child_main(self, device, mr, idx, queue):
99109

100110
class TestIPCSharedAllocationHandleAndBufferDescriptors:
101111
def test_main(self, ipc_device, ipc_memory_resource):
112+
if not supports_ipc_mempool(ipc_device):
113+
import pytest
114+
115+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
102116
"""
103117
Demonstrate that a memory pool allocation handle can be reused for IPC
104118
with multiple processes. Uses buffer descriptors.

cuda_core/tests/memory_ipc/test_send_buffers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from cuda.core.experimental import DeviceMemoryResource, DeviceMemoryResourceOptions
99
from utility import IPCBufferTestHelper
1010

11+
from cuda_python_test_helpers import supports_ipc_mempool
12+
1113
CHILD_TIMEOUT_SEC = 20
1214
NBYTES = 64
1315
NMRS = 3
@@ -18,6 +20,8 @@
1820
@pytest.mark.parametrize("nmrs", (1, NMRS))
1921
def test_ipc_send_buffers(ipc_device, nmrs):
2022
"""Test passing buffers sourced from multiple memory resources."""
23+
if not supports_ipc_mempool(ipc_device):
24+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
2125
# Set up several IPC-enabled memory pools.
2226
device = ipc_device
2327
options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True)

cuda_core/tests/memory_ipc/test_workerpool.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from cuda.core.experimental import Buffer, Device, DeviceMemoryResource, DeviceMemoryResourceOptions
1010
from utility import IPCBufferTestHelper
1111

12+
from cuda_python_test_helpers import supports_ipc_mempool
13+
1214
CHILD_TIMEOUT_SEC = 20
1315
NBYTES = 64
1416
NWORKERS = 2
@@ -28,6 +30,8 @@ class TestIpcWorkerPool:
2830

2931
@pytest.mark.parametrize("nmrs", (1, NMRS))
3032
def test_main(self, ipc_device, nmrs):
33+
if not supports_ipc_mempool(ipc_device):
34+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
3135
device = ipc_device
3236
options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True)
3337
mrs = [DeviceMemoryResource(device, options=options) for _ in range(nmrs)]
@@ -62,6 +66,8 @@ def init_worker(mrs):
6266

6367
@pytest.mark.parametrize("nmrs", (1, NMRS))
6468
def test_main(self, ipc_device, nmrs):
69+
if not supports_ipc_mempool(ipc_device):
70+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
6571
device = ipc_device
6672
options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True)
6773
mrs = [DeviceMemoryResource(device, options=options) for _ in range(nmrs)]
@@ -104,6 +110,8 @@ def init_worker(mrs):
104110

105111
@pytest.mark.parametrize("nmrs", (1, NMRS))
106112
def test_main(self, ipc_device, nmrs):
113+
if not supports_ipc_mempool(ipc_device):
114+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
107115
device = ipc_device
108116
options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True)
109117
mrs = [DeviceMemoryResource(device, options=options) for _ in range(nmrs)]

cuda_core/tests/test_event.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import os
5-
import pathlib
6-
import platform
75
import time
86

97
import cuda.core.experimental
@@ -22,10 +20,7 @@
2220
)
2321

2422
from conftest import skipif_need_cuda_headers
25-
26-
27-
def platform_is_wsl():
28-
return platform.system() == "Linux" and "microsoft" in pathlib.Path("/proc/version").read_text().lower()
23+
from cuda_python_test_helpers import IS_WSL
2924

3025

3126
def test_event_init_disabled():
@@ -47,7 +42,7 @@ def test_timing_success(init_cuda):
4742
# We only want to exercise the __sub__ method, this test is not meant
4843
# to stress-test the CUDA driver or time.sleep().
4944
delay_ms = delay_seconds * 1000
50-
if os.name == "nt" or platform_is_wsl(): # noqa: SIM108
45+
if os.name == "nt" or IS_WSL: # noqa: SIM108
5146
# For Python <=3.10, the Windows timer resolution is typically limited to 15.6 ms by default.
5247
generous_tolerance = 100
5348
else:

cuda_core/tests/test_memory.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from cuda.core.experimental._utils.cuda_utils import handle_return
2929
from cuda.core.experimental.utils import StridedMemoryView
3030

31+
from cuda_python_test_helpers import supports_ipc_mempool
32+
3133
POOL_SIZE = 2097152 # 2MB size
3234

3335

@@ -529,6 +531,9 @@ def test_mempool_attributes(ipc_enabled, mempool_device, property_name, expected
529531
if platform.system() == "Windows":
530532
return # IPC not implemented for Windows
531533

534+
if ipc_enabled and not supports_ipc_mempool(device):
535+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
536+
532537
options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=ipc_enabled)
533538
mr = DeviceMemoryResource(device, options=options)
534539
assert mr.is_ipc_enabled == ipc_enabled
@@ -567,6 +572,10 @@ def test_mempool_attributes(ipc_enabled, mempool_device, property_name, expected
567572
def test_mempool_attributes_ownership(mempool_device):
568573
"""Ensure the attributes bundle handles references correctly."""
569574
device = mempool_device
575+
# Skip if IPC mempool is not supported on this platform/device
576+
if not supports_ipc_mempool(device):
577+
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
578+
570579
mr = DeviceMemoryResource(device, dict(max_size=POOL_SIZE))
571580
attributes = mr.attributes
572581
mr.close()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import functools
5+
import os
6+
from contextlib import suppress
7+
from typing import Union
8+
9+
from cuda.core.experimental._utils.cuda_utils import handle_return
10+
11+
12+
def _detect_wsl() -> bool:
13+
data = ""
14+
with suppress(Exception), open("/proc/sys/kernel/osrelease") as f:
15+
data = f.read().lower()
16+
if "microsoft" in data or "wsl" in data:
17+
return True
18+
return any(os.environ.get(k) for k in ("WSL_DISTRO_NAME", "WSL_INTEROP"))
19+
20+
21+
IS_WSL: bool = _detect_wsl()
22+
23+
24+
@functools.cache
25+
def supports_ipc_mempool(device_id: Union[int, object]) -> bool:
26+
"""Return True if mempool IPC via POSIX file descriptor is supported.
27+
28+
Uses cuDeviceGetAttribute(CU_DEVICE_ATTRIBUTE_MEMPOOL_SUPPORTED_HANDLE_TYPES)
29+
to check for CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR support. Does not
30+
require an active CUDA context.
31+
"""
32+
if _detect_wsl():
33+
return False
34+
35+
try:
36+
# Lazy import to avoid hard dependency when not running GPU tests
37+
try:
38+
from cuda.bindings import driver # type: ignore
39+
except Exception:
40+
from cuda import cuda as driver # type: ignore
41+
42+
# Initialize CUDA
43+
handle_return(driver.cuInit(0))
44+
45+
# Resolve device id from int or Device-like object
46+
dev_id = int(getattr(device_id, "device_id", device_id))
47+
48+
# Query supported mempool handle types bitmask
49+
attr = driver.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_MEMPOOL_SUPPORTED_HANDLE_TYPES
50+
mask = handle_return(driver.cuDeviceGetAttribute(attr, dev_id))
51+
52+
# Check POSIX FD handle type support via bitmask
53+
posix_fd = driver.CUmemAllocationHandleType.CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR
54+
return (int(mask) & int(posix_fd)) != 0
55+
except Exception:
56+
return False
57+
58+
59+
__all__ = [
60+
"IS_WSL",
61+
"supports_ipc_mempool",
62+
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
[build-system]
5+
requires = ["setuptools>=77.0.0"]
6+
build-backend = "setuptools.build_meta"
7+
8+
[project]
9+
name = "cuda-python-test-helpers"
10+
version = "0.1.0"
11+
description = "Shared test helpers for CUDA Python projects"
12+
readme = {file = "README.md", content-type = "text/markdown"}
13+
authors = [{ name = "NVIDIA Corporation" }]
14+
license = "Apache-2.0"
15+
requires-python = ">=3.9"
16+
classifiers = [
17+
"Programming Language :: Python :: 3 :: Only",
18+
"Operating System :: POSIX :: Linux",
19+
]
20+
21+
[tool.setuptools]
22+
packages = ["cuda_python_test_helpers"]
23+
24+
[project.urls]
25+
repository = "https://github.com/NVIDIA/cuda-python"

0 commit comments

Comments
 (0)