-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Phi] Support construct Scalar by using Non-CPU Tensosr (#41528)
* support construct scalar using non-cpu tensor * fix bugs when run unittest * fix compile bugs * fix bugs when run ci * fix compile bugs * fix bugs when move copy * perfect unit test * perfect unittest * update according to comment * add target dependency
- Loading branch information
1 parent
b239043
commit fe214af
Showing
19 changed files
with
446 additions
and
95 deletions.
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
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,48 @@ | ||
/* 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/phi/common/scalar.h" | ||
|
||
#include "paddle/phi/api/lib/tensor_copy.h" | ||
#include "paddle/phi/common/place.h" | ||
#include "paddle/phi/core/enforce.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
template <> | ||
ScalarBase<Tensor>::ScalarBase(const Tensor& tensor_in) | ||
: dtype_(tensor_in.dtype()) { // NOLINT | ||
PADDLE_ENFORCE_EQ(tensor_in.numel(), | ||
1, | ||
phi::errors::InvalidArgument( | ||
"The Scalar only supports Tensor with 1 element, but " | ||
"now Tensor has `%d` elements", | ||
tensor_in.numel())); | ||
if (tensor_in.place() == PlaceType::kGPU) { | ||
Tensor dst_tensor; | ||
copy(tensor_in, phi::CPUPlace(), true, &dst_tensor); | ||
GetDataFromTensor(dst_tensor); | ||
} else if (tensor_in.place() == PlaceType::kCPU) { | ||
GetDataFromTensor(tensor_in); | ||
} else { | ||
PADDLE_THROW(phi::errors::Unimplemented( | ||
"Now, it is not supported to construct Scalar using tensor that its " | ||
"PlaceType is (%d)", | ||
static_cast<int>(tensor_in.place()))); | ||
} | ||
} | ||
|
||
} // 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,57 @@ | ||
/* 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/phi/api/lib/tensor_copy.h" | ||
#include "paddle/phi/api/lib/api_gen_utils.h" | ||
#include "paddle/phi/api/lib/kernel_dispatch.h" | ||
#include "paddle/phi/api/lib/utils/storage.h" | ||
#include "paddle/phi/core/compat/convert_utils.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/core/meta_tensor.h" | ||
#include "paddle/phi/infermeta/unary.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
void copy(const Tensor& src, Place place, bool blocking, Tensor* dst) { | ||
auto kernel_key_set = ParseKernelKeyByInputArgs(src); | ||
kernel_key_set.backend_set = | ||
kernel_key_set.backend_set | BackendSet(phi::TransToPhiBackend(place)); | ||
auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey(); | ||
auto kernel = phi::KernelFactory::Instance().SelectKernelOrThrowError( | ||
"copy", kernel_key); | ||
|
||
VLOG(6) << "copy API kernel key: " << kernel_key; | ||
VLOG(6) << "copy API kernel: " << kernel; | ||
|
||
auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend()); | ||
|
||
auto dense_x = TensorToDenseTensor(src); | ||
|
||
auto kernel_out = SetKernelOutput(kernel_key.backend(), dst); | ||
phi::MetaTensor meta_out(kernel_out); | ||
phi::UnchangedInferMeta(*dense_x, &meta_out); | ||
|
||
using kernel_signature = void (*)(const platform::DeviceContext&, | ||
const phi::DenseTensor&, | ||
phi::Place, | ||
bool, | ||
phi::DenseTensor*); | ||
|
||
auto* kernel_fn = kernel.GetVariadicKernelFn<kernel_signature>(); | ||
(*kernel_fn)(*dev_ctx, *dense_x, place, blocking, kernel_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,25 @@ | ||
/* 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. */ | ||
|
||
#pragma once | ||
|
||
#include "paddle/phi/api/include/tensor.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
void copy(const Tensor& src, Place place, bool blocking, Tensor* dst); | ||
|
||
} // 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
cc_library(phi_api_utils SRCS storage.cc tensor_utils.cc DEPS | ||
tensor_base convert_utils dense_tensor lod_tensor selected_rows_utils place var_type_traits scalar string_tensor) | ||
tensor_base convert_utils dense_tensor lod_tensor selected_rows_utils place var_type_traits string_tensor scalar) |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
cc_library(phi_place SRCS place.cc) | ||
cc_library(scalar SRCS scalar.cc DEPS phi_enforce) | ||
cc_library(scalar SRCS scalar.cc DEPS phi_enforce tensor) |
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
Oops, something went wrong.