Skip to content

[rl] Generator: vLLM's dense CUDA graph sizes, plus a prefill tail - #5383

Merged
felipemello1 merged 1 commit into
mainfrom
felipemello1/gen-perf/4-vllm-capture-sizes
Nov 6, 2026
Merged

felipemello1 merged 1 commit into
mainfrom
felipemello1/gen-perf/4-vllm-capture-sizes

Conversation

@felipemello1

@felipemello1 felipemello1 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

TLDR: The generator's default CUDA graph sizes go from powers of 2 to vLLM's spacing, plus a coarse tail for FULL prefill. 34 running sequences now pad to the 40 graph instead of the 64 graph. On a 4-layer Qwen3.5-35B-A3B, a 64-row decode step takes 2408 us and a 34-row step 2072 us.

Problem

  1. Padding. vLLM runs each step on the smallest captured CUDA graph that fits it, and pads the batch to that size.
  2. Today. VLLMCudaGraphConfig captures powers of 2 up to the cap, plus max_num_seqs and the cap.
  3. Agent rollouts. A generator often runs a few dozen sequences: 34 sequences pad to the 64 graph.
  4. Cost. 4-layer Qwen3.5-35B-A3B, 2k context, GB300: a 64-row decode step takes 2408 us, a 34-row step 2072 us (+16%).
before:  1 2 4 8 16 32 64 128 256 ... 16384                                                 (+ max_num_seqs)
after:   1 2 4 | 8 ... 248 (+8) | 256 ... 1024 (+16) | 1536 ... 4096 (+512) | 6144 ... 16384 (+2048)   (+ max_num_seqs)

Solution

  1. _default_capture_sizes: 1, 2, 4, every 8 to 256, every 16 to 1024 (or to max_num_seqs, if larger). This is vLLM's own spacing, and 1024 is vLLM's default limit on Blackwell.
  2. FULL also graphs prefill, so above that it adds every 512 up to 4096, then every 2048 up to max_num_batched_tokens.
  3. max_num_seqs stays an exact size: FULL decode graphs stop at max_num_seqs, so a full batch must not pad past it.
  4. Unchanged: explicit capture_sizes, the cap, and the expert-parallel size filter.

Results

Numerics: not compared. A step now replays a smaller padded graph, so its GEMMs can pick other tiles (rounding only).

4-layer Qwen3.5-35B-A3B, GB300, TorchTitan V1 runner, FULL graphs, max_num_batched_tokens 16384

decode, 2k context, kernel us per step
B=34          2072   <- this PR pads 34 to 40 (the 40 graph was not timed)
B=64          2408   <- where 34 sequences pad on main

prefill, wall per request (2k-16k new tokens): with the +512/+2048 sizes vs no graph above 1024
median   1.81x -> 1.00x
worst    2.83x -> 1.00x
Engine init, Qwen3.5-9B, TP2, 2x GB300, FULL, max_num_seqs 230,
max_num_batched_tokens 16384, max_model_len 16384. Two runs per side:
cold = first run on the host (kernels compile), warm = second run (compile cache hit)
                                        before (powers of 2)        this PR
graphs captured (mixed + decode)        25 (16 + 9)                 128 (96 + 32)
graph capture time, cold / warm         73 s / 21 s                 81 s / 43 s
engine init, cold / warm                236.6 s / 44.2 s            246.0 s / 61.8 s
graph memory, real, cold / warm         0.81 / 0.81 GiB             1.40 / 2.59 GiB
vLLM's graph-memory estimate            8.76 GiB                    2.54 GiB
  = largest + (n - 1) x 2nd largest     1264 + 15 x 514 MiB         1264 + 95 x 14 MiB
KV cache                                228.88 GiB, 12.00M tokens   235.11 GiB, 12.33M tokens (+2.7%)
  • Capture time: the first graph takes about 46 s on a cold host (kernel compiles), which hides the difference. Warm, capture doubles: +22 s, and engine init grows by 18 s.
  • Memory: vLLM captures only the two largest graphs and charges every other graph like the second one. It takes that estimate, not the real graph memory, out of the KV cache.
  • Before, the second largest graph (8192 tokens) added 514 MiB, so 15 graphs were charged 7.5 GiB against 0.81 GiB real. With this PR it is 14336 tokens and adds 14 MiB, so the estimate (2.54 GiB) lands near the real 1.40-2.59 GiB. The KV cache grows because the old estimate over-reserved.
  • That depends on the two largest sizes, so other settings can land differently. Real graph memory differed between the two identical runs of this PR (1.40 vs 2.59 GiB); not investigated.

You may ask:

  • Why not stop where vLLM's default does, at min(2 x max_num_seqs, 1024) on Blackwell? Prefill steps above it would run without a graph: 1.8x slower.
  • Why keep max_num_seqs as its own size? FULL decode graphs exist only up to max_num_seqs. A full batch of e.g. 230 padded to 232 finds no decode graph and replays the mixed graph, with the slower mixed-batch GDN/KDA and FA4 kernels (eager in batch-invariant mode).

NOTE: Not measured: the new prefill sizes vs main's powers of 2 (which also graph 2048 to 16384, with more padding).

Test plan

pytest tests/rl/unit_tests/cpu   # 563 passed
# engine init: a recipe wrapping rl_grpo_qwen3_5_9b_varlen with max_num_batched_tokens and
# max_context_length set to 16384, on this PR and on its base
torchrun --nproc-per-node=2 -m torchtitan.rl.generate --module <recipe module> --config <config> --max-num-seqs 230 --max-tokens 16

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 14, 2026
@felipemello1
felipemello1 added this pull request to stack #5386 October 14, 2026 03:04
@felipemello1
felipemello1 removed this pull request from stack #5386 October 14, 2026 03:13
@felipemello1
felipemello1 changed the base branch from felipemello1/gen-perf/3-train-gather-combine to felipemello1/qwen35-router October 14, 2026 03:14
@felipemello1
felipemello1 force-pushed the felipemello1/gen-perf/4-vllm-capture-sizes branch from 9b59185 to 2d3bfba Compare October 14, 2026 03:14
@felipemello1 felipemello1 changed the title [rl] Generator: vLLM's dense CUDA graph sizes, plus a prefill tail (4/6) [rl] Generator: vLLM's dense CUDA graph sizes, plus a prefill tail (2/6) Oct 14, 2026
@felipemello1
felipemello1 changed the base branch from felipemello1/qwen35-router to felipemello1/gen-perf/5-gdn-metadata October 14, 2026 03:14
@felipemello1
felipemello1 added this pull request to stack #5387 October 14, 2026 03:15
@felipemello1
felipemello1 removed this pull request from stack #5387 October 14, 2026 03:23
@felipemello1
felipemello1 changed the base branch from felipemello1/gen-perf/5-gdn-metadata to main October 14, 2026 03:24
@felipemello1
felipemello1 force-pushed the felipemello1/gen-perf/4-vllm-capture-sizes branch from 2d3bfba to 3e832f6 Compare October 14, 2026 03:24
@felipemello1 felipemello1 changed the title [rl] Generator: vLLM's dense CUDA graph sizes, plus a prefill tail (2/6) [rl] Generator: vLLM's dense CUDA graph sizes, plus a prefill tail (3/7) Oct 14, 2026
@felipemello1
felipemello1 changed the base branch from main to felipemello1/gen-perf/5-gdn-metadata October 14, 2026 03:24
@felipemello1
felipemello1 added this pull request to stack #5388 October 14, 2026 03:25
@felipemello1 felipemello1 changed the title [rl] Generator: vLLM's dense CUDA graph sizes, plus a prefill tail (3/7) [rl] Generator: vLLM's dense CUDA graph sizes, plus a prefill tail (3/9) Oct 15, 2026
@felipemello1
felipemello1 removed this pull request from stack #5388 October 18, 2026 04:16
@felipemello1
felipemello1 changed the base branch from felipemello1/gen-perf/5-gdn-metadata to main October 18, 2026 04:16
@felipemello1
felipemello1 force-pushed the felipemello1/gen-perf/4-vllm-capture-sizes branch from f134db5 to 32633b3 Compare October 18, 2026 04:17
@felipemello1
felipemello1 changed the base branch from main to felipemello1/inference-graph-sizes/1-drop-full-sizes-above-max-model-len October 18, 2026 04:17
@felipemello1
felipemello1 added this pull request to stack #5496 October 18, 2026 04:18
@felipemello1 felipemello1 changed the title [rl] Generator: vLLM's dense CUDA graph sizes, plus a prefill tail (3/9) [rl] Generator: vLLM's dense CUDA graph sizes, plus a prefill tail Oct 18, 2026
@felipemello1
felipemello1 force-pushed the felipemello1/gen-perf/4-vllm-capture-sizes branch from 32633b3 to 68ea196 Compare November 6, 2026 01:31
@felipemello1

Copy link
Copy Markdown
Contributor Author

@claude please review

@claude

claude Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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


Request changes -- see the review. The KV/init cost of 6x more FULL graphs isn't measured yet, the decode-graph count in the worker.py docstring is stale, and the comment on keeping max_num_seqs as its own size is wrong for FULL mode.

@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: Request changes -- the PR raises the number of FULL graphs about 6x (16 -> 96 at max_num_seqs 230), but the engine-init and KV-block cost of that is still <FILL IN>, and a stale docstring in worker.py gives the old graph count.

What this PR does. It replaces the generator's power-of-2 CUDA graph sizes with vLLM's spacing: 1, 2, 4, then every 8 up to 256, then every 16 up to max(1024, max_num_seqs). For FULL prefill it adds a coarse tail: every 512 up to 4096, then every 2048 up to the cap. max_num_seqs and the cap stay as exact sizes.

Example. max_num_seqs=230, max_num_batched_tokens=16384, FULL:

34 decodes      main: pad to 64     PR: pad to 40
230 decodes     main: 230 (exact)   PR: 230 (exact)
1100-tok chunk  main: pad to 2048   PR: pad to 1536
9000-tok chunk  main: pad to 16384  PR: pad to 10240
FULL graphs     main: 16            PR: 96   (decode graphs 9 -> 32)

Must fix

  1. PR description -- "engine init and KV blocks: <FILL IN BEFORE POSTING>" is still a placeholder. This is the main cost of the PR. worker.py:116-118 says vLLM's graph-memory estimate, which is taken out of the KV cache, charges every FULL mixed graph like the second-largest one. If that estimate scales with the graph count, 16 -> 96 mixed graphs (sizes up to 14336 tokens) could shrink the KV cache noticeably, and capture time grows too. I couldn't verify how vLLM computes the estimate. Please fill in the before/after for init time and KV blocks on one recipe. If KV loss is large, thin the 256-1024 +16 band for FULL (prefill doesn't need +16 there).
  2. torchtitan/rl/generator/vllm/worker.py:47-48 (not in the diff) -- "one additional graph capture per decode-specialized batch size (11 at max_num_seqs 1024)". With this PR it is 83 at max_num_seqs 1024 (3 + 31 + 49). The 0.03 GiB figure for 11 decode graphs at worker.py:117-118 also no longer describes the default. Update both, or drop the specific numbers.
  3. config.py:201-202 -- the comment (and the "runs eager" answer in the description) is wrong for FULL mode: that batch replays the mixed graph. See the inline comment.

Should consider

  • test_generator.py:908-916 -- exact-list asserts copy the implementation. Assert the invariants instead (inline).

Questions

  • The description calls +16 up to 1024 "vLLM's own spacing" and "vLLM's default". As far as I know, vLLM's default max_cudagraph_capture_size is min(2 * max_num_seqs, 512). Is the 1024 bound picked on purpose (evidence for 512-1024 at +16 rather than coarser)?
Details
  • Gate: the problem is real: padding 34 rows to 64 costs +16% per decode step, as measured in the description. This is the right layer: torchtitan already owns _default_capture_sizes, and vLLM's own +16 default would mean ~1000 graphs up to 16384. No overlapping PR found.
  • I checked the graph counts in the description against the code: 3 + 31 + 49 + 1 (230) + 6 + 6 = 96, and decode sizes up to 230 = 32. Both match.
  • max_num_seqs > 1024: the dense +16 band extends to max_num_seqs, e.g. 4096 gives ~240 graphs below the tail. This is intentional per the docstring, but it multiplies the cost in item 1 for large-batch recipes.
  • The expert-SP filter still works: every size from 8 up is a multiple of 8, and only 1/2/4 and a non-aligned max_num_seqs get dropped (same as before).
  • The 1024 -> 1536 gap pads a 1040-token chunk by about 48%, which is still better than main's pad to 2048.

Not verified: vLLM's graph-memory estimate formula and the resulting KV-block change (no GPU, vLLM source not readable here). A maintainer still needs to review.

Comment thread torchtitan/rl/generator/vllm/config.py Outdated
Comment on lines +201 to +202
# Linear-attention (GDN, KDA) decode graphs stop at max_num_seqs. Without this size,
# at max_num_seqs=230 a batch of 225-230 pads to 232 and runs without a CUDA graph.

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.

This says the wrong thing about what happens without this size. In FULL mode, TorchTitanCudagraphDispatcher.dispatch (worker.py:87-94) sends a uniform-decode batch above max_decode_capture_size to the mixed FULL graph. It runs eager only in batch-invariant mode. test_full_mode_decode_dispatch covers this: "3 decodes pad to 8, past the largest decode graph (2): mixed graph". So a batch of 225-230 replays the 232 mixed graph, with the slower mixed-path GDN/KDA and FA4 kernels. It doesn't run without a graph. In FULL_DECODE_ONLY, cap == max_num_seqs, so cap already adds this size and the separate entry only matters for FULL. The PR description's "runs eager" answer needs the same correction.

Suggested change
# Linear-attention (GDN, KDA) decode graphs stop at max_num_seqs. Without this size,
# at max_num_seqs=230 a batch of 225-230 pads to 232 and runs without a CUDA graph.
# FULL decode graphs (decode kernels for GDN/KDA and FA4) stop at max_num_seqs. Without this
# size, at max_num_seqs=230 a batch of 225-230 pads to 232 and replays the mixed graph instead.

Comment on lines +908 to +916
assert cfg.cudagraph_capture_sizes == [
*[1, 2, 4],
*range(8, 256, 8), # +8
*range(256, 500, 16), # +16
500, # max_num_seqs: a full decode batch has its own graph
*range(512, 1025, 16), # +16
*range(1536, 4097, 512), # +512
*range(6144, 16385, 2048), # +2048
]

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.

Optional: this rebuilds _default_capture_sizes with the same range calls, so it snapshots the ladder and every spacing tweak has to be copied here. It doesn't check the invariants the PR depends on. Assert those instead: 40 in sizes (34 sequences pad to 40), 500 in sizes (a full decode batch gets an exact graph), and sizes[-1] == 16384 (the largest chunk is graphed). The FULL_DECODE_ONLY test above has the same issue.

@felipemello1
felipemello1 force-pushed the felipemello1/gen-perf/4-vllm-capture-sizes branch from 68ea196 to b547051 Compare November 6, 2026 01:59
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review again. Rebased on #5488's new head; head b547051.

  • Must fix 1: measured. Qwen3.5-9B TP2, 2x GB300, max_num_seqs 230, 16384 budget and context: KV cache 228.88 -> 235.11 GiB, real graph memory 0.81 -> 1.40 GiB, capture 73 -> 81 s, engine init 236.6 -> 246.0 s. Table and why in the body.
  • Must fix 2: worker.py docstring now says 83 decode graphs at max_num_seqs 1024; the Qwen3-8B number is labeled as a sample of 11 decode graphs.
  • Must fix 3: the comment and the body now say a 225-230 batch replays the mixed graph (eager only in batch-invariant mode).
  • Tests assert the invariants (40, 500, 16384 in the sizes) instead of the full list.
  • Your question: 1024 is vLLM's default limit on Blackwell (min(2 x max_num_seqs, 1024); 512 on other GPUs), vllm/config/vllm.py lines 2221-2227 at 9f03b51.

@claude

claude Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 51s —— View job


Approve -- all earlier must-fix items are resolved, see the 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: Approve -- all three must-fix items from the last review are resolved at b547051, and the new commit adds no new problems.

Earlier findings

  1. Engine-init / KV cost: resolved. The description now has the before/after table (Qwen3.5-9B TP2, max_num_seqs 230). KV cache goes up 2.7%, real graph memory goes up 0.59 GiB, and engine init is 4% slower. It also explains why: vLLM's estimate charges each graph like the second largest one, which went from 8192 to 14336 tokens.
  2. worker.py:47-48 graph count: resolved. 83 matches the code at max_num_seqs 1024 (3 + 31 + 49). worker.py:118 now labels the Qwen3-8B 0.03 GiB number as a measurement of 11 decode graphs.
  3. config.py:193-194 comment: resolved. Both the comment and the description now say a 225-230 batch replays the mixed graph, and runs eager only in batch-invariant mode.
  • Tests: resolved. They now check the invariants (40, 500, 16384) instead of copying the size ladder. The [992, 1000] tail in test_cuda_graph_full_mode_drops_sizes_above_max_model_len is correct for max_model_len 1000.
  • Question about the 1024 bound: answered (vLLM's Blackwell default, min(2 x max_num_seqs, 1024)).
Details
  • Example, max_num_seqs=230, FULL, 16384 budget: 34 decodes pad to 40 (main: 64). 230 decodes get an exact graph. A 9000-token chunk pads to 10240 (main: 16384).
  • With expert SP of degree 16, the +8 sizes are filtered out, so 34 sequences pad to 48. That is still better than 64, and the filter behaves as before.
  • One tradeoff stays: the dense +16 band goes up to max_num_seqs when max_num_seqs is above 1024, so large-batch recipes capture many more graphs. The docstring says this is intentional, and the measured KV effect at 230 is positive.

Not verified: vLLM's estimate formula and the vllm/config/vllm.py reference (vLLM source not available here). A maintainer still needs to review.

@felipemello1
felipemello1 force-pushed the felipemello1/gen-perf/4-vllm-capture-sizes branch from b547051 to 326f534 Compare November 6, 2026 02:09
@felipemello1
felipemello1 marked this pull request as ready for review November 6, 2026 02:09
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 6, 2026
@github-actions
github-actions Bot requested a review from pzhan9 November 6, 2026 02:09
"""CUDA graph sizes up to ``cap``. vLLM pads each step up to the next size.

1 2 4
8 16 24 ... 248 +8 vLLM's spacing: 34 sequences pad to 40, not 64

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.

regarding "vLLM's spacing", could you share a code pointer?

if there is one, can we call their function, private or not?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reply from claude: vLLM builds its default list inline in VllmConfig._set_cudagraph_sizes: [1, 2, 4], then every 8 up to 256, then every 16 up to its cap (vllm/config/vllm.py lines 2336-2346 at the pinned 9f03b51; 2697-2707 on main). Its cap is min(2 x max_num_seqs, 1024 on Blackwell, 512 elsewhere) (lines 2226-2227).

We can't call it. It is a method on the VllmConfig that vLLM builds inside the engine, and it uses either our explicit list or its own list, not both (lines 2322-2327 vs 2328-2346). Its own list would continue every 16 up to max_num_batched_tokens: about 1,040 graphs at 16384. So we copy its spacing up to 1024 and add the prefill tail. 3d4dd0d puts this pointer in _default_capture_sizes's docstring.

@felipemello1
felipemello1 force-pushed the felipemello1/gen-perf/4-vllm-capture-sizes branch from 326f534 to 3d4dd0d Compare November 6, 2026 03:20
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review again. Head 3d4dd0d: rebased on #5488's new head (graphs up to max_num_batched_tokens, capped at max_num_seqs * max_model_len; capture dummies capped at max_model_len), and _default_capture_sizes's docstring points at vLLM's ladder (tianyu-l's question).

@claude

claude Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 44s —— View job


Approve: the rebase onto #5488 works with the new size ladder, and tianyu-l's question is answered in the docstring. See the 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: Approve -- the rebase onto #5488's new head works with the new ladder, and tianyu-l's question has an answer in the code. The new head adds no new problems.

What changed since the last review (b547051 -> 3d4dd0d)

  • Rebase: cap is now min(max(max_num_seqs, budget), max_num_seqs * max_model_len) (config.py:126-129). _default_capture_sizes drops every size above cap (config.py:202), so the new ladder works with the lower cap. The docstring examples (config.py:90-91) and the test tails match the code:
    FULL, max_num_seqs=8, max_model_len=1000, budget 2048  -> cap 2048: ... 1024, 1536, 2048
    FULL, max_num_seqs=1, max_model_len=1000, budget 2048  -> cap 1000: ... 976, 992, 1000
    
    In FULL, cap >= max_num_seqs always holds (max_model_len >= 1), so the exact max_num_seqs size is never filtered out.
  • tianyu-l's question at config.py:184 (code pointer, and whether we can call vLLM's function): the author answered it in the thread. The docstring now points to VllmConfig._set_cudagraph_sizes and explains why we can't call it: vLLM uses either our list or its own, and its own list would keep the +16 spacing up to the budget. No pushback yet. tianyu-l should confirm the answer is enough.
  • Since the rebase leaves the cap unchanged at max_num_seqs 230 with budget and context both 16384, the engine-init/KV table in the description should still hold.

Not verified: the vllm/config/vllm.py line references (vLLM source not available here). The PR touches only torchtitan/rl/, and a maintainer still needs to review it.

Base automatically changed from felipemello1/inference-graph-sizes/1-drop-full-sizes-above-max-model-len to main November 6, 2026 14:31
The auto ladder was powers of 2 up to the cap. Agent rollouts keep a generator
at a few dozen sequences, so decode padded up to the next power of 2 (34
running sequences -> the 64 graph).

Use vLLM's default ladder (1, 2, 4, every 8 to 256, every 16 to 1024), then,
for FULL prefill steps, every 512 up to 4096 and every 2048 up to
max_num_batched_tokens.
@felipemello1
felipemello1 force-pushed the felipemello1/gen-perf/4-vllm-capture-sizes branch from 3d4dd0d to 2ac320f Compare November 6, 2026 14:31
@felipemello1
felipemello1 merged commit 8fcc150 into main Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests 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.

2 participants