Skip to content

Comments

[jit_kernel] Add JIT segment_packbits kernel (port of sgl-kernel packbit.cu) #19088

Open
Johnsonms wants to merge 4 commits intosgl-project:mainfrom
Johnsonms:packbit-jit
Open

[jit_kernel] Add JIT segment_packbits kernel (port of sgl-kernel packbit.cu) #19088
Johnsonms wants to merge 4 commits intosgl-project:mainfrom
Johnsonms:packbit-jit

Conversation

@Johnsonms
Copy link
Contributor

@Johnsonms Johnsonms commented Feb 20, 2026

Motivation

#17865

sgl-kernel ships an AOT CUDA kernel for segment_packbits. This PR ports
it to the JIT kernel framework so it can be used without the pre-built
sgl-kernel wheel, following the same pattern as the eagle_utils and
ngram_utils JIT ports.

Modifications

  • python/sglang/jit_kernel/csrc/speculative/packbit.cuh: TVM FFI
    entry point wrapping flashinfer::quantization::SegmentPackBits; uses
    RuntimeCheck for input validation and LaunchKernel::resolve_device for
    stream handling.
  • python/sglang/jit_kernel/packbit.py: Python wrapper with
    @register_custom_op (mutates_args=["y"]) and @cache_once; loads with
    the flashinfer include path for quantization.cuh.
  • python/sglang/jit_kernel/tests/test_packbit.py: 13 tests —
    known-answer (0xFF, 0x00, little-endian, partial byte), CPU reference
    validation for fixed-length and variable-length segments, and JIT vs AOT
    cross-validation.
  • python/sglang/jit_kernel/benchmark/bench_packbit.py: Throughput
    benchmark across batch sizes and segment lengths with a correctness diff
    between JIT and AOT.

Accuracy Tests

python -m pytest python/sglang/jit_kernel/tests/test_packbit.py
image

Benchmarking and Profiling

python python/sglang/jit_kernel/benchmark/bench_packbit.py

image

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.

Port sgl-kernel/csrc/speculative/packbit.cu to the JIT kernel framework.

- csrc/speculative/packbit.cuh: TVM FFI entry point wrapping
  flashinfer::quantization::SegmentPackBits; uses RuntimeCheck validation
  and LaunchKernel::resolve_device for stream handling
- packbit.py: Python wrapper with @register_custom_op and @cache_once;
  loads with flashinfer include path for quantization.cuh header
- tests/test_packbit.py: 12 tests covering known-answer (0xFF, 0x00,
  little-endian, partial byte), CPU reference validation, and JIT vs AOT
- benchmark/bench_packbit.py: throughput benchmark across batch sizes
  and segment lengths with correctness diff
Add test_variable_length_segments covering segments of lengths 1–65 in a
single batch, mixing exact multiples-of-8 and partial last bytes, validated
against the CPU reference.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Johnsonms, 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 integrates the segment_packbits functionality into the JIT kernel framework, eliminating the dependency on the pre-built sgl-kernel wheel. This change enhances the flexibility and ease of use for segment_packbits by allowing it to be compiled and used on-the-fly, aligning with the pattern established by other JIT ports. The implementation includes the C++ CUDA kernel, a Python wrapper, and thorough testing and benchmarking to ensure correctness and performance parity with the original AOT version.

Highlights

  • JIT Kernel Implementation: A new JIT (Just-In-Time) CUDA kernel for segment_packbits has been added, porting functionality previously available only as an AOT (Ahead-Of-Time) kernel.
  • Python Integration: A Python wrapper for the new JIT kernel is introduced, making it accessible via @register_custom_op and load_jit.
  • Comprehensive Testing: Extensive unit tests have been added, including known-answer tests, validation against a CPU reference, and cross-validation against the existing AOT kernel.
  • Performance Benchmarking: A new benchmark script is included to compare the throughput and correctness of the JIT segment_packbits against its AOT counterpart.

🧠 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/jit_kernel/benchmark/bench_packbit.py
    • Added a new benchmark script to compare the performance and correctness of the JIT segment_packbits kernel against the AOT sgl_kernel version.
  • python/sglang/jit_kernel/csrc/speculative/packbit.cuh
    • Implemented the C++ CUDA kernel for segment_packbits, including TVM FFI entry points, input validation using RuntimeCheck, and stream handling with LaunchKernel::resolve_device.
  • python/sglang/jit_kernel/packbit.py
    • Created a Python wrapper for the JIT segment_packbits kernel, utilizing @register_custom_op and load_jit for dynamic compilation and integration.
  • python/sglang/jit_kernel/tests/test_packbit.py
    • Added a comprehensive suite of unit tests for the JIT segment_packbits kernel, covering known-answer scenarios, validation against a CPU reference implementation for fixed and variable-length segments, and cross-validation against the AOT kernel.
Activity
  • Accuracy tests were performed using pytest python/sglang/jit_kernel/tests/test_packbit.py, with results indicating successful execution.
  • Benchmarking and profiling were conducted using python python/sglang/jit_kernel/benchmark/bench_packbit.py, providing throughput comparisons between JIT and AOT implementations.
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 JIT kernel for segment_packbits, porting an existing AOT kernel to the JIT framework. This is a valuable addition that reduces reliance on pre-built wheels. The implementation is well-structured, with a clear separation between the C++ FFI wrapper, the Python JIT loader, and the user-facing Python function. The accompanying tests are comprehensive, covering known-answer cases, comparison with a CPU reference implementation, and cross-validation against the AOT kernel, which ensures a high degree of confidence in its correctness. The benchmark script is also well-designed for comparing the JIT and AOT versions. I have one minor suggestion to improve the reproducibility of the benchmark.

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.

1 participant