futuremls-lab/oscar/add-sgl-kernel
Claude Code and agent skill by futuremls-lab/oscar
- community
Checking that the SKILL.md file is still in the repository.
Step-by-step tutorial for adding a heavyweight AOT CUDA/C++ kernel to sgl-kernel (including tests & benchmarks)
Install
npx vskill@latest install futuremls-lab/oscar/add-sgl-kernelSKILL.md
Tutorial: Adding a New Kernel to sgl-kernel (AOT / Heavyweight)
This tutorial walks through adding a simple element-wise scale operation as an AOT kernel. We'll implement scale(x, factor) = x * factor to demonstrate the complete workflow.
Goal
Add a new operation that scales each element of a tensor by a scalar factor:
- Input: tensor
x(CUDA) and scalarfactor(float) - Output:
x * factor(element-wise, in-place or into pre-allocatedout) - Supported dtypes: FP16 (`torch.float16`), BF16 (`torch.bfloat16`), FP32 (`torch.float32`)
- Dispatched via
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FLOAT_FP16macro (defined insgl-kernel/include/utils.h)
Two rules of thumb (must follow)
- Prefer `python/sglang/jit_kernel` first when the kernel does not depend on CUTLASS or another large C++ project. This is the default path for lightweight kernels that benefit from rapid iteration.
- Prefer `sgl-kernel` when the kernel does depend on CUTLASS or another large C++ project, or when it should be part of the AOT wheel / torch op registration flow.
- Exception: if the dependency is
flashinfer, or CUTLASS that is already provided throughflashinfer, the kernel can still be implemented asjit_kernel.
In addition, every new kernel must ship with:
- Tests (pytest)
- A benchmark script (triton.testing)
Repository integration map
You will typically touch these files/areas:
- Implementation:
sgl-kernel/csrc/elementwise/scale.cu(pick the right subdirectory) - 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_scale.py - Benchmarks:
sgl-kernel/benchmark/bench_scale.py
Step 1: Implement the kernel in csrc/
Pick the right subdirectory:
csrc/elementwise/— for element-wise ops (our example)csrc/gemm/,csrc/attention/,csrc/moe/— for other categories
Create sgl-kernel/csrc/elementwise/scale.cu:
Key points:
- Use
at::Tensor(PyTorch tensors),TORCH_CHECKfor validation,at::cuda::getCurrentCUDAStream()for stream - Keep Python wrappers thin; do shape/dtype/device validation in C++ right around the launch path
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FLOAT_FP16coversfloat,half(FP16),__nv_bfloat16(BF16)- Add device error checking after every kernel launch
- If a kernel only works on certain architectures, enforce that with
TORCH_CHECKand skip logic in tests
Step 2: Add a C++ declaration in include/sgl_kernel_ops.h
Edit sgl-kernel/include/sgl_kernel_ops.h, add to the elementwise section:
Step 3: Register the op in csrc/common_extension.cc
Edit sgl-kernel/csrc/common_extension.cc, inside TORCH_LIBRARY_FRAGMENT(sgl_kernel, m):
Key points:
Tensor!means in-place / mutable output argument- The schema is important for
torch.compileand for consistent call signatures - Keep the torch schema in PyTorch scalar types (
floathere), but note that the C++ launcher signature still needsdoublefor scalar arguments accepted bytorch::Library
Step 4: Add the new source file to CMakeLists.txt
Edit sgl-kernel/CMakeLists.txt, add to set(SOURCES ...):
Key points:
- Keep the list alphabetically sorted (the file explicitly requires this)
- If the kernel has arch constraints, reflect that in tests/benchmarks via skip logic
Step 5: Expose a Python API under sgl-kernel/python/sgl_kernel/
Prefer following the existing module organization first. For elementwise kernels, the usual pattern is:
- implement the Python wrapper in
sgl-kernel/python/sgl_kernel/elementwise.py - then re-export it from
sgl-kernel/python/sgl_kernel/__init__.py
For example, in sgl-kernel/python/sgl_kernel/elementwise.py, add:
Then re-export it from sgl-kernel/python/sgl_kernel/__init__.py following the existing import style used by other kernels.
Step 6: Write tests (required)
Create sgl-kernel/tests/test_scale.py:
Step 7: Add a benchmark (required)
Create sgl-kernel/benchmark/bench_scale.py:
Step 8: Build
Build:
If you need to limit host resource usage:
Step 9: Validate
After building successfully, run the test and benchmark:
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 - CMake sources list: if your
.cufile is missing fromSOURCES, the symbol will be undefined at link time
References
sgl-kernel/README.mdsgl-kernel/include/sgl_kernel_ops.hsgl-kernel/csrc/common_extension.ccsgl-kernel/CMakeLists.txtsgl-kernel/include/utils.h—DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FLOAT_FP16macro and friendssgl-kernel/csrc/elementwise/activation.cu— reference for the FP16/BF16/FP32 dispatch pattern
Summary of Files Created/Modified
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingsglang-research/.claude/skills/add-sgl-kernel/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/futuremls-lab/oscar/add-sgl-kernel)