Skip to content

Enable IndexCache for DeepSeek V3.2#21405

Merged
Fridge003 merged 11 commits intosgl-project:mainfrom
jinyouzhi:ic
Apr 5, 2026
Merged

Enable IndexCache for DeepSeek V3.2#21405
Fridge003 merged 11 commits intosgl-project:mainfrom
jinyouzhi:ic

Conversation

@jinyouzhi
Copy link
Copy Markdown
Contributor

Motivation

fix #21286

Modifications

Accuracy Tests

python3 -m sglang.launch_server   --model-path /ssd/hf_models/DeepSeek-V3.2-Exp --tp 8 --mem-fraction-static=0.9 --tool-call-parser deepseekv32  --reasoning-parser deepseek-v3 --json-model-override-args '{"index_topk_freq": 4}'
lm_eval --model local-completions --model_args "base_url=http://127.0.0.1:30000/v1/completions,model=/ssd/hf_models/DeepSeek-V3.2-Exp,num_concurrent=100,tokenized_requests=False" --tasks gsm8k

gsm8k with DeepSeek-V3.2-Exp with this PR:

|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value |   |Stderr|
|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:|
|gsm8k|      3|flexible-extract|     5|exact_match|↑  |0.9583|±  |0.0055|
|     |       |strict-match    |     5|exact_match|↑  |0.9575|±  |0.0056|

main

|Tasks|Version|     Filter     |n-shot|  Metric   |   |Value |   |Stderr|
|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:|
|gsm8k|      3|flexible-extract|     5|exact_match|↑  |0.9598|±  |0.0054|
|     |       |strict-match    |     5|exact_match|↑  |0.9575|±  |0.0056|

Benchmarking and Profiling

Throghput improved ~ +6.4%
TTFT improved ~ -5.4%
TPOT improved ~ -5.5%

python3 -m sglang.bench_serving   \
  --backend sglang \
  --host 127.0.0.1 \
  --port 30000 \
  --model /ssd/hf_models/DeepSeek-V3.2-Exp \
  --dataset-name random \
  --num-prompts 500 \
  --random-input-len 1024 \
  --random-output-len 512

this PR:

============ Serving Benchmark Result ============
Backend:                                 sglang
Traffic request rate:                    inf
Max request concurrency:                 not set
Successful requests:                     500
Benchmark duration (s):                  113.18
Total input tokens:                      253168
Total input text tokens:                 253168
Total generated tokens:                  131674
Total generated tokens (retokenized):    129011
Request throughput (req/s):              4.42
Input token throughput (tok/s):          2236.90
Output token throughput (tok/s):         1163.42
Peak output token throughput (tok/s):    1811.00
Peak concurrent requests:                500
Total token throughput (tok/s):          3400.32
Concurrency:                             267.13
----------------End-to-End Latency----------------
Mean E2E Latency (ms):                   60465.83
Median E2E Latency (ms):                 60908.48
P90 E2E Latency (ms):                    103027.63
P99 E2E Latency (ms):                    111428.53
---------------Time to First Token----------------
Mean TTFT (ms):                          47792.35
Median TTFT (ms):                        47176.86
P99 TTFT (ms):                           102048.78
-----Time per Output Token (excl. 1st token)------
Mean TPOT (ms):                          49.13
Median TPOT (ms):                        49.81
P99 TPOT (ms):                           65.31
---------------Inter-Token Latency----------------
Mean ITL (ms):                           48.31
Median ITL (ms):                         35.71
P95 ITL (ms):                            116.53
P99 ITL (ms):                            158.38
Max ITL (ms):                            1811.18
==================================================

main

============ Serving Benchmark Result ============
Backend:                                 sglang
Traffic request rate:                    inf
Max request concurrency:                 not set
Successful requests:                     500
Benchmark duration (s):                  120.48
Total input tokens:                      253168
Total input text tokens:                 253168
Total generated tokens:                  131674
Total generated tokens (retokenized):    129011
Request throughput (req/s):              4.15
Input token throughput (tok/s):          2101.31
Output token throughput (tok/s):         1092.90
Peak output token throughput (tok/s):    1617.00
Peak concurrent requests:                500
Total token throughput (tok/s):          3194.21
Concurrency:                             265.43
----------------End-to-End Latency----------------
Mean E2E Latency (ms):                   63957.86
Median E2E Latency (ms):                 64404.77
P90 E2E Latency (ms):                    108928.34
P99 E2E Latency (ms):                    118216.62
---------------Time to First Token----------------
Mean TTFT (ms):                          50530.24
Median TTFT (ms):                        49860.22
P99 TTFT (ms):                           107822.09
-----Time per Output Token (excl. 1st token)------
Mean TPOT (ms):                          52.01
Median TPOT (ms):                        52.70
P99 TPOT (ms):                           68.16
---------------Inter-Token Latency----------------
Mean ITL (ms):                           51.18
Median ITL (ms):                         38.67
P95 ITL (ms):                            121.57
P99 ITL (ms):                            160.03
Max ITL (ms):                            1821.74
==================================================

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. 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.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates the IndexCache mechanism into DeepSeek V3.2 models within the SGLang framework. The primary goal is to enhance inference performance by intelligently skipping redundant top-k index computations in attention layers, leveraging a technique ported from THUDM. This optimization leads to notable improvements in throughput and latency, while maintaining accuracy, and is supported by new unit tests to ensure stability across different distributed processing configurations.

Highlights

  • IndexCache Enablement: Enabled IndexCache for DeepSeek V3.2 models to significantly improve inference performance.
  • Core Logic Integration: Integrated the IndexCache mechanism, originally from THUDM, into the SGLang framework, introducing logic to conditionally skip top-k index computation in attention layers and reuse previous layer's indices.
  • Performance Improvements: Demonstrated significant performance gains, including approximately 6.4% increase in throughput, 5.4% decrease in Time to First Token (TTFT), and 5.5% decrease in Time per Output Token (TPOT).
  • New Unit Tests: Added comprehensive unit tests for DeepSeek V3.2 with IndexCache, covering various configurations such as DP8, TP8, and both frequency-based and pattern-based skipping.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

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 an "IndexCache" mechanism for the DeepSeekV2 model, enabling certain attention layers to reuse topk_indices from previous layers instead of recomputing them. This behavior is controlled by skip_topk and next_skip_topk flags, configurable via index_topk_freq or index_topk_pattern in the model's configuration. The changes involve modifying attention forward methods to accept and return topk_indices, updating decoder layers to propagate these indices, and adding a new test file to validate the functionality and performance of the IndexCache across different configurations. A minor issue was noted regarding a typo in an arXiv reference year within the deepseek_v2.py file.

Comment thread python/sglang/srt/models/deepseek_v2.py
@Fridge003
Copy link
Copy Markdown
Collaborator

Thanks for supporting! Will review on it soon

@baonudesifeizhai
Copy link
Copy Markdown
Contributor

baonudesifeizhai commented Mar 25, 2026

HI could you also provide Traffic request rate: at 1 5 10 20 ? thanks!

@ChefWu551 ChefWu551 mentioned this pull request Mar 27, 2026
5 tasks
@ChefWu551
Copy link
Copy Markdown
Contributor

I adapted this codebase for NPU, and also validated the corresponding accuracy. The CEval accuracy score is 0.9198, and the E2E latency shows about 1.2x speedup. For details, please refer to PR #21502.

@Fridge003 Fridge003 self-assigned this Mar 27, 2026
@jinyouzhi
Copy link
Copy Markdown
Contributor Author

HI could you also provide Traffic request rate: at 1 5 10 20 ? thanks!

Nice advice. thanks

@Fridge003
Copy link
Copy Markdown
Collaborator

@jinyouzhi Have you tried how it works with MTP/CP? Also, how this feature affects harder tests like GPQA or AIME25?

from sglang.test.test_utils import ModelLaunchSettings
from sglang.test.tool_call_test_runner import ToolCallTestParams

register_cuda_ci(est_time=5400, suite="nightly-8-gpu-common", nightly=True)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please don't add such a huge nightly test for index_cache.
Rather, add a single per-commit test similar to test/registered/8-gpu-models/test_deepseek_v32_basic.py

Comment thread python/sglang/srt/models/deepseek_v2.py
@jinyouzhi
Copy link
Copy Markdown
Contributor Author

HI could you also provide Traffic request rate: at 1 5 10 20 ? thanks!

FYI.

Request Rate Throughput TTFT TPOT
1 - Base 794.16 139.23 19.79
1 - Freq=4 796.27 (+0.27%) 138.75 (-0.34%) 15.83 (-20.01%)
5 - Base 3047.37 8356.24 52.49
5 - Freq=4 3167.40 (+3.94%) 7105.00 (-14.97%) 50.57 (-3.66%)
10 - Base 3117.71 29061.12 53.71
10 - Freq=4 3296.38 (+5.73%) 26511.61 (-8.78%) 53.17 (-1.01%)
20 - Base 3018.98 43372.26 55.35
20 - Freq=4 3363.01 (+11.39%) 36536.14 (-15.76%) 50.63 (-8.54%)
python3 -m sglang.bench_serving \
  --backend sglang \
  --host 127.0.0.1 \
  --port 30000 \
  --model /ssd/hf_models/DeepSeek-V3.2-Exp \
  --dataset-name random \
  --num-prompts 500 \
  --random-input-len 1024 \
  --random-output-len 512 \
  --request-rate 1/5/10/20

@Fridge003
Copy link
Copy Markdown
Collaborator

@jinyouzhi Which GPU are you testing on?

@jinyouzhi
Copy link
Copy Markdown
Contributor Author

@jinyouzhi Which GPU are you testing on?

H20x8

@zRzRzRzRzRzRzR
Copy link
Copy Markdown
Contributor

This works on GLM-5-FP8, and I think it can serve as a tutorial in the SGLang CookBook for users attempting their own deployment, though we cannot guarantee zero precision loss.

@Fridge003
Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci label Apr 4, 2026
@Fridge003
Copy link
Copy Markdown
Collaborator

/rerun-test test_kimi_linear_models.py

@Fridge003
Copy link
Copy Markdown
Collaborator

/rerun-stage stage-c-test-8-gpu-h200

@Fridge003
Copy link
Copy Markdown
Collaborator

/rerun-stage stage-c-test-4-gpu-b200

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 5, 2026

✅ Triggered stage-c-test-8-gpu-h200 to run independently (skipping dependencies). View workflow run

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 5, 2026

✅ Triggered stage-c-test-4-gpu-b200 to run independently (skipping dependencies). View workflow run

@Fridge003
Copy link
Copy Markdown
Collaborator

/rerun-stage stage-c-test-deepep-8-gpu-h200

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 5, 2026

✅ Triggered stage-c-test-deepep-8-gpu-h200 to run independently (skipping dependencies). View workflow run

Fridge003 and others added 2 commits April 5, 2026 00:11
The index cache feature makes forward_core return a tuple
(hidden_states, topk_indices) for NSA models. The TBO overlapped
operations path (op_core) didn't unpack this tuple, causing
'tuple' object has no attribute 'shape' errors in DeepEP tests
with DeepSeek V3.2.

Fix:
- Unpack the tuple in op_core, discarding topk_indices (TBO path
  doesn't propagate it between layers)
- Fall back to computing topk when prev_topk_indices is None
  (i.e., in TBO mode) even if skip_topk is set, to avoid using
  None as topk indices

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Fridge003 Fridge003 merged commit 5a35316 into sgl-project:main Apr 5, 2026
240 of 278 checks passed
Fridge003 added a commit that referenced this pull request Apr 5, 2026
Co-authored-by: Baizhou Zhang <sobereddiezhang@gmail.com>
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
Co-authored-by: Baizhou Zhang <sobereddiezhang@gmail.com>
Fridge003 added a commit that referenced this pull request Apr 7, 2026
Co-authored-by: Baizhou Zhang <sobereddiezhang@gmail.com>
xiezhq-hermann pushed a commit to antgroup/sglang that referenced this pull request Apr 7, 2026
Co-authored-by: Baizhou Zhang <sobereddiezhang@gmail.com>
@dongyibo
Copy link
Copy Markdown

dongyibo commented Apr 7, 2026

Hello, I understand that the larger the index_topk_freq setting is (i.e., the more S layers there are), the greater the impact on output accuracy. Is that correct? @jinyouzhi

@jinyouzhi
Copy link
Copy Markdown
Contributor Author

Hello, I understand that the larger the index_topk_freq setting is (i.e., the more S layers there are), the greater the impact on output accuracy. Is that correct? @jinyouzhi

Yes

caijixueIT pushed a commit to caijixueIT/sglang that referenced this pull request Apr 11, 2026
Co-authored-by: Baizhou Zhang <sobereddiezhang@gmail.com>
yhyang201 pushed a commit to yhyang201/sglang that referenced this pull request Apr 22, 2026
Co-authored-by: Baizhou Zhang <sobereddiezhang@gmail.com>
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.

[Feature] Implement IndexCache for GLM-5/DeepSeek V3.2

6 participants