11# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22# SPDX-License-Identifier: Apache-2.0
33
4+ import functools
45import multiprocessing
56import os
67import 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+
94159def 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
196261def 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
211266def _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