已合并
feat: complete Python compilable custom-op online and offline example #4477
shangdf创建于 16 天前
feat: complete Python compilable custom-op online and offline example #4477
已合并
Pull Request已成功合入, 合并人@CANN-robot
(感谢 shangdf 的贡献)atomgit-bot
16 天前 评论:
16 天前 评论:
变更摘要
本 PR 为 GE PR 4388 新增一个独立的 Python compilable custom-op 示例(examples/custom_op/python_compilable_add_custom),仅包含示例代码与索引,不重复提交 4388 的核心实现。示例演示了通过 register_op_impl 注册的 PythonCompilableAddCustom.compile 回调获取 OpCompileContext(get_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()获取NpuArch与soc_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_dim与stream_id;编译缓存未命中时抛出RuntimeError。 - Ascend C kernel 与 op 原型注册(
kernel/add_custom.asc、proto/add_custom.h、proto/add_custom.cc):新增KernelAdd分块 Add 实现(kBlockLength=1024、kTileNum=8、kBufferNum=2,KERNEL_TYPE_AIV_ONLY);REG_OP(PythonCompilableAddCustom)定义两输入(x1、x2)一输出(y)且仅支持DT_FLOAT,InferShape将输入 shape 拷贝到输出,InferDataType同步输出数据类型。 - 在线执行入口(
src/run.py、run.sh):run.py用GraphBuilder构建在线图,以 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 声明被拒绝的契约。


atomgit-bot
16 天前 评论:
16 天前 评论:
16 天前 添加了label:cann-cla/yes
CANN-robot
16 天前 评论:
16 天前 评论:
此处折叠了192条消息 查看更多
GengChao
9 天前 评论:
9 天前 评论:
/lgtm


9 天前 添加了label:lgtm
9 天前 添加了label:approved
9 天前 合入了pull request
描述
为 GE PR 4388 提供独立的 Python compilable custom-op 示例,完整覆盖 online + offline 两条链路。本 PR 只包含
examples/custom_op下的示例、索引和离线验证工具,不重复提交 4388 的核心实现。示例使用同一个 Python
compile/declare_launch_args实现覆盖两条链路:run_online.sh通过 GESession构图,验证CustomGraphOptimizer调用 Pythoncompile、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_impl的compile能力、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.pyruff 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.pybash examples/custom_op/python_compilable_add_custom/run_online.shbash examples/custom_op/python_compilable_add_custom/run_offline.sh;该脚本在最后一步移除 Python 插件,验证 OM 执行不依赖 Python callback。