From 35e7b748421116b89815426354ecdbeb45411556 Mon Sep 17 00:00:00 2001 From: Jinfeng Date: Thu, 27 Aug 2026 23:05:47 +0000 Subject: [PATCH 1/4] raise CUDAError instead of RuntimeError to fix a test case failure due to Tegra gpu not supporting a cuda graph feature --- cuda_core/cuda/core/graph/_graph_builder.pyx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cuda_core/cuda/core/graph/_graph_builder.pyx b/cuda_core/cuda/core/graph/_graph_builder.pyx index 07973db30c4..c1373d9fea8 100644 --- a/cuda_core/cuda/core/graph/_graph_builder.pyx +++ b/cuda_core/cuda/core/graph/_graph_builder.pyx @@ -176,6 +176,7 @@ class GraphCompleteOptions: def _instantiate_graph(source, options: GraphCompleteOptions | None = None) -> Graph: cdef GraphHandle h_graph cdef GraphExecHandle h_exec + cdef cydriver.CUresult status if isinstance(source, GraphBuilder): GB_check_open(source) @@ -209,7 +210,10 @@ def _instantiate_graph(source, options: GraphCompleteOptions | None = None) -> G # The exec is adopted only when result_out reports success, so the # diagnostics below run before the handle is checked. h_exec = create_graph_exec_handle(h_graph, ¶ms) + status = get_last_error() if params.result_out == driver.CUgraphInstantiateResult.CUDA_GRAPH_INSTANTIATE_ERROR: + # HANDLE_RETURN raises CUDAError when status is not CUDA_SUCCESS. + HANDLE_RETURN(status) raise RuntimeError( "Instantiation failed for an unexpected reason which is described in the return value of the function." ) @@ -230,7 +234,7 @@ def _instantiate_graph(source, options: GraphCompleteOptions | None = None) -> G raise RuntimeError(f"Graph instantiation failed with unexpected error code: {params.result_out}") if as_cu(h_exec) == NULL: - HANDLE_RETURN(get_last_error()) + HANDLE_RETURN(status) return Graph._init(h_exec) From 6aa629f4aa681f249f921bec0610c8027c2f74c5 Mon Sep 17 00:00:00 2001 From: Jinfeng Date: Thu, 27 Aug 2026 23:11:12 +0000 Subject: [PATCH 2/4] add docstring saying HANDLE_RETURN adds CUresult message into CUDAError so that the message is not swallowed by RuntimeError --- cuda_core/cuda/core/graph/_graph_builder.pyx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cuda_core/cuda/core/graph/_graph_builder.pyx b/cuda_core/cuda/core/graph/_graph_builder.pyx index c1373d9fea8..8af3de6b275 100644 --- a/cuda_core/cuda/core/graph/_graph_builder.pyx +++ b/cuda_core/cuda/core/graph/_graph_builder.pyx @@ -212,7 +212,8 @@ def _instantiate_graph(source, options: GraphCompleteOptions | None = None) -> G h_exec = create_graph_exec_handle(h_graph, ¶ms) status = get_last_error() if params.result_out == driver.CUgraphInstantiateResult.CUDA_GRAPH_INSTANTIATE_ERROR: - # HANDLE_RETURN raises CUDAError when status is not CUDA_SUCCESS. + # HANDLE_RETURN raises CUDAError with the CUresult name and message (e.g. CUDA_ERROR_INVALID_VALUE) + # when status is not CUDA_SUCCESS. HANDLE_RETURN(status) raise RuntimeError( "Instantiation failed for an unexpected reason which is described in the return value of the function." From a2164d562473be20ad49fee5ced4b97cb8f02645 Mon Sep 17 00:00:00 2001 From: Jinfeng Li Date: Fri, 28 Aug 2026 16:06:04 -0700 Subject: [PATCH 3/4] Update cuda_core/cuda/core/graph/_graph_builder.pyx Co-authored-by: Ralf W. Grosse-Kunstleve --- cuda_core/cuda/core/graph/_graph_builder.pyx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cuda_core/cuda/core/graph/_graph_builder.pyx b/cuda_core/cuda/core/graph/_graph_builder.pyx index 8af3de6b275..071fff38386 100644 --- a/cuda_core/cuda/core/graph/_graph_builder.pyx +++ b/cuda_core/cuda/core/graph/_graph_builder.pyx @@ -216,7 +216,8 @@ def _instantiate_graph(source, options: GraphCompleteOptions | None = None) -> G # when status is not CUDA_SUCCESS. HANDLE_RETURN(status) raise RuntimeError( - "Instantiation failed for an unexpected reason which is described in the return value of the function." + "CUDA graph instantiation failed, but cuGraphInstantiateWithParams " + "returned CUDA_SUCCESS; no driver error details are available." ) elif params.result_out == driver.CUgraphInstantiateResult.CUDA_GRAPH_INSTANTIATE_INVALID_STRUCTURE: raise RuntimeError("Instantiation failed due to invalid structure, such as cycles.") From 6283221f69cf0c5b200a1959b4d03cbc53af6543 Mon Sep 17 00:00:00 2001 From: Jinfeng Date: Fri, 28 Aug 2026 23:15:28 +0000 Subject: [PATCH 4/4] add test_graph_complete_invalid_options_raise_cuda_error per review comment --- cuda_core/tests/graph/test_options.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cuda_core/tests/graph/test_options.py b/cuda_core/tests/graph/test_options.py index 391f521b65c..a189eaf9160 100644 --- a/cuda_core/tests/graph/test_options.py +++ b/cuda_core/tests/graph/test_options.py @@ -7,6 +7,7 @@ from helpers.graph_kernels import compile_common_kernels, compile_conditional_kernels from cuda.core import Device, LaunchConfig, launch +from cuda.core._utils.cuda_utils import CUDAError from cuda.core.graph import GraphBuilder, GraphCompleteOptions, GraphDebugPrintOptions @@ -65,6 +66,20 @@ def test_graph_complete_options(init_cuda): gb.complete(options).close() +@pytest.mark.agent_authored(model="gpt-5.6") +def test_graph_complete_invalid_options_raise_cuda_error(init_cuda): + mod = compile_common_kernels() + empty_kernel = mod.get_kernel("empty_kernel") + + gb = Device().create_graph_builder().begin_building() + launch(gb, LaunchConfig(grid=1, block=1), empty_kernel) + gb.end_building() + + options = GraphCompleteOptions(auto_free_on_launch=True, device_launch=True) + with pytest.raises(CUDAError, match="CUDA_ERROR_INVALID_VALUE"): + gb.complete(options) + + def test_graph_build_mode(init_cuda): mod = compile_common_kernels() empty_kernel = mod.get_kernel("empty_kernel")