Skip to content

Conversation

@triple-Mu
Copy link
Contributor

Motivation

support qk rotary_embedding in one triton kernel.
Profiler:

before:
image

after:
image

result comparison:

before:

A_cat_and_a_dog_baking_a_cake_together_in_a_kitchen._The_cat_is_carefully_measuring_flour_while_the_20251219-220049_47059d65.mp4

after:

A_cat_and_a_dog_baking_a_cake_together_in_a_kitchen._The_cat_is_carefully_measuring_flour_while_the_20251219-224628_47059d65.mp4

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

@github-actions github-actions bot added the diffusion SGLang Diffusion label Dec 19, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @triple-Mu, 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 efficiency of Rotary Positional Embeddings (RoPE) within the system. By developing a specialized Triton kernel that processes both query and key tensors concurrently, the change minimizes computational overhead and accelerates the application of positional encodings across various diffusion models. This optimization directly contributes to faster model inference and improved overall performance.

Highlights

  • Optimized Rotary Embedding Kernel: Introduced a new Triton kernel, _qk_rotary_embedding_kernel, and its Python wrapper apply_qk_rotary_embedding, designed to apply Rotary Positional Embeddings (RoPE) to both query (q) and key (k) tensors simultaneously within a single kernel launch. This replaces separate operations for q and k.
  • Refactored RoPE Application: Updated the rotary_embedding.py module with a new helper function _apply_qk_rotary_emb that leverages the combined Triton kernel. This function now handles both Neox-style and standard RoPE application for query and key pairs.
  • Integration Across Models: Modified several diffusion models (flux.py, flux_2.py, hunyuanvideo.py, qwen_image.py, wanvideo.py, zimage.py) to utilize the new combined QK rotary embedding functions, streamlining their attention mechanisms and improving performance.
  • Performance Improvement: The consolidation of qk rotary embedding into a single kernel reduces overhead and improves execution speed, as indicated by the provided profiler results showing a decrease in processing time.

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

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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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

  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 introduces a new Triton kernel to apply rotary embeddings to query and key tensors simultaneously, which is a great optimization to reduce kernel launch overhead. The changes are well-contained and applied across several models to leverage this new fused kernel. I have identified a couple of issues: a redundant conditional check in _apply_qk_rotary_emb that includes unreachable code, and an unused interleaved parameter in the new Triton kernel _qk_rotary_embedding_kernel which could lead to incorrect behavior. Addressing these points will improve code correctness and clarity.

Comment on lines +112 to +119
if is_neox_style:
q1, q2 = torch.chunk(q, 2, dim=-1)
k1, k2 = torch.chunk(k, 2, dim=-1)
else:
q1 = q[..., ::2]
q2 = q[..., 1::2]
k1 = k[..., ::2]
k2 = k[..., 1::2]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The inner if is_neox_style: check is redundant because it is already inside an if is_neox_style: block. This makes the else branch with slicing logic unreachable. For neox_style, only torch.chunk should be used. The slicing logic is for GPT-J style, which is handled by the Triton kernel in the outer else block.

        q1, q2 = torch.chunk(q, 2, dim=-1)
        k1, k2 = torch.chunk(k, 2, dim=-1)

stride_k_row,
stride_cos_row,
stride_sin_row,
interleaved: tl.constexpr,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The interleaved parameter is defined as a tl.constexpr but is not used within the _qk_rotary_embedding_kernel. The kernel's implementation currently assumes an interleaved memory layout for q and k tensors due to the calculation of offsets_x1 and offsets_x2. If non-interleaved tensors are passed, this will lead to incorrect results. Consider implementing the logic to handle both interleaved and non-interleaved cases based on this parameter, or remove it if only interleaved is supported.

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

Labels

diffusion SGLang Diffusion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant