Skip to content

Conversation

@DarkSharpness
Copy link
Collaborator

Motivation

python3 -m sglang.launch_server --model-path Qwen/Qwen3-VL-30B-A3B-Thinking \
    --context-length 262144 --cuda-graph-max-bs 64 --reasoning-parser deepseek-r1 --load-format dummy --port 1919 --disable-radix

Modifications

Move cos and sin contiguous op to torch.compile region.

q = q.contiguous()
k = k.contiguous()
cos = cos.contiguous()
sin = sin.contiguous()

Before:

image

After:

image

This directly results in a 4% speedup in small bs decode (< 16).

Accuracy Tests

Benchmarking and Profiling

Checklist

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @DarkSharpness, 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 focuses on improving the inference performance of the system, particularly for scenarios involving small batch sizes. By adjusting the placement of contiguous operations for cos and sin within the torch.compile region of the rotary embedding layer, the change aims to optimize memory access patterns and computation flow, leading to a measurable speed increase.

Highlights

  • Performance Optimization: This pull request introduces a performance enhancement by strategically moving contiguous operations for cos and sin within the torch.compile region.
  • Rotary Embedding Layer: The specific change targets the _forward_triton function in the rotary embedding layer, ensuring cos and sin tensors are contiguous after being chunked.
  • Speedup Achieved: This optimization directly results in a notable 4% speedup for small batch size decoding (batch sizes less than 16).
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 moves the .contiguous() calls for cos and sin tensors into a torch.compile region to improve performance, which is a good optimization. My review includes a suggestion to extend this optimization to the query and key tensors for further performance gains and consistency. I also recommend removing the now-redundant .contiguous() calls from the triton_mrope function to improve code clarity.

Comment on lines +1484 to +1485
cos = cos.contiguous()
sin = sin.contiguous()
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This is a good optimization to move the .contiguous() calls into the torch.compile region. To make this optimization more complete, you could apply the same logic to the query and key tensors.

Currently, triton_mrope also calls .contiguous() on q and k outside the compiled region. Moving these calls for query and key into _forward_triton should provide further performance benefits. I recommend adding query = query.contiguous() and key = key.contiguous() within the if positions.ndim == 2: block, just before the call to triton_mrope_wrapper.

After ensuring query, key, cos, and sin are all contiguous within _forward_triton, the corresponding .contiguous() calls on lines 1277-1280 in the triton_mrope function can be removed to improve code clarity.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant