Skip to content

[rl] SGLang generator: GDN and KDA prefill in breakable prefill graphs under attention DP - #6425

Draft
felipemello1 wants to merge 4 commits into
felipemello1/sgl203/s2-gdn-prefill-graphsfrom
felipemello1/sgl203/dp-prefill-graphs
Draft

felipemello1 wants to merge 4 commits into
felipemello1/sgl203/s2-gdn-prefill-graphsfrom
felipemello1/sgl203/dp-prefill-graphs

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

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.

Under attention DP, GDN and KDA prefill now stays inside the breakable graph segments, and rows past the real requests become padding; the 2-GPU test has not run yet.

Not run at attention DP yet: the 2-GPU class waits for a node (see NOTE).

attention DP 2, one step           before                                  this PR
rank 0 prefills, rank 1 prefills   graph replays, GDN/KDA layers eager     GDN/KDA inside the graph
rank 0 prefills, rank 1 idle       graph replays, GDN/KDA layers eager     GDN/KDA inside the graph; rank 1's
  (from SGLang's code)                                                     made-up request writes nothing
rank 0 prefills, rank 1 decodes    eager on both ranks                     eager on both ranks (unchanged)
# before: supports_prefill_graph_extend
return not self.is_draft_worker and get_parallel().attn_dp_size == 1
# after
return not self.is_draft_worker

# after: _fill_prefill_graph_metadata takes only the real requests
num_requests = forward_batch._original_batch_size
if num_requests is None:
    num_requests = forward_batch.batch_size

Problem

  1. Attention DP. Each DP rank runs its own scheduler. In every step, the ranks vote on replaying the prefill graph, and SGLang takes the minimum vote.
  2. The idle rank. When one rank prefills and another has nothing to run, SGLang pads both ranks to one captured bucket, so the graph can replay on both (from SGLang's code; the 2-GPU test checks it, see NOTE). The idle rank runs one made-up request: one sequence as long as that bucket, on request row 0, with ForwardBatch._original_batch_size 0.
  3. The gap. The parent PR has no test at attention DP, so it keeps the GDN and KDA extend (SGLang's name for a prefill forward) eager there: supports_prefill_graph_extend returns 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.
  4. The cost. On those steps, every GDN or KDA layer breaks the graph and runs eagerly. The parent PR measured about 2 ms of host time per GDN layer break at DP 1 (Qwen3.5-0.8B, one GB300).

Solution

  1. Mask. _fill_prefill_graph_metadata takes the real requests from ForwardBatch._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.
  2. Gate. supports_prefill_graph_extend drops attn_dp_size == 1. The engine's 256-request prefill cap (prefill_max_requests) now applies at any DP size, per DP rank.
  3. Guard. On a replay, refresh_prefill_graph_metadata first 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.
  4. Unchanged. At attention DP 1, _original_batch_size is 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:

  • Why not run the decoding rank's rows as 1-token extends, so mixed steps replay too? A 1-token extend can't take decode's prefix-cache snapshot: decode fills no prefill track lengths. SGLang's vote refuses it for these models for the same reason ("The converted view lacks prepare_for_extend's mamba-track fills"). The guard raises if a later pin lets those rows in.
  • Why mask, if row 0's slot writes nothing anyway? The tables then don't depend on SGLang reserving request row 0, and the idle rank runs no tokens. SGLang's own GDN and KDA backends mask the same way.
  • Why a new GDN class rather than turning on graphs in 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.
  • Why can't the engine test prove the mask? In an engine, the made-up request sits on SGLang's padding row 0, so it writes nothing with or without the mask. The 1-GPU test maps row 0 to a real slot, and it fails without the mask.

NOTE: the 2-GPU class has not run yet. TestSGLangBitwiseParityGDNDPEP needs 2 GPUs and the dist_moe package, 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:

  • Rank 1 shows 0 replays while idle. Then no engine step runs the mask, and this PR covers only steps where every rank prefills. SGLang's comment in maybe_convert_decode_to_extend says 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.
  • The mixed test counts 0 decode steps during rank 0's prefill. Then its setup does not make the steps it is there for, and it needs new prompts.

NOTE: KDA's captured extend at DP has only the 1-GPU test (test_kda_idle_rank_replay_writes_nothing). TestSGLangBitwiseParityKDADPEP runs 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_cpu and _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_size comes 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):
    • The capture batch now matches SGLang's: track slots on the device, no CPU track lists. So a check placed at capture fails here.
    • GDN: after a real batch, an idle rank's made-up request gives zero output and leaves every slot but the dummy slot 0 unchanged, slot 1 included: the test's stand-in maps request row 0 to it. A replay with decode rows raises.
    • KDA (new): the same idle replay, in default mode and in batch-invariant mode (with Attention Gym's replay state).
    • 4 passed on one GB300. Two code mutants fail it: no mask (4 failed), and the raise moved into the fill, where capture runs it (4 failed).
  • TestSGLangBitwiseParityGDNDPEP (new; 2 GPUs and dist_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.
    • The GDN class's tests also run at DP 2, with the same requests on both ranks.
    • Not run yet (see NOTE). It collects 10 tests. CI: added to the B200 RL job's 2-process loop.
  • On one GB300 at TP 1 (attention DP 1), with the GDN and KDA extends captured:
    • 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.
  • pre-commit on the changed files (pyrefly excludes torchtitan/rl/): all hooks pass.

… 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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 28, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@felipemello1
felipemello1 added this pull request to stack #6416 November 28, 2026 03:28
@claude

claude Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

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.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: Request changes -- the PR 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

  1. Run TestSGLangBitwiseParityGDNDPEP and 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.

  2. linear_attention.py:275 -- the private _original_batch_size read 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 -- is prefill_max_requests really 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_metadata too, 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_extend returned False at attn_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 from batch_size to num_requests. The rest is tests.
  • Mask correctness. With num_requests=0: num_tokens=0, so cu_seqlens[0:]=0 and padding_tokens are 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. With 0 < 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. GDNDPEP inherits all GDN base tests at DP 2. Its 2-process CI slot fits the existing B200 loop; dist_moe is already needed there by DistMoE.
  • 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.

Comment on lines +273 to +275
# 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This 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.

Comment on lines +244 to +254
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."
)

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.

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

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.

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.

This branch has not been deployed

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant