已合并
fix: TensorRedirect 交付件规范对齐(issue #4553),并删除 AddV2 不可达的混合 dtype 组合 #4505
fix: TensorRedirect 交付件规范对齐(issue #4553),并删除 AddV2 不可达的混合 dtype 组合 #4505
已合并
raoliang_sac创建于 17 天前
raoliang_sac成员
17 天前

描述

本 PR 包含三部分改动,均针对 conversion/tensor_redirectmath/add_v2 两个新增算子。

一、TensorRedirect 对齐 issue #4553 的两条交付件规范

issue 提出两条要求,TensorRedirect 两条都不满足

  1. InferDataType 须交付在 op_graph 目录(仅图场景使用);InferShape 图与算子双用,保留在 op_host

    • 原实现把 InferDataType4TensorRedirect 和 InferShape 一起注册在 op_host/tensor_redirect_infershape.cpp
    • 现新增 op_graph/tensor_redirect_graph_infer.cpp 承载 InferDataType,op_host 只保留 IMPL_OP_INFERSHAPE(TensorRedirect).InferShape(...)
    • 顺带修复:InferDataType 原先丢弃 SetOutputDataType 返回值无条件返回 SUCCESS,现改为透传。
  2. 原型从 legacy 挪出后须补防重定义宏

    • canndev ops/built-in/op_proto/inc/ops_proto_legacy.h:13857 确实存在 REG_OP(TensorRedirect),且已用 OPS_PROTO_DEF_TENSORREDIRECT 包裹;本仓 op_graph/tensor_redirect_proto.h 无守卫。
    • 已实测:构造同时包含两份定义的 TU,加守卫前报 error: redefinition of 'class ge::op::TensorRedirect',加守卫后编译通过。
    • 现补同名守卫 OPS_PROTO_DEF_TENSORREDIRECT,与 legacy 侧配对。

AddV2 这两条本来就满足,未作改动:InferDataType 一直在 op_graph/add_v2_graph_infer.cppAddV2 不在 ops_proto_legacy.h 中(仅在 elewise_calculation_ops.h),按 issue 条件不需要加守卫。

两个算子的 REG_OP 均未改动,与 canndev 保持逐字符一致(忽略空白后比对:AddV2 vs elewise_calculation_ops.h:1390;TensorRedirect vs elewise_calculation_ops.h:3779ops_proto_legacy.h:13857)。

二、代码检视报告问题修复

级别 问题 修复
P1 TensorRedirect Tiling 不拒绝负维与 shape 乘积溢出,负 numel 可穿透到 DoTiling 产生 usedCoreNum == 0 / SetBlockDim(0) 逐维拒绝 < 0GetShapeSize() 后拦截溢出哨兵 kInvalidDimValue,且置于 numel == 0 判断之前
P1 TensorRedirect PrecisionReduceFlag(true) 与 bit-exact 语义冲突:allow_mix_precision 下允许把 FP32 节点降为 FP16,破坏 README 声明的逐 bit 相等(含负零 / NaN payload / 非规格化数) 改为 PrecisionReduceFlag(false)
P1 AddV2 注册的 4 个混合 dtype 组合在 GE 图通路上不可达 删除,详见下
P2 文档与实现不一致 docs/zh/op_list.md 删除 add_v2 已不存在的 bool logical_or 声明;README 删除混合精度组合表

关于 AddV2 的 4 个混合 dtype 组合(FP16+FP32、FP32+FP16、BF16+FP32、FP32+BF16):

canonical AddV2 契约要求 x1/x2 同 dtype —— canndev ops/built-in/op_proto/elewise_calculation_ops.cc:475AddV2Verify 调用 CheckTwoInputDtypeSame,原型注释也写明 x2: A tensor of the same dtype as "x1"。既然本仓 REG_OP 与 canndev 保持一致,OpDef 里注册异类型组合就与自身原型契约矛盾。

已实测复现:FP16+FP32 离线构图被 Verifier 拒绝(The AddV2_0 op dtype is not same, type1:DT_FLOAT16, type2:DT_FLOAT),FP16+FP16 对照组构图成功。本算子无 ACLNN,因此这 4 个已编译 binary 对公开调用方实际不可达。

删除范围(REG_OP 不动):

  • add_v2_def.cpp:x1/x2/y 的 DataType 列表 13 → 9 组同 dtype,ADD_V2_FORMAT_LIST 同步收缩
  • add_v2_binary.json:删除 4 个混合 binary,13 → 9
  • add_v2_graph_infer.cpp:删除 34×34 类型提升表,InferDataType 改为 y 与 x1 同 dtype,并显式拒绝异类型输入
  • add_v2_tiling_arch35.cppIsMixedDtype 与 4 个混合分支改为 CheckDtype 前置校验
  • add_v2.cpp / add_v2_dag.h:删除 isMixDtype 分支与 AddMixDtypeCompute 模板
  • tests/assets/golden.py:删除已不注册的 bool logical_or 死分支;对异类型输入直接抛错,避免静默按 x1.dtype 生成错误标杆

三、AddV2 支持空 Tensor

上板回归时补的空张量用例暴露出一个既有能力缺口:AddV2 对 numel == 0 的输入一律 OPTILING_FAILURE,9 种 dtype × 4 种空场景 36/36 全挂。根因是 ATVOSS 的 BroadcastBaseTiling 在合轴之后显式拒绝 0 元素(broadcast_tiling.h"tensor check is empty, check failed"),空 Tensor 根本落不到 DoTiling

对比:同批提交的 TensorRedirect 本来就支持(Tiling 里有 HandleEmptyTensor 早返回,README 也声明了"支持空Tensor")。AddV2 此前既没实现也没声明。

math/select 的既有惯例另开一条自定义模板分支承接:

  • add_v2_struct_arch35.h:新增 userDef 模板键(0 常规 / 1 空 Tensor),空分支复用 BRC_TEMP_SCH_CUSTOM_MODE_KEY_SELschMode 999;并补 AddV2EmptyTilingData —— opc 靠 kernel 里的 GET_TILING_DATA_WITH_STRUCT 反推每个模板实例的 tiling 结构体大小,自定义分支不引用任何结构体会导致编译期 tiling_struct_size 未定义。
  • add_v2.cpp:kernel 增加 userDef 模板参数,userDef == 1 时直接返回。
  • add_v2_tiling_arch35.cpp:抽出 SetWorkspace(),新增 HandleEmptyTensor();按输出 y 的元素数判空,blockDim = 1、tilingKey 走自定义分支。判空之前先拦截 GetShapeSize() 的溢出哨兵(负值),否则负的 numel 会被当成非空穿透到 DoTiling
  • 常规通路的 tilingKey 统一补 userDef = 0,编码后取值不变(仍为 8),原有 UT 的 expectTilingKey 无需调整。

关联的Issue

测试

单元测试

  • ./build.sh -u --ophost --soc=ascend950 --ops=add_v222/22 通过(10 条 tiling,含新增 4 条空 Tensor;12 条 infershape)。
  • op_host 全量 UT 75/75 通过(前两个 commit 时验证)。

上板回归(Ascend950PR,TTK kernel 模式)

全部以 --binary-only-d=false -b=release)+ --pc=1 串行执行,只采信 BIN_*bin_compile_s = BINARY_MATCH 才算真正匹配到部署的 .o,而非 JIT 现编兜底)。

算子 用例数 BINARY_MATCH 精度通过率
AddV2 1735 1735 100.00%
TensorRedirect 900 900 100.00%
合计 2635 2635 100.00%

其中 36 条空 Tensor 用例全部 BINARY_MATCH + 100%,tiling_key = 65550 (0x1000e)block_dim = 1,确认走的是新增的自定义分支(改动前为 36/36 INVALID_TILING)。

负向对照

add_v2_removed_mixed_dtype.csv 12 条(已删除的 4 组混合 dtype)如期 全部 BINARY_MATCH_FAILURE。历史交付件里这 4 组是 BINARY_MATCH + 100% PASS —— 前后翻转,可作为"部署的确实是本 PR 代码"的直接证据。

编译

  • ./build.sh --pkg --soc=ascend950 --ops=add_v2,tensor_redirect:AddV2 产出 9 个 binary(原 13),TensorRedirect 4 个,无残留混合 dtype binary。
  • 原型防重定义:构造同时包含 legacy 与本仓两份 REG_OP(TensorRedirect) 的 TU,加守卫前编译失败、加守卫后通过。

需要评审者注意的测试局限(如实说明)

  1. 本 PR 为 TensorRedirect 新增的 4 个负维 / 溢出 UT,在打补丁前也是通过的 —— 当前 UT faker 构造这类 context 时本就返回 GRAPH_FAILED(并非因为走到了新增的显式校验)。这些用例锁定了契约,但不能证明新增守卫本身生效;真实 GE 通路的行为需由 GEIR 测试覆盖。
  2. 两个算子的 InferDataType 均无单元覆盖 —— 仓内 UT harness 只有 InferShape executor,没有 InferDataType 的对应设施,本 PR 未为此新造 harness。AddV2 新增的"x1/x2 异类型返回 GRAPH_FAILED"这条也因此未被测到。
  3. TensorRedirect 的 900 条回归是在前一版代码上跑的,本次 AddV2 改动后未复跑。依据:conversion/tensor_redirect 源码相对该版本零改动,且重新编译产出的 4 个 binary 配置 json 与已部署版本逐字段一致(仅豁免随构建变化的 sha256.o 本身构建不可复现,MD5 不具可比性)。
  4. 性能仅有 TTK 采集的 cycle 数,未做三方对标。

文档更新

  • math/add_v2/README.md:删除混合精度组合表;x2/y 描述改为"与 x1 一致";约束说明补充"输入 x1 和 x2 的数据类型需相同"
  • docs/zh/op_list.md:删除 add_v2 条目中已不存在的"bool 输入按 logical_or 处理";描述补充"支持空Tensor"
  • math/add_v2/README.md:约束说明补充"支持空Tensor。当输出y的元素个数为0时,算子不下发有效计算,直接返回成功。"

类型标签

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 raoliang_sac 的贡献)
Rraoliang_sac成员
17 天前 创建了 pull request,commit 95b57e30
Rraoliang_sac成员
17 天前 关联了issue:[Bug-Report|缺陷反馈]: INTrainingReduceV2 算子 ,NormalizeBBox 算子,SGD 优化器算子 原型重定义问题 和inferdatatype放置问题有误
atomgit-bot
atomgit-bot
17 天前 评论:

变更摘要

本 PR 包含两部分改动:一是将 TensorRedirect 算子的 InferDataTypeop_host 迁移至 op_graph 目录并对齐交付件规范(新增防重定义宏),同时修复 Tiling 层对负维与 shape 乘积溢出的防御缺失以及 PrecisionReduceFlag 与 bit-exact 语义冲突的问题;二是删除 AddV2 算子中 4 组不可达的混合 dtype 组合(FP16+FP32、FP32+FP16、BF16+FP32、FP32+BF16)及其关联的类型提升表、AddMixDtypeCompute 模板和混合精度 binary,使实现与 canonical AddV2 的「x1/x2 同 dtype」契约一致。同步更新了相关文档与 golden 测试脚本。

主要改动

  • TensorRedirect InferDataType 按规范迁移至 op_graph:在 conversion/tensor_redirect/op_graph/tensor_redirect_graph_infer.cpp 中新增 InferDataType4TensorRedirect,透传 SetOutputDataType 返回值;op_host/tensor_redirect_infershape.cpp 中移除原有的 InferDataType 注册,仅保留 InferShape

  • TensorRedirect 原型增加防重定义宏:在 op_graph/tensor_redirect_proto.h 中为 REG_OP(TensorRedirect) 包裹 #ifndef OPS_PROTO_DEF_TENSORREDIRECT / #define / #endif 守卫,与 canndev legacy 侧同名宏配对以防止链接期重定义错误。

  • TensorRedirect Tiling 增加负维与溢出拦截、修正精度策略tensor_redirect_tiling_arch35.cpp 中新增逐维非负校验及 GetShapeSize() 溢出哨兵 kInvalidDimValue 拦截(置于 numel == 0 之前);tensor_redirect_def.cpp 中将 PrecisionReduceFlagtrue 改为 false,避免混合精度编译选项将 FP32 节点降为 FP16 而破坏恒等拷贝的 bit-exact 语义。

  • AddV2 删除不可达的 4 组混合 dtype 组合add_v2_def.cpp 中 DataType 列表从 13 组收缩为 9 组同 dtype;add_v2_binary.json 删除 4 个混合 binary;add_v2_graph_infer.cpp 删除 34×34 类型提升表,InferDataType 改为 y 与 x1 同 dtype 并显式拒绝异类型输入;add_v2_tiling_arch35.cppIsMixedDtype 替换为 CheckDtype 前置校验;add_v2.cppadd_v2_dag.h 删除 isMixDtype 分支和 AddMixDtypeCompute 模板。

  • 文档与 golden 脚本同步更新math/add_v2/README.md 删除混合精度组合表并补充 x1/x2 同 dtype 约束;tests/assets/golden.py 删除已不注册的 bool logical_or 分支,对异类型输入直接抛 ValueError

likedislike
atomgit-bot
atomgit-bot
17 天前 评论:

代码审查

✅ 未发现问题

likedislike
CANN-robotCANN-robot成员
17 天前 添加了label:stat/needs-squash
CANN-robotCANN-robot成员
17 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
17 天前 评论:

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


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
*/*/README.md 陈娇, 汤平川 (2/2) 陈娇 (1/1)
*/*/op_graph/*_proto.h 王永光, 汤平川 (2/2) 王永光 (1/1)
*/*/op_host/*_def.cpp 王永光, 汤平川 (2/2) 王永光 (1/1)
repo-cann/ops-math 汤平川, 宋恺 (2/2) 汤平川, 宋恺 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robot成员
17 天前 将zhou-qilong,gitee-duhuiping,wangrui_,rxtfeng,loov1,tang-lei01,zhangzijie,jia0liang,zl_hw,songkai111,gitcode-chenjiao,pingchuantang,llimwang,zhouwenfang,gubaocheng,andong_hw,wangyongguang,wuyao51511,xuejinghui,zhanw_coding,caiwenwen设为评审人
此处折叠了11条事件消息 查看更多
CANN-robotCANN-robot成员
16 天前 添加了label:cann-cla/yes
raoliang_sac成员
14 天前 评论:

compile

likedislike
Rraoliang_sac成员
14 天前 update merge request[project id: 7649531, iid: 4505, commit_id: 3e9e6d6dba6831fac5fdb816de6ad55db25581eb] virtual merging success
CANN-robotCANN-robot成员
14 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
14 天前 评论:

流水线任务触发成功
任务链接 [861ef9718e534fb5b81286c48b073020][流水线指导]

任务名称状态日志下载链接
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_harmony-infer ✅ SUCCESS >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_A5_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_9030_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single ✅ SUCCESS >>>>> >>>>>
UT_Test ✅ SUCCESS
UT_Test_experimental ✅ SUCCESS
UT_Test_kernel ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
API_Check ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_harmony-infer-chs-math ✅ SUCCESS
UT_Test_harmony-infer-cpro-math ✅ SUCCESS

[2026-08-10 10:21:35]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
14 天前 评论:

流水线任务触发成功
任务链接 [2679221ba2b94ff8967de78419d55549][流水线指导]

任务名称状态日志下载链接
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_Pr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_precommit ✅ SUCCESS >>>>>

[2026-08-10 10:07:11]    CI执行结束

likedislike
CANN-robotCANN-robot成员
14 天前 添加了label:api-check-pass
CANN-robotCANN-robot成员
14 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
14 天前 添加了label:ci-pipeline-passed
wangyongguang成员
14 天前 评论:

/approve

likedislike
TangPC
TangPC成员
14 天前 评论:

/lgtm
/approve

likedislike
songkai111成员
14 天前 评论:

/approve

likedislike
chenjiao成员
14 天前 评论:

/approve

likedislike
CANN-robotCANN-robot成员
14 天前 添加了label:lgtmapproved
CANN-robotCANN-robot成员
14 天前 合入了pull request