Pull Request已成功合入, 合并人@CANN-robot
(感谢 nextyale 的贡献)变更摘要
本 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实现类型不同)。


代码审查
经过对全部 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 |
⛔ 需要修改


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
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


流水线任务触发成功
任务链接 [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执行结束


流水线任务触发成功
任务链接 [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执行结束


🟠 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 执行。这会引发:
- 性能严重退化(AI_CPU 远慢于 AICore/TVM)
- 与同批次其余 4 个 random 算子不一致(它们均使用
ImplyType::TVM,且均有 AICore 内核和 AICore 的 def 注册) - 若 AI_CPU 路径与 AICore 路径存在行为差异,可能导致正确性问题
触发条件:任何通过 TF 图适配器调用 StatelessRandomNormalV2 的场景。
建议:将 ImplyType::AI_CPU 改为 ImplyType::TVM,与该算子的 AICore def 注册、AscendC 内核及同批次其他 random 算子保持一致。
| 18
| - .ImplyType(ImplyType:: |
|
18 | + .ImplyType(ImplyType::TVM); |


/approve


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


流水线任务触发成功
任务链接 [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执行结束


流水线任务触发成功
任务链接 [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执行结束


描述
关联的Issue
测试
文档更新
类型标签