Skip to content

[Bugfix]Fix deepseek 3.2 C8 precision by rotary tensor - #7537

Merged
wangxiyuan merged 10 commits into
vllm-project:mainfrom
Yaphets24:main
Mar 25, 2026
Merged

wangxiyuan merged 10 commits into
vllm-project:mainfrom
Yaphets24:main

Conversation

@Yaphets24

@Yaphets24 Yaphets24 commented Mar 23, 2026 •

Copy link
Copy Markdown
Collaborator

What this PR does / why we need it?

During the attention quantization process of DeepSeek V3.2, it is necessary to retrieve the Hadamard matrix from the weights to facilitate the computation.

Does this PR introduce any user-facing change?

No. But there will be two new tensor in quant weight.

How was this patch tested?

@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 enhances the vllm_ascend framework to support DeepseekV2 3.2 models, specifically by integrating C8 rotation tensors into the attention mechanism. It refactors the handling of query and key rotation components, introduces a dedicated quantization scheme for these new rotation tensors, and updates the model loading process to correctly map and initialize them. These changes are crucial for optimizing the performance and accuracy of DeepseekV2 models on Ascend hardware.

Highlights

  • DeepseekV2 C8 Rotation Tensor Support: Introduced support for DeepseekV2 3.2 C8 rotation tensors by splitting the previously unified qk_hadamard tensor into separate q_hadamard and k_hadamard tensors within the AscendSFAImpl attention mechanism.
  • Dynamic Quantization for Indexer Rotation: Implemented a new AscendSFAQuantAttentionMethod for INT8_DYNAMIC quantization specifically for attention layers, which creates indexer.q_rot and indexer.k_rot parameters to handle the rotation tensors.
  • DeepseekV2 Weight Loading Patch: Added a new patch file (patch_deepseekv2.py) that overrides the DeepseekV2ForCausalLM.load_weights method to correctly remap and load indexer.q_rot and indexer.k_rot weights during model initialization.
  • Quantization Configuration Updates: Modified the modelslim_config.py to recognize and enable indexer_quant_type for attention layers, allowing the system to apply specific quantization methods to the indexer rotation components.

🧠 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. ↩

@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 support for a new attention mechanism feature (C8 rot tensor) for Deepseek V2 models on Ascend hardware. The changes involve splitting the Hadamard tensor for Q and K in sparse attention, adding a patch to load new indexer rotation tensor weights, and updating quantization configurations to support this. My review has identified a critical issue in the new weight loading patch that would prevent most of the model's weights from being loaded due to a hardcoded layer limit. I've also pointed out a performance improvement opportunity in the attention implementation to avoid redundant tensor creation. Addressing these points will ensure the feature works correctly and efficiently.

Comment on lines +77 to +80
import re
layer_idx = re.findall(r"\d+", name)
if len(layer_idx) > 0 and int(layer_idx[0]) >= NUM_HIDDEN_LAYERS:
continue

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.

critical

This block of code contains two issues:

  1. Critical Bug: It uses a hardcoded NUM_HIDDEN_LAYERS = 4 (defined on line 16) to skip loading weights for layers with an index of 4 or greater. This will prevent loading most of the model's layers, as models like DeepseekV2 have far more than 4 layers. This logic should be removed to ensure the entire model is loaded.
  2. Bad Practice: The import re statement is inside a loop. Imports should be at the top of the file for performance and style. Since the logic using re should be removed, this import becomes unnecessary.

Comment on lines +529 to 536
if self.use_sparse_c8_indexer and AscendSFAImpl.q_hadamard is None:
AscendSFAImpl.q_hadamard = torch.tensor(scipy.linalg.hadamard(128), dtype=torch.bfloat16, device="npu") / (
128**0.5
)
if self.use_sparse_c8_indexer and AscendSFAImpl.k_hadamard is None:
AscendSFAImpl.k_hadamard = torch.tensor(scipy.linalg.hadamard(128), dtype=torch.bfloat16, device="npu") / (
128**0.5
)

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.

high

The Hadamard matrix is being created twice, which is inefficient. Since q_hadamard and k_hadamard are initialized with the same value, you can create the tensor once and assign it to both, avoiding redundant computation and memory allocation.

        if self.use_sparse_c8_indexer and (AscendSFAImpl.q_hadamard is None or AscendSFAImpl.k_hadamard is None):
            hadamard_matrix = torch.tensor(scipy.linalg.hadamard(128), dtype=torch.bfloat16, device="npu") / (128**0.5)
            if AscendSFAImpl.q_hadamard is None:
                AscendSFAImpl.q_hadamard = hadamard_matrix
            if AscendSFAImpl.k_hadamard is None:
                AscendSFAImpl.k_hadamard = hadamard_matrix

Signed-off-by: mayumeng <m30059191@china.huawei.com>
Signed-off-by: mayumeng <m30059191@china.huawei.com>
@github-actions

Copy link
Copy Markdown
Contributor

👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:‌‌

  • A PR should do only one thing, smaller PRs enable faster reviews.
  • Every PR should include unit tests and end-to-end tests ‌to ensure it works and is not broken by other future PRs.
  • Write the commit message by fulfilling the PR description to help reviewer and future developers understand.

If CI fails, you can run linting and testing checks locally according Contributing and Testing.

mayumeng added 6 commits March 23, 2026 12:04
Signed-off-by: mayumeng <m30059191@china.huawei.com>
Signed-off-by: mayumeng <m30059191@china.huawei.com>
Signed-off-by: mayumeng <m30059191@china.huawei.com>
Signed-off-by: mayumeng <m30059191@china.huawei.com>
Signed-off-by: mayumeng <m30059191@china.huawei.com>
Signed-off-by: mayumeng <m30059191@china.huawei.com>
@Yaphets24 Yaphets24 changed the title add deepseek 3.2 C8 rot tensor [Bugfix]Fix deepseek 3.2 C8 precision by rotary tensor Mar 23, 2026
@SidaoY
SidaoY requested a review from MengqingCao as a code owner March 23, 2026 07:00

@rjg-lyh rjg-lyh 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.

This PR replaces the default matrix values with more precise weights loaded during model initialization, enhancing overall network performance in the C8 scenario. Thanks for this contribution.

mayumeng added 2 commits March 23, 2026 17:52
Signed-off-by: mayumeng <m30059191@china.huawei.com>
Signed-off-by: mayumeng <m30059191@china.huawei.com>

@MengqingCao MengqingCao left a comment

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.

LGTM

@wangxiyuan
wangxiyuan merged commit 8977be1 into vllm-project:main Mar 25, 2026
38 checks passed
starmountain1997 pushed a commit to starmountain1997/vllm-ascend that referenced this pull request Mar 25, 2026
…7537)

### What this PR does / why we need it?
During the attention quantization process of DeepSeek V3.2, it is
necessary to retrieve the Hadamard matrix from the weights to facilitate
the computation.

### Does this PR introduce _any_ user-facing change?
No. But there will be two new tensor in quant weight.

### How was this patch tested?

- vLLM version: v0.18.0
- vLLM main:
vllm-project/vllm@8b63257

---------

Signed-off-by: mayumeng <m30059191@china.huawei.com>
Co-authored-by: mayumeng <m30059191@china.huawei.com>
845473182 pushed a commit to 845473182/vllm-ascend that referenced this pull request Mar 25, 2026
…to qwen3next_graph

* 'main' of https://github.com/vllm-project/vllm-ascend: (94 commits)
  [bugfix] Fixed the error issue when overlaying MTP and full decode on DSV3.1 C8. (vllm-project#7571)
  [eagle3][pcp] fix acceptance rate for eagle3 and pcp enabled (vllm-project#7549)
  [bugfix][CI] fix '_OpNamespace' 'vllm' object has no attribute 'qkv_rmsnorm_rope' (vllm-project#7620)
  [Nightly] Nightly pre-build image (vllm-project#7388)
  [Bugfix]Fix deepseek 3.2 C8  precision by rotary tensor (vllm-project#7537)
  adapt to main2main for model runner v2 (vllm-project#7578)
  [Patch] Fix balance scheduling (vllm-project#7611)
  [310P]fused recurrent gated delta rule pytorch core and ut (vllm-project#7398)
  [CI] refine issue triage rules, wan regex and update stale setting (vllm-project#7531)
  [Lint]Add lint hooks for clang-format, shellcheck, forbidden imports, and boolean context manager checks (vllm-project#7511)
  [doc] add enable_sparse_c8 option in configuration options (vllm-project#7600)
  lower log level in PD Disaggregation (vllm-project#7589)
  [model_runner_v2]:optimize the performance of the _compute_slot_mappings_kernel (vllm-project#7575)
  [Feat][SP] Suport SP for VL MoE models (vllm-project#7044)
  Fix  Qwen3Next CI Config (vllm-project#7561)
  [Feat] Add npugraph_ex enablement logging (vllm-project#7574)
  [UT] Align input arguments with Ascend(Yarn)RotaryEmbedding with vLLM and add ut (vllm-project#7358)
  [P/D] Check wildcard  address for layerwise connector (vllm-project#7389)
  [P/D] [Bugfix] fix mooncake layerconnector dead when update_decoder_info fail (vllm-project#7514)
  [BugFix][P/D] fix padding error on FullGraph mode && fix layerwise connector mamba accuracy (vllm-project#7506)
  ...
lihaokun-2026 pushed a commit to lihaokun-2026/vllm-ascend that referenced this pull request Mar 29, 2026
…7537)

### What this PR does / why we need it?
During the attention quantization process of DeepSeek V3.2, it is
necessary to retrieve the Hadamard matrix from the weights to facilitate
the computation.

### Does this PR introduce _any_ user-facing change?
No. But there will be two new tensor in quant weight.

### How was this patch tested?

- vLLM version: v0.18.0
- vLLM main:
vllm-project/vllm@8b63257

---------

Signed-off-by: mayumeng <m30059191@china.huawei.com>
Co-authored-by: mayumeng <m30059191@china.huawei.com>
chenchuw886 pushed a commit to chenchuw886/vllm-ascend that referenced this pull request Apr 1, 2026
…7537)

### What this PR does / why we need it?
During the attention quantization process of DeepSeek V3.2, it is
necessary to retrieve the Hadamard matrix from the weights to facilitate
the computation.

### Does this PR introduce _any_ user-facing change?
No. But there will be two new tensor in quant weight.

### How was this patch tested?

- vLLM version: v0.18.0
- vLLM main:
vllm-project/vllm@8b63257

---------

Signed-off-by: mayumeng <m30059191@china.huawei.com>
Co-authored-by: mayumeng <m30059191@china.huawei.com>
zouyida2052 pushed a commit to zouyida2052/vllm-ascend that referenced this pull request Apr 28, 2026
…7537)

### What this PR does / why we need it?
During the attention quantization process of DeepSeek V3.2, it is
necessary to retrieve the Hadamard matrix from the weights to facilitate
the computation.

### Does this PR introduce _any_ user-facing change?
No. But there will be two new tensor in quant weight.

### How was this patch tested?

- vLLM version: v0.18.0
- vLLM main:
vllm-project/vllm@8b63257

---------

Signed-off-by: mayumeng <m30059191@china.huawei.com>
Co-authored-by: mayumeng <m30059191@china.huawei.com>
Signed-off-by: zouyida2052 <zouyida2002@gmail.com>
yangzhe-2026 pushed a commit to yangzhe-2026/vllm-ascend that referenced this pull request May 6, 2026
…7537)

### What this PR does / why we need it?
During the attention quantization process of DeepSeek V3.2, it is
necessary to retrieve the Hadamard matrix from the weights to facilitate
the computation.

### Does this PR introduce _any_ user-facing change?
No. But there will be two new tensor in quant weight.

### How was this patch tested?

- vLLM version: v0.18.0
- vLLM main:
vllm-project/vllm@8b63257

---------

Signed-off-by: mayumeng <m30059191@china.huawei.com>
Co-authored-by: mayumeng <m30059191@china.huawei.com>
nanxingMy pushed a commit to nanxingMy/vllm-ascend that referenced this pull request May 15, 2026
…7537)

### What this PR does / why we need it?
During the attention quantization process of DeepSeek V3.2, it is
necessary to retrieve the Hadamard matrix from the weights to facilitate
the computation.

### Does this PR introduce _any_ user-facing change?
No. But there will be two new tensor in quant weight.

### How was this patch tested?

- vLLM version: v0.18.0
- vLLM main:
vllm-project/vllm@8b63257

---------

Signed-off-by: mayumeng <m30059191@china.huawei.com>
Co-authored-by: mayumeng <m30059191@china.huawei.com>
Signed-off-by: nanxing <1014662416@qq.com>
ader47 pushed a commit to ader47/vllm-ascend that referenced this pull request Jun 18, 2026
…7537)

### What this PR does / why we need it?
During the attention quantization process of DeepSeek V3.2, it is
necessary to retrieve the Hadamard matrix from the weights to facilitate
the computation.

### Does this PR introduce _any_ user-facing change?
No. But there will be two new tensor in quant weight.

### How was this patch tested?

- vLLM version: v0.18.0
- vLLM main:
vllm-project/vllm@8b63257

---------

Signed-off-by: mayumeng <m30059191@china.huawei.com>
Co-authored-by: mayumeng <m30059191@china.huawei.com>
CXY-Katrina pushed a commit to CXY-Katrina/vllm-ascend that referenced this pull request Jun 27, 2026
…7537)

### What this PR does / why we need it?
During the attention quantization process of DeepSeek V3.2, it is
necessary to retrieve the Hadamard matrix from the weights to facilitate
the computation.

### Does this PR introduce _any_ user-facing change?
No. But there will be two new tensor in quant weight.

### How was this patch tested?

- vLLM version: v0.18.0
- vLLM main:
vllm-project/vllm@8b63257

---------

Signed-off-by: mayumeng <m30059191@china.huawei.com>
Co-authored-by: mayumeng <m30059191@china.huawei.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.

5 participants