triton-kernels
Use when the user asks about OpenAI Triton kernels, @triton.jit, tl.load, tl.dot, triton.autotune, fused kernels in Python or torch.compile Inductor kernels…
от j4flmao★ 47 звёзд на GitHub
Установить triton-kernels в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
Triton Kernels
Purpose
Triton is a Python DSL and compiler for GPU kernels. You program one program instance that processes a block (tile) of data with vector operations; the compiler handles thread mapping, coalescing, shared-memory staging, and tensor-core use. It is the fastest route to fused custom ops in PyTorch (and what torch.compile's Inductor generates), on NVIDIA and AMD GPUs.
Agent Protocol
Trigger
"triton kernel", "openai triton", "@triton.jit", "tl.load", "tl.dot", "triton.autotune", "fused kernel", "custom kernel in python", "inductor generated kernel".
Input Context
- Operation to implement, tensor shapes/dtypes/strides, GPU, PyTorch and Triton versions.
- Baseline implementation for correctness and performance comparison.
Output Artifact
Kernel + Python wrapper + correctness test + benchmark. No file unless requested.
Response Format
Kernel: {name} grid={expr} blocks={BLOCK_*}
Fusion: {ops fused, HBM round-trips removed}
Correctness: torch.testing.assert_close(..., atol, rtol)
Benchmark: {GB/s or TFLOP/s} vs {baseline}
Completion Criteria
- Masks on every load/store that can go out of bounds.
- Correctness test vs PyTorch reference across odd sizes (not just powers of two).
- Benchmarked with
triton.testing.do_benchagainst the PyTorch/torch.compilebaseline. - Autotune
keyincludes the dimensions that change the best config.
Programming Model
| Concept | Triton |
|---|---|
| Kernel | @triton.jit function; pointer args are tensors passed from Python |
| Instance id | tl.program_id(axis) |
| Block of indices | tl.arange(0, BLOCK); BLOCK must be a tl.constexpr power of two |
| Memory | tl.load(ptr + offs, mask=..., other=0.0), tl.store(ptr + offs, val, mask=...) |
| Math | elementwise ops, tl.sum/tl.max reductions, tl.dot (tensor cores), tl.exp, tl.where |
| Launch | kernel[grid](args...), grid is a tuple or lambda meta: (...) |
| Tuning | num_warps, num_stages (software pipelining depth), block sizes |
Values from tl.load live in registers; the compiler decides when to use shared memory (e.g. for tl.dot operands).
Example: Fused Row-wise Softmax
import torch
import triton
import triton.language as tl
@triton.jit
def softmax_kernel(out_ptr, in_ptr, in_row_stride, out_row_stride, n_cols,
BLOCK_SIZE: tl.constexpr):
row = tl.program_id(0)
cols = tl.arange(0, BLOCK_SIZE)
mask = cols < n_cols
x = tl.load(in_ptr + row * in_row_stride + cols, mask=mask, other=-float("inf"))
x = x - tl.max(x, axis=0)
num = tl.exp(x)
y = num / tl.sum(num, axis=0)
tl.store(out_ptr + row * out_row_stride + cols, y, mask=mask)
def softmax(x: torch.Tensor) -> torch.Tensor:
if x.ndim != 2 or not x.is_cuda:
raise ValueError(f"expected 2-D CUDA tensor, got shape={tuple(x.shape)} device={x.device}")
n_rows, n_cols = x.shape
out = torch.empty_like(x)
block = triton.next_power_of_2(n_cols)
num_warps = 4 if block <= 2048 else 8 if block <= 8192 else 16
softmax_kernel[(n_rows,)](out, x, x.stride(0), out.stride(0), n_cols,
BLOCK_SIZE=block, num_warps=num_warps)
return out
x = torch.randn(4096, 1000, device="cuda")
torch.testing.assert_close(softmax(x), torch.softmax(x, dim=1))
One HBM read and one write per element, versus several passes for unfused max/exp/sum/divide. This simple version assumes a row fits in one block; very long rows need a loop with online softmax.
Example: Autotuned Matmul (core loop)
@triton.autotune(
configs=[
triton.Config({"BLOCK_M": 128, "BLOCK_N": 128, "BLOCK_K": 32}, num_warps=8, num_stages=3),
triton.Config({"BLOCK_M": 64, "BLOCK_N": 128, "BLOCK_K": 32}, num_warps=4, num_stages=4),
triton.Config({"BLOCK_M": 128, "BLOCK_N": 64, "BLOCK_K": 64}, num_warps=4, num_stages=4),
],
key=["M", "N", "K"],
)
@triton.jit
def matmul_kernel(a_ptr, b_ptr, c_ptr, M, N, K,
stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn,
BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr):
pid_m = tl.program_id(0)
pid_n = tl.program_id(1)
offs_m = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
offs_n = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)
offs_k = tl.arange(0, BLOCK_K)
a_ptrs = a_ptr + offs_m[:, None] * stride_am + offs_k[None, :] * stride_ak
b_ptrs = b_ptr + offs_k[:, None] * stride_bk + offs_n[None, :] * stride_bn
acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32)
for k in range(0, tl.cdiv(K, BLOCK_K)):
k_rem = K - k * BLOCK_K
a = tl.load(a_ptrs, mask=(offs_m[:, None] < M) & (offs_k[None, :] < k_rem), other=0.0)
b = tl.load(b_ptrs, mask=(offs_k[:, None] < k_rem) & (offs_n[None, :] < N), other=0.0)
acc = tl.dot(a, b, acc) # tensor cores for fp16/bf16 inputs
a_ptrs += BLOCK_K
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл triton-kernels?
Use when the user asks about OpenAI Triton kernels, @triton.jit, tl.load, tl.dot, triton.autotune, fused kernels in Python or torch.compile Inductor kernels. Produces a kernel, PyTorch wrapper, correctness test and do_bench benchmark. Not for Triton Inference Server (use ml-model-serving) or CUDA C++ (use cuda-programming).
Как установить скилл triton-kernels?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл triton-kernels запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
mcp-builder
AnthropicGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
claude-api
AnthropicReference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
algorithmic-art
AnthropicCreating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
mcp-integration
AnthropicThis skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…