1919from cuda .core .experimental ._memory import _DefaultPinnedMemorySource
2020
2121
22- def test_graph_is_building (init_cuda ):
23- gb = Device ().create_graph_builder ()
24- assert gb .is_building is False
25- gb .begin_building ()
26- assert gb .is_building is True
27- gb .end_building ()
28- assert gb .is_building is False
29-
30-
3122def _common_kernels ():
3223 code = """
3324 extern "C" __device__ __cudart_builtin__ void CUDARTAPI cudaGraphSetConditional(cudaGraphConditionalHandle handle,
@@ -48,6 +39,15 @@ def _common_kernels():
4839 return mod
4940
5041
42+ def test_graph_is_building (init_cuda ):
43+ gb = Device ().create_graph_builder ()
44+ assert gb .is_building is False
45+ gb .begin_building ()
46+ assert gb .is_building is True
47+ gb .end_building ()
48+ assert gb .is_building is False
49+
50+
5151def test_graph_straight (init_cuda ):
5252 mod = _common_kernels ()
5353 empty_kernel = mod .get_kernel ("empty_kernel" )
@@ -75,11 +75,18 @@ def test_graph_fork_join(init_cuda):
7575 gb = Device ().create_graph_builder ().begin_building ()
7676 launch (gb , LaunchConfig (grid = 1 , block = 1 ), empty_kernel )
7777
78+ with pytest .raises (ValueError , match = "^Invalid split count: expecting >= 2, got 1" ):
79+ gb .split (1 )
80+
7881 left , right = gb .split (2 )
7982 launch (left , LaunchConfig (grid = 1 , block = 1 ), empty_kernel )
8083 launch (left , LaunchConfig (grid = 1 , block = 1 ), empty_kernel )
8184 launch (right , LaunchConfig (grid = 1 , block = 1 ), empty_kernel )
8285 launch (right , LaunchConfig (grid = 1 , block = 1 ), empty_kernel )
86+
87+ with pytest .raises (ValueError , match = "^Must join with at least two graph builders" ):
88+ GraphBuilder .join (left )
89+
8390 gb = GraphBuilder .join (left , right )
8491
8592 launch (gb , LaunchConfig (grid = 1 , block = 1 ), empty_kernel )
@@ -135,7 +142,7 @@ def test_graph_is_join_required(init_cuda):
135142
136143 # Create final node
137144 launch (gb , LaunchConfig (grid = 1 , block = 1 ), empty_kernel )
138- gb .end_building ()
145+ gb .end_building (). complete ()
139146
140147
141148@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
@@ -161,17 +168,17 @@ def test_graph_repeat_capture(init_cuda):
161168 assert arr [0 ] == 1
162169
163170 # Continue capturing to extend the graph
164- with pytest .raises (RuntimeError ):
171+ with pytest .raises (RuntimeError , match = "^Cannot resume building after building has ended." ):
165172 gb .begin_building ()
166173
167174
168175def test_graph_capture_errors (init_cuda ):
169176 gb = Device ().create_graph_builder ()
170- with pytest .raises (RuntimeError ):
177+ with pytest .raises (RuntimeError , match = "^Graph has not finished building." ):
171178 gb .complete ()
172179
173180 gb .begin_building ()
174- with pytest .raises (RuntimeError ):
181+ with pytest .raises (RuntimeError , match = "^Graph has not finished building." ):
175182 gb .complete ()
176183 gb .end_building ().complete ()
177184
@@ -286,7 +293,7 @@ def test_graph_conditional_if_else(init_cuda, condition_value):
286293 assert arr [1 ] == 3
287294
288295
289- @pytest .mark .parametrize ("condition_value" , [0 , 1 , 2 ])
296+ @pytest .mark .parametrize ("condition_value" , [0 , 1 , 2 , 3 ])
290297def test_graph_conditional_switch (init_cuda , condition_value ):
291298 mod = _common_kernels ()
292299 add_one = mod .get_kernel ("add_one" )
@@ -358,6 +365,11 @@ def test_graph_conditional_switch(init_cuda, condition_value):
358365 assert arr [0 ] == 1
359366 assert arr [1 ] == 0
360367 assert arr [2 ] == 3
368+ elif condition_value == 3 :
369+ # No branch is taken if case index is out of range
370+ assert arr [0 ] == 1
371+ assert arr [1 ] == 0
372+ assert arr [2 ] == 0
361373
362374
363375@pytest .mark .parametrize ("condition_value" , [True , False ])
0 commit comments