[NPU Feature] Qwen3.5 NPU FLA and fused-operator patches#203
Closed
ys2025-AI wants to merge 0 commit into
Closed
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request significantly enhances NPU optimization support for Transformers, specifically targeting Qwen3.5 and MoE architectures. Key changes include the introduction of a Flash Linear Attention (FLA) implementation using MindSpeed Triton kernels, support for MoE Packed Experts and Sparse Blocks, and dynamic model discovery for automatic patching. Feedback focused on performance optimizations, including removing dead code in the FLA implementation, replacing torch.histc with torch.bincount for expert counting, caching normalized expert weights to avoid redundant operations in the forward pass, and moving environment variable lookups out of hot paths.
tastelikefeet
approved these changes
May 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR type
PR information
Description
This PR ports the Qwen3.5 FLA and Ascend NPU fused-operator patches.
Motivation
Qwen3.5 introduces hybrid attention layers (
linear_attention+full_attention). Thelinear_attentionpath relies onchunk_gated_delta_rulefrom theflash-linear-attention(FLA) library, which contains CUDA-only Triton kernels. On Ascend NPU, these kernels must be redirected to MindSpeed Triton implementations to achieve comparable performance.Without this patch, Qwen3.5 falls back to the pure PyTorch
torch_chunk_gated_delta_rule, resulting in ~33% slower training on NPU.Main changes
twinkle/kernel/chunk_gated_delta_rule.pychunk_gated_delta_rule. Re-exports the public API with identical signature to the FLA library.twinkle/kernel/monkey_patch_npu.py_patch_qwen3_5_fla()which: (1) spoofstransformers.utils.is_flash_linear_attention_availableto bypass CUDA-only checks; (2) replaces module-levelchunk_gated_delta_rulewith the MindSpeed implementation; (3) traverses instantiated model layers to re-bind per-instancechunk_gated_delta_rule(required because Qwen3.5 caches the function reference at__init__time).Environment Variables
All FLA behavior is gated under the existing
TWINKLE_NPU_PATCHhierarchy:Related: modelscope/ms-swift#9223
Experiment results
Qwen3.5-35B-A3B(40 layers, 30×linear_attention)