Skip to main content

Motivation

Standard CUDA graphs capture an entire forward pass as a single, opaque graph. This is great for performance, but creates two problems:
  1. Debugging is hard. When something goes wrong inside a captured graph (wrong outputs, numerical mismatches, crashes), there is no way to step through the operations or insert print statements because the graph replays as a monolithic unit.
  2. Some ops are incompatible. Certain operations — dynamic control flow, host-device synchronization, JIT compilation, or ops that change behavior across iterations — cannot be captured into a CUDA graph at all. Today, the only workaround is to disable CUDA graphs entirely, which sacrifices the kernel launch overhead savings for the rest of the model.
Breakable CUDA Graph solves both problems by allowing graph breaks to be inserted at specific points. The computation is split into multiple captured graph segments with eager (non-graph) execution in between. This preserves most of the CUDA graph performance benefit while allowing targeted operations to run outside the graph.

Usage

Debug Mode: Run Everything Eagerly

The simplest use case is debugging. The --debug-cuda-graph flag wraps the entire decode forward pass in a graph break, so every operation runs eagerly while still going through the full CUDA graph capture/replay code path. This lets you debug CUDA graph issues without changing model code. It requires the breakable backend for decode.
This mode is intended for debugging only — it eliminates the performance benefit of CUDA graphs since every op runs eagerly.

Selective Graph Breaks in Model Code

For production use, you can mark specific functions as “non-graphable” using the @eager_on_graph decorator. During replay, these functions run eagerly between captured graph segments. Outside a breakable CUDA graph, they run as plain calls.

Pool-owned outputs

When you can determine the output layout before a break, supply an output_factory. It runs during capture, before the segment ends, so output storage belongs to the graph pool and can be reused after its consumers:
The factory receives the function’s arguments and returns a tensor, or nested tuples/lists of tensors and None. Output shapes, dtypes, devices, and structure must match on every replay. The wrapper copies the results into those buffers; this does not automatically eliminate the computation’s temporary outputs. For a function that can fill buffers directly, set pass_output=True, accept an optional out=None keyword argument, and return the supplied buffers for the leaves you filled. Those leaves are not copied. Outside capture, the factory and output copies are bypassed, and the function receives its original arguments. Use .run_eager(...) for an already-decorated operation inside another eager break to avoid nested graph segmentation. You can also insert a bare graph break (no computation) using the break_graph() helper:
Breakable CUDA graph is selected per phase. Prefill uses it by default on CUDA; to use it for decode (without debug mode), pass --cuda-graph-backend-decode breakable:

Server Args

ArgumentDefaultDescription
—debug-cuda-graphFalseEnable debug/eager mode. Wraps the entire forward pass in a graph break so every op runs eagerly through the capture/replay path.
—cuda-graph-backend-decodefullSet to breakable to capture decode with breakable CUDA graph. Required for —debug-cuda-graph and for @eager_on_graph breaks to take effect in decode.
—cuda-graph-backend-prefillbreakable on CUDAPrefill capture backend; breakable is the CUDA default.

How It Works

Capture

Breakable CUDA graph extends PyTorch’s torch.cuda.CUDAGraph by splitting a single capture into multiple segments separated by graph breaks. During capture, the flow is:
Each segment is independently instantiated as a CUDA graph executable. The non-graph functions and their argument references are stored for replay.

Replay

During replay:
The non-graph functions are invoked with the same tensor references as capture time. Since these references point to the CUDA graph’s static input/output buffers, they see updated values on each replay.

Outputs

A non-graph function writes its results into buffers its caller allocated before the break, so the downstream graph segments read the same addresses at every replay. Host-side results go into a holder object the caller owns. Without an output_factory, a function that returns a value fails with an assertion. Every captured shape is warmed up before capture, so lazy setup (JIT compilation, buffers sized by the first run) happens outside capture.

Stream Fork/Join Tracking

Some models fork work onto secondary CUDA streams (e.g., for overlapped computation). Breakable CUDA graph hooks torch.cuda.Stream.wait_stream to track which streams are forked from the capture stream. When a graph break occurs, all forked streams are automatically joined back before ending the capture segment, and re-forked after beginning the next segment.

Compatibility

  • CUDA and ROCm/HIP. Breakable CUDA graph runs on both NVIDIA and AMD GPUs. Other platforms (NPU, CPU, MPS, XPU) are unsupported; there --debug-cuda-graph is automatically disabled with a warning.
  • Requires cuda-python on NVIDIA. Stream-capture-status queries use the CUDA runtime via cuda.bindings (pip install cuda-python); the portable torch.cuda.is_current_stream_capturing() has proven unreliable on CUDA. On ROCm/HIP — where cuda-python is unavailable — the portable torch.cuda API (which maps to the HIP runtime) is used instead.
  • Not compatible with memory saver mode. Cannot be used together with SGLANG_MEMORY_SAVER_CUDA_GRAPH.

Performance

When no graph breaks are inserted, breakable CUDA graph has minimal overhead compared to standard CUDA graph — the capture/replay path is nearly identical. Each graph break adds:
  • One cudaGraphLaunch call (to replay the segment before the break)
  • One eager Python function call
  • One cudaStreamBeginCapture / cudaStreamEndCapture pair during capture
For typical use cases with a small number of graph breaks, the overhead is negligible compared to the saved kernel launch overhead from the captured segments.

Code Reference

FileDescription
python/sglang/srt/model_executor/runner_backend_utils/breakable_cuda_graph/breakable_cuda_graph.pyCore implementation: eager_on_graph, BreakableCUDAGraph, BreakableCUDAGraphCapture
python/sglang/srt/model_executor/runner_backend_utils/breakable_cuda_graph/cuda_utils.pyCUDA runtime binding utilities (NVIDIA stream-capture queries)
python/sglang/srt/model_executor/runner_backend/breakable_cuda_graph_backend.pyIntegration with CUDA graph runner backends
python/sglang/srt/server_args.py—debug-cuda-graph flag handling