cuda.core.graph.GraphBuilder#
- class cuda.core.graph.GraphBuilder#
A graph under construction by stream capture.
A graph groups a set of CUDA kernels and other CUDA operations together and executes them with a specified dependency tree. It speeds up the workflow by combining the driver activities associated with CUDA kernel launches and CUDA API calls.
Directly creating a
GraphBuilderis not supported due to ambiguity. New graph builders should instead be created through aDevice, or astreamobject.Methods
- __init__(self)#
- begin_building(self, mode='relaxed') GraphBuilder#
Begins the building process.
Build mode for controlling interaction with other API calls must be one of the following:
global : Prohibit potentially unsafe operations across all streams in the process.
thread_local : Prohibit potentially unsafe operations in streams created by the current thread.
relaxed : The local thread is not prohibited from potentially unsafe operations.
- Parameters:
mode (str, optional) – Build mode to control the interaction with other API calls that are porentially unsafe. Default set to use relaxed.
- callback(self, fn, *, user_data=None)#
Add a host callback to the graph during stream capture.
The callback runs on the host CPU when the graph reaches this point in execution. Two modes are supported:
Python callable: Pass any callable. The GIL is acquired automatically. The callable must take no arguments; use closures or
functools.partialto bind state.ctypes function pointer: Pass a
ctypes.CFUNCTYPEinstance. The function receives a singlevoid*argument (theuser_data). The caller must keep the ctypes wrapper alive for the lifetime of the graph.
Warning
Callbacks must not call CUDA API functions. Doing so may deadlock or corrupt driver state.
- Parameters:
fn (callable or ctypes function pointer) – The callback function.
user_data (int or bytes-like, optional) – Only for ctypes function pointers. If
int, passed as a raw pointer (caller manages lifetime). If bytes-like, the data is copied and its lifetime is tied to the graph.
- close(self)#
Destroy the graph builder.
Closes the associated stream if we own it. Borrowed stream object will instead have their references released.
- complete(
- self,
- options: GraphCompleteOptions | None = None,
Completes the graph builder and returns the built
Graphobject.- Parameters:
options (
GraphCompleteOptions, optional) – Customizable dataclass for the graph builder completion options.- Returns:
graph – The newly built graph.
- Return type:
Graph
- create_condition(
- self,
- default_value=None,
Create a condition variable for use with conditional nodes.
The returned
GraphConditionobject is passed to conditional-node builder methods (if_then(),if_else(),while_loop(),switch()). Its value is controlled at runtime by device code viacudaGraphSetConditional.- Parameters:
default_value (int, optional) – The default value to assign to the condition. If None, no default is assigned.
- Returns:
A condition variable for controlling conditional execution.
- Return type:
- debug_dot_print(
- self,
- path,
- options: GraphDebugPrintOptions | None = None,
Generates a DOT debug file for the graph builder.
- Parameters:
path (str) – File path to use for writting debug DOT output
options (
GraphDebugPrintOptions, optional) – Customizable dataclass for the debug print options.
- embed(self, child: GraphBuilder)#
Embed a previously-built
GraphBuilderas a child node.- Parameters:
child (
GraphBuilder) – The child graph builder. Must have finished building.
- end_building(self) GraphBuilder#
Ends the building process.
- if_else(
- self,
- GraphCondition condition: GraphCondition,
Adds an if-else condition branch and returns new graph builders for both branches.
The resulting if graph will execute the branch if the condition evaluates to true at runtime, otherwise the else branch will execute.
The new builders inherit work dependencies from the original builder.
- Parameters:
condition (
GraphCondition) – The condition variable fromcreate_condition()controlling which branch executes.- Returns:
graph_builders – A tuple of two new graph builders, one for the if branch and one for the else branch.
- Return type:
tuple[
GraphBuilder,GraphBuilder]
- if_then(
- self,
- GraphCondition condition: GraphCondition,
Adds an if condition branch and returns a new graph builder for it.
The resulting if graph will only execute the branch if the condition evaluates to true at runtime.
The new builder inherits work dependencies from the original builder.
- Parameters:
condition (
GraphCondition) – The condition variable fromcreate_condition()controlling whether the branch executes.- Returns:
graph_builder – The newly created conditional graph builder.
- Return type:
GraphBuilder
- static join(*graph_builders) GraphBuilder#
Joins multiple graph builders into a single graph builder.
The returned builder inherits work dependencies from the provided builders.
- Parameters:
*graph_builders (
GraphBuilder) – The graph builders to join.- Returns:
graph_builder – The newly joined graph builder.
- Return type:
GraphBuilder
- split(self, int count: int) tuple[GraphBuilder, ...]#
Splits the original graph builder into multiple graph builders.
The new builders inherit work dependencies from the original builder. The original builder is reused for the split and is returned first in the tuple.
- Parameters:
count (int) – The number of graph builders to split the graph builder into.
- Returns:
graph_builders – A tuple of split graph builders. The first graph builder in the tuple is always the original graph builder.
- Return type:
tuple[
GraphBuilder, …]
- switch(
- self,
- GraphCondition condition: GraphCondition,
- int count: int,
Adds a switch condition branch and returns new graph builders for all cases.
The resulting switch graph will execute the branch whose case index matches the value of the condition at runtime. If no match is found, no branch will be executed.
The new builders inherit work dependencies from the original builder.
- Parameters:
condition (
GraphCondition) – The condition variable fromcreate_condition()selecting which case executes.count (int) – The number of cases to add to the switch conditional.
- Returns:
graph_builders – A tuple of new graph builders, one for each branch.
- Return type:
tuple[
GraphBuilder, …]
- while_loop(
- self,
- GraphCondition condition: GraphCondition,
Adds a while loop and returns a new graph builder for it.
The resulting while loop graph will execute the branch repeatedly at runtime until the condition evaluates to false.
The new builder inherits work dependencies from the original builder.
- Parameters:
condition (
GraphCondition) – The condition variable fromcreate_condition()controlling loop continuation.- Returns:
graph_builder – The newly created while loop graph builder.
- Return type:
GraphBuilder
Attributes