已合并
feat: 用native bindings替换ONNX plugin Python objects #4421
feat: 用native bindings替换ONNX plugin Python objects #4421
已合并
gentle-knight创建于 3 天前
gentle-knight
3 天前

描述

将 ONNX Plugin Python 对象实现切换为评审确定的原生绑定链路:OnnxNode 使用 pybind11,Operator 使用 ctypes 调用 GE C API。补充对应构建接入、Operator 借用句柄接口和单元测试。

本 PR 只提交 PR1 的源码、构建配置和测试代码,不包含设计文档、开发日志或构建产物。

变更类型

请选择本次引入的变更类型:

关联的Issue

无。

如何测试

  1. 使用 CANN 9.2.0 配置并编译 graph_wrapper_ge_onnx_plugin_native,两个目标均成功。
  2. 运行原生 OnnxNode smoke test,通过;PR1 Python 对象和 callback 测试隔离运行 25 个通过。完整 Python 测试仍需匹配 CANN 9.2.0 的 runtime wrapper。

核对清单

其他信息

  • 目标分支:develop
  • 源分支:gentle-knight:feat/onnx-plugin-python-pr1-ctypes-pybind
  • 基于最新 upstream/develop 提交 809de117d
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 gentle-knight 的贡献)
gentle-knightgentle-knight
3 天前 创建了 pull request,commit b56b789c
atomgit-bot
atomgit-bot
3 天前 评论:

变更摘要

本 PR 将 ONNX Plugin 的 Python 对象实现切换为评审确定的原生绑定链路:OnnxNode 由纯 Python 实现改为通过 pybind11 暴露的原生模块 ge.onnx_plugin._ge_onnx_plugin_native 提供;Operator 由直接操作 Python 后端改为通过 ctypes 调用 GE 的借用句柄 C API。同时新增对应 C API 实现与声明、pybind11 构建绑定代码,并同步调整相关单元测试。原 onnx_node.py 被删除,新增 _native.pynative_bindings/c_operator.cc 等文件;测试中的 Operator 仍保留 Python 后端路径以便隔离验证。

主要改动

  • OnnxNode 切换为 pybind11 原生绑定:删除 api/python/ge/ge/onnx_plugin/onnx_node.py,新增 _native.py 动态导入 ge.onnx_plugin._ge_onnx_plugin_native 并导出 OnnxNodecreate_onnx_nodeonnx_plugin/__init__.py_bridge.py 相应改为从 _native 导入。
  • 新增原生绑定实现onnx_node_binding.cc 通过 pybind11 实现 OnnxNode 的只读属性 nameorigin_typeinputsoutputsattrscreate_onnx_node,属性支持从仅 int/float 扩展为标量及同质列表,并新增 binding_common.hbindings.hmodule.cc 完成模块注册。
  • Operator 改用 ctypes 借用句柄operator.py 新增 _backend/_handle 双路径,ctypes.c_void_pint 句柄直接走 graph_lib 的 C API;set_attr 借助 _AttrValue,并新增 register_inputregister_optional_inputregister_output_get_string_register_port 等方法,Python 后端仅保留用于当前单测。
  • 新增 GE C API 与声明pygraph_wrapper.py 声明 GeApiWrapper_Operator_GetName/GetType/SetAttr 及输入输出注册等 ctypes 接口;c_operator.cc 实现这些借用 Operator 的 C API,ge_api_c_wrapper_utils.h 增加函数声明,operator.h 增加 friend class OperatorPortAccess
  • 测试同步更新python_onnx_plugin_objects_test.py 覆盖原生 OnnxNode 属性校验、字符串/列表属性、Operator 借用 c_void_p 句柄路径及各端口注册;python_onnx_plugin_bridge_test.py 移除对 register_input 等已支持接口的"未暴露"断言。
likedislike
atomgit-bot
atomgit-bot
3 天前 评论:

代码审查

我已完成对全部变更文件的审查。以下为结论汇总。

变更文件审查结果

  • api/python/ge/CMakeLists.txt — 发现 1 个问题(P3:wheel 打包未清理 onnx_plugin 构建文件)
  • api/python/ge/ge/_capi/pygraph_wrapper.py — no issues(新增的 ctypes 绑定 restype/argtypes 与既有 c_char_ptr/FreeString 模式一致)
  • api/python/ge/ge/graph/operator.py — no issues(_validate_attr_value/_get_string/set_attr_AttrValue 的 bool/int/float/str/list 分派一致,内存释放路径正确)
  • api/python/ge/ge/onnx_plugin/CMakeLists.txt — no issues(与 runtime/custom_op/passes 的 CMake 模式一致)
  • api/python/ge/ge/onnx_plugin/__init__.py — no issues
  • api/python/ge/ge/onnx_plugin/_bridge.py — no issues
  • api/python/ge/ge/onnx_plugin/_native.py — no issues
  • api/python/ge/ge/onnx_plugin/native_bindings/binding_common.h — no issues(与既有 binding_common.h 模式一致)
  • api/python/ge/ge/onnx_plugin/native_bindings/bindings.h — no issues
  • api/python/ge/ge/onnx_plugin/native_bindings/module.cc — no issues
  • api/python/ge/ge/onnx_plugin/native_bindings/onnx_node_binding.cc — 发现 1 个问题(P2:py::cast(vector) 产出 list 而非 tuple)
  • api/python/ge/ge_api_c_wrapper/CMakeLists.txt — no issues
  • api/python/ge/ge_api_c_wrapper/c_operator.cc — 发现 1 个问题(P3:错误处理宏与既有模式不一致)
  • api/python/ge/ge_api_c_wrapper/ge_api_c_wrapper_utils.h — no issues(前向声明与函数声明齐备)
  • inc/graph_metadef/external/graph/operator.h — no issues(OperatorPortAccess 前向声明 + friend 声明,配合 protected 的 InputRegister 等访问,一致)
  • tests/.../python_onnx_plugin_bootstrap_test.py — no issues(文件重命名为 ge_py_mixed_plugin.py_ge_py_onnx_plugin_ 模块前缀约定相符)
  • tests/.../python_onnx_plugin_bridge_test.py — no issues(移除已支持的接口名与 operator.py 新增方法一致)
  • tests/.../python_onnx_plugin_objects_test.py — no issues(新增测试与实现一致,但依赖上述 P2 的元组契约)
  • api/python/ge/ge/onnx_plugin/onnx_node.py(删除)— no issues

汇总

  • 按优先级统计:P0 = 0,P1 = 0,P2 = 1,P3 = 2
  • 整体风险判断:本次变更整体结构清晰、与既有 native_bindings 模式高度一致;最需关注的是 onnx_node_binding.ccinputs/outputs 属性声明为 py::tuple 却用 py::cast(vector) 构造(会返回 list 或抛类型错误),可能直接导致 test_onnx_node_exposes_immutable_flattened_values 失败并破坏对外契约,建议优先核实;其余为打包清理遗漏与错误处理宏一致性的低风险项。
类型 数量
🔴 阻塞 1
🟡 建议 0

⛔ 需要修改

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

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.
For more, you also can visit HICANN.


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
repo-cann/ge hugo111, 张德鹏, kobemini (3/2) 张德鹏 (1/1)

💡 Tip:

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

CLA Signature Pass

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

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

/lgtm

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

/approve

likedislike
CANN-robotCANN-robot成员
14 小时前 添加了label:approved
CANN-robotCANN-robot成员
14 小时前 合入了pull request