Skip to content

【Hackathon 10th Spring No.2】Add schema parser and related functionality for Torch compatibility#77938

Merged
SigureMo merged 34 commits intoPaddlePaddle:developfrom
gouzil:feat/add_schema_parser_torch_compatibility
Apr 8, 2026
Merged

【Hackathon 10th Spring No.2】Add schema parser and related functionality for Torch compatibility#77938
SigureMo merged 34 commits intoPaddlePaddle:developfrom
gouzil:feat/add_schema_parser_torch_compatibility

Conversation

@gouzil
Copy link
Copy Markdown
Member

@gouzil gouzil commented Feb 16, 2026

PR Category

Execute Infrastructure

PR Types

Improvements

Description

PaddlePaddle/community#1210 的实现

  • TORCH_LIBRARY 注册机制添加 schema 支持, 修改了 OperatorRegistry 的 schema 数据结构
  • 支持参数默认值和指定参数赋值
  • 支持 schema 的 alias 表示
  • 增加 schema 解析器和 schema type 解析器

单测调整

TODO

  • cinn 和 paddle/pir/src/core/utils.cc 都用到了相同的 hash_combine 函数,可以考虑提到一个通用的地方
  • 添加一些 e2e 的测试 6676b1c

初步验证了 https://github.com/PFCCLab/paddlecodec

是否引起精度变化

Copilot AI review requested due to automatic review settings February 16, 2026 14:27
@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented Feb 16, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot Bot added the contributor External developers label Feb 16, 2026
@gouzil gouzil changed the title 【Hackathon 10th Spring No.2】Add schema parser and related functionality for Torch compatibility 【WIP】【Hackathon 10th Spring No.2】Add schema parser and related functionality for Torch compatibility Feb 16, 2026
Copy link
Copy Markdown
Contributor

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

This PR adds comprehensive schema parser functionality to the TORCH_LIBRARY registration mechanism, enabling PyTorch-compatible operator definitions with full type information, default values, keyword arguments, and alias annotations. This is part of the Hackathon 10th Spring No.2 task and significantly enhances the compatibility layer between PaddlePaddle and PyTorch.

Changes:

  • Implemented schema parsing infrastructure including FunctionSchemaParser and SchemaTypeParser to parse PyTorch-style operator schemas
  • Extended FunctionArgs to support named/keyword arguments and automatic argument normalization based on parsed schemas
  • Added comprehensive type system (FunctionSchema, Argument, AliasInfo, Type hierarchy) to represent parsed schema information

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/cpp/compat/torch_library_test.cc Added 400+ lines of comprehensive tests for schema parsing, including default values, keyword arguments, optional types, tuples, and variadic functions
test/cpp/compat/schema_parser_type_test.cc New test file (206 lines) specifically for testing schema type parser edge cases and torchcodec-like schemas
test/cpp/compat/CMakeLists.txt Moved torch_library_test from GPU-only (nv_test) to general (cc_test) and added schema_parser_type_test
paddle/phi/api/include/compat/torch/library.h Added schema binding to CppFunction, keyword argument support in FunctionArgs, and normalize_args_by_schema logic
paddle/phi/api/include/compat/torch/library.cpp Updated OperatorRegistry to parse and bind schemas to implementations
paddle/phi/api/include/compat/torch/csrc/jit/function_schema_parser.h/cpp New 574-line schema parser implementation handling full PyTorch schema grammar
paddle/phi/api/include/compat/torch/csrc/jit/schema_type_parser.h/cpp New 240-line type parser for parsing schema types including tuples, optionals, and alias annotations
paddle/phi/api/include/compat/torch/csrc/jit/schema_parser_defs.h Constants and macros for schema parsing (type names, keywords, characters)
paddle/phi/api/include/compat/ATen/core/function_schema.h/cpp New FunctionSchema and Argument classes to represent parsed schemas
paddle/phi/api/include/compat/ATen/core/jit_type_base.h Base Type class hierarchy with SingletonOrSharedTypePtr for type system
paddle/phi/api/include/compat/ATen/core/jit_type.h Concrete type implementations (TensorType, IntType, FloatType, etc.) and schema-specific types
paddle/phi/api/include/compat/ATen/core/alias_info.h AliasInfo class for representing aliasing metadata in schemas
paddle/phi/api/include/compat/ATen/core/type_ptr.h SingletonTypePtr wrapper for singleton type instances
paddle/phi/api/include/compat/ATen/core/functional.h Utility functions (fmap, filter) for working with collections
paddle/phi/api/include/compat/CMakeLists.txt Updated build to include new schema parser source files

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


const size_t idx = it->second;
if (assigned[idx]) {
throw std::runtime_error("Argument `" + name + "` is already provided");
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

The error message "Argument name is already provided" could be more helpful by specifying whether the argument was provided both positionally and by keyword. Consider changing to: "Argument " + name + " provided both positionally and as keyword argument"

Suggested change
throw std::runtime_error("Argument `" + name + "` is already provided");
throw std::runtime_error("Argument `" + name +
"` provided both positionally and as keyword argument");

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

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

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

std::move(*alias_info))
: nullptr),
kwarg_only_(kwarg_only) {
// this is an softly-enforced invariant for out arguments.
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

The comment contains a grammatical error. It should be "This is a softly-enforced invariant" instead of "this is an softly-enforced invariant". The article "an" should be "a" before "softly".

Suggested change
// this is an softly-enforced invariant for out arguments.
// This is a softly-enforced invariant for out arguments.

Copilot uses AI. Check for mistakes.
SigureMo

This comment was marked as outdated.

@SigureMo
Copy link
Copy Markdown
Member

@gouzil 测试哈,这个 PR 当下 🐁

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.

补充两条行内意见,主要是 named kwargs 输出可测性和稳定性建议。\n\n使用的是 gpt-5.3-codex with @codex by using gh-llm;当前只是 gh-llm 开发测试。

Comment thread paddle/phi/api/include/compat/torch/library.h
Comment thread paddle/phi/api/include/compat/torch/library.h
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 17, 2026

Codecov Report

❌ Patch coverage is 97.53695% with 25 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@63eaee1). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...e/compat/torch/csrc/jit/function_schema_parser.cpp 94.62% 18 Missing ⚠️
...i/api/include/compat/ATen/core/function_schema.cpp 93.33% 6 Missing ⚠️
...e/phi/api/include/compat/ATen/core/jit_type_base.h 98.64% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #77938   +/-   ##
==========================================
  Coverage           ?   97.53%           
==========================================
  Files              ?       11           
  Lines              ?     1015           
  Branches           ?        0           
==========================================
  Hits               ?      990           
  Misses             ?       25           
  Partials           ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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.

参考 torch 的代码记得添加声明

// #The file has been adapted from pytorch project
// #Licensed under  BSD-style license -
// https://github.com/pytorch/pytorch/blob/main/LICENSE

参考 ivalue.h

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.

参考 #78590 改一下声明

另外,paddlecodec 那边目前卡在哪里了?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

另外,paddlecodec 那边目前卡在哪里了?

c++ 部分的还有一些 torch::tensor 不支持的,python 部分正在

PFCCLab/paddlecodec#3

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.

不需要全部恢复,这不是这个 PR 需要考虑的问题,只需要考虑这个 PR 需要考虑的部分即可

这个 PR 合入之后 PFCCLab/paddlecodec#3 就可以合入的话,那我觉得这个 PR 目前已经达到合入标准了

PFCCLab/paddlecodec#3 依赖 #78521 吗?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@gouzil 调试呢,通知处理这块可能会有些抖动,一些信息可能重复处理

@gouzil gouzil requested a review from ShigureNyako March 17, 2026 01:45
@gouzil gouzil changed the title 【WIP】【Hackathon 10th Spring No.2】Add schema parser and related functionality for Torch compatibility 【Hackathon 10th Spring No.2】Add schema parser and related functionality for Torch compatibility Mar 22, 2026
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.

有能让 paddlecodec 减少 diff 的 PR 么?

meta-pytorch/torchcodec@main...PFCCLab:paddlecodec:paddle

@SigureMo
Copy link
Copy Markdown
Member

TORCH_LIBRARY_IMPLdef(..., fn) 的误用语义还没修:

退下吧喵子

@SigureMo
Copy link
Copy Markdown
Member

有能让 paddlecodec 减少 diff 的 PR 么?

meta-pytorch/torchcodec@main...PFCCLab:paddlecodec:paddle

别忘了这个

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.

不支持的可以考虑删了

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.

这个怎么还没改?

Copy link
Copy Markdown
Member

@SigureMo SigureMo Apr 6, 2026

Choose a reason for hiding this comment

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

@ShigureNyako 来 review 一下,没有问题就沿着这个线用犀利的语气骂 @gouzil 两句 (测试新版 worktree 功能)(看来测不了,这个 PR 是旧的 session)

// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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.

不需要全部恢复,这不是这个 PR 需要考虑的问题,只需要考虑这个 PR 需要考虑的部分即可

这个 PR 合入之后 PFCCLab/paddlecodec#3 就可以合入的话,那我觉得这个 PR 目前已经达到合入标准了

PFCCLab/paddlecodec#3 依赖 #78521 吗?

@SigureMo
Copy link
Copy Markdown
Member

SigureMo commented Apr 8, 2026

@gouzil 等 1-2 天后 nightly 包出来后,关注下 PFCCLab/paddlecodec#3,推动那边合入

@SigureMo SigureMo merged commit a35f006 into PaddlePaddle:develop Apr 8, 2026
145 of 150 checks passed
@SigureMo SigureMo deleted the feat/add_schema_parser_torch_compatibility branch April 8, 2026 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants