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
- sgl-project/sglangedited, first seen Oct 8, 2026
- drunkcoding/AgentSkillsArxivedited, first seen Mar 1, 2026
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 2
- sgl-project/sglang★ 39k11% same
- 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.
- add-jit-kernelSkill
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
- Very specific: line 18
Heavyweight kernels go to
sgl-kernel: If it depends on CUTLASS/FlashInfer/DeepGEMM (or similarly heavy stacks), implement it in … - Very specific: line 19
Lightweight kernels go to
python/sglang/jit_kernel: If it is small, has few dependencies, and benefits from rapid iteration, implement it … - Distinctive: line 30
Add a new kernel end-to-end, including:
- Very specific: line 66
If assumptions are violated, fail fast with a readable error (e.g.
- Distinctive: line 67
After kernel launch, perform device error checking (follow existing project conventions)
- Distinctive: line 71
Prefer explicit validation over "it probably works".
- Distinctive: line 72
If a kernel only works on certain architectures, make that restriction explicit (error/skip behavior).
- Distinctive: line 82
Add your function declaration in the appropriate section.
- Very specific: line 94
Add m.def(...) with a schema.
- Very specific: line 95
Add m.impl(...) for CUDA dispatch.
- Very specific: line 100
If your underlying C++ API uses native types (e.g.
- Very specific: line 110
Add your new .cu / .cc file to the set(SOURCES ...) list.
- Distinctive: line 114
Keep the list alphabetically sorted (the file explicitly requires this).
- Distinctive: line 115
If your kernel has arch constraints, reflect that in tests/benchmarks via skip logic.
- Very specific: line 123
Add/extend a Python wrapper under sgl-kernel/python/sgl_kernel/ (follow existing module organization).
- Very specific: line 143
Use @pytest.mark.skipif(..., reason="...") when compute capability requirements apply.
- Distinctive: line 159
Follow the repository convention:
- Very specific: line 162
Prefer triton.testing.do_bench_cudagraph for timing
- Distinctive: line 189
If you need to limit host resource usage:
- Very specific: line 208
Binary bloat: use sgl-kernel/analyze_whl_kernel_sizes.py
≈ “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.
Files
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_kernelAPI and used by higher-level code (includingtorch.compile)
Two rules of thumb (must follow)
- Heavyweight kernels go to
sgl-kernel. If it depends on CUTLASS/FlashInfer/DeepGEMM (or similarly heavy stacks), implement it insgl-kernel/. - 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.defschema +m.impldispatch) - 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/...andsgl-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/
- Pick the right subdirectory:
csrc/elementwise/csrc/gemm/csrc/attention/csrc/moe/
- 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):
- Add
m.def(...)with a schema. - Add
m.impl(...)for CUDA dispatch.
Key points:
- The schema is important for
torch.compileand for consistent call signatures. - If your underlying C++ API uses native types (e.g.
int,float), but PyTorch bindings expectint64_t/double, use the project’s recommended shim approach (seesgl-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 -qStep 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_cudagraphfor timing
Minimum benchmark requirements:
- At least two providers/variants:
- Your
sgl_kernelimplementation - A baseline (PyTorch /
torch.compile/ Triton / FlashInfer)
- Your
- Quantiles output (median/min/max)
- CI-friendly ranges controlled by
CI/GITHUB_ACTIONS
Run:
python sgl-kernel/benchmark/bench_<op>.pyStep 8: Build and validate
Build:
cd sgl-kernel
make build -j16If 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_JOBSandSGL_KERNEL_COMPILE_THREADS - Binary bloat: use
sgl-kernel/analyze_whl_kernel_sizes.py
References
sgl-kernel/README.mdsgl-kernel/include/sgl_kernel_ops.hsgl-kernel/csrc/common_extension.ccsgl-kernel/CMakeLists.txt
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
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.

