Skip to content

Commit 68f832c

Browse files
committed
test(cuda.core): request init_cuda for memory resource tests
Tests that allocate through Device.memory_resource / Device.allocate or construct any memory resource without init_cuda did Device() + set_current() by hand, leaving a stale cached default MemoryResource on the thread-local Device: no teardown gc.collect(), no cuCtxSynchronize, and the context never popped. The stale slot is the aliasing route for the planned live-resource-count gc.collect() gate, and the tests violate the tests/AGENTS.md rule that constructing a memory resource counts as touching CUDA. Route both groups onto init_cuda (using the yielded device) so the fixture teardown clears the cached slot via del _tls.devices: - device.memory_resource allocators: 6 test_from_buffer* functions in test_utils.py and test_buffer_external_device in test_memory.py. - resource constructors: 28 functions in test_memory.py (buffers, dlpack, vmm, pinned/managed ipc, legacy pinned, abstract stubs) and 4 launch-scalar tests in test_launcher.py. test_buffer_external_device and test_buffer_external_pinned_alloc still select Device(n - 1) as before; init_cuda only supplies the context and teardown. The no-context tests (test_close_with_default_stream_requires_context, test_from_handle_mr_default_stream_requires_context) keep their no_current_context() body. Assertions are unchanged. Add a sentence to tests/AGENTS.md that allocating through device.memory_resource or Device.allocate, or constructing any memory resource, counts as touching CUDA and requires init_cuda.
1 parent 106da2f commit 68f832c

4 files changed

Lines changed: 75 additions & 100 deletions

File tree

‎cuda_core/tests/AGENTS.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ that calls a CUDA API without `init_cuda` (or an explicit
239239
to leave current on the thread — possibly none. With `pytest-randomly` that
240240
makes the pass/fail outcome depend on test order, so it moves seed to seed and
241241
looks like flakiness. Request `init_cuda` for any test that calls into the
242-
driver, or set up and tear down a context yourself.
242+
driver, or set up and tear down a context yourself. Allocating through
243+
`device.memory_resource` or `Device.allocate`, or constructing any memory
244+
resource, counts as touching CUDA and requires `init_cuda`.
243245

244246
## Assert on behavior, not implementation
245247

‎cuda_core/tests/test_launcher.py‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_to_native_launch_config_priority(init_cuda, priority, expected_num_attr
244244

245245

246246
@skipif_need_cuda_headers
247-
def test_pdl_primary_secondary_overlap_same_stream():
247+
def test_pdl_primary_secondary_overlap_same_stream(init_cuda):
248248
"""Primary + secondary PDL launch on one stream can overlap on Hopper+.
249249
250250
Secondary is launched with ``programmatic_stream_serialization=True``. After
@@ -255,10 +255,9 @@ def test_pdl_primary_secondary_overlap_same_stream():
255255
Note concurrency is opportunistic, so a missing overlap execution is reported as
256256
an expected failure.
257257
"""
258-
dev = Device()
258+
dev = init_cuda
259259
if dev.compute_capability < (9, 0):
260260
pytest.skip("Programmatic Dependent Launch requires compute capability >= 9.0")
261-
dev.set_current()
262261
stream = dev.create_stream(options=StreamOptions(nonblocking=True))
263262

264263
# clock64 budgets are in GPU cycles; keep the post-trigger window long enough
@@ -592,9 +591,8 @@ def write_scalar_module():
592591

593592
@pytest.mark.parametrize("python_type, cpp_type, init_value", PARAMS)
594593
@requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)")
595-
def test_launch_scalar_argument(write_scalar_module, python_type, cpp_type, init_value):
596-
dev = Device(0)
597-
dev.set_current()
594+
def test_launch_scalar_argument(init_cuda, write_scalar_module, python_type, cpp_type, init_value):
595+
dev = init_cuda
598596

599597
# Prepare pinned host array
600598
mr = LegacyPinnedMemoryResource()
@@ -825,7 +823,7 @@ class MyBool(ctypes.c_bool):
825823
"numpy_float32",
826824
],
827825
)
828-
def test_launch_scalar_argument_subclass_fallback(base_type, np_dtype, cpp_type, raw_value):
826+
def test_launch_scalar_argument_subclass_fallback(init_cuda, base_type, np_dtype, cpp_type, raw_value):
829827
"""Subclassed scalar arguments survive fallback handling and reach the kernel
830828
with the correct width/sign. The readback value (not just ptr != 0) guards each
831829
fallback branch against marshalling the wrong C type, e.g. uint64 -> uint32_t."""
@@ -836,8 +834,7 @@ class Subclassed(base_type):
836834
scalar = Subclassed(raw_value)
837835
expected = np_dtype(raw_value)
838836

839-
dev = Device()
840-
dev.set_current()
837+
dev = init_cuda
841838

842839
mr = LegacyPinnedMemoryResource()
843840
b = mr.allocate(np.dtype(np_dtype).itemsize)
@@ -939,7 +936,9 @@ class MyComplex(complex):
939936
ids=_NUMPY_SUBCLASS_FALLBACK_IDS,
940937
)
941938
@pytest.mark.agent_authored(model="gpt-5.6-sol")
942-
def test_launch_numpy_scalar_subclass_fallback(write_scalar_module, base_type, np_dtype, cpp_type, raw_value):
939+
def test_launch_numpy_scalar_subclass_fallback(
940+
init_cuda, write_scalar_module, base_type, np_dtype, cpp_type, raw_value
941+
):
943942
"""Subclassed numpy scalars take prepare_numpy_arg's isinstance fallback and reach the kernel (readback)."""
944943

945944
class Subclassed(base_type):
@@ -948,8 +947,7 @@ class Subclassed(base_type):
948947
scalar = Subclassed(raw_value)
949948
expected = np_dtype(raw_value)
950949

951-
dev = Device(0)
952-
dev.set_current()
950+
dev = init_cuda
953951

954952
mr = LegacyPinnedMemoryResource()
955953
b = mr.allocate(np.dtype(np_dtype).itemsize)

0 commit comments

Comments
 (0)