Repository navigation
[Perf] dflash: Keep a sliding-window MLA draft's KV in a per-request ring - #49664
Merged
hnyls2002 merged 68 commits intoNov 12, 2026
Merged
Conversation
…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.
…a and cutedsl_mla
# 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
requested review from
AgainstEntropy,
DarkSharpness,
HydraQYH,
bingxche,
celve,
kevin-mii,
michaelzhang-ai and
niehen6174
as code owners
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
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 |
Contributor
|
🚀 🚀 🚀 |
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
…nch's is_dflash_mla_draft_config
4 of 5 tasks
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]>
1 of 3 tasks
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 lastwindow_left + 1positions, 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):cache_locruns without Day-0's megakernel tail check (if not (tail_done and self._tail_appends_aux(bs))), which is not ported.--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._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.flag.set(False)intoos.environ.SGLANG_OPT_DFLASH_RING_LOCS_FUSEDis 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._charges_dflash_draft_kv_per_tokenkeeps only the ring clause.min_pos) is set on the first extend after its req pool slot is allocated (the pool'sreq_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.cutedsl_mla/trtllm_mladraft backend, without--enable-unified-memory. ROCm's Triton draft backend keeps the per-token draft pool._window_key_positionscall and noSGLANG_DISABLE_DFLASH_COMPACT_SWA_WINDOW.API changes the source's callers need:
SGLANG_DFLASH_DRAFT_KV_RINGis renamedSGLANG_ENABLE_DFLASH_DRAFT_KV_RINGand is only the user switch; readers use the no-CLI spec fieldspeculative_dflash_draft_kv_ringthroughget_spec().SGLANG_OPT_DFLASH_RING_LOCS_FUSEDis removed.ModelRunner.draft_kv_ringis removed: the worker hands the ring to the draft attention backend withTRTLLMMLABackend.set_draft_kv_ring(ring).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) + 1rows per DCP rank: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=)), anddflash_ring_block_locscomputes 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 (alsocutedsl_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 fieldspeculative_dflash_draft_kv_ring. The readers useget_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 backendtrtllm_mlaorcutedsl_mla, and with a draft that runs DCP when DCP is on (--speculative-draft-window-sizeis already rejected for such a draft). Under PD disaggregation or--enable-unified-memoryit is off. Where it does not fit, it resolves off with a log line, and an explicitSGLANG_ENABLE_DFLASH_DRAFT_KV_RING=1is 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_posputs 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, setSGLANG_ENABLE_DFLASH_DRAFT_KV_RING=0.Relation to #48842 and #48785
--speculative-draft-window-kv, with FA3 drafts. It rejects colocated serving.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 vsSGLANG_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.max_total_num_tokensAccept 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 rowsDFlashDraftKVRing.locswrites, floored atmin_pos.kernels/ops/speculative/test_dflash_ring_locs.py(GPU, new):dflash_ring_block_locsmatchesDFlashDraftKVRing.locs.unit/spec/test_dflash_draft_graph_eligibility.py: the worker stub getsdraft_kv_ring = None.Local runs at the branch head (1c7839c, after merging #49621's head), 8x B300, one GPU per file:
test_dflash_mla_backends.py3 passed (9 subtests, incl. 96 heads),test_dflash_grouped_conv.py18,test_trtllm_mla_family_dcp_metadata.py26,test_dcp_lse_combine.py21,test_mla_window_indices.py10,test_dflash_ring_locs.py2 passed.test_dflash_draft_kv_ring.py8 (14 subtests),test_pool_configurator.py75,test_dflash_draft_graph_eligibility.py1,test_oot_dflash_hooks.py7,test_dflash_mla.py8 passed.No CI test turns the ring on end to end. That needs an all-SWA MLA DFLASH draft, which only K3.1 has.
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-testto run the tests your change touches, or copy this prompt for an agent: