[model][NPU]:Add NPU fusion operator patch to Zimage model to improve performance#1256
[model][NPU]:Add NPU fusion operator patch to Zimage model to improve performance#1256Feng0w0 wants to merge 1 commit intomodelscope:mainfrom
Conversation
Summary of ChangesHello @Feng0w0, 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 significantly enhances the performance of the Zimage model, particularly on NPU hardware, by introducing and integrating specialized fused operators for RMSNorm and Rotary Embeddings. The changes provide a configurable system to dynamically apply these NPU-optimized operations, aiming to accelerate model execution and training efficiency. This allows for better utilization of NPU capabilities without altering the core model architecture. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
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.
Code Review
This pull request introduces NPU-specific fused operators for RMSNorm and RoPE to improve performance for the Zimage model. The changes include adding new NPU operator implementations, refactoring the attention module to allow monkey-patching, and adding logic to apply these patches conditionally. The implementation looks good, but I have a few suggestions to improve robustness and consistency.
| except: | ||
| pass |
| from ..models.general_modules import RMSNorm | ||
| from transformers.models.qwen3.modeling_qwen3 import Qwen3RMSNorm | ||
| from ..models.z_image_dit import Attention | ||
| from ..core.npu_patch.npu_fused_operator import ( | ||
| rms_norm_forward_npu, | ||
| rms_norm_forward_transformers_npu, | ||
| rotary_emb_Zimage_npu | ||
| ) | ||
|
|
||
| RMSNorm.forward = rms_norm_forward_npu | ||
| Qwen3RMSNorm.forward = rms_norm_forward_transformers_npu | ||
| Attention.apply_rotary_emb = rotary_emb_Zimage_npu |
There was a problem hiding this comment.
The import from transformers on line 678 can fail if the library isn't installed or the module path changes. To make this patching more robust, it's best to wrap the transformers-related import and monkey-patch in a try...except ImportError block. This prevents a missing optional dependency from crashing the application, while still allowing other patches to be applied.
| from ..models.general_modules import RMSNorm | |
| from transformers.models.qwen3.modeling_qwen3 import Qwen3RMSNorm | |
| from ..models.z_image_dit import Attention | |
| from ..core.npu_patch.npu_fused_operator import ( | |
| rms_norm_forward_npu, | |
| rms_norm_forward_transformers_npu, | |
| rotary_emb_Zimage_npu | |
| ) | |
| RMSNorm.forward = rms_norm_forward_npu | |
| Qwen3RMSNorm.forward = rms_norm_forward_transformers_npu | |
| Attention.apply_rotary_emb = rotary_emb_Zimage_npu | |
| from ..models.general_modules import RMSNorm | |
| from ..models.z_image_dit import Attention | |
| from ..core.npu_patch.npu_fused_operator import ( | |
| rms_norm_forward_npu, | |
| rms_norm_forward_transformers_npu, | |
| rotary_emb_Zimage_npu | |
| ) | |
| RMSNorm.forward = rms_norm_forward_npu | |
| Attention.apply_rotary_emb = rotary_emb_Zimage_npu | |
| try: | |
| from transformers.models.qwen3.modeling_qwen3 import Qwen3RMSNorm | |
| Qwen3RMSNorm.forward = rms_norm_forward_transformers_npu | |
| except ImportError: | |
| pass # Silently ignore if transformers is not installed |
| offload_models=None, | ||
| device="cpu", | ||
| task="sft", | ||
| enable_npu_patch=True, |
There was a problem hiding this comment.
The default value for enable_npu_patch is True here, but the corresponding command-line argument in z_image_parser defaults to False. To maintain consistency between programmatic use and CLI use, it's better to have the same default value. I suggest changing this to False.
| enable_npu_patch=True, | |
| enable_npu_patch=False, |
Replace RMSNorm and Rope in ZImage