Skip to content

[Feat] unified-memory: Support asymmetric KV rows - #43325

Merged
ch-wan merged 2 commits into
sgl-project:mainfrom
caihuali95:mainline/unified-asymmetric-kv-rows
Oct 18, 2026
Merged

ch-wan merged 2 commits into
sgl-project:mainfrom
caihuali95:mainline/unified-asymmetric-kv-rows

Conversation

@caihuali95

@caihuali95 caihuali95 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

This PR is part of a stack (oldest at bottom):

Current head cba61d6e7f, based on main 1d17066bad. Stack predecessor: #43171. This PR targets main after its stack predecessor merged.

Motivation

MiMo-V2-Flash has asymmetric K/V rows: 192-wide K heads and 128-wide V heads, on its full-attention and its sliding-window layers alike. --enable-unified-memory refuses any such model at boot ("does not yet admit asymmetric K/V rows"), so MiMo serves only on the default pool. That pool splits its memory between the two layer kinds at boot, and under long decodes it retracts requests while the other kind's memory still has room.

Modifications

Backend rejection reports the applicable allowed set without incorrectly attributing symmetric or MLA failures to asymmetric KV rows.

  1. Admit asymmetric K/V rows on the backends that carry v_head_dim (8523eb5e81). The unified pool stores each token's K and V rows side by side in one per-token entry, each at its own offset, so a V row narrower than K needs no pool-level refusal. What remains is a backend property: whether the kernel is told the V width at all. FlashInfer's paged_kv_t keeps separate K and V strides but one head_dim for both tiles, and sglang's MHA plan path never passes head_dim_vo; trtllm_mha refuses unequal QK/V dims. For a model with asymmetric rows, the attention backends that --enable-unified-memory accepts therefore narrow to ASYMMETRIC_KV_BACKENDS: triton, fa3 and fa4, which pass layer.v_head_dim through. fa4 is the resolved default for an asymmetric-K/V model on SM100, so leaving it out would make such a model unbootable in its own default configuration; it also runs on SM90, and the accuracy table below includes it. A speculative draft whose KV is fused into the target's pages follows the same rule (it fuses only when the target's and the draft's backends all carry v_head_dim), its share of each entry is sized with the draft's own v_head_dim, and the check that every row is 16-byte aligned covers the V row as well as the K row.
  2. Build pools from the actual full/SWA head geometry (2aaa913354). Both hybrid-SWA factories and fused FULL host sizing share _unified_swa_head_geometry, so the bytes priced at startup match the entries allocated. The GPU path reads ModelConfig's full/SWA KV-head counts and K/V widths; the NPU compress path retains its HF-config geometry. Non-SWA Mamba/full hosts also carry their actual V width. This removes duplicated geometry decisions and covers asymmetric rows outside the compress-model list.

Tests (CPU): TestUnifiedSWAHeadGeometry runs both factories on a model outside the compress list whose sliding-window layers have 8 KV heads against 4 and 128-wide V rows against 192-wide K, and checks the head geometry each hands its pool constructor: (4, 192, 192, 4, 192, 192) before this change, (4, 192, 128, 8, 192, 128) after it. The test stubs for models with both Mamba-style state and sliding-window layers gain the SWA head fields and v_head_dim.

Accuracy Tests

Earlier combined stack b550cacde6 adds a test-fixture correction in #43326: the Gemma4 read-stream fixture declares its FULL address space, and the HiSparse registry test uses real CacheInitParams for the retained-window calculation. Both complete test files pass locally with GPUs hidden: 4 Gemma4 tests and 26 registry tests. Pre-commit passed. The five descendant patches are unchanged (git range-diff); this follow-up changes no production code. The earlier 455-test run did not cover these two files; GitHub CI has not yet been revalidated for this head.

Validation before the test-fixture follow-up, on combined stack 4cf27a551c: 455 tests and 921 subtests passed on B300, plus the two widened-history regressions against Rust TreeCore. The new FULL-fallback metadata cases cover eager execution and CUDA graph replay. New allocation-reclaim, explicit-prefill and JIT-reuse regressions fail on the previous code and pass with the fixes. These are combined-stack checks, not independent model reruns of each PR.

The measurements in this section are historical results from before this rebase, at the revisions/configurations stated below. Current combined-stack validation is linked above.

The fa4 runs and the GPU unit tests used this PR's current commits over main 7224fce. The triton and fa3 GSM8K runs used the same main, the load tests and the Qwen comparison under Speed Tests the earlier main 4b384df, and both used an earlier form of commit 2 that built the same pools for every model below.

SM90, MiMo-V2-Flash, TP4, no speculation, radix cache, overlap scheduler, CUDA graphs on. Default pool against --enable-unified-memory. GSM8K, all 1314 test questions, 5-shot, greedy:

attention backend default pool unified
triton 0.839 0.838
fa3 0.837 0.836
fa4 0.830 0.849

With CUDA graphs off, fa4 scores 0.844 on the default pool and 0.830 on the unified pool. In the fa4 runs the unified pool filled every byte that is never written with NaN (SGLANG_DEBUG_POISON_POOL=1), so a read of a slot nobody wrote would show up as lost accuracy. Over 1314 questions one run's score has a sampling error of about one point, so both fa4 pairs are within noise.

On both layer kinds, the unified pool stores MiMo's K and V rows at their real 192 and 128 widths, in entries of the size the startup memory sizing computes.

The triton and fa3 MiMo runs used the head of the next PR in this stack, which changes what a speculative draft binds and leaves a run without a draft as it is in this PR.

GPU unit tests for unified memory, on this PR's current commits: 4141 passed, 0 failed.

Speed Tests and Profiling

The measurements in this section are historical results from before this rebase, at the revisions/configurations stated below. Current combined-stack validation is linked above.

The same MiMo configuration under two load tests: 100 requests of 512 input / 4096 output tokens at concurrency 128, and 192 requests that share a 4096-token system prompt, each with a 128-token question and 2048 output tokens, at concurrency 192. Every request completed on every server, and no server reported an error.

attention backend requests × input / output tokens default pool req/s unified req/s change
triton 100 × 512 / 4096 0.615 0.896 +45.6%
triton 192 × (4096 shared + 128) / 2048 0.867 1.259 +45.2%
fa3 100 × 512 / 4096 0.611 0.874 +43.2%
fa3 192 × (4096 shared + 128) / 2048 0.824 1.156 +40.3%

The gain is capacity: from the same memory, the unified pool lets either layer kind take the bytes the default pool splits between them at boot, so it retracts fewer requests.

A model this PR does not admit is unaffected: Qwen3.5-9B without speculation (200 requests of 2048 input / 512 output tokens at concurrency 64), run back to back on one host with and without this PR, keeps its unified-vs-default-pool req/s gap at −0.4% (fa3), −0.5% (triton) and −0.4% (trtllm_mha), against −0.3%, −0.2% and −0.5% without it. The default pool runs twice per configuration, and its two runs differ by at most 0.2%.

Checklist


CI States

Latest PR Test (Base): ⏳ Run #39128069121
Latest PR Test (Extra): ⏳ Run #39128069110
Latest PR Test (AMD ROCm 10): ⏳ Run #39128069108

The token-major entry holds a K row and a narrower V row at their own
offsets under one slot stride, so the pool-level refusal of
head_dim != v_head_dim goes. What remains is a backend property: whether the
kernel is told the V width at all. FlashInfer's `paged_kv_t` keeps separate
K and V strides but a single `head_dim`, dispatched once for both tiles, and
sglang's MHA plan path never passes `head_dim_vo`; trtllm_mha refuses
unequal QK/V dims outright. So the MHA arm of the page-major allowlist
narrows to ASYMMETRIC_KV_BACKENDS -- triton, fa3 and fa4, which carry
`layer.v_head_dim` through. fa4 belongs there on its own merits and because
it is the resolved default for an asymmetric-K/V model on SM100, so dropping
it would make such a model unbootable under its own default configuration.

The fused draft placement follows the same rule -- an asymmetric-row draft
fuses only when the target's and the draft's backends all carry v_head_dim
(`_draft_backends_carry_v_head_dim`, handed to `place_fused_draft`) -- and
the region carries the draft's v_head_dim, so the priced entry counts the V
row at its own width.

fa4 is admitted for asymmetric K/V rows and supported, but not yet
validated: every validation run was on SM90, where the fa4 kernel does not
run. Reviewers with SM100-class hardware: please run an asymmetric-K/V
model on fa4 before merging.

With V rows of their own width, the placement's row-alignment check covers
both rows: a 16-B-aligned K row beside a misaligned V row would otherwise
pass the decision and trip the token-major layout's assert at pool build.

test_unified_tri_pool.py's lazy-checkpoint stub model gains v_head_dim,
which the mamba factory now forwards.
…ometry

The two SWA-family unified factories -- `_init_unified_swa_pools` (two-pool
hybrid-SWA hosts) and `_init_unified_mamba_swa_pools` (tri-pool hosts) --
each derived the full and window head geometry inline, and on every GPU
host (the non-compress branch) both hard-coded a symmetric shape:

    v_head_dim = head_dim
    swa_head_num = head_num
    swa_head_dim = head_dim
    swa_v_head_dim = head_dim

That was harmless while the unified pool refused asymmetric K/V models at
boot. The previous commit admits them, and under this derivation an
admitted MiMo-V2-Flash (192/128) would have its swa sub-pool allocated
with 192-wide V rows while `pool_configurator.py` prices the entry from
`swa_head_dim + swa_v_head_dim` = 192 + 128: the allocated entry is 20%
larger than the priced one, so `UnifiedKVPool.max_slots` lands ~17%
below what the boot solve committed to -- the under-sizing the tri-pool
sizing tests call a retract livelock.

One `_UnifiedSWAHeadGeometry`, produced by `_unified_swa_head_geometry()`
from the ModelConfig wrapper (`v_head_dim`, `get_swa_num_kv_heads`,
`swa_head_dim`, `swa_v_head_dim`; the hf text config on the NPU compress
path), now feeds both factories and the fused-entry price in
`_full_host_spec`, so the priced and the allocated entry cannot drift.
The fused price also gains a hybrid-SWA arm reading the same struct.

A draft with SWA layers of its own still declines in `place_fused_draft`
(no host arm serves them yet); the tri-pool test pins that the factory
stores the placement it was handed and puts the region on the full spec
alone.

test_fused_draft_placement.py: TestUnifiedSWAHeadGeometry builds the
geometry from a 192/128 model config and fails before this change (the
factories had no shared source, and the GPU path was symmetric); its
pricing case keeps the fused price on that same geometry.
test_unified_tri_pool.py's lazy-checkpoint stubs gain the SWA head fields
and `_unified_swa_head_geometry`, which the tri-pool factory now reads.
@ch-wan
ch-wan force-pushed the mainline/unified-asymmetric-kv-rows branch from 2aaa913 to cba61d6 Compare October 18, 2026 12:37
@ch-wan
ch-wan merged commit 19cb24f into sgl-project:main Oct 18, 2026
36 of 43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants