Skip to content

【Hackathon 10th Sprint No.2】【RFC】跨生态自定义算子注册层兼容能力增强设计文档#1210

Merged
luotao1 merged 5 commits intoPaddlePaddle:masterfrom
gouzil:feat/add_c10_func_schema
Feb 26, 2026
Merged

【Hackathon 10th Sprint No.2】【RFC】跨生态自定义算子注册层兼容能力增强设计文档#1210
luotao1 merged 5 commits intoPaddlePaddle:masterfrom
gouzil:feat/add_c10_func_schema

Conversation

@gouzil
Copy link
Copy Markdown
Member

@gouzil gouzil commented Feb 3, 2026

为 ”跨生态自定义算子注册层兼容能力增强“ 添加设计文档

Copilot AI review requested due to automatic review settings February 3, 2026 02:45
@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented Feb 3, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请检查PR提交格式和内容是否完备,具体请参考示例模版
Your PR has been submitted. Thanks for your contribution!
Please check its format and content. For this, you can refer to Template and Demo.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +330 to +333
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 \
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的两个 PyTorch 源码 URL 行尾也带了 \(紧跟在 .h/.cpp 后)。这通常会直接破坏链接(反斜杠会被算进 URL)。建议删除 URL 行尾的 \

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment on lines +109 to +116
- **目标改造(对齐 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 字符串仅用于解析输入或调试展示。
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里写“实现 c10::FunctionSchema”,但前文的路线是“最小子集 + 轻量实现”。建议明确是直接复用 PyTorch 的 c10::FunctionSchema 及其解析器,还是自研一个兼容层 schema(例如 CompatSchema/CompatFunctionSchema);否则命名与依赖范围容易产生误解。

Suggested change
- **目标改造(对齐 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 字符串仅用于解析输入或调试展示。

Copilot uses AI. Check for mistakes.
Comment on lines +3 to +9
|任务名称 | 跨生态自定义算子注册层兼容能力增强 |
|---|---|
| 提交作者 | gouzil |
| 提交时间 | 2026-02-02 |
| 版本号 | V1.0 |
| 依赖飞桨版本 | develop |
|文件名 | 20260202_design_Cross_for_ecosystem_Op_Reg_Compatibility.md<br> |
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

顶部信息表的 Markdown 语法不符合仓库 RFC 模板:多行以 || 开头会导致表格第一列为空、渲染异常。建议按 rfcs/design_template.md 的格式统一为单个 | 开头/分隔,并保留 |---|---| 分隔行。

Suggested change
|任务名称 | 跨生态自定义算子注册层兼容能力增强 |
|---|---|
| 提交作者 | 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 |

Copilot uses AI. Check for mistakes.
- **基础注册与调用**:支持 `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&`。
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里列举的参数类型包含 torch::optional/c10::optional,但后文“类型映射”表又使用 std::optional。建议全文统一 optional 类型的写法,并明确兼容层最终采用哪一种(以及原因)。

Suggested change
- **参数类型覆盖**(已有测试涉及):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&`

Copilot uses AI. Check for mistakes.
Comment on lines +127 to +130
| (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> |
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“类型映射”表里 optional 统一写成了 std::optional,与前文提到的 torch::optional/c10::optional 不一致。建议在表中改为与兼容层一致的 optional 类型,或在表下注明三者关系/取舍。

Copilot uses AI. Check for mistakes.
Comment on lines +323 to +335
- 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):\
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考资料列表用行尾反斜杠 \ 做换行(例如此处)。在 GitHub Markdown 中这容易导致渲染怪异,也可能把 \ 误当成链接内容的一部分。建议移除行尾 \,改用正常的列表换行/缩进或将链接合并到同一行。

Suggested change
- 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

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

@SigureMo SigureMo left a comment

@gouzil
Copy link
Copy Markdown
Member Author

gouzil commented Feb 3, 2026

@ShigureNyako 帮我补充一下 1、2、3、4

Copy link
Copy Markdown
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- **IValue 基础类型**:None/bool/int/double/string/tensor/list/tuple 的构造与类型检查。

尚存在的主要不足:
- **缺乏 schema 解析/登记与绑定能力**:测试使用显式 schema 字符串与纯位置参数调用,尚未覆盖默认参数、keyword 参数、keyword-only 绑定;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **缺乏 schema 解析/登记与绑定能力**:测试使用显式 schema 字符串与纯位置参数调用,尚未覆盖默认参数、keyword 参数、keyword-only 绑定;
- **缺乏 schema 解析/注册与绑定能力**:测试使用显式 schema 字符串与纯位置参数调用,尚未覆盖默认参数、keyword 参数、keyword-only 绑定;

@gouzil gouzil changed the title 【WIP】【Hackathon 10th Sprint No.2】【RFC】跨生态自定义算子注册层兼容能力增强设计文档 【Hackathon 10th Sprint No.2】【RFC】跨生态自定义算子注册层兼容能力增强设计文档 Feb 21, 2026
@luotao1 luotao1 merged commit 9057434 into PaddlePaddle:master Feb 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants