已合并
迁移开源TF插件--random #4309
nextyale创建于 16 天前
迁移开源TF插件--random #4309
已合并
nextyale创建于 16 天前
nextyale成员
16 天前

描述

关联的Issue

测试

文档更新

类型标签

  • Bug修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他,请描述:迁移TF插件
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 nextyale 的贡献)
Nnextyale成员
16 天前 创建了 pull request,commit b70f5905
atomgit-bot
atomgit-bot
16 天前 评论:

变更摘要

本 PR 将 5 个 TensorFlow 开源随机算子插件迁移至 CANN 框架,在 random 目录下新增对应的框架注册文件。每个插件通过 REGISTER_CUSTOM_OP 宏将自定义算子注册为 TensorFlow 框架类型,并采用 AutoMappingByOpFn 自动参数映射方式,实现算子从 TF 到 CANN 的适配。

主要改动

  • 新增 DropOutDoMask 算子注册: 在 random/drop_out_do_mask/framework/drop_out_do_mask_tf_plugin.cpp 中注册 DropOutDoMask,框架类型为 TENSORFLOW,实现类型为 TVM
  • 新增 DropOutDoMaskV3 算子注册: 在 random/drop_out_do_mask_v3/framework/drop_out_do_mask_v3_tf_plugin.cpp 中注册 DropOutDoMaskV3,框架类型为 TENSORFLOW,实现类型为 TVM
  • 新增 DropOutV3 算子注册: 在 random/drop_out_v3/framework/drop_out_v3_tf_plugin.cpp 中注册 DropOutV3,原始算子类型映射为 StatelessDropout,框架类型为 TENSORFLOW,实现类型为 TVM
  • 新增 StatelessRandomChoiceWithMask 算子注册: 在 random/stateless_random_choice_with_mask/framework/stateless_random_choice_with_mask_tf_plugin.cpp 中注册该算子,框架类型为 TENSORFLOW,实现类型为 TVM
  • 新增 StatelessRandomNormalV2 算子注册: 在 random/stateless_random_normal_v2/framework/stateless_random_normal_v2_tf_plugin.cpp 中注册该算子,框架类型为 TENSORFLOW,实现类型为 AI_CPU(与其余 4 个算子的 TVM 实现类型不同)。
likedislike
atomgit-bot
atomgit-bot
16 天前 评论:

代码审查

经过对全部 5 个变更文件的逐行审查以及与现有代码库中同类 TF 插件文件的对比,我未发现任何具体的正确性、安全、可靠性或回归问题。

以下是每个文件的审查结论:


审查结论汇总:

文件 审查结果
random/drop_out_do_mask/framework/drop_out_do_mask_tf_plugin.cpp 无问题 —— 注册模式与现有 add_tf_plugin.cpp / left_shift_tf_plugin.cpp 一致,REGISTER_CUSTOM_OP.OriginOpType 匹配,用 ImplyType::TVM
random/drop_out_do_mask_v3/framework/drop_out_do_mask_v3_tf_plugin.cpp 无问题 —— 同上,命名一致
random/drop_out_v3/framework/drop_out_v3_tf_plugin.cpp 无问题 —— .OriginOpType("StatelessDropout")REGISTER_CUSTOM_OP("DropOutV3") 不同名,但经过语义分析,DropOutV3(含 seed/offset 输入)对应 TF 的 stateless 系列 op 命名约定,此映射为有意为之,非拷贝粘贴错误
random/stateless_random_choice_with_mask/framework/stateless_random_choice_with_mask_tf_plugin.cpp 无问题 —— 命名一致,使用 ImplyType::TVM
random/stateless_random_normal_v2/framework/stateless_random_normal_v2_tf_plugin.cpp 无问题 —— 命名一致,使用 ImplyType::AI_CPU 而非 TVM,可能是该算子的 TF 路径仅支持 AI_CPU 执行,属设计选择

发现数: P0: 0, P1: 0, P2: 0, P3: 0

整体风险评估: 低风险。所有 5 个文件均为新增的算子注册文件,代码模式与现有插件一致,无逻辑缺陷、安全漏洞或兼容性问题。

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

⛔ 需要修改

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

CLA Signature Pass

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

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

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
repo-cann/ops-math 宋恺, 冯彤 (2/2) 宋恺 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robotCANN-robot成员
16 天前 将zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,zl_hw,songkai111,llimwang,gubaocheng,andong_hw,xuejinghui,zhanw_coding设为评审人
CANN-robotCANN-robot成员
16 天前 将zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,zl_hw,songkai111,llimwang,gubaocheng,andong_hw,xuejinghui,zhanw_coding设为审查人
nextyale成员
16 天前 评论:

/compile

likedislike
Nnextyale成员
16 天前 update merge request[project id: 7649531, iid: 4309, commit_id: 245f483bc6a4caff85b79c8508a65396ccf0136f] virtual merging success
CANN-robotCANN-robot成员
16 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
16 天前 评论:

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

任务名称状态日志下载链接
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

[2026-07-28 22:22:23]    CI执行结束

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

流水线任务触发成功
任务链接 [7095e175507947db98e98f39d2db96c1][流水线指导]

任务名称状态日志下载链接
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-07-28 22:13:49]    CI执行结束

likedislike
Nnextyale成员
16 天前 修改了pull request 的描述
Nnextyale成员
16 天前 修改标题为 “迁移开源TF插件--random”,原标题为“[feat] 迁移开源TF插件--random”
CANN-robotCANN-robot成员
16 天前 添加了label:api-check-pass
CANN-robotCANN-robot成员
16 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
16 天前 添加了label:ci-pipeline-passed
atomgit-bot
atomgit-bot16 天前进行代码检视1
random/stateless_random_normal_v2/framework/stateless_random_normal_v2_tf_plugin.cpp
@@ -0,0 +15,4 @@
15+ .FrameworkType(TENSORFLOW)
16+ .OriginOpType("StatelessRandomNormalV2")
17+ .ParseParamsByOperatorFn(AutoMappingByOpFn)
18+ .ImplyType(ImplyType::AI_CPU);
atomgit-bot
atomgit-bot16 天前评论:

🟠 High Priority

变更行(第 18 行):.ImplyType(ImplyType::AI_CPU);

受影响的契约:REGISTER_CUSTOM_OP("StatelessRandomNormalV2") 在 TF 插件中声明的 ImplyType 决定了 TF 图适配器为该算子创建的 CANN 节点类型。

失败模式:该算子的 op_def 注册(stateless_random_normal_v2_def.cpp 第 57 行)使用 this->AICore().AddConfig("ascend950"),内核实现(stateless_random_normal_v2.cpp 第 24 行)是 extern "C" __global__ __aicore__ 的 AscendC 内核,op_api 调度(stateless_random_normal_v2.cpp 第 29-30 行)的 AICORE 支持列表为 {DT_FLOAT, DT_FLOAT16, DT_BF16}。但 TF 插件却声明为 AI_CPU,这会导致框架将所有调用路由到 AI_CPU 路径,即使 dtype 完全支持 AICore 执行。这会引发:

  1. 性能严重退化(AI_CPU 远慢于 AICore/TVM)
  2. 与同批次其余 4 个 random 算子不一致(它们均使用 ImplyType::TVM,且均有 AICore 内核和 AICore 的 def 注册)
  3. 若 AI_CPU 路径与 AICore 路径存在行为差异,可能导致正确性问题

触发条件:任何通过 TF 图适配器调用 StatelessRandomNormalV2 的场景。

建议:将 ImplyType::AI_CPU 改为 ImplyType::TVM,与该算子的 AICore def 注册、AscendC 内核及同批次其他 random 算子保持一致。

改动建议
18
- .ImplyType(ImplyType::AI_CPU);
18
+ .ImplyType(ImplyType::TVM);
应用建议
likedislike
songkai111成员
11 天前 评论:

/approve

likedislike
CANN-robotCANN-robot成员
11 天前 添加了label:approved
rxtfeng成员
11 天前 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
11 天前 添加了label:lgtm
CANN-robotCANN-robot成员
11 天前 删除了label:ci-pipeline-passed
CANN-robot
CANN-robot成员
11 天前 评论:

The following label is not ready.

ci-pipeline-passed: The ci-pipeline-passed label is expired. Please compile again.

likedislike
nextyale成员
11 天前 评论:

/compile

likedislike
Nnextyale成员
11 天前 update merge request[project id: 7649531, iid: 4309, commit_id: 8cf341f81bbcd359959d2a47bdad1d3c985b484c] virtual merging success
CANN-robotCANN-robot成员
11 天前 删除了label:api-check-pass
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
11 天前 评论:

流水线任务触发成功
任务链接 [3748d142371f4ab690807313d3b74575][流水线指导]

任务名称状态日志下载链接
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-03 10:22:46]    CI执行结束

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

流水线任务触发成功
任务链接 [1a17f60ab66542f39e626687a8a6401d][流水线指导]

任务名称状态日志下载链接
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-03 10:08:17]    CI执行结束

likedislike
CANN-robotCANN-robot成员
11 天前 添加了label:api-check-pass
CANN-robotCANN-robot成员
11 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-passed
CANN-robotCANN-robot成员
11 天前 解决了最后一个问题
CANN-robotCANN-robot成员
11 天前 合入了pull request