Skip to content

Commit 2512b84

Browse files
committed
Close MR allocs so that they don't GC during future Graph building
1 parent de890c8 commit 2512b84

3 files changed

Lines changed: 48 additions & 8 deletions

File tree

cuda_core/cuda/core/experimental/_graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def complete(self, options: Optional[CompleteOptions] = None) -> Graph:
282282
flags |= driver.CUgraphInstantiate_flags.CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH
283283
if options.use_node_priority:
284284
flags |= driver.CUgraphInstantiate_flags.CUDA_GRAPH_INSTANTIATE_FLAG_USE_NODE_PRIORITY
285-
graph = Graph._init(handle_return(driver.cuGraphInstantiateWithFlags(self._mnff.graph, flags)))
285+
return Graph._init(handle_return(driver.cuGraphInstantiateWithFlags(self._mnff.graph, flags)))
286286

287287
params = driver.CUDA_GRAPH_INSTANTIATE_PARAMS()
288288
if options:

cuda_core/tests/test_event.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,5 @@ def test_error_timing_incomplete():
169169
arr[0] = 1
170170
event3.sync()
171171
event3 - event1 # this should work
172+
173+
b.close()

cuda_core/tests/test_graph.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

198202
def 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+")
465488
def 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+")
513541
def 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

595633
def test_graph_stream_lifetime(init_cuda):
596634
mod = _common_kernels()

0 commit comments

Comments
 (0)