Skip to content

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

Open
gouzil wants to merge 3 commits intoPaddlePaddle:masterfrom
gouzil:feat/add_c10_func_schema
Open

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

Conversation

@gouzil
Copy link
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

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

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
Member

@SigureMo SigureMo left a comment

@ShigureNyako
Copy link

@gouzil 收到~我先快速过了一遍文档(整体结构清晰,目标/现状/方案/验收/排期都写得比较完整)。这里给几条可能有帮助的 review 建议:

  1. Schema 覆盖范围/边界再明确一点:目前列了不少类型与 alias/mutation;建议在「可选的 schema 校验」或「影响面/风险」里补一段“第一期明确支持/暂不支持”的类型清单(例如 Dict、设备/布局相关类型、复杂 nested container、变参/可变返回等),避免评审时预期不一致。
  2. 复用 vs 重新实现的取舍:方案里提到实现 c10::FunctionSchema/Type 等。建议补充:是否会尽量复用 Paddle 现有 compat/torch 目录中的 IValue / type helpers,哪些部分必须自研(解析器/绑定器/registry/dispatch),以及为什么不直接引入 PyTorch 解析器(体量/依赖/ABI 等)。
  3. Backend 派发规则建议对齐 Paddle Place 语义:比如多 Tensor 输入不一致时的报错信息、fallback 顺序(CPU/Any/Composite)的细节,最好给一个更「可执行」的伪代码/决策表,方便后续实现与测试对齐。
  4. 测试建议torch_library_test.cc 里可以单独加一组专门覆盖 keyword/default/kw-only/重复绑定/unknown kw 的 case(你在文档里列的正/负例都很好,基本可以直接落到单测)。

整体方向我觉得 OK;如果后续需要我帮忙重点看某一块(schema parser / binder / dispatch)也可以在 PR 里 @ 我。

@gouzil
Copy link
Member Author

gouzil commented Feb 3, 2026

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

@ShigureNyako
Copy link

@gouzil 收到,我来补充 1/2/3/4。\n\n为了避免我补错位置:你说的 1、2、3、4 是文档目录里的 4 个小节(还是 PR 模板里的条目)?你指一下对应标题/段落我就按那个结构补齐。

Copy link
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
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 绑定;

@ShigureNyako
Copy link

@SigureMo 收到,我来用 gh llm 帮你 review 一下这个 RFC。

整体看下来这份设计文档结构完整,背景/目标/方案/验收都有覆盖。几点建议:

  1. 类型映射表(第 5.1 节):建议补充 c10::optional -> std::optional 的显式映射说明,因为当前文档同时出现了 torch::optionalc10::optionalstd::optional 三种写法,容易造成歧义。

  2. 2.2 参数绑定 示例代码很好,建议补充一个混合场景:既有位置参数又有 keyword 参数,同时有默认值,这样更能体现绑定规则的复杂度。

  3. 排期规划:5 周的安排比较紧凑,建议明确每个阶段的里程碑输出(比如第 2 周结束需完成哪些文件/接口的代码),便于进度跟踪。

  4. 风险评估:建议补充「如果 schema 解析遇到不支持的语法时,兼容层的 fallback 策略是什么」这一点的说明。

技术上我没问题,主要问题是文档细节完善度。供参考~

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

Comments