ReLU — Triton Ascend 实现示例
import torch
import triton
import triton.language as tl
@triton.jit
def relu_kernel(
x_ptr, y_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
x = tl.load(x_ptr + offsets, mask=mask, other=0.0)
y = tl.maximum(x, 0.0)
tl.store(y_ptr + offsets, y, 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()
y = torch.empty_like(x)
n_elements = x.numel()
BLOCK_SIZE = 1024
grid = (self.VEC_CORE_NUM,)
relu_kernel[grid](x, y, n_elements, BLOCK_SIZE=BLOCK_SIZE, CORE_NUM=self.VEC_CORE_NUM)
return y
1---2name: triton-ascend-example-relu3description: "ReLU 逐元素算子的完整 Triton Ascend 实现示例。展示向量化逐元素操作的标准模式:1D 分块遍历、mask 边界处理、交错循环。当生成 elementwise 类算子时可参考此示例的代码结构。"4category: example5version: "1.0.0"6metadata:7 source: https://skillmd.com/skills/wenyi-li/triton-ascend-example-relu8 backend: ascend9 dsl: triton_ascend10 hardware: "Atlas A2, Atlas A3"11 operator_type: "elementwise"12---1314# ReLU — Triton Ascend 实现示例1516```python17import torch18import triton19import triton.language as tl202122@triton.jit23def relu_kernel(24 x_ptr, y_ptr,25 n_elements,26 BLOCK_SIZE: tl.constexpr, CORE_NUM: tl.constexpr,27):28 pid = tl.program_id(0)29 num_blocks = tl.cdiv(n_elements, BLOCK_SIZE)30 for block_id in range(pid, num_blocks, CORE_NUM):31 offsets = block_id * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)32 mask = offsets < n_elements33 x = tl.load(x_ptr + offsets, mask=mask, other=0.0)34 y = tl.maximum(x, 0.0)35 tl.store(y_ptr + offsets, y, mask=mask)363738class ModelNew(torch.nn.Module):39 def __init__(self):40 super().__init__()41 try:42 self.VEC_CORE_NUM = torch_npu.npu.npu_config.get_device_limit(0).get("vector_core_num", 40)43 except:44 self.VEC_CORE_NUM = 404546 def forward(self, x):47 if not x.is_contiguous():48 x = x.contiguous()49 y = torch.empty_like(x)50 n_elements = x.numel()51 BLOCK_SIZE = 102452 grid = (self.VEC_CORE_NUM,)53 relu_kernel[grid](x, y, n_elements, BLOCK_SIZE=BLOCK_SIZE, CORE_NUM=self.VEC_CORE_NUM)54 return y55```