Merged
fix(tensor): Fix assemble offset for slice assignment without start and IR dump update #5941
fix(tensor): Fix assemble offset for slice assignment without start and IR dump update #5941
Merged
HuYiShengAdd on Sep 4
HuYiSheng
HuYiShengCollaborators
Sep 4

loop 循环中 assemble 写法不带起始 start 偏移量时存在精度问题。典型写法:

output_tensor[b_idx * tile_b:(b_idx + 1) * tile_b, :h] = left # "[:h]" 不带 start
output_tensor[b_idx * tile_b:(b_idx + 1) * tile_b, h:] = right
Issue 中最小复现(test_coe_issue.py 1)triple-compare 精度 FAILED,IR 偏移证据为 to offset:[0,-64] to dynoffset:[...,-64]。

修复 Tensor._get_assemble_offset 对 slice(None, stop) 的偏移错算,使不带 start 的切片赋值写入正确位置。

按 Python 切片语义,切片起始位置即写入偏移——start 为 None 时 offset 置 0,否则为 start。同步清理不再使用的 stop/axis 局部变量。

此外,IR 文本 dump 能力增强

IRTextDumper::PrintAttrValue 缺少 std::vector 类型分支,导致 assemble/view 属性中的动态偏移(toOffset、fromOffset、toValidShape 等)序列化为 "Unsupported"

新增缺失分支,复用文件内已有的 VisitScalarExpr::Visit 打印器。[expr, expr] 列表格式符合 attr_value_list 文法,IRTextLoader round-trip 不受影响

新增 assemble_offset/ IR 快照回归测试(复刻 issue kernel 场景,pil.compile + check_snapshot);重渲染 94 个受影响黄金文件(Unsupported → 真实偏移表达式)
Related Issue: #3216

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for HuYiSheng's contribution.)
HuYiShengHuYiShengCollaborators
Sep 4 Created pull request, commit 87c0de99
HuYiShengHuYiShengCollaborators
Sep 4 Associated issue: [Bug-Report|缺陷反馈]: loop循环中assemble写法不带起始start偏移量时有精度问题
atomgit-bot
atomgit-bot
Sep 4 Comments:

变更摘要

本 PR 修复了 Tensor._get_assemble_offset 对不带起始偏移的切片(如 [:h]、[h:])在 assemble 写入时偏移量计算错误的问题。原实现会在 start 为 None 而 stop 非 None 时用 stop - shape[axis] 推算偏移,导致写入位置错算、循环中 triple-compare 精度 FAILED(IR 偏移表现为 to offset:[0,-64] to dynoffset:[...,-64])。修复后按 Python 切片语义处理:切片起始位置即写入偏移,start 为 None 时偏移直接置 0,否则取 start 值,同时清理了不再使用的 stop/axis 局部变量,并补充了对应单元测试。

主要改动

  • 修正 _get_assemble_offset 的偏移计算逻辑:删除 start is None and stop is None 的合并判断及 elif isinstance(stop, (int, SymbolicScalar)): offsets.append(stop - shape[axis]) 分支,改为 start is None 时偏移置 0、start 为 int/SymbolicScalar 时偏移取 start,使不带 start 的切片(如 slice(None, stop))写入正确位置。
  • 简化循环与局部变量:将 for axis, k in enumerate(key) 改为 for k in key,仅解构 start, step = k.start, k.step,移除不再使用的 axis 与 stop 变量。
  • 新增单元测试覆盖:在 python/tests/ut/interface/test_tensor_setitem.py 中添加 test_tensor_setitem_assemble_offset_without_start,验证 key = (slice(0, 16), slice(None, 64)) 时 _get_assemble_offset 返回 [0, 0]。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 4 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 4 Added label: cann-cla/yes
83 messages are collapsed here。 See more
杨旭
杨旭Collaborators
Sep 7 Comments:

/lgtm

likedislike
杨旭
杨旭Collaborators
Sep 7 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 7 Added label: lgtm
CANN-robotCANN-robotCollaborators
Sep 7 Closed associated issue
CANN-robotCANN-robotCollaborators
Sep 7 Merged pull request