Skip to content

[rl] SGLang generator: decode CUDA graphs up to max_num_seqs - #6417

Draft
felipemello1 wants to merge 3 commits into
mainfrom
felipemello1/sgl203/s2-decode-graphs
Draft

felipemello1 wants to merge 3 commits into
mainfrom
felipemello1/sgl203/s2-decode-graphs

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

TLDR: SGLangEngine captures decode CUDA graphs up to max_num_seqs requests, the rule the vLLM engine already follows. SGLang's default stops at 512 and runs a larger decode batch eagerly. Qwen3.5-0.8B on one GB300 at max_num_seqs 1024: +32% output tok/s (5,669 -> 7,477), decode steps 57.1 -> 18.6 ms.

SGLang now captures decode graphs up to max_num_seqs instead of 512, so 1,024-row decode steps take 18.6 ms instead of 57.1 ms and output rises from 5,669 to 7,477 tokens/s.

# before: SGLang picks the decode graphs' largest batch, min(512, max_running_requests) on a GB300
server_kwargs["cuda_graph_config"] = {"prefill": {"backend": "breakable"}}
# after
cuda_graph_config = {"decode": {"max_bs": max_num_seqs}}
cuda_graph_config["prefill"] = {"backend": "breakable"}

Problem

  1. A decode batch holds up to max_num_seqs requests per DP rank. The controller sets it to 2x each DP rank's share of the rollouts in flight, rounded up to a power of two (max_num_seqs_per_generator_dp_rank): 4,096 rollouts in flight over 8 DP ranks give 1,024.
  2. SGLang captures decode graphs up to min(512, max_running_requests) on GPUs with at least 160 GB (memory_hook.py, _handle_gpu_memory_settings).
  3. A decode batch above its largest graph runs eagerly.
  4. At max_num_seqs 1024, every decode step of the agent load below had more than 512 rows (up to 1,019). Each ran eagerly, at 57 ms on average.

Solution

  1. _build_server_args sets cuda_graph_config["decode"]["max_bs"] = max_num_seqs for every cuda_graph mode except "none".
    This is the vLLM engine's rule: its decode graph cap is max_num_seqs (VLLMCudaGraphConfig.get_vllm_compilation_config).
  2. SGLang's own ladder fills in the sizes: +32 rows from 512 to max_num_seqs, plus max_num_seqs itself.
  3. At max_num_seqs <= 512 the graphs are the same as before on a GB300. On GPUs below 160 GB, SGLang's default is smaller (256 or less at TP < 4), so this PR captures more graphs there.

Results

Qwen3.5-0.8B, one GB300, cuda_graph "breakable", max_num_seqs 1024, max_num_batched_tokens 16,384,
1,024 closed-loop agent sessions (100-400 output tokens per turn, 100-1,000 tool tokens), 120 s window, one run each:
                              out tok/s   decode step              prefill step
main (graphs stop at 512)     5,669       57.1 ms, 969 rows avg    55.8 ms, 1,009 tokens avg
this PR (graphs up to 1,024)  7,477       18.6 ms, 892 rows avg    61.9 ms, 1,175 tokens avg
Engine build time: 84 s (main) vs 79 s (this PR). KV capacity: 10.6M tokens in both.

+32% is about 5x the run-to-run spread of this harness: 3-6% over two runs per engine of a 128-session load. Prefill steps take longer with this PR because they hold more tokens: faster decode lets more turns arrive per second.

You may ask:

  • Why not set SGLang's graph sizes directly? Its ladder already matches vLLM's spacing up to 512 (+8 to 256, +16 to 512). Only the cap was missing.
  • Why not change the chunked prefill size too? SGLang's default is already 16,384 tokens on GPUs with at least 160 GB, and max_num_batched_tokens still overrides it.

NOTE: more decode graphs take more GPU memory, and TorchTitan sets mem_fraction_static itself, so SGLang's own reserve for graph memory doesn't apply: the graphs take what the static pool leaves. The ladder adds one graph per 32 rows above 512: 16 more at max_num_seqs 1,024, 112 more at 4,096. The controller caps max_num_seqs only at max_num_batched_tokens. The 0.8B run above kept the same KV capacity. Qwen3.5-27B on one GB300 captures the same 19 graphs at 4,096 as at 256: SGLang's linear-attention state cache caps it at 119 requests. A dense model pays the full ladder: on Qwen3-14B (one GB300), 4,096 captures 164 decode graphs instead of 36 at 256, with 48.9 s instead of 14.8 s of decode capture and 3.36 GB instead of 0.09 GB of decode graph memory. The vLLM engine has the same exposure.
NOTE: with MTP, SGLang's decode ladder is denser (one graph per 16 rows above 272), and each row holds k + 1 tokens. Build time and graph memory with MTP at a large max_num_seqs were not measured.

Test plan

  • The A/B above, with a one-off harness (not in the PR) that drives SGLangEngine through add_requests and step. It records each step's kind and rows by wrapping scheduler.run_batch.
  • pre-commit on the changed files: all hooks pass.
  • pytest tests/rl/unit_tests/cpu: 771 passed, 8 skipped, 2 failed. The 2 fail on main too: test_kv_offload_coordination.py::test_terminal_cleanup_preserves_block_identity, 2 cases.
  • TestSGLangBitwiseParityKDA (1 GPU, breakable graphs): 6 passed on one GB300.
  • cuda_graph="decode" (decode graphs, prefill eager), which no GPU test class runs: the same harness built the engine in that mode at max_num_seqs 1024 (67 s) and ran the 1,024-session load for 60 s. Decode steps had 1,016 rows on average (up to 1,024), p50 13.6 ms.
  • No test added: the GPU test classes run "full" and "breakable" at max_num_seqs 5, below 512, where nothing changes. They do build the engine with the new decode config.

SGLang's decode graphs stop at min(512, max_running_requests), and it runs a larger decode batch eagerly. Capture up to max_num_seqs, the largest decode batch.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 27, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 2m 29s —— View job


Needs discussion: all the measurements ran at DP 1, and I couldn't confirm from SGLang's source whether decode.max_bs counts requests per attention-DP rank. If it counts across the whole instance, DP > 1 captures fewer graphs than SGLang's default. One stale comment also needs a rewording. Review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Needs discussion -- the change is the right size and matches the vLLM rule, but every measurement ran at DP 1, and I couldn't confirm from SGLang's source whether cuda_graph_config["decode"]["max_bs"] counts requests per attention-DP rank or across the whole instance.

What this PR does. For every cuda_graph mode except "none", _build_server_args now sets SGLang's largest decode graph size to max_num_seqs. Before, it used SGLang's default, min(512, max_running_requests) on a GB300. Prefill settings don't change. They are now built into the same cuda_graph_config dict, which is passed only when graphs are on.

Example.

max_num_seqs = 1024, DP 1, GB300
main:    decode graphs 1..512; a 900-row decode batch runs eagerly (57 ms/step)
this PR: decode graphs 1..512, 544, ..., 1024; a 900-row batch pads to 928 and replays (18.6 ms/step)
max_num_seqs <= 512 on a GB300: graphs are the same as before

Must fix

  1. torchtitan/rl/generator/sglang/engine.py:159-161 -- this comment justifies banning CUDA-graph keys in extra_server_args. It says "SGLang's default capture sizes also stay within the step budget that Dist-MoE's buffers and the DeepEP / HybridEP capacity check assume." After this PR the decode cap is TorchTitan's, not SGLang's default. The claim still holds: max_num_seqs decode tokens per DP rank fit in SGLangModelWrapper's max_num_tokens_per_step, which is chunked_prefill_size + max_running_requests // attn_dp_size (model_wrapper.py:358). The comment should say so, for example: "The engine's decode graphs stop at max_num_seqs, a DP rank's decode tokens per step, which is within the step budget that ...".

Questions

  • Unverified (SGLang source not available here): under attention DP, does SGLang read decode.max_bs as a per-DP-rank batch size? The engine passes max_running_requests = max_num_seqs * dp because SGLang splits that value over DP ranks (engine.py:437). If max_bs is also divided by attn_dp_size, or compared against the global count, DP > 1 captures only max_num_seqs / dp rows per rank. That is a regression from the default min(512, max_num_seqs * dp). Please cite the SGLang line that applies max_bs, or run one DP 2 build and check the largest captured decode size.
  • The graph-memory note says nothing reserves memory for the extra graphs, since mem_fraction_static is set by TorchTitan. #6332 adds a user override for max_num_seqs. Has a large max_num_seqs (e.g. 4096, 112 more graphs) been built once on a large model, to show that capture doesn't OOM? If it hasn't, a maintainer should decide whether the vLLM-parity rule is enough, or whether the cap should be bounded.
Details
  • Gate: the problem is real (decode batches above 512 rows run eagerly on main when max_num_seqs > 512). The fix sits at the right layer (the engine already owns cuda_graph_config), adds no knob, and matches VLLMCudaGraphConfig's decode cap (vllm/config.py:126, :197). The +32% gain is about 5x the reported run-to-run spread.
  • The != "none" guard is required. Per the __post_init__ comment, SGLang applies per-phase graph keys over disable_cuda_graph, so passing cuda_graph_config with "none" could capture graphs.
  • MoE step budget: with no MTP, one decode token per request gives at most max_num_seqs tokens per DP rank, which is within max_num_tokens_per_step. #6414 (SGLang MTP) bans MoE for now, so the k + 1 tokens per verify row don't reach the EP capacity check yet.
  • Overlap search: "sglang cuda graph" found #6414 (MTP), #6415 (GDN prefill graphs), #6200 (full prefill graphs for linear attention) and #6332 (max_num_seqs override). None of them edits the decode cuda_graph_config block, so there's nothing to rebase onto. Through #6332, a user-set max_num_seqs now also sets the graph count.

Not verified: SGLang's handling of cuda_graph_config.decode.max_bs (source not available in this environment), and runs at DP > 1.

Before merge: fill in the human-written summary.

@felipemello1

felipemello1 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Reply from claude: Fixed the comment in bbd4126. max_bs is a per-DP-rank count in SGLang, so max_num_seqs is the right value at DP > 1 too.

The extra_server_args comment. It now says why the decode graphs fit the step budget.

# before
# checks of cuda_graph assume are off. SGLang's default capture sizes
# also stay within the step budget that Dist-MoE's buffers and the
# DeepEP / HybridEP capacity check assume.
# after
# checks of cuda_graph assume are off. SGLang's prefill capture sizes and
# the decode graphs, which stop at max_num_seqs (a DP rank's decode
# tokens per step), stay within the step budget that Dist-MoE's buffers
# and the DeepEP / HybridEP capacity check assume.

decode.max_bs under attention DP. It counts requests per DP rank, as max_num_seqs does. At the pinned SGLang (851642ade):

  1. Under DP, the decode runner picks its graph by the largest per-rank request count: _max_dp_batch_size returns max(forward_batch.original_global_num_tokens_cpu) (model_executor/runner/decode_cuda_graph_runner.py:858-864).
  2. It then checks cuda_graph_bs <= self.max_bs (:921-925). self.max_bs is the largest size in cuda_graph_config.decode.bs (:400-405).
  3. That list is clamped to req_to_token_pool.size (model_executor/runner/base_cuda_graph_runner.py:79-115). That pool is sized per DP rank: resolve_max_num_reqs takes max(1, max_running_requests // self.attn_dp_size) requests per DP worker (mem_cache/kv_cache_configurator.py:3043-3053).
  4. SGLang's own memory estimate also counts graphs per rank: max_bs * attn_dp_size (arg_groups/memory_hook.py:378).

So at DP 2 and max_num_seqs 1024, each rank captures up to 1,024 rows, and nothing divides it again. The default this replaces compares max_bs with the instance-wide max_running_requests (memory_hook.py:184-190).

Large max_num_seqs on a large model. Not built yet. The NOTE in the body says so. The largest build so far: Qwen3.5-0.8B at 1,024, which kept the same KV capacity. A 27B build at 4,096 is on my list. Until it runs, whether to bound the cap is a maintainer's call.

@claude please review

@claude

claude Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 1m 34s —— View job


Needs discussion: nothing is left for the author to fix. Whether to bound the decode-graph cap at a large max_num_seqs is a maintainer's call. Review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Needs discussion -- nothing is left for the author to fix. A maintainer still has to decide whether capping decode graphs at max_num_seqs is enough, given that no large-model or large-max_num_seqs build has run.

What this PR does. For every cuda_graph mode except "none", the engine now caps SGLang's decode graphs at max_num_seqs instead of SGLang's default, min(512, max_running_requests). The prefill settings don't change.

Example.

max_num_seqs = 1024, DP 2, GB300 (per the author's SGLang citations)
main:    each rank captures decode graphs 1..512; a 900-row decode batch on a rank runs eagerly
this PR: each rank captures 1..512, 544, ..., 1024; that batch pads to 928 and replays
max_num_seqs <= 512 on a GB300: same graphs as before

Earlier review, at bbd4126

  • Must fix 1 (stale extra_server_args comment, engine.py:159-162): resolved. The comment now ties the decode cap to the step budget. That matches model_wrapper.py:358-360, where the budget is chunked_prefill_size + max_running_requests // attn_dp_size, and max_running_requests // attn_dp_size equals max_num_seqs (engine.py:439).
  • Question (does decode.max_bs count per DP rank?): answered. The author cites SGLang 851642ade: graphs are chosen by the largest per-rank count (decode_cuda_graph_runner.py:858-864, :921-925), and sizes are clamped to the per-rank req_to_token_pool (base_cuda_graph_runner.py:79-115, kv_cache_configurator.py:183). The two halves are consistent. A count across all ranks would conflict with the per-rank pool clamp. Unverified here: SGLang's source isn't in this environment.
  • Question (graph memory at a large max_num_seqs): still open, and it's a maintainer's call. Nothing reserves memory for the extra graphs, because TorchTitan sets mem_fraction_static itself. At 4,096 that is 112 more graphs than main, and #6332 lets users set max_num_seqs. The author has a 27B build at 4,096 planned. Until it runs, a maintainer decides whether to accept parity with vLLM's rule (VLLMCudaGraphConfig caps at max_num_seqs with the same exposure) or to bound the cap.
Details
  • Re-reviewed every commit since the last full review. That is bbd4126 (a comment-only change) plus a merge of main, which touches no file in this PR.
  • cuda_graph="decode" now passes cuda_graph_config={"decode": {...}} together with disable_prefill_cuda_graph=True. There is no prefill key, so per-phase prefill settings can't re-enable prefill graphs. The author's decode-mode run built and decoded at up to 1,024 rows. The description doesn't say prefill stayed eager in that run. Worth one line if it was checked.
  • The PR adds no knob and changes no shared code.

Not verified: the SGLang lines the author cited, and builds at DP > 1 or at a large max_num_seqs on a large model.

Before merge: fill in the human-written summary.

@felipemello1

felipemello1 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Reply from claude: I ran the large build. On Qwen3.5-27B, max_num_seqs 4096 captures the same decode graphs as 256, because SGLang clamps the graph list to the request pool it can afford.

Qwen3.5-27B, one GB300, gpu_memory_limit 0.85, breakable prefill graphs, this PR's head (bbd4126), one build each:

max_num_seqs   build    decode capture   decode graphs            graph memory   KV tokens
256            121.1 s  12.0 s           19 (bs 1 ... 112, 119)   0.07 GB        1,562,226
4096           117.2 s  11.7 s           19 (bs 1 ... 112, 119)   0.07 GB        1,562,226

Why they match:

  1. SGLang caps the requests by memory: "max_running_requests is capped to 119 by the mamba state cache (max_mamba_cache_size=596, 5 state slots per request)" (mem_cache/kv_cache_configurator.py:3043-3076, resolve_max_num_reqs).
  2. The decode runner drops every graph size above the request pool (model_executor/runner/base_cuda_graph_runner.py:80, :115). So 4096 yields the same 19 graphs as 256.

So the cap's worst case is bounded by how many requests fit in memory. The extra graphs appear only when memory holds that many requests: Qwen3.5-0.8B at 1,024 added 16 graphs and kept its KV capacity (body). A dense model with a large max_num_seqs is the case left untested.

@felipemello1

felipemello1 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Reply from claude: A follow-up with a dense model, where SGLang doesn't cap the requests. On Qwen3-14B, max_num_seqs 4096 captures 164 decode graphs instead of 36. Decode capture takes 48.9 s instead of 14.8 s, and the decode graphs hold 3.36 GB instead of 0.09 GB.

Qwen3-14B, one GB300, gpu_memory_limit 0.85, breakable prefill graphs, this PR's head (bbd4126), one build each:

max_num_seqs   build    decode graphs          decode capture   decode graph memory   KV tokens
256            122.4 s  36 (bs 1 ... 256)      14.8 s           0.09 GB               1,333,899
4096           153.0 s  164 (bs 1 ... 4096)    48.9 s           3.36 GB               1,333,714

What it means:

  1. On main, the decode graphs stop at 512 on this GPU: 52 of these 164 sizes. A decode batch above 512 requests runs eagerly. This PR adds the other 112.
  2. The KV pool keeps its size. The graphs take their memory from what gpu_memory_limit leaves for activations: 36.9 GB stayed free after the build at 256, 30.7 GB at 4096.
  3. Part of that drop is not this PR: the prefill graphs also grew, 3.84 -> 6.75 GB. This PR sets only the decode graph sizes (cuda_graph_config["decode"]["max_bs"]), and max_num_seqs sets SGLang's max_running_requests on main too. I read that in the code; I did not build main at 4096.

So at 4096 requests per DP rank, a dense 14B pays about 34 s more decode capture at startup and 3.3 GB more decode graph memory, in exchange for no eager decode steps above 512 requests. Whether to cap that is the maintainers' call.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant