Pull Request已成功合入, 合并人@CANN-robot
(感谢 陈思 的贡献)变更摘要
此 PR 修复了 OpGenerator 在已有分类目录中生成新算子时,错误地向祖先 CMakeLists.txt 重复追加 add_subdirectory(<category>) 的问题。通过在初始化时记录分类目录是否已存在,并在 _create_category_cmake 的向上遍历中提前终止,避免了重复添加。同时新增了完整的单元测试覆盖默认模板、AI CPU 模板、已有分类和新分类等场景。
主要改动
- 在
OpGenerator.__init__中新增category_dir和category_existed属性:生成器初始化时记录分类目录的绝对路径及其是否已存在,为后续判断提供依据。 - 在
_create_category_cmake的向上遍历中加入提前终止逻辑:当分类目录在生成前已存在且当前遍历的父目录恰为分类目录时,执行break不再继续向祖先追加add_subdirectory,仅更新分类自身的 CMake 文件。 - 新增
scripts/opgen/tests/test_opgen_standalone.py测试文件:添加OpGeneratorCmakeChainTest类,包含 4 个回归测试用例,覆盖已有分类不修改祖先、已有显式分类仅添加算子、新分类正确接入祖先、以及 AI CPU 模板复用相同规则等场景。


代码审查
我已经仔细审查了两个变更文件的全部差异及周围的完整代码。以下是逐文件分析:
文件 1: scripts/opgen/opgen_standalone.py
变更内容:
-
在
__init__中新增两个属性(第 32-33 行):self.category_dir:分类目录的绝对路径self.category_existed:记录生成前分类目录是否已存在
-
在
_update_cmake_chain的 while 循环末尾新增提前退出逻辑(第 189-194 行):- 当分类在生成前已存在,且当前处理的父目录恰好是分类目录时,
break跳出循环,不再继续向上遍历祖先 CMakeLists.txt。
- 当分类在生成前已存在,且当前处理的父目录恰好是分类目录时,
逻辑审查:
self.category_existed在__init__中设置,在_copy_template之前,因此准确反映了"生成前"的状态。✓- 新的
break条件self.category_existed and parent_abs == self.category_dir仅在首轮迭代时触发(因为self.dest_dir的父目录即为self.category_dir),此时分类自身的 CMakeLists.txt 已经被处理完毕(追加了算子或确认为 glob 覆盖)。✓ - 对于新分类(
category_existed=False),break条件不满足,循环继续向上遍历到project_root,正确地将新分类接入祖先构建系统。✓ - 路径比较使用
os.path.abspath确保一致性。✓ - 不存在竞态条件(单线程脚本)。✓
文件 2: scripts/opgen/tests/test_opgen_standalone.py
变更内容: 新增 108 行测试文件,包含四个测试用例:
| 测试 | 场景 | 覆盖点 |
|---|---|---|
test_existing_category_does_not_modify_ancestor |
已有分类 + glob CMake | 分类与祖先 CMake 均不被修改 |
test_existing_explicit_category_adds_only_operator |
已有分类 + 空 CMake | 仅分类 CMake 追加算子,祖先不变 |
test_new_category_is_connected_to_ancestor |
新分类 | 分类 CMake 被创建,祖先追加 add_subdirectory |
test_aicpu_uses_same_existing_category_rule |
aicpu 模板 + 已有分类 | 祖先不被修改 |
审查结论:
- 测试覆盖了默认模板、AI CPU 模板、已有分类(glob 和显式空 CMake)、新分类四种场景。✓
setUp/tearDown使用tempfile.TemporaryDirectory确保隔离。✓_write/_read辅助方法正确处理文件和目录。✓- 所有断言与
opgen_standalone.py的实际行为一致。✓
最终结论
经过对两个变更文件的逐行审查及上下文追踪,未发现任何正确性、安全性、可靠性或回归问题。此 diff 的修改精准地解决了 Issue #2288 描述的问题(已有分类中重复追加 add_subdirectory),同时正确保留了新分类的完整 CMake 链构建行为。
| 优先级 | 数量 |
|---|---|
| P0 | 0 |
| P1 | 0 |
| P2 | 0 |
| P3 | 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 | ✅ 宋恺, wangrui_ (2/2) | ✅ 宋恺, wangrui_ (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
chensi79, thanks for your pull request. All authors of the commits have signed the CLA. 👍


compile


流水线任务触发成功
任务链接 [df8bbbfde751402490135ebb07d49501][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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-23 10:09:00] CI执行结束


流水线任务触发成功
任务链接 [e87c73aade7c4210890388e8417cca6f][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| SCA | ❌ FAILED | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_Pr | ✅ SUCCESS | ||
| StaticCheck_codespell | ✅ SUCCESS | ||
| StaticCheck_link_validity | ✅ SUCCESS | ||
| StaticCheck_resource_existence | ✅ SUCCESS | ||
| StaticCheck_tag_closed | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_codestyle | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ❌ FAILED | >>>>> | >>>>> |


compile


流水线任务触发成功
任务链接 [d4469f2bc6c0486eb243bb69f4356622][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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-23 10:24:22] CI执行结束


流水线任务触发成功
任务链接 [29df0382badd49239d2c1d75299014ee][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| SCA | ❌ FAILED | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_Pr | ✅ SUCCESS | ||
| StaticCheck_codespell | ✅ SUCCESS | ||
| StaticCheck_link_validity | ✅ SUCCESS | ||
| StaticCheck_resource_existence | ✅ SUCCESS | ||
| StaticCheck_tag_closed | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_codestyle | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ✅ SUCCESS | >>>>> |


compile


流水线任务触发成功
任务链接 [2862fa358c6c478dbd44f9131d099de6][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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-23 10:45:58] CI执行结束


流水线任务触发成功
任务链接 [eb3ad7e716554d598caa5b3e9c4cdd10][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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_codestyle | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ✅ SUCCESS | >>>>> |
[2026-07-23 10:38:26] CI执行结束


/lgtm
/approve


描述
--genop=experimental/<category>/<op>在已有分类中生成算子时,错误向祖先CMakeLists.txt重复追加add_subdirectory(<category>)的问题。关联的Issue
https://gitcode.com/cann/ops-math/issues/2288
测试
bash build.sh --genop=experimental/math/sigmoid:算子模板生成成功,experimental/CMakeLists.txt保持不变。ENABLE_EXPERIMENTAL=TRUE执行完整 CMake configure:配置和生成成功,不再出现 binary directory 重复错误。文档更新
不涉及。
类型标签