--- title: "Piecewise CUDA Graph" metatags: description: "Use Piecewise CUDA Graph to reduce prefill and extend kernel launch overhead while supporting dynamic token shapes." --- ## Motivation Standard CUDA graphs capture the entire model forward pass as a single graph. This works well for decode (fixed batch size), but not for extend/prefill where the number of tokens varies across iterations. Piecewise CUDA Graph (PCG) solves this by splitting the model's computation graph into pieces (roughly one per layer) at "split points" (e.g., MoE dispatch ops). Each piece is captured as a separate CUDA graph for a set of pre-defined token lengths. At runtime, the input is padded to the nearest captured size, and each piece is replayed. This eliminates kernel launch overhead for prefill/extend while still supporting dynamic shapes. Recently we **enabled PCG by default**, which means that the old `--enable-piecewise-cuda-graph` flag is deprecated. Use `--disable-piecewise-cuda-graph` to turn it off. ## Usage PCG is enabled by default for supported configurations. No extra flags needed: ```bash python3 -m sglang.launch_server \ --model-path meta-llama/Llama-3.1-8B-Instruct ``` ### Disable PCG ```bash python3 -m sglang.launch_server \ --model-path meta-llama/Llama-3.1-8B-Instruct \ --disable-piecewise-cuda-graph ``` ### Custom capture sizes ```bash python3 -m sglang.launch_server \ --model-path meta-llama/Llama-3.1-8B-Instruct \ --piecewise-cuda-graph-max-tokens 2048 ``` ### Server Args
| Argument | Default | Description |
|---|---|---|
--disable-piecewise-cuda-graph |
False |
Disable PCG for extend/prefill. |
--enforce-piecewise-cuda-graph |
False |
Force-enable PCG, skipping all auto-disable conditions. For testing only. |
--piecewise-cuda-graph-max-tokens |
None (auto) |
Maximum token count to capture. Defaults to chunked_prefill_size (non-MLA) or 2048 (MLA). |
--piecewise-cuda-graph-tokens |
None (auto) |
Explicit list of token lengths to capture. Auto-generated if not set. |
--piecewise-cuda-graph-compiler |
"eager" |
Compiler backend for the captured subgraphs. Choices: eager, inductor. |
--enable-piecewise-cuda-graph |
— | Deprecated. PCG is now enabled by default. Use --enforce-piecewise-cuda-graph to skip auto-disable conditions. |
| Token range | Step size |
|---|---|
| 4 – 32 | 4 |
| 48 – 256 | 16 |
| 288 – 512 | 32 |
| 576 – 1024 | 64 |
| 1280 – 4096 | 256 |
| 4096+ | 512 |
| File | Description |
|---|---|
python/sglang/srt/model_executor/piecewise_cuda_graph_runner.py |
Main runner: init, capture, replay |
python/sglang/srt/compilation/compile.py |
install_torch_compiled trampoline |
python/sglang/srt/compilation/backend.py |
SGLangBackend, graph splitting, piecewise compilation |
python/sglang/srt/compilation/cuda_piecewise_backend.py |
Per-subgraph CUDA graph capture/replay |
python/sglang/srt/compilation/piecewise_context_manager.py |
Global context flags and ForwardContext |
python/sglang/srt/compilation/compilation_config.py |
Capture sizes, split ops, compiler config |
python/sglang/srt/utils/custom_op.py |
register_custom_op for torch.compile compatibility |
python/sglang/srt/server_args.py |
Server arguments and auto-disable logic |