ワンクリックで
triton-ascend-basics
Triton Ascend 编程基础,包括核心概念(program_id、block、grid)、内核函数结构、装饰器用法和标准代码模式。适用使用 Triton Ascend、需要了解基本语法结构的任意内核代码生成场景
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Triton Ascend 编程基础,包括核心概念(program_id、block、grid)、内核函数结构、装饰器用法和标准代码模式。适用使用 Triton Ascend、需要了解基本语法结构的任意内核代码生成场景
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
矩阵乘法矩阵乘法 A[M, K] @ B[K, N] = C[M, N]中,大K维度矩阵乘法(K>>M,N)优化:针对M/N较小但K极大(如M=N=256,K=131072)的场景,Split-K切分K维度并行化、Workspace+Reduce替代全局同步,实现显著性能提升
Triton Ascend hard API restrictions and forbidden syntax. MUST-follow rules that apply to every kernel: forbidden control flow (return/break/continue/lambda/while), tensor slice/index restrictions, scalar conversion rules, BLOCK_SIZE upper bound. Violating any of these produces a compile or runtime error on Ascend.
Triton Ascend 性能优化通用策略: BLOCK_SIZE 选择 (1024-2048 for elementwise, must be <65536), grid configuration (use VEC_CORE_NUM / CUBE_CORE_NUM, 2D/3D grid for matmul / conv / reduce, 1D grid + inner loop for elementwise / pointwise), 256B alignment for memory transfers, autotune block-size patterns, fp16 / fp32 precision conversion. Bind via keywords like matmul, elementwise, reduce, block_size, grid, autotune, alignment, fp16, fp32, tile, interleaved-loop, cube-core, vec-core.
通过 adaptive_search 或 evolve 搜索式 workflow 生成优化算子。 后台 silent mode 执行,轮询监控进度。
适用于归约(reduce)类算子和含归约子步骤的复合算子(如归一化)的优化指南。典型算子包括:sum, mean, max, min, prod, argmax, argmin, cumsum, cumprod, softmax, logsoftmax, layernorm, rmsnorm, groupnorm, instancenorm, batchnorm, l1norm, l2norm, frobeniusnorm, var, std, average_pooling, sum_pooling 等。特别重要:当归约维度不是最后一维(如 dim=1 归约 shape=[B,F,D1,D2]),需要正确处理多维索引和两阶段归约。包含 PyTorch normalized_shape 多轴归一化语义说明。不适用于纯逐元素运算或矩阵乘法。如果算子是损失函数(先逐元素计算再全局归约),应选择 elementwise-reduce-fused 指南。
CPU C++ 算子核心概念、标准结构模式、KernelBench 代码规范和内嵌扩展方法
| name | triton-ascend-basics |
| description | Triton Ascend 编程基础,包括核心概念(program_id、block、grid)、内核函数结构、装饰器用法和标准代码模式。适用使用 Triton Ascend、需要了解基本语法结构的任意内核代码生成场景 |
| category | fundamental |
| version | 1.0.0 |
| metadata | {"backend":"ascend","dsl":"triton_ascend","hardware":"Atlas A2, Atlas A3","operator_patterns":"all"} |
@triton.jit
def kernel(
output_ptr, input_ptr, n_elements,
BLOCK_SIZE: tl.constexpr, CORE_NUM: tl.constexpr,
):
pid = tl.program_id(0)
num_blocks = tl.cdiv(n_elements, BLOCK_SIZE)
for block_id in range(pid, num_blocks, CORE_NUM):
offsets = block_id * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
data = tl.load(input_ptr + offsets, mask=mask, other=0.0)
result = compute(data)
tl.store(output_ptr + offsets, result, mask=mask)
class ModelNew(torch.nn.Module):
def __init__(self):
super().__init__()
try:
self.VEC_CORE_NUM = torch_npu.npu.npu_config.get_device_limit(0).get("vector_core_num", 40)
except:
self.VEC_CORE_NUM = 40
def forward(self, x):
out = torch.empty_like(x)
BLOCK_SIZE = 1024
grid = (self.VEC_CORE_NUM,) # Ascend: 固定为核心数
kernel[grid](out, x, x.numel(), BLOCK_SIZE=BLOCK_SIZE, CORE_NUM=self.VEC_CORE_NUM)
return out
offsets = block_id * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
data = tl.load(ptr + offsets, mask=mask, other=0.0)
result = tl.where(condition, true_val, false_val)
Autotune 通过自动 benchmark 多组配置参数,找到当前硬件和数据规模下的最优配置并缓存,免去手动调参。
key 参数缓存最佳 config,动态 shape 下每组新 shape 都会触发一次完整 benchmark,反而严重拖慢性能restore_value:列出 kernel 的所有输出指针参数名。autotune benchmark 会对每个 config 反复执行 kernel,restore_value 在每次迭代前保存输出张量副本、迭代后恢复原值,防止不同 config 之间的结果互相污染。不写 restore_value 会导致验证失败。PARAM: tl.constexpr。# 正确写法:有 restore_value,grid 固定核心数
@triton.autotune(
configs=[
triton.Config({'BLOCK_SIZE': 1024}),
triton.Config({'BLOCK_SIZE': 512}),
],
key=['n_elements'],
restore_value=['output_ptr'], # ⚠ 必须:列出所有输出指针参数名
)
@triton.jit
def kernel(input_ptr, output_ptr, n_elements,
BLOCK_SIZE: tl.constexpr):
pass
# Ascend: grid 固定为核心数
grid = (VEC_CORE_NUM,)
kernel[grid](input_ptr, output_ptr, n_elements)
# 错误:缺少 restore_value → CodeChecker 会拦截,验证会失败
@triton.autotune(
configs=[...],
key=[...],
)
@triton.jit
def kernel(input_ptr, output_ptr, ...):
pass
grid = lambda meta: (...)Ascend NPU 有两类计算核心,必须根据算子类型正确选择:
硬约束:涉及 tl.dot / 矩阵乘法运算的算子必须使用 CUBE_CORE_NUM,混合运算(先 matmul 再 elementwise 后处理)也使用 CUBE_CORE_NUM。核心数获取代码和详细策略见 grid-config 文档。
torch.empty / torch.empty_like(避免 zeros/ones 初始化开销)torch.empty_like() 创建的输出默认连续以下 API 在 CUDA Triton 中存在,但在 Ascend Triton 中不支持,使用会导致编译错误:
| 不支持的 API | 替代方案 |
|---|---|
tl.any / tl.all | tl.sum(mask.to(tl.int32)) > 0 |
tl.histogram | 手动实现分桶逻辑 |
tl.sort | 手动排序或分阶段比较 |
tl.gather / tl.scatter (部分) | tl.load / tl.store + 索引计算 |
num_warps / num_ctas / num_stages (autotune 参数) | Ascend 不需要,忽略即可 |