[NPU Feature] Qwen3.5 NPU FLA and fused-operator patches#204
Merged
Conversation
- Add kernelize_model integration to ep_fsdp2_lora and fsdp2 examples - Support model parameter in apply_npu_patch for FLA instance patching - Implement NPU-accelerated packed MoE experts with weight caching - Add Qwen3.5 SparseMoeBlock forward with dual Transformers version support - Support partial RoPE and gated RMSNorm with FP32 mode option - Add MindSpeed Triton FLA backend integration for Qwen3.5 - Add environment variable controls for patch toggles - Add dynamic model discovery for unknown model families
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces Ascend NPU optimizations and monkey patches for Qwen3.5 and Qwen3.5-MoE models, including Flash Linear Attention (FLA) via MindSpeed Triton kernels, packed MoE experts, and dynamic model discovery. The review feedback highlights two critical issues in the patching logic: caching expert weights when gradients are required can break the PyTorch autograd graph, and calling model.named_modules() directly on a TransformersModel wrapper will raise an AttributeError during training.
- Skip weight cache when requires_grad=True to preserve autograd graph - Resolve underlying PyTorch model from TransformersModel wrapper in FLA patch
tastelikefeet
approved these changes
May 26, 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)