Skip to content

[rl] Generator: default vLLM's max_num_batched_tokens to vllm serve's per-GPU value - #5952

Draft
felipemello1 wants to merge 18 commits into
mainfrom
felipemello1/gen-default-batched-tokens
Draft

felipemello1 wants to merge 18 commits into
mainfrom
felipemello1/gen-default-batched-tokens

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

TLDR: With max_num_batched_tokens unset, the vLLM generator now uses what vllm serve picks for the GPU: 16,384 on a GB300, 8,192 on an H100, by GPU memory on every GPU platform vLLM supports. On main it got 2,048 on every GPU. SGLang already used its own default, and its code is unchanged. On Qwen3.5-35B-A3B (MoE, 4 GB300s, DP4 x EP4), 16,384 gives 1.72x the prefill and 1.60x the mixed-step throughput of 2,048.

With max_num_batched_tokens unset, a GB300 generator gets vllm serve's 16,384 and FULL CUDA graphs up to the same size. Passing only the usage context would run steps above 2,048 tokens eager (prefill 0.76x of main); this PR gives prefill 1.72x and mixed 1.60x on Qwen3.5-35B-A3B, 4 GB300s.

max_num_batched_tokens unset, per vLLM DP rank     main     this PR
>= 160 GiB (GB300, B200, MI300X)                   2,048    16,384
>= 70 GiB, not A100 (H100, H200)                   2,048     8,192
smaller GPUs and A100 (A10G, A100, L40S)           2,048     2,048
set value                                          kept     kept
SGLang                                             SGLang's own default, unchanged

Problem

  1. max_num_batched_tokens caps the tokens one engine step schedules on a DP rank: one per running decode, plus prefill chunks.
  2. vLLM picks it by GPU memory, but only for vllm serve and the LLM class. The generator builds its engine with LLMEngine.from_engine_args, which falls back to 2,048 on every GPU.
  3. torchtitan turns vLLM's compiler off (CompilationMode.NONE), so it lists the FULL CUDA graph sizes itself, before the engine exists, up to max_num_batched_tokens. A bigger step runs eager.
  4. The controller applied two vLLM rules to max_num_seqs with an assumed 2,048: at most max_num_batched_tokens (vLLM raises otherwise) and at least max_num_batched_tokens / 64 (a FlashAttention-2 bug, flash-attention issue 3117).
  5. On a GB300 with 500 running decodes, 2,048 leaves ~1,500 prefill tokens per step: a 30k-token uncached turn takes ~20 steps instead of 2.

Solution

  1. resolve_scheduler_limits in the vLLM engine returns (max_num_batched_tokens, max_num_seqs). When max_num_batched_tokens is unset, it asks vLLM's config builder what vllm serve picks (create_engine_config(UsageContext.OPENAI_API_SERVER), 0.05 s), then caps it at max_num_seqs x max_model_len, as vllm serve does.
  2. It leaves max_num_seqs out of that call (vLLM raises when it is above vLLM's pick), then fits it: at least max_num_batched_tokens // 64 on CUDA devices below SM90 (FlashAttention-2), at most max_num_batched_tokens everywhere.
  3. The engine, the FULL graph list and both rules use that one number. The controller only sizes concurrency: 2x each DP rank's share, rounded up to a power of two.
  4. SGLang is unchanged: unset passes nothing, so SGLang uses its own default. generate.py builds its engine args with VLLMEngine.build_engine_args instead of its own copy.

Results

Qwen3.5-35B-A3B-Base, 4x GB300, DP4 x EP4, Dist-MoE, FULL CUDA graphs, 800 concurrent rollouts (200 per rank)
setup   max_num_batched_tokens      which steps replay a CUDA graph
A       2,048 (main)                all of them
B       16,384 (this PR on GB300)   all of them
C       16,384                      only steps of up to 2,048 tokens; bigger steps run eager
D       32,768                      all of them

tokens/s over all ranks (ratio vs A)
setup   decode (200 per rank)   prefill (4k prompts)   mixed (200 decodes + prefill)   graph memory / GPU   KV cache   capture
A       56,443                  180,470                159,579                         1.20 GiB             -          100 s
B       56,471                  310,573 (1.72x)        254,744 (1.60x)                 2.67 GiB             -2.5%      129 s
C       50,133                  137,867 (0.76x)        138,640 (0.87x)                 1.20 GiB             -2.3%      114 s
D       57,232                  319,750 (1.77x)        292,381 (1.83x)                 4.30 GiB             -5.4%      130 s

one 16,384-token step, GPU forward: 168.5 ms with a CUDA graph (B), 270.2 ms eager (C)

Conditions: real weights, bf16; max_num_seqs 512, max_model_len 16,384, gpu_memory_limit 0.9; random prompts (no prefix hits); decode = 64-token prompts x 256 output tokens, prefill = 4,096-token prompts x 1 output token, mixed = 200 decoders, then 64 prompts of 4,096 tokens 0.5 s later; torchtitan c561eee, vLLM nightly 1.0.0.dev20261105; one run per row.

Qwen3.5-9B, 1x GB300, Terminal-Bench-shaped multi-turn replay, 2,048 vs 16,384
  FULL graphs   wall 450.7 -> 450.9 s   TTFT p50 148 -> 157 ms   ITL p50 11.4 -> 11.4 ms
                capture 30 -> 33 s, graph pool 0.54 -> 2.00 GiB, KV cache -0.9%
  eager         wall 1366 -> 1348 s     TTFT p50 1125 -> 1025 ms (-9%)
Qwen3.5-9B, 1x H100, 2,048 vs 8,192
  decode-only graphs, TB-shaped   wall -0.1%, KV blocks -0.9%
  FULL graphs, DAPO-shaped        wall -0.6%, KV blocks -1.6%, graph pool 0.89 -> 1.81 GiB
Qwen3.5-27B, 1x GB300, FULL graphs, 100 concurrent multi-turn requests: ITL 37.0 -> 35.7 ms (-3.5%)
Qwen3.5-35B-A3B Terminal-Bench generator as the recipe builds it (eager, DP2 x TP2 x EP4, 65,536 context), 4x GB300:
  starts at 16,384, KV cache 21.0M tokens; decode, prefill and mixed phases run with no OOM and no preemption.

Not measured: an RL run end to end. These runs used earlier heads; on these GPUs they resolve the same values as this one.

You may ask:

  • Why not pass usage_context=UsageContext.OPENAI_API_SERVER to from_engine_args? The engine would get the same number, but the FULL graph list is built before the engine, so it would stop at 2,048 (setup C: prefill 0.76x of main). A longer list works for the graphs (vLLM drops sizes above its value, with a warning that prints both lists), but the two max_num_seqs rules still need the exact value.
  • Does vLLM 0.34 (the V2 runner port) make this unnecessary? No. In 0.34, from_engine_args still falls back to 2,048, and max_num_seqs above the value still raises. 0.34 can build prefill graphs up to its own value, which removes only the graph reason.
  • Why not call vLLM's resolver directly? EngineArgs._set_default_max_num_seqs_and_batched_tokens_args is private, and it needs the configs that only create_engine_config builds.
  • Why not one torchtitan table, or one fixed default on every GPU? vLLM already picks by GPU memory, and a second table would drift from it. Our FULL graphs go up to max_num_batched_tokens, so a big fixed value costs graph memory on every GPU.
  • Why vllm serve's values and not the LLM class's (16,384 on an H100, 8,192 on an A10G)? The A10G CI engine would move to 8,192. On an H100, 8,192 vs 2,048 moved wall time by under 1%, so there is no evidence for 16,384 there.
  • Why not keep 2,048 and set the value per recipe? New recipes would start at setup A: prefill 0.58x and mixed 0.63x of B.
  • Why not undo SGLang's DP split too? Undivided was faster (SGLang at DP4 on 4 GB300s: prefill 1.23x, mixed prefill 1.72x), but it means reading SGLang's default back only to pass it in again. A recipe that wants it sets the value, which is per DP rank in both engines.
  • What happens on AMD? vLLM reads GPU memory through amdsmi, so an MI300X gets 16,384. Not run: the varlen generator can't run on ROCm today (it asserts vLLM's FlashAttention version, which vLLM sets to None on ROCm).
  • Why 16,384 and not 32,768? Setup D adds 3% prefill (+15% mixed, one run) for 1.6 GiB more graph memory per GPU and 3% less KV. A recipe can set 32,768.
  • MLA models (DeepSeek-V3, Kimi)? The MLA backend graphs only uniform decode batches, so vLLM turns FULL into FULL_DECODE_ONLY (it logs a warning). The bigger value only means fewer, bigger eager prefill steps.

NOTE: the max_num_batched_tokens / 64 floor on max_num_seqs now applies only on CUDA devices below SM90 (FlashAttention-2), ROCm included as on main. SM90+ and XPU (which runs only flex attention) skip it, so small runs there get max_num_seqs below main's 32. generate.py --max-num-seqs 1 on a FlashAttention-2 GPU now gets max_num_batched_tokens // 64 (32 at 2,048).
NOTE: Dist-MoE generators size their scratch from max_num_batched_tokens. The Dist-MoE debug recipe and test_generator_dist_moe_mxfp8.py leave it unset, so they now run 16,384 on SM100. So does the Muse Spark 816B DAPO recipe on GB300: its Dist-MoE scratch grows from 2 to 16 GiB per GPU, taken from the KV cache. Not run.
NOTE: open PRs touching the same code. The vision-encoder CUDA graphs PR calls get_vllm_compilation_config, which now needs max_num_batched_tokens. The PR that moves the Terminal-Bench 35B-A3B recipe to Dist-MoE + FULL graphs leaves it unset, so on GB300 it would run 16,384 with FULL graphs. The V2 runner port edits vllm/config.py near get_vllm_compilation_config. Whichever lands second rebases.

Follow up

  • ROCm: the varlen backend's FlashAttention-version assert, then measure the default on an MI300X.
  • Graph-safe MLA prefill, so FULL also graphs MLA prefill and mixed steps.
  • Drop the FlashAttention-2 floor once flash-attention issue 3117 is fixed (TODO in resolve_scheduler_limits).

Test plan

  • pytest tests/rl/unit_tests/cpu with main merged (GRPO on XPU included): 714 passed, 7 skipped. test_unset_max_num_batched_tokens_uses_vllm_serve_default covers 5 cases: plain, the FlashAttention-2 floor, no floor on XPU, the clamp (and that the probe leaves max_num_seqs out), and the max_num_seqs x max_model_len cap.
  • GB300, the real VLLMEngine.build_engine_args, then vLLM's own create_engine_config() on its result (Qwen3-0.6B, max_model_len 2,048). (max_num_seqs, max_num_batched_tokens) in -> out: (64, unset) -> (64, 16,384), largest graph 16,384; (32,768, unset) -> (16,384, 16,384); (2, unset) -> (2, 4,096); (64, 4,096) -> (64, 4,096). vLLM accepted all 4. The previous head raised on the second case.
  • At earlier heads, on GB300 (same vLLM path on SM90+): torchtitan.rl.generate at TP1; test_bitwise_parity.py::TestBitwiseParityVarlen 3 passed; test_generator_dp_cudagraph.py::test_vllm_dp_decode_graphs_with_prefilling_peer passed with both ranks at 16,384; test_generator_dist_moe_mxfp8.py and test_vllm_mla_backends.py 2 passed.
  • CI: ciflow/rl (A10G at 2,048, H100 at 8,192), ciflow/b200 (16,384).

…ks for the GPU

With max_num_batched_tokens unset, the generator ran vLLM's 2048 fallback on
every GPU: LLMEngine.from_engine_args uses UsageContext.ENGINE_CONTEXT, which
vLLM's per-GPU defaults table skips. Unset now resolves to the `vllm serve`
value from that table (16384 on B200/GB300, 8192 on H100/H200, 2048 on A100
and smaller GPUs), capped at max_num_seqs * max_model_len as vLLM caps its own
default. SGLang picks the same values on H100/H200 and >= 160 GB GPUs. The
engine and the FULL CUDA graph capture cap take the same resolved value.
Explicit values are unchanged. The Muse Spark 816B DAPO recipe pins 2048,
because its Dist-MoE scratch grows with the budget.
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Nov 8, 2026
@felipemello1 felipemello1 added the ciflow/b200 Trigger B200 CI label Nov 8, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 8, 2026
@claude

claude Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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


Approve, but please address the items in the review first. Review: #5952 (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 -- please address the items below before merging. The root cause is real (LLMEngine.from_engine_args uses ENGINE_CONTEXT, which falls back to 2,048). The fix is small and resolves the budget once, so the engine, the FULL capture cap and the Dist-MoE scratch all see the same value. A maintainer still needs to review, since this changes the generator's default perf and memory on H100 and B200/GB300.

What this PR does. When Generator.Config.max_num_batched_tokens is None, the new resolve_max_num_batched_tokens reads vLLM's own per-GPU table (EngineArgs.get_batch_defaults, the OPENAI_API_SERVER entry). It caps that value at max_num_seqs * max_model_len, like vllm serve does, and always passes the result to EngineArgs. get_vllm_compilation_config now requires the budget, and the _DEFAULT_MAX_NUM_BATCHED_TOKENS fallback is deleted. The Muse Spark 816B recipe pins 2,048 so its Dist-MoE scratch stays the same size.

Example.

Qwen3.5-9B generator, max_num_batched_tokens unset, FULL graphs, max_num_seqs=512, max_model_len=65536
  main:    H100 -> EngineArgs has no budget -> vLLM picks 2048; capture cap 2048
  this PR: H100 -> 8192 passed to EngineArgs; capture cap 8192
           GB300 -> 16384 for both; Dist-MoE scratch (model_wrapper.py:536 reads scheduler_config) grows to match
           A10G -> 2048 (RL golden on A10G unchanged)
  max_num_seqs=1, max_model_len=1024 on GB300 -> min(16384, 1024) = 1024

Must fix

  1. PR description -- "Summary (human-written)" is empty. The author has to fill it in. Agents don't.

Should consider

  • torchtitan/rl/generator/vllm/config.py:32 -- the docstring and generator.py:130 copy vLLM's tier table, which goes stale on the next vLLM bump (see inline).
  • torchtitan/rl/generator/vllm/config.py:28 -- tensor_parallel_size only affects vLLM's CPU backend, but every caller has to pass it (see inline).
Details
  • Gate. The problem is real on main, and torchtitan is the right layer: the capture sizes are built before EngineArgs, so passing usage_context=OPENAI_API_SERVER would leave mixed steps above 2,048 running eager, as the description says. EngineArgs.get_batch_defaults is a public staticmethod, so no private-API TODO is needed. No overlapping PR changes the default. The two open PRs the description names touch the same lines but don't supersede this one.
  • Callsites. Every path that builds the vLLM engine resolves the budget: VLLMEngine._build_engine_args, generate.py and the test_bitwise_parity.py helper. Dist-MoE sizing reads vllm_config.scheduler_config.max_num_batched_tokens, so it follows the resolved value. The DeepEP recipes (alphabet_sort.py and search_r1.py) size their buffers from an explicit budget and are unchanged. add_extra_vllm_engine_args still rejects max_num_batched_tokens in extra_engine_args.
  • Numerics. Rollout chunking changes on H100 and SM100 for recipes that leave the budget unset, so generator logprobs there aren't bitwise identical to main unless batch-invariant mode is on. The only RL golden runs on A10G, which stays at 2,048, so no golden needs regenerating.
  • Memory. The description measures FULL-graph pool growth (~1.5 GiB) and a 1-2% KV cut. One unset recipe is rl_grpo_qwen35_35b_a3b_terminal_bench (eager, all-to-all MoE, needs >80 GB GPUs), and it now profiles at 16,384 tokens per step. Its peak activation memory during the profile run wasn't measured.
  • Test. test_unset_max_num_batched_tokens_uses_vllm_serve_default guards the table key and the max_num_seqs * max_model_len cap. Both would silently regress to 2,048 without it, and it runs on CPU. The edits to the other tests are mechanical (the new required argument).

Not verified: that the installed vLLM's get_batch_defaults has the >=160 GB tier and applies the max_num_seqs * max_model_len cap in vllm serve (vLLM isn't installed in this runner), and the GPU and CI results in the description.

Comment thread torchtitan/rl/generator/vllm/config.py Outdated
Comment on lines +32 to +36
``vllm serve`` reads vLLM's per-GPU table (``EngineArgs.get_batch_defaults``) and caps the
value at ``max_num_seqs * max_model_len``. The table today:
>= 160 GB (B200, GB300) -> 16384
>= 70 GB, not A100 (H100) -> 8192
other GPUs (A100, A10G) -> 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.

Should consider: the PR reads the table from vLLM at runtime because "vLLM's table changes", but this docstring copies the tiers (and Generator.Config.max_num_batched_tokens in generator.py:130 says "16384 on GB300"). Both go stale on the next vLLM bump, and nothing will flag it. Either note which vLLM version the numbers come from ("as of vLLM X.Y"), or drop the numbers here and keep one example in the PR description.

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: Done in ee5973a. The docstring table now says "as of vLLM 0.32" (checked against v0.32.0:vllm/engine/arg_utils.py), and Generator.Config.max_num_batched_tokens no longer quotes a number: it points to resolve_max_num_batched_tokens.

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: Kept the three rows and the "as of vLLM 0.32" date in 4eadb08. The docstring now says the table is keyed on total device memory plus an A100 name check, and that GPUs below compute capability 9.0 (FA2) and ROCm keep 2048. Generator.Config still quotes no number.

Comment thread torchtitan/rl/generator/vllm/config.py Outdated
*,
max_num_seqs: int,
max_model_len: int,
tensor_parallel_size: int,

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.

Should consider: the docstring says tensor_parallel_size matters only on vLLM's CPU backend. The generator can't run there (it uses CUDA attention backends and checks has_cuda_capability). Every caller (engine.py, generate.py, test_bitwise_parity.py) still has to thread TP through. If you'd rather keep it to match vllm serve, which passes pipeline_parallel_size * tensor_parallel_size, that's fine, but then say so in the docstring. Otherwise, drop the parameter and call EngineArgs.get_batch_defaults(world_size=1) with a one-line comment. That's minor, about -6 LoC.

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: Dropped tensor_parallel_size in ee5973a. The helper calls EngineArgs.get_batch_defaults(world_size=1) with a one-line comment (world_size only scales the CPU backend, which the generator never uses), and the 3 callers no longer pass TP.

…the table to vLLM 0.32

world_size scales vLLM's batch-default table only on its CPU backend, which
the generator never uses. Name the vLLM version the docstring table comes
from, and keep the numbers out of Generator.Config.
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review (both suggestions applied in ee5973a)

@claude

claude Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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


Approve, but please address the items in the review first. Both earlier suggestions are resolved in ee5973a, and the "Summary (human-written)" section is still empty. Review: #5952 (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 -- please address the item below before merging. ee5973a resolves both should-consider items from the last review and adds no new problems. A maintainer still needs to review, because this changes the generator's default perf and memory on H100 and B200/GB300.

Re-review of ee5973a.

  • Resolved: the tier table in torchtitan/rl/generator/vllm/config.py:32 is now labeled "as of vLLM 0.32", and Generator.Config.max_num_batched_tokens (generator.py:129) no longer quotes a number.
  • Resolved: tensor_parallel_size is gone. resolve_max_num_batched_tokens calls EngineArgs.get_batch_defaults(world_size=1) and has a one-line comment explaining why. I checked all three callers (vllm/engine.py:277, generate.py:170, test_bitwise_parity.py:260), and none passes TP anymore. The CPU test's monkeypatch (test_generator.py:694) takes world_size as a keyword, which matches the new call.

Must fix

  1. PR description -- "Summary (human-written)" is still empty. The author has to fill it in. Agents don't.

Not verified: that vLLM 0.32's get_batch_defaults takes a world_size keyword, and the tier values (vLLM isn't installed in this runner). The CPU test mocks the function, so it won't catch a renamed parameter. The GPU CI runs on ciflow/rl and ciflow/b200 will.

probe and others added 5 commits November 9, 2026 09:40
…string on device memory

- Log the budget when it comes from vLLM's table, the one fact vLLM can't print.
- The table keys on total device memory for any GPU vendor; FULL graph sizes are built
  before the engine, which is why torchtitan reads it instead of passing a usage context.
- FULL docstring: vLLM switches to FULL_DECODE_ONLY when an attention backend graphs only
  uniform decode batches (MLA).
- max_num_seqs property: fix the comment on the budget, TODO for FA2 GPUs that resolve > 2048.

Co-authored-by: Claude <[email protected]>
…0 keep 2048

FA2 writes out of bounds past 64 tokens per profile request, and the controller's
max_num_seqs floor assumes a 2048 budget when unset. vLLM's table gives an A800 80 GB
8192; FA2 GPUs now keep main's 2048 (A10G, A100 and L40S already get it). The test
covers the FA2 case.

Co-authored-by: Claude <[email protected]>
… not always vllm serve's

The resolver keeps 2048 on FA2 GPUs and ROCm, so 'what vllm serve picks' no longer holds
there. The TODO says dropping the FA2 branch also moves ROCm to vLLM's table.

Co-authored-by: Claude <[email protected]>
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review 4eadb08. Changes since your last review:

  • GPUs below compute capability 9.0 (FlashAttention-2) and ROCm keep 2048 when the budget is unset. The controller's max_num_seqs floor assumes 2048 there, and vLLM's table would give an A800 8192. The CPU test covers it.
  • One startup log line when the budget is unset; the resolver docstring explains why we read vLLM's table ourselves instead of passing a usage context (the FULL graph sizes are built before the engine).
  • The FULL docstring notes that vLLM turns FULL into FULL_DECODE_ONLY for the MLA backend.
  • Merged main (incl. the merged max_num_seqs sizing); the body has the costs, a 4-GB300 check of the eager 35B-A3B Terminal-Bench generator at 16,384, and the AMD answer.

@claude

claude Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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


Approve. Please fill in the "Summary (human-written)" section before merging. 4eadb08 adds no new problems. Review: #5952 (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 -- please address the item below before merging. 4eadb08 adds the FA2/ROCm 2,048 branch, the startup log line and the MLA docstring note, and none of them introduces a new problem. A maintainer still needs to review, because this changes the generator's default perf and memory on H100 and B200/GB300.

Re-review of 4eadb08.

  • FA2/ROCm branch (torchtitan/rl/generator/vllm/config.py:47): has_cuda_capability returns False on ROCm (torch.version.hip is None, torchtitan/tools/utils.py:35), so MI300X/MI350X also keep 2,048, as the docstring says. The TODO links the FA2 issue, matching the controller's TODO.
  • Controller invariant (controller.py:479): max_num_seqs_per_generator_dp_rank still assumes 2,048 when the budget is unset. The resolved budget is min(table >= 2048, max_num_seqs * max_model_len), and max_num_seqs <= 2048, so max_num_seqs <= max_num_batched_tokens still holds on every GPU. The FA2 floor (budget / 64) is only needed below SM90, and there the budget stays 2,048, so the floor is still correct.
  • Callsites after the merge with main: all three engine builders (vllm/engine.py:295, generate.py:170, test_bitwise_parity.py:251) resolve the budget and pass it to get_vllm_compilation_config. Dist-MoE sizing (model_wrapper.py:345) reads scheduler_config, so it follows the resolved value. No _DEFAULT_MAX_NUM_BATCHED_TOKENS or or 2048 fallback remains outside the controller.
  • Test: the new (False, 16384, 2048) case patches has_cuda_capability on the config module, which is the name the resolver looks up. So it guards the FA2 branch.

Must fix

  1. PR description -- "Summary (human-written)" is still empty. The author has to fill it in. Agents don't.

Not verified: the vLLM get_batch_defaults(world_size=...) signature and tier values (vLLM isn't installed in this runner; the CPU test mocks it), and the GPU results in the description. ciflow/rl and ciflow/b200 will exercise the real call.

Comment thread torchtitan/rl/generator/generator.py Outdated
(prefill + decode, summed over the batch). ``None`` (default) leaves the
engine's own default in place."""
(prefill + decode, summed over the batch). ``None`` (default): the engine's default
for this GPU. For vLLM, see ``resolve_max_num_batched_tokens``."""

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.

lets make this also be the same for sglang

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: SGLang already does this. When the field is unset, torchtitan passes no chunked_prefill_size, so SGLang picks its own value for the GPU. The docstring now says that for both engines. One difference: SGLang splits its default over its DP ranks, so at DP4 on a GB300 each rank gets 4,096. A set value is per DP rank in both engines.

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: Follow-up in 9b205ba. Unset now means the same in both engines: the engine's own per-device default, per DP rank. SGLang divides its default over DP ranks, so at DP > 1 SGLangEngine._build_server_args asks SGLang's resolver for its per-rank share and multiplies it back by DP. On a GB300 at DP 2 that is 16,384 per rank (was 8,192). DP 1 is unchanged.

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: Update, replacing my 9b205ba follow-up: 41476c6 reverts the SGLang DP-split change, so the SGLang engine matches main. Unset means each engine's own default for the GPU. vLLM uses it on each DP rank; SGLang splits its default over its DP ranks, as the field's docstring says. A set value is per DP rank in both engines. Undoing the split meant reading SGLang's default back only to pass it in again.

Comment thread torchtitan/rl/generate.py Outdated
engine_kwargs["max_num_seqs"] = max_num_seqs
if gen_config.max_num_batched_tokens is not None:
engine_kwargs["max_num_batched_tokens"] = gen_config.max_num_batched_tokens
max_num_batched_tokens = resolve_max_num_batched_tokens(

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.

i dont see why we have this code repeated here and in vllm

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: Fixed in c6f49ec. generate.py had a copy of VLLMEngine's whole engine-args build (#5951 and #5488 each had to edit both). build_engine_args is now a public static method, and generate.py calls it. It only turns the tokenizer back on, because it prints text. Ran on 1 GB300: it generates the sorted names.

Comment thread torchtitan_recipes/rl/dapo_math.py Outdated
Comment on lines +373 to +376
# Dist-MoE scratch grows with the step budget: 2 GiB per GPU at 2,048 tokens,
# 16 GiB at 16,384 (the GB300 default), taken from the KV cache.
# TODO: measure 16,384 against the KV cache it costs.
max_num_batched_tokens=2048,

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.

lets just test and remove this change.

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: Not done yet. The test needs the 816B weights and 4 GB300 hosts for the generator alone (DP4 x TP4, EP16). At 16,384 its Dist-MoE scratch grows from 2.0 to 16.0 GiB per GPU, and that memory comes out of the KV cache. The pin stays until that run happens.

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: Removed in 8bd2f70, as you asked: the recipe now takes the default (16,384 on GB300). Not run. Its Dist-MoE scratch grows from 2 to 16 GiB per GPU, taken from the KV cache; the body NOTE says so.

Comment thread torchtitan/rl/generator/engine.py Outdated
Comment on lines +46 to +47
max_num_batched_tokens: Max tokens scheduled per engine step, or None for the
engine's default.
engine's default for this GPU.

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.

i find this a bit deceiving, because we dont use vllms default, which would actually be 16k for gb300. We set our own undocumented global constant default

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: Fixed in c6f49ec. Unset now means vLLM's own pick. resolve_max_num_batched_tokens asks vLLM's config builder (create_engine_config(UsageContext.OPENAI_API_SERVER)), so a GB300 gets 16,384. The one exception is now written in Generator.Config.max_num_batched_tokens: below compute capability 9.0, or off CUDA, it asks for LLMEngine's default, 2,048 (the FA2 bug; the TODO links it). This docstring points there.

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: Follow-up in 9b205ba: the exception in my previous reply is gone. Unset takes vLLM's own value on every device. Only FlashAttention-2 GPUs cap it, at 64 x max_num_seqs (the bug's limit). This docstring now says "per DP rank" and points to Generator.Config.max_num_batched_tokens.

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: Update: the FlashAttention-2 cap in my last reply is gone (41476c6). Unset takes vllm serve's value on every device. On FlashAttention-2 GPUs, resolve_scheduler_limits raises max_num_seqs instead.

Comment thread torchtitan/rl/controller/controller.py Outdated
Comment on lines +454 to +455
budget is ``generator.max_num_batched_tokens``, or 2048 when unset: the smallest value in
vLLM's table, and what FA2 GPUs keep (``resolve_max_num_batched_tokens``).

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.

There is no such a thing as 2k if unset. If unset, we default to resolve_max_num_batched_tokens

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: Fixed in c6f49ec. The docstring now says each generator picks the budget for its GPU. The controller runs before any generator, so it can't know that value. Its clamp uses 2,048 as a lower bound, the smallest default vLLM picks on CUDA, so max_num_seqs <= max_num_batched_tokens still holds.

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: Follow-up in 98d4aa7: the controller no longer has a 2,048. It only computes concurrency (2x each DP rank's share, rounded up to a power of two). The two vLLM rules it used to apply with an assumed budget (max_num_seqs <= budget, and the FlashAttention-2 floor of budget / 64) moved to fit_max_num_seqs, which the vLLM engine calls after it resolves the budget.

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: Update: fit_max_num_seqs from 98d4aa7 is merged into resolve_scheduler_limits (41476c6), which returns both max_num_batched_tokens and max_num_seqs. The controller keeps 2x the share, rounded up to a power of two, and no longer reads max_num_batched_tokens.

Comment thread torchtitan/rl/generator/vllm/config.py Outdated
) -> int:
"""Return ``max_num_batched_tokens``, or a default for this GPU when it is None.

``vllm serve`` reads vLLM's table (``EngineArgs.get_batch_defaults``), keyed on total device

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.

can we make this work for sglang as well? should we?

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: No code change for SGLang (see the generator.py thread): it already uses its own default. I don't think we should compute SGLang's value ourselves. "Unset" means what each engine's server picks.

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: Follow-up in 9b205ba: yes, done (see the generator.py thread). SGLang needs no budget up front: it sizes its prefill graphs from its own chunk. The only change is undoing its DP split. The divide-or-not runs in the body measured that at 1.23x prefill and 1.72x mixed-step prefill.

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: Update to my 9b205ba follow-up: the SGLang change is reverted in 41476c6 (see the generator.py thread). The rule is shared: the Generator.Config.max_num_batched_tokens field. The code differs because vLLM needs the value before the engine exists (the FULL graph list and two max_num_seqs rules), while SGLang sizes its prefill graphs from its own chunk.

Comment thread torchtitan/rl/generator/vllm/config.py Outdated
Comment on lines +35 to +38
table as of vLLM 0.32:
>= 160 GiB (B200, GB300) -> 16384
>= 70 GiB, not A100 (H100) -> 8192
other GPUs (A100, A10G) -> 2048

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.

what if someone uses it with intel, amd, tpu, etc? Please make this clear.

Can we import vllms internal resolver here? i would prefer

i believe you told me that we cannot leave it to vllm, because we need the value for our cudagraph. So lets get the value from vllm, use here, and then pass. Same for sglang i guess? but iirc, they are the same?

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: Fixed in c6f49ec. We now take the value from vLLM's resolver, through its public config builder: EngineArgs(**engine_kwargs).create_engine_config(usage_context), then scheduler_config.max_num_batched_tokens (0.05 s on a GB300). The resolver itself (_set_default_max_num_seqs_and_batched_tokens_args) is private, and it needs configs that only create_engine_config builds. We build the CUDA graph sizes from that value and pass it to the engine. Extra engine args such as performance_mode now reach vLLM's rules too.
Platforms, as the docstring says: CUDA with compute capability >= 9.0 gets vllm serve's value. Everything else (older CUDA, ROCm, XPU, TPU, CPU) gets LLMEngine's 2,048, as on main.
SGLang: no change needed. Its table matches vLLM's on H100 (8,192) and GB300 (16,384).

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: Follow-up in 9b205ba, replacing my previous answer. Every platform now gets vllm serve's value. vLLM keys it on device memory on CUDA, ROCm (through amdsmi) and XPU, so an MI300X gets 16,384. TPU and CPU follow vLLM's own rules. The old "below SM90 or off CUDA -> 2,048" branch is gone.
The one exception is the FlashAttention-2 bug, now capped at its real limit: 64 x max_num_seqs, on CUDA below SM90. A10G CI stays at 2,048.
Not run on AMD: torchtitan's varlen generator can't run on ROCm today.
Why we still read the value up front: torchtitan lists FULL graphs up to the budget before the engine exists. On vLLM 0.34's V2 runner that is no longer needed (TODO in the code).

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: Update: since 41476c6, unset takes vllm serve's value on every platform, with no FlashAttention-2 cap. On FlashAttention-2 GPUs the bug now raises max_num_seqs to at least max_num_batched_tokens // 64 instead (only CUDA below SM90 since 0940851; XPU skips it). One function, resolve_scheduler_limits, returns both numbers.

Felipe Mello added 2 commits November 19, 2026 20:30
# Conflicts:
#	tests/rl/unit_tests/cpu/test_generator.py
#	torchtitan/rl/generator/vllm/engine.py
…py reuses VLLMEngine.build_engine_args

resolve_max_num_batched_tokens(engine_kwargs) builds EngineArgs(**engine_kwargs) and reads
create_engine_config(UsageContext.OPENAI_API_SERVER).scheduler_config.max_num_batched_tokens, so vLLM's own
rules apply (per-GPU table, max_num_seqs * max_model_len cap, extra engine args such as performance_mode).
Below compute capability 9.0 and off CUDA it asks for ENGINE_CONTEXT (2048), as on main (FA2 TODO).
Resolution runs after extra_engine_args. generate.py calls VLLMEngine.build_engine_args instead of a copy.
Docstrings in Generator.Config, GenerationEngine and the controller say what an unset budget means.
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review c6f49ec, which addresses the maintainer's threads 12-18.

  • resolve_max_num_batched_tokens(engine_kwargs) now asks vLLM's config builder (EngineArgs(**engine_kwargs).create_engine_config(usage_context)): OPENAI_API_SERVER on CUDA compute capability >= 9.0, ENGINE_CONTEXT (2048, as on main) elsewhere. It runs after extra_engine_args.
  • generate.py calls VLLMEngine.build_engine_args (now a public static method) instead of its own copy.
  • Docstrings in Generator.Config, GenerationEngine and the controller say what an unset budget means. SGLang is unchanged.
  • The 816B recipe pin stays until a 4-host run (thread 14).
    Tests: RL CPU 687 passed, 7 skipped; GB300 GPU runs in the body's Test plan.

@claude

claude Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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


Approve -- please address the items below before merging. The description's human-written summary is still empty, and the 816B recipe still pins 2,048 (thread 14). Review: #5952 (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 -- please address the items below before merging. c6f49ec resolves threads 12, 13, 15, 16, 17 and 18 and adds no new problems. Thread 14 (the 816B pin) is still open. A maintainer still needs to review, because this changes the generator's default perf and memory on H100 and B200/GB300.

What changed in c6f49ec. resolve_max_num_batched_tokens(engine_kwargs) no longer reads vLLM's table itself. It builds a throwaway EngineArgs(**engine_kwargs).create_engine_config(usage_context) and reads scheduler_config.max_num_batched_tokens. It runs after extra_engine_args and before the FULL capture sizes are built. VLLMEngine.build_engine_args is now a public staticmethod, and generate.py calls it instead of keeping its own copy (-51 lines there).

Example.

FULL graphs, max_num_batched_tokens unset, extra_engine_args={}
  GB300 (SM100): create_engine_config(OPENAI_API_SERVER) -> 16384 -> EngineArgs and capture cap 16384
  A10G (SM86):   create_engine_config(ENGINE_CONTEXT)    -> 2048  -> same as main
  set to 4096:   no probe build                           -> 4096
extra_engine_args={"compilation_config": ...} -> still ValueError ("use VLLMEngine.Config.cuda_graph")

Must fix

  1. PR description: the "Summary (human-written)" section is still empty. The author has to fill it in. Agents don't.
  2. torchtitan_recipes/rl/dapo_math.py:375 (thread 14): felipemello1 asked to "test and remove this change", and the 2,048 pin is still there. Either do the 4-host run and delete the 4 lines, or say on the thread that the pin (with its TODO) stays in this PR.
Details
  • Thread status at HEAD. 12 (SGLang in Generator.Config): answered, and the docstring now covers both engines. 13 (generate.py duplication): fixed. 15 (the "undocumented constant"): fixed, since the value now comes from vLLM's config builder, and the FA2/off-CUDA 2,048 is documented in Generator.Config with the TODO link. 16 (controller): the docstring says each generator picks its own budget and the clamp uses 2,048 as a lower bound. 17 and 18 (SGLang, other platforms, vLLM's resolver): fixed, and the private _set_default_max_num_seqs_and_batched_tokens_args isn't called.
  • Ordering. add_extra_vllm_engine_args now runs before compilation_config and the budget are set. Both keys are in config_field_by_engine_arg (vllm/engine.py:80-85), so a user still can't set them through extras. seed and mm_processor_cache_gb are set before the extras, as before.
  • Controller invariant. max_num_seqs_per_generator_dp_rank returns at most min(max_num_seqs, 2048). Both usage contexts resolve to at least min(2048, max_num_seqs * max_model_len), so vLLM's max_num_seqs <= max_num_batched_tokens check still holds. If an extra arg ever pushes vLLM's pick below that, vLLM's own scheduler-config check would raise. It wouldn't be silent.
  • generate.py. It now also gets the generator's scheduling_policy="priority", logprobs_mode="processed_logprobs", prefix_cache_retention_interval=None and enable_cumem_allocator=False. That is the point of sharing the builder. Requests without a priority use 0, so the single-request flow is unchanged. Setting skip_tokenizer_init = False after the build is fine, because the budget doesn't depend on it.
  • Test. test_unset_max_num_batched_tokens_uses_vllm_default guards the capability -> usage-context choice and checks that the value reaches both the engine args and the FULL capture cap. vLLM now owns the max_num_seqs * max_model_len cap, so dropping that case from the CPU test is right.

Not verified: vLLM isn't installed in this runner. So I couldn't confirm that the probe create_engine_config leaves the objects it shares with the real EngineArgs unchanged (attention_config, dict-valued extras such as speculative_config), or that vLLM applies the max_num_seqs * max_model_len cap. The GB300 runs in the Test plan and ciflow/rl / ciflow/b200 exercise the real call.

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review 8bd2f70. It removes the 816B recipe's max_num_batched_tokens=2048 pin (thread 14), so torchtitan_recipes/ matches main. Nothing else changed since c6f49ec.

@claude

claude Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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


Approve. Please fill in the "Summary (human-written)" section before merging. 8bd2f70 closes thread 14 and adds no new problems. Review: #5952 (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 -- please address the item below before merging. 8bd2f70 deletes the 4-line max_num_batched_tokens=2048 pin and its TODO from the 816B recipe, which closes thread 14. torchtitan_recipes/ now matches main, and the commit adds no new problems. A maintainer still needs to review, because this changes the generator's default perf and memory on H100 and B200/GB300.

Re-review of 8bd2f70.

  • Resolved (thread 14): torchtitan_recipes/rl/dapo_math.py is no longer in the PR diff. muse_spark_816b_a42b_dapo_math_gb300_config leaves the budget unset (dapo_math.py:364-379), so on GB300 it resolves through resolve_max_num_batched_tokens to vLLM's vllm serve value, like every other unset recipe. The description's NOTE says the same thing: its Dist-MoE scratch grows from 2 to 16 GiB per GPU, the memory comes out of the KV cache, and it was not run.
  • No other files changed since c6f49ec, so the rest of the previous review still holds.

Must fix

  1. PR description: the "Summary (human-written)" section is still empty. The author has to fill it in. Agents don't.
Details
  • Thread 14 asked to "test and remove". The pin is removed, but the 816B generator hasn't run at 16,384. That recipe has FULL graphs, gpu_memory_limit=0.9, and Dist-MoE scratch with scratch_capacity_factor=16.0, so it now profiles with about 14 GiB more scratch per GPU. If the KV cache left over is too small, vLLM fails at startup with a clear error rather than giving wrong results. So this is a perf and memory question for whoever runs the recipe next, not a correctness risk, and the description already discloses it.

Not verified: the 816B generator's KV cache size and throughput at 16,384 on GB300 (it needs 4 hosts).

Felipe Mello added 3 commits November 20, 2026 11:19
…e default on every platform, per DP rank

vLLM: always take vllm serve's value from vLLM's config builder (vLLM keys it on device memory
on CUDA, ROCm and XPU). Before, only CUDA with compute capability >= 9.0 got it; every other
GPU, ROCm included, was forced to 2048. FlashAttention-2 GPUs now cap it at 64 * max_num_seqs,
the limit of flash-attention#3117, instead of falling back to 2048.

SGLang: at DP > 1, SGLang splits its per-GPU default over attention-DP ranks (4096 per rank at
DP 4 on a GB300). Ask SGLang's resolver for that share and undo the split, so an unset value is
per DP rank in both engines, like a set one.
@felipemello1 felipemello1 changed the title [rl] Generator: default max_num_batched_tokens to what vllm serve picks for the GPU [rl] Generator: default max_num_batched_tokens to the engine's per-device default (vLLM and SGLang) Nov 20, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review 9b205ba. Changes since your last review:

  • vLLM: unset takes vllm serve's value on every platform. The "below SM90 or off CUDA -> 2,048" branch is replaced by a FlashAttention-2 cap at 64 x max_num_seqs, the limit of the FA2 bug.
  • SGLang: at DP > 1, unset now undoes SGLang's DP split, so the value is per DP rank in both engines.
  • Merged main 078bdc6. A TODO for the V2 runner, where vLLM can size prefill graphs from its own budget.

@claude

claude Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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


Approve. Please fill in the "Summary (human-written)" section before merging. 9b205ba adds no correctness problems. I left one should-consider inline: the FA2 cap also applies on XPU. Review: #5952 (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 -- please address the item below before merging. 9b205ba makes "unset" mean the same thing in both engines: the engine's own per-device default, per DP rank. It also replaces the blanket 2,048 fallback with a FlashAttention-2 cap at the bug's real limit. Neither change adds a correctness problem. A maintainer still needs to review, because this changes the generator's default perf and memory on H100, B200/GB300 and, now, ROCm.

What changed since 8bd2f70.

  • vLLM: resolve_max_num_batched_tokens asks for OPENAI_API_SERVER on every platform. Only CUDA below SM90 then caps the value at 64 * max_num_seqs.
  • SGLang: at DP > 1 with the budget unset, _build_server_args builds a probe ServerArgs from the same kwargs (extras included), reads the resolved per-rank chunked_prefill_size, and multiplies it back by DP.

Example.

budget unset, controller max_num_seqs = 64
  A10G (SM86):  vllm serve 2048  -> min(2048, 64*64)  = 2048   (as main)
  A800 80 GB:   vllm serve 8192  -> min(8192, 64*64)  = 4096   (main: 2048)
  MI300X:       vllm serve 16384 -> 16384                       (main: 2048)
  generate.py on SM86, max_num_seqs=1 -> 64                     (main: 2048; disclosed)
SGLang, GB300, DP2 unset: probe resolves 8192/rank -> 16384 -> chunked_prefill_size 32768 -> 16384/rank (main: 8192/rank)

Must fix

  1. PR description: the "Summary (human-written)" section is still empty. The author has to fill it in. Agents don't.

Should consider

  • torchtitan/rl/generator/vllm/config.py:51: the FA2 cap also applies on XPU/CPU/TPU, not just CUDA below SM90 as the docstring says. Details and a suggested fix are inline.
Details
  • Controller invariant. With the budget unset, the controller clamps max_num_seqs to [32, 2048] (controller.py:578-586). On FA2 GPUs the resolved budget is min(vllm_default, 64 * max_num_seqs), which is >= 2048 whenever vLLM's default is. vLLM's max_num_seqs <= max_num_batched_tokens check still holds on every path, including 64 * max_num_seqs >= max_num_seqs for generate.py's max_num_seqs=1. The updated docstring and comment at controller.py:559 and :584 match this.
  • The two FA2 guards don't overlap. The controller's floor (budget / 64) covers set budgets, and the resolver's cap covers unset ones, where vLLM may pick more than 2,048 (A800). Neither one makes the other redundant.
  • SGLang. chunked_prefill_size is in _TORCHTITAN_SERVER_ARGS, so extra_server_args can't set it, and the probe resolves SGLang's own default. At DP 1 the branch is skipped and nothing changes from main. A set value is still multiplied by DP as before.
  • Callsites. VLLMEngine.build_engine_args, generate.py (through build_engine_args) and test_bitwise_parity.py:251-258 all resolve the budget before get_vllm_compilation_config, which now requires it. No _DEFAULT_MAX_NUM_BATCHED_TOKENS fallback is left.
  • Test. The new ROCm case (hip="7.0", SM90 False -> 4096) guards the torch.version.hip is None half of the condition, and the SM86 case guards the cap.
  • TODO at config.py:43 links #5956 (the V2-runner port, open). That PR is in-repo, so the link is fine.

Not verified: SGLang's ServerArgs.resolve_once() / resolved_dict() API and whether a second resolution has side effects (SGLang isn't installed here). Also not verified: vLLM's XPU/ROCm memory table and the GPU results in the description. The B200 SGLang DP 2 parity classes exercise the SGLang probe.

Comment thread torchtitan/rl/generator/vllm/config.py Outdated
)
vllm_serve_default = vllm_config.scheduler_config.max_num_batched_tokens
max_num_batched_tokens = vllm_serve_default
if torch.version.hip is None and not has_cuda_capability(9, 0):

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.

Should consider: this condition is also true off CUDA. On XPU, torch.version.hip is None and has_cuda_capability returns False because torch.cuda.is_available() is False (torchtitan/tools/utils.py:33). So the FA2 cap applies there too. The docstring (line 37) and the PR's "every platform gets vllm serve's value" both say it applies only on CUDA below SM90. Example: on XPU at max_num_seqs=64, a 16,384 default becomes 4,096. The generator probably can't run on XPU today, so this mostly makes the code disagree with its docstring. Smallest fix: limit the cap to CUDA. The FA2 test case then also needs torch.cuda.is_available patched to True.

Suggested change
if torch.version.hip is None and not has_cuda_capability(9, 0):
if torch.cuda.is_available() and torch.version.hip is None and not has_cuda_capability(9, 0):

The other option is to keep the code and have the docstring say "CUDA below compute capability 9.0, and non-CUDA non-ROCm devices".

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: Fixed in 0940851. The GRPO-on-XPU PR landed on main, so XPU now reaches this code. The floor now runs only when device_type == "cuda" and the GPU is below SM90. XPU runs only flex attention, so it never hits the FlashAttention-2 bug. ROCm keeps the floor, as on main. The CPU test has an XPU case.
Also, since 41476c6 the floor raises max_num_seqs instead of lowering max_num_batched_tokens, so the 16,384 -> 4,096 example no longer happens on any device.

Felipe Mello added 3 commits November 20, 2026 11:53
… controller

The controller assumed the generator's budget (2048 when unset) to apply two vLLM rules: max_num_seqs <= budget,
and the FlashAttention-2 floor (budget / 64). fit_max_num_seqs applies both after the budget is resolved, so the
controller only computes concurrency. The FA2 floor now applies only on FA2 GPUs, and the resolver returns vLLM's
value unchanged (an A800 keeps 8192).
…ction; SGLang as on main

resolve_scheduler_limits replaces resolve_max_num_batched_tokens and fit_max_num_seqs. Unset
max_num_batched_tokens: ask vLLM's config builder what vllm serve picks, without max_num_seqs (vLLM
raised when max_num_seqs was above its pick), then cap at max_num_seqs x max_model_len. On FA2 GPUs,
max_num_seqs >= max_num_batched_tokens // 64; then max_num_seqs <= max_num_batched_tokens.

The controller keeps its power of two and no longer reads max_num_batched_tokens. SGLang's engine is
back to main: unset passes nothing, so SGLang uses its own default.
@felipemello1 felipemello1 changed the title [rl] Generator: default max_num_batched_tokens to the engine's per-device default (vLLM and SGLang) [rl] Generator: default vLLM's max_num_batched_tokens to vllm serve's per-GPU value Nov 20, 2026
Felipe Mello added 2 commits November 21, 2026 23:24
… devices

XPU runs only flex attention (_validate_platform), so it never hits the FA2 bug. ROCm keeps the floor, as on main.
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review 0940851. Changes since your last review (9b205ba):

  • One resolve_scheduler_limits(engine_kwargs) -> (max_num_batched_tokens, max_num_seqs) in vllm/config.py. It asks vLLM what vllm serve picks, with max_num_seqs left out of that call (vLLM raised when the controller's value was above its pick). It caps the value at max_num_seqs x max_model_len, then sets max_num_seqs to at least max_num_batched_tokens // 64 (FlashAttention-2, CUDA below SM90) and at most max_num_batched_tokens.
  • The controller keeps 2x the share, rounded up to a power of two, and no longer reads max_num_batched_tokens.
  • SGLang matches main again (no DP-split change).
  • Your XPU comment: the floor now runs only on device_type == "cuda". Main is merged, including GRPO on XPU.

@claude

claude Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 3m 19s —— View job


Approve. Please fill in the "Summary (human-written)" section before merging. 0940851 resolves the XPU comment. I left two should-consider inline comments: the FA2 floor when the capped budget isn't a power of two, and SGLang losing the controller's clamp. Review: #5952 (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 -- please address the item below before merging. 0940851 puts both vLLM scheduler limits in one place: resolve_scheduler_limits in the engine. The controller now only sizes concurrency, and the XPU comment from the last review is resolved. A maintainer still needs to review, because this changes the generator's default perf and memory on H100, B200/GB300 and ROCm.

What changed since 9b205ba. resolve_scheduler_limits(engine_kwargs) builds a probe EngineArgs with max_num_seqs=None and asks it what vllm serve picks. It caps that at max_num_seqs * max_model_len. It then fits max_num_seqs to [budget // 64 (CUDA below SM90 only), budget]. The controller returns 2x the even share, rounded up to a power of two, with no clamp. The FA2 cap on the budget is gone, and so is the SGLang DP-split change.

Example.

budget unset, max_model_len 4096
  GB300, controller 512        -> (16384, 512)
  A10G,  controller 16         -> (2048, 32)    floor now in the engine (main: controller gave 32)
  A10G,  controller 4096       -> (2048, 2048)  (main: same, clamped in the controller)
  XPU,   controller 16         -> (vllm serve's pick, 16)   no FA2 floor
  budget set to 4096, A10G, 8  -> (4096, 64)

Must fix

  1. PR description: the "Summary (human-written)" section is still empty. The author has to fill it in. Agents don't.

Should consider

  • torchtitan/rl/generator/vllm/config.py:64 -- on FA2, the // 64 floor isn't safe when the new max_num_seqs * max_model_len cap gives a budget that isn't a power of two (inline).
  • torchtitan/rl/controller/controller.py:576 -- without the clamp, SGLang's max_running_requests / full_prefill_max_req change too, and the controller's startup log shows the pre-fit value (inline).
Details
  • Resolved (XPU thread). device_type == "cuda" and not has_cuda_capability(9, 0) skips XPU and keeps ROCm, where device_type is "cuda" and has_cuda_capability is False on HIP. That matches the docstring and the description. The new ("xpu", False, 8, 4096, 8) test case guards it.
  • Probe. max_num_seqs=None in the probe avoids vLLM's max_num_seqs > max_num_batched_tokens check against its own pick. The CPU test asserts that the probe leaves it out. The probe runs before compilation_config is added and after add_extra_vllm_engine_args, so extras such as performance_mode reach vLLM's rules. That ordering is correct.
  • Invariant. Every return path satisfies max_num_seqs <= max_num_batched_tokens, and on FA2, max_num_seqs >= budget // 64. vLLM's own check therefore can't fire, whatever value the controller passes.
  • Callsites. VLLMEngine.build_engine_args (and generate.py through it) and test_bitwise_parity.py:246-258 all call resolve_scheduler_limits and pass the fitted pair to both EngineArgs and get_vllm_compilation_config. Nothing else reads max_num_seqs_per_generator_dp_rank except the controller log and the SGLang engine.
  • Controller docstring. The new example 16 -> 16 is correct, and the comment explains the power-of-two rounding.

Not verified: that vLLM's create_engine_config(OPENAI_API_SERVER) accepts max_num_seqs=None and leaves shared objects in engine_kwargs unchanged, and that vLLM's profile run puts the remainder on the last request (vLLM isn't installed here). Also not verified: the GPU results in the description.

# TODO(https://github.com/Dao-AILab/flash-attention/issues/3117): drop once fixed. vLLM's memory profile splits
# max_num_batched_tokens over max_num_seqs requests, the remainder on the last; FA2 writes out of bounds past 64.
# XPU is skipped: it runs only flex attention (_validate_platform in vllm/engine.py).
max_num_seqs = max(max_num_seqs, max_num_batched_tokens // 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.

Should consider: this floor only keeps every profile request at 64 tokens or fewer when max_num_batched_tokens is a power of two. On main the budget was always 2,048 or a value the user set. This PR adds the max_num_seqs * max_model_len cap at line 53, and that cap can produce a budget that isn't a power of two. Then // 64 rounds down, and the comment above says vLLM puts the remainder on the last request.

Example on an A10G: generate.py --max-num-seqs 1 with max_model_len=1000 resolves to (1000, 15). The profile then runs 15 requests of 66 tokens, which is past the 65-token trigger in flash-attention#3117. With max_num_seqs=2 you get (2000, 31), where 30 requests get 64 tokens and the last gets 80. Using ceil instead of floor doesn't fix it (16 requests -> 62 each, last 70).

The smallest fix keeps main's invariant (a power-of-two budget split over a power-of-two max_num_seqs). On this path, round the capped value down to a power of two, e.g. max_num_batched_tokens = 1 << (max_num_batched_tokens.bit_length() - 1): 1000 -> 512 -> 8 x 64. If you'd rather not touch the cap, a user-set or capped budget that isn't a multiple of 64 x max_num_seqs could raise ValueError on FA2 GPUs. This is niche (FA2 only, tiny max_num_seqs * max_model_len), and I haven't run it.


# 2x leaves room for uneven routing across DP ranks, validation and a growing buffer. A power
# of two splits vLLM's max_num_batched_tokens evenly over max_num_seqs (resolve_scheduler_limits).
return 2 ** math.ceil(math.log2(2 * even_share))

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.

Should consider: removing the clamp here also changes SGLang. SGLangEngine._build_server_args passes this value as max_running_requests (times DP) and full_prefill_max_req (sglang/engine.py:431,475). On main it was clamped to [32, 2048] when the budget was unset. Now it has neither bound. For example, 16 rollouts over 2 DP ranks gives 16 (main: 32), and more than 1,024 rollouts per DP rank gives 4,096 or more (main: 2,048). So "SGLang matches main again" isn't quite accurate.

The small end looks harmless. The large end lets SGLang capture full prefill graphs for more requests, and no in-repo SGLang recipe reaches it. Either say so in the description, or keep min(..., 2048) for SGLang in its engine if the upper bound mattered there. Also, the controller's logger.info("max_num_seqs=%d ...") at line 765 now logs the pre-fit value. Logging the fitted max_num_seqs in resolve_scheduler_limits would keep startup logs accurate on FA2 GPUs.

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/b200 Trigger B200 CI 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