@@ -194,6 +194,10 @@ def test_graph_repeat_capture(init_cuda):
194194 with pytest .raises (RuntimeError , match = "^Cannot resume building after building has ended." ):
195195 gb .begin_building ()
196196
197+ # Close the memroy resource now because the garbage collected might
198+ # de-allocate it during the next graph builder process
199+ b .close ()
200+
197201
198202def test_graph_capture_errors (init_cuda ):
199203 gb = Device ().create_graph_builder ()
@@ -228,9 +232,10 @@ def test_graph_conditional_if(init_cuda, condition_value):
228232 try :
229233 handle = gb .create_conditional_handle ()
230234 except RuntimeError as e :
231- with pytest .raises (RuntimeError , match = r "^Driver version \d+ does not support conditional handles " ):
235+ with pytest .raises (RuntimeError , match = "^Driver version" ):
232236 raise e
233237 gb .end_building ()
238+ b .close ()
234239 pytest .skip ("Driver does not support conditional handle" )
235240 launch (gb , LaunchConfig (grid = 1 , block = 1 ), set_handle , handle , condition_value )
236241
@@ -262,6 +267,10 @@ def test_graph_conditional_if(init_cuda, condition_value):
262267 assert arr [0 ] == 1
263268 assert arr [1 ] == 0
264269
270+ # Close the memroy resource now because the garbage collected might
271+ # de-allocate it during the next graph builder process
272+ b .close ()
273+
265274
266275@pytest .mark .parametrize ("condition_value" , [True , False ])
267276@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
@@ -289,9 +298,10 @@ def test_graph_conditional_if_else(init_cuda, condition_value):
289298 try :
290299 gb_if , gb_else = gb .if_else (handle )
291300 except RuntimeError as e :
292- with pytest .raises (RuntimeError , match = r "^Driver version \d+ does not support conditional if-else " ):
301+ with pytest .raises (RuntimeError , match = "^Driver version" ):
293302 raise e
294303 gb .end_building ()
304+ b .close ()
295305 pytest .skip ("Driver does not support conditional if-else" )
296306
297307 ## IF nodes
@@ -329,6 +339,10 @@ def test_graph_conditional_if_else(init_cuda, condition_value):
329339 assert arr [0 ] == 1
330340 assert arr [1 ] == 3
331341
342+ # Close the memroy resource now because the garbage collected might
343+ # de-allocate it during the next graph builder process
344+ b .close ()
345+
332346
333347@pytest .mark .parametrize ("condition_value" , [0 , 1 , 2 , 3 ])
334348@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
@@ -357,9 +371,10 @@ def test_graph_conditional_switch(init_cuda, condition_value):
357371 try :
358372 gb_case = list (gb .switch (handle , 3 ))
359373 except RuntimeError as e :
360- with pytest .raises (RuntimeError , match = r "^Driver version \d+ does not support conditional switch " ):
374+ with pytest .raises (RuntimeError , match = "^Driver version" ):
361375 raise e
362376 gb .end_building ()
377+ b .close ()
363378 pytest .skip ("Driver does not support conditional switch" )
364379
365380 ## Case 0
@@ -415,6 +430,10 @@ def test_graph_conditional_switch(init_cuda, condition_value):
415430 assert arr [1 ] == 0
416431 assert arr [2 ] == 0
417432
433+ # Close the memroy resource now because the garbage collected might
434+ # de-allocate it during the next graph builder process
435+ b .close ()
436+
418437
419438@pytest .mark .parametrize ("condition_value" , [True , False ])
420439@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
@@ -460,6 +479,10 @@ def test_graph_conditional_while(init_cuda, condition_value):
460479 else :
461480 assert arr [0 ] == 0
462481
482+ # Close the memroy resource now because the garbage collected might
483+ # de-allocate it during the next graph builder process
484+ b .close ()
485+
463486
464487@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
465488def test_graph_child_graph (init_cuda ):
@@ -495,6 +518,7 @@ def test_graph_child_graph(init_cuda):
495518 ):
496519 raise e
497520 gb_parent .end_building ()
521+ b .close ()
498522 pytest .skip ("Launching child graphs is not implemented for versions older than CUDA 12" )
499523
500524 launch (gb_parent , LaunchConfig (grid = 1 , block = 1 ), add_one , arr .ctypes .data )
@@ -508,6 +532,10 @@ def test_graph_child_graph(init_cuda):
508532 assert arr [0 ] == 2
509533 assert arr [1 ] == 3
510534
535+ # Close the memroy resource now because the garbage collected might
536+ # de-allocate it during the next graph builder process
537+ b .close ()
538+
511539
512540@pytest .mark .skipif (tuple (int (i ) for i in np .__version__ .split ("." )[:2 ]) < (2 , 1 ), reason = "need numpy 2.1.0+" )
513541def test_graph_update (init_cuda ):
@@ -533,11 +561,11 @@ def build_graph(condition_value):
533561 # Add Node B (while condition)
534562 try :
535563 gb_case = list (gb .switch (handle , 3 ))
536- except RuntimeError as e :
537- with pytest .raises (RuntimeError , match = r "^Driver version \d+ does not support conditional switch " ):
564+ except Exception as e :
565+ with pytest .raises (RuntimeError , match = "^Driver version" ):
538566 raise e
539567 gb .end_building ()
540- pytest . skip ( "Driver does not support conditional switch" )
568+ raise e
541569
542570 ## Case 0
543571 gb_case [0 ].begin_building ()
@@ -562,7 +590,13 @@ def build_graph(condition_value):
562590
563591 return gb .end_building ()
564592
565- graph_variants = [build_graph (0 ), build_graph (1 ), build_graph (2 )]
593+ try :
594+ graph_variants = [build_graph (0 ), build_graph (1 ), build_graph (2 )]
595+ except Exception as e :
596+ with pytest .raises (RuntimeError , match = "^Driver version" ):
597+ raise e
598+ b .close ()
599+ pytest .skip ("Driver does not support conditional switch" )
566600
567601 # Launch the first graph
568602 assert arr [0 ] == 0
@@ -591,6 +625,10 @@ def build_graph(condition_value):
591625 assert arr [1 ] == 3
592626 assert arr [2 ] == 3
593627
628+ # Close the memroy resource now because the garbage collected might
629+ # de-allocate it during the next graph builder process
630+ b .close ()
631+
594632
595633def test_graph_stream_lifetime (init_cuda ):
596634 mod = _common_kernels ()
0 commit comments