Motivation
Standard CUDA graphs capture an entire forward pass as a single, opaque graph. This is great for performance, but creates two problems:- 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.
- 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.
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.
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 anoutput_factory. It runs during capture, before the segment ends, so output
storage belongs to the graph pool and can be reused after its consumers:
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:
--cuda-graph-backend-decode breakable:
Server Args
| Argument | Default | Description |
|---|---|---|
—debug-cuda-graph | False | Enable 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-decode | full | Set 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-prefill | breakable on CUDA | Prefill capture backend; breakable is the CUDA default. |
How It Works
Capture
Breakable CUDA graph extends PyTorch’storch.cuda.CUDAGraph by splitting a single capture into multiple segments separated by graph breaks.
During capture, the flow is:
Replay
During 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 anoutput_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 hookstorch.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-graphis automatically disabled with a warning. - Requires
cuda-pythonon NVIDIA. Stream-capture-status queries use the CUDA runtime viacuda.bindings(pip install cuda-python); the portabletorch.cuda.is_current_stream_capturing()has proven unreliable on CUDA. On ROCm/HIP — wherecuda-pythonis unavailable — the portabletorch.cudaAPI (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
cudaGraphLaunchcall (to replay the segment before the break) - One eager Python function call
- One
cudaStreamBeginCapture/cudaStreamEndCapturepair during capture
Code Reference
| File | Description |
|---|---|
python/sglang/srt/model_executor/runner_backend_utils/breakable_cuda_graph/breakable_cuda_graph.py | Core implementation: eager_on_graph, BreakableCUDAGraph, BreakableCUDAGraphCapture |
python/sglang/srt/model_executor/runner_backend_utils/breakable_cuda_graph/cuda_utils.py | CUDA runtime binding utilities (NVIDIA stream-capture queries) |
python/sglang/srt/model_executor/runner_backend/breakable_cuda_graph_backend.py | Integration with CUDA graph runner backends |
python/sglang/srt/server_args.py | —debug-cuda-graph flag handling |
