Skip to content

[Feat] dflash: Send the draft KV ring over PD disaggregation (Mooncake) - #49665

Merged
hnyls2002 merged 94 commits into
sgl-project:mainfrom
yhyang201:port/dflash-draft-kv-ring-pd
Nov 12, 2026
Merged

hnyls2002 merged 94 commits into
sgl-project:mainfrom
yhyang201:port/dflash-draft-kv-ring-pd

Conversation

@yhyang201

@yhyang201 yhyang201 commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Original authors: @yhyang201

Port of #48414

Builds on #49664 and #49621 (both merged); based on main.

#49664 keeps an all-sliding-window MLA DFLASH draft's KV in a per-request ring of window + block rows, but nothing sends the ring between PD peers, so #49664 resolves it off under PD disaggregation and K3.1's PD deployment keeps the per-token draft pool. This takes the PD part of the Day-0 ring (with its later Day-0 fixes), which sends the ring from prefill to decode, so PD gets the same target KV gain (+15.0% target KV on K3.1 colocated, see #49664).

Changes against the raw port (the first commit, [Feat] dflash: Raw port of the draft KV ring's PD transfer from kimi-k3.1-day-0, copies the Day-0 hunks byte for byte; each change below is its own commit):

  • dflash_draft_ring.py keeps Any and Callable in its typing import next to the PD transfer's names; the ring part of the module ([Perf] dflash: Keep a sliding-window MLA draft's KV in a per-request ring #49664) uses them.
  • The ring is read from the resolved speculative_dflash_draft_kv_ring ([Perf] dflash: Keep a sliding-window MLA draft's KV in a per-request ring #49664), not from Day-0's env flag.
  • The scheduler gives the ring to PrefillBootstrapQueue / DecodePreallocQueue, which pass it to setup_state_kv_args and read it in their payloads. Day-0's process-wide set_draft_kv_ring / get_draft_kv_ring global is not added. setup_state_kv_args registers the ring when it is given one and reads no config bag. Day-0's version needed a published spec namespace in every caller (13 existing wire tests failed).
  • Resolution: the ring stays on for PD over Mooncake (mooncake_tcp is rewritten to mooncake before resolution). It also resolves off when --disaggregation-decode-host-receive-threshold is below 1: decode host buffering receives only SWA state. With other transfer backends (NIXL, Mori, Ascend, fake) it resolves off with a log line, because they have no DRAFT_RING path. An explicit SGLANG_ENABLE_DFLASH_DRAFT_KV_RING=1 there is an error.
  • KV checksum (--disaggregation-enable-kv-checksum): KvChecksumComputer digested every registered state entry at the request's state indices. For a hybrid (KDA) target, these are the mamba slots, broadcast to all entries. With the ring, it digested ring rows and the min_pos row at mamba slot indices. That read past min_pos, and the digest did not match between the peers. On a unified pool, it failed the per-entry length assert. KvChecksumComputer.for_kv_args now leaves out exactly the ring's two entries. The ring is not checksummed. The same gap was found in [Feat] dflash: Enable FA3 draft KV rings and NIXL PD transfer #48842's draft-window rows.
  • The decode floor is reset only where no floor arrives from prefill: a request resumed from a decode retraction (host backup), and a fake transfer. Without it, a resumed request kept the slot's old floor and read the previous occupant's ring rows. On a real transfer, prefill's remote REQUEST_ROW write is the floor's only writer: a local reset there could land after that write under prefill_complete allocation and leave the request with an empty window.
  • The ring's entries are registered before state_window_tails is built, so state_wire carries them. Bootstrap then rejects a pair where only one side resolved the ring on, with a message that names the ring and SGLANG_ENABLE_DFLASH_DRAFT_KV_RING.
  • Mooncake treats DRAFT_RING and the REQUEST_ROW floor as replicated state, like QSA's (_REPLICATED_STATE_TYPES): no KV-head slicing, one elected writer under mixed TP, flat layout. A non-MLA target used to split them as K/V heads. This also covers DSpark's REQUEST_ROW (its SWA valid-from): under mixed attention TP one prefill rank now writes it, with the flat layout (the same as the MLA path DSV4 already takes), the rule should_send_replicated_state applies to other replicated state; on main every rank wrote the same value.
  • The ring's budget charge counts the PD decode pre-alloc rows (_req_pool_pre_alloc_size).
  • No pipelined prefill stage support: DFLASH requires pp_size == 1, so empty-slot registration for a stage without the draft is left for [Feat] dflash: Run DFLASH on a pipelined PD prefill #49648.
  • The Mooncake reslice-check skip is keyed on the manager's own DRAFT_RING registration, not on "zero draft entries" or a global flag. Day-0's condition also let through a DFLASH peer with no draft entries and no ring, which main rejects.
  • With the ring, decode's non-blocking metadata publish (staging with SGLANG_ENABLE_NV_MEGA_RUNTIME_OPTIMIZATION) falls back to the blocking path, because the ring's payloads are host arrays. The K3.1 recipes set neither.
  • The K3.1 memory calculator counts the ring under PD over Mooncake, and not below a decode host-receive threshold of 1.
  • draft_kv_ring_row_pairs (the row-0 filter) moves out of the Mooncake sender into disaggregation/utils.py so it can be unit-tested without importing the speculative module.

API changes the source's callers need:

  • Day-0's process-wide set_draft_kv_ring / get_draft_kv_ring are not added: the scheduler passes the ring to PrefillBootstrapQueue / DecodePreallocQueue, and setup_state_kv_args takes a draft_kv_ring argument.
  • The ring is read from the spec field speculative_dflash_draft_kv_ring ([Perf] dflash: Keep a sliding-window MLA draft's KV in a per-request ring #49664), not from SGLANG_DFLASH_DRAFT_KV_RING.
  • New draft_kv_ring_row_pairs in disaggregation/utils.py (the Mooncake row-0 filter); KvChecksumComputer.for_kv_args drops the ring's two entries.
  • DFlashDraftKVRing takes window_left, dcp_rank and pool (required); its min_pos uses the pool's custom mem pool.

Left for #49648: running the ring on a pipelined PD prefill (stages without the draft register empty slots for the ring's entries and are not charged for it).

Modifications

What the PD transfer does:

  • StateType.DRAFT_RING: one item per ring row. The ring's window floor (min_pos) is sent as the existing REQUEST_ROW state, one item per req pool row.
  • Prefill: _draft_ring_payload sends transfer_rows(slot, seq_len), which is this rank's ring rows of the last window_left + 1 positions in position order. Row 0 marks a position that this rank does not own under DCP.
  • Decode: receives the same positions into its own slot's rows. Each decode rank keeps the positions it owns.
  • setup_state_kv_args registers the ring's two entries last on both sides, so they pair by position.
  • Mooncake: DRAFT_RING is a generic, exactly-matched, replicated state. Pairs where either side has row 0 are dropped. The draft TP-reslice check is skipped, because there are no draft KV entries.
  • min_pos is allocated in the KV pools' cuMemCreate pool, because Mooncake's NVLink transport writes it remotely.
  • The scheduler passes no draft token pool to the KV transfer when the ring is on.

Dependencies

Compared with #48842's lifecycle handling

#48842 (NIXL, FA3 drafts, opt-in --speculative-draft-window-kv) keeps a per-request draft-window ring on decode. It handles these lifecycle cases:

The target model verifies every draft token. So these gaps can lower acceptance length, but they cannot change the output. Ring locs are always in range.

Accuracy and performance

Not run. PD end to end needs two 8-GPU nodes (prefill and decode). Day-0 measured it on the source branch (K3.1, TP8 prefill, DCP8 decode, Mooncake): target KV +15.2%, accept length unchanged within noise. Those numbers are not from this port.

Tests

  • unit/spec/test_dflash_draft_kv_ring.py (CPU). With DCP 1/8 and 8/8, each decode rank gets exactly the window positions it owns, at the row its own ring locs give. The test also checks the buffer infos (one item per ring row and per req pool row), and the resolution under PD over mooncake (on), over nixl and with decode host receive (off, and an error with an explicit =1).
  • unit/disaggregation/test_disaggregation_wire.py (CPU): setup_state_kv_args registers the ring's rows and floor after the storage's entries. Mooncake accepts a ring peer across a TP mismatch, and on a non-MLA target under mixed TP one rank sends the ring entries flat. The existing no-ring rejection still holds.
  • unit/disaggregation/test_unified_window_wire.py: a ring on one side only is rejected with a message naming the ring.
  • unit/disaggregation/test_decode_queue_cleanup.py: decode resets the ring floor for a fake transfer and a resumed request, and never on a real transfer.
  • unit/disaggregation/test_kv_checksum.py: for_kv_args leaves out the ring's two entries and keeps a non-ring REQUEST_ROW.
  • unit/model_executor/test_pool_configurator.py::TestDflashDraftKvBudget: the ring charge counts the PD decode pre-alloc rows.

Local runs before the review fixes (1b4ed1b), 8x B300, one GPU per file:

  • GPU: test_dflash_mla_backends.py 3 passed (9 subtests), test_mla_window_indices.py 10, test_dflash_ring_locs.py 2, test_kv_checksum.py 30, test_dcp_lse_combine.py 21 passed.
  • CPU cases: test_dflash_draft_kv_ring.py 12 (27 subtests), test_disaggregation_wire.py 82, test_minimax_sparse_disagg_state_kv_args.py 2, test_mooncake_transfer_batching.py 21, test_pd_role_switch.py 39, test_decode_queue_cleanup.py 20, test_pool_configurator.py 75, test_oot_dflash_hooks.py 7, test_dflash_mla.py 8 passed.
/rerun-test registered/unit/spec/test_dflash_draft_kv_ring.py registered/unit/disaggregation/test_disaggregation_wire.py registered/unit/disaggregation/test_kv_checksum.py registered/unit/disaggregation/test_minimax_sparse_disagg_state_kv_args.py registered/unit/disaggregation/test_mooncake_transfer_batching.py registered/unit/disaggregation/test_unified_window_wire.py registered/unit/disaggregation/test_decode_queue_cleanup.py registered/unit/model_executor/test_pool_configurator.py::TestDflashDraftKvBudget registered/kernels/ops/attention/test_mla_window_indices.py registered/kernels/ops/speculative/test_dflash_ring_locs.py

CI States

Latest PR Test (Base): ⏳ Run #43196938396
Latest PR Test (Extra): ❌ Run #43196938299
Latest PR Test (AMD ROCm 10): ⏳ Run #43196938498

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

kpham-sgl and others added 30 commits November 10, 2026 12:28
…d cutedsl_mla from sgl-project#48414

Copy the Day-0 hunks that serve a DFLASH MLA draft's sliding_attention
layers on the MLA-only draft backends: trtllm_mla's sliding-window verify
path over trtllm-gen sparse MLA (with the rank-local LSE under DCP), the
MLA-only draft backend admission, and the compact draft window
resolution. The per-row window index kernel (mla_window_sparse_indices)
and its test are on main since sgl-project#49411.

Co-authored-by: Yuhao Yang <[email protected]>
…rafts

Copy the Day-0 hunk that runs DFlashGroupedConv on the Triton
dflash_grouped_conv kernel (on main since sgl-project#49411) for 2-D CUDA inputs.
…_draft_config

Main's parse_dflash_draft_config reads is_mla through _parse_dflash_is_mla,
which accepts a scalar attention_mode without num_hidden_layers. The Day-0
get_dflash_attention_modes rejects that, so restore main's parser and make
is_dflash_mla_draft_config, the name the ported hook and window resolution
call, a thin entry over it.
Day-0 defaults it to SGLANG_ENABLE_NV_MEGA_RUNTIME_K31_GB_OPTIMIZATION,
which main does not have.
The draft KV ring is not ported, so the sliding-window path always reads
the draft's keys through req_to_token and the runner's page table.
…layers

Day-0 drops the triton-only check and rejects windows in FlashInfer MLA's
causal planning, which main replaced with _planned_causal. Keep main's
check in DFlashMLAAttention and widen it to the window-capable backends,
resolving with algo_label="DFLASH" so an MLA-only backend is not silently
replaced by the platform fallback.
…aft comment

KVCacheConfigurator.draft_mla_rows_are_physical is Day-0's; on main the
draft's row space comes from draft_loc_space_scale.
…layer

forward_extend now reads layer.sliding_window_size to pick the window
path; the stub layer of the DCP metadata test had none.
The raw port admits trtllm_mla for any MLA draft, including one that
inherits it from the target. Its decode kernel has no bidirectional
mask, so main falls back to flashinfer there
(test_oot_dflash_hooks); keep that fallback and admit trtllm_mla only
from --speculative-draft-attention-backend.
# Conflicts:
#	python/sglang/srt/models/dflash.py
Without the ring transfer a decode server would start with an empty draft
window. Resolve the default off with a log line on prefill and decode
servers, and refuse an explicit SGLANG_DFLASH_DRAFT_KV_RING=1.
Under --enable-unified-memory the draft runner binds its KV views inside the
target's pool (the fused draft placement, which gives sliding-window draft
layers their own window band), so there is no private draft pool to size as
a ring, and ring locs would index the target's pages.
A draft pool that follows the target's post-capture backing is resized to
the target's token count after capture, which would replace the ring's
fixed size. The ring is allocated up front instead.
…fill port

The raw port brings _has_dflash_draft_kv with Day-0's last-PP-stage clause,
which belongs to DFLASH on a pipelined prefill, ported separately. Keep only
the ring clause here.
The raw port takes draft rows out of the target's per-token cost but charges
the ring itself nowhere, so it is allocated from the activation slack after
the target pool. Main charges per-request fixed pools before token sizing
(the Mamba state in _profile_available_bytes); charge the ring there too,
sized like the draft runner builds it: rows for every req pool row and the
pool's padding page, at the draft's resolved bytes per row.
The ring stays on for a sliding-window MLA draft on trtllm_mla and
cutedsl_mla, resolves off under PD and unified memory (an explicit 1 there
is an error), and is charged as its draft pool while the target's per-token
cost drops the draft row.
dflash_ring_block_locs (SGLANG_OPT_DFLASH_RING_LOCS_FUSED) has no test on
main; compare it with DFlashDraftKVRing.locs with and without DCP.
…KV ring

The draft block now reads DFlashWorkerV2.draft_kv_ring, which the stub
built with object.__new__ did not set.
…T_KV_RING

The env-var conventions want a verb token. The default stays on, and an
explicit 1 where the ring does not fit stays an error. The K3.1 memory
calculator reads the new name.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@hnyls2002 hnyls2002 added the highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed label Nov 12, 2026
@hnyls2002

Copy link
Copy Markdown
Collaborator

/rerun-test registered/unit/mem_cache/test_decode_retraction_backup.py registered/unit/managers/test_priority_scheduling_disaggregation.py registered/ep/test_mooncake_ep_small.py

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/mem_cache/test_decode_retraction_backup.py  # base-b

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/managers/test_priority_scheduling_disaggregation.py  # base-a

🚀 4-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/ep/test_mooncake_ep_small.py  # base-c

@hnyls2002
hnyls2002 merged commit 406b727 into sgl-project:main Nov 12, 2026
89 of 165 checks passed
hnyls2002 added a commit that referenced this pull request Nov 12, 2026
# Conflicts:
#	python/sglang/srt/disaggregation/base/conn.py
#	python/sglang/srt/disaggregation/decode.py
#	python/sglang/srt/disaggregation/mooncake/conn.py
#	python/sglang/srt/disaggregation/prefill.py
#	python/sglang/srt/managers/scheduler.py
#	python/sglang/srt/speculative/dflash_draft_ring.py
#	python/sglang/srt/speculative/dflash_worker_v2.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

amd blackwell SM100/SM120 documentation Improvements or additions to documentation highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed mooncake run-ci CI: run the baseline test suite on this PR speculative-decoding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants