Triton ascend memory

From mindspore-ai/akg · View interactive version

Triton ascend memory is an AI agent skill from the GitHub repository mindspore-ai/akg. Ascend NPU 内存访问优化策略,包括 UB(统一缓冲区)利用、数据布局优化、合并访存和预取技巧。适用于内存带宽受限、需要优化数据搬运效率、或处理大规模数据的内核代码性能优化场景

How to use this skill

Skill file

内存访问优化

块大小选择原理

块大小需要根据算子类型和硬件存储层级来平衡:

2D 数据:优先 tl.make_block_ptr

A_block_ptr = tl.make_block_ptr(
    base=A_ptr, shape=(M, K), strides=(stride_am, stride_ak),
    offsets=(pid_m * BLOCK_M, 0), block_shape=(BLOCK_M, BLOCK_K), order=(1, 0),
)
a = tl.load(A_block_ptr, boundary_check=(0, 1))
# 移动指针
A_block_ptr = tl.advance(A_block_ptr, (0, BLOCK_K))

连续内存:一维访问

非连续张量先 .contiguous() 转换,再用一维 ptr + offsets 访问:

class ModelNew(torch.nn.Module):
    def forward(self, x):
        if not x.is_contiguous():
            x = x.contiguous()
        out = torch.empty_like(x)
        n = x.numel()
        grid = (triton.cdiv(n, BLOCK_SIZE),)
        kernel[grid](x, out, n, BLOCK_SIZE=1024)
        return out

一维访问比 stride 计算效率更高,推荐优先使用。

对齐要求

要点

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.