Skip to content

Conversation

@maxyanghu
Copy link

@maxyanghu maxyanghu commented Jun 10, 2025

Added in #16457, MRotaryEmedding currently uses flash_attn.ops.triton.rotary.apply_rotary when 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:

  1. Added forward_native and forward_cuda functions for MRotaryEmedding so that by default it will use native pytorch implementation
  2. Also added forward_native and forward_cuda functions for DualChunkRotaryEmbedding/Llama4VisionRotaryEmbedding/DeepseekScalingRotaryEmbedding and remove their forward functions to follow the custom op design
  3. Created a RotaryEmbeddingBase class to achieve the correct MRO chain

@github-actions
Copy link

👋 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 fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

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 ready label to the PR or enable auto-merge.

🚀

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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, and DualChunkRotaryEmbedding classes to conform to the custom-op design.
  • Native vs. CUDA Dispatch: Introduces forward_native and forward_cuda methods 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, and Llama4VisionRotaryEmbedding to inherit from CustomOp before RotaryEmbedding to ensure correct dispatch logic.
  • Conditional Kernel Usage: Modifies the internal _apply_rotary_emb helper function to conditionally use the flash_attn Triton kernel based on an enable_custom_op flag.

Changelog

  • vllm/model_executor/layers/rotary_embedding.py
    • Modified _apply_rotary_emb helper function to accept enable_custom_op flag and make the CUDA kernel call conditional.
    • Renamed the main forward method to _forward in MRotaryEmbedding and added an enable_custom_op parameter.
    • Added forward_native and forward_cuda methods to MRotaryEmbedding to call _forward with appropriate enable_custom_op values.
    • Renamed the main forward method to forward_native in DeepseekScalingRotaryEmbedding, Llama4VisionRotaryEmbedding, and DualChunkRotaryEmbedding.
    • Added forward_cuda methods to DeepseekScalingRotaryEmbedding, Llama4VisionRotaryEmbedding, and DualChunkRotaryEmbedding (currently aliasing forward_native).
    • Changed the inheritance order for DeepseekScalingRotaryEmbedding, Llama4VisionRotaryEmbedding, and MRotaryEmbedding to CustomOp, RotaryEmbedding.
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

  1. 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 _forward method in MRotaryEmbedding has a typo: enalbe_custom_op should be enable_custom_op. This is a low-severity issue and a comment was not added due to review settings.
  • Docstrings for forward_cuda methods: For classes DeepseekScalingRotaryEmbedding, Llama4VisionRotaryEmbedding, and DualChunkRotaryEmbedding, their new forward_cuda methods currently call self.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 these forward_cuda methods 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.

@maxyanghu maxyanghu marked this pull request as draft June 10, 2025 00:35
@maxyanghu maxyanghu marked this pull request as ready for review June 10, 2025 01:35
@maxyanghu maxyanghu marked this pull request as draft June 10, 2025 13:38
@mergify
Copy link

mergify bot commented Sep 8, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @maxyanghu.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant