Closed
[Bug-Report|缺陷反馈]: 嵌套循环中 view[:]= 写回静默不执行,但无任何报错信息提示,文档中无相关描述,直到数据校验才校验失败。 #3245
liuxiaoxiao11Add on  Sep 7Closed at  Sep 9
liuxiaoxiao11
liuxiaoxiao11Collaborators
Sep 7 Create

Thanks for sending an issue! Please fill in the following template to help quickly solve your problem.

Describe the current behavior / 问题描述 (Mandatory / 必填)

在嵌套循环(pypto.loop 嵌套)中,通过 view[:] = ... 方式写回 persistent buffer 时,编译器可能跳过或优化掉该写回操作,导致输出全零。该问题具有以下特征:

静默不执行:view[:] = ... 写回在嵌套循环内不生效,但无任何编译错误或运行时警告
persistent buffer 全零:因写回未执行,persistent buffer 保持初始值(全零),kernel 输出为全部 0
仅嵌套循环触发:单层循环下写回正常,嵌套循环的内层循环中写回丢失
受影响算子: ds_v32_sparse_attention_antiquant(Stage 5 验证时首次输出全零,verifier 报告 F74008 all_close=False)

Environment / 环境信息 (Mandatory / 必填)

服务器/NPU 型号: Ascend910(A3 平台)
PyPTO 版本/Commit: 0.2.1 (pip) / 源码 HEAD 41b340b76
CANN 版本: 9.2.0 (V100R001C25B114)
Python 版本: 3.13.9
操作系统: Huawei Cloud EulerOS 2.0 (aarch64)

Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)

编写 JIT kernel,使用 pypto.loop 嵌套循环(外层循环沿 batch 轴,内层循环沿 head 轴)
内层循环中,通过 view[:] = tile 将计算结果写回 persistent buffer
编译运行,将输出与预期值对比
观察输出全零(写回未执行)
改用 pypto.assemble(tile, offsets, buffer) 替代 view[:] = 重新运行,确认输出正确
最小复现脚本: repro_view_writeback.py

source /home/operation/Ascend/ascend-toolkit/set_env.sh
export TILE_FWK_DEVICE_ID=<空闲设备号>
cd /home/l00605417/lxx/benchmark_runs/gen-0905-issue/issues/issue-11
python repro_view_writeback.py
复现运行结果(CANN 9.2.0):

=== 版本 1: 嵌套循环 + view[:] = 写回 ===
out:
[[-1. -1. -1. -1.]
[-1. -1. -1. -1.]
[-1. -1. -1. -1.]
[-1. -1. -1. -1.]]
RESULT: ALL -1 (writeback did NOT execute → persistent buffer all-zero)
确认:嵌套循环中 view[:]= 写回静默不执行

=== 版本 2: 嵌套循环 + pypto.assemble 写回(对照实验)===
out:
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]
[12. 13. 14. 15.]]
RESULT: assemble writeback executed correctly
对照实验确认:assemble 可替代 view[:]= 写回
view[:]= 写回在嵌套循环中静默不执行(输出全为 -1 哨兵值),pypto.assemble 写回正确(输出 = x 原始值)。两版本唯一差异是写回 API,确认根因。

Describe the expected behavior / 预期结果 (Mandatory / 必填)

不支持的操作应在文档中写清楚,并给出报错提示

F74008: all_close=False # verifier 报告输出全零,与 golden 逐元素对比全 fail
Root cause path: nested loop (outer: batch, inner: head) → view[:] = writeback
inside inner loop → compiler skips the writeback → persistent buffer stays all zeros
→ replace with pypto.assemble → writeback works correctly

Special notes for this issue/备注 (Optional / 选填)

复现状态: ✅ 复现(ds_v32_sparse_attention_antiquant 通过对照实验确认:view[:]= 全零 → pypto.assemble 正确)
仓分类: pypto(编译器优化/调度缺陷)
完整证据目录: fracture-points/ds_v32_sparse_attention_antiquant.md(FP-1, FP-2)34ff4b9e11e04117b52eaf6bd8b8dc57.zip

likedislike
liuxiaoxiao11liuxiaoxiao11Collaborators
Sep 7 Added label: bug-report
liuxiaoxiao11liuxiaoxiao11Collaborators
Sep 7 Change title to "[Bug-Report|缺陷反馈]: 嵌套循环中 view[:]= 写回静默不执行,但无任何报错信息提示,文档中无相关描述,直到数据校验才校验失败。", originally titled "[Bug-Report|缺陷反馈]: 嵌套循环中 view[:]= 写回静默不执行,但无任何报错信息提示,直到数据校验才校验失败。".
宋乐宋乐Collaborators
Sep 7 Assign xiaoyueyueyueyue as responsible person
宋乐宋乐Collaborators
Sep 7 Assign seu_chang as responsible person
宋乐宋乐Collaborators
Sep 7 Added label: Frontend
宋乐
宋乐Collaborators
Sep 7 Comments:

@seu_chang 请确认前端是否需要修改

likedislike
LiuChang
LiuChangCollaborators
Sep 8 Comments:

示例中用户写法合法,但当前实现与用户理解存在偏差
后续在 pypto-view.md「约束说明」新增:

  • view 返回的是创建时刻的独立快照拷贝:对 view 的写入(含 view[:] = ...)只作用于 view 自身,不会写回源 input;input 后续被修改,view 也不会感知
likedislike
HuYiShengHuYiShengCollaborators
Sep 8 Associated pull request
HuYiShengHuYiShengCollaborators
Sep 9 Associated pull request
CANN-robotCANN-robotCollaborators
Sep 9 Close issue
CANN-robotCANN-robotCollaborators
Sep 9 Added label: resolved
HuYiShengHuYiShengCollaborators
Sep 11 Associated pull request