| name | tilekernels-gpu-kernels |
| description | Expert skill for using TileKernels, a library of optimized GPU kernels for LLM operations (MoE routing, quantization, transpose, engram gating, Manifold HyperConnection) built with TileLang. |
| triggers | ["use tilekernels for moe routing","optimize gpu kernels with tilelang","fp8 quantization kernel deepseek","mixture of experts kernel library","engram gating kernel","manifold hyperconnection kernel","tilekernels quantization","write optimized llm kernels with tilelang"] |
TileKernels GPU Kernel Library
Skill by ara.so — Daily 2026 Skills collection.
TileKernels is a high-performance GPU kernel library for LLM operations (MoE routing, FP8/FP4 quantization, transpose, engram gating, Manifold HyperConnection) written in TileLang — a Python DSL for expressing GPU kernels with automatic optimization. Kernels target NVIDIA SM90/SM100 (Hopper/Blackwell) architectures and approach hardware performance limits.
Requirements
- Python 3.10+
- PyTorch 2.10+
- TileLang 0.1.9+
- NVIDIA SM90 or SM100 GPU (H100/H200/B100/B200)
- CUDA Toolkit 13.1+
Installation
pip install -e ".[dev]"
pip install tile-kernels
Project Structure
tile_kernels/
├── moe/ # MoE routing: top-k selection, token-to-expert mapping, weight normalization
├── quant/ # FP8/FP4/E5M6 quantization with fused SwiGLU ops
├── transpose/ # Batched matrix transpose
├── engram/ # Engram gating: fused RMSNorm, forward/backward, weight gradient reduction
├── mhc/ # Manifold HyperConnection: Sinkhorn normalization, mix split/apply
├── modeling/ # High-level torch.autograd.Function wrappers
├── torch/ # PyTorch reference implementations for validation
└── testing/ # Test and benchmark utilities
Key Modules and Usage
MoE Routing Kernels
import torch
from tile_kernels.moe import (
topk_gating,
token_to_expert_map,
fused_expand_reduce,
weight_normalize,
)
logits = torch.randn(1024, 256, device="cuda", dtype=torch.float32)
topk_weights, topk_indices = topk_gating(logits, top_k=8)
Quantization Kernels
import torch
from tile_kernels.quant import (
per_token_cast_fp8,
per_block_cast_fp8,
per_channel_cast_fp8,
fused_swiglu_quant_fp8,
)
x = torch.randn(1024, 4096, device="cuda", dtype=torch.bfloat16)
x_fp8, scale = per_token_cast_fp8(x)
w = torch.randn(8192, 4096, device="cuda", dtype=torch.bfloat16)
w_fp8, scale = per_block_cast_fp8(w, block_size=128)
gate = torch.randn(1024, 8192, device="cuda", dtype=torch.bfloat16)
up = torch.randn(1024, 8192, device="cuda", dtype=torch.bfloat16)
out_fp8, scale = fused_swiglu_quant_fp8(gate, up)
Transpose Kernels
import torch
from tile_kernels.transpose import batched_transpose
x = torch.randn(32, 1024, 4096, device="cuda", dtype=torch.bfloat16)
x_T = batched_transpose(x)
Engram Gating Kernels
import torch
from tile_kernels.engram import (
engram_gate_forward,
engram_gate_backward,
engram_weight_grad,
)
hidden = torch.randn(1024, 2048, device="cuda", dtype=torch.bfloat16)
weight = torch.randn(256, 2048, device="cuda", dtype=torch.bfloat16)
output, norm_hidden = engram_gate_forward(hidden, weight)
Manifold HyperConnection (mHC) Kernels
import torch
from tile_kernels.mhc import (
sinkhorn_normalize,
mhc_mix_split,
mhc_mix_apply,
)
conn_weights = torch.randn(8, 64, device="cuda", dtype=torch.float32)
normalized = sinkhorn_normalize(conn_weights, num_iters=20)
High-Level Modeling Layers
import torch
from tile_kernels.modeling import EngramGateLayer, MHCPipeline
gate_layer = EngramGateLayer(
hidden_size=2048,
num_experts=256,
).cuda()
hidden_states = torch.randn(1024, 2048, device="cuda", dtype=torch.bfloat16)
gate_output = gate_layer(hidden_states)
mhc = MHCPipeline(
num_connections=8,
hidden_size=2048,
).cuda()
Testing
pytest tests/transpose/test_transpose.py -n 4
pytest tests/transpose/test_transpose.py --run-benchmark
pytest tests/moe/ -n 4
pytest tests/quant/ -n 4
pytest tests/engram/ -n 4
TK_FULL_TEST=1 pytest -n 4 --count 2
pytest tests/quant/test_fp8_cast.py -n 4 --run-benchmark
Common Patterns
Pattern: Fused MoE Forward Pass
import torch
from tile_kernels.moe import topk_gating, token_to_expert_map
from tile_kernels.quant import per_token_cast_fp8
def moe_dispatch(hidden_states, gate_weight, top_k=8):
"""Full MoE dispatch using TileKernels."""
logits = torch.mm(hidden_states, gate_weight.T)
topk_weights, topk_indices = topk_gating(logits, top_k=top_k)
routing_map = token_to_expert_map(topk_indices, num_experts=gate_weight.shape[0])
hidden_fp8, scale = per_token_cast_fp8(hidden_states)
return hidden_fp8, scale, topk_weights, routing_map
Pattern: Using PyTorch Reference Implementations for Validation
import torch
from tile_kernels.quant import per_token_cast_fp8
from tile_kernels.torch import per_token_cast_fp8 as per_token_cast_fp8_ref
x = torch.randn(512, 4096, device="cuda", dtype=torch.bfloat16)
out_kernel, scale_kernel = per_token_cast_fp8(x)
out_ref, scale_ref = per_token_cast_fp8_ref(x)
torch.testing.assert_close(
out_kernel.float(), out_ref.float(), atol=1e-2, rtol=1e-2
)
print("Kernel matches reference ✓")
Pattern: Benchmarking a Kernel
import torch
from tile_kernels.testing import benchmark_kernel
from tile_kernels.transpose import batched_transpose
x = torch.randn(64, 4096, 4096, device="cuda", dtype=torch.bfloat16)
result = benchmark_kernel(
fn=batched_transpose,
args=(x,),
warmup=25,
rep=100,
)
print(f"Latency: {result.mean:.3f} ms, Bandwidth: {result.gbps:.1f} GB/s")
Pattern: Custom TileLang Kernel (extending the library)
import tilelang
import tilelang.language as T
def make_elementwise_scale_kernel(M, N, dtype="float16"):
@T.prim_func
def scale_kernel(
A: T.Buffer((M, N), dtype),
scale: T.Buffer((M,), "float32"),
B: T.Buffer((M, N), dtype),
):
for i, j in T.grid(M, N):
B[i, j] = T.cast(
T.cast(A[i, j], "float32") * scale[i],
dtype
)
return scale_kernel
kernel = tilelang.compile(make_elementwise_scale_kernel(1024, 4096))
Architecture-Specific Notes
- SM90 (Hopper: H100/H200): Full support, primary target
- SM100 (Blackwell: B100/B200): Full support
- Kernels use hardware-specific features (tensor memory accelerator, async copy, warp-specialized pipelines) — do NOT run on older GPUs (Ampere/Ada)
Troubleshooting
CUDA Architecture Mismatch
RuntimeError: CUDA error: no kernel image is available for execution on the device
→ You need SM90 or SM100. Check with: python -c "import torch; print(torch.cuda.get_device_capability())"
TileLang Version Mismatch
ImportError: cannot import name 'xyz' from 'tilelang'
→ Ensure TileLang >= 0.1.9: pip install tilelang>=0.1.9
CUDA Toolkit Version
error: identifier "__nv_fp8_e4m3" is undefined
→ Requires CUDA 13.1+. Check: nvcc --version
Out of Shared Memory
→ Kernels are tuned for specific tile sizes. If you hit shared memory limits, reduce batch size or sequence length, or file an issue.
Running Tests Without Benchmark Flag
pytest tests/ -n 4
pytest tests/ -n 4 --run-benchmark
Citation
@misc{tilekernels,
title={TileKernels},
author={Xiangwen Wang, Chenhao Xu, Huanqi Cao, Rui Tian, Weilin Zhao, Kuai Yu and Chenggang Zhao},
year={2026},
publisher = {GitHub},
howpublished = {\url{https://github.com/deepseek-ai/TileKernels}},
}