agentleFS
MicrosoftKnown publishermicrosoft/llm-42/.claude/skills/add-sgl-kernel/SKILL.md

add-sgl-kernel

Skill30 starsChanged 9 months agoApache-2.0View on GitHub

Step-by-step tutorial for adding a heavyweight AOT CUDA/C++ kernel to sgl-kernel (including tests & benchmarks)

2 other projects use a version of this, 2 of them edited.

Adopted by

Most recently taken up by

  • Other versions2 projects use a version of this.

    Word for word · 0

    None found.

    Edited · 2

    1. sgl-project/sglang★ 39k11% same
    2. drunkcoding/AgentSkillsArxiv★ 418% same

    Wrote their own version · 0

    None found.

    How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.

  • In this repositoryOne other file it gives its agents.

The file

Show its rules and text

We split this skill into the rules it gives an agent

20 rules. About 0 are common, 9 are distinctive, and 11 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈9

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 11

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Heavyweight kernels go to sgl-kernel: If it depends on CUTLASS/FlashInfer/DeepGEMM (or similarly heavy stacks), implement it in …

    line 18
  2. Very specific:

    Lightweight kernels go to python/sglang/jit_kernel: If it is small, has few dependencies, and benefits from rapid iteration, implement it …

    line 19
  3. Distinctive:

    Add a new kernel end-to-end, including:

    line 30
  4. Very specific:

    If assumptions are violated, fail fast with a readable error (e.g.

    line 66
  5. Distinctive:

    After kernel launch, perform device error checking (follow existing project conventions)

    line 67
  6. Distinctive:

    Prefer explicit validation over "it probably works".

    line 71
  7. Distinctive:

    If a kernel only works on certain architectures, make that restriction explicit (error/skip behavior).

    line 72
  8. Distinctive:

    Add your function declaration in the appropriate section.

    line 82
  9. Very specific:

    Add m.def(...) with a schema.

    line 94
  10. Very specific:

    Add m.impl(...) for CUDA dispatch.

    line 95
  11. Very specific:

    If your underlying C++ API uses native types (e.g.

    line 100
  12. Very specific:

    Add your new .cu / .cc file to the set(SOURCES ...) list.

    line 110
  13. Distinctive:

    Keep the list alphabetically sorted (the file explicitly requires this).

    line 114
  14. Distinctive:

    If your kernel has arch constraints, reflect that in tests/benchmarks via skip logic.

    line 115
  15. Very specific:

    Add/extend a Python wrapper under sgl-kernel/python/sgl_kernel/ (follow existing module organization).

    line 123
  16. Very specific:

    Use @pytest.mark.skipif(..., reason="...") when compute capability requirements apply.

    line 143
  17. Distinctive:

    Follow the repository convention:

    line 159
  18. Very specific:

    Prefer triton.testing.do_bench_cudagraph for timing

    line 162
  19. Distinctive:

    If you need to limit host resource usage:

    line 189
  20. Very specific:

    Binary bloat: use sgl-kernel/analyze_whl_kernel_sizes.py

    line 208

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Tutorial: Adding a New Kernel to sgl-kernel (AOT / Heavyweight)

This SKILL is a step-by-step guide for adding a heavyweight CUDA/C++ kernel to sgl-kernel/.

Typical characteristics:

  • Depends on heavyweight components such as CUTLASS / FlashInfer / DeepGEMM / sgl-attn
  • Needs AOT build and distribution (wheel / torch extension), so build time, link flags, CUDA arch targets, and binary size matter
  • Exposed as a stable sgl_kernel API and used by higher-level code (including torch.compile)

Two rules of thumb (must follow)

  1. Heavyweight kernels go to sgl-kernel. If it depends on CUTLASS/FlashInfer/DeepGEMM (or similarly heavy stacks), implement it in sgl-kernel/.
  2. Lightweight kernels go to python/sglang/jit_kernel. If it is small, has few dependencies, and benefits from rapid iteration, implement it as a JIT kernel instead.

In addition, every new kernel must ship with:

  • Tests (pytest)
  • A benchmark script (triton.testing)

Goal

Add a new kernel end-to-end, including:

  • CUDA/C++ implementation
  • Torch library registration (m.def schema + m.impl dispatch)
  • Build system integration (CMake sources list)
  • Python-facing API
  • Correctness tests and performance benchmarks

Repository integration map

You will typically touch these files/areas:

  • Implementation: sgl-kernel/csrc/...
  • Public declarations: sgl-kernel/include/sgl_kernel_ops.h
  • Torch extension registration: sgl-kernel/csrc/common_extension.cc
  • Build: sgl-kernel/CMakeLists.txt (set(SOURCES ...))
  • Python API: sgl-kernel/python/sgl_kernel/... and sgl-kernel/python/sgl_kernel/__init__.py
  • Tests: sgl-kernel/tests/test_<op>.py
  • Benchmarks: sgl-kernel/benchmark/bench_<op>.py

Step 1: Implement the kernel in csrc/

  1. Pick the right subdirectory:
  • csrc/elementwise/
  • csrc/gemm/
  • csrc/attention/
  • csrc/moe/
  1. Implementation requirements:
  • Clearly define dtype/shape/stride/contiguity assumptions
  • If assumptions are violated, fail fast with a readable error (e.g. TORCH_CHECK(...))
  • After kernel launch, perform device error checking (follow existing project conventions)

Key points:

  • Prefer explicit validation over "it probably works".
  • If a kernel only works on certain architectures, make that restriction explicit (error/skip behavior).

Step 2: Add a C++ declaration in include/sgl_kernel_ops.h

Edit:

  • sgl-kernel/include/sgl_kernel_ops.h

Add your function declaration in the appropriate section.


Step 3: Register the op in csrc/common_extension.cc (schema + dispatch)

Edit:

  • sgl-kernel/csrc/common_extension.cc

Inside TORCH_LIBRARY_FRAGMENT(sgl_kernel, m):

  1. Add m.def(...) with a schema.
  2. Add m.impl(...) for CUDA dispatch.

Key points:

  • The schema is important for torch.compile and for consistent call signatures.
  • If your underlying C++ API uses native types (e.g. int, float), but PyTorch bindings expect int64_t / double, use the project’s recommended shim approach (see sgl-kernel/README.md).

Step 4: Add the new source file to CMakeLists.txt

Edit:

  • sgl-kernel/CMakeLists.txt

Add your new .cu / .cc file to the set(SOURCES ...) list.

Key points:

  • Keep the list alphabetically sorted (the file explicitly requires this).
  • If your kernel has arch constraints, reflect that in tests/benchmarks via skip logic.

Step 5: Expose a Python API under sgl-kernel/python/sgl_kernel/

Goal: users can call sgl_kernel.<op>(...).

  • Add/extend a Python wrapper under sgl-kernel/python/sgl_kernel/ (follow existing module organization).
  • Export it from sgl-kernel/python/sgl_kernel/__init__.py.

Step 6: Write tests (required)

Create:

  • sgl-kernel/tests/test_<op>.py

Minimum coverage:

  • Shapes: typical + edge cases
  • Dtypes: whatever the kernel claims to support
  • Correctness: compare with a reference implementation (PyTorch / FlashInfer / another stable backend)
  • Negative cases: unsupported dtype/shape/arch should either raise a clear error or be explicitly skipped

Skipping by architecture:

  • Use @pytest.mark.skipif(..., reason="...") when compute capability requirements apply.

Run:

pytest sgl-kernel/tests/test_<op>.py -q

Step 7: Add a benchmark (required)

Create:

  • sgl-kernel/benchmark/bench_<op>.py

Follow the repository convention:

  • Use triton.testing.Benchmark + triton.testing.perf_report
  • Prefer triton.testing.do_bench_cudagraph for timing

Minimum benchmark requirements:

  • At least two providers/variants:
    • Your sgl_kernel implementation
    • A baseline (PyTorch / torch.compile / Triton / FlashInfer)
  • Quantiles output (median/min/max)
  • CI-friendly ranges controlled by CI / GITHUB_ACTIONS

Run:

python sgl-kernel/benchmark/bench_<op>.py

Step 8: Build and validate

Build:

cd sgl-kernel
make build -j16

If you need to limit host resource usage:

cd sgl-kernel
make build -j1 MAX_JOBS=2 CMAKE_ARGS="-DSGL_KERNEL_COMPILE_THREADS=1"

Validate:

  • Tests: pytest sgl-kernel/tests/test_<op>.py -q
  • Benchmark: python sgl-kernel/benchmark/bench_<op>.py

Troubleshooting

  • Async CUDA errors: CUDA_LAUNCH_BLOCKING=1
  • Memory errors: compute-sanitizer --tool memcheck python ...
  • Build is too slow / OOM: reduce MAX_JOBS and SGL_KERNEL_COMPILE_THREADS
  • Binary bloat: use sgl-kernel/analyze_whl_kernel_sizes.py

References

  • sgl-kernel/README.md
  • sgl-kernel/include/sgl_kernel_ops.h
  • sgl-kernel/csrc/common_extension.cc
  • sgl-kernel/CMakeLists.txt

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.