一键导入
triton-ascend-examples-mindspore
MindSpore 框架下 Triton Ascend 内核的集成示例,展示 MindSpore 自定义算子注册、Primitive 定义、tensor 传入传出等标准写法。当目标框架为 mindspore 时应导入此示例作为代码结构参考。
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
MindSpore 框架下 Triton Ascend 内核的集成示例,展示 MindSpore 自定义算子注册、Primitive 定义、tensor 传入传出等标准写法。当目标框架为 mindspore 时应导入此示例作为代码结构参考。
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
AscendC direct-invoke 崩溃/挂起修复索引:Kernel timeout、hang、Segmentation Fault、aic error、buffer 死锁、plog/memcheck 调试。
AscendC direct-invoke 精度失败修复索引:输出全 0/随机值、DataCopy 对齐、EnQue/DeQue 同步、FP16/FP32 精度、Cast RoundMode、DumpTensor 分段定位。
AscendC direct-invoke 工程契约:WA 使用 kernel.py + ascendc_op/,ModelNew 调用 torch.ops.npu.*,adapter 负责复制工程、CMake 构建和 npu-arch patch。适用于 dsl=ascendc 的 autoresearch 任务。
把注册式 AscendC 算子迁移为 direct-invoke 工程的保真原则:kernel 算法和 tiling 公式不乱改,只替换注册框架胶水、入口 ABI、host launch 与 PyTorch extension。
CATLASS TileShape 与 on-chip 缓存容量约束:L1/L0A/L0B/L0C 预算公式、fp16/fp32 Pingpong 双缓冲、512B 对齐与排布对 Tile 选型的影响。调参前必读。
CATLASS Gemm 性能调优:DispatchPolicy、Tile 与分核负载均衡、Swizzle、何时用 padding/Split-K/Preload。面向 AR 修改 catlass_kernel.asc 中的类型别名。
| name | triton-ascend-examples-mindspore |
| description | MindSpore 框架下 Triton Ascend 内核的集成示例,展示 MindSpore 自定义算子注册、Primitive 定义、tensor 传入传出等标准写法。当目标框架为 mindspore 时应导入此示例作为代码结构参考。 |
| category | example |
| version | 1.0.0 |
| metadata | {"backend":"ascend","dsl":"triton_ascend","hardware":"Atlas A2, Atlas A3","framework":"mindspore"} |
| 特性 | PyTorch | MindSpore |
|---|---|---|
| 基类 | torch.nn.Module | mindspore.nn.Cell |
| 前向函数 | forward | construct |
| 张量创建 | torch.empty | mindspore.mint.empty / mindspore.mint.empty_like |
| 设备 | device='cuda'/'npu' | mindspore.set_device("Ascend", 0) / mindspore.set_device("CPU") |
| 数据类型 | torch.float16 | mindspore.float16 |
| 获取核心数 | triton.runtime.driver.active.utils.get_device_properties | mindspore.runtime.get_device_limit(0) |
MindSpore 实现:
import mindspore as ms
from mindspore import nn
import triton
import triton.language as tl
@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)
class ModelNew(nn.Cell):
def __init__(self):
super().__init__()
def construct(self, a, b):
c = ms.mint.empty_like(a)
n_elements = a.size
grid = (triton.cdiv(n_elements, BLOCK_SIZE),)
vector_add_kernel[grid](a, b, c, n_elements, BLOCK_SIZE=1024)
return c
关键差异:
class ModelNew(nn.Cell):
def __init__(self):
super().__init__()
def construct(self, x0, x1): # 注意:使用 construct 而非 forward
B, C = x0.shape
C2, D = x1.shape
assert C == C2, f"矩阵维度不匹配: {C} != {C2}"
# MindSpore 张量创建
output = ms.mint.empty((B, D), dtype=ms.float32)
matmul_kernel[1, 1, 1](output, x0, x1, 1, B, C, D)
return output
MindSpore 特有处理:
class ModelNew(nn.Cell):
def __init__(self, normalized_shape, eps=1e-5):
super().__init__()
self.eps = eps
self.normalized_shape = normalized_shape
# MindSpore 参数初始化
ms.set_seed(0) # 注意:使用 ms.set_seed 而非 torch.manual_seed
self.weight = ms.Parameter(ms.ops.ones(normalized_shape, ms.float32))
self.bias = ms.Parameter(ms.ops.zeros(normalized_shape, ms.float32))
def construct(self, x):
M, N = x.shape
output = ms.mint.empty_like(x)
grid = (M,)
layernorm_kernel[grid](
x, output, self.weight, self.bias,
N, self.eps, BLOCK_SIZE=triton.next_power_of_2(N)
)
return output
class ModelNew(nn.Cell):
def __init__(self):
super().__init__()
def construct(self, x):
n_rows, n_cols = x.shape
output = ms.mint.empty_like(x)
BLOCK_SIZE = triton.next_power_of_2(n_cols)
grid = (n_rows,)
softmax_kernel[grid](x, output, n_cols, BLOCK_SIZE)
return output
class ModelNew(nn.Cell):
def __init__(self):
super().__init__()
def construct(self, x):
# 第一个 kernel
intermediate = ms.mint.empty_like(x)
kernel1[grid](x, intermediate, ...)
# 第二个 kernel
output = ms.mint.empty_like(x)
kernel2[grid](intermediate, output, ...)
return output