Skip to main content

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 BreakableCUDAGraph, 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.
graph = BreakableCUDAGraph()
with breakable_graph(graph):
    workload(static_input, result)

# Replay with new data by overwriting the static input buffer.
static_input.fill_(5.0)
graph.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 BreakableCUDAGraph, breakable_graph, force_no_graph

graph = BreakableCUDAGraph()
with breakable_graph(graph):
    a = step1(x)
    force_no_graph()
    b = step2(a)

Replacing eager work during capture

An eager break can use a cheaper capture_stub while the surrounding graph segments are being constructed. Replay always calls the real function. Both callables receive the same arguments; CUDA outputs must use pre-allocated argument buffers.

def initialize_output(output: torch.Tensor, x: torch.Tensor) -> None:
    output.zero_()


@no_graph(capture_stub=initialize_output)
def distributed_step(output: torch.Tensor, x: torch.Tensor) -> None:
    run_distributed_operation(output, x)

Sharing memory pools

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

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

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

Reference

  • BreakableCUDAGraph(pool=None): captured graph/eager segment sequence. Methods: replay(), reset(), pool().
  • breakable_graph(graph, stream=None, capture_error_mode="global", barrier_fn=None): capture context, analogous to torch.cuda.graph. barrier_fn is an optional zero-argument callable run before each eager break during capture. It can re-align distributed ranks after segment finalization and before rank-coupled eager work.
  • @no_graph / @no_graph(enable=..., capture_stub=...): mark functions that run eagerly inside breakable_graph. An optional capture_stub replaces the eager body during capture only; replay calls the original function.
  • 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.

Metadata

Release files for breakable-cuda-graphs 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for breakable-cuda-graphs 0.1.0
File Size Uploaded Provenance Actions
breakable_cuda_graphs-0.1.0.tar.gz 20.7 kB With provenance Details for breakable_cuda_graphs-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for breakable-cuda-graphs 0.1.0
File Interpreter ABI Platform Actions
breakable_cuda_graphs-0.1.0-py3-none-any.whl Python 3 none any Details for breakable_cuda_graphs-0.1.0-py3-none-any.whl

Total release size: 31.1 kB

Release files / breakable_cuda_graphs-0.1.0.tar.gz

Download URL breakable_cuda_graphs-0.1.0.tar.gz
Size 20.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7507507ff9f41f1e50a5cc3137f39a3d3064e3c51e59542bc7feac8993d3ce48
BLAKE2b-256 checksum
How to use checksums
41fd1200ff7c95ad2524ac70ca27b1555a0c68a47e7f50d64c7af1919c407dcf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 8, 2026.

Transparency log

Release files / breakable_cuda_graphs-0.1.0-py3-none-any.whl

Download URL breakable_cuda_graphs-0.1.0-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ff99eafb3314981cda9d67c17e005d9f41ec686f93b46d5ac490e15429cd8773
BLAKE2b-256 checksum
How to use checksums
00148b7ff65cc806e006361172b0ef0e3e4bef51e735364b1f15a667f7bb1cbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 8, 2026.

Transparency log

Security for breakable-cuda-graphs 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for breakable_cuda_graphs-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 8, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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