Converts a PyTorch operator into a Triton Ascend NPU-backed PyTorch operator, preserving the input-helper block and validating via standalone or differential testing.
Ships in triton-optimizer-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Convert one PyTorch operator file into a PyTorch-facing operator backed by a real Triton Ascend NPU kernel path.
Use this skill when the user wants a new converted operator artifact instead of an in-place optimize round.
triton_<origin-name>.pyFor test generation, benchmark generation, validation execution, repair heuristics, and other supporting functions, use the triton-npu-optimize skill. See its function index for all available capabilities.
triton-npu-optimize skill's gen-test function to generate a test for the converted output.triton-npu-optimize skill's run-eval function validation — run the appropriate validation command (run-test-optimize for differential mode, run-test-baseline for standalone mode) as prescribed by the skill. The command output must contain PASS: for validation to be considered successful. See "Validation Enforcement Rules" below for the complete set of mandatory validation constraints.triton-npu-optimize skill's repair-guide function heuristics and then re-run validation.Correctness validation is not advisory — it is the final gate before conversion is considered complete. The following rules are mandatory and non-negotiable.
You MUST use the validation commands prescribed by the triton-npu-optimize skill's run-eval function:
cli.py run-test-optimize with --baseline-operator-file <original>cli.py run-test-baselineThese commands handle result archiving, TRITON_ALWAYS_COMPILE, NPU synchronization, and comparison — all of which you cannot replicate reliably with ad-hoc scripting.
The following self-validation patterns are strictly forbidden. Violating any of these means the conversion is incomplete, regardless of what other checks you believe have passed.
| Forbidden | Example | Why |
|---|---|---|
Ad-hoc python3 -c "import torch; ..." comparison scripts | python3 -c "import torch; ref=torch.load(...); torch.allclose(...)" | Bypasses the standard compare_result.py and its audited tolerance levels |
Using torch.allclose / torch.testing.assert_close with custom tolerances | torch.allclose(a, b, rtol=1e-5, atol=1e-8) | Tolerances different from the NPU accuracy contract thresholds may mask real errors or produce false positives |
Running a differential test file directly with python3 instead of through cli.py | python3 differential_test_xxx.py --operator-file xxx | Bypasses result archiving, TRITON_ALWAYS_COMPILE, synchronization, and comparison logic |
Generating and comparing custom .pt files on your own | torch.save(ref, "REFERENCE_RESULT.pt") then manual compare | Creates files that may interfere with the CLI validation loop and use inconsistent formats |
| Self-declaring "PASS" based on your own comparison logic | "PASS: Minor 1-ULP differences only (expected for cross-implementation)" | Only run-test-optimize / run-test-baseline output determines pass/fail |
PASS: all N case(s) matched the NPU accuracy contract. (or per-case PASS case '...' matched ...) — differential mode conversion is definitively validatedAll tests passed! or matching All tests passed! in the command output — standalone mode has passedFAIL:, Python exceptions, or empty output) — conversion has NOT passedPASS: or All tests passed!, do NOT declare success. Instead, use the failure output to diagnose and fix the kernel, then re-run the same validation command.Use a real converted output example in the generated file, not only a prose description. For a simple elementwise add conversion, a converted output may look like this:
import torch
import torch.nn as nn
import triton
import triton.language as tl
@triton.jit
def add_kernel(
x_ptr,
y_ptr,
out_ptr,
n_elements,
BLOCK_SIZE: tl.constexpr,
):
block_start = tl.program_id(0) * BLOCK_SIZE
offsets = block_start + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
x = tl.load(x_ptr + offsets, mask=mask, other=0.0)
y = tl.load(y_ptr + offsets, mask=mask, other=0.0)
out = x + y
tl.store(out_ptr + offsets, out, mask=mask)
def triton_add(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
x = x.contiguous()
y = y.contiguous()
out = torch.empty_like(x)
n_elements = out.numel()
grid = lambda meta: ((n_elements + meta["BLOCK_SIZE"] - 1) // meta["BLOCK_SIZE"],)
add_kernel[grid](x, y, out, n_elements, BLOCK_SIZE=128)
return out
class Model(nn.Module):
def __init__(self) -> None:
super().__init__()
def forward(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
return triton_add(a, b)
N = 2048
def get_inputs():
a = torch.randn(N, N, device="npu")
b = torch.randn(N, N, device="npu")
return [a, b]
def get_init_inputs():
return []
In this kind of conversion:
def triton_add(...) is the PyTorch-facing wrapper that calls the Triton kernelclass Model is the converted public architectureget_init_inputs() / get_inputs() block is preserved in the converted output instead of being droppedThe converted operator must be a pure Triton Ascend implementation. The forward() method may only allocate buffers and launch kernels — all computation must happen inside @triton.jit kernels.
| Category | Examples | Reason |
|---|---|---|
torch compute functions | torch.matmul(x, w), torch.relu(x), torch.sum(x) | Must be inside a @triton.jit kernel |
torch.nn.functional | F.softmax(x, dim=-1), F.linear(x, w), F.relu(x) | Must be inside a @triton.jit kernel |
| tensor method compute | x.sum(), x.mean(), x.softmax(dim=-1), x.relu() | Must be inside a @triton.jit kernel |
| tensor operators | x @ w, x + y, x * y, x / y | Must be inside a @triton.jit kernel |
nn.Module calls | self.conv(x), self.linear(x), self.layer(x) | Must be inside a @triton.jit kernel |
| Category | Examples | Purpose |
|---|---|---|
| buffer alloc | torch.empty(shape), torch.zeros(shape), torch.ones(shape) | Allocate output for kernel |
| shape ops | x.view(...), x.reshape(...), x.permute(...), x.transpose(...) | No compute involved |
| metadata | x.shape, x.dtype, x.device, x.numel() | Needed for grid calculation |
| kernel launch | kernel[grid](...args) | Call a custom @triton.jit kernel |
1. Fully PyTorch — no kernel at all
# Forbidden: pure PyTorch, no Triton kernel
def forward(self, x, w):
return torch.matmul(x, w)
2. Kernel defined but never called
@triton.jit
def matmul_kernel(...):
pass
def forward(self, x, w):
return torch.matmul(x, w) # Forbidden: kernel defined but unused
3. Mixed: partial kernel + partial torch
def forward(self, x, w):
y = self.kernel[grid](x, w)
return y.sum(dim=-1) # Forbidden: tensor method compute after kernel
4. Tensor operators in forward
def forward(self, x, w):
y = self.kernel[grid](x, w)
return y + 1 # Forbidden: + is a PyTorch operator
@triton.jit
def add_kernel(x_ptr, y_ptr, output_ptr, n, BLOCK_SIZE: tl.constexpr):
idx = tl.arange(0, BLOCK_SIZE)
x = tl.load(x_ptr + idx)
y = tl.load(y_ptr + idx)
output = x + y # compute inside kernel
tl.store(output_ptr + idx, output)
class Model(nn.Module):
def forward(self, x, y):
output = torch.empty_like(x) # Allowed: buffer alloc
add_kernel[(1,)](x, y, output, x.numel(), BLOCK_SIZE=128) # Allowed: kernel launch
return output # Allowed: return kernel output
.dtype, .ndim, .device, .shape, .numel()). Never scan tensor data for bounds or value-range validation — calling .min().item(), .max().item(), or any reduction+.item() on input tensors forces a GPU→CPU synchronization on every forward call and destroys performance. The caller is responsible for providing valid inputs, just as it is for the original PyTorch operator.optimize or create opt-round-* directories from this workflow.baseline/ or any optimize-session artifacts from this workflow._validate_index, _check_bounds, _assert_indices, or similarly-named functions) that scan tensor data. Specifically, never call .min().item(), .max().item(), .sum().item(), or any reduction followed by .item() on GPU/NPU tensors before launching a kernel. These force a full-tensor GPU→CPU synchronization on every forward call. The converted operator inherits the same input contract as the original PyTorch operator — if the caller passes out-of-bounds indices, that is a caller bug, not something the conversion must guard against.python3 -c "import torch; ...") to compare result .pt files or operator outputs.torch.allclose, torch.testing.assert_close, torch.equal, or any other numerical comparison function with custom tolerances to validate conversion correctness — always use run-test-optimize or run-test-baseline from the triton-npu-optimize skill's run-eval function.run-test-optimize or run-test-baseline determines success.python3 — always use cli.py run-test-optimize or cli.py run-test-baseline..pt files (e.g., REFERENCE_RESULT.pt, COMPARE_RESULT.pt) for manual comparison — this may interfere with the CLI validation loop's result caching.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/triton-optimizer-skills:triton-npu-convertfrom triton-optimizer-skills
npx claudepluginhub catman626/cannbot-skills --plugin triton-optimizer-skills2 skills in this plugin
2 plugins in one repository ship this exact skill · first indexed Oct 20, 2026
Other plugins' skills for the same job