Skip to content

Commit 19e9304

Browse files
committed
Draft
1 parent 23fbab5 commit 19e9304

5 files changed

Lines changed: 37 additions & 32 deletions

File tree

cuda_core/cuda/core/experimental/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from cuda.core.experimental import utils
66
from cuda.core.experimental._device import Device
77
from cuda.core.experimental._event import EventOptions
8-
from cuda.core.experimental._graph import GraphBuilder, Graph
8+
from cuda.core.experimental._graph import Graph, GraphBuilder
99
from cuda.core.experimental._launcher import LaunchConfig, launch
1010
from cuda.core.experimental._linker import Linker, LinkerOptions
1111
from cuda.core.experimental._program import Program, ProgramOptions

cuda_core/cuda/core/experimental/_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,6 @@ def sync(self):
340340
handle_return(runtime.cudaDeviceSynchronize())
341341

342342
@precondition(_check_context_initialized)
343-
def create_graph(self) -> GraphBuilder:
343+
def build_graph(self) -> GraphBuilder:
344344
private_stream = self.create_stream()
345345
return GraphBuilder._init(stream=private_stream)

cuda_core/cuda/core/experimental/_graph.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
from __future__ import annotations
66

7-
import os
8-
import warnings
9-
import weakref
107
from dataclasses import dataclass
11-
from typing import TYPE_CHECKING, Optional, Tuple, Union
8+
from typing import TYPE_CHECKING, Optional, Tuple
129

1310
if TYPE_CHECKING:
1411
from cuda.core.experimental._stream import Stream
@@ -17,8 +14,7 @@
1714

1815
@dataclass
1916
class DebugPrintOptions:
20-
"""
21-
"""
17+
""" """
2218

2319
VERBOSE: bool = False
2420
RUNTIME_TYPES: bool = False
@@ -60,7 +56,7 @@ def _init(stream, is_root=True):
6056
# If from Stream, then we can't
6157
self._stream = stream
6258
self._capturing = False
63-
self._is_root = is_root # TODO: Is this info needed?
59+
self._is_root = is_root # TODO: Is this info needed?
6460
return self
6561

6662
def _check_capture_stream_provided(self, *args, **kwargs):
@@ -101,8 +97,10 @@ def is_capture_active(self) -> bool:
10197
elif capture_status == driver.CUstreamCaptureStatus.CU_STREAM_CAPTURE_STATUS_ACTIVE:
10298
return True
10399
elif capture_status == driver.CUstreamCaptureStatus.CU_STREAM_CAPTURE_STATUS_INVALIDATED:
104-
raise RuntimeError("Stream is part of a capture sequence that has been invalidated, but "
105-
"not terminated. The capture sequence must be terminated with self.`().")
100+
raise RuntimeError(
101+
"Stream is part of a capture sequence that has been invalidated, but "
102+
"not terminated. The capture sequence must be terminated with self.`()."
103+
)
106104
else:
107105
raise NotImplementedError(f"Unsupported capture stuse type received: {capture_status}")
108106

@@ -124,30 +122,30 @@ def debug_dot_print(self, path, options: Optional[DebugPrintOptions] = None):
124122

125123
handle_return(driver.cuGraphDebugDotPrint(self._graph, path, options_value))
126124

127-
def split(self, count) -> Tuple[Graph, ...]:
125+
def fork(self, count) -> Tuple[Graph, ...]:
128126
if count <= 1:
129-
raise ValueError(f"Invalid split count: expecting >= 2, got {count}")
127+
raise ValueError(f"Invalid fork count: expecting >= 2, got {count}")
130128

131129
# 1. Record an event on our stream
132130
event = self._stream.record()
133131

134132
# TODO: Steps 2,3,4 can be combined under a single loop
135133

136-
# 2. Create a streams for each of the new splits
137-
# TODO: Optimization where one of the split stream is allowed to use
134+
# 2. Create a streams for each of the new forks
135+
# TODO: Optimization where one of the fork stream is allowed to use
138136
# TODO: Should use the same stream options as initial stream??
139-
split_stream = [self._stream.device.create_stream() for i in range(count)]
137+
fork_stream = [self._stream.device.create_stream() for i in range(count)]
140138

141139
# 3. Have each new stream wait on our singular event
142-
for stream in split_stream:
140+
for stream in fork_stream:
143141
stream.wait(event)
144142

145143
# 4. Discard the event
146144
# TODO: Is this actually allowed when using with a graph? Surely, since it just needs to create an edge for us... right?
147145
event.close()
148146

149-
# 5. Create new graph builders for each new stream split
150-
return [GraphBuilder._init(stream=stream, is_root=False) for stream in split_stream]
147+
# 5. Create new graph builders for each new stream fork
148+
return [GraphBuilder._init(stream=stream, is_root=False) for stream in fork_stream]
151149

152150
def join(self, *graph_builders):
153151
if len(graph_builders) < 1:
@@ -158,6 +156,18 @@ def join(self, *graph_builders):
158156
# TODO: Can we close each of those new streams? Do we need weakref?
159157
graph.close()
160158

159+
def create_conditional_handle(self, default_value=None):
160+
pass
161+
162+
def if_cond(self, handle):
163+
pass
164+
165+
def if_else(self, handle):
166+
pass
167+
168+
def switch(self, handle, count):
169+
pass
170+
161171
def close(self):
162172
if self._capturing:
163173
raise RuntimeError("Trying to close a graph builder who is still capturing")
@@ -166,8 +176,7 @@ def close(self):
166176

167177

168178
class Graph:
169-
"""
170-
"""
179+
""" """
171180

172181
def __init__(self):
173182
raise RuntimeError("directly constructing a Graph instance is not supported")

cuda_core/cuda/core/experimental/_stream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def __cuda_stream__(self):
294294

295295
return Stream._init(obj=_stream_holder())
296296

297-
def create_graph(self) -> GraphBuilder:
297+
def build_graph(self) -> GraphBuilder:
298298
return GraphBuilder._init(stream=self)
299299

300300

cuda_core/tests/test_graph.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
# this software and related documentation outside the terms of the EULA
77
# is strictly prohibited.
88

9-
import pytest
109

1110
from cuda.core.experimental import Device, LaunchConfig, Program, ProgramOptions, launch
11+
1212
# from cuda.core.experimental import Device, Stream, StreamOptions
1313
# from cuda.core.experimental._event import Event
1414
# from cuda.core.experimental._stream import LEGACY_DEFAULT_STREAM, PER_THREAD_DEFAULT_STREAM, default_stream
1515

1616

1717
def test_graph_is_capture_alive(init_cuda):
18-
graph_builder = Device().create_graph()
18+
graph_builder = Device().build_graph()
1919
assert graph_builder.is_capture_active() is False
2020
graph_builder.begin_capture()
2121
assert graph_builder.is_capture_active() is True
@@ -24,7 +24,6 @@ def test_graph_is_capture_alive(init_cuda):
2424

2525

2626
def test_graph_straight(init_cuda):
27-
2827
# TODO: Maybe share these between tests?
2928
code = """
3029
__global__ void empty_kernel() {}
@@ -35,9 +34,8 @@ def test_graph_straight(init_cuda):
3534
mod = prog.compile("cubin", name_expressions=("empty_kernel",))
3635
empty_kernel = mod.get_kernel("empty_kernel")
3736

38-
3937
# Test start
40-
graph_builder = Device().create_graph()
38+
graph_builder = Device().build_graph()
4139
config = LaunchConfig(grid=1, block=1, stream=graph_builder.legacy_stream_capture)
4240

4341
assert graph_builder.is_capture_active() is False
@@ -48,8 +46,7 @@ def test_graph_straight(init_cuda):
4846
graph_builder.end_capture()
4947

5048

51-
def test_graph_split_join(init_cuda):
52-
49+
def test_graph_fork_join(init_cuda):
5350
# TODO: Maybe share these between tests?
5451
code = """
5552
__global__ void empty_kernel() {}
@@ -60,14 +57,13 @@ def test_graph_split_join(init_cuda):
6057
mod = prog.compile("cubin", name_expressions=("empty_kernel",))
6158
empty_kernel = mod.get_kernel("empty_kernel")
6259

63-
6460
# Test start
65-
graph_builder = Device().create_graph()
61+
graph_builder = Device().build_graph()
6662
assert graph_builder.is_capture_active() is False
6763
graph_builder.begin_capture()
6864
launch(empty_kernel, {"grid": 1, "block": 1, "stream": graph_builder.legacy_stream_capture})
6965

70-
left, right = graph_builder.split(2)
66+
left, right = graph_builder.fork(2)
7167
launch(empty_kernel, {"grid": 1, "block": 1, "stream": left.legacy_stream_capture})
7268
launch(empty_kernel, {"grid": 1, "block": 1, "stream": left.legacy_stream_capture})
7369
launch(empty_kernel, {"grid": 1, "block": 1, "stream": right.legacy_stream_capture})

0 commit comments

Comments
 (0)