Triton CUDA 编程模式
3.1 向量操作模式
适用于元素级运算:加法、乘法、激活函数等。
标准代码结构
@triton.jit
def vector_add_kernel(a_ptr, b_ptr, c_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
pid = tl.program_id(0)
offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
a = tl.load(a_ptr + offsets, mask=mask)
b = tl.load(b_ptr + offsets, mask=mask)
c = a + b
tl.store(c_ptr + offsets, c, mask=mask)
适用算子
- 算术运算: add, mul, sub, div
- 激活函数: relu, sigmoid, tanh(需用
tl.extra.cuda.libdevice.tanh), gelu
- 数学函数: exp, log, sqrt, pow
关键要点
- 使用一维索引和偏移
- 边界处理用
mask
- 简单直接的数据流:加载 → 计算 → 存储
3.2 归约模式
适用于求和、最大值、最小值等聚合操作。
标准代码结构
@triton.jit
def reduction_kernel(input_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
pid = tl.program_id(0)
offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
# 加载数据
data = tl.load(input_ptr + offsets, mask=mask, other=0.0)
# 块内归约
block_sum = tl.sum(data, axis=0)
# 原子操作写回全局内存
tl.atomic_add(output_ptr, block_sum)
适用算子
- 基础归约: sum, mean, max, min
- 归一化: softmax, logsoftmax, layernorm, batchnorm
- 统计: variance, std
关键要点
- 块内归约:使用
tl.sum, tl.max 等
- 原子操作:使用
tl.atomic_add 等写回全局内存
- 数值稳定性:减去最大值防止溢出(见 triton-cuda-reduce)
3.3 矩阵乘法模式
适用于矩阵乘法等多维块计算。
标准代码结构
@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_SIZE_M: tl.constexpr,
BLOCK_SIZE_N: tl.constexpr,
BLOCK_SIZE_K: tl.constexpr,
):
# 获取程序 ID
pid_m = tl.program_id(0)
pid_n = tl.program_id(1)
# 初始化累加器
accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
# K 维度循环
for k in range(0, K, BLOCK_SIZE_K):
# 创建块指针
a_block_ptr = tl.make_block_ptr(
base=a_ptr, shape=(M, K), strides=(stride_am, stride_ak),
offsets=(pid_m * BLOCK_SIZE_M, k),
block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_K), order=(1, 0)
)
b_block_ptr = tl.make_block_ptr(
base=b_ptr, shape=(K, N), strides=(stride_bk, stride_bn),
offsets=(k, pid_n * BLOCK_SIZE_N),
block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_N), order=(1, 0)
)
# 加载数据块
a = tl.load(a_block_ptr, boundary_check=(0, 1))
b = tl.load(b_block_ptr, boundary_check=(0, 1))
# 矩阵乘累加
accumulator += tl.dot(a, b)
# 存储结果(需显式转换类型,匹配输出 dtype)
c = accumulator.to(c_ptr.dtype.element_ty)
c_block_ptr = tl.make_block_ptr(
base=c_ptr, shape=(M, N), strides=(stride_cm, stride_cn),
offsets=(pid_m * BLOCK_SIZE_M, pid_n * BLOCK_SIZE_N),
block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_N), order=(1, 0)
)
tl.store(c_block_ptr, c, boundary_check=(0, 1))
Host 侧启动
class ModelNew(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, a, b):
M, K = a.shape
K2, N = b.shape
assert K == K2
c = torch.empty((M, N), device=a.device, dtype=a.dtype)
BLOCK_M, BLOCK_N, BLOCK_K = 128, 256, 64
grid = (triton.cdiv(M, BLOCK_M), triton.cdiv(N, BLOCK_N))
matmul_kernel[grid](
a, b, c, M, N, K,
a.stride(0), a.stride(1),
b.stride(0), b.stride(1),
c.stride(0), c.stride(1),
BLOCK_SIZE_M=BLOCK_M,
BLOCK_SIZE_N=BLOCK_N,
BLOCK_SIZE_K=BLOCK_K,
)
return c
适用算子
- 矩阵运算: matmul, bmm (batch matmul), linear
- 卷积: conv2d, conv3d(im2col 变换后)
- 其他多维计算
关键要点
- 2D Grid: 使用
grid=(grid_m, grid_n) 二维并行
- 分块计算: 将大矩阵分成小块,减少内存占用
- K 维度循环: 累加多个部分乘积
- block_ptr: 使用
tl.make_block_ptr 简化 2D 数据访问
- Tensor Core: 使用
tl.dot 自动利用 Tensor Core
模式选择指南
| 算子类型 |
推荐模式 |
关键特征 |
| Element-wise |
向量操作模式 |
逐元素独立计算 |
| Reduction |
归约模式 |
需要聚合多个值 |
| MatMul/Conv |
矩阵乘法模式 |
多维块计算,2D Grid |
| Attention |
归约 + 矩阵乘法 |
组合模式,见 triton-cuda-attention |
最佳实践
- 选择合适的模式: 根据算子特性选择基础模式
- 优化块大小: 平衡并行度和资源占用
- 注意边界: 使用 mask 处理不规则形状
- 数值稳定性: 对于 reduce 类算子特别注意
- 内存访问: 优化数据布局,提高缓存命中率
- 利用硬件特性: 使用 num_warps/num_stages 优化流水线
1---2name: triton-cuda-patterns3description: "Triton CUDA 三大核心编程模式(向量/逐元素、归约、矩阵乘法)的标准实现范式和代码模板。适用于需要快速确定算子属于哪种编程模式、或需要了解各模式基本代码结构的 CUDA 内核代码生成场景"4category: method5version: "1.0.0"6metadata:7 source: https://skillmd.com/skills/wenyi-li/triton-cuda-patterns8 backend: cuda9 dsl: triton_cuda10 operator_patterns: "elementwise, reduce, matmul"11structure:12 child_skills:13 - triton-cuda-elementwise14 - triton-cuda-reduce15 - triton-cuda-matmul16---1718# Triton CUDA 编程模式1920## 3.1 向量操作模式2122适用于元素级运算:加法、乘法、激活函数等。2324### 标准代码结构2526```python27@triton.jit28def vector_add_kernel(a_ptr, b_ptr, c_ptr, n_elements, BLOCK_SIZE: tl.constexpr):29 pid = tl.program_id(0)30 offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)31 mask = offsets < n_elements32 33 a = tl.load(a_ptr + offsets, mask=mask)34 b = tl.load(b_ptr + offsets, mask=mask)35 c = a + b36 37 tl.store(c_ptr + offsets, c, mask=mask)38```3940### 适用算子41- 算术运算: add, mul, sub, div42- 激活函数: relu, sigmoid, tanh(需用 `tl.extra.cuda.libdevice.tanh`), gelu43- 数学函数: exp, log, sqrt, pow4445### 关键要点46- 使用一维索引和偏移47- 边界处理用 `mask`48- 简单直接的数据流:加载 → 计算 → 存储4950## 3.2 归约模式5152适用于求和、最大值、最小值等聚合操作。5354### 标准代码结构5556```python57@triton.jit58def reduction_kernel(input_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr):59 pid = tl.program_id(0)60 offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)61 mask = offsets < n_elements62 63 # 加载数据64 data = tl.load(input_ptr + offsets, mask=mask, other=0.0)65 66 # 块内归约67 block_sum = tl.sum(data, axis=0)68 69 # 原子操作写回全局内存70 tl.atomic_add(output_ptr, block_sum)71```7273### 适用算子74- 基础归约: sum, mean, max, min75- 归一化: softmax, logsoftmax, layernorm, batchnorm76- 统计: variance, std7778### 关键要点79- 块内归约:使用 `tl.sum`, `tl.max` 等80- 原子操作:使用 `tl.atomic_add` 等写回全局内存81- 数值稳定性:减去最大值防止溢出(见 triton-cuda-reduce)8283## 3.3 矩阵乘法模式8485适用于矩阵乘法等多维块计算。8687### 标准代码结构8889```python90@triton.jit91def matmul_kernel(92 a_ptr, b_ptr, c_ptr,93 M, N, K,94 stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn,95 BLOCK_SIZE_M: tl.constexpr,96 BLOCK_SIZE_N: tl.constexpr,97 BLOCK_SIZE_K: tl.constexpr,98):99 # 获取程序 ID100 pid_m = tl.program_id(0)101 pid_n = tl.program_id(1)102103 # 初始化累加器104 accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)105106 # K 维度循环107 for k in range(0, K, BLOCK_SIZE_K):108 # 创建块指针109 a_block_ptr = tl.make_block_ptr(110 base=a_ptr, shape=(M, K), strides=(stride_am, stride_ak),111 offsets=(pid_m * BLOCK_SIZE_M, k),112 block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_K), order=(1, 0)113 )114 b_block_ptr = tl.make_block_ptr(115 base=b_ptr, shape=(K, N), strides=(stride_bk, stride_bn),116 offsets=(k, pid_n * BLOCK_SIZE_N),117 block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_N), order=(1, 0)118 )119120 # 加载数据块121 a = tl.load(a_block_ptr, boundary_check=(0, 1))122 b = tl.load(b_block_ptr, boundary_check=(0, 1))123124 # 矩阵乘累加125 accumulator += tl.dot(a, b)126127 # 存储结果(需显式转换类型,匹配输出 dtype)128 c = accumulator.to(c_ptr.dtype.element_ty)129 c_block_ptr = tl.make_block_ptr(130 base=c_ptr, shape=(M, N), strides=(stride_cm, stride_cn),131 offsets=(pid_m * BLOCK_SIZE_M, pid_n * BLOCK_SIZE_N),132 block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_N), order=(1, 0)133 )134 tl.store(c_block_ptr, c, boundary_check=(0, 1))135```136137### Host 侧启动138139```python140class ModelNew(torch.nn.Module):141 def __init__(self):142 super().__init__()143144 def forward(self, a, b):145 M, K = a.shape146 K2, N = b.shape147 assert K == K2148 c = torch.empty((M, N), device=a.device, dtype=a.dtype)149150 BLOCK_M, BLOCK_N, BLOCK_K = 128, 256, 64151152 grid = (triton.cdiv(M, BLOCK_M), triton.cdiv(N, BLOCK_N))153154 matmul_kernel[grid](155 a, b, c, M, N, K,156 a.stride(0), a.stride(1),157 b.stride(0), b.stride(1),158 c.stride(0), c.stride(1),159 BLOCK_SIZE_M=BLOCK_M,160 BLOCK_SIZE_N=BLOCK_N,161 BLOCK_SIZE_K=BLOCK_K,162 )163 return c164```165166### 适用算子167- 矩阵运算: matmul, bmm (batch matmul), linear168- 卷积: conv2d, conv3d(im2col 变换后)169- 其他多维计算170171### 关键要点172- **2D Grid**: 使用 `grid=(grid_m, grid_n)` 二维并行173- **分块计算**: 将大矩阵分成小块,减少内存占用174- **K 维度循环**: 累加多个部分乘积175- **block_ptr**: 使用 `tl.make_block_ptr` 简化 2D 数据访问176- **Tensor Core**: 使用 `tl.dot` 自动利用 Tensor Core177178## 模式选择指南179180| 算子类型 | 推荐模式 | 关键特征 |181|---------|---------|---------|182| Element-wise | 向量操作模式 | 逐元素独立计算 |183| Reduction | 归约模式 | 需要聚合多个值 |184| MatMul/Conv | 矩阵乘法模式 | 多维块计算,2D Grid |185| Attention | 归约 + 矩阵乘法 | 组合模式,见 triton-cuda-attention |186187## 最佳实践1881891. **选择合适的模式**: 根据算子特性选择基础模式1902. **优化块大小**: 平衡并行度和资源占用1913. **注意边界**: 使用 mask 处理不规则形状1924. **数值稳定性**: 对于 reduce 类算子特别注意1935. **内存访问**: 优化数据布局,提高缓存命中率1946. **利用硬件特性**: 使用 num_warps/num_stages 优化流水线