Merged
fix(pass): Avoid null pointer errors. #7445
wangqun23Add on 19 days ago
fix(pass): Avoid null pointer errors. #7445
Merged
wangqun23Add on 19 days ago
wangqun23
wangqun23Collaborators
19 days ago

背景

由于框架的修改,在Token开启的情况下,存在与Assemble输出同RawMagic的Incast不与其他op相连接的情况,目前仅限于Incast存在类似情况,即Incast游离在其他连接关系之外。导致ReplaceTensor里的map的value存储空指针,后续使用空指针导致段错误。

方案

针对map可能访问不存在的key做补充和校验。
对可能的空指针使用新增校验。

Related Issue: #3806

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for wangqun23's contribution.)
wangqun23wangqun23Collaborators
19 days ago Created pull request, commit 2c9dae7c
atomgit-bot
atomgit-bot
19 days ago Comments:

变更摘要

该 PR 针对 ReplaceTensor 图变换过程中出现的空指针问题做了防御性修复,核心围绕 UnionFind 并查集与张量排序索引的构建:在 BuildTensorOrderIndexMap 中过滤空张量并补录 Function::GetIncast() 中未与算子连接的 incast 张量,在并查集的构造、合并、查找与分组环节对空指针及缺失顶点做兜底处理,同时新增 UT 用例覆盖「未连接 incast 与 Assemble 输出共享 rawmagic」的场景。另外将两处日志级别由 WARN 下调为 DEBUG。

主要改动

  • 补录未连接的 incast 张量: ReplaceTensor::BuildTensorOrderIndexMap 引入 addTensor lambda,跳过空指针并保留首次出现顺序,并遍历 function.GetIncast() 把未与任何算子相连的 incast 记入 tensorToOrderIndex,避免 base 张量选择时空指针访问。
  • UnionFind 空指针与缺失顶点防护: 构造函数跳过 nullptr 顶点并使用 emplace;Unite 在任一参数为空时直接返回;Find 变为非 const 方法,对空指针返回 nullptr,对不在 parentMap 中的张量按单点根节点补注册。
  • rankMap 访问方式调整: Unite 内对 rankMap 的读写由 operator[] 改为 rankMap.at(...),GetGroups 去掉 const 限定并跳过遗留的空键,且不再使用 mutable。
  • 日志级别下调: InferShapeFuncRegistry 未注册 infershape 的提示由 PASS_LOGW 改为 PASS_LOGD,LargeBitmap::ClearBit 越界提示由 APASS_LOG_WARN_F 改为 APASS_LOG_DEBUG_F,减少非异常路径的告警噪音。
  • 新增单元测试: test_replace_tensor.cpp 增加 ConstructUnconnectedIncastGraph 与 CheckUnconnectedIncast,构造未连接 incast 与 Assemble 输出共享 rawmagic 的图,验证 pass 执行成功、rawmagic 一致以及 token 依赖被正确重连。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
19 days ago Comments:

代码审查

审查结论

各文件审查情况

  • framework/src/interface/operation/op_infer_shape_impl.h:无问题。PASS_LOGD 宏确实存在于 framework/include/tilefwk/pypto_fwk_log.h:42,编译无忧;该分支是“op 未注册 infershape 时回退用 shape 填 validshape”的预期路径,降级为 DEBUG 属于合理的降噪。
  • framework/src/passes/block_graph_pass/memory_reuse/large_bm.cpp:1 个 P3(ClearBit 日志降级掩盖了 ResizeBits 中永不生效的清理循环,且与同文件 SetBit/GetBit 的越界告警级别不一致)。
  • framework/src/passes/tile_graph_pass/graph_constraint/replace_tensor.cpp:无问题。addTensor 跳过 nullptr 并保留首次出现顺序;新增的 incast 追加在末尾,而含 incast/outcast 的组会在 FindBaseTensor 首个循环中提前返回,不会改变原有 base 选择结果;同时补齐了 tensorToOrderIndex.at() 的索引缺失来源。
  • framework/src/passes/tile_graph_pass/graph_constraint/replace_tensor.h:无问题。Find 改为非 const、去掉 mutable、GetGroups 去掉 const 的唯一调用方(RunOnFunction 中的 uf)本就是非 const 对象;新的 null 守卫与“缺失顶点补为单点根”不会把 nullptr 写入 parentMap/rankMap,Unite/GetGroups 的 at() 访问在“键与 rank 成对插入”的不变式下安全。
  • framework/tests/ut/passes/src/test_replace_tensor.cpp:1 个 P3(新增用例的 rawmagic 断言在改动前就已成立,未覆盖 base raw 的实际传播)。

发现统计

  • P0/P1/P2:0
  • P3:2(large_bm.cpp 日志/死循环可见性;新增 UT 断言空转)

整体风险判断

低风险。本 PR 的核心改动方向正确且自洽:union-find 的 nullptr 顶点被彻底排除在返回分组之外,缺失顶点改为显式补为单点根,构建索引时补齐了游离 incast,从源头消除了“分组里出现 nullptr → base 选择空指针解引用”的路径;两条 P3 均为可选的可维护性/测试增强项,不构成功能回归。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
19 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
19 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
framework/src/interface/operation ✅ xinweiliu, zhangxiangjie (2/2) ✅ xinweiliu (1/1)
framework/src/passes ✅ zhangyuebing, zhangxiangjie (2/2) ✅ zhangyuebing (1/1)
repo-cann/pypto ✅ zhangyuebing, xinweiliu, zhangxiangjie (3/2) ✅ zhangyuebing, xinweiliu (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

wangqun23, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
50 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
18 days ago Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
18 days ago Merged pull request
wangqun23wangqun23Collaborators
2 days ago Deleted associated issue: [Task|任务跟踪]: 解决空指针报错问题
wangqun23wangqun23Collaborators
2 days ago Associated issue: [M1客户算子兼容性问题][Bug-Report|缺陷反馈]: ReplaceTensor pass 空指针解引用导致编译期 SIGSEGV(同 buffer 先写后读视图链触发)
CANN-robotCANN-robotCollaborators
2 days ago Removed label: ci-pipeline-passed