# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 import functools import gc import importlib import multiprocessing import os import pathlib import sys import warnings import pytest # BEGIN SYNCED PYTEST PLUGIN BOOTSTRAP # Keep every block with this marker byte-for-byte identical. # Before editing or reviewing, find all copies; see root AGENTS.md. # Keep the shared pytest plugin available in wheel-test jobs, which run from # the monorepo checkout without installing cuda-python-test-helpers. try: import cuda_python_test_helpers._pytest_plugin # noqa: F401 except ImportError as e: # Don't call .resolve(): resolving symlinks can make parents[2] point # somewhere other than the monorepo root if a sub-directory is symlinked. _test_helpers_root = pathlib.Path(__file__).parents[2] / "cuda_python_test_helpers" if not _test_helpers_root.is_dir(): raise RuntimeError(f"cuda-python-test-helpers not installed and not found at {_test_helpers_root}") from e for _k in list(sys.modules): if _k == "cuda_python_test_helpers" or _k.startswith("cuda_python_test_helpers."): del sys.modules[_k] sys.path.insert(0, str(_test_helpers_root)) importlib.invalidate_caches() pytest_plugins = ["cuda_python_test_helpers._pytest_plugin"] # END SYNCED PYTEST PLUGIN BOOTSTRAP from helpers.constants import POOL_SIZE from helpers.memory import ( BaselineMemoryResourceReleasedWarning, UnreleasedMemoryResourceWarning, skip_if_host_numa_pinned_memory_unsupported, ) import cuda.core from cuda.bindings import driver from cuda.core import ( Device, DeviceMemoryResource, DeviceMemoryResourceOptions, ManagedMemoryResource, ManagedMemoryResourceOptions, PinnedMemoryResource, PinnedMemoryResourceOptions, VirtualMemoryResource, VirtualMemoryResourceOptions, _device, ) from cuda.core._memory._buffer import _live_owned_mempool_count, _live_va_reservation_count from cuda.core._utils.cuda_utils import handle_return def pytest_collection_finish(session): # The per-test gc.collect() in init_cuda (below) must not re-traverse the # collected items and the imported modules on every teardown: freeze them # once. Objects created after this point are collected as before (#2381). gc.collect() gc.freeze() def pytest_configure(config): # When using `parallel-threads` set up mini-plugin to ensure each thread has a CUDA context parallel_threads = getattr(config.option, "parallel_threads", 0) if parallel_threads == "auto" or int(parallel_threads) > 1: config.pluginmanager.register(_CudaCoreParallelPlugin(), name="_cuda_core_parallel_plugin") @pytest.hookimpl(wrapper=True) def pytest_runtest_makereport(item, call): # Runs the OOM reason checker on the first CUDA OOM of a session; see # issue #2381 and helpers/oom_diagnostics.py for why this is latched and # what it checks (host VA exhaustion vs. physical device memory). report = yield from helpers import oom_diagnostics oom_diagnostics.record_if_oom(item, call, report) return report def pytest_terminal_summary(terminalreporter): from helpers import oom_diagnostics oom_diagnostics.report_terminal_summary(terminalreporter) def _wrap_worker_cuda_test(func): if getattr(func, "_cuda_core_worker_cuda_wrapped", False): return func @functools.wraps(func) def wrapper(*args, **kwargs): kwargs = dict(kwargs) # copy before mutating device = Device(0) device.set_current() try: if "init_cuda" in kwargs: kwargs["init_cuda"] = device if "mempool_device_x2" in kwargs: kwargs["mempool_device_x2"] = _mempool_device_impl(2) if "mempool_device_x3" in kwargs: kwargs["mempool_device_x3"] = _mempool_device_impl(3) if "device_x2" in kwargs: kwargs["device_x2"] = _device_x2_impl() # These are used by test_green_context.py. The original fixtures include # pytest.skip() but that should have correctly fired by this time. if "sm_resource" in kwargs: kwargs["sm_resource"] = device.resources.sm if "wq_resource" in kwargs: kwargs["wq_resource"] = device.resources.workqueue if "green_ctx" in kwargs: from cuda.core import ContextOptions, SMResourceOptions groups, _ = device.resources.sm.split(SMResourceOptions(count=None)) kwargs["green_ctx"] = device.create_context(ContextOptions(resources=[groups[0]])) return func(*args, **kwargs) finally: # Unlike the `init_cuda` fixture we do not synchronize here # to avoid doing so while other workers are still running. # (E.g. for stream capture). The fixture cleanup is still run # even with pytest-run-parallel after worker join. _ = _device_unset_current() wrapper._cuda_core_worker_cuda_wrapped = True return wrapper def _item_uses_init_cuda(item): return "init_cuda" in getattr(item, "fixturenames", ()) # init_cuda collects at teardown only if the process-wide count of live *owned* # driver resources (owned memory pools and VA reservations) differs from its # value at setup. The counts come from the C++ RAII layer, so a borrowed pool # reference (the device default pool) and MemoryResource subclasses that hold no # pool or reservation do not move them. That makes the gate sound only if no # older owned resource is released during a test (it could cancel a new leak), # so tests that construct owned resources must use init_cuda, and the leftover # check below reports owned resources that outlive a test without it. def _live_resource_counts(): """(owned memory pools, VA reservations) live process-wide, as a tuple. Only *owned* driver resources are counted (see the C++ accessors), so this excludes borrowed pool references and non-owning MemoryResource subclasses. """ return (_live_owned_mempool_count(), _live_va_reservation_count()) def _format_counts(counts): owned_pools, va_reservations = counts return f"owned_pools={owned_pools}, va_reservations={va_reservations}" # Counts after the last init_cuda test; None until one has run. _mr_expected_live = None # Counts at the start of the current item, before its fixtures set up. _mr_live_at_item_start = (0, 0) _mr_last_init_cuda_nodeid = None @pytest.hookimpl(wrapper=True) def pytest_runtest_protocol(item, nextitem): # Leftover check: compare with the counts after the previous init_cuda test, # not the session start, because they legitimately rise over a run (pools # owned by module- or class-scoped fixtures). global _mr_expected_live, _mr_live_at_item_start, _mr_last_init_cuda_nodeid _mr_live_at_item_start = _live_resource_counts() try: return (yield) finally: live = _live_resource_counts() if _item_uses_init_cuda(item): _mr_expected_live = live _mr_last_init_cuda_nodeid = item.nodeid elif _mr_expected_live is not None: # Only lower each component: a released resource must not hide a later leak. _mr_expected_live = tuple(min(a, b) for a, b in zip(_mr_expected_live, live)) class _CudaCoreParallelPlugin: """A mini pytest plugin used only for pytest-run-parallel testing. pytest-run-parallel spawns new threads for each test and we need to initialize and pass the correct CUDA context for each these. This plugin looks for context specific fixtures and replaces them new context specific fixtures may have to be added. This plugin approach is not ideal, it would be nicer to introduce hooks into pytest-run-parallel. Once that issue is closed this would be good to refactor: https://github.com/Quansight-Labs/pytest-run-parallel/issues/189 """ @pytest.hookimpl(tryfirst=True) def pytest_collection_modifyitems(self, config, items): for item in items: if _item_uses_init_cuda(item): item.obj = _wrap_worker_cuda_test(item.obj) def _require_ipc_mempool_devices(devices): """Return devices if they all support IPC-enabled mempools, otherwise skip.""" from helpers import supports_ipc_mempool from cuda_python_test_helpers import IS_WSL checked_devices = tuple(devices) if not all(device.properties.handle_type_posix_file_descriptor_supported for device in checked_devices): pytest.skip("Device does not support IPC") if IS_WSL or not all(supports_ipc_mempool(device) for device in checked_devices): pytest.skip("Driver rejects IPC-enabled mempool creation on this platform") return devices @pytest.fixture(scope="session", autouse=True) def session_setup(): # Always init CUDA. handle_return(driver.cuInit(0)) # Never fork processes. multiprocessing.set_start_method("spawn", force=True) @pytest.fixture def init_cuda(request): # TODO: rename this to e.g. init_context device = Device(0) # Everything that can raise after set_current() runs inside the try, so the # finally pops this context; the leftover warning below is escalated to an # error. A failed set_current() leaves nothing for the finally to undo. # live_at_setup stays None until the setup snapshot is recorded. device.set_current() live_at_setup = None primary_error = None try: # Set option to avoid spin-waiting on synchronization. if int(os.environ.get("CUDA_CORE_TEST_BLOCKING_SYNC", 0)) != 0: handle_return( driver.cuDevicePrimaryCtxSetFlags(device.device_id, driver.CUctx_flags.CU_CTX_SCHED_BLOCKING_SYNC) ) # Leftover check: growth since the previous init_cuda test means something # in between left an owned resource alive. Collect first to sweep a cyclic # leftover, and before the counts at setup are recorded so they are reflected there. if _mr_expected_live is not None and any( now > exp for now, exp in zip(_mr_live_at_item_start, _mr_expected_live) ): gc.collect() now_counts = _live_resource_counts() held = tuple(max(n - e, 0) for n, e in zip(now_counts, _mr_expected_live)) excess = tuple(s - e for s, e in zip(_mr_live_at_item_start, _mr_expected_live)) seed = getattr(request.config.option, "randomly_seed", None) seed_msg = f" (pytest-randomly seed: {seed})" if seed is not None else "" warnings.warn( f"Owned resources were left alive by tests that ran since {_mr_last_init_cuda_nodeid} " f"({_format_counts(excess)}; still alive after a collect: {_format_counts(held)}). " f"A test or fixture that constructs a memory resource must request init_cuda.{seed_msg}", UnreleasedMemoryResourceWarning, stacklevel=2, ) # Counts at setup. The device default pool is a borrowed reference and is # not counted, so no cached-default bookkeeping is needed. live_at_setup = _live_resource_counts() yield device except BaseException as exc: primary_error = exc raise finally: # pytest holds fixture values in item.funcargs (a pytest-internal dict) # until all teardown has run; we deliberately drop them so already # torn-down fixtures are not counted below. Revisit if pytest changes # how it retains fixture values. request.node.funcargs.clear() # Only an owned resource in a reference cycle outlives the test frame, so # collect only if the counts differ from their values at setup. A lower # count means an older resource was released, which could mask a leak # in this test: collect in that case too. Skip the comparison when setup # aborted before the snapshot (the leftover warning raises). live_after = _live_resource_counts() if live_at_setup is not None else None if live_after is not None and live_after != live_at_setup: gc.collect() # Force any pool/allocation whose only remaining reference was a local # in this test's frame to actually get destroyed now, then drain the # context so the stream-ordered frees that destruction enqueues retire # before the next test runs (issue #2381). gc.collect() above must run # first: cuCtxSynchronize alone cannot drain frees that were never # enqueued because their owning object had not been collected yet. # With pytest-run-parallel this runs after worker join. This also runs # when setup raises, so an escalated leftover warning cannot leave the # context current. # The test wrapper may already have popped the calling thread's # context; synchronize the fixture device's bound context explicitly. # Keep the first failure primary; cleanup still runs for interrupts. try: device.sync() except BaseException as exc: if primary_error is None: primary_error = exc raise primary_error.add_note(f"CUDA fixture synchronization failed: {type(exc).__name__}: {exc}") finally: try: _ = _device_unset_current() except BaseException as exc: if primary_error is None: raise primary_error.add_note(f"CUDA fixture context pop failed: {type(exc).__name__}: {exc}") # After the cleanup, so an escalated warning cannot skip it. if live_after is not None and any(now < snap for now, snap in zip(live_after, live_at_setup)): released = tuple(s - n for s, n in zip(live_at_setup, live_after)) warnings.warn( f"{request.node.nodeid} released owned resources that existed before it started " f"({_format_counts(released)}; counts {_format_counts(live_after)}, snapshot " f"{_format_counts(live_at_setup)}). A leak in this test could have been masked by " "that release. Find what held the released resource.", BaselineMemoryResourceReleasedWarning, stacklevel=2, ) def _device_unset_current() -> bool: """Pop current CUDA context. Returns True if context was popped, False it the stack was empty. """ ctx = handle_return(driver.cuCtxGetCurrent()) if int(ctx) == 0: # no active context, do nothing return False handle_return(driver.cuCtxPopCurrent()) if hasattr(_device._tls, "devices"): del _device._tls.devices return True @pytest.fixture def deinit_cuda(): # TODO: rename this to e.g. deinit_context yield _ = _device_unset_current() def _device_x2_impl(): devices = Device.get_all_devices() if len(devices) < 2: pytest.skip("Test requires at least 2 CUDA devices") return devices[:2] @pytest.fixture def device_x2(init_cuda): """Provide two CUDA devices, or skip when fewer are available. Depends on ``init_cuda`` so that, under pytest-run-parallel, the test is wrapped by ``_wrap_worker_cuda_test`` and the devices are re-fetched on the worker thread (Device objects are thread-local). """ return _device_x2_impl() @pytest.fixture def deinit_all_contexts_function(): def pop_all_contexts(): max_iters = 256 for _ in range(max_iters): if _device_unset_current(): # context was popped, continue until stack is empty continue # no active context, we are ready break else: raise RuntimeError(f"Number of iterations popping current CUDA contexts, exceded {max_iters}") return pop_all_contexts @pytest.fixture def ipc_device(init_cuda): """Obtains a device suitable for IPC-enabled mempool tests, or skips. The fixture also tracks every ``multiprocessing.Process`` spawned during the test and kills any survivors at teardown. This prevents a stuck child (e.g., compute-sanitizer wedged during IPC teardown -- see issue #2004) from blocking ``ipc_memory_resource``'s ``mr.close()`` for hours. """ from helpers.child_processes import track_child_processes device = init_cuda if not device.properties.memory_pools_supported: pytest.skip("Device does not support mempool operations") device = _require_ipc_mempool_devices((device,))[0] with track_child_processes(): yield device def _require_vmm_ipc_device(device): """Return ``device`` if it can export VMM allocations as POSIX file descriptors, otherwise skip.""" from cuda_python_test_helpers import IS_WINDOWS, IS_WSL props = device.properties if not props.virtual_memory_management_supported: pytest.skip("Virtual memory management is not supported on this device") if IS_WINDOWS or IS_WSL or not props.handle_type_posix_file_descriptor_supported: pytest.skip("Sharing virtual memory needs POSIX file descriptor handles") return device @pytest.fixture def vmm_ipc_device(init_cuda): """A device whose VirtualMemoryResource buffers can be shared between processes, or skip. Tracks spawned ``multiprocessing.Process`` children like :func:`ipc_device`. """ from helpers.child_processes import track_child_processes device = _require_vmm_ipc_device(init_cuda) with track_child_processes(): yield device @pytest.fixture def vmm_ipc_device_x2(device_x2): """Two devices whose VirtualMemoryResource buffers can be shared between processes, or skip.""" from helpers.child_processes import track_child_processes devices = tuple(_require_vmm_ipc_device(device) for device in device_x2) with track_child_processes(): yield devices _fabric_allocation_error = {} # device ordinal -> "" when a fabric allocation works there, else the status def _require_vmm_fabric_device(device): """Return a VMM device with fabric handles and an accessible IMEX channel, or skip.""" from cuda_python_test_helpers import IS_WINDOWS, IS_WSL if IS_WINDOWS or IS_WSL: pytest.skip("Sharing fabric handles requires Linux") if not device.properties.virtual_memory_management_supported: pytest.skip("Virtual memory management is not supported on this device") if not device.properties.handle_type_fabric_supported: pytest.skip("Fabric handles need an NVLink fabric and an accessible IMEX channel") # The attribute does not check for an NVLink fabric, and older drivers do not check for an IMEX # channel either, so it is true on the CI runners (no fabric, no channel), where the allocation # itself is refused. Probe once per device. if device.device_id not in _fabric_allocation_error: from cuda.core import Device, VirtualMemoryResource, VirtualMemoryResourceOptions from cuda.core._utils.cuda_utils import CUDAError current = Device() device.set_current() try: mr = VirtualMemoryResource(device, VirtualMemoryResourceOptions(handle_type="fabric")) mr.allocate(1).close() _fabric_allocation_error[device.device_id] = "" except CUDAError as exc: # Only a refused physical allocation means "no fabric here" (NOT_PERMITTED on the CI runners, # INVALID_VALUE on an H200 without an IMEX channel). VirtualMemoryResource attaches its note about # the fabric and IMEX requirements at that stage alone; # a failure of the reservation, the mapping, or the stream setup is a real error and propagates. if "fabric handle" not in " ".join(getattr(exc, "__notes__", ())): raise _fabric_allocation_error[device.device_id] = str(exc).split(":", 1)[0] finally: current.set_current() if _fabric_allocation_error[device.device_id]: pytest.skip( "The device reports fabric handle support, but a fabric allocation is refused here: " + _fabric_allocation_error[device.device_id] ) return device @pytest.fixture def vmm_fabric_device(init_cuda): """A fabric-capable VMM device, tracking spawned children for teardown.""" from helpers.child_processes import track_child_processes device = _require_vmm_fabric_device(init_cuda) with track_child_processes(): yield device @pytest.fixture def vmm_fabric_device_x2(device_x2): """Two fabric-capable VMM devices, tracking spawned children for teardown.""" from helpers.child_processes import track_child_processes devices = tuple(_require_vmm_fabric_device(device) for device in device_x2) with track_child_processes(): yield devices def _make_ipc_memory_resource(kind, device): """Create an IPC-enabled memory resource of the given kind, or skip when the device cannot.""" if kind == "device": options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True) return DeviceMemoryResource(device, options=options) if kind == "pinned": skip_if_host_numa_pinned_memory_unsupported(device) options = PinnedMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True) return PinnedMemoryResource(options=options) assert kind == "vmm", kind _require_vmm_ipc_device(device) return VirtualMemoryResource(device, config=VirtualMemoryResourceOptions(handle_type="posix_fd")) def _close_memory_resource(mr): # A VirtualMemoryResource owns no pool and has no close(); its buffers free themselves. close = getattr(mr, "close", None) if close is not None: close() @pytest.fixture( params=[ pytest.param("device", id="DeviceMR"), pytest.param("pinned", id="PinnedMR"), pytest.param("vmm", id="VirtualMR"), ] ) def ipc_memory_resource(request, ipc_device): """Provides an IPC-enabled memory resource: a device pool, a pinned pool, or a VirtualMemoryResource. The VMM parameter needs VMM support and POSIX file descriptor handles on top of what :func:`ipc_device` requires; it skips otherwise. Tests that use the pool-only half of the IPC protocol (``allocation_handle``, ``from_allocation_handle``, the registry, ``uuid``, ``is_mapped`` on the resource) guard or skip for it. """ mr = _make_ipc_memory_resource(request.param, ipc_device) assert mr.is_ipc_enabled yield mr _close_memory_resource(mr) # TODO(seberg): Make sure the `mr` and it's buffers are fully torn down. # May be unnecessary as `mr.close()` is not parallel with other work. ipc_device.sync() @pytest.fixture( params=[ pytest.param("device", id="DeviceMR"), pytest.param("vmm", id="VirtualMR"), ] ) def ipc_memory_resource_factory(request, ipc_device): """Provides a callable that creates fresh IPC-enabled resources of one kind; all are closed at teardown.""" created = [] def make(): mr = _make_ipc_memory_resource(request.param, ipc_device) created.append(mr) return mr yield make for mr in created: _close_memory_resource(mr) ipc_device.sync() @pytest.fixture def mempool_device(init_cuda): """Obtains a device suitable for mempool tests, or skips.""" device = init_cuda if not device.properties.memory_pools_supported: pytest.skip("Device does not support mempool operations") return device def _mempool_device_impl(num): num_devices = len(cuda.core.Device.get_all_devices()) if num_devices < num: pytest.skip(f"Test requires at least {num} GPUs") devs = [Device(i) for i in range(num)] for i in reversed(range(num)): devs[i].set_current() # ends with device 0 current if not all(devs[i].can_access_peer(j) for i in range(num) for j in range(num)): pytest.skip("Test requires GPUs with peer access") if not all(devs[i].properties.memory_pools_supported for i in range(num)): pytest.skip("Device does not support mempool operations") return devs @pytest.fixture def mempool_device_x2(init_cuda): """Fixture that provides two devices if available, otherwise skips test.""" return _mempool_device_impl(2) @pytest.fixture def mempool_device_x3(init_cuda): """Fixture that provides three devices if available, otherwise skips test.""" return _mempool_device_impl(3) @pytest.fixture def ipc_mempool_device_x2(mempool_device_x2): """Fixture that provides two IPC-capable mempool devices, or skips. Also tracks/kills any leftover ``multiprocessing.Process`` children at teardown for the same reasons documented on :func:`ipc_device`. """ from helpers.child_processes import track_child_processes devices = _require_ipc_mempool_devices(mempool_device_x2) with track_child_processes(): yield devices @pytest.fixture( params=[ pytest.param((DeviceMemoryResource, DeviceMemoryResourceOptions), id="DeviceMR"), pytest.param((PinnedMemoryResource, PinnedMemoryResourceOptions), id="PinnedMR"), pytest.param((ManagedMemoryResource, ManagedMemoryResourceOptions), id="ManagedMR"), ] ) def memory_resource_factory(request, init_cuda): """Parametrized fixture providing memory resource types. Returns a 2-tuple of (MRClass, MROptionClass). Usage: def test_something(memory_resource_factory): MRClass, MROptions = memory_resource_factory device = Device() if MRClass is DeviceMemoryResource: mr = MRClass(device) elif MRClass is PinnedMemoryResource: mr = MRClass() elif MRClass is ManagedMemoryResource: mr = MRClass() """ return request.param