已合并
feat: 支持schema-bound自定义算子execute接口 #4188
feat: 支持schema-bound自定义算子execute接口 #4188
已合并
GengChao创建于 7月30日
GengChao
GengChao成员
7月30日

Pull Request

描述

在已合入的自定义算子 IR 元信息 bridge 基础上,完成 Python 自定义算子 execute 的 schema-bound 调用改造:

  • 为 Python binding 补充动态输入实例数读取和 RuntimeAttrs typed getter。
  • 根据 canonical IR 顺序从 context 获取 required、optional、dynamic inputs 和属性实参。
  • schema-bound 实现通过 execute(*inputs, **attrs) 调用,属性以 IR 名称传入。
  • 新增公开接口 get_execute_ctx(),供 schema-bound execute 在回调期间获取当前执行上下文。
  • get_execute_ctx() 使用 ContextVar 管理回调作用域,支持嵌套调用恢复;复制的上下文在回调结束后也会失效。
  • 保留 legacy execute(ctx) 调用方式,EagerExecuteOp 基类继续保持 execute(self, *args, **kwargs),现有 example 继续兼容。
  • 不修改现有 EagerOpExecutionContext 类。

变更类型

关联的 Issue

无。

如何测试

  • 使用 -j4 构建 install_ge_py_for_tests:通过。
  • 运行 python_custom_op_test.py:33 passed。
  • 使用 -j4 构建 ut_exe_graph:通过。
  • 运行 EagerOpExecutionContextUT.GetDynamicInputTensor:1 passed。
  • 对本次变更文件运行 pre-commit:通过。

核对清单

其他信息

函数签名参数顺序默认与 canonical IR 顺序一致,本次不增加额外签名一致性校验。
前置pr:pr

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 GengChao 的贡献)
GengChaoGengChao成员
7月30日 创建了 pull request,commit ace809d7
atomgit-bot
atomgit-bot
7月30日 评论:

变更摘要

本次 PR 为 Python 自定义算子实现了 schema-bound 的 execute 调用方式:根据 canonical IR 元信息从 EagerOpExecutionContext 中按顺序提取 required/optional/dynamic 输入和类型化属性,以 execute(*inputs, **attrs) 签名调用用户算子,同时保留对旧式 execute(ctx) 的兼容。新增了 RuntimeAttrs 绑定类、动态输入实例数查询以及属性按 IR 名称透传的能力。

主要改动

  • _bridge.py 新增 schema-bound 执行链路: 引入 _is_legacy_execute 检测旧式签名、_build_execute_inputs 按 IR 输入类型(required/optional/dynamic)从 context 获取张量、_build_execute_attrs 依据 _RUNTIME_ATTR_GETTERS 映射将属性按 IR 名称组装为 **kwargs,并在 call_execute 中根据签名判断走新链路或回退 legacy ctx 调用。
  • _ge_custom_op_native.pyi 新增 RuntimeAttrs 类型桩: 声明 get_int、get_float、get_tensor、get_list_int 等 12 个类型化 getter 及 get_attr_num,并为 EagerOpExecutionContext 补充 get_dynamic_input_num 和 get_attrs 方法签名。
  • base.py 大幅简化 EagerExecuteOp: 删除 execute(self, inputs) 的自动适配逻辑(含 _adapt_inputs_execute、_inject_ctx_methods、_restore_ctx_methods 等),将 execute 抽象方法签名改为 execute(self, *args, **kwargs),用户可直接按 execute(x, optional_y, dynamic_z, *, alpha) 形式编写算子。
  • context_binding.cc 新增 BorrowedRuntimeAttrs 绑定: 实现基于 gert::RuntimeAttrs 的 borrowed view,通过 GetRequiredAttr + 类型特定辅助函数(BuildTypedList、BuildDataTypeList、BuildBoolList 等)暴露类型安全的属性读取接口;同时为 BorrowedEagerOpExecutionContext 增加 GetDynamicInputNum 和 GetAttrs 并将两者导出到 Python。
  • 测试适配与覆盖: python_custom_op_test.py 删除旧 execute(self, inputs) 适配测试,新增 test_schema_bound_execute_binds_schema_inputs_and_attrs、test_bridge_reads_runtime_attr_by_canonical_type 参数化测试、无 IR meta 时的错误路径测试等;C++ 单测 eager_op_execution_context_unittest.cc 新增 GetIrInputInstanceInfo 和 GetInstanceNum 断言。
likedislike
不准确?
atomgit-bot
atomgit-bot
7月30日 评论:

代码审查

审查总结

经过对所有 6 个变更文件的逐行审查,共发现 1 个问题:

  • P3: 1 个(BuildStringList 中无边界保护的 strlen 可能导致越界读取)

各文件审查结果:

文件 结果
api/python/ge/ge/custom_op/_bridge.py 无问题 — _is_legacy_execute、_build_execute_inputs、_build_execute_attrs、call_execute 逻辑正确,边界情况处理到位,finally 块保证 ctx._invalidate() 始终执行
api/python/ge/ge/custom_op/_ge_custom_op_native.pyi 无问题 — 新增 RuntimeAttrs 类型桩与 C++ binding 完全一致,EagerOpExecutionContext 新增方法签名正确
api/python/ge/ge/custom_op/base.py 无问题 — __init_subclass__ 适配器移除是有意为之的 API 简化;新抽象方法签名 execute(self, *args, **kwargs) 兼容旧版 execute(self, ctx)(Liskov 替换原则),仅 execute(self, inputs) 旧模式不再支持,属于有意 breaking change
api/python/ge/ge/custom_op/native_bindings/context_binding.cc 1 个 P3(见上);其余 BorrowedRuntimeAttrs / BorrowedEagerOpExecutionContext 新增方法正确,valid_ 共享指针生命周期管理正确,pybind11 绑定注册完整
tests/ge/ut/ge/graph/pyge_tests/python_custom_op_test.py 无问题 — 新增测试覆盖 schema-bound、零参数、属性读取、缺失 IR meta 拒绝等关键路径;_FakeEagerContext / _FakeRuntimeAttrs 测试替身行为正确
tests/graph_metadef/ut/exe_graph/eager_op_execution_context_unittest.cc 无问题 — 新增两行断言合理增强 GetDynamicInputTensor 测试

整体风险判断:低。 变更设计清晰、逻辑严密,异常路径通过 finally 块妥善保护,C++ 侧生命周期管理通过 shared_ptr<bool> valid_ 正确传递。唯一的 P3 发现来自受信框架内部数据的理论边界风险,实际触发概率极低。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
不准确?
CANN-robotCANN-robot成员
7月30日 添加了label:stat/needs-squash
CANN-robotCANN-robot成员
7月30日 添加了label:cann-cla/yes
此处折叠了53条消息 查看更多
CANN-robotCANN-robot成员
7月30日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月30日 添加了label:ci-pipeline-passed
zhanj
zhanj成员
7月30日 评论:

/approve

likedislike
CANN-robotCANN-robot成员
7月30日 添加了label:approved
CANN-robotCANN-robot成员
7月30日 合入了pull request