Triton ascend

From mindspore-ai/akg · View interactive version

Triton ascend is an AI agent skill from the GitHub repository mindspore-ai/akg. Triton Ascend NPU编程指南,包含核心概念、标准模式和完整示例

How to use this skill

Skill file

Triton Ascend NPU编程指南

基于 AKG Agents resources/docs/triton_ascend_docs

核心概念

内核 (Kernel)

网格与块

内存层次

标准内核结构

@triton.jit
def standard_kernel(output_ptr, input_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
    # 1. 获取程序ID和计算偏移
    pid = tl.program_id(0)
    offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
    
    # 2. 创建边界掩码
    mask = offsets < n_elements
    
    # 3. 加载数据
    data = tl.load(input_ptr + offsets, mask=mask)
    
    # 4. 执行计算
    result = compute_function(data)
    
    # 5. 存储结果
    tl.store(output_ptr + offsets, result, mask=mask)

三大编程模式

1. 向量操作模式

适用于元素级运算。

2. 归约模式

适用于聚合操作。

3. 矩阵乘法模式

使用分块策略。

Ascend NPU特性

NPU架构特点

优化建议

  1. 利用NPU的矩阵计算单元
  2. 优化数据布局
  3. 使用合适的block size
  4. 考虑内存对齐

完整示例

参考: python/akg_agents/op/resources/docs/triton_ascend_docs/examples/

最佳实践

  1. 始终使用mask处理边界
  2. Block size选择2的幂
  3. 测试不同配置找到最优
  4. 注意NPU特定的内存访问模式

相关资源

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.