-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Refactor rotary embedding kernels to conform to custom-op design #19388
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
Conversation
|
👋 Hi! Thank you for contributing to the vLLM project. 💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels. Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging. To run CI, PR reviewers can either: Add 🚀 |
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.
Hello @maxyanghu, 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!
Summary of Changes
Hello! Gemini here, providing a summary of this pull request to help everyone get oriented. This PR focuses on refactoring several Rotary Embedding (RoPE) implementations within vLLM to align them with the custom operator (custom-op) design pattern. The primary motivation is to ensure that custom CUDA kernels (like the Triton one from flash_attn) are not used by default, but rather dispatched correctly when the custom-op feature is enabled. By default, the native PyTorch implementation should be used, allowing for potential future compiler optimizations.
Highlights
- Custom Op Refactoring: The core change refactors the
MRotaryEmbedding,DeepseekScalingRotaryEmbedding,Llama4VisionRotaryEmbedding, andDualChunkRotaryEmbeddingclasses to conform to the custom-op design. - Native vs. CUDA Dispatch: Introduces
forward_nativeandforward_cudamethods in the affected RoPE classes to explicitly separate the PyTorch implementation from the potential custom CUDA kernel implementation. - Inheritance Order: Changes the inheritance order for
MRotaryEmbedding,DeepseekScalingRotaryEmbedding, andLlama4VisionRotaryEmbeddingto inherit fromCustomOpbeforeRotaryEmbeddingto ensure correct dispatch logic. - Conditional Kernel Usage: Modifies the internal
_apply_rotary_embhelper function to conditionally use theflash_attnTriton kernel based on anenable_custom_opflag.
Changelog
- vllm/model_executor/layers/rotary_embedding.py
- Modified
_apply_rotary_embhelper function to acceptenable_custom_opflag and make the CUDA kernel call conditional. - Renamed the main
forwardmethod to_forwardinMRotaryEmbeddingand added anenable_custom_opparameter. - Added
forward_nativeandforward_cudamethods toMRotaryEmbeddingto call_forwardwith appropriateenable_custom_opvalues. - Renamed the main
forwardmethod toforward_nativeinDeepseekScalingRotaryEmbedding,Llama4VisionRotaryEmbedding, andDualChunkRotaryEmbedding. - Added
forward_cudamethods toDeepseekScalingRotaryEmbedding,Llama4VisionRotaryEmbedding, andDualChunkRotaryEmbedding(currently aliasingforward_native). - Changed the inheritance order for
DeepseekScalingRotaryEmbedding,Llama4VisionRotaryEmbedding, andMRotaryEmbeddingtoCustomOp, RotaryEmbedding.
- Modified
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 is currently in preview and 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 to provide feedback.
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.
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 effectively refactors the rotary embedding kernels to align with the CustomOp design. The changes introduce forward_native and forward_cuda methods, ensuring that MRotaryEmbedding defaults to a native PyTorch implementation and correctly respects custom operator enablement. Other rotary embedding classes are also updated to fit this pattern, enhancing consistency and maintainability. The use of CustomOp as the first parent class correctly ensures the desired dispatch logic. Overall, the changes are well-structured and achieve the stated goals of improving design clarity and enabling better control over custom op usage for future optimizations.
Summary of Findings
- Minor Typo in Docstring: In
vllm/model_executor/layers/rotary_embedding.py, line 1042 (RIGHT), the docstring for the_forwardmethod inMRotaryEmbeddinghas a typo:enalbe_custom_opshould beenable_custom_op. This is a low-severity issue and a comment was not added due to review settings. - Docstrings for
forward_cudamethods: For classesDeepseekScalingRotaryEmbedding,Llama4VisionRotaryEmbedding, andDualChunkRotaryEmbedding, their newforward_cudamethods currently callself.forward_native. While this is a valid implementation choice (especially if no specific CUDA kernels are planned for these variants under the custom op path), adding a brief docstring to theseforward_cudamethods explaining this behavior (e.g., "Currently uses the native PyTorch implementation for CUDA operations.") would improve clarity for future maintainers. This is a low-severity suggestion and a comment was not added due to review settings.
Merge Readiness
The pull request appears to be in good shape and addresses the outlined objectives effectively. The refactoring aligns the rotary embedding kernels with the custom-op design, which is a positive step for maintainability and future flexibility. No critical or high-severity issues were found. Based on this review, the PR seems ready for merging, pending any other reviews or CI checks. As an AI, I am not authorized to approve pull requests.
|
This pull request has merge conflicts that must be resolved before it can be |
Added in #16457,
MRotaryEmeddingcurrently usesflash_attn.ops.triton.rotary.apply_rotarywhen the platform is CUDA no matter if it's been enabled as a custom op or not. But by design custom op shouldn't be turned on by default. This could potentially undermine future compiler optimization oppurtunities.This PR:
forward_nativeandforward_cudafunctions forMRotaryEmeddingso that by default it will use native pytorch implementationforward_nativeandforward_cudafunctions forDualChunkRotaryEmbedding/Llama4VisionRotaryEmbedding/DeepseekScalingRotaryEmbeddingand remove theirforwardfunctions to follow the custom op designRotaryEmbeddingBaseclass to achieve the correct MRO chain