Triton cuda api

From mindspore-ai/akg · View interactive version

Triton cuda api is an AI agent skill from the GitHub repository mindspore-ai/akg. Triton CUDA API 完整参考手册,包括 tl.load/store、tl.reduce、tl.dot、tl.atomic 等核心函数的签名、参数和使用示例。适用于需要查阅具体 API 用法、了解函数参数含义的任意 Triton CUDA 内核代码生成场景

How to use this skill

Skill file

Triton CUDA API 参考手册

本文档提供 Triton 核心 API 的详细参考,包括函数签名、参数说明和使用示例。

1. 内核装饰器

@triton.jit

@triton.jit
def kernel_function(...):
    pass

2. 程序 ID 与网格 API

tl.program_id(axis)

pid = tl.program_id(axis)  # axis: 0, 1, or 2

tl.num_programs(axis)

num_pids = tl.num_programs(axis)  # axis: 0, 1, or 2

triton.cdiv(a, b)

grid_size = triton.cdiv(total_elements, block_size)

3. 内存操作 API

tl.load(pointer, mask=None, other=None, boundary_check=None)

data = tl.load(ptr + offsets, mask=mask, other=0.0)

tl.store(pointer, value, mask=None, boundary_check=None)

tl.store(ptr + offsets, result, mask=mask)

tl.make_block_ptr(base, shape, strides, offsets, block_shape, order)

block_ptr = tl.make_block_ptr(
    base=ptr,                    # 基础指针
    shape=(M, N),                # 完整矩阵形状
    strides=(stride_m, stride_n), # 步长
    offsets=(start_m, start_n),   # 当前块偏移
    block_shape=(BLOCK_M, BLOCK_N), # 块形状
    order=(1, 0)                 # 内存布局顺序
)

tl.advance(ptr, offsets)

block_ptr = tl.advance(block_ptr, (BLOCK_M, 0))

4. 张量创建与操作 API

tl.arange(start, end)

offsets = tl.arange(0, BLOCK_SIZE)

tl.zeros(shape, dtype)

accumulator = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32)

tl.full(shape, value, dtype)

ones = tl.full((M, N), 1.0, dtype=tl.float32)

tl.cast(input, dtype)

float_data = tl.cast(int_data, tl.float32)

5. 数学运算 API

tl.dot(a, b, acc=None, allow_tf32=True)

result = tl.dot(a, b, acc=accumulator)

tl.sum(x, axis)

block_sum = tl.sum(data, axis=0)

tl.max(x, axis)

max_val = tl.max(data, axis=0)

tl.min(x, axis)

min_val = tl.min(data, axis=0)

tl.where(condition, x, y)

result = tl.where(mask, data, 0.0)

tl.exp(x) / tl.log(x) / tl.sqrt(x)

exp_val = tl.exp(x)
log_val = tl.log(x)
sqrt_val = tl.sqrt(x)

tl.sigmoid(x)

sigmoid_val = tl.sigmoid(x)

tl.extra.cuda.libdevice.tanh(x)

tanh_val = tl.extra.cuda.libdevice.tanh(x)

tl.math.exp2(x) / tl.math.log2(x)

exp2_val = tl.math.exp2(x)
log2_val = tl.math.log2(x)

tl.cumsum(input, axis=0, reverse=False, dtype=None)

cumulative_sum = tl.cumsum(data, axis=0)
reverse_cumsum = tl.cumsum(data, axis=1, reverse=True)

tl.cumprod(input, axis=0, reverse=False)

cumulative_prod = tl.cumprod(data, axis=0)

6. 原子操作 API

tl.atomic_add(pointer, value)

tl.atomic_add(output_ptr, block_sum)

tl.atomic_max(pointer, value)

tl.atomic_max(max_ptr, local_max)

tl.atomic_min(pointer, value)

tl.atomic_min(min_ptr, local_min)

tl.atomic_cas(pointer, cmp, val)

old = tl.atomic_cas(ptr, expected, desired)

tl.constexpr

BLOCK_SIZE: tl.constexpr = 1024

7. 块分配优化 API

tl.swizzle2d(i, j, size_i, size_j, group_size)

task_i, task_j = tl.swizzle2d(block_i, block_j, NUM_BLOCKS_I, NUM_BLOCKS_J, GROUP_SIZE)

使用建议

  1. 内存操作: 优先使用 tl.make_block_ptr 处理 2D 数据
  2. 边界检查: 始终使用 mask 或 boundary_check 防止越界
  3. 原子操作: 仅在必要时使用,有性能开销
  4. 数据类型: 注意类型转换,使用 tl.cast 显式转换
  5. 编译优化: 使用 tl.constexpr 标记常量参数
  6. Tensor Core: MatMul 类操作使用 allow_tf32=True 利用 Tensor Core
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.