已合并
feat: complete Python compilable custom-op online and offline example #4477
feat: complete Python compilable custom-op online and offline example #4477
已合并
shangdf创建于 16 天前
shangdf成员
16 天前

描述

为 GE PR 4388 提供独立的 Python compilable custom-op 示例,完整覆盖 online + offline 两条链路。本 PR 只包含
examples/custom_op 下的示例、索引和离线验证工具,不重复提交 4388 的核心实现。

示例使用同一个 Python compile / declare_launch_args 实现覆盖两条链路:

  • 在线:run_online.sh 通过 GE Session 构图,验证 CustomGraphOptimizer 调用 Python compile、Bisheng 生成 Ascend C kernel,并在线执行 x + y
  • 离线:run_offline.sh 由 C++ 程序生成 AIR,ATC 在 Python 插件可加载时调用同一个 compile 回调生成 OM;随后清除 Python 插件路径,仅保留 C++ OPP 交付件加载 OM 并执行。

平台信息使用 4388 的最终 API get_compile_platform_info()ASCEND_CUSTOM_OPP_PATH 同时配置 OPP 根目录和 Python 插件目录。

依赖关系

本 PR 依赖 GE PR 4388 提供的 Python compilable 接口(register_op_implcompile 能力、get_compile_platform_info()、native bridge)。请使用 4388 合入后的 GE/CANN 运行包进行完整验证。

如何测试

  • python3 -m pytest -q examples/custom_op/python_compilable_add_custom/test/test_python_compilable_add_custom.py
  • ruff check examples/custom_op/python_compilable_add_custom/src/ge/python_compilable_add_custom.py examples/custom_op/python_compilable_add_custom/src/run.py examples/custom_op/python_compilable_add_custom/test/test_python_compilable_add_custom.py
  • 配置 CANN、Bisheng 和 NPU 后执行 bash examples/custom_op/python_compilable_add_custom/run_online.sh
  • 配置 ATC 后执行 bash examples/custom_op/python_compilable_add_custom/run_offline.sh;该脚本在最后一步移除 Python 插件,验证 OM 执行不依赖 Python callback。
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 shangdf 的贡献)
Sshangdf成员
16 天前 创建了 pull request,commit bd1b9b58
atomgit-bot
atomgit-bot
16 天前 评论:

变更摘要

本 PR 为 GE PR 4388 新增一个独立的 Python compilable custom-op 示例(examples/custom_op/python_compilable_add_custom),仅包含示例代码与索引,不重复提交 4388 的核心实现。示例演示了通过 register_op_impl 注册的 PythonCompilableAddCustom.compile 回调获取 OpCompileContextget_compile_ctx()),调用 bisheng 在线编译 Ascend C kernel 并提取设备二进制,再通过 declare_launch_args 发布 AnnotatedKernelLaunchInfo 完成在线执行校验。该示例为 online-only,不包含 ATC/OM 流程,依赖 4388 提供的 compile 能力与 native bridge。

主要改动

  • Python 在线编译回调核心实现src/ge/python_compilable_add_custom.py):通过 register_op_impl(op_type="PythonCompilableAddCustom") 注册算子类,compile 方法校验 Tensor 后经 get_compile_ctx() 获取 NpuArchsoc_version,调用 bisheng 编译 add_custom.asc 并用 llvm-objcopy 提取 .aicore_binary,按 tensor shape/dtype 组合键缓存 KernelArtifact(kernel_name、kernel_bin、block_dim)。
  • Launch 参数发布python_compilable_add_custom.py):declare_launch_args 通过 get_declare_launch_args_ctx() 创建 kernel args,依次 append_input/append_output 绑定输入输出,并 add_launch(AnnotatedKernelLaunchInfo(...), args) 发布 kernel 二进制、block_dimstream_id;编译缓存未命中时抛出 RuntimeError
  • Ascend C kernel 与 op 原型注册kernel/add_custom.ascproto/add_custom.hproto/add_custom.cc):新增 KernelAdd 分块 Add 实现(kBlockLength=1024kTileNum=8kBufferNum=2KERNEL_TYPE_AIV_ONLY);REG_OP(PythonCompilableAddCustom) 定义两输入(x1x2)一输出(y)且仅支持 DT_FLOATInferShape 将输入 shape 拷贝到输出,InferDataType 同步输出数据类型。
  • 在线执行入口src/run.pyrun.sh):run.pyGraphBuilder 构建在线图,以 float32 ND 数据运行 session.run_graph 并校验输出(PY_COMPILE_ONLINE_NPU=PASS);run.sh 负责 cmake 构建 GE ES 包、打包 proto 库、无网络 staging Python wrapper,设置 PYTHONPATH/LD_LIBRARY_PATH/ASCEND_CUSTOM_OPP_PATH 后执行并通过日志关键字断言成功。
  • 单测覆盖test/test_python_compilable_add_custom.py):以桩模块替换 ge.custom_op/ge.runtime 与编译过程,验证 compile 产物可供 declare_launch_args 发布,以及编译缓存缺失时 launch 声明被拒绝的契约。
likedislike
atomgit-bot
atomgit-bot
16 天前 评论:

代码审查

✅ 未发现问题

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

CLA Signature Pass

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

likedislike
此处折叠了192条消息 查看更多
GengChao
GengChao成员
9 天前 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
9 天前 添加了label:lgtm
张德鹏成员
9 天前 评论:

/approve

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