Merged
fix(tensor): Fix assemble offset for slice assignment without start and IR dump update #5941
HuYiShengAdd on Sep 4
fix(tensor): Fix assemble offset for slice assignment without start and IR dump update #5941
Merged
Pull Request has been successfully merged by CANN-robot.
(Thank you for HuYiSheng's contribution.)Sep 4 Associated issue: [Bug-Report|缺陷反馈]: loop循环中assemble写法不带起始start偏移量时有精度问题
atomgit-bot
Sep 4 Comments:
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]。


Incorrect?
atomgit-bot
Sep 4 Comments:
Sep 4 Comments:
Sep 4 Added label: cann-cla/yes
83 messages are collapsed here。 See more
杨旭
Sep 7 Comments:
Sep 7 Comments:
/lgtm


杨旭
Sep 7 Comments:
Sep 7 Comments:
/approve


Sep 7 Added label: lgtm
Sep 7 Closed associated issue
Sep 7 Merged pull request
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