Skip to content

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

Open
gouzil wants to merge 7 commits intoPaddlePaddle:developfrom
gouzil:feat/add_schema_parser_torch_compatibility
Open

【WIP】【Hackathon 10th Spring No.2】Add schema parser and related functionality for Torch compatibility#77938
gouzil wants to merge 7 commits intoPaddlePaddle:developfrom
gouzil:feat/add_schema_parser_torch_compatibility

Conversation

@gouzil
Copy link
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 函数,可以考虑提到一个通用的地方

还未验证 https://github.com/PFCCLab/paddlecodec

是否引起精度变化

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

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
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
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
Member

@gouzil 测试哈,这个 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.

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

@@ -153,6 +192,10 @@ class FunctionArgs {

Copy link
Member

Choose a reason for hiding this comment

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

[gh-llm test] second inline review comment: named args API 这里建议后续补充顺序稳定性说明。

std::ostringstream oss;
oss << "FunctionArgs[";
for (size_t i = 0; i < args_.size(); ++i) {
if (i > 0) oss << ", ";
Copy link
Member

Choose a reason for hiding this comment

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

[gh-llm test] third inline review comment: to_string() 输出 kwargs 时可考虑 deterministic 排序,方便测试断言。

@codecov-commenter
Copy link

codecov-commenter commented Feb 17, 2026

Codecov Report

❌ Patch coverage is 67.82609% with 259 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@6bbddb2). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...e/compat/torch/csrc/jit/function_schema_parser.cpp 64.86% 117 Missing ⚠️
paddle/phi/api/include/compat/ATen/core/jit_type.h 37.75% 61 Missing ⚠️
...e/phi/api/include/compat/ATen/core/jit_type_base.h 33.33% 30 Missing ⚠️
...clude/compat/torch/csrc/jit/schema_type_parser.cpp 83.33% 21 Missing ⚠️
...phi/api/include/compat/ATen/core/function_schema.h 78.33% 13 Missing ⚠️
paddle/phi/api/include/compat/torch/library.h 87.01% 10 Missing ⚠️
...ddle/phi/api/include/compat/ATen/core/alias_info.h 75.00% 3 Missing ⚠️
paddle/phi/api/include/compat/ATen/core/type_ptr.h 0.00% 2 Missing ⚠️
paddle/phi/api/include/compat/torch/library.cpp 88.23% 2 Missing ⚠️

❌ Your patch status has failed because the patch coverage (67.82%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #77938   +/-   ##
==========================================
  Coverage           ?   67.82%           
==========================================
  Files              ?       11           
  Lines              ?      805           
  Branches           ?        0           
==========================================
  Hits               ?      546           
  Misses             ?      259           
  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
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

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.

3 participants

Comments