已合并
feat: 支持schema-bound自定义算子execute接口 #4188
GengChao创建于 7月30日
feat: 支持schema-bound自定义算子execute接口 #4188
已合并
Pull Request已成功合入, 合并人@CANN-robot
(感谢 GengChao 的贡献)atomgit-bot
7月30日 评论:
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中根据签名判断走新链路或回退 legacyctx调用。_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断言。


不准确?
atomgit-bot
7月30日 评论:
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 |
💬 仅评论


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


7月30日 添加了label:approved
7月30日 合入了pull request
Pull Request
描述
在已合入的自定义算子 IR 元信息 bridge 基础上,完成 Python 自定义算子 execute 的 schema-bound 调用改造:
变更类型
关联的 Issue
无。
如何测试
核对清单
其他信息
函数签名参数顺序默认与 canonical IR 顺序一致,本次不增加额外签名一致性校验。
前置pr:pr