Pypto basics

From mindspore-ai/akg · View interactive version

Pypto basics is an AI agent skill from the GitHub repository mindspore-ai/akg. PyPTO 编程原则与核心模式

How to use this skill

Skill file

PyPTO 编程原则

原则 0:先抽取 Torch 基线的语义合同

写 PyPTO 前,先把 baseline forward 变成“可执行语义合同”,再做实现与优化。

最少做 5 件事:

  1. 写出数学式(谁参与计算、谁是被规约对象、输出定义是什么)。
  2. 核对 API 语义而不是变量名(input/target/prediction 这些名字经常误导)。
  3. 固化规约合同(sum、batchmean、或 mean 语义=sum/count,并明确规约轴与 keepdim)。
  4. 判断是否非对称(A||B 与 B||A 是否等价)。
  5. 做 1 组语义自检样例(优先用非对称输入,先验方向是否正确)。

注意:性能规则(tile/loop)只能在语义合同确定后应用,不能反向决定语义。

原则 1:静态 shape

kernel 一切在编译时确定。工厂函数封装 kernel,shape 和标量参数(eps、slope 等)作为闭包传入。forward 直接传 torch.Tensor。ModelNew.init 签名必须与原始 Model 一致,shape 在 forward 中获取。

补充(高优先级):

原则 2:forward 的职责

  1. Assert:assert x.dim() == N、assert tuple(x.shape) == (...)
  2. Reshape(如需):torch reshape 为 kernel 能处理的 shape
  3. 调用 kernel:传入 contiguous 张量,reshape 回原始 shape 返回

forward 内禁止 torch 计算。kernel 内不能 reshape。

输出语义(keepdim/是否 squeeze)要与 baseline 直接对齐;不要先改语义再靠额外 squeeze/unsqueeze 回补。

原则 3:选择维度

| 算子类型 | forward 策略 | kernel 维度 |

|----------|-------------|-------------|

| Elementwise / 简单 Loss | reshape(-1) | 1D |

| GroupNorm / InstanceNorm | reshape(flat_batch, hidden) | 2D |

| BatchNorm / RMSNorm | reshape(B, C, -1) | 3D |

| Batched matmul(同 batch) | 保持 3D,不需要 loop | 3D |

| 2D Matmul | 保持 2D | 2D |

| 单轴归约 | 保持原始维度 | 原维 |

补充:

补充:

原则 4:tile 双约束

  1. prod(tile_shape) ≤ 16384
  2. auto_tiles = prod(ceil(shape[i]/tile[i])) ≤ 2048

tile 参数个数 = 被操作 tensor 的 rank。常用:

原则 5:运算符规则

+ *:标量任意位置。- /:tensor 必须在左。 1.0 - x crash → 用 x * (-1.0) + 1.0。函数调用第一参数必须 Tensor。

原则 6:loop

能不用 loop 就不用。loop 场景:auto_tiles > 2048(优先原因)/ 极大归约轴 / matmul M 轴(按对数刻度先定中段 loop_count,常见先试 16/32,再反推 BASIC_BATCH=ceil_div(m, loop_count);必要时再扩到 8/64)。不嵌套,沿最外轴。 view shape 只能用编译期常量。


核心编程模式

模块名 pypto(不是 pyto)。

import os, pypto, torch
_PYPTO_RUN_MODE = int(os.getenv("AIKG_PYPTO_RUN_MODE", "0"))
_PYPTO_RUNTIME_DEBUG_MODE = int(os.getenv("AIKG_PYPTO_RUNTIME_DEBUG_MODE", "0"))

模式 A:Elementwise

关键判断:auto_tiles = prod(ceil(shape[i]/tile[i])) 必须 ≤ 2048。 超过则必须 loop。

优先策略:Elementwise 无数据依赖,优先 flatten 为 1D;loop 仅由 auto_tiles > 2048 触发,与维度无关。

小矩阵(flatten 后 auto_tiles ≤ 2048):reshape(-1) → 1D kernel。

大矩阵(如 16384×4096,flatten 后 auto_tiles > 2048):1D + loop(或 2D + loop 仅当语义需要保持 2D)。

# 小矩阵:ELU(16×16384=262144, tile 8192, auto_tiles=32)
def create_elu_kernel(flat_size, alpha):
    @pypto.frontend.jit(...)
    def kernel(x: pypto.Tensor((flat_size,), pypto.DT_FP32)) -> ...:
        output = pypto.tensor([flat_size], pypto.DT_FP32)
        pypto.set_vec_tile_shapes(8192)
        pos = pypto.maximum(x, 0.0)
        neg = pypto.minimum(x, 0.0)
        output[:] = pos + (pypto.exp(neg) - 1.0) * alpha
        return output
    return kernel

# 大矩阵 1D 版本:scalar mul(16384×4096,flatten 后 loop)
def create_scalar_mul_kernel_1d(m, n, s):
    flat_size = m * n
    TARGET_LOOP_COUNT = 32
    BASIC_BATCH = ceil_div(flat_size, TARGET_LOOP_COUNT)
    num_iters = ceil_div(flat_size, BASIC_BATCH)
    @pypto.frontend.jit(...)
    def kernel(a: pypto.Tensor((m, n), pypto.DT_FP32)) -> ...:
        a_flat = pypto.view(a, [flat_size], [0])
        c = pypto.tensor([flat_size], pypto.DT_FP32)
        pypto.set_vec_tile_shapes(8192)
        for bi in pypto.loop(0, num_iters, 1, name="LOOP", idx_name="bi"):
            off = bi * BASIC_BATCH
            chunk = pypto.view(a_flat, [BASIC_BATCH], [off])
            pypto.assemble(chunk * s, [off], c)
        return pypto.view(c, [m, n], [0])
    return kernel

# 大矩阵 2D 版本:仅当语义需要保持 2D 布局时使用
def create_scalar_mul_kernel_2d(m, n, s):
    TARGET_LOOP_COUNT = 32
    BASIC_BATCH = ceil_div(m, TARGET_LOOP_COUNT)
    num_iters = ceil_div(m, BASIC_BATCH)
    @pypto.frontend.jit(...)
    def kernel(a: pypto.Tensor((m, n), pypto.DT_FP32)) -> ...:
        c = pypto.tensor([m, n], pypto.DT_FP32)
        pypto.set_vec_tile_shapes(8, 4096)
        for bi in pypto.loop(0, num_iters, 1, name="LOOP", idx_name="bi"):
            off = bi * BASIC_BATCH
            chunk = pypto.view(a, [BASIC_BATCH, n], [off, 0])
            pypto.assemble(chunk * s, [off, 0], c)
        return c
    return kernel

模式 B:Matmul

模式 C:Norm + Loop

GroupNorm / InstanceNorm(2D):reshape (flat_batch, hidden_size),tile (1, 16384),loop 沿 flat_batch。var = sq_sum * inv_hidden - mean * mean。

RMSNorm / BatchNorm(3D):(B, C, S),tile (1, 1, 16384) 或 (1, 16, 256)。

模式 D:Loss → 标量

简单 loss(MSE/Hinge/KLDiv)flatten 到 1D。per-sample loss(Triplet/Cosine)保持 2D,两段 tile。

KL(target || pred),并按 batchmean 规约。

模式 E:大张量全局归约

pypto.zeros 累加器 + loop 分块 + acc[:] = acc + part。

def ceil_div(a, b):
    return (a + b - 1) // b
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.