[Performance] Optimize Mllama LayerNorm -> Upd#9725
[Performance] Optimize Mllama LayerNorm -> Upd#9725Kangyan-Zhou merged 7 commits intosgl-project:mainfrom
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @vincentzed, 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 refactors the layer normalization implementation within the Mllama model by replacing a custom RMSNorm class with an existing, shared RMSNorm from the project's collection. The primary goal is code consolidation and improved maintainability, without introducing significant changes to performance or correctness. The changes involve removing the redundant custom class and updating the MllamaTextCrossAttention module to use the standardized RMSNorm, along with necessary adjustments to tensor handling.
Highlights
- Removed Custom Layer Normalization Class: The custom MllamaTextRMSNorm class, which provided a specific implementation of RMS normalization, has been removed from the codebase.
- Adopted Shared RMSNorm Implementation: The MllamaTextCrossAttention module now utilizes an existing, shared RMSNorm implementation instead of the previously custom-defined MllamaTextRMSNorm for its query (q_norm) and key (k_norm) normalization layers.
- Adjusted Tensor Operations for New Norm: The forward pass logic within MllamaTextCrossAttention was updated to correctly handle tensor reshaping before and after applying the new RMSNorm, ensuring compatibility and proper operation.
Using Gemini Code Assist
The 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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.
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
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request is a good refactoring that replaces the model-specific MllamaTextRMSNorm with the shared RMSNorm implementation from the common layers. This improves code reuse and maintainability. The necessary adjustments in MllamaTextCrossAttention to handle the generic RMSNorm layer appear correct. The benchmark results confirm that this change has a negligible impact on performance, which is great.
python/sglang/srt/models/mllama.py
Outdated
| k = self.k_norm(k.reshape(-1, self.head_dim)).reshape( | ||
| -1, self.num_local_key_value_heads, self.head_dim | ||
| ) | ||
| q = q.reshape(-1, self.num_local_heads, self.head_dim) |
There was a problem hiding this comment.
reshape will cause additional memory copy and kernel launch (can be seen in the torch profile). could you check if it's necessary?
There was a problem hiding this comment.
I will do the check.
There was a problem hiding this comment.
I updated, I removed two reshapes to be views, the norm ones still need it (both).
9995662 to
9317eff
Compare
9c0c814 to
9b39a67
Compare
|
It is ready |
Motivation
Modifications
This doesn't change much in performance, and doesn't affect correctness, but we get to
RMSNormfrom our own layernorm collection.Accuracy Tests
Benchmarking and Profiling
On
mainOn new branch
Checklist