Skip to content

Commit c1c0c05

Browse files
committed
Add build modes
1 parent 0a831f3 commit c1c0c05

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

cuda_core/cuda/core/experimental/_graph.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,32 @@ def is_join_required(self) -> bool:
189189
"""Returns True if this graph builder must be joined before building is ended."""
190190
return self._mnff.is_join_required
191191

192-
def begin_building(self) -> GraphBuilder:
193-
"""Begins the building process."""
192+
def begin_building(self, mode="global") -> GraphBuilder:
193+
"""Begins the building process.
194+
195+
Build `mode` for controlling interaction with other API calls must be one of the following:
196+
197+
- `global` : Prohibit potentially unsafe operations across all streams in the process.
198+
- `thread_local` : Prohibit potentially unsafe operations in streams created by the current thread.
199+
- `relaxed` : The local thread is not prohibited from potentially unsafe operations.
200+
201+
Parameters
202+
----------
203+
mode : str, optional
204+
Build mode to control the interaction with other API calls that are porentially unsafe.
205+
206+
"""
194207
if self._building_ended:
195208
raise RuntimeError("Cannot resume building after building has ended.")
209+
if mode not in ("global", "thread_local", "relaxed"):
210+
raise ValueError(f"Unsupported build mode: {mode}")
211+
if mode == "global":
212+
capture_mode = driver.CUstreamCaptureMode.CU_STREAM_CAPTURE_MODE_GLOBAL
213+
elif mode == "thread_local":
214+
capture_mode = driver.CUstreamCaptureMode.CU_STREAM_CAPTURE_MODE_THREAD_LOCAL
215+
elif mode == "relaxed":
216+
capture_mode = driver.CUstreamCaptureMode.CU_STREAM_CAPTURE_MODE_RELAXED
217+
196218
if self._mnff.conditional_graph:
197219
handle_return(
198220
driver.cuStreamBeginCaptureToGraph(
@@ -201,15 +223,11 @@ def begin_building(self) -> GraphBuilder:
201223
None, # dependencies
202224
None, # dependencyData
203225
0, # numDependencies
204-
driver.CUstreamCaptureMode.CU_STREAM_CAPTURE_MODE_GLOBAL,
226+
capture_mode,
205227
)
206228
)
207229
else:
208-
handle_return(
209-
driver.cuStreamBeginCapture(
210-
self._mnff.stream.handle, driver.CUstreamCaptureMode.CU_STREAM_CAPTURE_MODE_GLOBAL
211-
)
212-
)
230+
handle_return(driver.cuStreamBeginCapture(self._mnff.stream.handle, capture_mode))
213231
return self
214232

215233
@property

cuda_core/tests/test_graph.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,30 @@ def test_graph_complete_options(init_cuda):
638638
gb.complete(options).close()
639639
options = CompleteOptions(use_node_priority=True)
640640
gb.complete(options).close()
641+
642+
643+
def test_graph_build_mode(init_cuda):
644+
mod = _common_kernels()
645+
empty_kernel = mod.get_kernel("empty_kernel")
646+
647+
# Simple linear topology
648+
gb = Device().create_graph_builder().begin_building(mode="global")
649+
launch(gb, LaunchConfig(grid=1, block=1), empty_kernel)
650+
launch(gb, LaunchConfig(grid=1, block=1), empty_kernel)
651+
launch(gb, LaunchConfig(grid=1, block=1), empty_kernel)
652+
gb.end_building()
653+
654+
gb = Device().create_graph_builder().begin_building(mode="thread_local")
655+
launch(gb, LaunchConfig(grid=1, block=1), empty_kernel)
656+
launch(gb, LaunchConfig(grid=1, block=1), empty_kernel)
657+
launch(gb, LaunchConfig(grid=1, block=1), empty_kernel)
658+
gb.end_building()
659+
660+
gb = Device().create_graph_builder().begin_building(mode="relaxed")
661+
launch(gb, LaunchConfig(grid=1, block=1), empty_kernel)
662+
launch(gb, LaunchConfig(grid=1, block=1), empty_kernel)
663+
launch(gb, LaunchConfig(grid=1, block=1), empty_kernel)
664+
gb.end_building()
665+
666+
with pytest.raises(ValueError, match="^Unsupported build mode:"):
667+
gb = Device().create_graph_builder().begin_building(mode=None)

0 commit comments

Comments
 (0)