Skip to content

Feat: add compact linear spec cache replay to reduce intermediate cache memory. - #27658

Open
strgrb wants to merge 6 commits into
sgl-project:mainfrom
antgroup:pr/linear-compact-spec-cache
Open

strgrb wants to merge 6 commits into
sgl-project:mainfrom
antgroup:pr/linear-compact-spec-cache

Conversation

@strgrb

@strgrb strgrb commented Jun 9, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

For KDA and GDN, we can use delta cache instead of full ssm state:
image

In equation above, $k_t$ and $v_t^T - k_t^T Diag(\alpha_t)S_{t-1}$ are vectors, so we can store them and $\alpha_t$ as delta of ssm state.
Currently, KDA, GDN, Lightning Attn can use this method, and only GDN is implemented in this pr, because KDA verify is not implemented yet, and Lightning Attn is not formatted with RadixLinearAttention and may be modified when refactored.

In this pr, I store these 3 vectors to replace intermediate ssm states to decrease memory usage with large concurrency. To restore full ssm state, a replay kernel is organized.
For each head in each layer, full ssm state is [128, 128], and is reduced to [3, 128]. With 70 layers, 64 heads, 4 draft tokens, each req needs 4B * 128 * 128 * 64 * 70 * 4 = 560MB, which is quite significant for 32 or 64 concurrency.

This feature can also be used with radix cache, a full ssm state and some deltas are stored in 1 page, and we can replay to cache hit of any single token with it. This may be hard, and may be implemented in the future.

For performance, verify kernel speeds up by decreasing writing to global memory, and relay kernel slows down by increasing calculation, and total performance is the same or a little higher since both kernels are memory bound. I think with better GPUs, more performance gain can be found.

Following tests are done with H20.

starting command:

sglang serve --port 8188 \
  --model-path /dev/shm/Qwen3-Next-80B-A3B-Instruct/ \
  --tp-size 4 --chunked-prefill-size 16384 \
  --speculative-algorithm NEXTN \
  --mamba-scheduler-strategy extra_buffer \
  --max-mamba-cache-size 160 \
  --max-running-requests 32 \
  --mem-fraction-static 0.87 \
  --linear-attn-decode-backend triton \
  --linear-attn-prefill-backend triton \
  --enable-linear-compact-spec-cache

memory usage before:

[2026-06-09 15:40:52 TP2] Mamba Cache is allocated. max_mamba_cache_size: 160, conv_state size: 0.07GB, ssm_state size: 2.83GB intermediate_ssm_state_cache size: 2.32GB intermediate_conv_window_cache size: 0.05GB 
[2026-06-09 15:40:53 TP1] KV Cache is allocated. dtype: torch.bfloat16, #tokens: 3391626, K size: 19.41 GB, V size: 19.41 GB

memory usage after:

[2026-06-09 15:40:52 TP0] Mamba Cache is allocated with linear compact spec cache. max_mamba_cache_size: 160, conv_state size: 0.07GB, ssm_state size: 2.83GB intermediate_k_cache size: 0.02GB intermediate_v_cache size: 0.02GB intermediate_decay_cache size: 0.02GB intermediate_conv_window_cache size: 0.05GB 
[2026-06-09 15:40:53 TP0] KV Cache is allocated. dtype: torch.bfloat16, #tokens: 3583477, K size: 20.50 GB, V size: 20.50 GB

Modifications

Accuracy Tests

before:

$ python bench_sglang.py --port 8189 --data-path ./test.jsonl --num-questions 1319
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1319/1319 [02:28<00:00,  8.88it/s]
Accuracy: 0.951
Invalid: 0.000
Latency: 148.467 s
Output throughput: 1454.830 token/s

after:

$ python bench_sglang.py --port 8188 --data-path ./test.jsonl --num-questions 1319
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1319/1319 [02:25<00:00,  9.04it/s]
Accuracy: 0.952
Invalid: 0.000
Latency: 145.898 s
Output throughput: 1493.588 token/s

Speed Tests and Profiling

With 8k input and 1k output: opt vs origin

  • batch1: tpot 2.02 vs 2.03
  • batch8: tpot 3.32 vs 3.4
  • batch 32: tpot 9.36 vs 9.55

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ❌ Run #27732324898
Latest PR Test (Extra): ❌ Run #27732324795

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces a compact K/V/decay replay cache (--enable-linear-compact-spec-cache) for KDA and GDN hybrid linear attention models during speculative target verification, optimizing memory usage by caching per-token states instead of full SSM states. The code review identified several critical issues: Triton compilation errors and correctness bugs in the state replay kernels (using Python's range instead of tl.range and incorrect request slot indexing), an AttributeError in model_runner_kv_cache_mixin.py due to a misspelled ServerArgs attribute, and a missing import of TritonGDNKernel in gdn_backend.py that would cause a runtime NameError.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@strgrb

strgrb commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Jun 9, 2026
@strgrb

strgrb commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@yuan-luo yuan-luo added the run-ci-extra CI: also run the extra suite (requires run-ci) label Jun 10, 2026
@yuan-luo

yuan-luo commented Jun 10, 2026 •

Copy link
Copy Markdown
Collaborator

@strgrb Please add unit tests to cover the new functions for GDN.

@yuan-luo

Copy link
Copy Markdown
Collaborator

This is a novel and valuable feature. LGTM in general.

@strgrb
strgrb requested a review from sogalin as a code owner June 11, 2026 04:05
@strgrb

strgrb commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator Author

@strgrb Please add unit tests to cover the new functions for GDN.

Extra option is added to GDN test suite, and new cases are added to registered case.

@yuan-luo

Copy link
Copy Markdown
Collaborator

Could you add Qwen3-Next-80B-A3B-Instruct GSM8K full 1319 q result?

@yuan-luo

Copy link
Copy Markdown
Collaborator

@yizhang2077 @hanming-lu @kaixih Could you help to review this PR?

@yuan-luo

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@strgrb

strgrb commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator Author

Could you add Qwen3-Next-80B-A3B-Instruct GSM8K full 1319 q result?

Scores in description is 1319 result

@yuan-luo

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@yuan-luo

Copy link
Copy Markdown
Collaborator

@strgrb please resolve conflict.

@strgrb

strgrb commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator Author

@strgrb please resolve conflict.

resolved

@strgrb

strgrb commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

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

run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) speculative-decoding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants