-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[PIR][1/N] Support register Python function on PIR (IR part) #76935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SigureMo
merged 21 commits into
PaddlePaddle:develop
from
cattidea:pir/python-op-part-1
Dec 17, 2025
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
70a3ac0
add custom pyop IR
DrRyanHuang 0a0c7d5
rename custom_pyop -> python_operator
DrRyanHuang 4c7d96d
add all
DrRyanHuang e14ac1f
recover
DrRyanHuang a3101ed
rename some file && function
DrRyanHuang 4ca7657
use NativeMetaTensor instead of IrTensor
SigureMo d926867
export `paddle/phi/core/ddim.h`
SigureMo b723138
rm MetaTensor
SigureMo 88893b5
`PythonOperationFunctionInstruction` -> `PythonFunctionInstruction`
SigureMo 2bfd3b7
2023 -> 2025
DrRyanHuang b8320a8
refine name in instructions
SigureMo a4432ed
custom py func -> python function
SigureMo 5c7db9d
use void* and PointerAttribute
DrRyanHuang 7287248
`HandleForPythonOp`
SigureMo cdb9201
refine comment
SigureMo a3ce468
Merge branch 'develop' into add_custom_pyop_IR
SigureMo 54c30ef
revert changes non-normalized
SigureMo 5da73eb
revert changes about native meta tensor
SigureMo ffb1c0c
Apply suggestions from code review
SigureMo 8e747f2
Update paddle/phi/api/lib/op_meta_info.cc
SigureMo 97df0d5
fix codestyle
SigureMo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // 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. | ||
|
|
||
| #include <utility> // for std::move | ||
|
|
||
| #include "paddle/fluid/framework/custom_operator_utils.h" | ||
| #include "paddle/fluid/framework/python_operator.h" | ||
| #include "paddle/fluid/pir/dialect/operator/ir/op_dialect.h" | ||
| #include "paddle/phi/api/ext/op_meta_info.h" | ||
|
|
||
| namespace paddle::framework { | ||
|
|
||
| void RegisterPythonOperator( | ||
| const std::string& op_name, | ||
| std::vector<std::string>&& op_inputs, | ||
| std::vector<std::string>&& op_outputs, | ||
| std::vector<std::string>&& op_attrs, | ||
| std::unordered_map<std::string, std::string>&& op_inplace_map, | ||
| PythonOperatorFunctionType&& pyop_func, | ||
| PythonOperatorInferMetaFunctionType&& pyop_func_infer_meta) { | ||
| ::paddle::OpMetaInfoBuilder op_meta_info_builder = | ||
| ::paddle::OpMetaInfoBuilder(std::string(op_name), 0); | ||
| op_meta_info_builder.Inputs(std::move(op_inputs)) | ||
| .Outputs(std::move(op_outputs)) | ||
| .Attrs(std::move(op_attrs)) | ||
| .SetInplaceMap(std::move(op_inplace_map)) | ||
| .SetPythonOperatorFunction(pyop_func) | ||
| .SetPythonOperatorInferMetaFunction(pyop_func_infer_meta); | ||
|
|
||
| const std::vector<paddle::OpMetaInfo>& op_meta_info_vector = | ||
| OpMetaInfoMap::Instance()[op_name]; | ||
|
|
||
| PADDLE_ENFORCE_EQ(op_meta_info_vector.size(), | ||
| 1, | ||
| common::errors::OutOfRange( | ||
| "Current op_name(%s) must not be registered more " | ||
| "than once, because it does not support gradient op.", | ||
| op_name)); | ||
|
|
||
| const auto& op_meta_info = op_meta_info_vector.back(); | ||
|
|
||
| auto& inplace_map = OpMetaInfoHelper::GetInplaceMap(op_meta_info); | ||
| const auto suffix = inplace_map.empty() ? "" : "_"; | ||
|
|
||
| ::pir::IrContext* ctx = ::pir::IrContext::Instance(); | ||
| auto* python_operator_dialect = | ||
| ctx->GetOrRegisterDialect<paddle::dialect::PythonOperatorDialect>(); | ||
|
|
||
| if (python_operator_dialect->HasRegistered( | ||
| paddle::framework::kPythonOperatorDialectPrefix + op_name + suffix)) { | ||
| return; | ||
| } | ||
| python_operator_dialect->RegisterPythonOperator(op_meta_info); | ||
| } | ||
|
|
||
| } // namespace paddle::framework |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <functional> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
| #include "paddle/fluid/pir/dialect/operator/ir/ir_tensor.h" | ||
| #include "paddle/phi/api/ext/op_meta_info.h" | ||
|
|
||
| namespace paddle { | ||
| namespace framework { | ||
|
|
||
| void RegisterPythonOperator( | ||
| const std::string& op_name, | ||
| std::vector<std::string>&& op_inputs, | ||
| std::vector<std::string>&& op_outputs, | ||
| std::vector<std::string>&& op_attrs, | ||
| std::unordered_map<std::string, std::string>&& op_inplace_map, | ||
| PythonOperatorFunctionType&& func, | ||
| PythonOperatorInferMetaFunctionType&& infer_meta); | ||
|
|
||
| } // namespace framework | ||
| } // namespace paddle | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.