LayerNorm — Triton Ascend 实现示例
import torch
import triton
import triton.language as tl
@triton.jit
def layernorm_kernel(
X_ptr, Y_ptr,
batch_size: tl.constexpr, feature_size: tl.constexpr,
eps: tl.constexpr,
BLOCK_SIZE: tl.constexpr, CORE_NUM: tl.constexpr,
):
core_id = tl.program_id(0)
for batch_idx in range(core_id, batch_size, CORE_NUM):
batch_offset = batch_idx * feature_size
# Phase 1: compute mean & variance
mean_acc = 0.0
var_acc = 0.0
for i in range(0, feature_size, BLOCK_SIZE):
offsets = batch_offset + i + tl.arange(0, BLOCK_SIZE)
mask = offsets < batch_offset + feature_size
x = tl.load(X_ptr + offsets, mask=mask, other=0.0)
mean_acc += tl.sum(x, axis=0)
var_acc += tl.sum(x * x, axis=0)
mean_val = mean_acc / feature_size
std_val = tl.sqrt(var_acc / feature_size - mean_val * mean_val + eps)
# Phase 2: normalize
for i in range(0, feature_size, BLOCK_SIZE):
offsets = batch_offset + i + tl.arange(0, BLOCK_SIZE)
mask = offsets < batch_offset + feature_size
x = tl.load(X_ptr + offsets, mask=mask, other=0.0)
tl.store(Y_ptr + offsets, (x - mean_val) / std_val, 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):
shape = x.shape
batch_size = shape[0]
feature_size = 1
for s in shape[1:]:
feature_size *= s
if not x.is_contiguous():
x = x.contiguous()
y = torch.empty_like(x)
BLOCK_SIZE = 1024
grid = (self.VEC_CORE_NUM,)
layernorm_kernel[grid](x, y, batch_size, feature_size, 1e-5,
BLOCK_SIZE=BLOCK_SIZE, CORE_NUM=self.VEC_CORE_NUM)
return y
1---2name: triton-ascend-example-layernorm3description: "LayerNorm 归约算子的完整 Triton Ascend 实现示例。展示两阶段归约模式(统计量计算 → 归一化输出)、标量累加器、分块遍历等技巧。当生成 reduce/normalize 类算子时可参考此示例的代码结构。"4category: example5version: "1.0.0"6metadata:7 source: https://skillmd.com/skills/wenyi-li/triton-ascend-example-layernorm8 backend: ascend9 dsl: triton_ascend10 hardware: "Atlas A2, Atlas A3"11 operator_type: "reduce"12---1314# LayerNorm — Triton Ascend 实现示例1516```python17import torch18import triton19import triton.language as tl202122@triton.jit23def layernorm_kernel(24 X_ptr, Y_ptr,25 batch_size: tl.constexpr, feature_size: tl.constexpr,26 eps: tl.constexpr,27 BLOCK_SIZE: tl.constexpr, CORE_NUM: tl.constexpr,28):29 core_id = tl.program_id(0)30 for batch_idx in range(core_id, batch_size, CORE_NUM):31 batch_offset = batch_idx * feature_size3233 # Phase 1: compute mean & variance34 mean_acc = 0.035 var_acc = 0.036 for i in range(0, feature_size, BLOCK_SIZE):37 offsets = batch_offset + i + tl.arange(0, BLOCK_SIZE)38 mask = offsets < batch_offset + feature_size39 x = tl.load(X_ptr + offsets, mask=mask, other=0.0)40 mean_acc += tl.sum(x, axis=0)41 var_acc += tl.sum(x * x, axis=0)4243 mean_val = mean_acc / feature_size44 std_val = tl.sqrt(var_acc / feature_size - mean_val * mean_val + eps)4546 # Phase 2: normalize47 for i in range(0, feature_size, BLOCK_SIZE):48 offsets = batch_offset + i + tl.arange(0, BLOCK_SIZE)49 mask = offsets < batch_offset + feature_size50 x = tl.load(X_ptr + offsets, mask=mask, other=0.0)51 tl.store(Y_ptr + offsets, (x - mean_val) / std_val, mask=mask)525354class ModelNew(torch.nn.Module):55 def __init__(self):56 super().__init__()57 try:58 self.VEC_CORE_NUM = torch_npu.npu.npu_config.get_device_limit(0).get("vector_core_num", 40)59 except:60 self.VEC_CORE_NUM = 406162 def forward(self, x):63 shape = x.shape64 batch_size = shape[0]65 feature_size = 166 for s in shape[1:]:67 feature_size *= s68 if not x.is_contiguous():69 x = x.contiguous()70 y = torch.empty_like(x)71 BLOCK_SIZE = 102472 grid = (self.VEC_CORE_NUM,)73 layernorm_kernel[grid](x, y, batch_size, feature_size, 1e-5,74 BLOCK_SIZE=BLOCK_SIZE, CORE_NUM=self.VEC_CORE_NUM)75 return y76```