Repository navigation
[rl] FULL graphs: cap each capture request at max_model_len - #5488
felipemello1 merged 4 commits into
Conversation
Under FULL, the generator captures every size up to max_num_batched_tokens (2048 by default). vLLM captures a size with dummy requests as long as the size, so when max_model_len is smaller, attention reads past each dummy's KV block table: an illegal memory access during capture on H100 (GPT-OSS and Qwen3 debug models at max_model_len 1024). get_vllm_compilation_config now takes max_model_len. In FULL, the capture cap is at most max_model_len and larger sizes (explicit capture_sizes, or max_num_seqs) are dropped with a warning; it raises if none is left. FULL_DECODE_ONLY is unchanged: its dummies are one-token requests.
cd21e39 to
cb5ef25
Compare
|
@claude please review |
|
Claude finished @felipemello1's task in 2m 51s —— View job Approve (please address the items before merging): review -- |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. The fix is at the right layer: torchtitan raises the FULL capture cap past max_model_len, and this filter brings it back under. It updates every callsite. The only missing piece is the upstream TODO. A maintainer still needs to review.
What this PR does. Under FULL CUDA graphs, torchtitan extends the capture cap to max_num_batched_tokens (default 2048). The PR's diagnosis is that vLLM captures each size with dummy requests that long, so when max_model_len is below the cap, those dummies read past their KV block table row. The PR clamps the cap to max_model_len and drops capture sizes above it. FULL_DECODE_ONLY is unchanged. All 3 production/test callers now pass max_model_len=model.max_context_length.
Example.
FULL, max_num_seqs=8, max_num_batched_tokens unset (2048), max_model_len=1024
before: sizes [1, 2, 4, ..., 1024, 2048] -> 2048-token dummy reads 128 pages of a 64-page row
after: sizes [1, 2, 4, ..., 1024] -> steps with >1024 tokens run eager
FULL, capture_sizes=[512, 2048], max_model_len=1024
before: [512, 2048] after: [512] + warning
FULL, capture_sizes=[2048], max_model_len=1024
before: [2048] after: ValueError
Must fix
torchtitan/rl/generator/vllm/config.py:149-- this works around vLLM's capture dummy run and has noTODOwith an upstream vLLM issue link (principle 3). Details inline.
Should consider
torchtitan/rl/generator/vllm/config.py:151-163-- for explicitcapture_sizesabovemax_model_len, raise instead of warning and dropping them. Details inline.torchtitan/rl/generator/vllm/attention/linear_attention.py:156-166(not in diff) -- the GDN/KDAmax_seq_len > block-table capacitycheck exists to catch exactly this capture overrun ("CUDA graph capture sizes (bounded by max_num_batched_tokens) must not exceed max_model_len"). Its error message names the old bound. With this PR, capture should no longer reach that check, and real requests are bounded bymax_model_len. If you agree no other path reaches it, delete it here so the invariant is enforced in one place. Otherwise, update the message to point at this filter.
Questions
- With
max_num_seqs=8and a 2048-token capture, I'd expect vLLM's_dummy_runto split the tokens acrossmin(num_tokens, max_num_seqs)requests (about 256 each). That would fit in a 1024-token row. Your memcheck shows a 2048-token read. Which vLLM code path builds one request of lengthsize(or setsseq_lensto it)? If the dummy length actually depends onmax_num_seqs, the boundsize <= max_model_lenis stricter than needed. Either way, put the answer in the upstream issue.
Details
- Gate: the problem is real on
main(memcheck shows 1,056 errors). Layer: native vLLM's default cap (min(2 * max_num_seqs, 512)) wouldn't reachmax_model_lenfor these recipes; torchtitan's extension tomax_num_batched_tokensis what crosses it, so filtering in our config is reasonable. The vLLM runner still builds an out-of-bounds dummy without complaint, so it's worth reporting upstream. - Callsites:
engine.py:295,generate.py:176andtest_bitwise_parity.py:262all passmax_context_length, the same value used forengine_kwargs["max_model_len"]. All 15 test callers updated. - The
max_num_seqs > max_model_lencase also drops the exact decode-batch size under FULL. The controller capsmax_num_seqsat 512, so this only affectsmax_model_len < 512. - The new test guards an otherwise silent out-of-bounds read: the 1024-token CI recipes don't fault. It earns its place.
Not verified: vLLM's dummy-run request layout (vLLM isn't installed here), and the GPU results.
| if self.mode == "FULL": | ||
| removed_sizes = [size for size in sizes if size > max_model_len] |
There was a problem hiding this comment.
This works around vLLM behavior: during FULL capture, the dummy run builds requests longer than max_model_len, so they overrun the KV block table. The PR description says so ("That's vLLM's runner"), but the code has no TODO, and CLAUDE.md principle 3 requires a TODO with an upstream issue link for vLLM gaps. Without one, nobody will know to delete this filter once vLLM caps the dummy length. It also tells readers where the "dummy requests as long as the size" claim comes from (please name the vLLM function in the issue).
| if self.mode == "FULL": | |
| removed_sizes = [size for size in sizes if size > max_model_len] | |
| if self.mode == "FULL": | |
| # TODO: vLLM's FULL capture dummy run builds requests as long as the capture | |
| # size without capping them at max_model_len. Drop this once vLLM caps them: | |
| # <vLLM issue link> | |
| removed_sizes = [size for size in sizes if size > max_model_len] |
| if removed_sizes: | ||
| logger.warning( | ||
| "CUDA graph capture sizes %s are removed because they exceed " | ||
| "max_model_len %d", | ||
| removed_sizes, | ||
| max_model_len, | ||
| ) | ||
| sizes = [size for size in sizes if size <= max_model_len] | ||
| if not sizes: | ||
| raise ValueError( | ||
| f"No CUDA graph capture sizes are <= max_model_len {max_model_len}; " | ||
| f"got capture_sizes {self.capture_sizes}" | ||
| ) |
There was a problem hiding this comment.
Should consider: on the auto-derived path, line 130 already caps cap at max_model_len. So this block only drops something when (a) the user set capture_sizes explicitly, or (b) max_num_seqs > max_model_len. In case (a), the user asked for a size that can't be captured, and principle 8 says to raise rather than warn and continue. No in-repo recipe sets capture_sizes, so raising breaks nothing. The "Why not raise" argument in the description covers only the auto path (the 1024-token CI recipes). One option: raise when any explicit size is above max_model_len, and keep the silent filter for the auto path only. That also removes the "none left" branch for explicit sizes. (The ESP filter below does warn-and-drop on explicit sizes too, but that's pre-existing, so not this PR's concern.)
Address review: - TODO naming vLLM's GPUModelRunner._dummy_run, which gives every capture dummy request a sequence length equal to the capture size. - Explicit cuda_graph.capture_sizes above max_model_len now raise in FULL mode instead of being dropped with a warning. Auto sizes stop at the cap. - The GDN/KDA block-table check's message points at VLLMCudaGraphConfig.
|
Reply from claude: @claude please review again. Changes in aa074ae:
|
|
Claude finished @felipemello1's task in 1m 8s —— View job Approve (please add the vLLM issue link to the TODO before merging): review |
There was a problem hiding this comment.
Verdict: Approve -- please add the vLLM issue link to the TODO before merging. aa074ae resolves both earlier findings at HEAD and adds no new problems. A maintainer still needs to review.
Earlier items
- Must fix, TODO at the vLLM workaround (
config.py:130-132): partly resolved. The TODO is there and namesGPUModelRunner._dummy_run, but it still has no upstream issue link. Principle 3 asks for one. Since you're filing the issue, add the link in this PR, not in a follow-up. - Should consider, raise on explicit sizes: resolved.
config.py:140-144raises in FULL whenmax(capture_sizes) > max_model_len. Auto sizes stop atcap. The drop-and-warn block and its "none left" branch are gone. - Should consider, GDN/KDA check: resolved. The message now points at
VLLMCudaGraphConfig. Keeping the check as a guard on vLLM's input is reasonable. - Question about the dummy length: answered. Each dummy's
seq_lensismax_query_len = N, whatevermax_num_seqsis, so thesize <= max_model_lenbound is tight, not stricter than needed. Put the three line references in the upstream issue.
Example (HEAD)
FULL, max_num_seqs=8, max_model_len=1000, auto -> [1, 2, ..., 512, 1000]
FULL, max_num_seqs=1500, max_model_len=1000, auto -> [..., 512, 1000] (1500 dropped by the cap filter)
FULL, capture_sizes=[512, 2048], max_model_len=1000 -> ValueError
FULL_DECODE_ONLY, max_num_seqs=1500 -> [..., 1024, 1500] (unchanged)
Details
config.py:156: the newsize <= capfilter only removes anything in FULL whenmax_num_seqs > max_model_len. In FULL_DECODE_ONLYcap == max_num_seqs, so it's a no-op there. The ESP divisibility filter runs after it. Powers of 2 up to 512 survive any power-of-2 ESP degree, so the "no sizes left" raise can't newly trigger for the auto path.- Callsites:
engine.py:295andgenerate.py:176pass the samemax_context_lengththey put inengine_kwargs["max_model_len"](engine.py:269,generate.py:167). So doestest_bitwise_parity.py:262. No in-repo recipe setscapture_sizes, so the new raise breaks no recipe. The GPU GDN tests build vLLMCompilationConfigdirectly and skip this path.
Not verified: the vLLM _dummy_run line references (vLLM isn't installed here), and the GPU results.
This may make sense, but I want to check one thing.
Are we saying that in inference, continuous batching would never pack multiple prefill sequences together as one model input? It'd be hard to believe for me. But if that's not the case, why cudagraph capture would cap at |
|
Claude finished @tianyu-l's task in 45s —— View job You're right: continuous batching does pack several prefills into one step, up to DetailsWhat the PR caps, and what it doesn't
Why capture can't go past
|
Are we sure this is OK and doesn't bring regression on some workload? Also it seems we should fix the problem in vllm rather than here. |
…capture sizes Continuous batching packs several requests into one step, up to max_num_batched_tokens. Capping capture sizes at max_model_len left steps above it without a graph (eager). - TorchTitanGPUModelRunner._dummy_run passes vLLM's profile_seq_lens = max_model_len to a mixed dummy batch larger than max_model_len. vLLM gives each dummy request seq_len = the whole batch, past its KV block table. - VLLMCudaGraphConfig: FULL captures up to max_num_batched_tokens again, bounded by max_num_seqs * max_model_len (no step holds more). Explicit sizes above that raise.
|
Reply from claude: You're right on both points, thanks. Changed in 8aaca73.
It shouldn't. Continuous batching packs requests into one step up to
The only bad input was the capture dummy: vLLM gives each dummy request Check, 1x GB300, Qwen3.5-0.8B,
Agreed it belongs in vLLM: the override has a TODO to go once vLLM caps the dummy, and a vLLM issue is drafted. |
|
Claude finished @felipemello1's task in 2m 2s —— View job Approve (please add the vLLM issue link to the TODO and an H100 rerun of the original failing config on this head before merging): review |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging. 8aaca73 fixes the bad input itself, the capture dummy, so FULL graphs again cover the full max_num_batched_tokens budget. This answers tianyu-l's point that real steps pack several prefills. A maintainer (tianyu-l) still needs to review.
What this PR does. TorchTitanGPUModelRunner._dummy_run overrides vLLM's private _dummy_run. For a mixed (non-uniform-decode) dummy batch above max_model_len, it passes profile_seq_lens=max_model_len, so no dummy request reads past its KV block table row. FULL capture sizes go up to max_num_batched_tokens, capped at max_num_seqs * max_model_len. Explicit sizes above that cap raise.
Example.
FULL, max_model_len=1024, max_num_seqs=8, budget 2048
main: capture 2048 as 8 x 256 tokens, each seq_len 2048 -> 128 pages from a 64-page row
previous: sizes capped at 1024 -> steps of 1025..2048 tokens run eager
HEAD: capture 2048 as 8 x 256 tokens, each seq_len 1024; sizes [1, ..., 1024, 2048]
FULL, max_num_seqs=1, max_model_len=1000 -> sizes [..., 512, 1000]; capture_sizes=[2048] -> ValueError
Must fix
torchtitan/rl/generator/vllm/worker.py:130: still open. The TODO has no vLLM issue link. It now also covers overriding a private vLLM method (_dummy_run) and itsprofile_seq_lenskwarg (principle 3). Add the link once the drafted issue is filed.- Evidence at this head for the original failure: the Results section shows only Qwen3.5 (GDN) on GB300. The illegal memory access on H100 (GPT-OSS debug and Qwen3 debug, TP1,
max_model_len1024) was only checked against the earlier head, which never captured 2048. This head captures 2048 again through a different mechanism. Rerun one of those configs on this head without memcheck and add the result. The original fault reproduced without memcheck, so a clean run is enough.
Should consider
torchtitan/rl/generator/vllm/worker.py:131-136: in an edge case, the largest dummy request can still get a query longer than itsseq_len. Make that case raise. Details inline.
Details
- Maintainer comments (tianyu-l): "cap at the real longest step, not
max_context_length" is resolved in code (config.py:133). "Regression on some workload?": at HEAD, no step runs eager that didn't on main, and graphs up to the budget are captured as on main. The only change is the dummy'sseq_lenduring capture. "Fix it in vLLM": agreed by the author; it depends on item 1. - Why the
max_num_seqs * max_model_lencap is needed and not just cosmetic: withmax_num_seqs=1, a 2048 capture would be one request with query 2048 andseq_len1024. The cap prevents that. - Uniform-decode dummies keep
seq_len = 1, and an explicitprofile_seq_lens(vLLM'sprofile_run) is passed through. Dummies at or belowmax_model_lenare unchanged. linear_attention.py:164-165: the new message is accurate. Keeping the check as a guard is fine.- The new CPU test guards the cap arithmetic. The override itself has no CPU coverage; the GB300 run is its evidence.
Not verified: vLLM's _dummy_run request split and how it applies profile_seq_lens (vLLM isn't installed here), and all GPU results.
| if ( | ||
| profile_seq_lens is None | ||
| and not uniform_decode | ||
| and num_tokens > self.max_model_len | ||
| ): | ||
| profile_seq_lens = self.max_model_len |
There was a problem hiding this comment.
Should consider (unverified: depends on how vLLM splits the dummy's tokens). The max_num_seqs * max_model_len cap in config.py:133 guarantees the average dummy request fits in max_model_len, not the largest one. If vLLM gives the remainder to the last request ([n // r] * r, last += n % r), a size near the cap that max_num_seqs doesn't divide gives that request a query longer than the seq_len set here:
max_num_seqs=3, max_model_len=1000, max_num_batched_tokens=2999 -> cap 2999
dummy: queries [999, 999, 1001], each seq_len 1000 -> last query_len > seq_len
Then num_computed_tokens goes negative for that request during capture. Recipes with power-of-2 max_num_seqs don't hit this, so a full fix isn't needed. Make it fail loudly instead, either by asserting here that the largest dummy request fits (num_tokens // num_reqs + num_tokens % num_reqs <= self.max_model_len, with num_reqs = min(num_tokens, self.max_num_reqs)) or by tightening the cap in config.py.
| max_model_len=1024, capture 2048 tokens as 8 requests of 256 | ||
| vLLM: each seq_len 2048 -> reads 128 KV pages from a 64-page row | ||
| here: each seq_len 1024 -> reads its 64 pages |
There was a problem hiding this comment.
hmmmm really sounds a vllm "bug / feature", which we would prefer not to play with.
If you created a vllm issue, could you link it here?
TLDR: Under
FULLCUDA graphs, vLLM captures each graph with fake requests whose sequence length is the whole graph size. When that is abovemax_model_len, attention reads past the request's KV block table: an illegal memory access, or aValueErrorat engine start for GDN/KDA models. The generator now caps each fake request atmax_model_lenand keeps graphs up tomax_num_batched_tokens, so no step runs eager.Problem
FULLcaptures a graph for every size up tomax_num_batched_tokens(2048 by default). Continuous batching packs several requests into one step, up to that budget.min(N, max_num_seqs)requests, but every request getsseq_len = N(GPUModelRunner._dummy_run).max_model_lentokens. Atmax_model_len=1024, the 2048 dummy reads 128 pages from a 64-page row.ValueErrorat engine start. Only setups withmax_model_lenbelowmax_num_batched_tokenshit it: the 1024-token CI recipes. Shipped recipes use 2048 or more.Solution
TorchTitanGPUModelRunner._dummy_runpasses vLLM's ownprofile_seq_lens = max_model_lento a mixed dummy batch bigger thanmax_model_len. Uniform decode dummies (one token per request) are unchanged.FULLgraph sizes still go up tomax_num_batched_tokens, capped atmax_num_seqs * max_model_len: no step holds more. So every step the scheduler can build has a graph.capture_sizesabovemax_num_seqs * max_model_lenraise.Results
max_model_len), H100, TP1: main hit an illegal memory access in capture for GPT-OSS debug and Qwen3 debug (memcheck: 1,056 errors in the paged-KV load); that head ran with 0 errors, but steps above 1024 tokens ran eager.You may ask:
max_model_len, as an earlier version did? Steps that pack more tokens thanmax_model_lenwould run eager.NOTE: memcheck was not rerun on this head (the GB300 host has compute-sanitizer 12.8; the stack is CUDA 13.2). Measured on GB300 and H100; not on B200.
Test plan