|
6 | 6 | import ctypes |
7 | 7 |
|
8 | 8 | import pytest |
| 9 | +from helpers.contexts import current_context_handle, use_context |
9 | 10 | from helpers.graph_kernels import compile_common_kernels |
10 | 11 | from helpers.misc import try_create_condition |
11 | 12 |
|
12 | | -from cuda.core import Device, LaunchConfig |
13 | | -from cuda.core._utils.cuda_utils import CUDAError |
| 13 | +from cuda.core import ContextOptions, Device, LaunchConfig, SMResourceOptions |
| 14 | +from cuda.core._utils.cuda_utils import CUDAError, driver, handle_return |
| 15 | +from cuda.core._utils.version import driver_version |
14 | 16 | from cuda.core.graph import ( |
15 | 17 | EmptyNode, |
| 18 | + GraphCompleteOptions, |
16 | 19 | GraphCondition, |
17 | 20 | GraphDefinition, |
18 | 21 | ) |
@@ -150,6 +153,118 @@ def test_multiple_instantiation(init_cuda): |
150 | 153 | assert g1 is not g2 |
151 | 154 |
|
152 | 155 |
|
| 156 | +@pytest.mark.agent_authored(model="gpt-6-sol") |
| 157 | +def test_instantiate_rejects_cycle_and_accepts_repaired_graph(init_cuda): |
| 158 | + """Removing a cycle makes an invalid graph executable again.""" |
| 159 | + definition = GraphDefinition() |
| 160 | + called = [] |
| 161 | + first = definition.empty() |
| 162 | + last = first.callback(lambda: called.append(True)) |
| 163 | + last.succ.add(first) |
| 164 | + stream = init_cuda.create_stream() |
| 165 | + executable = None |
| 166 | + try: |
| 167 | + with pytest.raises(RuntimeError, match="invalid structure, such as cycles"): |
| 168 | + definition.instantiate() |
| 169 | + assert called == [] |
| 170 | + last.succ.discard(first) |
| 171 | + executable = definition.instantiate() |
| 172 | + executable.launch(stream) |
| 173 | + stream.sync() |
| 174 | + assert called == [True] |
| 175 | + finally: |
| 176 | + if executable is not None: |
| 177 | + executable.close() |
| 178 | + last.succ.discard(first) |
| 179 | + last.destroy() |
| 180 | + first.destroy() |
| 181 | + stream.close() |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.agent_authored(model="gpt-6-sol") |
| 185 | +def test_device_launch_rejects_host_callback(init_cuda): |
| 186 | + """Device-launch graphs reject host callbacks before execution.""" |
| 187 | + definition = GraphDefinition() |
| 188 | + called = [] |
| 189 | + node = definition.callback(lambda: called.append(True)) |
| 190 | + try: |
| 191 | + with pytest.raises( |
| 192 | + RuntimeError, match="device launch failed because the graph contained an unsupported operation" |
| 193 | + ): |
| 194 | + definition.instantiate(GraphCompleteOptions(device_launch=True)) |
| 195 | + assert called == [] |
| 196 | + finally: |
| 197 | + node.destroy() |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.agent_authored(model="gpt-6-sol") |
| 201 | +def test_device_launch_rejects_nodes_from_different_contexts(init_cuda): |
| 202 | + """Device-launch instantiation rejects mixed contexts and accepts a repaired graph.""" |
| 203 | + if driver_version() < (12, 4, 0): |
| 204 | + pytest.skip("green contexts require CUDA driver 12.4 or newer") |
| 205 | + if not init_cuda.properties.unified_addressing: |
| 206 | + pytest.skip("device launch requires unified addressing") |
| 207 | + original_context = current_context_handle() |
| 208 | + definition = GraphDefinition() |
| 209 | + green_context = first = foreign = executable = stream = None |
| 210 | + try: |
| 211 | + groups, _ = init_cuda.resources.sm.split(SMResourceOptions(count=None)) |
| 212 | + green_context = init_cuda.create_context(ContextOptions(resources=[groups[0]])) |
| 213 | + module = compile_common_kernels() |
| 214 | + kernel = module.get_kernel("empty_kernel") |
| 215 | + config = LaunchConfig(grid=1, block=1) |
| 216 | + first = definition.launch(config, kernel) |
| 217 | + with use_context(init_cuda, green_context): |
| 218 | + foreign = definition.launch(config, kernel) |
| 219 | + |
| 220 | + # CUkernel handles are context-independent; check each node's resolved context. |
| 221 | + first_params = handle_return(driver.cuGraphKernelNodeGetParams(first.handle)) |
| 222 | + foreign_params = handle_return(driver.cuGraphKernelNodeGetParams(foreign.handle)) |
| 223 | + assert int(first_params.ctx) == original_context |
| 224 | + assert int(foreign_params.ctx) == int(green_context.handle) |
| 225 | + assert int(foreign_params.ctx) != original_context |
| 226 | + assert current_context_handle() == original_context |
| 227 | + with pytest.raises(RuntimeError, match="nodes belonging to different contexts"): |
| 228 | + definition.instantiate(GraphCompleteOptions(device_launch=True)) |
| 229 | + assert current_context_handle() == original_context |
| 230 | + |
| 231 | + # A single-context graph remains valid after the failed instantiation. |
| 232 | + foreign.destroy() |
| 233 | + foreign = None |
| 234 | + executable = definition.instantiate(GraphCompleteOptions(device_launch=True)) |
| 235 | + stream = init_cuda.create_stream() |
| 236 | + executable.launch(stream) |
| 237 | + stream.sync() |
| 238 | + assert current_context_handle() == original_context |
| 239 | + finally: |
| 240 | + if executable is not None: |
| 241 | + executable.close() |
| 242 | + if foreign is not None: |
| 243 | + foreign.destroy() |
| 244 | + if first is not None: |
| 245 | + first.destroy() |
| 246 | + if stream is not None: |
| 247 | + stream.close() |
| 248 | + if green_context is not None: |
| 249 | + green_context.close() |
| 250 | + |
| 251 | + |
| 252 | +@pytest.mark.agent_authored(model="gpt-6-sol") |
| 253 | +def test_instantiate_rejects_unassociated_condition(init_cuda): |
| 254 | + """Instantiation rejects a condition that no conditional node uses.""" |
| 255 | + if driver_version() < (12, 8, 0): |
| 256 | + pytest.skip("Unused graph-condition diagnostics require CUDA driver 12.8 or newer") |
| 257 | + definition = GraphDefinition() |
| 258 | + # Deliberately leave this graph-owned condition unassociated with a node. |
| 259 | + definition.create_condition(default_value=1) |
| 260 | + node = definition.empty() |
| 261 | + try: |
| 262 | + with pytest.raises(RuntimeError, match="conditional handles are not associated with conditional builders"): |
| 263 | + definition.instantiate() |
| 264 | + finally: |
| 265 | + node.destroy() |
| 266 | + |
| 267 | + |
153 | 268 | def test_unmatched_alloc_succeeds(init_cuda): |
154 | 269 | """Alloc without corresponding free is valid (graph-scoped lifetime).""" |
155 | 270 | _skip_if_no_mempool() |
|
0 commit comments