Skip to content

[Kernel] Add JIT rotary_embedding_kernel#17934

Merged
BBuf merged 12 commits into
sgl-project:mainfrom
pansicheng:jit-rotary-embedding
Feb 13, 2026
Merged

[Kernel] Add JIT rotary_embedding_kernel#17934
BBuf merged 12 commits into
sgl-project:mainfrom
pansicheng:jit-rotary-embedding

Conversation

@pansicheng
Copy link
Copy Markdown
Collaborator

@pansicheng pansicheng commented Jan 29, 2026

Motivation

Add JIT-compiled CUDA kernels for rotary_embedding

Modifications

Accuracy Tests

Verified against torch.ops.sgl_kernel.rotary_embedding
Check python/sglang/jit_kernel/tests/test_pos_enc.py
Run python -m pytest python/sglang/jit_kernel/tests/test_pos_enc.py -s

Benchmarking and Profiling

h20

root@h20-dev:/sgl-workspace/sglang# python -m pytest python/sglang/jit_kernel/tests/test_pos_enc.py -s
===================================================================================== test session starts ======================================================================================
platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
rootdir: /sgl-workspace/sglang/python
configfile: pyproject.toml
plugins: anyio-4.12.1, typeguard-4.4.4
collected 48 items                                                                                                                                                                             

python/sglang/jit_kernel/tests/test_pos_enc.py ..........................................
Performance Test - Batch=1, SeqLen=1, Tokens=1
JIT: 0.015313625ms, SGL: 0.016829967ms
Speedup (SGL/JIT): 1.10x
.
Performance Test - Batch=4, SeqLen=16, Tokens=64
JIT: 0.021228790ms, SGL: 0.024037361ms
Speedup (SGL/JIT): 1.13x
.
Performance Test - Batch=8, SeqLen=64, Tokens=512
JIT: 0.020864010ms, SGL: 0.024294853ms
Speedup (SGL/JIT): 1.16x
.
Performance Test - Batch=16, SeqLen=128, Tokens=2048
JIT: 0.021586418ms, SGL: 0.024285316ms
Speedup (SGL/JIT): 1.13x
.
Performance Test - Batch=32, SeqLen=512, Tokens=16384
JIT: 0.078592300ms, SGL: 0.084607601ms
Speedup (SGL/JIT): 1.08x
.
Performance Test - Batch=64, SeqLen=1024, Tokens=65536
JIT: 0.300598145ms, SGL: 0.321390629ms
Speedup (SGL/JIT): 1.07x
.

===================================================================================== 48 passed in 10.20s ======================================================================================

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @pansicheng, 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 rotary embedding functionality by introducing a highly optimized, JIT-compiled CUDA kernel. The primary goal is to accelerate the application of positional embeddings to query and key tensors in attention mechanisms, supporting both common embedding styles. The changes involve low-level CUDA programming for performance and a user-friendly Python API for integration, backed by thorough testing to ensure both accuracy and speed improvements.

Highlights

  • New JIT CUDA Kernel: Introduced a new JIT-compiled CUDA kernel for rotary embedding, designed for efficient application of positional encodings to query and key tensors.
  • Rotary Embedding Styles: The kernel supports both GPT-NeoX and GPT-J style rotary embeddings, providing flexibility for different model architectures.
  • Data Type Support: The kernel is templated to support float32, float16, and bfloat16 data types, ensuring broad compatibility with various precision requirements.
  • Optimized Memory Access: Added an SGLANG_LDG macro to utils.cuh to leverage CUDA's __ldg instruction for optimized global memory loads, improving performance on NVIDIA GPUs.
  • Python Integration and Testing: Provided a Python interface (pos_enc.py) to easily call the JIT kernel and included comprehensive correctness and performance tests (test_pos_enc.py) against existing SGL implementations.

🧠 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
Copy Markdown
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 JIT-compiled CUDA kernel for rotary embedding, complete with Python bindings and comprehensive tests for correctness and performance. The implementation is robust and well-tested. My review includes feedback on a few areas for improvement, such as correcting a type hint, removing unused code, clarifying comments, and refactoring for better maintainability and clarity in both the CUDA kernel and the Python test suite.

Comment thread python/sglang/jit_kernel/pos_enc.py
Comment thread python/sglang/jit_kernel/csrc/elementwise/pos_enc.cuh
Comment thread python/sglang/jit_kernel/csrc/elementwise/pos_enc.cuh
Comment thread python/sglang/jit_kernel/csrc/elementwise/pos_enc.cuh
Comment thread python/sglang/jit_kernel/pos_enc.py Outdated
Comment thread python/sglang/jit_kernel/tests/test_pos_enc.py Outdated
Comment thread python/sglang/jit_kernel/tests/test_pos_enc.py Outdated
Comment thread python/sglang/jit_kernel/tests/test_pos_enc.py Outdated
Comment thread python/sglang/jit_kernel/tests/test_pos_enc.py
pansicheng and others added 2 commits January 29, 2026 19:55
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Comment thread python/sglang/jit_kernel/pos_enc.py Outdated

@cache_once
def can_use_jit_rotary_embedding() -> bool:
logger = logging.getLogger(__name__)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can_use_jit_rotary_embedding is not need.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

removed

@@ -0,0 +1,301 @@
#include <sgl_kernel/tensor.h>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

added

Copy link
Copy Markdown
Collaborator

@BBuf BBuf left a comment

Choose a reason for hiding this comment

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

Please apply it in sglang srt.

@BBuf
Copy link
Copy Markdown
Collaborator

BBuf commented Jan 30, 2026

Please report benchmark result

@pansicheng
Copy link
Copy Markdown
Collaborator Author

Please apply it in sglang srt.

applied in python/sglang/srt/layers/rotary_embedding.py

@pansicheng
Copy link
Copy Markdown
Collaborator Author

Benchmarking and Profiling

reported in # Benchmarking and Profiling

Comment thread python/sglang/srt/layers/rotary_embedding.py Outdated
Comment thread python/sglang/srt/layers/rotary_embedding.py Outdated
Comment thread python/sglang/srt/layers/rotary_embedding.py Outdated
Comment thread python/sglang/srt/layers/rotary_embedding.py Outdated
Comment thread python/sglang/srt/layers/rotary_embedding.py Outdated
@BBuf
Copy link
Copy Markdown
Collaborator

BBuf commented Jan 30, 2026

@DarkSharpness Any other advices?

pansicheng and others added 2 commits February 2, 2026 10:12
Co-authored-by: Xiaoyu Zhang <35585791+BBuf@users.noreply.github.com>
Copy link
Copy Markdown
Collaborator

@BBuf BBuf left a comment

Choose a reason for hiding this comment

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

Thanks.

@BBuf
Copy link
Copy Markdown
Collaborator

BBuf commented Feb 2, 2026

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci label Feb 2, 2026
@pansicheng pansicheng force-pushed the jit-rotary-embedding branch from 7f7e875 to 8c91c7a Compare February 4, 2026 03:06
@pansicheng pansicheng force-pushed the jit-rotary-embedding branch from 7fa0310 to 4fe032e Compare February 5, 2026 06:58
@pansicheng pansicheng force-pushed the jit-rotary-embedding branch from 4fe032e to 9f95f8a Compare February 5, 2026 07:47
@BBuf
Copy link
Copy Markdown
Collaborator

BBuf commented Feb 6, 2026

/rerun-failed-ci

1 similar comment
@BBuf
Copy link
Copy Markdown
Collaborator

BBuf commented Feb 12, 2026

/rerun-failed-ci

@BBuf BBuf merged commit 7d4ae05 into sgl-project:main Feb 13, 2026
222 of 239 checks passed
Johnsonms pushed a commit to Johnsonms/sglang that referenced this pull request Feb 14, 2026
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Xiaoyu Zhang <35585791+BBuf@users.noreply.github.com>
Co-authored-by: root <root@zhikuan-A10x2.ea134>
Kangyan-Zhou pushed a commit that referenced this pull request Feb 17, 2026
…18903)

Co-authored-by: michaelzhang-ai <michaelzhang-ai@users.noreply.github.com>
HaiShaw added a commit that referenced this pull request Feb 17, 2026
magicYang1573 pushed a commit to magicYang1573/sglang that referenced this pull request Mar 9, 2026
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Xiaoyu Zhang <35585791+BBuf@users.noreply.github.com>
Co-authored-by: root <root@zhikuan-A10x2.ea134>
magicYang1573 pushed a commit to magicYang1573/sglang that referenced this pull request Mar 9, 2026
…ect#17934) (sgl-project#18903)

Co-authored-by: michaelzhang-ai <michaelzhang-ai@users.noreply.github.com>
Wangzheee pushed a commit to Wangzheee/sglang that referenced this pull request Mar 21, 2026
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Xiaoyu Zhang <35585791+BBuf@users.noreply.github.com>
Co-authored-by: root <root@zhikuan-A10x2.ea134>
Wangzheee pushed a commit to Wangzheee/sglang that referenced this pull request Mar 21, 2026
…ect#17934) (sgl-project#18903)

Co-authored-by: michaelzhang-ai <michaelzhang-ai@users.noreply.github.com>
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.

2 participants