-
Notifications
You must be signed in to change notification settings - Fork 993
Add npu fused operators supported in modeling_qwen2 #6610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @tongtong0613, 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 integrates NPU-optimized fused operators into the Qwen2 model architecture within the Highlights
Using Gemini Code AssistThe 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
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 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
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 adds support for NPU fused operators for Qwen2 models, which should improve training and inference performance on Ascend NPUs. The changes introduce a new npu_patcher.py file that monkey-patches Qwen2RMSNorm, apply_rotary_pos_emb, and Qwen2MLP.forward with NPU-optimized implementations from torch_npu. The patch is conditionally applied only when an NPU is detected, which is good practice. The implementation is clear, but I have a couple of suggestions to improve maintainability and to note a potential future performance optimization.
| # Copyright (c) Alibaba, Inc. and its affiliates. | ||
| import torch | ||
| import torch_npu | ||
| from torch import nn | ||
| from transformers.models.qwen2 import modeling_qwen2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new file introduces several monkey-patches for NPU performance. It would be beneficial to add a module-level docstring explaining the purpose of this file (i.e., to replace certain transformers operations with NPU-accelerated versions from torch_npu for better performance on Ascend hardware). Also, adding brief comments to each function explaining what it replaces and why would improve long-term maintainability.
| def npu_swiglu_forward(self, hidden_state): | ||
| return self.down_proj( | ||
| torch_npu.npu_swiglu(torch.cat((self.gate_proj(hidden_state), self.up_proj(hidden_state)), dim=-1), dim=-1) | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of torch.cat here to combine the outputs of gate_proj and up_proj before passing them to the fused npu_swiglu kernel might introduce some performance overhead due to memory allocation and an extra kernel launch. While this is a valid approach for monkey-patching, for optimal performance, it would be better to avoid this runtime concatenation. A more advanced optimization could involve modifying the Qwen2MLP layer to use a single, larger linear layer for both projections, thus producing a pre-concatenated output. This is a more invasive change but is worth considering for future performance tuning if this part of the model proves to be a bottleneck.
Add npu fused operators supported in modeling_qwen2, include RMSNorm, SwiGLU and RoPE
PR type
PR information
This PR adds support for NPU fused operators in ms-swift, enhance the training performance on ascend npu.
This PR now adapt flowing fused operators for Qwen2: RMSNorm, SwiGLU and RoPE.In future, we can add more supported models and fused operators.
Experiment results
Paste your experiment result here(if needed).