Merged
feat: AICPU自定义算子加载支持从ASCEND_OPP_PATH/vendors推导路径 #698
feat: AICPU自定义算子加载支持从ASCEND_OPP_PATH/vendors推导路径 #698
Merged
liu-weiAdd on Aug 4
liu-wei
liu-weiCollaborators
Aug 4

Closes #311

描述

当 ASCEND_CUSTOM_OPP_PATH 未设置或为空时,自动从 ASCEND_OPP_PATH/vendors/config.ini 推导自定义算子包路径,避免用户必须显式设置环境变量。

推导流程

ASCEND_CUSTOM_OPP_PATH 已设置且非空 → 直接使用
ASCEND_CUSTOM_OPP_PATH 未设置或为空 → 读取 ASCEND_OPP_PATH/vendors/config.ini
                                     → 解析 priority/load_priority 字段获取 vendor 列表
                                     → 逐个检查 vendor 目录下是否存在 cust_aicpu_kernel.json
                                     → 拼接为 customOppPath

新增文件

文件 行数 说明
aicpu_json_load_utest.cpp 208 vendor 路径推导 UT,含 priority= 和 load_priority= 两种格式

修改文件

文件 变更 说明
aicpu_json_load_manager.cpp 新增 GetCustomOppPathFromVendors();CustJsonLoadAndParse() 增加 fallback 分支;config.ini 解析兼容 priority= 和 load_priority= 两种格式
aicpu_json_load_manager.h 声明 GetCustomOppPathFromVendors()
CMakeLists.txt UT 头文件搜索路径补充 aicpu/task_handler/

关键逻辑

  • config.ini 解析:逐行读取,跳过空行和 # 注释,匹配含 priority 的行(兼容 priority= 和 load_priority=),取 = 后的值用逗号分隔 vendor 名
  • vendor 名 trim:去除前后空格和 Tab,避免 load_priority=vnd_a, vnd_b 逗号后空格导致路径拼接错误
  • vendor 路径校验:通过 RealPath() 检查 cust_aicpu_kernel.json 是否存在,不存在则跳过
  • 与 op_kernel_lib.cpp、indv_collector.cpp 的 config.ini 解析方式对齐

场景对比

场景 基线 (bf44e6c) 当前 (我的改动) 差异
设 CUSTOM_OPP_PATH=path_math:path_nn 按冒号分割,依次加载 path_math、path_nn 的 cust_aicpu_kernel.json 完全相同 无
设 CUSTOM_OPP_PATH=path_math 只加载 path_math,nn 包不注册 完全相同 无
设 CUSTOM_OPP_PATH=""(空串) 走 else 但 pathEnv="" 无效,不加载自定义包 判空串,fallback 到 vendors 推导 修复:空串不再漏过
不设 CUSTOM_OPP_PATH,vendors 有 math+nn ❌ 不加载任何自定义包 ✅ 从 config.ini 读 priority,两个包都加载 核心改动
不设 CUSTOM_OPP_PATH,vendors 只有 math ❌ 不加载 math 自定义包 ✅ 从 config.ini 推导,加载 vendors/math 核心改动
不设 CUSTOM_OPP_PATH,config.ini 用 priority= 格式 ❌ 不加载 ✅ 兼容 priority= 和 load_priority= 兼容性修复
不设 CUSTOM_OPP_PATH,vendors 没有 config.ini ❌ 不加载自定义包 ❌ GetCustomOppPathFromVendors 返回 false,不加载 无
不设 CUSTOM_OPP_PATH,vendor 下无 cust_aicpu_kernel.json ❌ 不加载 ❌ 该 vendor 被 RealPath 检查跳过,不加载 无(但更健壮)
内置包加载 扫 built-in/op_impl/aicpu/config/*.json 完全相同 无
优先级控制 冒号顺序(左优先) 设了:冒号顺序;不设:config.ini 的 priority 顺序 不设时新增了优先级来源
同名算子冲突 emplace 先到先得,后到的 warning 丢弃 完全相同 无
aicpuCustLoadFlag_ 防重 末尾置位 完全相同 无
ParseCustOpInfo / FillCustOpInfos 区分 CUSTOM+BUILTIN 完全相同 无

关联的Issue

#311

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for liu-wei's contribution.)
liu-weiliu-weiCollaborators
Aug 4 Created pull request, commit 1e7cebed
atomgit-bot
atomgit-bot
Aug 4 Comments:

变更摘要

此 PR 为 AICPU 自定义算子加载新增了从 ASCEND_OPP_PATH/vendors 目录推导自定义算子路径的能力。当 ASCEND_CUSTOM_OPP_PATH 环境变量未设置或为空时,系统会回退到解析 ASCEND_OPP_PATH/vendors/config.ini 中的 load_priority(原 priority)配置,依次查找各 vendor 子目录下的 cust_aicpu_kernel.json 文件进行加载;同时保留对 ASCEND_OPP_PATH 根目录下同名 json 文件的支持。为便于测试,在头文件中通过 #ifdef RUN_TEST 暴露了 ResetForTest() 方法,并新增了覆盖多种场景的单元测试文件。

主要改动

  • 新增 GetCustomOppPathFromVendors 方法:在 aicpu_json_load_manager.cpp 中实现,读取 ASCEND_OPP_PATH/vendors/config.ini 的 load_priority 配置行,解析出 vendor 名称列表,依次拼接 vendor 路径并检查是否存在 cust_aicpu_kernel.json,同时将 ASCEND_OPP_PATH 根目录下的同名 json 也作为候选路径加入。
  • CustJsonLoadAndParse 增加回退逻辑:原逻辑仅依赖 ASCEND_CUSTOM_OPP_PATH 环境变量;现改为当该变量为空或未设置时,调用 GetCustomOppPathFromVendors 从 vendors 推导路径,实现自动回退加载。
  • 头文件新增测试辅助接口 ResetForTest:在 aicpu_json_load_manager.h 中通过 #ifdef RUN_TEST 条件编译暴露静态方法 ResetForTest(),用于重置所有内部静态状态(json 缓存、注册表、二进制句柄等),供单元测试在 TearDown 中清理。
  • 新增单元测试文件 aicpu_json_load_utest.cpp:覆盖七种典型场景,包括 ASCEND_CUSTOM_OPP_PATH 直接使用、空值回退到 vendors、未设置回退到 vendors、双环境变量优先级验证、无任何自定义 json、vendor 缺少 json 自动跳过、双环境变量均无效等,验证 CustJsonLoadAndParse 与 FindAndGetInCustomRegistry 的正确行为。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 4 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 4 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 4 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


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
repo-cann/opbase ✅ 宋恺, wangrui_ (2/2) ✅ 宋恺, wangrui_ (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
114 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 13 Added label: approved
RuiWang_Collaborators
Aug 13 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 13 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 13 Closed associated issue
CANN-robotCANN-robotCollaborators
Aug 13 Merged pull request