Skip to content

feat: add NPU SAS+LI attention patch for DeepSeek-V4#250

Open
0hujun wants to merge 9 commits into
modelscope:mainfrom
0hujun:main
Open

feat: add NPU SAS+LI attention patch for DeepSeek-V4#250
0hujun wants to merge 9 commits into
modelscope:mainfrom
0hujun:main

Conversation

@0hujun

@0hujun 0hujun commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

What is changed

为 DeepSeek-V4 的注意力层添加 NPU 融合算子 patch:

  1. SAS(SparseAttnSharedKV):将原始稀疏注意力替换为 mindspeed.ops.npu_sparse_attn_shared_kv.SparseAttnSharedKV 融合算子;
  2. LI(LightningIndexer):将原始 torch 实现 top-k 索引替换为 mindspeed.ops.npu_lightning_indexer.npu_lightning_indexer 融合算子。

两个 patch 均为 NPU-only,非 NPU 环境保持默认行为不变。

Motivation

DeepSeek-V4 的混合注意力结构中,稀疏注意力计算和 lightning indexer 索引计算在 NPU 上使用通用 PyTorch 算子串行执行,存在 kernel launch 开销大、显存占用高、耗时波动剧烈等问题。

Performance Results

速度提升(6 层对比)

配置 稳态每 step(s) 每层耗时(s) 提升 稳定性
不开 patch 8.56 1.427 1.0x 5.1~13.3s(极不稳定)
开 SAS+LI 3.60 0.600 2.38x 3.4~3.9s(稳定)

LI 增量收益(12 层对比)

配置 稳态每 step(s) 每层耗时(s) 相对 SAS-only
只开 SAS 7.80 0.650 1.0x
开 SAS+LI 7.34 0.612 1.06x(快 5.9%)

显存与可训练层数

配置 8 卡最大层数 显存峰值 2 机训练
不开 patch 6 层(8 层 OOM) 64GB/卡 不支持
开 SAS+LI ≥12 层 显著降低 支持

Summary

  • SAS+LI patch 使每层训练耗时降低 58%(1.427→0.600s/层),每 step 提速 2.38 倍;
  • 突破显存瓶颈,8 卡可训练层数从 6 层提升至 12+ 层,支持 2 机扩展;
  • LI 在 SAS 基础上额外提供约 6% 增量加速,并显著减少耗时毛刺。

@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 DeepSeek-V4 NPU sparse attention (SAS), Lightning Indexer (LI), and CSA Compressor forwards with Ascend NPU compatibility fixes. The feedback highlights several critical issues and improvement opportunities, including a missing import of _is_env_enabled that would cause a runtime NameError, a redundant import of npu_sdpa_attention_forward, a potential RuntimeError due to an uncasted index tensor in scatter_, a redundant scorer call when compressed_len is zero, and a missing type cast for position_bias in the CSA compressor forward.

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.

Comment thread src/twinkle/kernel/builtin.py Outdated
Comment on lines +263 to +264
if not _is_env_enabled('TWINKLE_NPU_DSV4_SAS', default=False):
return

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 function _is_env_enabled is called here but is neither imported nor defined in this file. This will raise a NameError at runtime when _add_deepseek_v4_entries is executed. Please import _is_env_enabled from its defining module (e.g., twinkle.utils.env).

Suggested change
if not _is_env_enabled('TWINKLE_NPU_DSV4_SAS', default=False):
return
from twinkle.utils.env import _is_env_enabled
if not _is_env_enabled('TWINKLE_NPU_DSV4_SAS', default=False):

Comment thread src/twinkle/kernel/npu_impls/attention.py Outdated
Comment thread src/twinkle/kernel/builtin.py Outdated
Comment thread src/twinkle/kernel/npu_impls/attention.py Outdated
Comment thread src/twinkle/kernel/npu_impls/attention.py Outdated
0hujun and others added 6 commits July 21, 2026 14:18
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant