Skip to content

[Perf] dflash: Keep a sliding-window MLA draft's KV in a per-request ring - #49664

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

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

Conversation

@yhyang201

@yhyang201 yhyang201 commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Original authors: @yhyang201 @Oasis-Git @alphabetc1 @Fridge003

Port of #48414

A DFLASH MLA draft whose layers are all sliding_attention (the Kimi-K3.1 DFlash2 drafts, window 1024) reads only the last window_left + 1 positions, and the draft KV of a position depends only on the target hidden state there. Main still keeps one draft row per target slot and charges it to the target's per-token KV budget. This takes the Day-0 per-request draft KV ring, which gives K3.1 +15.0% target KV (below).

Changes against the raw port (the first commit, [Perf] dflash: Raw port of the draft KV ring ..., copies the Day-0 hunks byte for byte; each change below is its own commit):

  • The verify-commit cache_loc runs without Day-0's megakernel tail check (if not (tail_done and self._tail_appends_aux(bs))), which is not ported.
  • No PD transfer: the ring resolves off under PD.
  • Off under --enable-unified-memory. There the draft binds its views inside the target's pool ([Refactor] mem_cache: Plan draft KV bands by retention lifetime #48005), so a ring would index the target's pages.
  • A ring draft pool does not follow the target's post-capture KV backing.
  • Budget: the raw port drops the draft row from the per-token cost but charges the ring nowhere. The ring is now charged before token sizing (_dflash_draft_kv_ring_bytes), and the charge, the draft's allocation and the ring's locs take the ring geometry from one function (draft_kv_ring_pool_rows_from_config, draft_kv_ring_rows_per_req_from_config). The charge counts the request rows plus the padding row; the PD pre-alloc rows come with the PD transfer in [Feat] dflash: Send the draft KV ring over PD disaggregation (Mooncake) #49665.
  • The ring decision is a declared spec field, not a flag.set(False) into os.environ.
  • SGLANG_OPT_DFLASH_RING_LOCS_FUSED is dropped. Both of its paths are bit-exact, so the fused launch is the only path. The K3.1 recipe lines that set it are removed.
  • The DCP-collapsed-rows check moves from worker init to resolution.
  • _charges_dflash_draft_kv_per_token keeps only the ring clause.
  • A request's window floor (min_pos) is set on the first extend after its req pool slot is allocated (the pool's req_generation), not per rid: a later request that reuses a rid in the same slot, and a retracted request that gets its slot back, get their own floor instead of reading earlier rows.
  • Under the ring the draft writes no window sub-pool ids, which are target-space ids.
  • The ring's pool sizing is one arm of the draft-allocator branch, next to the unified and HiSparse arms.
  • The K3.1 cookbook and memory calculator count the ring only where it runs: colocated, on a cutedsl_mla / trtllm_mla draft backend, without --enable-unified-memory. ROCm's Triton draft backend keeps the per-token draft pool.
  • The compact draft window is not taken ([Feat] dflash: Serve the Kimi-K3.1 DFlash2 sliding-window MLA draft #49621 leaves it out), so the window indices keep no _window_key_positions call and no SGLANG_DISABLE_DFLASH_COMPACT_SWA_WINDOW.

API changes the source's callers need:

  • SGLANG_DFLASH_DRAFT_KV_RING is renamed SGLANG_ENABLE_DFLASH_DRAFT_KV_RING and is only the user switch; readers use the no-CLI spec field speculative_dflash_draft_kv_ring through get_spec().
  • SGLANG_OPT_DFLASH_RING_LOCS_FUSED is removed.
  • ModelRunner.draft_kv_ring is removed: the worker hands the ring to the draft attention backend with TRTLLMMLABackend.set_draft_kv_ring(ring).
  • New in KVCacheConfigurator: _dflash_draft_kv_ring_bytes.

Left for #49665: the PD transfer (state types, payload, decode floor reset, Mooncake handling, the PD pre-alloc rows in the ring charge).
Left for #49648: Day-0's last-PP-stage clause of _charges_dflash_draft_kv_per_token; whichever PR lands second combines both clauses.
Left for the GB optimization / megakernel port: the ring's megakernel verify tail (_tail_on_megakernel(cache_loc=ring_block_locs ...)).

Modifications

Each request owns ceil((window_left + 1 + block) / dcp) + 1 rows per DCP rank:

loc(req, pos) = (1 + req * rows_per_req + (pos // dcp) % rows_per_req) * dcp + pos % dcp

The loc keeps the pool's DCP owner rule, so the existing writers work unchanged. Row 0 is the write sink. The window kernel reads the same rows in ring mode (mla_window_sparse_indices(ring_rows_per_req=, min_pos=)), and dflash_ring_block_locs computes a draft block's locs. Both kernels are on main from #49411.

  • speculative/dflash_draft_ring.py (new): ring geometry, locs, the extend tail, window floors, and the support check.
  • speculative/dflash_worker_v2.py: builds the ring after the draft pool and gives it to the draft backend (set_draft_kv_ring). Prefill, draft-block and verify-commit writes go to ring locs. There is no HiCache draft plan under the ring.
  • layers/attention/trtllm_mla_backend.py: the window indices read the ring and its floors (also cutedsl_mla, which subclasses it).
  • arg_groups/speculative_hook.py, arg_groups/fields/spec.py, environ.py: the ring is resolved once at startup into the no-CLI spec field speculative_dflash_draft_kv_ring. The readers use get_spec(). SGLANG_ENABLE_DFLASH_DRAFT_KV_RING (default on) is only the user switch.
  • mem_cache/kv_cache_configurator.py, model_executor/pool_configurator.py, model_executor/model_runner_components/kv_pool_runtime.py: the ring-sized draft pool, the target budget charge, and the post-capture opt-out.

The ring is on only for an MLA draft whose layers are all sliding_attention, on draft backend trtllm_mla or cutedsl_mla, and with a draft that runs DCP when DCP is on (--speculative-draft-window-size is already rejected for such a draft). Under PD disaggregation or --enable-unified-memory it is off. Where it does not fit, it resolves off with a log line, and an explicit SGLANG_ENABLE_DFLASH_DRAFT_KV_RING=1 is an error. Resolution checks the launch flags before it loads the draft HF config. Other DFLASH drafts (MHA, any full-attention layer, other draft backends) keep the per-token pool.

Behavior change

The ring is on by default for the drafts above, and it changes what the draft attends to. The ring holds nothing from before a request's first extend, so min_pos puts the window floor at the prefix-cache hit length. After a radix or HiCache hit, the draft sees no context before the hit until the window fills with new tokens. HiCache keeps no draft sidecar for the ring. The cost is acceptance on the first draft steps after a hit. The gain is target KV capacity (+15.0% on K3.1, below). To turn the ring off, set SGLANG_ENABLE_DFLASH_DRAFT_KV_RING=0.

Relation to #48842 and #48785

Accuracy and performance

8x B300, Kimi-K3.1-Pro TP8 DCP8 EP2, DFlash2-Preview draft (5 sliding-window MLA layers, window 1024) on cutedsl_mla, block 8, HiCache L2, prefill breakable CUDA graph. Same launch, ring on vs SGLANG_ENABLE_DFLASH_DRAFT_KV_RING=0. These runs used this branch before the review fixes (the env var then had its old name, SGLANG_DFLASH_DRAFT_KV_RING); the fixes do not change the ring's data path.

Ring off Ring on
Target KV tokens per rank 663,104 762,752 (+15.0%)
max_total_num_tokens 5,304,832 6,102,016
Draft KV pool rows 663,104 4,480
GSM8K 97.73% 97.95%
AIME26 x8 pass@1 (maj@8) 99.58% (100%) 99.58% (100%)
Accept length, concurrency 1-8 / 9-32 / 33-64 2.583 / 3.149 / 3.454 2.906 / 3.169 / 3.376
Accept length, AIME phase only, 1-8 / 9-32 / 33-64 2.625 / 3.021 / 3.224 2.843 / 3.006 / 3.218

Accept lengths are corrected for repetition loops (one looping sample in each run, a known model behavior with the ring on and off). The 9-32 and 33-64 columns agree within 0.015 on the AIME phase; the 1-8 column has few samples and is noisy. Ring log: "130 rows per request per rank (1040 positions), 34 req pool rows, draft pool 4480 rows", 0.02 GB charged to the KV budget. No tracebacks.

GSM8K and AIME26 have no prefix-cache hits, so this table does not measure the prefix-hit trade-off (Behavior change). A multi-turn workload with radix or HiCache hits is still to be measured.

For reference, the source branch before the squash (#48414 history, TP8 DCP8 EP2, --mem-fraction-static 0.85): target KV slots per GPU 688,256 → 793,088 (+15.2%), accept length 3.117 → 3.107 over 10 chats x 20 turns.

Tests

  • unit/spec/test_dflash_draft_kv_ring.py (CPU, new): resolution on, off (also for an explicit =0) and error cases. The cheap flags settle the answer without loading the draft config. The DCP case, the window/block disjointness, the extend tail, and the floor reset for a new request in a reused slot.
  • unit/model_executor/test_pool_configurator.py::TestDflashDraftKvBudget: the ring removes the per-token draft charge. The ring charge equals the pool built over the req_to_token table the configurator really builds (with and without DCP and paging). With the ring off, nothing is charged.
  • kernels/ops/attention/test_mla_window_indices.py (GPU): in ring mode, the window rows are the rows DFlashDraftKVRing.locs writes, floored at min_pos.
  • kernels/ops/speculative/test_dflash_ring_locs.py (GPU, new): dflash_ring_block_locs matches DFlashDraftKVRing.locs.
  • unit/spec/test_dflash_draft_graph_eligibility.py: the worker stub gets draft_kv_ring = None.

Local runs at the branch head (1c7839c, after merging #49621's head), 8x B300, one GPU per file:

  • GPU: test_dflash_mla_backends.py 3 passed (9 subtests, incl. 96 heads), test_dflash_grouped_conv.py 18, test_trtllm_mla_family_dcp_metadata.py 26, test_dcp_lse_combine.py 21, test_mla_window_indices.py 10, test_dflash_ring_locs.py 2 passed.
  • CPU cases: test_dflash_draft_kv_ring.py 8 (14 subtests), test_pool_configurator.py 75, test_dflash_draft_graph_eligibility.py 1, test_oot_dflash_hooks.py 7, test_dflash_mla.py 8 passed.

No CI test turns the ring on end to end. That needs an all-SWA MLA DFLASH draft, which only K3.1 has.

/rerun-test registered/unit/spec/test_dflash_draft_kv_ring.py registered/unit/model_executor/test_pool_configurator.py::TestDflashDraftKvBudget registered/unit/spec/test_dflash_draft_graph_eligibility.py registered/kernels/ops/attention/test_mla_window_indices.py registered/kernels/ops/speculative/test_dflash_ring_locs.py registered/spec/dflash/test_dflash.py::TestDFlashServerBase

CI States

Latest PR Test (Base): ⏳ Run #43140832932
Latest PR Test (Extra): ❌ Run #43140832945
Latest PR Test (AMD ROCm 10): ⏳ Run #43140832887

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.
@hnyls2002
hnyls2002 changed the base branch from k31-dflash/1-swa-draft to main November 11, 2026 12:34
hnyls2002 added a commit to yhyang201/sglang that referenced this pull request Nov 11, 2026
…name ring rows and the per-token charge helper; move pd pre-alloc rows to the pd layer; cookbook counts the ring only where it runs; trim test docstrings
hnyls2002 added a commit to yhyang201/sglang that referenced this pull request Nov 11, 2026
hnyls2002 added a commit to yhyang201/sglang that referenced this pull request Nov 12, 2026
…ring sizing as its own draft-allocator arm; no window sub-pool ids under the ring; drop the unreachable draft-window check
@hnyls2002

Copy link
Copy Markdown
Collaborator

/rerun-test registered/unit/spec/test_dflash_draft_kv_ring.py registered/unit/model_executor/test_pool_configurator.py::TestDflashDraftKvBudget registered/unit/spec/test_dflash_draft_graph_eligibility.py registered/kernels/ops/attention/test_mla_window_indices.py registered/kernels/ops/speculative/test_dflash_ring_locs.py registered/spec/dflash/test_dflash.py::TestDFlashServerBase

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (3 tests): ✅ View workflow run

cd test/ && python3 registered/unit/spec/test_dflash_draft_kv_ring.py  # base-a
cd test/ && python3 registered/unit/model_executor/test_pool_configurator.py TestDflashDraftKvBudget  # base-a
cd test/ && python3 registered/unit/spec/test_dflash_draft_graph_eligibility.py  # base-a

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

cd test/ && python3 registered/kernels/ops/attention/test_mla_window_indices.py  # base-b-kernel-unit

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

cd test/ && python3 registered/kernels/ops/speculative/test_dflash_ring_locs.py  # base-b-kernel-unit
cd test/ && python3 registered/spec/dflash/test_dflash.py TestDFlashServerBase  # extra-a

@hnyls2002
hnyls2002 merged commit cb79602 into sgl-project:main Nov 12, 2026
135 of 164 checks passed
yhyang201 added a commit to yhyang201/sglang that referenced this pull request Nov 12, 2026
Conflicts: the ring module keeps PD's custom-mem-pool min_pos and transfer helpers with sgl-project#49664's req_generation floors; the ring charge sizes with draft_kv_ring_pool_rows_from_config over request, PD pre-alloc and padding rows. The draft worker reads window_left from the draft config again, which sgl-project#49664 no longer kept as a local.
yhyang201 added a commit to yhyang201/sglang that referenced this pull request Nov 12, 2026
main carries sgl-project#49664 as squash cb79602. Conflicts were re-merged with the final ring head 5f321ab as the base, so the PR diff against main equals the PD changes on top of the ring.
hnyls2002 added a commit that referenced this pull request Nov 12, 2026
hnyls2002 added a commit that referenced this pull request Nov 12, 2026
… the ring test main's ring tests replace
hnyls2002 added a commit that referenced this pull request Nov 12, 2026
kevin-mii added a commit to kevin-mii/sglang that referenced this pull request Nov 18, 2026
…rough set_draft_kv_ring

main's sgl-project#49664 hands the ring to the draft's attention backend with
set_draft_kv_ring, and no longer leaves it on the draft model runner.
TritonAttnBackend had no such method, so every DFLASH server on the ROCm
triton draft backend under DCP (the MI35x DCP8 recipe) failed at startup
with an AttributeError, and MlaDcpWindowVerify read a runner attribute
that nothing sets. The backend now passes the ring to its window verify.

Found by a Kimi-K3.1-Pro TP8 DCP8 serving start on MI350X.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 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