-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[PIR][N/N] Add CustomPyOP IR #76888
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
Open
DrRyanHuang
wants to merge
19
commits into
PaddlePaddle:develop
Choose a base branch
from
cattidea:add_custom_pyop_IR
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,323
−3
Open
[PIR][N/N] Add CustomPyOP IR #76888
Changes from 1 commit
Commits
Show all changes
19 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 a7ac4f3
normalize eager_utils
SigureMo 18c9c2b
align part1 and part2
SigureMo ef81be2
Merge branch 'develop' into add_custom_pyop_IR
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // 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/custom_pyoperator.h" | ||
| #include "paddle/fluid/pir/dialect/operator/ir/op_dialect.h" | ||
| #include "paddle/phi/api/ext/op_meta_info.h" | ||
|
|
||
| namespace paddle::framework { | ||
|
|
||
| void RegisterCustomPyOp( | ||
| 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, | ||
| WrapPythonFunction&& pyop_func, | ||
| WrapInferMetaPythonFunction&& 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)) | ||
| .SetPyCustomPyOpFunction(pyop_func) | ||
| .SetPyCustomPyOpInferMetaFunction(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 one, because it don't support gradient op.")); | ||
|
|
||
| const auto& op_meta_info = op_meta_info_vector.back(); | ||
|
|
||
| auto& inplace_map = OpMetaInfoHelper::GetInplaceMap(op_meta_info); | ||
| const auto postfix = inplace_map.empty() ? "" : "_"; | ||
DrRyanHuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ::pir::IrContext* ctx = ::pir::IrContext::Instance(); | ||
| auto* custom_pyop_dialect = | ||
| ctx->GetOrRegisterDialect<paddle::dialect::CustomPyOpDialect>(); | ||
|
|
||
| if (custom_pyop_dialect->HasRegistered( | ||
| paddle::framework::kCustomPyDialectPrefix + op_name + postfix)) { | ||
| return; | ||
| } | ||
| custom_pyop_dialect->RegisterCustomPyOp(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 RegisterCustomPyOp( | ||
| 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, | ||
| WrapPythonFunction&& func, | ||
| WrapInferMetaPythonFunction&& 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
统一一下命名吧:
py_oppython_operator,两者一起不缩写避免和 custom op 相互之间冲突
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lower 后的 kernel dialect 叫啥合适呢?之前叫
custom_py_func,改成python_operator_function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自定义算子的叫啥?