Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Breakable CUDA Graphs

Breakable CUDA Graphs extends the standard PyTorch CUDA graph workflow.

With torch.cuda.graph, one context manager captures one CUDA graph. That works well when the whole region is capture-compatible. breakable-cuda-graphs keeps the same capture/replay shape, but lets one with breakable_graph(...) block produce a sequence of CUDA graph segments separated by explicit eager breaks. If no eager breaks occur, the block is captured as a single graph segment.

Use this when most of a workload should run under CUDA graphs, but some sections are not CUDA-graph-compatible or otherwise need to run eagerly. Mark those sections with @no_graph, and make their CUDA inputs/outputs obey the constraints below.

Quick start

Install from source into an environment with PyTorch and CUDA support:

git clone https://github.com/meta-pytorch/breakable-cuda-graphs.git
cd breakable-cuda-graphs
pip install -e .

The capture flow is the same as regular CUDA graphs: allocate static buffers, warm up on a side stream, capture, then replay by updating the static inputs. The only addition is @no_graph, which marks functions that should run eagerly between captured graph segments.

import torch
from breakable_cuda_graphs import CUDAGraphSequence, breakable_graph, no_graph


@no_graph
def dynamic_scale(x: torch.Tensor) -> None:
    # Cannot be captured: reads a value back to the CPU.
    if x.sum().item() > 0:
        x.clamp_(min=0)


# Pre-allocate static buffers.
static_input = torch.empty(1024, device="cuda")
result = torch.empty(1024, device="cuda")


def workload(src: torch.Tensor, dst: torch.Tensor) -> None:
    dst.copy_(src * 2)
    dynamic_scale(dst)  # ends the current graph segment and runs eagerly
    dst.add_(1.0)


# Warm up on a side stream, as required by CUDA graphs.
s = torch.cuda.Stream()
s.wait_stream(torch.cuda.current_stream())
with torch.cuda.stream(s):
    for _ in range(3):
        static_input.fill_(1.0)
        workload(static_input, result)
torch.cuda.current_stream().wait_stream(s)

# Capture.
seq = CUDAGraphSequence()
with breakable_graph(seq):
    workload(static_input, result)

# Replay with new data by overwriting the static input buffer.
static_input.fill_(5.0)
seq.replay()

Constraints

  • @no_graph functions must not return CUDA tensors. Write CUDA outputs into pre-allocated buffers passed as arguments.
  • Side streams must be joined back to the capturing stream before entering an @no_graph function or leaving the breakable_graph context. Set BREAKABLE_CUDA_GRAPHS_DEBUG=1 to add still-unjoined stream id(s) to the resulting error.
  • Usual CUDA graph constraints still apply: replay uses the same tensor addresses captured during warmup/capture.

Additional usage

Explicit split points

force_no_graph() inserts a graph break with no eager work. This can be useful for debugging or isolating capture regions.

from breakable_cuda_graphs import CUDAGraphSequence, breakable_graph, force_no_graph

seq = CUDAGraphSequence()
with breakable_graph(seq):
    a = step1(x)
    force_no_graph()
    b = step2(a)

Sharing memory pools

All graph segments within a sequence share the same CUDA graph memory pool. You can also share pools across sequences:

seq1 = CUDAGraphSequence()
with breakable_graph(seq1):
    workload_a(buf_a, src_a)

seq2 = CUDAGraphSequence(pool=seq1.pool())
with breakable_graph(seq2):
    workload_b(buf_b, src_b)

Reference

  • CUDAGraphSequence(pool=None): captured graph/eager segment sequence. Methods: replay(), reset(), pool().
  • breakable_graph(seq, stream=None, capture_error_mode="global"): capture context, analogous to torch.cuda.graph.
  • @no_graph / @no_graph(enable=...): mark functions that run eagerly inside breakable_graph.
  • force_no_graph(): explicit split point with no eager work.

For implementation details, see DESIGN.md.

License

BSD 3-Clause License. See LICENSE for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

breakable_cuda_graphs-0.1.0rc0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

breakable_cuda_graphs-0.1.0rc0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file breakable_cuda_graphs-0.1.0rc0.tar.gz.

File metadata

  • Download URL: breakable_cuda_graphs-0.1.0rc0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for breakable_cuda_graphs-0.1.0rc0.tar.gz
Algorithm Hash digest
SHA256 8d5f2a6e5477e63d7de84294f7bb4f3ad78756e0a6634eddb5ae536cc6981040
MD5 f765764ae5d95501f1238b162cd0e121
BLAKE2b-256 082c1c037b8e3325584cc3a2e9a5f9c42395594d37c90e15913361ff626cbe66

See more details on using hashes here.

Provenance

The following attestation bundles were made for breakable_cuda_graphs-0.1.0rc0.tar.gz:

Publisher: publish_release.yml on meta-pytorch/breakable-cuda-graphs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file breakable_cuda_graphs-0.1.0rc0-py3-none-any.whl.

File metadata

File hashes

Hashes for breakable_cuda_graphs-0.1.0rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf97d7f307be1d08a8393c3cb56b03e2239de794717fca0f79a93cde287fce2b
MD5 dd315e019997fad71c4e07c4b469def3
BLAKE2b-256 651baa7b11d701150ac2465b0a79f8a5eb884a96a9dad200200ac3ebe662ffcd

See more details on using hashes here.

Provenance

The following attestation bundles were made for breakable_cuda_graphs-0.1.0rc0-py3-none-any.whl:

Publisher: publish_release.yml on meta-pytorch/breakable-cuda-graphs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.0rc0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page