Skip to content

Commit 6dba05a

Browse files
mdboomclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 8d24882 commit 6dba05a

3 files changed

Lines changed: 29 additions & 49 deletions

File tree

‎cuda_core/cuda/core/_memory/_managed_memory_resource.pyi‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,3 @@ class ManagedMemoryResource(_MemPool):
121121

122122
def reset_concurrent_access_warning() -> None:
123123
"""Reset the concurrent access warning flag for testing purposes."""
124-
def _set_concurrent_managed_access_for_testing(supported: bool | None) -> None:
125-
"""Override whether the concurrent-access warning check sees concurrent managed access.
126-
127-
Test hook: only the attribute value is faked; the warning, its category and
128-
the once-per-process lock run for real. ``None`` reads the driver again.
129-
"""

‎cuda_core/cuda/core/_memory/_managed_memory_resource.pyx‎

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
from __future__ import annotations
66

7-
from cuda.bindings cimport cydriver
7+
from cuda.bindings cimport cydriver # no-cython-lint
88

99
from cuda.core._memory._memory_pool cimport _MemPool, _MP_allocate, MP_check_open
1010
from cuda.core._memory._memory_pool cimport MP_init_create_pool, MP_init_current_pool # no-cython-lint
1111
from cuda.core._stream cimport Stream, Stream_accept
12-
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
1312
from cuda.core._utils.cuda_utils cimport check_or_create_options # no-cython-lint
1413
from cuda.core._utils.cuda_utils import CUDAError # no-cython-lint
1514

@@ -18,6 +17,7 @@ import threading
1817
from typing import TYPE_CHECKING
1918
import warnings
2019

20+
from cuda.core._device import Device
2121
from cuda.core._memory._managed_buffer import ManagedBuffer
2222
from cuda.core.typing import ManagedMemoryLocationType
2323

@@ -300,9 +300,6 @@ cdef inline _MMR_init(ManagedMemoryResource self, options):
300300

301301
cdef bint _concurrent_access_warned = False
302302
cdef object _concurrent_access_lock = threading.Lock()
303-
# Test hook: the CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS value to use
304-
# instead of the driver's (0 or 1), or -1 to read the driver.
305-
cdef int _concurrent_access_override = -1
306303

307304

308305
cdef inline _check_concurrent_managed_access():
@@ -311,22 +308,13 @@ cdef inline _check_concurrent_managed_access():
311308
if _concurrent_access_warned:
312309
return
313310

314-
cdef int c_concurrent = 0
315311
with _concurrent_access_lock:
316312
if _concurrent_access_warned:
317313
return
318314

319-
if _concurrent_access_override >= 0:
320-
c_concurrent = _concurrent_access_override
321-
else:
322-
# concurrent_managed_access is a system-level attribute for sm_60 and
323-
# later, so any device will do.
324-
with nogil:
325-
HANDLE_RETURN(cydriver.cuDeviceGetAttribute(
326-
&c_concurrent,
327-
cydriver.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS,
328-
0))
329-
if not c_concurrent:
315+
# concurrent_managed_access is a system-level attribute for sm_60 and
316+
# later, so any device will do. This runs once per process.
317+
if not Device(0).properties.concurrent_managed_access:
330318
warnings.warn(
331319
"This platform does not support concurrent managed memory access "
332320
"(Device.properties.concurrent_managed_access is False). Host access to any managed "
@@ -346,18 +334,3 @@ def reset_concurrent_access_warning() -> None:
346334
"""Reset the concurrent access warning flag for testing purposes."""
347335
global _concurrent_access_warned
348336
_concurrent_access_warned = False
349-
350-
351-
def _set_concurrent_managed_access_for_testing(supported: bool | None) -> None:
352-
"""Override whether the concurrent-access warning check sees concurrent managed access.
353-
354-
Test hook: only the attribute value is faked; the warning, its category and
355-
the once-per-process lock run for real. ``None`` reads the driver again.
356-
"""
357-
global _concurrent_access_override
358-
if supported is None:
359-
_concurrent_access_override = -1
360-
elif supported:
361-
_concurrent_access_override = 1
362-
else:
363-
_concurrent_access_override = 0

‎cuda_core/tests/test_managed_memory_warning.py‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
platform without concurrent managed memory access.
77
88
No CI platform supports managed memory pools without concurrent managed access,
9-
so the warning tests fake that attribute with a test hook.
9+
so the warning tests make Device.properties report no concurrent managed access.
1010
"""
1111

1212
import sys
@@ -17,10 +17,7 @@
1717

1818
import cuda.bindings
1919
from cuda.core import Device, ManagedMemoryResource, ManagedMemoryResourceOptions
20-
from cuda.core._memory._managed_memory_resource import (
21-
_set_concurrent_managed_access_for_testing,
22-
reset_concurrent_access_warning,
23-
)
20+
from cuda.core._memory._managed_memory_resource import reset_concurrent_access_warning
2421
from cuda.core._utils.cuda_utils import CUDAError
2522

2623
_cuda_major = int(cuda.bindings.__version__.split(".")[0])
@@ -54,11 +51,23 @@ def device_without_concurrent_managed_access(init_cuda):
5451
return device
5552

5653

54+
class _PropertiesWithoutConcurrentManagedAccess:
55+
"""Device properties that report no concurrent managed access and delegate everything else."""
56+
57+
concurrent_managed_access = False
58+
59+
def __init__(self, properties):
60+
self._properties = properties
61+
62+
def __getattr__(self, name):
63+
return getattr(self._properties, name)
64+
65+
5766
@pytest.fixture
58-
def faked_no_concurrent_managed_access(init_cuda):
67+
def faked_no_concurrent_managed_access(init_cuda, monkeypatch):
5968
"""Make the warning check see no concurrent managed access, on any device with memory pools.
6069
61-
Only the attribute value is faked: creating the resource, the warning and
70+
Only the property value is faked: creating the resource, the warning and
6271
its once-per-process lock run for real.
6372
"""
6473
device = Device()
@@ -67,12 +76,16 @@ def faked_no_concurrent_managed_access(init_cuda):
6776
if not device.properties.memory_pools_supported:
6877
pytest.skip("Device does not support memory pools")
6978

70-
_set_concurrent_managed_access_for_testing(False)
79+
properties = Device.properties
80+
monkeypatch.setattr(
81+
Device,
82+
"properties",
83+
property(lambda self: _PropertiesWithoutConcurrentManagedAccess(properties.__get__(self))),
84+
)
7185
reset_concurrent_access_warning()
7286
try:
7387
yield device
7488
finally:
75-
_set_concurrent_managed_access_for_testing(None)
7689
reset_concurrent_access_warning()
7790

7891

@@ -92,7 +105,7 @@ def test_default_pool_error_without_concurrent_access(device_without_concurrent_
92105

93106

94107
@requires_cuda_13
95-
@pytest.mark.thread_unsafe(reason="the concurrent-access override and warning flag are process-wide")
108+
@pytest.mark.thread_unsafe(reason="the patched Device.properties and the warning flag are process-wide")
96109
def test_warning_emitted(faked_no_concurrent_managed_access):
97110
"""ManagedMemoryResource emits a warning when concurrent managed access is unsupported."""
98111
dev_id = faked_no_concurrent_managed_access.device_id
@@ -112,7 +125,7 @@ def test_warning_emitted(faked_no_concurrent_managed_access):
112125

113126

114127
@requires_cuda_13
115-
@pytest.mark.thread_unsafe(reason="the concurrent-access override and warning flag are process-wide")
128+
@pytest.mark.thread_unsafe(reason="the patched Device.properties and the warning flag are process-wide")
116129
def test_warning_emitted_only_once(faked_no_concurrent_managed_access):
117130
"""Warning fires only once even when multiple ManagedMemoryResources are created."""
118131
dev_id = faked_no_concurrent_managed_access.device_id

0 commit comments

Comments
 (0)