feat: add NPU SAS+LI attention patch for DeepSeek-V4#250
Conversation
There was a problem hiding this comment.
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.
| if not _is_env_enabled('TWINKLE_NPU_DSV4_SAS', default=False): | ||
| return |
There was a problem hiding this comment.
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).
| 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): |
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>
What is changed
为 DeepSeek-V4 的注意力层添加 NPU 融合算子 patch:
mindspeed.ops.npu_sparse_attn_shared_kv.SparseAttnSharedKV融合算子;mindspeed.ops.npu_lightning_indexer.npu_lightning_indexer融合算子。两个 patch 均为 NPU-only,非 NPU 环境保持默认行为不变。
Motivation
DeepSeek-V4 的混合注意力结构中,稀疏注意力计算和 lightning indexer 索引计算在 NPU 上使用通用 PyTorch 算子串行执行,存在 kernel launch 开销大、显存占用高、耗时波动剧烈等问题。
Performance Results
速度提升(6 层对比)
LI 增量收益(12 层对比)
显存与可训练层数
Summary