Softmax — Triton Ascend 实现示例
import torch
import triton
import triton.language as tl
@triton.jit
def softmax_kernel(
X_ptr, Y_ptr,
B: tl.constexpr, N: tl.constexpr,
stride_xb: tl.constexpr, stride_xn: tl.constexpr,
stride_yb: tl.constexpr, stride_yn: tl.constexpr,
BLOCK_SIZE_N: tl.constexpr, CORE_NUM: tl.constexpr,
):
pid = tl.program_id(0)
for b in range(pid, B, CORE_NUM):
# Phase 1: max
max_val = -float('inf')
for off in range(0, N, BLOCK_SIZE_N):
n_off = off + tl.arange(0, BLOCK_SIZE_N)
mask = n_off < N
x = tl.load(X_ptr + b * stride_xb + n_off * stride_xn,
mask=mask, other=-float('inf'))
max_val = tl.maximum(max_val, tl.max(x, axis=0))
# Phase 2: sum(exp(x - max))
sum_val = 0.0
for off in range(0, N, BLOCK_SIZE_N):
n_off = off + tl.arange(0, BLOCK_SIZE_N)
mask = n_off < N
x = tl.load(X_ptr + b * stride_xb + n_off * stride_xn,
mask=mask, other=0.0)
exp_x = tl.math.exp(x - max_val)
sum_val += tl.sum(exp_x, axis=0).to(tl.float32)
# Phase 3: normalize
for off in range(0, N, BLOCK_SIZE_N):
n_off = off + tl.arange(0, BLOCK_SIZE_N)
mask = n_off < N
x = tl.load(X_ptr + b * stride_xb + n_off * stride_xn,
mask=mask, other=0.0)
result = tl.math.exp(x - max_val) / sum_val
tl.store(Y_ptr + b * stride_yb + n_off * stride_yn,
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):
if not x.is_contiguous():
x = x.contiguous()
B, N = x.shape
y = torch.empty_like(x)
BLOCK_SIZE_N = 4096
grid = (self.VEC_CORE_NUM,)
softmax_kernel[grid](
x, y, B, N,
x.stride(0), x.stride(1), y.stride(0), y.stride(1),
BLOCK_SIZE_N=BLOCK_SIZE_N, CORE_NUM=self.VEC_CORE_NUM)
return y
1---2name: triton-ascend-example-softmax3description: "Softmax 归约算子的完整 Triton Ascend 实现示例。展示三阶段归约模式(求 max → 求 sum(exp) → 归一化)、分块累加、标量累加器精度提升等技巧。当生成 reduce 类算子时可参考此示例的代码结构。"4category: example5version: "1.0.0"6metadata:7 source: https://skillmd.com/skills/wenyi-li/triton-ascend-example-softmax8 backend: ascend9 dsl: triton_ascend10 hardware: "Atlas A2, Atlas A3"11 operator_type: "reduce"12---1314# Softmax — Triton Ascend 实现示例1516```python17import torch18import triton19import triton.language as tl202122@triton.jit23def softmax_kernel(24 X_ptr, Y_ptr,25 B: tl.constexpr, N: tl.constexpr,26 stride_xb: tl.constexpr, stride_xn: tl.constexpr,27 stride_yb: tl.constexpr, stride_yn: tl.constexpr,28 BLOCK_SIZE_N: tl.constexpr, CORE_NUM: tl.constexpr,29):30 pid = tl.program_id(0)31 for b in range(pid, B, CORE_NUM):32 # Phase 1: max33 max_val = -float('inf')34 for off in range(0, N, BLOCK_SIZE_N):35 n_off = off + tl.arange(0, BLOCK_SIZE_N)36 mask = n_off < N37 x = tl.load(X_ptr + b * stride_xb + n_off * stride_xn,38 mask=mask, other=-float('inf'))39 max_val = tl.maximum(max_val, tl.max(x, axis=0))4041 # Phase 2: sum(exp(x - max))42 sum_val = 0.043 for off in range(0, N, BLOCK_SIZE_N):44 n_off = off + tl.arange(0, BLOCK_SIZE_N)45 mask = n_off < N46 x = tl.load(X_ptr + b * stride_xb + n_off * stride_xn,47 mask=mask, other=0.0)48 exp_x = tl.math.exp(x - max_val)49 sum_val += tl.sum(exp_x, axis=0).to(tl.float32)5051 # Phase 3: normalize52 for off in range(0, N, BLOCK_SIZE_N):53 n_off = off + tl.arange(0, BLOCK_SIZE_N)54 mask = n_off < N55 x = tl.load(X_ptr + b * stride_xb + n_off * stride_xn,56 mask=mask, other=0.0)57 result = tl.math.exp(x - max_val) / sum_val58 tl.store(Y_ptr + b * stride_yb + n_off * stride_yn,59 result, mask=mask)606162class ModelNew(torch.nn.Module):63 def __init__(self):64 super().__init__()65 try:66 self.VEC_CORE_NUM = torch_npu.npu.npu_config.get_device_limit(0).get("vector_core_num", 40)67 except:68 self.VEC_CORE_NUM = 406970 def forward(self, x):71 if not x.is_contiguous():72 x = x.contiguous()73 B, N = x.shape74 y = torch.empty_like(x)75 BLOCK_SIZE_N = 409676 grid = (self.VEC_CORE_NUM,)77 softmax_kernel[grid](78 x, y, B, N,79 x.stride(0), x.stride(1), y.stride(0), y.stride(1),80 BLOCK_SIZE_N=BLOCK_SIZE_N, CORE_NUM=self.VEC_CORE_NUM)81 return y82```