Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cuda_core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from helpers.memory import (
BaselineMemoryResourceReleasedWarning,
UnreleasedMemoryResourceWarning,
make_pinned_allocator,
skip_if_pinned_memory_unsupported,
)

Expand All @@ -47,6 +48,7 @@
Device,
DeviceMemoryResource,
DeviceMemoryResourceOptions,
LegacyPinnedMemoryResource,
ManagedMemoryResource,
ManagedMemoryResourceOptions,
PinnedMemoryResource,
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 4 additions & 7 deletions cuda_core/tests/graph/test_device_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from cuda.core import (
Device,
LaunchConfig,
LegacyPinnedMemoryResource,
Linker,
LinkerOptions,
ObjectCode,
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
38 changes: 15 additions & 23 deletions cuda_core/tests/graph/test_graph_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
CUDAWarning,
Device,
LaunchConfig,
LegacyPinnedMemoryResource,
Program,
ProgramOptions,
StreamOptions,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
32 changes: 13 additions & 19 deletions cuda_core/tests/graph/test_graph_builder_conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@
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


@pytest.mark.parametrize(
"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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -245,16 +242,15 @@ 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")
empty_kernel = mod.get_kernel("empty_kernel")

# 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

Expand Down Expand Up @@ -293,16 +289,15 @@ 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)
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

Expand Down Expand Up @@ -338,16 +333,15 @@ 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)
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

Expand Down
Loading
Loading