【WIP】【Hackathon 10th Sprint No.2】【RFC】跨生态自定义算子注册层兼容能力增强设计文档#1210
【WIP】【Hackathon 10th Sprint No.2】【RFC】跨生态自定义算子注册层兼容能力增强设计文档#1210gouzil wants to merge 3 commits intoPaddlePaddle:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
为“跨生态自定义算子注册层兼容能力增强”补充一份 RFC/设计文档,阐述 schema 支持、默认参数/keyword 参数绑定以及多 backend 注册与派发的整体方案与验收标准。
Changes:
- 新增跨生态自定义算子注册层兼容能力增强的设计文档(背景/目标/方案/验收/排期)。
- 给出 schema 表达、参数绑定、多 backend dispatch 的关键设计要点与示例。
- 补充参考资料与相关链接,便于后续实现对齐。
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.h \ | ||
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.cpp | ||
| - PyTorch schema 字符串解析(parseSchema/parseSchemaOrName):\ | ||
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/function_schema_parser.h \ |
There was a problem hiding this comment.
这里的两个 PyTorch 源码 URL 行尾也带了 \(紧跟在 .h/.cpp 后)。这通常会直接破坏链接(反斜杠会被算进 URL)。建议删除 URL 行尾的 \。
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.h \ | |
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.cpp | |
| - PyTorch schema 字符串解析(parseSchema/parseSchemaOrName):\ | |
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/function_schema_parser.h \ | |
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.h | |
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.cpp | |
| - PyTorch schema 字符串解析(parseSchema/parseSchemaOrName):\ | |
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/function_schema_parser.h |
| - **目标改造(对齐 PyTorch)**:注册阶段将 schema string **解析为结构化 `FunctionSchema` 并保存**,后续参数绑定/类型推导均以结构化 schema 为准;原始字符串仅作为输入,不作为持久“数据源”(需要展示时由 `FunctionSchema::toString()` 生成规范化字符串)。 | ||
| - 实现 `c10::FunctionSchema`: | ||
| - 覆盖表格中的核心类型(标量/字符串/Tensor/Optional/Tuple 等); | ||
| - 兼容 `List[T]`/`ArrayRef[T]`/固定长度列表 `T[N]` 及其可选/嵌套组合; | ||
| - 支持 alias/mutation 标注(如 `Tensor(a!)`)并保存在 schema 元数据中; | ||
| - 支持 `default value`、`*` keyword-only 分隔符; | ||
| - 保留参数名与默认值字符串,用于参数绑定。 | ||
| - Registry 以 `namespace::op` 为 key:保存 `FunctionSchema` 与 `implementations`,必要时可缓存派发所需的 `signature`/`metadata`;schema 字符串仅用于解析输入或调试展示。 |
There was a problem hiding this comment.
这里写“实现 c10::FunctionSchema”,但前文的路线是“最小子集 + 轻量实现”。建议明确是直接复用 PyTorch 的 c10::FunctionSchema 及其解析器,还是自研一个兼容层 schema(例如 CompatSchema/CompatFunctionSchema);否则命名与依赖范围容易产生误解。
| - **目标改造(对齐 PyTorch)**:注册阶段将 schema string **解析为结构化 `FunctionSchema` 并保存**,后续参数绑定/类型推导均以结构化 schema 为准;原始字符串仅作为输入,不作为持久“数据源”(需要展示时由 `FunctionSchema::toString()` 生成规范化字符串)。 | |
| - 实现 `c10::FunctionSchema`: | |
| - 覆盖表格中的核心类型(标量/字符串/Tensor/Optional/Tuple 等); | |
| - 兼容 `List[T]`/`ArrayRef[T]`/固定长度列表 `T[N]` 及其可选/嵌套组合; | |
| - 支持 alias/mutation 标注(如 `Tensor(a!)`)并保存在 schema 元数据中; | |
| - 支持 `default value`、`*` keyword-only 分隔符; | |
| - 保留参数名与默认值字符串,用于参数绑定。 | |
| - Registry 以 `namespace::op` 为 key:保存 `FunctionSchema` 与 `implementations`,必要时可缓存派发所需的 `signature`/`metadata`;schema 字符串仅用于解析输入或调试展示。 | |
| - **目标改造(对齐 PyTorch)**:注册阶段将 schema string **解析为结构化 `CompatFunctionSchema`(语义对齐 `c10::FunctionSchema`)并保存**,后续参数绑定/类型推导均以结构化 schema 为准;原始字符串仅作为输入,不作为持久“数据源”(需要展示时由 `CompatFunctionSchema::toString()` 生成规范化字符串)。 | |
| - 实现轻量 `CompatFunctionSchema`(对齐 PyTorch `c10::FunctionSchema` 的最小必要子集,而非直接引入或完整复用 c10 依赖): | |
| - 覆盖表格中的核心类型(标量/字符串/Tensor/Optional/Tuple 等); | |
| - 兼容 `List[T]`/`ArrayRef[T]`/固定长度列表 `T[N]` 及其可选/嵌套组合; | |
| - 支持 alias/mutation 标注(如 `Tensor(a!)`)并保存在 schema 元数据中; | |
| - 支持 `default value`、`*` keyword-only 分隔符; | |
| - 保留参数名与默认值字符串,用于参数绑定。 | |
| - Registry 以 `namespace::op` 为 key:保存 `CompatFunctionSchema` 与 `implementations`,必要时可缓存派发所需的 `signature`/`metadata`;schema 字符串仅用于解析输入或调试展示。 |
| |任务名称 | 跨生态自定义算子注册层兼容能力增强 | | ||
| |---|---| | ||
| | 提交作者 | gouzil | | ||
| | 提交时间 | 2026-02-02 | | ||
| | 版本号 | V1.0 | | ||
| | 依赖飞桨版本 | develop | | ||
| |文件名 | 20260202_design_Cross_for_ecosystem_Op_Reg_Compatibility.md<br> | |
There was a problem hiding this comment.
顶部信息表的 Markdown 语法不符合仓库 RFC 模板:多行以 || 开头会导致表格第一列为空、渲染异常。建议按 rfcs/design_template.md 的格式统一为单个 | 开头/分隔,并保留 |---|---| 分隔行。
| |任务名称 | 跨生态自定义算子注册层兼容能力增强 | | |
| |---|---| | |
| | 提交作者 | gouzil | | |
| | 提交时间 | 2026-02-02 | | |
| | 版本号 | V1.0 | | |
| | 依赖飞桨版本 | develop | | |
| |文件名 | 20260202_design_Cross_for_ecosystem_Op_Reg_Compatibility.md<br> | | |
| | 任务名称 | 跨生态自定义算子注册层兼容能力增强 | | |
| |---|---| | |
| | 提交作者 | gouzil | | |
| | 提交时间 | 2026-02-02 | | |
| | 版本号 | V1.0 | | |
| | 依赖飞桨版本 | develop | | |
| | 文件名 | 20260202_design_Cross_for_ecosystem_Op_Reg_Compatibility.md | |
| - **基础注册与调用**:支持 `TORCH_LIBRARY` + `TORCH_LIBRARY_IMPL` 注册,能通过 `OperatorRegistry` 查找并以 `FunctionArgs/IValue` 触发调用;当前测试均为 `DispatchKey::CPU`。 | ||
| - **增量注册**:支持 `TORCH_LIBRARY_FRAGMENT` 分片注册多个算子。 | ||
| - **自定义类**:支持 `ClassRegistry` 注册类、构造函数、成员方法与静态方法,并进行调用。 | ||
| - **参数类型覆盖**(已有测试涉及):Tensor/ScalarType/int/double/string;int const/const& 入参;`torch::optional<T>`、`c10::optional<c10::ArrayRef<int64_t>>`;`c10::ArrayRef<int64_t>`;`optional<Tensor> const&`。 |
There was a problem hiding this comment.
这里列举的参数类型包含 torch::optional/c10::optional,但后文“类型映射”表又使用 std::optional。建议全文统一 optional 类型的写法,并明确兼容层最终采用哪一种(以及原因)。
| - **参数类型覆盖**(已有测试涉及):Tensor/ScalarType/int/double/string;int const/const& 入参;`torch::optional<T>`、`c10::optional<c10::ArrayRef<int64_t>>`;`c10::ArrayRef<int64_t>`;`optional<Tensor> const&`。 | |
| - **参数类型覆盖**(已有测试涉及):Tensor/ScalarType/int/double/string;int const/const& 入参;`std::optional<T>`、`std::optional<c10::ArrayRef<int64_t>>`(兼容 `torch::optional`/`c10::optional`,在兼容层统一抽象为 `std::optional`);`c10::ArrayRef<int64_t>`;`std::optional<Tensor> const&`。 |
| | (Tensor, Tensor, Tensor)? | std::optional<std::tuple<at::Tensor, at::Tensor, at::Tensor>> | std::optional<std::tuple<paddle::Tensor, paddle::Tensor, paddle::Tensor>> | | ||
| | Tensor(a!) / Tensor(a) | at::Tensor(带别名/写标注) | Tensor + alias/mutation 标注(建议保留在 schema 元数据中) | | ||
| | float?/int?/bool?/str?/Tensor? | std::optional<T> | std::optional<T> | | ||
| | T[] / List[T] | c10::List<T> / c10::ArrayRef<T> | c10::List<T> / c10::ArrayRef<T> | |
There was a problem hiding this comment.
“类型映射”表里 optional 统一写成了 std::optional,与前文提到的 torch::optional/c10::optional 不一致。建议在表中改为与兼容层一致的 optional 类型,或在表下注明三者关系/取舍。
| - Paddle 兼容层 TORCH_LIBRARY/OperatorRegistry 等实现:\ | ||
| https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/api/include/compat/torch/library.h | ||
| - Paddle 兼容层单测(torch_library_test):\ | ||
| https://github.com/PaddlePaddle/Paddle/blob/develop/test/cpp/compat/torch_library_test.cc | ||
| - PyTorch FunctionSchema 定义:\ | ||
| https://github.com/pytorch/pytorch/blob/main/c10/core/FunctionSchema.h | ||
| - PyTorch schema 类型解析/映射(SchemaTypeParser):\ | ||
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.h \ | ||
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.cpp | ||
| - PyTorch schema 字符串解析(parseSchema/parseSchemaOrName):\ | ||
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/function_schema_parser.h \ | ||
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/function_schema_parser.cpp | ||
| - PyTorch schema 类型覆盖测试(op_registration_test.cpp):\ |
There was a problem hiding this comment.
参考资料列表用行尾反斜杠 \ 做换行(例如此处)。在 GitHub Markdown 中这容易导致渲染怪异,也可能把 \ 误当成链接内容的一部分。建议移除行尾 \,改用正常的列表换行/缩进或将链接合并到同一行。
| - Paddle 兼容层 TORCH_LIBRARY/OperatorRegistry 等实现:\ | |
| https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/api/include/compat/torch/library.h | |
| - Paddle 兼容层单测(torch_library_test):\ | |
| https://github.com/PaddlePaddle/Paddle/blob/develop/test/cpp/compat/torch_library_test.cc | |
| - PyTorch FunctionSchema 定义:\ | |
| https://github.com/pytorch/pytorch/blob/main/c10/core/FunctionSchema.h | |
| - PyTorch schema 类型解析/映射(SchemaTypeParser):\ | |
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.h \ | |
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.cpp | |
| - PyTorch schema 字符串解析(parseSchema/parseSchemaOrName):\ | |
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/function_schema_parser.h \ | |
| https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/function_schema_parser.cpp | |
| - PyTorch schema 类型覆盖测试(op_registration_test.cpp):\ | |
| - Paddle 兼容层 TORCH_LIBRARY/OperatorRegistry 等实现:https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/api/include/compat/torch/library.h | |
| - Paddle 兼容层单测(torch_library_test):https://github.com/PaddlePaddle/Paddle/blob/develop/test/cpp/compat/torch_library_test.cc | |
| - PyTorch FunctionSchema 定义:https://github.com/pytorch/pytorch/blob/main/c10/core/FunctionSchema.h | |
| - PyTorch schema 类型解析/映射(SchemaTypeParser): | |
| - https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.h | |
| - https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/schema_type_parser.cpp | |
| - PyTorch schema 字符串解析(parseSchema/parseSchemaOrName): | |
| - https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/function_schema_parser.h | |
| - https://github.com/pytorch/pytorch/blob/main/torch/csrc/jit/frontend/function_schema_parser.cpp | |
| - PyTorch schema 类型覆盖测试(op_registration_test.cpp):https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/core/op_registration/op_registration_test.cpp |
SigureMo
left a comment
There was a problem hiding this comment.
@ShigureNyako 有时间帮我 review 下,任务背景参考 https://github.com/PaddlePaddle/community/blob/master/hackathon/hackathon_10th/%E3%80%90Hackathon_10th%E3%80%91%E5%BC%80%E6%BA%90%E8%B4%A1%E7%8C%AE%E4%B8%AA%E4%BA%BA%E6%8C%91%E6%88%98%E8%B5%9B%E6%98%A5%E8%8A%82%E7%89%B9%E5%88%AB%E5%AD%A3%E2%80%94%E4%BB%BB%E5%8A%A1%E5%90%88%E9%9B%86.md#no2-%E8%B7%A8%E7%94%9F%E6%80%81%E8%87%AA%E5%AE%9A%E4%B9%89%E7%AE%97%E5%AD%90%E6%B3%A8%E5%86%8C%E5%B1%82%E5%85%BC%E5%AE%B9%E8%83%BD%E5%8A%9B%E5%A2%9E%E5%BC%BA
|
@gouzil 收到~我先快速过了一遍文档(整体结构清晰,目标/现状/方案/验收/排期都写得比较完整)。这里给几条可能有帮助的 review 建议:
整体方向我觉得 OK;如果后续需要我帮忙重点看某一块(schema parser / binder / dispatch)也可以在 PR 里 @ 我。 |
|
@ShigureNyako 帮我补充一下 1、2、3、4 |
|
@gouzil 收到,我来补充 1/2/3/4。\n\n为了避免我补错位置:你说的 1、2、3、4 是文档目录里的 4 个小节(还是 PR 模板里的条目)?你指一下对应标题/段落我就按那个结构补齐。 |
SigureMo
left a comment
There was a problem hiding this comment.
整体理解上没啥大问题
@ShigureNyako 来帮我再 review 一遍,用 gh llm,任务背景参考 https://github.com/PaddlePaddle/community/blob/master/hackathon/hackathon_10th/%E3%80%90Hackathon_10th%E3%80%91%E5%BC%80%E6%BA%90%E8%B4%A1%E7%8C%AE%E4%B8%AA%E4%BA%BA%E6%8C%91%E6%88%98%E8%B5%9B%E6%98%A5%E8%8A%82%E7%89%B9%E5%88%AB%E5%AD%A3%E2%80%94%E4%BB%BB%E5%8A%A1%E5%90%88%E9%9B%86.md#no2-%E8%B7%A8%E7%94%9F%E6%80%81%E8%87%AA%E5%AE%9A%E4%B9%89%E7%AE%97%E5%AD%90%E6%B3%A8%E5%86%8C%E5%B1%82%E5%85%BC%E5%AE%B9%E8%83%BD%E5%8A%9B%E5%A2%9E%E5%BC%BA ,代码记得看看 Paddle repo 相关的模块,技术背景你可以看 https://github.com/PaddlePaddle/docs/tree/develop/docs/guides/custom_op/cross_ecosystem_custom_op,看本地 docs repo 的文件即可
| - **IValue 基础类型**:None/bool/int/double/string/tensor/list/tuple 的构造与类型检查。 | ||
|
|
||
| 尚存在的主要不足: | ||
| - **缺乏 schema 解析/登记与绑定能力**:测试使用显式 schema 字符串与纯位置参数调用,尚未覆盖默认参数、keyword 参数、keyword-only 绑定; |
There was a problem hiding this comment.
| - **缺乏 schema 解析/登记与绑定能力**:测试使用显式 schema 字符串与纯位置参数调用,尚未覆盖默认参数、keyword 参数、keyword-only 绑定; | |
| - **缺乏 schema 解析/注册与绑定能力**:测试使用显式 schema 字符串与纯位置参数调用,尚未覆盖默认参数、keyword 参数、keyword-only 绑定; |
|
@SigureMo 收到,我来用 gh llm 帮你 review 一下这个 RFC。 整体看下来这份设计文档结构完整,背景/目标/方案/验收都有覆盖。几点建议:
技术上我没问题,主要问题是文档细节完善度。供参考~ |
为 ”跨生态自定义算子注册层兼容能力增强“ 添加设计文档