Repository navigation
[rl] SGLang generator: GDN and KDA prefill in breakable prefill graphs under attention DP - #6425
felipemello1 wants to merge 4 commits into
Conversation
… refuse decode rows run as extends The prefill graph tables take the real requests from ForwardBatch._original_batch_size, as SGLang's own GDN and KDA backends do, so an idle attention-DP rank's made-up request runs no tokens. The fills move into _fill_prefill_graph_metadata: capture calls it, and the replay hook first raises on a batch that tracks prefix-cache snapshots without prefill track lengths (non-speculative decode rows run as 1-token extends). The 1-GPU test's batch stand-in gains _original_batch_size=None.
…hs under attention DP Drop the attention-DP gate from supports_prefill_graph_extend and the DP-1 condition on the 256-request prefill cap (per DP rank). SGLang replays prefill graphs at DP only on steps where no DP rank decodes; a step where one rank prefills and another decodes stays eager.
…g (GDN, KDA); replays refuse decode rows The capture batch now matches SGLang's (track slots on the device, no CPU track lists), so a check placed at capture fails here. After a real batch whose output is checked non-zero, the GDN test replays an idle rank's made-up request (_original_batch_size 0) and checks zero output and unchanged pools (the stand-in maps request row 0 to the real slot 1). A KDA test does the same, with Attention Gym's replay state in batch-invariant mode. A replay with a track mask and no prefill track lengths raises.
… TP 1) TestSGLangBitwiseParityGDNDPEP runs the GDN extend in breakable prefill graphs under attention DP, on 2 GPUs: - test_idle_dp_rank_replays_prefill_graphs: DP rank 1 caches 5 sessions, idles while rank 0 prefills (its prefill graph replays are counted), then resumes the sessions bitwise against the trainer. - test_dp_ranks_with_different_requests: rank 1 decodes while rank 0 still prefills in 100-token chunks (eager steps, counted on rank 1), then each rank runs a cached next turn. Rank 0's prompts share no prefix, so cache hits can't shorten its prefill. _count_prefill_replays now serves both the capture test and the idle test, and _assert_dp_rank_matches_trainer serves the DP tests. The class joins the B200 job's 2-process loop; it skips without the dist_moe package.
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 3m 25s —— View job Request changes -- review: the 2-GPU attention-DP class hasn't run yet, and the PR's central claim depends on that run. |
There was a problem hiding this comment.
Verdict: Request changes -- the PR exists to make an idle attention-DP rank's prefill-graph replay safe, but that path has never run in an engine. The description itself names two outcomes of the pending 2-GPU run that would shrink the PR, and the new guard may raise on that same path.
What this PR does. It turns on the captured GDN/KDA extend in breakable prefill graphs under attention DP. _fill_prefill_graph_metadata now fills table rows only for the real requests (ForwardBatch._original_batch_size), so the idle rank's made-up request gets all-padding rows. refresh_prefill_graph_metadata raises on decode rows run as 1-token extends. The engine applies the 256-request cap at any DP size.
Example. DP 2, rank 0 prefills 200 tokens, rank 1 is idle. Both ranks pad to the 512-token bucket and replay.
rank 1 forward_batch: batch_size=1, extend_seq_lens_cpu=[512], _original_batch_size=0
before: supports_prefill_graph_extend() False -> each GDN layer breaks the graph, runs eagerly
after: num_requests=0 -> cu_seqlens all 0, state_indices all pad_slot_id,
padding_tokens all True -> zero output, no slot written; layer stays in the graph
DP 1: _original_batch_size None -> num_requests = batch_size, tables unchanged
Must fix
-
Run
TestSGLangBitwiseParityGDNDPEPand post the result before merge. The engine-level claims are inferred from SGLang's code:- rank 1 replays while idle;
- the mixed test really produces decode steps during prefill;
- the guard doesn't fire on the idle batch.
The NOTE says the first two could change the scope. The third is new (inline at
linear_attention.py:244): if the made-up batch looks like the capture batch, the raise breaks the path being enabled. The 1-GPU stand-in can't tell, since_idle_rank_batch()has no track mask by construction. -
linear_attention.py:275-- the private_original_batch_sizeread is listed under the extra_buffer TODO (sglang#48463), which is about a different gap and goes away with #6318. It needs its own TODO and upstream issue link (inline).
Questions
README.md:109-- isprefill_max_requestsreally per DP rank in SGLang (inline)?- Draft #6200 (full prefill graphs for GDN/KDA, waiting on sglang#51225) would make
"breakable"unnecessary for these models. Does the full-graph path build its tables with_fill_prefill_graph_metadatatoo, so this mask carries over? Or does #6200 need its own DP handling?
Details
- Gate. The problem is real at the base branch:
supports_prefill_graph_extendreturned False atattn_dp_size > 1, so every GDN/KDA layer broke the graph on DP replays. The fix sits at the right layer, the table fill. It matches SGLang's own GDN/KDA backends, which also read_original_batch_size(per the description; I couldn't open SGLang's source here). Net change: a gate, a cap condition and a TODO removed; the table fill changes frombatch_sizetonum_requests. The rest is tests. - Mask correctness. With
num_requests=0:num_tokens=0, socu_seqlens[0:]=0andpadding_tokensare all True. Every snapshot table was already reset to pad/0 before the track branch. Slicing with[:0]makes the clamp (max=-1) a no-op on an empty tensor. With0 < num_requests < batch_size, the rows past the real ones get empty sequences and pad slots. At DP 1 the tables are bit-identical to before. - Tests. The 1-GPU KDA test and the extended GDN test guard the mask; per the description, both fail without it.
GDNDPEPinherits all GDN base tests at DP 2. Its 2-process CI slot fits the existing B200 loop;dist_moeis already needed there byDistMoE. - Overlap searched: "prefill graph", "extra_buffer", "linear_attention.py", "attention DP sglang". Found: #6415 (parent), #6318 (extra_buffer hooks, draft; already coordinated in the NOTE), #6200 (full prefill graphs, draft; see Questions).
Not verified: all SGLang-internal claims (DP vote, idle-rank padding, the fields of the made-up batch, per-rank prefill_max_requests). No GPU run.
Before merge: fill in the human-written summary.
| # The real requests: an idle attention-DP rank runs one made-up request, with | ||
| # _original_batch_size 0. | ||
| num_requests = forward_batch._original_batch_size |
There was a problem hiding this comment.
This is a private upstream read, but its only TODO is the extra_buffer one above (sgl-project/sglang#48463, "no public hooks for the extra_buffer prefix cache"). As the PR's own NOTE says, _original_batch_size comes from SGLang's attention-DP padding, not from extra_buffer. So when #6318 lands and deletes that TODO, this read is left with no TODO, and #48463 is the wrong issue to track it anyway. That breaks CLAUDE.md principle 3: a private upstream API gets a TODO with its own upstream issue link.
Fix: take _original_batch_size out of the TODO at lines 184-188. Put a TODO here instead, linking an SGLang issue that asks for a public real-request count on ForwardBatch under DP padding, e.g.:
# TODO(<sglang issue>): SGLang exposes the real request count under attention-DP
# padding only as the private ForwardBatch._original_batch_size (SGLang's own GDN
# and KDA backends read it too). An idle DP rank runs one made-up request, with 0.| if ( | ||
| forward_batch.mamba_track_mask is not None | ||
| and forward_batch.mamba_track_seqlens_cpu is None | ||
| ): | ||
| raise RuntimeError( | ||
| f"A batch of {forward_batch.batch_size} decode rows run as 1-token " | ||
| "extends reached a captured linear attention extend: it tracks " | ||
| "prefix-cache snapshots but has no prefill track lengths, so they " | ||
| "would be lost. TorchTitan relies on SGLang's attention DP vote to keep " | ||
| "such steps eager: check it after an SGLang bump." | ||
| ) |
There was a problem hiding this comment.
Unverified, and the 2-GPU run decides it: the guard looks for a batch with a track mask but no mamba_track_seqlens_cpu. That is also what the capture batch looks like (_capture_batch in the 1-GPU test). If SGLang builds the idle rank's made-up request from the capture buffers, or copies capture's track fields into it, this raise fires on every idle-rank replay, which is the exact path this PR turns on, and it kills the engine.
The 1-GPU stand-in can't tell: _idle_rank_batch() is _batch([BUCKET], [0]), which has track=None, so mamba_track_mask is None by construction. I couldn't read SGLang's source here to check how the DP padding builds that batch.
Fix: confirm with TestSGLangBitwiseParityGDNDPEP::test_idle_dp_rank_replays_prefill_graphs, which needs rank 1 > 0 replays and no raise. Or cite the SGLang lines showing the padded idle batch has mamba_track_mask is None, and build _idle_rank_batch to match them.
| steps where no DP rank decodes; a step where one rank prefills and another decodes | ||
| runs eagerly on every rank. The tables have a row per request, so for these models | ||
| the engine caps a prefill batch at 256 requests (per DP rank). | ||
| Capture takes about 37 s (Qwen3.5-0.8B) to 74 s (Qwen3.5-27B) at startup, on one |
There was a problem hiding this comment.
Unverified: is prefill_max_requests a per-DP-rank limit in SGLang? The engine scales chunked_prefill_size by data_parallel_degree (engine.py:571-573), which suggests SGLang divides some server-wide limits over the DP ranks. If it divides prefill_max_requests the same way, each rank gets 128, not 256, and "(per DP rank)" is wrong. If it doesn't, please cite where SGLang applies it per rank. Either way, prefill_graph_max_seqs reads the same get_schedule() value, so the table size can't fall below the cap. This is only about whether the doc is accurate.
Summary (human-written)
Agent details
TLDR: Under SGLang's attention DP, the GDN and KDA prefill now runs inside the breakable prefill graphs too, on the steps where SGLang replays them: every DP rank prefills, or the other ranks are idle. An idle rank's made-up request is masked out of the tables, so it writes no state. This is a cleanup: one gate, one condition on the 256-request cap and one TODO go. No speed claim: it is not measured at DP.
Not run at attention DP yet: the 2-GPU class waits for a node (see NOTE).
Problem
ForwardBatch._original_batch_size0.supports_prefill_graph_extendreturns False, with a TODO to mask the made-up request and add a DP 2 test. Its tables would give the made-up request a row. That row writes nothing only because SGLang maps request row 0 to its padding slot.Solution
_fill_prefill_graph_metadatatakes the real requests fromForwardBatch._original_batch_size, as SGLang's own GDN and KDA backends do. Every row past them is padding: slot -1, an empty sequence, zeroed output. An idle rank's made-up request is all padding, so it runs no tokens and writes no state, whatever slot row 0 maps to.supports_prefill_graph_extenddropsattn_dp_size == 1. The engine's 256-request prefill cap (prefill_max_requests) now applies at any DP size, per DP rank.refresh_prefill_graph_metadatafirst raises on decode rows run as 1-token extends: a track mask but no prefill track lengths (mamba_track_seqlens_cpu), so their prefix-cache snapshots would be lost. At this SGLang pin, the DP vote keeps such steps eager. The raise catches an SGLang bump that changes the vote. Capture (init_prefill_graph_metadata) only fills: SGLang's capture batch has the same fields._original_batch_sizeis None, so the tables are the same as before. A step where one rank prefills and another decodes stays eager on every rank. SGLang's draft runners (MTP) keep their eager extend.You may ask:
TestSGLangBitwiseParityKDADPEP? Graphs at DP need Dist-MoE experts: the default all-to-all dispatcher syncs the host, which CUDA graphs can't capture. KDADPEP keeps coverage no other class has: KDA's TP-sharded replay state, the eager idle step, and KDA with the all-to-all dispatcher. The changed code is shared by GDN and KDA.NOTE: the 2-GPU class has not run yet.
TestSGLangBitwiseParityGDNDPEPneeds 2 GPUs and thedist_moepackage, and it waits for a node with both. Dist-MoE on the Qwen3.5 MoE debug model is checked only at config level: its 4 MoE layers map to Dist-MoE experts. Two results would change this PR:maybe_convert_decode_to_extendsays an idle rank makes the prefill runner refuse the replay. The runner's code reads the padded token counts instead, so only the run settles it.NOTE: KDA's captured extend at DP has only the 1-GPU test (
test_kda_idle_rank_replay_writes_nothing).TestSGLangBitwiseParityKDADPEPruns without CUDA graphs.NOTE: stacked on "[rl] SGLang generator: run GDN and KDA prefill inside breakable prefill CUDA graphs". No recipe runs a GDN or KDA model on the SGLang engine today: the one SGLang recipe is Qwen3-0.6B (
rl_grpo_qwen3_0_6b_varlen_sglang).NOTE: the code's TODO on SGLang's extra_buffer hooks now also lists
mamba_track_seqlens_cpuand_original_batch_size. The open draft "use SGLang's public extra_buffer hooks for GDN and KDA" removes that TODO, but it predates the parent PR's refresh, so whichever lands second rebases the refresh's track-field reads._original_batch_sizecomes from SGLang's DP padding, not the extra_buffer cache: it stays a private read, as in SGLang's own GDN and KDA backends.Test plan
tests/rl/unit_tests/gpu/test_sglang_gdn_prefill_graph.py(1 GPU):TestSGLangBitwiseParityGDNDPEP(new; 2 GPUs anddist_moe): the Qwen3.5 MoE debug model with Dist-MoE experts, DP 2 x TP 1, EP 2, breakable graphs, 100-token prefill chunks.test_idle_dp_rank_replays_prefill_graphs: rank 1 caches 5 sessions, then idles while rank 0 prefills new prompts, then resumes the 5 sessions. It asserts rank 1 replays at least once while idle, no GDN layer runs eagerly inside a replay, and both ranks' logprobs equal the trainer's, bitwise.test_dp_ranks_with_different_requests: rank 1 decodes while rank 0 still prefills (eager steps, counted on rank 1, at least one), then each rank runs a cached next turn. Both turns bitwise against the trainer.TestSGLangBitwiseParityGDNHiCache(batch-invariant), run at TP 1 through a local subclass (this box has one GPU): 10 passed (its 9 tests, plus a local one that only prints the cap);TestSGLangBitwiseParityKDA: 7 passed;TestSGLangFp8KVCacheGDNHiCache: 8 passed, 2 skipped (the class skips its two batch-invariance tests).pytest tests/rl/unit_tests/cpu: 771 passed, 8 skipped, 2 failed. The 2 also fail on main:test_kv_offload_coordination.py::test_terminal_cleanup_preserves_block_identity, 2 cases.torchtitan/rl/): all hooks pass.