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