Pypto pitfalls

From mindspore-ai/akg · View interactive version

Pypto pitfalls is an AI agent skill from the GitHub repository mindspore-ai/akg. PyPTO 常见首次生成错误及正确写法

How to use this skill

Skill file

PyPTO 常见陷阱

1. 运算符规则(最高频错误)

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

1.0 + x            # OK(__radd__)
1.0 - x            # CRASH(__rsub__ 未实现)
1.0 / x            # CRASH(__rtruediv__ 未实现)
pypto.add(1.0, x)  # CRASH(函数调用标量在前)

# 1 - x 正确写法
x * (-1.0) + 1.0   # 推荐

# 标量之间用 Python 运算
neg_delta = -delta  # OK(delta 是闭包 float)

2. clamp / min(x, d) 实现

pypto.clamp 不可用。min(x, d) 用双重取反:-max(-x, -d)。pypto.minimum(x, 0.0) 可用。

# min(abs_diff, delta) — delta 是闭包 float
neg_abs = pypto.mul(abs_diff, -1.0)
clipped = pypto.mul(pypto.maximum(neg_abs, -delta), -1.0)  # = min(abs_diff, delta)

Huber Loss 完整模式(必须用 clamp,不能简化):

diff = predictions - targets
abs_diff = pypto.abs(diff)
neg_abs = pypto.mul(abs_diff, -1.0)
clipped = pypto.mul(pypto.maximum(neg_abs, -delta), -1.0)  # min(|d|, delta)
half_sq = clipped * clipped * 0.5
loss = half_sq + abs_diff - clipped  # 完整 Huber 公式
total = pypto.sum(loss, dim=0, keepdim=True)
output[:] = total / flat_size

3. 工厂函数

标量参数(eps、slope、margin 等)必须作为工厂函数参数通过闭包传入 kernel。

3D+2D matmul 时,forward 展平后传展平维度 nm=N*M 给工厂,不要分别传 N、M。

4. matmul K > 65535

用逐元素乘法 + pypto.sum(a * b_broadcast, dim=1) 替代。forward 中 B.reshape(1, -1) 使其可广播。

5. 距离度量必须 sqrt

sum(diff*diff) 是平方距离,不是 L2 距离。TripletMarginLoss 等必须 pypto.sqrt(sum_sq + eps)。

6. tile rank = tensor rank

set_vec_tile_shapes 参数个数必须等于被操作 tensor 的 rank。2D tensor 用 2D tile。

6.1 盲抄 tile 常量(尤其 16384)

示例里的 16384/8192 是经验候选,不是固定答案。必须按当前 shape 和归约维重算。

要点:

6.2 把“少分段”误读成“归约轴越大越快”

“连续搬运达阈值后再调归约轴”是二级目标,但不是“归约轴 tile 越大越快”。

正确做法:

6.3 把“搬运优先”误读成“只要更连续就一直加”

连续搬运是先达标,不是无限放大。达到高效区后,继续把预算给非规约轴通常收益很小,反而会挤占规约轴 tile 预算。

正确做法:

6.4 把 `shape` 当成连续搬运长度(高频误判)

连续搬运阈值必须按 tile 的实际连续段 计算,不能按输入 shape 计算。

错误示例:

正确计算:

6.5 混用 cube/vec 算子却只设一次 tile(编译期高频崩溃)

高频误用:

典型报错:

正确做法:

7. 优先内建函数

pypto.sigmoid、pypto.softmax、pypto.abs、pypto.exp、pypto.log、pypto.sqrt 等有内建的直接用,禁止手动实现。

8. 不需要 assert flat_size % tile_size == 0

auto-tile 自动处理余数。只有 loop+view 模式需要确保整除性。

9. per-sample loss 两段 tile

2D 输入的 per-sample loss:Phase 1 (4, 4096) 算 per-sample → Phase 2 (128, 1) 跨 batch 归约。

10. 条件分支用 maximum + minimum

pypto.where 不可用。pypto.minimum 可用!

# ELU
output[:] = pypto.maximum(x, 0.0) + (pypto.exp(pypto.minimum(x, 0.0)) - 1.0) * alpha
# LeakyReLU
output[:] = pypto.maximum(x, 0.0) + pypto.minimum(x, 0.0) * slope

禁止 maximum(x, f(x)) 做条件选择(正半轴 f(x)>x 时结果错误)。

11. 方差公式

var = sq_sum * inv_count - mean * mean(E[x²]-E[x]²)。符号反了会 NaN。

12. ModelNew.__init__ 签名

必须与原始 Model 一致。shape 在 forward 中获取,不要加到 __init__ 参数里。

12.1 静态任务写成“万能 kernel”(多 dim 分支)

高频误用:

为什么不推荐:

正确做法:

13. 模块名 `pypto`

不是 pyto、pytorch、pto。所有调用以 pypto. 开头。

14. 基线语义误读(高频且隐蔽)

最危险的错误不是语法,而是“语义合同”读错:代码能编译、甚至能 PASS,但算的不是同一个东西。

高发误读源:

防错流程(先语义、后实现):

  1. 从 baseline forward 写出数学式。
  2. 单独查 API 合同:参数含义、是否 log 输入、规约定义。
  3. 固化规约语义:sum、batchmean、或 mean 语义=sum/count,并明确规约轴与输出形状。
  4. 标记是否非对称;非对称时明确“参考项”和“被比较项”。
  5. 用 1 组可解释样例做语义自检(优先非对称分布)。

示例(KLDiv,仅示例):

15. 连续规约轴不合并,导致多次归约中间开销

当规约目标本质是“连续多轴联合规约”时,直接链式 sum(dim=...) 常会产生中间张量与额外调度。

更优候选:

高频误区:

正确判定:

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.