|
5 | 5 |
|
6 | 6 | import numpy as np |
7 | 7 | import pytest |
8 | | -from helpers.graph_kernels import compile_common_kernels |
| 8 | +from helpers.graph_kernels import compile_common_kernels, compile_conditional_kernels |
9 | 9 | from helpers.marks import requires_module |
| 10 | +from helpers.misc import try_create_condition |
10 | 11 |
|
11 | 12 | from cuda.core import Device, LaunchConfig, LegacyPinnedMemoryResource, launch |
12 | | -from cuda.core.graph import GraphBuilder |
| 13 | +from cuda.core.graph import GraphBuilder, GraphDefinition |
13 | 14 |
|
14 | 15 |
|
15 | 16 | def test_graph_is_building(init_cuda): |
@@ -384,3 +385,219 @@ def test_graph_stream_lifetime(init_cuda): |
384 | 385 |
|
385 | 386 | # Destroy the stream |
386 | 387 | stream.close() |
| 388 | + |
| 389 | + |
| 390 | +# --------------------------------------------------------------------------- |
| 391 | +# GraphBuilder.graph_definition |
| 392 | +# --------------------------------------------------------------------------- |
| 393 | + |
| 394 | + |
| 395 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 396 | +def test_graph_definition_returns_graph_definition_after_end_building(init_cuda): |
| 397 | + """Primary builder exposes its captured graph as a GraphDefinition after end_building().""" |
| 398 | + mod = compile_common_kernels() |
| 399 | + empty_kernel = mod.get_kernel("empty_kernel") |
| 400 | + |
| 401 | + gb = Device().create_graph_builder().begin_building() |
| 402 | + launch(gb, LaunchConfig(grid=1, block=1), empty_kernel) |
| 403 | + launch(gb, LaunchConfig(grid=1, block=1), empty_kernel) |
| 404 | + gb.end_building() |
| 405 | + |
| 406 | + gd = gb.graph_definition |
| 407 | + assert isinstance(gd, GraphDefinition) |
| 408 | + # The captured graph must contain the launched kernels. |
| 409 | + assert len(gd.nodes()) == 2 |
| 410 | + |
| 411 | + |
| 412 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 413 | +def test_graph_definition_raises_before_begin_building(init_cuda): |
| 414 | + """Primary builder has no graph allocated before begin_building().""" |
| 415 | + gb = Device().create_graph_builder() |
| 416 | + with pytest.raises(RuntimeError, match="before begin_building"): |
| 417 | + _ = gb.graph_definition |
| 418 | + |
| 419 | + |
| 420 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 421 | +def test_graph_definition_raises_during_capture(init_cuda): |
| 422 | + """graph_definition is unsafe while the driver is actively capturing.""" |
| 423 | + gb = Device().create_graph_builder().begin_building() |
| 424 | + try: |
| 425 | + with pytest.raises(RuntimeError, match="capture is in"): |
| 426 | + _ = gb.graph_definition |
| 427 | + finally: |
| 428 | + gb.end_building() |
| 429 | + |
| 430 | + |
| 431 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 432 | +def test_graph_definition_raises_for_forked(init_cuda): |
| 433 | + """Forked builders share the primary's graph; their property must raise.""" |
| 434 | + mod = compile_common_kernels() |
| 435 | + empty_kernel = mod.get_kernel("empty_kernel") |
| 436 | + |
| 437 | + gb = Device().create_graph_builder().begin_building() |
| 438 | + launch(gb, LaunchConfig(grid=1, block=1), empty_kernel) |
| 439 | + primary, sibling = gb.split(2) |
| 440 | + try: |
| 441 | + with pytest.raises(RuntimeError, match="forked"): |
| 442 | + _ = sibling.graph_definition |
| 443 | + finally: |
| 444 | + sibling = GraphBuilder.join(primary, sibling) |
| 445 | + sibling.end_building() |
| 446 | + |
| 447 | + |
| 448 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 449 | +def test_graph_definition_shares_ownership(init_cuda): |
| 450 | + """Closing the builder must not invalidate a held GraphDefinition.""" |
| 451 | + mod = compile_common_kernels() |
| 452 | + empty_kernel = mod.get_kernel("empty_kernel") |
| 453 | + |
| 454 | + gb = Device().create_graph_builder().begin_building() |
| 455 | + launch(gb, LaunchConfig(grid=1, block=1), empty_kernel) |
| 456 | + gb.end_building() |
| 457 | + |
| 458 | + gd = gb.graph_definition |
| 459 | + gb.close() |
| 460 | + # The shared CUgraph keeps the graph alive. |
| 461 | + assert len(gd.nodes()) == 1 |
| 462 | + |
| 463 | + |
| 464 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 465 | +def test_graph_definition_raises_after_close(init_cuda): |
| 466 | + """Accessing the property after close() must fail at the builder boundary. |
| 467 | +
|
| 468 | + close() resets the graph handle, so returning a view here would wrap a |
| 469 | + null CUgraph and defer the failure to a later CUDA call. Reject fresh |
| 470 | + access instead, matching complete() and debug_dot_print(). |
| 471 | + """ |
| 472 | + mod = compile_common_kernels() |
| 473 | + empty_kernel = mod.get_kernel("empty_kernel") |
| 474 | + |
| 475 | + gb = Device().create_graph_builder().begin_building() |
| 476 | + launch(gb, LaunchConfig(grid=1, block=1), empty_kernel) |
| 477 | + gb.end_building() |
| 478 | + gb.close() |
| 479 | + |
| 480 | + with pytest.raises(RuntimeError, match="closed"): |
| 481 | + _ = gb.graph_definition |
| 482 | + |
| 483 | + |
| 484 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 485 | +def test_graph_definition_round_trips_through_explicit_api(init_cuda): |
| 486 | + """Mutating via the explicit API survives complete() and runs correctly.""" |
| 487 | + mod = compile_common_kernels() |
| 488 | + add_one = mod.get_kernel("add_one") |
| 489 | + |
| 490 | + launch_stream = Device().create_stream() |
| 491 | + mr = LegacyPinnedMemoryResource() |
| 492 | + b = mr.allocate(4) |
| 493 | + arr = np.from_dlpack(b).view(np.int32) |
| 494 | + arr[0] = 0 |
| 495 | + |
| 496 | + gb = launch_stream.create_graph_builder().begin_building() |
| 497 | + launch(gb, LaunchConfig(grid=1, block=1), add_one, arr.ctypes.data) |
| 498 | + gb.end_building() |
| 499 | + |
| 500 | + # Add a second add_one through the explicit GraphDefinition view. |
| 501 | + gd = gb.graph_definition |
| 502 | + captured_node = next(iter(gd.nodes())) |
| 503 | + captured_node.launch(LaunchConfig(grid=1, block=1), add_one, arr.ctypes.data) |
| 504 | + assert len(gd.nodes()) == 2 |
| 505 | + |
| 506 | + graph = gb.complete() |
| 507 | + graph.launch(launch_stream) |
| 508 | + launch_stream.sync() |
| 509 | + assert arr[0] == 2 |
| 510 | + |
| 511 | + b.close() |
| 512 | + |
| 513 | + |
| 514 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 515 | +@requires_module(np, "2.1") |
| 516 | +def test_graph_definition_hybrid_conditional_body(init_cuda): |
| 517 | + """Populate a conditional body entirely through the explicit API. |
| 518 | +
|
| 519 | + This is the headline hybrid flow enabled by the new property: |
| 520 | + ``if_then`` returns a ``GraphBuilder`` for the body, but instead of |
| 521 | + calling ``begin_building`` and capturing into it, we reach for |
| 522 | + ``graph_definition`` and add nodes through the explicit API. |
| 523 | + """ |
| 524 | + mod = compile_conditional_kernels(int) |
| 525 | + add_one = mod.get_kernel("add_one") |
| 526 | + set_handle = mod.get_kernel("set_handle") |
| 527 | + |
| 528 | + launch_stream = Device().create_stream() |
| 529 | + mr = LegacyPinnedMemoryResource() |
| 530 | + b = mr.allocate(4) |
| 531 | + arr = np.from_dlpack(b).view(np.int32) |
| 532 | + arr[0] = 0 |
| 533 | + |
| 534 | + gb = Device().create_graph_builder().begin_building() |
| 535 | + condition = try_create_condition(gb) |
| 536 | + launch(gb, LaunchConfig(grid=1, block=1), set_handle, condition, 1) |
| 537 | + body_gb = gb.if_then(condition) |
| 538 | + |
| 539 | + # Skip body_gb.begin_building() entirely -- the body graph already |
| 540 | + # exists at conditional-node creation time and is exposed here. |
| 541 | + body_def = body_gb.graph_definition |
| 542 | + assert isinstance(body_def, GraphDefinition) |
| 543 | + assert len(body_def.nodes()) == 0 |
| 544 | + body_def.launch(LaunchConfig(grid=1, block=1), add_one, arr.ctypes.data) |
| 545 | + |
| 546 | + graph = gb.end_building().complete() |
| 547 | + graph.launch(launch_stream) |
| 548 | + launch_stream.sync() |
| 549 | + assert arr[0] == 1 |
| 550 | + |
| 551 | + b.close() |
| 552 | + |
| 553 | + |
| 554 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 555 | +@requires_module(np, "2.1") |
| 556 | +def test_graph_definition_conditional_body_after_capture(init_cuda): |
| 557 | + """Capture into a conditional body, then augment it via the explicit API.""" |
| 558 | + mod = compile_conditional_kernels(int) |
| 559 | + add_one = mod.get_kernel("add_one") |
| 560 | + set_handle = mod.get_kernel("set_handle") |
| 561 | + |
| 562 | + launch_stream = Device().create_stream() |
| 563 | + mr = LegacyPinnedMemoryResource() |
| 564 | + b = mr.allocate(4) |
| 565 | + arr = np.from_dlpack(b).view(np.int32) |
| 566 | + arr[0] = 0 |
| 567 | + |
| 568 | + gb = Device().create_graph_builder().begin_building() |
| 569 | + condition = try_create_condition(gb) |
| 570 | + launch(gb, LaunchConfig(grid=1, block=1), set_handle, condition, 1) |
| 571 | + body_gb = gb.if_then(condition).begin_building() |
| 572 | + |
| 573 | + # Capture one increment into the body. |
| 574 | + launch(body_gb, LaunchConfig(grid=1, block=1), add_one, arr.ctypes.data) |
| 575 | + body_gb.end_building() |
| 576 | + |
| 577 | + # Add a second increment via the explicit API on the same body graph. |
| 578 | + body_def = body_gb.graph_definition |
| 579 | + captured_node = next(iter(body_def.nodes())) |
| 580 | + captured_node.launch(LaunchConfig(grid=1, block=1), add_one, arr.ctypes.data) |
| 581 | + assert len(body_def.nodes()) == 2 |
| 582 | + |
| 583 | + graph = gb.end_building().complete() |
| 584 | + graph.launch(launch_stream) |
| 585 | + launch_stream.sync() |
| 586 | + assert arr[0] == 2 |
| 587 | + |
| 588 | + b.close() |
| 589 | + |
| 590 | + |
| 591 | +@pytest.mark.agent_authored(model="claude-opus-4.8") |
| 592 | +@requires_module(np, "2.1") |
| 593 | +def test_graph_definition_conditional_body_during_capture_raises(init_cuda): |
| 594 | + """The CAPTURING-state guard fires for conditional bodies too.""" |
| 595 | + gb = Device().create_graph_builder().begin_building() |
| 596 | + condition = try_create_condition(gb) |
| 597 | + body_gb = gb.if_then(condition).begin_building() |
| 598 | + try: |
| 599 | + with pytest.raises(RuntimeError, match="capture is in"): |
| 600 | + _ = body_gb.graph_definition |
| 601 | + finally: |
| 602 | + body_gb.end_building() |
| 603 | + gb.end_building() |
0 commit comments