Skip to content

[JIT Kernel] Migrate kimi_k2_moe_fused_gate to JIT - #19703

Closed
xingsy97 wants to merge 2 commits into
sgl-project:mainfrom
xingsy97:feat/jit-kimi-k2-moe-fused-gate
Closed

xingsy97 wants to merge 2 commits into
sgl-project:mainfrom
xingsy97:feat/jit-kimi-k2-moe-fused-gate

Conversation

@xingsy97

@xingsy97 xingsy97 commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

Motivation

Migrate kimi_k2_moe_fused_gate kernel to JIT compilation (#17865).

Modifications

Under python/sglang/jit_kernel/:

  • csrc/moe/kimi_k2_moe_fused_gate.cuh — CUDA kernel (ported from sgl-kernel/csrc/moe/kimi_k2_moe_fused_gate.cu, both small_token and large_token variants)
  • kimi_k2_moe_fused_gate.py — Python wrapper
  • tests/test_kimi_k2_moe_fused_gate.py — Unit tests (JIT vs PyTorch)
  • benchmark/bench_kimi_k2_moe_fused_gate.py — Benchmark (JIT vs AOT)

And

  • python/sglang/srt/layers/moe/topk.py — Switch import to JIT

Accuracy Tests

Pass all tests defined in python/sglang/jit_kernel/tests/test_kimi_k2_moe_fused_gate.py

Benchmarking and Profiling

GPU: NVIDIA GeForce RTX 5060 Laptop GPU
Driver: 580.126.20 | CUDA: 12.8 | PyTorch: 2.9.1+cu128
Script: python/sglang/jit_kernel/benchmark/bench_kimi_k2_moe_fused_gate.py

kimi-k2-moe-fused-gate-performance (unit: us):
   M × topk         JIT      AOT      AOT->JIT
   1 × 2            1.70     1.70     0.0%
   8 × 6            3.15     3.11     +1.3%
   32 × 4           2.79     2.74     +1.8%
   128 × 6          8.83     8.84     -0.1%
   256 × 2          7.07     6.89     +2.6%
   512 × 6         27.04    27.67     -2.3%
   1024 × 2         9.11     9.10     +0.1%
   1024 × 6        14.60    14.82     -1.5%

Both small_token (m <= 512) and large_token (m > 512) kernel variants are ported; no regression observed.

Checklist

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 migrates the kimi_k2_moe_fused_gate kernel to the JIT framework, enhancing its flexibility and maintainability. It includes a new CUDA kernel, a Python wrapper with JIT loading, unit tests, and benchmarks. The changes ensure accuracy and provide performance comparisons against the existing AOT implementation.

Highlights

  • JIT Kernel Migration: Migrated the kimi_k2_moe_fused_gate kernel from sgl-kernel to sglang.jit_kernel to leverage JIT compilation.
  • Performance Optimization: The JIT-compiled kernel matches the performance of the AOT-compiled kernel up to m=512, with AOT being faster at m=1024 due to the large_token kernel variant.
  • Accuracy and Benchmarking: Added unit tests and benchmarks to ensure accuracy and performance parity between the JIT and PyTorch 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.

Changelog
  • python/sglang/srt/layers/moe/topk.py
    • Switched the kimi_k2_moe_fused_gate import from sgl_kernel to sglang.jit_kernel.
Activity
  • New CUDA kernel added for kimi_k2_moe_fused_gate.
  • Python wrapper created with JIT loading.
  • Unit tests added to verify JIT vs PyTorch reference.
  • Benchmarks implemented to compare JIT vs AOT performance.
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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 successfully migrates the kimi_k2_moe_fused_gate kernel to the JIT framework, which is a good step towards kernel slimming. The changes include the ported CUDA kernel, a Python wrapper, unit tests, and benchmarks. The code is well-structured and the implementation seems correct. The new JIT kernel is properly integrated, and the tests provide good coverage.

I have one suggestion to improve the robustness of the reference implementation in the unit tests to avoid potential NaN values during division by zero, ensuring the tests are reliable under all conditions.

Comment thread python/sglang/jit_kernel/tests/test_kimi_k2_moe_fused_gate.py Outdated
@xingsy97
xingsy97 force-pushed the feat/jit-kimi-k2-moe-fused-gate branch 4 times, most recently from 82b5c39 to faacb87 Compare March 2, 2026 18:17
@xingsy97
xingsy97 force-pushed the feat/jit-kimi-k2-moe-fused-gate branch from faacb87 to d62a4d9 Compare March 2, 2026 18:49
@xingsy97
xingsy97 marked this pull request as draft March 2, 2026 18:52
@xingsy97
xingsy97 marked this pull request as ready for review March 5, 2026 09:41
@xingsy97
xingsy97 requested a review from yuan-luo as a code owner March 5, 2026 09:41
@xingsy97 xingsy97 changed the title [Kernel Slimming] Migrate kimi_k2_moe_fused_gate to JIT [JIT Kernel] Migrate kimi_k2_moe_fused_gate to JIT Mar 6, 2026
Port the large_token kernel (m > 512) from AOT to JIT with:
- Vectorized float4 loads (6 tokens/block, 1 warp/token)
- Runtime dispatch: small_token (m <= 512) vs large_token (m > 512)
- Extended tests to cover m=512, 1024, 2048
@hnyls2002

Copy link
Copy Markdown
Collaborator

Thanks @xingsy97! This migration has already landed - kimi_k2_moe_fused_gate runs through JIT on main via python/sglang/kernels/jit/csrc/trtllm_lora_temp/kimi_k2_moe_fused_gate.cuh and python/sglang/kernels/ops/moe/trtllm_lora_temp/kimi_k2_moe_fused_gate.py (merged in #27329), and python/sglang/srt/layers/moe/topk.py:1624 already imports the JIT wrapper. The python/sglang/jit_kernel/ package this PR adds to was also removed by #31666 when JIT infra moved to sglang.kernels. Closing as superseded - please reopen if I've missed something.

@hnyls2002 hnyls2002 closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants