[Performance] Move the contiguous to torch compile region#13199
[Performance] Move the contiguous to torch compile region#13199sglang-bot merged 1 commit intosgl-project:mainfrom
Conversation
Summary of ChangesHello @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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| cos = cos.contiguous() | ||
| sin = sin.contiguous() |
There was a problem hiding this comment.
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.
Motivation
Modifications
Move
cosandsincontiguous op totorch.compileregion.sglang/python/sglang/srt/layers/rotary_embedding.py
Lines 1277 to 1280 in aead0ef
Before:
After:
This directly results in a 4% speedup in small bs decode (< 16).
Accuracy Tests
Benchmarking and Profiling
Checklist