diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index b667f862d48..2eaca1de18b 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -38,6 +38,7 @@ from helpers.memory import ( BaselineMemoryResourceReleasedWarning, UnreleasedMemoryResourceWarning, + make_pinned_allocator, skip_if_pinned_memory_unsupported, ) @@ -47,6 +48,7 @@ Device, DeviceMemoryResource, DeviceMemoryResourceOptions, + LegacyPinnedMemoryResource, ManagedMemoryResource, ManagedMemoryResourceOptions, PinnedMemoryResource, @@ -586,6 +588,15 @@ def mempool_device(init_cuda): return device +@pytest.fixture( + params=(LegacyPinnedMemoryResource, PinnedMemoryResource), + ids=lambda resource_type: resource_type.__name__, +) +def pinned_alloc(request, init_cuda): + """Allocate host-ready buffers from either pinned memory resource.""" + return make_pinned_allocator(request.param) + + def _mempool_device_impl(num): num_devices = len(cuda.core.Device.get_all_devices()) if num_devices < num: diff --git a/cuda_core/tests/graph/test_device_launch.py b/cuda_core/tests/graph/test_device_launch.py index 5056b01fd43..b38344359c2 100644 --- a/cuda_core/tests/graph/test_device_launch.py +++ b/cuda_core/tests/graph/test_device_launch.py @@ -11,7 +11,6 @@ from cuda.core import ( Device, LaunchConfig, - LegacyPinnedMemoryResource, Linker, LinkerOptions, ObjectCode, @@ -77,7 +76,7 @@ def _compile_device_launcher_kernel(): reason="Device-side graph launch requires Hopper (sm_90+) architecture", ) @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_device_launch_basic(init_cuda): +def test_device_launch_basic(init_cuda, pinned_alloc): """Test basic device-side graph launch functionality. This test verifies that a graph can be launched from device code by: @@ -94,8 +93,7 @@ def test_device_launch_basic(init_cuda): launcher_kernel = _compile_device_launcher_kernel() # Allocate and initialize memory - mr = LegacyPinnedMemoryResource() - buf = mr.allocate(4) + buf = pinned_alloc(4) arr = np.from_dlpack(buf).view(np.int32) arr[0] = 0 stream.sync() @@ -129,7 +127,7 @@ def test_device_launch_basic(init_cuda): reason="Device-side graph launch requires Hopper (sm_90+) architecture", ) @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_device_launch_multiple(init_cuda): +def test_device_launch_multiple(init_cuda, pinned_alloc): """Test that device-side graph launch can be executed multiple times. This test verifies that both the outer and inner graphs can be reused @@ -144,8 +142,7 @@ def test_device_launch_multiple(init_cuda): launcher_kernel = _compile_device_launcher_kernel() # Allocate and initialize memory - mr = LegacyPinnedMemoryResource() - buf = mr.allocate(4) + buf = pinned_alloc(4) arr = np.from_dlpack(buf).view(np.int32) arr[0] = 0 stream.sync() diff --git a/cuda_core/tests/graph/test_graph_builder.py b/cuda_core/tests/graph/test_graph_builder.py index fbea7e7a571..a7920c05e6c 100644 --- a/cuda_core/tests/graph/test_graph_builder.py +++ b/cuda_core/tests/graph/test_graph_builder.py @@ -18,7 +18,6 @@ CUDAWarning, Device, LaunchConfig, - LegacyPinnedMemoryResource, Program, ProgramOptions, StreamOptions, @@ -154,14 +153,13 @@ def test_graph_is_join_required(init_cuda): @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_graph_repeat_capture(init_cuda): +def test_graph_repeat_capture(init_cuda, pinned_alloc): mod = compile_common_kernels() add_one = mod.get_kernel("add_one") # Allocate memory launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(4) + b = pinned_alloc(4) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 @@ -475,14 +473,13 @@ def callback(): @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_graph_child_graph(init_cuda): +def test_graph_child_graph(init_cuda, pinned_alloc): mod = compile_common_kernels() add_one = mod.get_kernel("add_one") # Allocate memory launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(8) + b = pinned_alloc(8) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 arr[1] = 0 @@ -706,14 +703,13 @@ def test_graph_definition_raises_after_close(init_cuda): @pytest.mark.agent_authored(model="claude-opus-4.8") -def test_graph_definition_round_trips_through_explicit_api(init_cuda): +def test_graph_definition_round_trips_through_explicit_api(init_cuda, pinned_alloc): """Mutating via the explicit API survives complete() and runs correctly.""" mod = compile_common_kernels() add_one = mod.get_kernel("add_one") launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(4) + b = pinned_alloc(4) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 @@ -737,7 +733,7 @@ def test_graph_definition_round_trips_through_explicit_api(init_cuda): @pytest.mark.agent_authored(model="claude-opus-4.8") @requires_module(np, "2.1") -def test_graph_definition_hybrid_conditional_body(init_cuda): +def test_graph_definition_hybrid_conditional_body(init_cuda, pinned_alloc): """Populate a conditional body entirely through the explicit API. This is the headline hybrid flow enabled by the new property: @@ -750,8 +746,7 @@ def test_graph_definition_hybrid_conditional_body(init_cuda): set_handle = mod.get_kernel("set_handle") launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(4) + b = pinned_alloc(4) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 @@ -777,15 +772,14 @@ def test_graph_definition_hybrid_conditional_body(init_cuda): @pytest.mark.agent_authored(model="claude-opus-4.8") @requires_module(np, "2.1") -def test_graph_definition_conditional_body_after_capture(init_cuda): +def test_graph_definition_conditional_body_after_capture(init_cuda, pinned_alloc): """Capture into a conditional body, then augment it via the explicit API.""" mod = compile_conditional_kernels(int) add_one = mod.get_kernel("add_one") set_handle = mod.get_kernel("set_handle") launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(4) + b = pinned_alloc(4) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 @@ -828,7 +822,7 @@ def test_graph_definition_conditional_body_during_capture_raises(init_cuda): @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_pdl_launch_graph_capture(init_cuda): +def test_pdl_launch_graph_capture(init_cuda, pinned_alloc): """PDL LaunchConfig is graph-compatible via GraphBuilder stream capture. Captures a first then a secondary launch with @@ -869,8 +863,7 @@ def _assert_programmatic_dependency_edge(graph_definition): dummy_kernel = mod.get_kernel("add_one") stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - buf = mr.allocate(4) + buf = pinned_alloc(4) arr = np.from_dlpack(buf).view(np.int32) arr[0] = 0 @@ -894,7 +887,7 @@ def _assert_programmatic_dependency_edge(graph_definition): @skipif_need_cuda_headers @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_pdl_same_stream_primary_secondary_overlap_via_graph(init_cuda): +def test_pdl_same_stream_primary_secondary_overlap_via_graph(init_cuda, pinned_alloc): """Same-stream PDL overlap via GraphBuilder stream capture on Hopper+.""" dev = Device() if dev.compute_capability < (9, 0): @@ -932,9 +925,8 @@ def test_pdl_same_stream_primary_secondary_overlap_via_graph(init_cuda): secondary = module.get_kernel("secondary_kernel") stream = dev.create_stream(options=StreamOptions(nonblocking=True)) - mr = LegacyPinnedMemoryResource() - secondary_started = np.from_dlpack(mr.allocate(4)).view(np.int32) - overlapped = np.from_dlpack(mr.allocate(4)).view(np.int32) + secondary_started = np.from_dlpack(pinned_alloc(4)).view(np.int32) + overlapped = np.from_dlpack(pinned_alloc(4)).view(np.int32) primary_cfg = LaunchConfig(grid=1, block=1) secondary_cfg = LaunchConfig(grid=1, block=1, programmatic_stream_serialization=True) diff --git a/cuda_core/tests/graph/test_graph_builder_conditional.py b/cuda_core/tests/graph/test_graph_builder_conditional.py index 98daa4233cc..0c060637e8f 100644 --- a/cuda_core/tests/graph/test_graph_builder_conditional.py +++ b/cuda_core/tests/graph/test_graph_builder_conditional.py @@ -10,7 +10,7 @@ from cuda_python_test_helpers.marks import requires_module from helpers.graph_kernels import compile_conditional_kernels -from cuda.core import Device, LaunchConfig, LegacyPinnedMemoryResource, launch +from cuda.core import Device, LaunchConfig, launch from cuda.core.graph import GraphBuilder @@ -18,15 +18,14 @@ "condition_value", [True, False, ctypes.c_bool(True), ctypes.c_bool(False), np.bool_(True), np.bool_(False), 1, 0] ) @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_graph_conditional_if(init_cuda, condition_value): +def test_graph_conditional_if(init_cuda, pinned_alloc, condition_value): mod = compile_conditional_kernels(type(condition_value)) add_one = mod.get_kernel("add_one") set_handle = mod.get_kernel("set_handle") # Allocate memory launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(8) + b = pinned_alloc(8) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 arr[1] = 0 @@ -82,15 +81,14 @@ def test_graph_conditional_if(init_cuda, condition_value): "condition_value", [True, False, ctypes.c_bool(True), ctypes.c_bool(False), np.bool_(True), np.bool_(False), 1, 0] ) @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_graph_conditional_if_else(init_cuda, condition_value): +def test_graph_conditional_if_else(init_cuda, pinned_alloc, condition_value): mod = compile_conditional_kernels(type(condition_value)) add_one = mod.get_kernel("add_one") set_handle = mod.get_kernel("set_handle") # Allocate memory launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(8) + b = pinned_alloc(8) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 arr[1] = 0 @@ -154,15 +152,14 @@ def test_graph_conditional_if_else(init_cuda, condition_value): @pytest.mark.parametrize("condition_value", [0, 1, 2, 3]) @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_graph_conditional_switch(init_cuda, condition_value): +def test_graph_conditional_switch(init_cuda, pinned_alloc, condition_value): mod = compile_conditional_kernels(type(condition_value)) add_one = mod.get_kernel("add_one") set_handle = mod.get_kernel("set_handle") # Allocate memory launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(12) + b = pinned_alloc(12) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 arr[1] = 0 @@ -245,7 +242,7 @@ def test_graph_conditional_switch(init_cuda, condition_value): @pytest.mark.parametrize("condition_value", [True, False, 1, 0]) @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_graph_conditional_while(init_cuda, condition_value): +def test_graph_conditional_while(init_cuda, pinned_alloc, condition_value): mod = compile_conditional_kernels(type(condition_value)) add_one = mod.get_kernel("add_one") loop_kernel = mod.get_kernel("loop_kernel") @@ -253,8 +250,7 @@ def test_graph_conditional_while(init_cuda, condition_value): # Allocate memory launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(4) + b = pinned_alloc(4) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 @@ -293,7 +289,7 @@ def test_graph_conditional_while(init_cuda, condition_value): @requires_module(np, "2.1") -def test_graph_conditional_on_forked_builder(init_cuda): +def test_graph_conditional_on_forked_builder(init_cuda, pinned_alloc): """A conditional created on a forked builder keeps its body graph's parent handle pinned to the owning primary graph.""" mod = compile_conditional_kernels(int) @@ -301,8 +297,7 @@ def test_graph_conditional_on_forked_builder(init_cuda): set_handle = mod.get_kernel("set_handle") launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(4) + b = pinned_alloc(4) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 @@ -338,7 +333,7 @@ def test_graph_conditional_on_forked_builder(init_cuda): @requires_module(np, "2.1") -def test_graph_conditional_nested(init_cuda): +def test_graph_conditional_nested(init_cuda, pinned_alloc): """A conditional nested inside another conditional body exercises the multi-level body -> outer-body -> primary keep-alive chain.""" mod = compile_conditional_kernels(int) @@ -346,8 +341,7 @@ def test_graph_conditional_nested(init_cuda): set_handle = mod.get_kernel("set_handle") launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(4) + b = pinned_alloc(4) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 diff --git a/cuda_core/tests/graph/test_graph_definition_lifetime.py b/cuda_core/tests/graph/test_graph_definition_lifetime.py index d0cc71797d9..dd724ed16af 100644 --- a/cuda_core/tests/graph/test_graph_definition_lifetime.py +++ b/cuda_core/tests/graph/test_graph_definition_lifetime.py @@ -13,7 +13,12 @@ import pytest from cuda_python_test_helpers.subprocess_runner import run_python_snippet from helpers.graph_kernels import compile_common_kernels -from helpers.memory import VMM_HANDLE_TYPE, skip_if_managed_mempool_unsupported, skip_if_mempool_unsupported +from helpers.memory import ( + VMM_HANDLE_TYPE, + make_pinned_allocator, + skip_if_managed_mempool_unsupported, + skip_if_mempool_unsupported, +) from helpers.misc import try_create_condition from cuda_python_test_helpers import under_compute_sanitizer @@ -82,6 +87,7 @@ def _wait_until(predicate, timeout=None, interval=0.02): Kernel, LaunchConfig, LegacyPinnedMemoryResource, + PinnedMemoryResource, VirtualMemoryResource, VirtualMemoryResourceOptions, ) @@ -1410,7 +1416,36 @@ def test_graph_nodes_reconstructed_via_pred_chain(init_cuda, factory): # ============================================================================= -def test_kernel_args_buffer_lifetime(init_cuda): +_KERNEL_ARG_RESOURCES = (DeviceMemoryResource, LegacyPinnedMemoryResource, PinnedMemoryResource) + + +def _allocate_zeroed_kernel_arg(dev, resource_type): + size = ctypes.sizeof(ctypes.c_int) + if resource_type is DeviceMemoryResource: + skip_if_mempool_unsupported(dev) + stream = dev.default_stream + buf = resource_type(dev).allocate(size, stream=stream) + buf.fill(0, stream=stream) + stream.sync() + else: + buf = make_pinned_allocator(resource_type)(size) + ctypes.c_int.from_address(int(buf.handle)).value = 0 + return buf + + +def _read_kernel_arg(dptr, resource_type): + if resource_type is not DeviceMemoryResource: + return ctypes.c_int.from_address(dptr).value + + from cuda.core._utils.cuda_utils import driver, handle_return + + out = (ctypes.c_int * 1)(0) + handle_return(driver.cuMemcpyDtoH(out, dptr, ctypes.sizeof(ctypes.c_int))) + return out[0] + + +@pytest.mark.parametrize("resource_type", _KERNEL_ARG_RESOURCES, ids=lambda resource_type: resource_type.__name__) +def test_kernel_args_buffer_lifetime(init_cuda, resource_type): """Buffer passed as a kernel arg is kept alive by the graph, the kernel executes against its memory after the original Python ref drops, and the Buffer is released once the graph is destroyed. @@ -1424,15 +1459,9 @@ def test_kernel_args_buffer_lifetime(init_cuda): free-threaded Python the resulting Py_DECREF chain may need an extra GC pass to settle. """ - from cuda.core._utils.cuda_utils import driver, handle_return - - skip_if_mempool_unsupported() dev = Device() - mr = DeviceMemoryResource(dev) + buf = _allocate_zeroed_kernel_arg(dev, resource_type) add_one = compile_common_kernels().get_kernel("add_one") - buf = mr.allocate(ctypes.sizeof(ctypes.c_int), stream=dev.default_stream) - buf.fill(0, stream=dev.default_stream) - dev.default_stream.sync() buf_weak = weakref.ref(buf) dptr = int(buf.handle) @@ -1447,15 +1476,14 @@ def test_kernel_args_buffer_lifetime(init_cuda): g.instantiate().launch(stream) stream.sync() - out = (ctypes.c_int * 1)(0) - handle_return(driver.cuMemcpyDtoH(out, dptr, ctypes.sizeof(ctypes.c_int))) - assert out[0] == 1 + assert _read_kernel_arg(dptr, resource_type) == 1 del g _wait_until(lambda: buf_weak() is None) -def test_kernel_args_survive_graph_clone(init_cuda): +@pytest.mark.parametrize("resource_type", _KERNEL_ARG_RESOURCES, ids=lambda resource_type: resource_type.__name__) +def test_kernel_args_survive_graph_clone(init_cuda, resource_type): """Cloned graph keeps Buffer alive via CUDA user objects. A graph clone does not inherit Python-level references, so only user @@ -1463,32 +1491,29 @@ def test_kernel_args_survive_graph_clone(init_cuda): """ from cuda.core._utils.cuda_utils import driver, handle_return - skip_if_mempool_unsupported() dev = Device() - mr = DeviceMemoryResource(dev) + buf = _allocate_zeroed_kernel_arg(dev, resource_type) add_one = compile_common_kernels().get_kernel("add_one") - buf = mr.allocate(ctypes.sizeof(ctypes.c_int), stream=dev.default_stream) - buf.fill(0, stream=dev.default_stream) - dev.default_stream.sync() dptr = int(buf.handle) g = GraphDefinition() g.launch(LaunchConfig(grid=1, block=1), add_one, buf) cloned_cu_graph = handle_return(driver.cuGraphClone(driver.CUgraph(g.handle))) - del buf, g - gc.collect() - - graph_exec = handle_return(driver.cuGraphInstantiate(cloned_cu_graph, 0)) - stream = dev.create_stream() - handle_return(driver.cuGraphLaunch(graph_exec, driver.CUstream(int(stream.handle)))) - stream.sync() + try: + del buf, g + gc.collect() - out = (ctypes.c_int * 1)(0) - handle_return(driver.cuMemcpyDtoH(out, dptr, ctypes.sizeof(ctypes.c_int))) - assert out[0] == 1 - handle_return(driver.cuGraphExecDestroy(graph_exec)) - handle_return(driver.cuGraphDestroy(cloned_cu_graph)) + graph_exec = handle_return(driver.cuGraphInstantiate(cloned_cu_graph, 0)) + try: + stream = dev.create_stream() + handle_return(driver.cuGraphLaunch(graph_exec, driver.CUstream(int(stream.handle)))) + stream.sync() + assert _read_kernel_arg(dptr, resource_type) == 1 + finally: + handle_return(driver.cuGraphExecDestroy(graph_exec)) + finally: + handle_return(driver.cuGraphDestroy(cloned_cu_graph)) # ============================================================================= diff --git a/cuda_core/tests/graph/test_graph_definition_mutation.py b/cuda_core/tests/graph/test_graph_definition_mutation.py index 57b7a85b93c..df6b237fc99 100644 --- a/cuda_core/tests/graph/test_graph_definition_mutation.py +++ b/cuda_core/tests/graph/test_graph_definition_mutation.py @@ -39,7 +39,7 @@ class YRig: graph has exactly the topology we expect by checking the final value. """ - def __init__(self): + def __init__(self, pinned_alloc): self.A_OPS = [(2, 1), (3, 2), (5, 3)] self.B_OPS = [(2, 7), (3, 1)] @@ -48,8 +48,7 @@ def __init__(self): self.combine = mod.get_kernel("combine") self.config = LaunchConfig(grid=1, block=1) - self._mr = LegacyPinnedMemoryResource() - self._buf = self._mr.allocate(3 * 4) + self._buf = pinned_alloc(3 * 4) self._arr = np.from_dlpack(self._buf).view(np.int32) self.ptr_a = self._arr[0:].ctypes.data @@ -146,19 +145,19 @@ def close(self): class TestMutateYRig: """Tests that mutate the Y-shaped graph built by YRig.""" - def test_baseline(self, init_cuda): + def test_baseline(self, init_cuda, pinned_alloc): """Unmodified graph produces the expected results.""" - rig = YRig() + rig = YRig(pinned_alloc) rig.run() assert rig.output == rig.expected_output assert rig.edges == rig.initial_edges assert rig.nodes == rig.initial_nodes rig.close() - def test_destroy_a1(self, init_cuda): + def test_destroy_a1(self, init_cuda, pinned_alloc): """Destroy a1 (creates a race on arm a). Arm b yields the expected value, and the final step is correctly ordered after b completes.""" - rig = YRig() + rig = YRig(pinned_alloc) rig.a[1].destroy() rig.run() _, b_exp, _ = rig.expected_output @@ -169,9 +168,9 @@ def test_destroy_a1(self, init_cuda): assert rig.nodes == rig.initial_nodes - {a1} rig.close() - def test_destroy_a2(self, init_cuda): + def test_destroy_a2(self, init_cuda, pinned_alloc): """Destroy a2, connect a1--r""" - rig = YRig() + rig = YRig(pinned_alloc) rig.a[2].destroy() rig.a[1].succ.add(rig.r) rig.A_OPS.pop() @@ -182,9 +181,9 @@ def test_destroy_a2(self, init_cuda): assert rig.nodes == rig.initial_nodes - {a2} rig.close() - def test_destroy_joint(self, init_cuda): + def test_destroy_joint(self, init_cuda, pinned_alloc): """Remove the joining node and instead add edges directly to r.""" - rig = YRig() + rig = YRig(pinned_alloc) _, _, a2, _, b1, j, r = rig.a + rig.b + [rig.j, rig.r] j.destroy() r.pred = {a2, b1} @@ -194,9 +193,9 @@ def test_destroy_joint(self, init_cuda): assert rig.nodes == rig.initial_nodes - {j} rig.close() - def test_insert_b(self, init_cuda): + def test_insert_b(self, init_cuda, pinned_alloc): """Insert a node into arm b.""" - rig = YRig() + rig = YRig(pinned_alloc) coeffs = 5, 3 b_new = rig.graph_def.launch(rig.config, rig.affine, rig.ptr_b, *coeffs) b0, b1 = rig.b @@ -441,7 +440,7 @@ def test_self_edge(init_cuda): @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_convert_linear_to_fan_in(init_cuda): +def test_convert_linear_to_fan_in(init_cuda, pinned_alloc): """Chain four computations sequentially, then rewire so all pairs run in parallel feeding into a reduce node. @@ -461,8 +460,7 @@ def test_convert_linear_to_fan_in(init_cuda): reduce_kern = mod.get_kernel("reduce") config = LaunchConfig(grid=1, block=1) - mr = LegacyPinnedMemoryResource() - buf = mr.allocate(5 * 4) + buf = pinned_alloc(5 * 4) arr = np.from_dlpack(buf).view(np.int32) arr[:] = 0 diff --git a/cuda_core/tests/graph/test_graph_node_update.py b/cuda_core/tests/graph/test_graph_node_update.py index e8e977771d9..4b6d0b8b4cf 100644 --- a/cuda_core/tests/graph/test_graph_node_update.py +++ b/cuda_core/tests/graph/test_graph_node_update.py @@ -10,12 +10,21 @@ import weakref from collections.abc import Callable from dataclasses import dataclass +from functools import partial import pytest from helpers.graph_kernels import compile_common_kernels +from helpers.memory import make_pinned_allocator from helpers.nanosleep_kernel import NanosleepKernel -from cuda.core import Device, DeviceMemoryResource, EventOptions, LaunchConfig, LegacyPinnedMemoryResource +from cuda.core import ( + Device, + DeviceMemoryResource, + EventOptions, + LaunchConfig, + LegacyPinnedMemoryResource, + PinnedMemoryResource, +) from cuda.core._memory._synchronous_memory_resource import _SynchronousMemoryResource from cuda.core._utils._weak_handles import weak_handle from cuda.core._utils.cuda_utils import CUDAError, driver, handle_return @@ -421,14 +430,14 @@ def _memcpy_destination_case(device): return _memcpy_case(device, replace_operand="dst") -def _kernel_case(device, *, replace): +def _kernel_case(device, *, replace, resource_type): + pinned_alloc = make_pinned_allocator(resource_type) module = compile_common_kernels() add_one = module.get_kernel("add_one") empty_kernel = module.get_kernel("empty_kernel") write_launch_dims = module.get_kernel("write_launch_dims") - memory_resource = LegacyPinnedMemoryResource() - original_buffer = memory_resource.allocate(ctypes.sizeof(ctypes.c_int)) - replacement_buffer = memory_resource.allocate(ctypes.sizeof(ctypes.c_int)) if replace == "args" else original_buffer + original_buffer = pinned_alloc(ctypes.sizeof(ctypes.c_int)) + replacement_buffer = pinned_alloc(ctypes.sizeof(ctypes.c_int)) if replace == "args" else original_buffer original_config = LaunchConfig(grid=1, block=1) replacement_config = LaunchConfig(grid=2, block=3) if replace == "config" else original_config @@ -512,21 +521,6 @@ def invalid_update(): ) -def _kernel_config_case(device): - """Replace launch dimensions while preserving the kernel and arguments.""" - return _kernel_case(device, replace="config") - - -def _kernel_args_case(device): - """Replace arguments while preserving the kernel and configuration.""" - return _kernel_case(device, replace="args") - - -def _kernel_function_case(device): - """Replace a kernel and explicitly supply its coupled arguments.""" - return _kernel_case(device, replace="kernel") - - def _child_graph_case(device): """Replace the embedded clone while preserving existing executables.""" called = [] @@ -595,9 +589,14 @@ def assert_exec_uses(graph, expected): pytest.param(_memcpy_size_case, id="memcpy-size"), pytest.param(_memcpy_source_case, id="memcpy-source"), pytest.param(_memcpy_destination_case, id="memcpy-destination"), - pytest.param(_kernel_config_case, id="kernel-config"), - pytest.param(_kernel_args_case, id="kernel-args"), - pytest.param(_kernel_function_case, id="kernel-function"), + *( + pytest.param( + partial(_kernel_case, replace=replace, resource_type=resource_type), + id=f"kernel-{name}-{resource_type.__name__}", + ) + for replace, name in (("config", "config"), ("args", "args"), ("kernel", "function")) + for resource_type in (LegacyPinnedMemoryResource, PinnedMemoryResource) + ), pytest.param(_child_graph_case, id="child-graph"), ] ) @@ -1202,15 +1201,14 @@ def second_replacement(): @pytest.mark.agent_authored(model="gpt-5.6") -def test_rejected_executable_update_rolls_back_owners(init_cuda): +def test_rejected_executable_update_rolls_back_owners(init_cuda, pinned_alloc): if driver_version() < (12, 2, 0): pytest.skip("individual graph node updates require CUDA 12.2+") kernel = compile_common_kernels().get_kernel("add_one") config = LaunchConfig(grid=1, block=1) - memory_resource = LegacyPinnedMemoryResource() - active = memory_resource.allocate(ctypes.sizeof(ctypes.c_int)) - rejected = memory_resource.allocate(ctypes.sizeof(ctypes.c_int)) + active = pinned_alloc(ctypes.sizeof(ctypes.c_int)) + rejected = pinned_alloc(ctypes.sizeof(ctypes.c_int)) ctypes.c_int.from_address(int(active.handle)).value = 0 rejected_weak = weak_handle(rejected) @@ -1306,7 +1304,7 @@ def active(): @pytest.mark.agent_authored(model="gpt-5.6") -def test_inflight_launch_defers_replaced_executable_owners(init_cuda): +def test_inflight_launch_defers_replaced_executable_owners(init_cuda, pinned_alloc): if driver_version() < (12, 2, 0): pytest.skip("individual graph node updates require CUDA 12.2+") @@ -1319,10 +1317,10 @@ def blocking_callback(): kernel = compile_common_kernels().get_kernel("add_one") config = LaunchConfig(grid=1, block=1) - memory_resource = LegacyPinnedMemoryResource() - original = memory_resource.allocate(ctypes.sizeof(ctypes.c_int)) - inflight = memory_resource.allocate(ctypes.sizeof(ctypes.c_int)) - future = memory_resource.allocate(ctypes.sizeof(ctypes.c_int)) + original = pinned_alloc(ctypes.sizeof(ctypes.c_int)) + inflight = pinned_alloc(ctypes.sizeof(ctypes.c_int)) + future = pinned_alloc(ctypes.sizeof(ctypes.c_int)) + ctypes.c_int.from_address(int(inflight.handle)).value = 0 inflight_weak = weak_handle(inflight) source = GraphDefinition() @@ -1436,7 +1434,7 @@ def replacement(): @pytest.mark.agent_authored(model="claude-opus-5") -def test_closing_executable_during_launch_defers_owner_release(init_cuda): +def test_closing_executable_during_launch_defers_owner_release(init_cuda, pinned_alloc): if driver_version() < (12, 2, 0): pytest.skip("individual graph node updates require CUDA 12.2+") @@ -1449,9 +1447,9 @@ def blocking_callback(): kernel = compile_common_kernels().get_kernel("add_one") config = LaunchConfig(grid=1, block=1) - memory_resource = LegacyPinnedMemoryResource() - original = memory_resource.allocate(ctypes.sizeof(ctypes.c_int)) - inflight = memory_resource.allocate(ctypes.sizeof(ctypes.c_int)) + original = pinned_alloc(ctypes.sizeof(ctypes.c_int)) + inflight = pinned_alloc(ctypes.sizeof(ctypes.c_int)) + ctypes.c_int.from_address(int(inflight.handle)).value = 0 inflight_weak = weak_handle(inflight) source = GraphDefinition() diff --git a/cuda_core/tests/graph/test_graph_update.py b/cuda_core/tests/graph/test_graph_update.py index c95e331913a..352d11253ed 100644 --- a/cuda_core/tests/graph/test_graph_update.py +++ b/cuda_core/tests/graph/test_graph_update.py @@ -12,7 +12,7 @@ from cuda_python_test_helpers.marks import requires_module from helpers.graph_kernels import compile_common_kernels, compile_conditional_kernels -from cuda.core import Device, LaunchConfig, LegacyPinnedMemoryResource, launch +from cuda.core import Device, LaunchConfig, launch from cuda.core._utils.cuda_utils import CUDAError from cuda.core.graph import GraphDefinition @@ -28,14 +28,13 @@ def _wait_until(predicate, timeout=5.0): @pytest.mark.parametrize("builder", ["GraphBuilder", "GraphDefinition"]) @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_graph_update_kernel_args(init_cuda, builder): +def test_graph_update_kernel_args(init_cuda, pinned_alloc, builder): """Update redirects a kernel to write to a different pointer.""" mod = compile_common_kernels() add_one = mod.get_kernel("add_one") launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(8) + b = pinned_alloc(8) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 arr[1] = 0 @@ -74,14 +73,13 @@ def build(ptr): @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_graph_update_conditional(init_cuda): +def test_graph_update_conditional(init_cuda, pinned_alloc): """Update swaps conditional switch graphs with matching topology.""" mod = compile_conditional_kernels(int) add_one = mod.get_kernel("add_one") launch_stream = Device().create_stream() - mr = LegacyPinnedMemoryResource() - b = mr.allocate(12) + b = pinned_alloc(12) arr = np.from_dlpack(b).view(np.int32) arr[0] = 0 arr[1] = 0 diff --git a/cuda_core/tests/helpers/latch.py b/cuda_core/tests/helpers/latch.py index 7702c5f617e..a7f33e46f01 100644 --- a/cuda_core/tests/helpers/latch.py +++ b/cuda_core/tests/helpers/latch.py @@ -8,7 +8,6 @@ import helpers from cuda.core import ( LaunchConfig, - LegacyPinnedMemoryResource, Program, ProgramOptions, launch, @@ -20,7 +19,7 @@ class LatchKernel: Manages a kernel that blocks stream progress until released. """ - def __init__(self, device, timeout_sec=60): + def __init__(self, device, *, pinned_alloc, timeout_sec=60): if helpers.CUDA_INCLUDE_PATH is None: pytest.skip("need CUDA header") code = """ @@ -58,8 +57,7 @@ def __init__(self, device, timeout_sec=60): mod = prog.compile(target_type="cubin") self.kernel = mod.get_kernel("latch") - mr = LegacyPinnedMemoryResource() - self.buffer = mr.allocate(4) + self.buffer = pinned_alloc(4) self.busy_wait_flag[0] = 0 clock_rate_hz = device.properties.clock_rate * 1000 self.timeout_cycles = int(timeout_sec * clock_rate_hz) diff --git a/cuda_core/tests/helpers/memory.py b/cuda_core/tests/helpers/memory.py index 77a879189a9..47f82b588ea 100644 --- a/cuda_core/tests/helpers/memory.py +++ b/cuda_core/tests/helpers/memory.py @@ -3,9 +3,18 @@ """Memory-related test helpers (skip guards and resource factories).""" +from contextlib import closing + import pytest -from cuda.core import Device, DeviceMemoryResource, ManagedMemoryResource, PinnedMemoryResource +from cuda.core import ( + Device, + DeviceMemoryResource, + LegacyPinnedMemoryResource, + ManagedMemoryResource, + PinnedMemoryResource, + StreamOptions, +) from cuda.core._utils.cuda_utils import CUDAError from cuda_python_test_helpers import IS_WINDOWS @@ -56,6 +65,24 @@ def skip_if_pinned_memory_unsupported(device): pytest.skip("PinnedMemoryResource requires CUDA 13.0 or later") +def make_pinned_allocator(resource_type): + """Return an allocator whose buffers are ready for immediate host access.""" + if resource_type is LegacyPinnedMemoryResource: + return resource_type().allocate + + skip_if_pinned_memory_unsupported(Device()) + resource = resource_type() + + def allocate(size): + # Use a nonblocking stream so setup can run alongside independent captures. + with closing(Device().create_stream(options=StreamOptions(nonblocking=True))) as stream: + buffer = resource.allocate(size, stream=stream) + stream.sync() + return buffer + + return allocate + + def skip_if_managed_memory_unsupported(device): try: if not device.properties.memory_pools_supported or not device.properties.concurrent_managed_access: diff --git a/cuda_core/tests/memory_ipc/test_event_ipc.py b/cuda_core/tests/memory_ipc/test_event_ipc.py index 90f05f1e7d7..929a6248923 100644 --- a/cuda_core/tests/memory_ipc/test_event_ipc.py +++ b/cuda_core/tests/memory_ipc/test_event_ipc.py @@ -26,7 +26,7 @@ class TestEventIpc: """Check the basic usage of IPC-enabled events with a latch kernel.""" @pytest.mark.flaky(reruns=2) - def test_main(self, ipc_device, ipc_memory_resource): + def test_main(self, ipc_device, ipc_memory_resource, pinned_alloc): log = TimestampedLogger(prefix="parent: ", enabled=ENABLE_LOGGING) device = ipc_device mr = ipc_memory_resource @@ -36,7 +36,7 @@ def test_main(self, ipc_device, ipc_memory_resource): # in which case the call to buffer.copy_from(...) below is a synchronous # operation that blocks the host. But calling the latch kernel here does not # make any sense. We should refactor this test. - latch = LatchKernel(device, timeout_sec=5) + latch = LatchKernel(device, pinned_alloc=pinned_alloc, timeout_sec=5) # Start the child process. q_out, q_in = [mp.Queue() for _ in range(2)] diff --git a/cuda_core/tests/test_event.py b/cuda_core/tests/test_event.py index 4be5f8b94c5..f894dd1213b 100644 --- a/cuda_core/tests/test_event.py +++ b/cuda_core/tests/test_event.py @@ -127,9 +127,9 @@ def test_error_timing_recorded(): @pytest.mark.skipif(Device().compute_capability.major < 7, reason="__nanosleep is only available starting Volta (sm70)") @pytest.mark.thread_unsafe(reason="requires a barrier wait to avoid overlapping pinned latch allocations") @pytest.mark.agent_authored(model="claude-opus-5-5") -def test_error_timing_incomplete(init_cuda): +def test_error_timing_incomplete(init_cuda, pinned_alloc): device = init_cuda - latch = LatchKernel(device, timeout_sec=_LATCH_TIMEOUT_SEC) + latch = LatchKernel(device, pinned_alloc=pinned_alloc, timeout_sec=_LATCH_TIMEOUT_SEC) enabled = EventOptions(timing_enabled=True) stream = device.create_stream() @@ -253,10 +253,10 @@ def test_event_ipc_descriptor_non_ipc(init_cuda): @pytest.mark.skipif(Device().compute_capability.major < 7, reason="__nanosleep is only available starting Volta (sm70)") @pytest.mark.thread_unsafe(reason="requires a barrier wait to avoid overlapping pinned latch allocations") -def test_event_is_done_false(init_cuda): +def test_event_is_done_false(init_cuda, pinned_alloc): """Event.is_done returns False when captured work has not yet completed.""" device = Device() - latch = LatchKernel(device) + latch = LatchKernel(device, pinned_alloc=pinned_alloc) stream = device.create_stream() latch.launch(stream) event = stream.record() diff --git a/cuda_core/tests/test_green_context.py b/cuda_core/tests/test_green_context.py index f9503707701..5f1df257854 100644 --- a/cuda_core/tests/test_green_context.py +++ b/cuda_core/tests/test_green_context.py @@ -2,6 +2,9 @@ # # SPDX-License-Identifier: Apache-2.0 +import ctypes +from contextlib import closing + import numpy as np import pytest from helpers.contexts import assert_device_operations_use_bound_context, current_context_handle, use_context @@ -746,6 +749,17 @@ def test_launch_and_verify(self, init_cuda, green_ctx, fill_kernel): stream = green_ctx.create_stream() _launch_fill_and_verify(init_cuda, stream, fill_kernel, n=64, value=42) + @pytest.mark.agent_authored(model="gpt-6") + def test_launch_with_pinned_output(self, init_cuda, green_ctx, fill_kernel, pinned_alloc): + """A kernel launched in a green context can write to pinned host memory.""" + n = 64 + with closing(green_ctx.create_stream()) as stream, pinned_alloc(n * ctypes.sizeof(ctypes.c_int)) as buffer: + result = (ctypes.c_int * n).from_address(int(buffer.handle)) + result[:] = [0] * n + launch(stream, LaunchConfig(grid=2, block=32), fill_kernel, buffer, np.int32(42), np.int32(n)) + stream.sync() + assert result[:] == [42] * n + def test_two_green_contexts_independent(self, init_cuda, sm_resource, fill_kernel): """Two SM groups -> two green contexts -> two independent kernels.""" dev = init_cuda diff --git a/cuda_core/tests/test_helpers.py b/cuda_core/tests/test_helpers.py index e2ec82d8503..340b2c6d911 100644 --- a/cuda_core/tests/test_helpers.py +++ b/cuda_core/tests/test_helpers.py @@ -34,7 +34,7 @@ @pytest.mark.skipif(Device().compute_capability.major < 7, reason="__nanosleep is only available starting Volta (sm70)") @pytest.mark.thread_unsafe(reason="requires a barrier wait to avoid overlapping pinned latch allocations") -def test_latchkernel(init_cuda): +def test_latchkernel(init_cuda, pinned_alloc): """Test LatchKernel.""" log = TimestampedLogger(enabled=ENABLE_LOGGING) log("begin") @@ -43,7 +43,7 @@ def test_latchkernel(init_cuda): target = make_scratch_buffer(device, 0, NBYTES) zeros = make_scratch_buffer(device, 0, NBYTES) ones = make_scratch_buffer(device, 1, NBYTES) - latch = LatchKernel(device) + latch = LatchKernel(device, pinned_alloc=pinned_alloc) log("launching latch kernel") latch.launch(stream) log("launching copy (0->1) kernel") diff --git a/cuda_core/tests/test_launcher.py b/cuda_core/tests/test_launcher.py index c01bf9103f1..d914ab4eadc 100644 --- a/cuda_core/tests/test_launcher.py +++ b/cuda_core/tests/test_launcher.py @@ -5,6 +5,7 @@ import helpers from cuda_python_test_helpers.marks import requires_module, skipif_need_cuda_headers +from helpers.memory import make_pinned_allocator from helpers.misc import StreamWrapper try: @@ -19,6 +20,7 @@ DeviceMemoryResource, LaunchConfig, LegacyPinnedMemoryResource, + PinnedMemoryResource, Program, ProgramOptions, StreamOptions, @@ -244,7 +246,7 @@ def test_to_native_launch_config_priority(init_cuda, priority, expected_num_attr @skipif_need_cuda_headers -def test_pdl_primary_secondary_overlap_same_stream(init_cuda): +def test_pdl_primary_secondary_overlap_same_stream(init_cuda, pinned_alloc): """Primary + secondary PDL launch on one stream can overlap on Hopper+. Secondary is launched with ``programmatic_stream_serialization=True``. After @@ -294,9 +296,8 @@ def test_pdl_primary_secondary_overlap_same_stream(init_cuda): primary = mod.get_kernel("primary_kernel") secondary = mod.get_kernel("secondary_kernel") - mr = LegacyPinnedMemoryResource() - secondary_started = np.from_dlpack(mr.allocate(4)).view(np.int32) - overlapped = np.from_dlpack(mr.allocate(4)).view(np.int32) + secondary_started = np.from_dlpack(pinned_alloc(4)).view(np.int32) + overlapped = np.from_dlpack(pinned_alloc(4)).view(np.int32) primary_cfg = LaunchConfig(grid=1, block=1) secondary_cfg = LaunchConfig(grid=1, block=1, programmatic_stream_serialization=True) @@ -605,12 +606,11 @@ def write_scalar_module(): @pytest.mark.parametrize("python_type, cpp_type, init_value", PARAMS) @requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)") -def test_launch_scalar_argument(init_cuda, write_scalar_module, python_type, cpp_type, init_value): +def test_launch_scalar_argument(init_cuda, pinned_alloc, write_scalar_module, python_type, cpp_type, init_value): dev = init_cuda # Prepare pinned host array - mr = LegacyPinnedMemoryResource() - b = mr.allocate(np.dtype(python_type).itemsize) + b = pinned_alloc(np.dtype(python_type).itemsize) arr = np.from_dlpack(b).view(python_type) arr[:] = 0 @@ -674,14 +674,17 @@ def test_cooperative_launch(): s.sync() -@pytest.mark.skipif(cp is None, reason="cupy not installed") @pytest.mark.parametrize( "memory_resource_class", [ - "device_memory_resource", # kludgy, but can go away after #726 is resolved - pytest.param( - LegacyPinnedMemoryResource, - marks=requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)"), + pytest.param("device_memory_resource", marks=pytest.mark.skipif(cp is None, reason="cupy not installed")), + *( + pytest.param( + resource_type, + marks=requires_module(np, "2.2.5", reason="need numpy 2.2.5+ (numpy GH #28632)"), + id=resource_type.__name__, + ) + for resource_type in (LegacyPinnedMemoryResource, PinnedMemoryResource) ), ], ) @@ -689,9 +692,12 @@ def test_launch_with_buffers_allocated_by_memory_resource(init_cuda, memory_reso """Test that kernels can access memory allocated by memory resources.""" dev = Device() dev.set_current() + is_host = memory_resource_class != "device_memory_resource" + if is_host: + pinned_alloc = make_pinned_allocator(memory_resource_class) stream = dev.create_stream() - # tell CuPy to use our stream as the current stream: - cp.cuda.ExternalStream(int(stream.handle)).use() + if not is_host: + cp.cuda.ExternalStream(int(stream.handle)).use() # Kernel that operates on memory code = """ @@ -712,39 +718,27 @@ def test_launch_with_buffers_allocated_by_memory_resource(init_cuda, memory_reso mod = prog.compile("cubin") kernel = mod.get_kernel("memory_ops") - # Create memory resource - if memory_resource_class == "device_memory_resource": - if dev.properties.memory_pools_supported: - mr = DeviceMemoryResource(dev.device_id) - else: - mr = _SynchronousMemoryResource(dev.device_id) - name = memory_resource_class - else: - mr = memory_resource_class() - name = str(mr) - # Allocate memory size = 1024 dtype = np.float32 element_size = dtype().itemsize total_size = size * element_size - buffer = mr.allocate(total_size, stream=stream) - - # Create array view based on memory type - if mr.is_host_accessible: - # For pinned memory, use numpy - array = np.from_dlpack(buffer).view(dtype=dtype) + if is_host: + buffer = pinned_alloc(total_size) + name = memory_resource_class.__name__ else: - array = cp.from_dlpack(buffer).view(dtype=dtype) + if dev.properties.memory_pools_supported: + mr = DeviceMemoryResource(dev.device_id) + else: + mr = _SynchronousMemoryResource(dev.device_id) + buffer = mr.allocate(total_size, stream=stream) + name = memory_resource_class - # Initialize data with random values - if mr.is_host_accessible: - rng = np.random.default_rng() - array[:] = rng.random(size, dtype=dtype) - else: - rng = cp.random.default_rng() - array[:] = rng.random(size, dtype=dtype) + xp = np if is_host else cp + array = xp.from_dlpack(buffer).view(dtype=dtype) + rng = xp.random.default_rng() + array[:] = rng.random(size, dtype=dtype) # Store original values for verification original = array.copy() @@ -761,13 +755,14 @@ def test_launch_with_buffers_allocated_by_memory_resource(init_cuda, memory_reso stream.sync() # Verify kernel operations - assert cp.allclose(array, original * 3.0), f"{name} operation failed" + assert xp.allclose(array, original * 3.0), f"{name} operation failed" # Clean up buffer.close(stream) stream.close() - cp.cuda.Stream.null.use() # reset CuPy's current stream to the null stream + if not is_host: + cp.cuda.Stream.null.use() # Verify buffer is properly closed assert buffer.handle == 0, f"{name} buffer should be closed" @@ -837,7 +832,7 @@ class MyBool(ctypes.c_bool): "numpy_float32", ], ) -def test_launch_scalar_argument_subclass_fallback(init_cuda, base_type, np_dtype, cpp_type, raw_value): +def test_launch_scalar_argument_subclass_fallback(init_cuda, pinned_alloc, base_type, np_dtype, cpp_type, raw_value): """Subclassed scalar arguments survive fallback handling and reach the kernel with the correct width/sign. The readback value (not just ptr != 0) guards each fallback branch against marshalling the wrong C type, e.g. uint64 -> uint32_t.""" @@ -850,8 +845,7 @@ class Subclassed(base_type): dev = init_cuda - mr = LegacyPinnedMemoryResource() - b = mr.allocate(np.dtype(np_dtype).itemsize) + b = pinned_alloc(np.dtype(np_dtype).itemsize) arr = np.from_dlpack(b).view(np_dtype) arr[:] = 0 @@ -951,7 +945,7 @@ class MyComplex(complex): ) @pytest.mark.agent_authored(model="gpt-5.6-sol") def test_launch_numpy_scalar_subclass_fallback( - init_cuda, write_scalar_module, base_type, np_dtype, cpp_type, raw_value + init_cuda, pinned_alloc, write_scalar_module, base_type, np_dtype, cpp_type, raw_value ): """Subclassed numpy scalars take prepare_numpy_arg's isinstance fallback and reach the kernel (readback).""" @@ -963,8 +957,7 @@ class Subclassed(base_type): dev = init_cuda - mr = LegacyPinnedMemoryResource() - b = mr.allocate(np.dtype(np_dtype).itemsize) + b = pinned_alloc(np.dtype(np_dtype).itemsize) arr = np.from_dlpack(b).view(np_dtype) arr[:] = 0 @@ -994,9 +987,8 @@ def _compile_write_ull_kernel(dev): return prog.compile("cubin", name_expressions=("write_ull",)).get_kernel("write_ull") -def _assert_kernel_sees_ull(dev, kernel_arg, expected): - mr = LegacyPinnedMemoryResource() - buf = mr.allocate(np.dtype(np.uint64).itemsize) +def _assert_kernel_sees_ull(dev, pinned_alloc, kernel_arg, expected): + buf = pinned_alloc(np.dtype(np.uint64).itemsize) try: arr = np.from_dlpack(buf).view(np.uint64) arr[:] = 0 @@ -1011,7 +1003,7 @@ def _assert_kernel_sees_ull(dev, kernel_arg, expected): @pytest.mark.parametrize("use_subclass", [False, True], ids=["exact_type", "subclass_fallback"]) @pytest.mark.agent_authored(model="gpt-5.6-sol") -def test_launch_graph_conditional_handle_as_kernel_arg(init_cuda, use_subclass): +def test_launch_graph_conditional_handle_as_kernel_arg(init_cuda, pinned_alloc, use_subclass): """CUgraphConditionalHandle is packed as its uint64 value (readback).""" from cuda.bindings import driver @@ -1023,7 +1015,7 @@ class SubclassedHandle(driver.CUgraphConditionalHandle): dev = Device() dev.set_current() - _assert_kernel_sees_ull(dev, handle, _UINT64_HANDLE_VALUE) + _assert_kernel_sees_ull(dev, pinned_alloc, handle, _UINT64_HANDLE_VALUE) @pytest.mark.parametrize("allocation", ["managed", "virtual"]) diff --git a/cuda_core/tests/test_linker.py b/cuda_core/tests/test_linker.py index 5b0bc0a9cbc..fb3f56d2ad7 100644 --- a/cuda_core/tests/test_linker.py +++ b/cuda_core/tests/test_linker.py @@ -2,16 +2,15 @@ # # SPDX-License-Identifier: Apache-2.0 +import ctypes import inspect import warnings -import numpy as np import pytest from cuda.core import ( Device, LaunchConfig, - LegacyPinnedMemoryResource, Linker, LinkerOptions, Program, @@ -626,25 +625,16 @@ def _compile_incremental_inputs(target_type): return caller, helper -def _launch_incrementally_linked_kernel(device, linked_code): +def _launch_incrementally_linked_kernel(device, linked_code, pinned_alloc): kernel = linked_code.get_kernel("incremental_kernel") stream = device.create_stream() try: - with LegacyPinnedMemoryResource().allocate(4) as host_buffer: - result = np.from_dlpack(host_buffer).view(np.int32) - try: - with device.memory_resource.allocate(4, stream=stream) as device_buffer: - result[:] = 0 - - launch(stream, LaunchConfig(grid=1, block=1), kernel, device_buffer) - device_buffer.copy_to(host_buffer, stream=stream) - stream.sync() - actual = int(result[0]) - finally: - # Drop the DLPack view before releasing its pinned allocation. - result = None - - assert actual == 42 + with pinned_alloc(ctypes.sizeof(ctypes.c_int)) as host_buffer: + result = ctypes.c_int.from_address(int(host_buffer.handle)) + result.value = 0 + launch(stream, LaunchConfig(grid=1, block=1), kernel, host_buffer) + stream.sync() + assert result.value == 42 finally: try: stream.sync() @@ -657,7 +647,7 @@ def _launch_incrementally_linked_kernel(device, linked_code): is_culink_backend or nvjitlink_version < (13, 2), reason="incremental linking requires nvJitLink 13.2 or newer", ) -def test_incremental_cubin_round_trip(init_cuda): +def test_incremental_cubin_round_trip(init_cuda, pinned_alloc): caller, helper = _compile_incremental_inputs("ptx") partial = Linker(caller, options=LinkerOptions(arch=ARCH, incremental=True)).link("cubin") @@ -669,10 +659,10 @@ def test_incremental_cubin_round_trip(init_cuda): options=LinkerOptions(arch=ARCH, incremental=True), ).link("cubin") assert resolved_partial.code_type == "cubin" - _launch_incrementally_linked_kernel(init_cuda, resolved_partial) + _launch_incrementally_linked_kernel(init_cuda, resolved_partial, pinned_alloc) final = Linker(resolved_partial, options=LinkerOptions(arch=ARCH)).link("cubin") - _launch_incrementally_linked_kernel(init_cuda, final) + _launch_incrementally_linked_kernel(init_cuda, final, pinned_alloc) @pytest.mark.agent_authored(model="gpt-5.6") @@ -680,7 +670,7 @@ def test_incremental_cubin_round_trip(init_cuda): is_culink_backend or nvjitlink_version < (13, 3), reason="linked LTOIR output requires nvJitLink 13.3 or newer and matching cuda-bindings", ) -def test_incremental_ltoir_round_trip(init_cuda): +def test_incremental_ltoir_round_trip(init_cuda, pinned_alloc): caller, helper = _compile_incremental_inputs("ltoir") incremental_options = LinkerOptions( arch=ARCH, @@ -698,7 +688,7 @@ def test_incremental_ltoir_round_trip(init_cuda): resolved_partial, options=LinkerOptions(arch=ARCH, link_time_optimization=True), ).link("cubin") - _launch_incrementally_linked_kernel(init_cuda, final) + _launch_incrementally_linked_kernel(init_cuda, final, pinned_alloc) @pytest.mark.agent_authored(model="gpt-5.6") @@ -706,7 +696,7 @@ def test_incremental_ltoir_round_trip(init_cuda): is_culink_backend or nvjitlink_version < (13, 3), reason="linked LTOIR output requires nvJitLink 13.3 or newer and matching cuda-bindings", ) -def test_complete_ltoir_round_trip_matches_direct_cubin(init_cuda): +def test_complete_ltoir_round_trip_matches_direct_cubin(init_cuda, pinned_alloc): caller, helper = _compile_incremental_inputs("ltoir") options = LinkerOptions(arch=ARCH, link_time_optimization=True) @@ -716,7 +706,7 @@ def test_complete_ltoir_round_trip_matches_direct_cubin(init_cuda): assert linked_ltoir.code_type == "ltoir" assert round_trip_cubin.code == direct_cubin.code - _launch_incrementally_linked_kernel(init_cuda, round_trip_cubin) + _launch_incrementally_linked_kernel(init_cuda, round_trip_cubin, pinned_alloc) @pytest.mark.agent_authored(model="gpt-5.6") @@ -724,7 +714,7 @@ def test_complete_ltoir_round_trip_matches_direct_cubin(init_cuda): is_culink_backend or nvjitlink_version < (13, 2), reason="incremental linking requires nvJitLink 13.2 or newer", ) -def test_incremental_lto_cubin_round_trip(init_cuda): +def test_incremental_lto_cubin_round_trip(init_cuda, pinned_alloc): caller, helper = _compile_incremental_inputs("ltoir") partial = Linker( caller, @@ -744,7 +734,7 @@ def test_incremental_lto_cubin_round_trip(init_cuda): helper, options=LinkerOptions(arch=ARCH, link_time_optimization=True), ).link("cubin") - _launch_incrementally_linked_kernel(init_cuda, final) + _launch_incrementally_linked_kernel(init_cuda, final, pinned_alloc) @pytest.mark.agent_authored(model="gpt-5.6") diff --git a/cuda_core/tests/test_module.py b/cuda_core/tests/test_module.py index 0e2ac833124..a4d1f6727b8 100644 --- a/cuda_core/tests/test_module.py +++ b/cuda_core/tests/test_module.py @@ -502,7 +502,7 @@ def test_object_code_load_rdc_from_file(kind, from_fn, suffix, tmp_path): ("library", ObjectCode.from_library), ], ) -def test_object_code_load_rdc_with_linker(kind, from_fn, init_cuda): +def test_object_code_load_rdc_with_linker(kind, from_fn, init_cuda, pinned_alloc): arch = f"sm_{init_cuda.arch}" kernel_code = Program( r""" @@ -522,24 +522,21 @@ def test_object_code_load_rdc_with_linker(kind, from_fn, init_cuda): kernel = linked.get_kernel("linked_kernel") stream = init_cuda.create_stream() - host_buf = cuda.core.LegacyPinnedMemoryResource().allocate(4) - result = np.from_dlpack(host_buf).view(np.float32) - result[:] = 0.0 - dev_buf = init_cuda.memory_resource.allocate(4, stream=stream) - - cuda.core.launch( - stream, - cuda.core.LaunchConfig(grid=1, block=1), - kernel, - np.float32(2.0), - np.float32(3.0), - np.float32(4.0), - dev_buf, - ) - dev_buf.copy_to(host_buf, stream=stream) - stream.sync() - - assert result[0] == 10.0 + with pinned_alloc(ctypes.sizeof(ctypes.c_float)) as host_buf: + result = ctypes.c_float.from_address(int(host_buf.handle)) + result.value = 0.0 + + cuda.core.launch( + stream, + cuda.core.LaunchConfig(grid=1, block=1), + kernel, + np.float32(2.0), + np.float32(3.0), + np.float32(4.0), + host_buf, + ) + stream.sync() + assert result.value == 10.0 def test_saxpy_arguments(get_saxpy_kernel_cubin, cuda12_4_prerequisite_check): @@ -870,12 +867,10 @@ def test_kernel_from_handle_foreign_kernel(init_cuda): assert k.attributes.max_threads_per_block > 0 -def test_kernel_keeps_library_alive(init_cuda): +def test_kernel_keeps_library_alive(init_cuda, pinned_alloc): """Test that a Kernel keeps its underlying library alive after ObjectCode goes out of scope.""" import gc - import numpy as np - def get_kernel_only(): """Return a kernel, letting ObjectCode go out of scope.""" code = """ @@ -904,23 +899,14 @@ def get_kernel_only(): device = Device() stream = device.create_stream() - # Allocate pinned host buffer and device buffer - pinned_mr = cuda.core.LegacyPinnedMemoryResource() - host_buf = pinned_mr.allocate(4) # sizeof(int) - result = np.from_dlpack(host_buf).view(np.int32) - result[:] = 0 - - dev_buf = device.memory_resource.allocate(4, stream=stream) - - # Launch kernel - config = cuda.core.LaunchConfig(grid=1, block=1) - cuda.core.launch(stream, config, kernel, dev_buf) - - # Copy result back to host - dev_buf.copy_to(host_buf, stream=stream) - stream.sync() + with pinned_alloc(ctypes.sizeof(ctypes.c_int)) as host_buf: + result = ctypes.c_int.from_address(int(host_buf.handle)) + result.value = 0 - assert result[0] == 42, f"Expected 42, got {result[0]}" + config = cuda.core.LaunchConfig(grid=1, block=1) + cuda.core.launch(stream, config, kernel, host_buf) + stream.sync() + assert result.value == 42 @pytest.mark.agent_authored(model="gpt-6-sol") diff --git a/cuda_core/tests/test_texture_surface.py b/cuda_core/tests/test_texture_surface.py index f04f182d7c0..f5dd4aba7c4 100644 --- a/cuda_core/tests/test_texture_surface.py +++ b/cuda_core/tests/test_texture_surface.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +import ctypes import gc import numpy as np @@ -1330,13 +1331,11 @@ def test_surface_invalid_resource_then_live_texture_lifecycle(init_cuda): @pytest.mark.agent_authored(model="gpt-6-sol") -def test_texture_border_color_is_sampled_on_device(init_cuda): +def test_texture_border_color_is_sampled_on_device(init_cuda, pinned_alloc): """Out-of-bounds texture reads return the configured RGBA border color.""" from contextlib import closing - from cuda.bindings import driver from cuda.core import LaunchConfig, Program, ProgramOptions, launch - from cuda.core._utils.cuda_utils import handle_return code = r""" extern "C" __global__ void sample_border(unsigned long long texture, float* result) { @@ -1354,14 +1353,13 @@ def test_texture_border_color_is_sampled_on_device(init_cuda): border = (0.25, 0.5, 0.75, 1.0) texel = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32) source = np.tile(texel, (2, 2, 1)) - actual = np.empty(8, dtype=np.float32) with ( closing(Program(code, "c++", options=ProgramOptions(std="c++17", arch=f"sm_{init_cuda.arch}"))) as program, closing(init_cuda.create_stream()) as stream, init_cuda.create_opaque_array( OpaqueArrayOptions(shape=(2, 2), format=ArrayFormatType.FLOAT32, num_channels=4) ) as array, - init_cuda.allocate(actual.nbytes, stream=stream) as output, + pinned_alloc(8 * ctypes.sizeof(ctypes.c_float)) as output, ): module = program.compile("cubin") kernel = module.get_kernel("sample_border") @@ -1375,8 +1373,8 @@ def test_texture_border_color_is_sampled_on_device(init_cuda): ), ) as texture: launch(stream, LaunchConfig(grid=1, block=2), kernel, np.uint64(texture.handle), output) - handle_return(driver.cuMemcpyDtoHAsync(actual.ctypes.data, output.handle, actual.nbytes, stream.handle)) stream.sync() + actual = np.ctypeslib.as_array((ctypes.c_float * 8).from_address(int(output.handle))) np.testing.assert_array_equal(actual[:4], np.asarray(border, dtype=np.float32)) np.testing.assert_array_equal(actual[4:], texel)