已合并
feat: 用native bindings替换ONNX plugin Python objects #4421
gentle-knight创建于 3 天前
feat: 用native bindings替换ONNX plugin Python objects #4421
已合并
Pull Request已成功合入, 合并人@CANN-robot
(感谢 gentle-knight 的贡献)3 天前 创建了 pull request,commit b56b789c
atomgit-bot
3 天前 评论:
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.py、native_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并导出OnnxNode、create_onnx_node,onnx_plugin/__init__.py与_bridge.py相应改为从_native导入。- 新增原生绑定实现:
onnx_node_binding.cc通过 pybind11 实现OnnxNode的只读属性name、origin_type、inputs、outputs、attrs及create_onnx_node,属性支持从仅int/float扩展为标量及同质列表,并新增binding_common.h、bindings.h、module.cc完成模块注册。 Operator改用 ctypes 借用句柄:operator.py新增_backend/_handle双路径,ctypes.c_void_p或int句柄直接走graph_lib的 C API;set_attr借助_AttrValue,并新增register_input、register_optional_input、register_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等已支持接口的"未暴露"断言。


atomgit-bot
3 天前 评论:
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 issuesapi/python/ge/ge/onnx_plugin/_bridge.py— no issuesapi/python/ge/ge/onnx_plugin/_native.py— no issuesapi/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 issuesapi/python/ge/ge/onnx_plugin/native_bindings/module.cc— no issuesapi/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 issuesapi/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.cc中inputs/outputs属性声明为py::tuple却用py::cast(vector)构造(会返回 list 或抛类型错误),可能直接导致test_onnx_node_exposes_immutable_flattened_values失败并破坏对外契约,建议优先核实;其余为打包清理遗漏与错误处理宏一致性的低风险项。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 0 |
⛔ 需要修改


3 天前 添加了label:cann-cla/yes
CANN-robot
3 天前 评论:
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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


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


1 天前 添加了label:lgtm
14 小时前 添加了label:approved
14 小时前 合入了pull request
描述
将 ONNX Plugin Python 对象实现切换为评审确定的原生绑定链路:OnnxNode 使用 pybind11,Operator 使用 ctypes 调用 GE C API。补充对应构建接入、Operator 借用句柄接口和单元测试。
本 PR 只提交 PR1 的源码、构建配置和测试代码,不包含设计文档、开发日志或构建产物。
变更类型
请选择本次引入的变更类型:
关联的Issue
无。
如何测试
graph_wrapper和_ge_onnx_plugin_native,两个目标均成功。核对清单
其他信息
developgentle-knight:feat/onnx-plugin-python-pr1-ctypes-pybindupstream/develop提交809de117d。