From 8d248820b95ff94b61606d985a299845a801b90c Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 30 Nov 2026 11:14:44 -0500 Subject: [PATCH 1/2] test(cuda.core): run the managed-memory concurrent-access warning tests test_warning_emitted and test_warning_emitted_only_once needed a GPU that supports managed memory pools but lacks concurrent managed access. No CI platform has that combination, so they never ran. Add a private _set_concurrent_managed_access_for_testing hook that overrides the CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS value the warning check sees (a cdef int, so the check stays a direct C call). Only the attribute value is faked: resource creation, the warning and its once-per-process lock run for real. A new fixture sets the override and resets the warning flag, and restores both afterwards. test_default_pool_error_without_concurrent_access still needs real hardware, since it tests the driver's own behavior. Remove the "not currently run in any CI job" markers. Closes #3573. Co-Authored-By: Claude Opus 5.5 --- .../core/_memory/_managed_memory_resource.pyi | 6 +++ .../core/_memory/_managed_memory_resource.pyx | 35 ++++++++++++--- .../tests/test_managed_memory_warning.py | 44 ++++++++++++++----- 3 files changed, 68 insertions(+), 17 deletions(-) diff --git a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyi b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyi index 72baa599b55b..51e713feb3e3 100644 --- a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyi +++ b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyi @@ -121,3 +121,9 @@ class ManagedMemoryResource(_MemPool): def reset_concurrent_access_warning() -> None: """Reset the concurrent access warning flag for testing purposes.""" +def _set_concurrent_managed_access_for_testing(supported: bool | None) -> None: + """Override whether the concurrent-access warning check sees concurrent managed access. + + Test hook: only the attribute value is faked; the warning, its category and + the once-per-process lock run for real. ``None`` reads the driver again. + """ diff --git a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx index f50472758b94..c654a28dfdda 100644 --- a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx +++ b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx @@ -300,6 +300,9 @@ cdef inline _MMR_init(ManagedMemoryResource self, options): cdef bint _concurrent_access_warned = False cdef object _concurrent_access_lock = threading.Lock() +# Test hook: the CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS value to use +# instead of the driver's (0 or 1), or -1 to read the driver. +cdef int _concurrent_access_override = -1 cdef inline _check_concurrent_managed_access(): @@ -313,13 +316,16 @@ cdef inline _check_concurrent_managed_access(): if _concurrent_access_warned: return - # concurrent_managed_access is a system-level attribute for sm_60 and - # later, so any device will do. - with nogil: - HANDLE_RETURN(cydriver.cuDeviceGetAttribute( - &c_concurrent, - cydriver.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS, - 0)) + if _concurrent_access_override >= 0: + c_concurrent = _concurrent_access_override + else: + # concurrent_managed_access is a system-level attribute for sm_60 and + # later, so any device will do. + with nogil: + HANDLE_RETURN(cydriver.cuDeviceGetAttribute( + &c_concurrent, + cydriver.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS, + 0)) if not c_concurrent: warnings.warn( "This platform does not support concurrent managed memory access " @@ -340,3 +346,18 @@ def reset_concurrent_access_warning() -> None: """Reset the concurrent access warning flag for testing purposes.""" global _concurrent_access_warned _concurrent_access_warned = False + + +def _set_concurrent_managed_access_for_testing(supported: bool | None) -> None: + """Override whether the concurrent-access warning check sees concurrent managed access. + + Test hook: only the attribute value is faked; the warning, its category and + the once-per-process lock run for real. ``None`` reads the driver again. + """ + global _concurrent_access_override + if supported is None: + _concurrent_access_override = -1 + elif supported: + _concurrent_access_override = 1 + else: + _concurrent_access_override = 0 diff --git a/cuda_core/tests/test_managed_memory_warning.py b/cuda_core/tests/test_managed_memory_warning.py index 60e99439b262..cb91e25d97f8 100644 --- a/cuda_core/tests/test_managed_memory_warning.py +++ b/cuda_core/tests/test_managed_memory_warning.py @@ -5,7 +5,8 @@ Test that a warning is emitted when ManagedMemoryResource is created on a platform without concurrent managed memory access. -These tests only run on affected platforms (concurrent_managed_access is False). +No CI platform supports managed memory pools without concurrent managed access, +so the warning tests fake that attribute with a test hook. """ import sys @@ -16,7 +17,10 @@ import cuda.bindings from cuda.core import Device, ManagedMemoryResource, ManagedMemoryResourceOptions -from cuda.core._memory._managed_memory_resource import reset_concurrent_access_warning +from cuda.core._memory._managed_memory_resource import ( + _set_concurrent_managed_access_for_testing, + reset_concurrent_access_warning, +) from cuda.core._utils.cuda_utils import CUDAError _cuda_major = int(cuda.bindings.__version__.split(".")[0]) @@ -50,6 +54,28 @@ def device_without_concurrent_managed_access(init_cuda): return device +@pytest.fixture +def faked_no_concurrent_managed_access(init_cuda): + """Make the warning check see no concurrent managed access, on any device with memory pools. + + Only the attribute value is faked: creating the resource, the warning and + its once-per-process lock run for real. + """ + device = Device() + device.set_current() + + if not device.properties.memory_pools_supported: + pytest.skip("Device does not support memory pools") + + _set_concurrent_managed_access_for_testing(False) + reset_concurrent_access_warning() + try: + yield device + finally: + _set_concurrent_managed_access_for_testing(None) + reset_concurrent_access_warning() + + @requires_cuda_13 def test_default_pool_error_without_concurrent_access(device_without_concurrent_managed_access): """ManagedMemoryResource() raises RuntimeError when the default pool doesn't support managed.""" @@ -65,12 +91,11 @@ def test_default_pool_error_without_concurrent_access(device_without_concurrent_ raise -# This test is not currently run in any CI job @requires_cuda_13 -def test_warning_emitted(device_without_concurrent_managed_access): +@pytest.mark.thread_unsafe(reason="the concurrent-access override and warning flag are process-wide") +def test_warning_emitted(faked_no_concurrent_managed_access): """ManagedMemoryResource emits a warning when concurrent managed access is unsupported.""" - dev_id = device_without_concurrent_managed_access.device_id - reset_concurrent_access_warning() + dev_id = faked_no_concurrent_managed_access.device_id with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") @@ -86,12 +111,11 @@ def test_warning_emitted(device_without_concurrent_managed_access): mr.close() -# This test is not currently run in any CI job @requires_cuda_13 -def test_warning_emitted_only_once(device_without_concurrent_managed_access): +@pytest.mark.thread_unsafe(reason="the concurrent-access override and warning flag are process-wide") +def test_warning_emitted_only_once(faked_no_concurrent_managed_access): """Warning fires only once even when multiple ManagedMemoryResources are created.""" - dev_id = device_without_concurrent_managed_access.device_id - reset_concurrent_access_warning() + dev_id = faked_no_concurrent_managed_access.device_id with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") From 6dba05abd1aa952330f628eed4627b68aa3bf061 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 30 Nov 2026 16:51:22 -0500 Subject: [PATCH 2/2] test(cuda.core): fake no concurrent managed access without a production test hook Remove _set_concurrent_managed_access_for_testing and its override global from _managed_memory_resource. The once-per-process warning check now reads Device(0).properties.concurrent_managed_access instead of calling cuDeviceGetAttribute itself, so the tests can monkeypatch Device.properties with a proxy that reports no concurrent managed access. Co-Authored-By: Claude Opus 5.5 --- .../core/_memory/_managed_memory_resource.pyi | 6 --- .../core/_memory/_managed_memory_resource.pyx | 37 +++---------------- .../tests/test_managed_memory_warning.py | 35 ++++++++++++------ 3 files changed, 29 insertions(+), 49 deletions(-) diff --git a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyi b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyi index 51e713feb3e3..72baa599b55b 100644 --- a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyi +++ b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyi @@ -121,9 +121,3 @@ class ManagedMemoryResource(_MemPool): def reset_concurrent_access_warning() -> None: """Reset the concurrent access warning flag for testing purposes.""" -def _set_concurrent_managed_access_for_testing(supported: bool | None) -> None: - """Override whether the concurrent-access warning check sees concurrent managed access. - - Test hook: only the attribute value is faked; the warning, its category and - the once-per-process lock run for real. ``None`` reads the driver again. - """ diff --git a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx index c654a28dfdda..1f79f1f58859 100644 --- a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx +++ b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx @@ -4,12 +4,11 @@ from __future__ import annotations -from cuda.bindings cimport cydriver +from cuda.bindings cimport cydriver # no-cython-lint from cuda.core._memory._memory_pool cimport _MemPool, _MP_allocate, MP_check_open from cuda.core._memory._memory_pool cimport MP_init_create_pool, MP_init_current_pool # no-cython-lint from cuda.core._stream cimport Stream, Stream_accept -from cuda.core._utils.cuda_utils cimport HANDLE_RETURN from cuda.core._utils.cuda_utils cimport check_or_create_options # no-cython-lint from cuda.core._utils.cuda_utils import CUDAError # no-cython-lint @@ -18,6 +17,7 @@ import threading from typing import TYPE_CHECKING import warnings +from cuda.core._device import Device from cuda.core._memory._managed_buffer import ManagedBuffer from cuda.core.typing import ManagedMemoryLocationType @@ -300,9 +300,6 @@ cdef inline _MMR_init(ManagedMemoryResource self, options): cdef bint _concurrent_access_warned = False cdef object _concurrent_access_lock = threading.Lock() -# Test hook: the CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS value to use -# instead of the driver's (0 or 1), or -1 to read the driver. -cdef int _concurrent_access_override = -1 cdef inline _check_concurrent_managed_access(): @@ -311,22 +308,13 @@ cdef inline _check_concurrent_managed_access(): if _concurrent_access_warned: return - cdef int c_concurrent = 0 with _concurrent_access_lock: if _concurrent_access_warned: return - if _concurrent_access_override >= 0: - c_concurrent = _concurrent_access_override - else: - # concurrent_managed_access is a system-level attribute for sm_60 and - # later, so any device will do. - with nogil: - HANDLE_RETURN(cydriver.cuDeviceGetAttribute( - &c_concurrent, - cydriver.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS, - 0)) - if not c_concurrent: + # concurrent_managed_access is a system-level attribute for sm_60 and + # later, so any device will do. This runs once per process. + if not Device(0).properties.concurrent_managed_access: warnings.warn( "This platform does not support concurrent managed memory access " "(Device.properties.concurrent_managed_access is False). Host access to any managed " @@ -346,18 +334,3 @@ def reset_concurrent_access_warning() -> None: """Reset the concurrent access warning flag for testing purposes.""" global _concurrent_access_warned _concurrent_access_warned = False - - -def _set_concurrent_managed_access_for_testing(supported: bool | None) -> None: - """Override whether the concurrent-access warning check sees concurrent managed access. - - Test hook: only the attribute value is faked; the warning, its category and - the once-per-process lock run for real. ``None`` reads the driver again. - """ - global _concurrent_access_override - if supported is None: - _concurrent_access_override = -1 - elif supported: - _concurrent_access_override = 1 - else: - _concurrent_access_override = 0 diff --git a/cuda_core/tests/test_managed_memory_warning.py b/cuda_core/tests/test_managed_memory_warning.py index cb91e25d97f8..0639b1ee35d2 100644 --- a/cuda_core/tests/test_managed_memory_warning.py +++ b/cuda_core/tests/test_managed_memory_warning.py @@ -6,7 +6,7 @@ platform without concurrent managed memory access. No CI platform supports managed memory pools without concurrent managed access, -so the warning tests fake that attribute with a test hook. +so the warning tests make Device.properties report no concurrent managed access. """ import sys @@ -17,10 +17,7 @@ import cuda.bindings from cuda.core import Device, ManagedMemoryResource, ManagedMemoryResourceOptions -from cuda.core._memory._managed_memory_resource import ( - _set_concurrent_managed_access_for_testing, - reset_concurrent_access_warning, -) +from cuda.core._memory._managed_memory_resource import reset_concurrent_access_warning from cuda.core._utils.cuda_utils import CUDAError _cuda_major = int(cuda.bindings.__version__.split(".")[0]) @@ -54,11 +51,23 @@ def device_without_concurrent_managed_access(init_cuda): return device +class _PropertiesWithoutConcurrentManagedAccess: + """Device properties that report no concurrent managed access and delegate everything else.""" + + concurrent_managed_access = False + + def __init__(self, properties): + self._properties = properties + + def __getattr__(self, name): + return getattr(self._properties, name) + + @pytest.fixture -def faked_no_concurrent_managed_access(init_cuda): +def faked_no_concurrent_managed_access(init_cuda, monkeypatch): """Make the warning check see no concurrent managed access, on any device with memory pools. - Only the attribute value is faked: creating the resource, the warning and + Only the property value is faked: creating the resource, the warning and its once-per-process lock run for real. """ device = Device() @@ -67,12 +76,16 @@ def faked_no_concurrent_managed_access(init_cuda): if not device.properties.memory_pools_supported: pytest.skip("Device does not support memory pools") - _set_concurrent_managed_access_for_testing(False) + properties = Device.properties + monkeypatch.setattr( + Device, + "properties", + property(lambda self: _PropertiesWithoutConcurrentManagedAccess(properties.__get__(self))), + ) reset_concurrent_access_warning() try: yield device finally: - _set_concurrent_managed_access_for_testing(None) reset_concurrent_access_warning() @@ -92,7 +105,7 @@ def test_default_pool_error_without_concurrent_access(device_without_concurrent_ @requires_cuda_13 -@pytest.mark.thread_unsafe(reason="the concurrent-access override and warning flag are process-wide") +@pytest.mark.thread_unsafe(reason="the patched Device.properties and the warning flag are process-wide") def test_warning_emitted(faked_no_concurrent_managed_access): """ManagedMemoryResource emits a warning when concurrent managed access is unsupported.""" dev_id = faked_no_concurrent_managed_access.device_id @@ -112,7 +125,7 @@ def test_warning_emitted(faked_no_concurrent_managed_access): @requires_cuda_13 -@pytest.mark.thread_unsafe(reason="the concurrent-access override and warning flag are process-wide") +@pytest.mark.thread_unsafe(reason="the patched Device.properties and the warning flag are process-wide") def test_warning_emitted_only_once(faked_no_concurrent_managed_access): """Warning fires only once even when multiple ManagedMemoryResources are created.""" dev_id = faked_no_concurrent_managed_access.device_id