Skip to main content

sglang-kernel (prior sgl-kernel)

Kernel Library for LLM inference engines

License: Apache-2.0 PyPI

sglang-kernel provides optimized compute primitives for LLM inference engines, enabling efficient inference for large language models and vision-language models through custom kernel operations. The source tree lives under the python/sglang/kernels/aot/ directory and the Python import path remains sgl_kernel.

Installation

Requires torch == 2.14.1

# Latest version
pip3 install sglang-kernel --upgrade

Building from Source

Requires

  • CMake ≥3.31,
  • Python ≥3.11
  • scikit-build-core
  • ninja(optional)

Use Makefile to build from the sgl-kernel source tree

make build

Limit build resource usage (CPU / parallelism)

By default, make build uses all available CPU cores. You can override build parallelism and NVCC compile threads:

# Limit parallel jobs (controls both make and cmake parallelism)
make build MAX_JOBS=2

# Additionally limit NVCC internal threads (reduces CPU and peak memory)
make build MAX_JOBS=2 CMAKE_ARGS="-DSGL_KERNEL_COMPILE_THREADS=1"

Contribution

Steps to add a new kernel:

  1. Implement the kernel in csrc
  2. Expose the interface in include/sgl_kernel_ops.h
  3. Create torch extension in csrc/common_extension.cc
  4. Update CMakeLists.txt to include new CUDA source
  5. Expose Python interface in python
  6. Add test and benchmark

Development Tips

  1. When creating torch extensions, add the function definition with m.def, and device binding with m.impl:
  • How to write schema: Schema reference

    // We need def with schema here for torch.compile
    m.def(
     "bmm_fp8(Tensor A, Tensor B, Tensor! D, Tensor A_scale, Tensor B_scale, Tensor workspace_buffer, "
     "int cublas_handle) -> ()");
    m.impl("bmm_fp8", torch::kCUDA, &bmm_fp8);
    

Adapting C++ Native Types for Torch Compatibility

Third-party C++ libraries often use int and float, but PyTorch bindings require int64_t and double due to Python's type mapping.

Use make_pytorch_shim from sgl_kernel_torch_shim.h to handle conversions automatically:

// Add type conversion for int -> int64_t
template <>
struct pytorch_library_compatible_type<int> {
  using type = int64_t;
  static int convert_from_type(int64_t arg) {
    TORCH_CHECK(arg <= std::numeric_limits<int>::max(), "value too large");
    TORCH_CHECK(arg >= std::numeric_limits<int>::min(), "value too small");
    return arg;
  }
};
// Wrap your function
m.impl("fwd", torch::kCUDA, make_pytorch_shim(&mha_fwd));

Testing & Benchmarking

  1. Add pytest tests in tests/, if you need to skip some test, please use @pytest.mark.skipif
@pytest.mark.skipif(
    skip_condition, reason="Nvfp4 Requires compute capability of 10 or above."
)
  1. Add benchmarks using triton benchmark in benchmark/

    We recommend using triton.testing.do_bench_cudagraph for kernel benchmarking:

    Compared to triton.testing.do_bench, do_bench_cudagraph provides:

    • Reduced CPU overhead impact for more accurate kernel performance measurements
    • Incorporation of PDL (Programmatic Dependent Launch) effects into individual kernel results
    • More realistic performance data on PDL-supported architectures (SM >= 90)
  2. Run test suite

Kernel Size Analysis

Analyze CUDA kernel sizes in compiled wheel files to identify oversized kernels and template-instantiation bloat:

This tool requires cubloaty (install with pip install cubloaty) to work.

# Install cubloaty
pip install cubloaty

# Analyze a wheel file
python analyze_whl_kernel_sizes.py path/to/sglang_kernel-*.whl

# Custom output file
python analyze_whl_kernel_sizes.py path/to/sglang_kernel-*.whl --output my_analysis.txt

The tool generates:

  • A text report with:
    • Kernel groups (by name prefix)
    • Individual kernel sizes (sorted by size)

Use this to identify large kernels and potential template instantiation bloat.

FAQ

Metadata

Release files for sglang-kernel 0.4.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for sglang-kernel 0.4.11
File Interpreter ABI Platform Actions
sglang_kernel-0.4.11-cp311-abi3-manylinux2014_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for sglang_kernel-0.4.11-cp311-abi3-manylinux2014_x86_64.whl
sglang_kernel-0.4.11-cp311-abi3-manylinux2014_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for sglang_kernel-0.4.11-cp311-abi3-manylinux2014_aarch64.whl

Total release size: 47.2 MB

Release files / sglang_kernel-0.4.11-cp311-abi3-manylinux2014_x86_64.whl

Download URL sglang_kernel-0.4.11-cp311-abi3-manylinux2014_x86_64.whl
Size 25.4 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
b1ae547c29b94bdf4fb31fcefd2f513d20693c6ec8566620cdf592bf8e463475
BLAKE2b-256 checksum
How to use checksums
ed874890f56b9b4af69376c73ecd3a8f8dd31a2cefb75d64e46bb024d48856d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Release files / sglang_kernel-0.4.11-cp311-abi3-manylinux2014_aarch64.whl

Download URL sglang_kernel-0.4.11-cp311-abi3-manylinux2014_aarch64.whl
Size 21.9 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
32e7f6fb918cc737c8874413b1662d657f7579e199f5f7bc72cd92abf0ab67a0
BLAKE2b-256 checksum
How to use checksums
be70087ea3a92f9d6ac59877cd395d2989e2a28bf9f5320abf1287106de64b4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Security for sglang-kernel 0.4.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page