Skip to content

Commit 2d94fc4

Browse files
committed
TST: add mini parallel plugins in test conftests
Add lightweight pytest plugins in bindings/core conftest files to wrap parallel worker tests with CUDA context setup and fixture-aware initialization paths used by mini plugin execution.
1 parent a9156b6 commit 2d94fc4

2 files changed

Lines changed: 145 additions & 21 deletions

File tree

‎cuda_bindings/tests/conftest.py‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
33

4+
import functools
5+
import inspect
46
import pathlib
57
import sys
8+
from contextlib import contextmanager
69
from importlib.metadata import PackageNotFoundError, distribution
710

811
import pytest
@@ -25,6 +28,74 @@
2528
sys.path.insert(0, test_helpers_root)
2629

2730

31+
def pytest_configure(config):
32+
# When using `parallel-threads` set up mini-plugin to ensure each thread has a CUDA context
33+
parallel_threads = getattr(config.option, "parallel_threads", 0)
34+
if parallel_threads == "auto" or int(parallel_threads) > 1:
35+
config.pluginmanager.register(_CudaBindingsParallelPlugin(), name="_cuda_bindings_parallel_plugin")
36+
37+
38+
@contextmanager
39+
def _thread_context():
40+
# Context setting up `device` and `ctx` for individual threads on
41+
# pytest-run-parallel
42+
err, device = cuda.cuDeviceGet(0)
43+
assert err == cuda.CUresult.CUDA_SUCCESS
44+
err, ctx = cuda.cuCtxCreate(None, 0, device)
45+
assert err == cuda.CUresult.CUDA_SUCCESS
46+
try:
47+
yield device, ctx
48+
finally:
49+
(err,) = cuda.cuCtxDestroy(ctx)
50+
assert err == cuda.CUresult.CUDA_SUCCESS
51+
52+
53+
def _wrap_worker_cuda_test(func):
54+
if getattr(func, "_cuda_bindings_worker_cuda_wrapped", False):
55+
return func
56+
57+
sig = inspect.signature(func)
58+
wants_device = "device" in sig.parameters
59+
wants_ctx = "ctx" in sig.parameters
60+
61+
@functools.wraps(func)
62+
def wrapper(*args, **kwargs):
63+
with _thread_context() as (device, ctx):
64+
# device is None when reusing an existing context (defensive path);
65+
# keep whatever the fixture provided in kwargs as-is.
66+
if wants_device and device is not None:
67+
kwargs["device"] = device
68+
if wants_ctx:
69+
kwargs["ctx"] = ctx
70+
return func(*args, **kwargs)
71+
72+
wrapper._cuda_bindings_worker_cuda_wrapped = True
73+
return wrapper
74+
75+
76+
def _item_needs_thread_ctx(item):
77+
fixturenames = getattr(item, "fixturenames", ())
78+
# The 'device' fixture is the main fixture to set up a CUDA context.
79+
# 'driver' is specific to the cufile tests and used there instead.
80+
return "device" in fixturenames or "driver" in fixturenames
81+
82+
83+
class _CudaBindingsParallelPlugin:
84+
"""A mini pytest plugin used only for pytest-run-parallel testing.
85+
pytest-run-parallel spawns new threads for each test and we need to
86+
initialize and pass the correct CUDA context for each these.
87+
88+
This plugin looks for context specific fixtures and replaces them
89+
new context specific fixtures may have to be added.
90+
"""
91+
92+
@pytest.hookimpl()
93+
def pytest_collection_modifyitems(self, config, items):
94+
for item in items:
95+
if _item_needs_thread_ctx(item):
96+
item.obj = _wrap_worker_cuda_test(item.obj)
97+
98+
2899
@pytest.fixture(scope="module")
29100
def cuda_driver():
30101
(err,) = cuda.cuInit(0)

‎cuda_core/tests/conftest.py‎

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4+
import functools
45
import multiprocessing
56
import os
67
import pathlib
@@ -91,6 +92,70 @@ def xfail_if_mempool_oom(err_or_exc, api_name=None, device=0):
9192
sys.path.insert(0, test_helpers_root)
9293

9394

95+
def pytest_configure(config):
96+
# When using `parallel-threads` set up mini-plugin to ensure each thread has a CUDA context
97+
parallel_threads = getattr(config.option, "parallel_threads", 0)
98+
if parallel_threads == "auto" or int(parallel_threads) > 1:
99+
config.pluginmanager.register(_CudaCoreParallelPlugin(), name="_cuda_core_parallel_plugin")
100+
101+
102+
@contextmanager
103+
def _init_cuda_context():
104+
# TODO: rename this to e.g. init_context
105+
device = Device(0)
106+
device.set_current()
107+
108+
# Set option to avoid spin-waiting on synchronization.
109+
if int(os.environ.get("CUDA_CORE_TEST_BLOCKING_SYNC", 0)) != 0:
110+
handle_return(
111+
driver.cuDevicePrimaryCtxSetFlags(device.device_id, driver.CUctx_flags.CU_CTX_SCHED_BLOCKING_SYNC)
112+
)
113+
114+
try:
115+
yield device
116+
finally:
117+
_ = _device_unset_current()
118+
119+
120+
def _wrap_worker_cuda_test(func):
121+
if getattr(func, "_cuda_core_worker_cuda_wrapped", False):
122+
return func
123+
124+
@functools.wraps(func)
125+
def wrapper(*args, **kwargs):
126+
with _init_cuda_context() as device:
127+
if "init_cuda" in kwargs:
128+
kwargs["init_cuda"] = device
129+
if "mempool_device_x2" in kwargs:
130+
kwargs["mempool_device_x2"] = _mempool_device_impl(2)
131+
if "mempool_device_x3" in kwargs:
132+
kwargs["mempool_device_x3"] = _mempool_device_impl(3)
133+
return func(*args, **kwargs)
134+
135+
wrapper._cuda_core_worker_cuda_wrapped = True
136+
return wrapper
137+
138+
139+
def _item_uses_init_cuda(item):
140+
return "init_cuda" in getattr(item, "fixturenames", ())
141+
142+
143+
class _CudaCoreParallelPlugin:
144+
"""A mini pytest plugin used only for pytest-run-parallel testing.
145+
pytest-run-parallel spawns new threads for each test and we need to
146+
initialize and pass the correct CUDA context for each these.
147+
148+
This plugin looks for context specific fixtures and replaces them
149+
new context specific fixtures may have to be added.
150+
"""
151+
152+
@pytest.hookimpl(tryfirst=True)
153+
def pytest_collection_modifyitems(self, config, items):
154+
for item in items:
155+
if _item_uses_init_cuda(item):
156+
item.obj = _wrap_worker_cuda_test(item.obj)
157+
158+
94159
def skip_if_pinned_memory_unsupported(device):
95160
try:
96161
if not device.properties.host_memory_pools_supported:
@@ -194,18 +259,8 @@ def session_setup():
194259

195260
@pytest.fixture
196261
def init_cuda():
197-
# TODO: rename this to e.g. init_context
198-
device = Device(0)
199-
device.set_current()
200-
201-
# Set option to avoid spin-waiting on synchronization.
202-
if int(os.environ.get("CUDA_CORE_TEST_BLOCKING_SYNC", 0)) != 0:
203-
handle_return(
204-
driver.cuDevicePrimaryCtxSetFlags(device.device_id, driver.CUctx_flags.CU_CTX_SCHED_BLOCKING_SYNC)
205-
)
206-
207-
yield device
208-
_ = _device_unset_current()
262+
with _init_cuda_context() as device:
263+
yield device
209264

210265

211266
def _device_unset_current() -> bool:
@@ -247,7 +302,7 @@ def pop_all_contexts():
247302

248303

249304
@pytest.fixture
250-
def ipc_device():
305+
def ipc_device(init_cuda):
251306
"""Obtains a device suitable for IPC-enabled mempool tests, or skips.
252307
253308
The fixture also tracks every ``multiprocessing.Process`` spawned during
@@ -257,8 +312,7 @@ def ipc_device():
257312
"""
258313
from helpers.child_processes import track_child_processes
259314

260-
device = Device(0)
261-
device.set_current()
315+
device = init_cuda
262316

263317
if not device.properties.memory_pools_supported:
264318
pytest.skip("Device does not support mempool operations")
@@ -293,10 +347,9 @@ def ipc_memory_resource(request, ipc_device):
293347

294348

295349
@pytest.fixture
296-
def mempool_device():
350+
def mempool_device(init_cuda):
297351
"""Obtains a device suitable for mempool tests, or skips."""
298-
device = Device(0)
299-
device.set_current()
352+
device = init_cuda
300353

301354
if not device.properties.memory_pools_supported:
302355
pytest.skip("Device does not support mempool operations")
@@ -323,13 +376,13 @@ def _mempool_device_impl(num):
323376

324377

325378
@pytest.fixture
326-
def mempool_device_x2():
379+
def mempool_device_x2(init_cuda):
327380
"""Fixture that provides two devices if available, otherwise skips test."""
328381
return _mempool_device_impl(2)
329382

330383

331384
@pytest.fixture
332-
def mempool_device_x3():
385+
def mempool_device_x3(init_cuda):
333386
"""Fixture that provides three devices if available, otherwise skips test."""
334387
return _mempool_device_impl(3)
335388

0 commit comments

Comments
 (0)