-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Support custom implement for C++ API #39521
Merged
Merged
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d18dfc0
Support custom implement for C++ API
zyfncg 046b283
rename api_invoke_impl to api_custom_impl
zyfncg c8b7f37
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 999a49a
remove manual_api
zyfncg edc2184
delete mutable_data in copy_to api
zyfncg 620ed4c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 1d5304b
Merge branch 'custom_api_impl' of github.com:zyfncg/Paddle into custo…
zyfncg 2cd1df7
fix problem of copy_to
zyfncg 77853a9
add unittest for infer_meta_fn_factory
zyfncg 39d3776
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 73a2a1a
fix split cofig in yaml
zyfncg e8d5b8e
fix split cofig in yaml
zyfncg 23bbd71
modify sum api yaml
zyfncg 85b19f6
add copy_to wrapped infermeta
zyfncg acd7577
Merge commit 'refs/pull/39703/head' of https://github.com/PaddlePaddl…
zyfncg 972c950
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 76d62c7
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 714a77e
rollback copy impl
zyfncg 06f9a20
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 4d8fe8c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 6381b03
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 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 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 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 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,102 @@ | ||
/* Copyright (c) 2022 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 "paddle/pten/api/lib/api_custom_impl.h" | ||
|
||
#include "paddle/pten/api/lib/api_registry.h" | ||
#include "paddle/pten/api/lib/api_utils.h" | ||
#include "paddle/pten/api/lib/data_transform.h" | ||
#include "paddle/pten/api/lib/kernel_dispatch.h" | ||
#include "paddle/pten/api/lib/utils/storage.h" | ||
#include "paddle/pten/common/backend.h" | ||
#include "paddle/pten/core/kernel_registry.h" | ||
#include "paddle/pten/core/meta_tensor.h" | ||
#include "paddle/pten/infermeta/binary.h" | ||
#include "paddle/pten/infermeta/multiary.h" | ||
#include "paddle/pten/infermeta/nullary.h" | ||
#include "paddle/pten/infermeta/unary.h" | ||
|
||
#include "glog/logging.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
PADDLE_API std::vector<Tensor> split_impl(const Tensor& x, | ||
const ScalarArray& num_or_sections, | ||
const Scalar& axis) { | ||
Backend kernel_backend = Backend::UNDEFINED; | ||
DataLayout kernel_layout = DataLayout::UNDEFINED; | ||
DataType kernel_data_type = DataType::UNDEFINED; | ||
|
||
if (kernel_backend == Backend::UNDEFINED || | ||
kernel_layout == DataLayout::UNDEFINED || | ||
kernel_data_type == DataType::UNDEFINED) { | ||
auto kernel_key_set = ParseKernelKeyByInputArgs(x); | ||
auto kernel_key = kernel_key_set.GetHigestPriorityKernelKey(); | ||
if (kernel_backend == Backend::UNDEFINED) { | ||
kernel_backend = kernel_key.backend(); | ||
} | ||
if (kernel_layout == DataLayout::UNDEFINED) { | ||
kernel_layout = kernel_key.layout(); | ||
} | ||
if (kernel_data_type == DataType::UNDEFINED) { | ||
kernel_data_type = kernel_key.dtype(); | ||
} | ||
} | ||
|
||
auto kernel = pten::KernelFactory::Instance().SelectKernelOrThrowError( | ||
"split", {kernel_backend, kernel_layout, kernel_data_type}); | ||
VLOG(6) << "split API kernel key: [" << kernel_backend << ", " | ||
<< kernel_layout << ", " << kernel_data_type << "]"; | ||
VLOG(6) << "split API kernel: " << kernel; | ||
|
||
auto* dev_ctx = GetDeviceContextByBackend(kernel_backend); | ||
|
||
auto dense_x = PrepareData(x, kernel.InputAt(0), {}); | ||
|
||
// Calculate the number of out tensors | ||
size_t out_number; | ||
if (num_or_sections.GetData().size() == 1) { | ||
out_number = num_or_sections.GetData()[0]; | ||
} else { | ||
out_number = num_or_sections.GetData().size(); | ||
} | ||
|
||
std::vector<Tensor> out; | ||
auto dense_outs = SetKernelOutput(out_number, kernel_backend, &out); | ||
std::vector<pten::MetaTensor> meta_outs; | ||
for (size_t i = 0; i < out_number; ++i) { | ||
meta_outs.push_back(dense_outs[i]); | ||
} | ||
|
||
pten::SplitInferMeta( | ||
MakeMetaTensor(*dense_x), num_or_sections, axis, &meta_outs); | ||
|
||
using kernel_signature = void (*)(const platform::DeviceContext&, | ||
const pten::DenseTensor&, | ||
const pten::ScalarArray&, | ||
const pten::Scalar&, | ||
std::vector<pten::DenseTensor*>&); | ||
auto* kernel_fn = kernel.GetVariadicKernelFn<kernel_signature>(); | ||
(*kernel_fn)(*dev_ctx, | ||
*dense_x, | ||
pten::ScalarArray(num_or_sections), | ||
pten::Scalar(axis), | ||
dense_outs); | ||
|
||
return out; | ||
} | ||
|
||
} // namespace experimental | ||
} // namespace paddle |
This file contains 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,29 @@ | ||
/* Copyright (c) 2021 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 "paddle/pten/api/include/tensor.h" | ||
#include "paddle/pten/common/scalar.h" | ||
#include "paddle/pten/common/scalar_array.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
PADDLE_API std::vector<Tensor> split_impl(const Tensor& x, | ||
const ScalarArray& num_or_sections, | ||
const Scalar& axis); | ||
|
||
} // namespace experimental | ||
} // namespace paddle |
This file contains 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 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 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 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 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
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.
manual_api和api_custom_impl定位有点模糊了
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.
Done. 已将manual_api删掉 thx~