From 068f48d86456dd4ee8e6ca88f04496bcebbe94be Mon Sep 17 00:00:00 2001 From: xiongkun Date: Fri, 15 Jul 2022 19:11:26 +0800 Subject: [PATCH] [ Phi Kernel ] Transfer as_real to phi. (#44263) * transfer as_real to phi * fix erros * blocking: True -> False --- paddle/fluid/operators/complex_view_op.cc | 21 ++++------- paddle/fluid/operators/complex_view_op.cu | 5 --- paddle/fluid/operators/complex_view_op.h | 15 -------- paddle/phi/api/yaml/legacy_api.yaml | 9 +++++ paddle/phi/infermeta/unary.cc | 8 +++++ paddle/phi/infermeta/unary.h | 2 ++ paddle/phi/kernels/as_real_kernel.h | 26 ++++++++++++++ paddle/phi/kernels/cpu/as_real_kernel.cc | 22 ++++++++++++ paddle/phi/kernels/gpu/as_real_kernel.cu | 22 ++++++++++++ paddle/phi/kernels/impl/as_real_impl.h | 35 +++++++++++++++++++ .../tests/unittests/test_complex_view_op.py | 1 + 11 files changed, 131 insertions(+), 35 deletions(-) create mode 100644 paddle/phi/kernels/as_real_kernel.h create mode 100644 paddle/phi/kernels/cpu/as_real_kernel.cc create mode 100644 paddle/phi/kernels/gpu/as_real_kernel.cu create mode 100644 paddle/phi/kernels/impl/as_real_impl.h diff --git a/paddle/fluid/operators/complex_view_op.cc b/paddle/fluid/operators/complex_view_op.cc index 6bdd2b48c45..ce46a0f0121 100644 --- a/paddle/fluid/operators/complex_view_op.cc +++ b/paddle/fluid/operators/complex_view_op.cc @@ -20,7 +20,9 @@ #include #include "paddle/fluid/framework/data_type.h" +#include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/infermeta/unary.h" namespace paddle { namespace operators { @@ -94,17 +96,6 @@ class AsRealOp : public framework::OperatorWithKernel { public: using framework::OperatorWithKernel::OperatorWithKernel; - void InferShape(framework::InferShapeContext *ctx) const override { - OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "as_real"); - OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "as_real"); - - auto out_dims_v = phi::vectorize(ctx->GetInputDim("X")); - out_dims_v.push_back(2); - const framework::DDim out_dims = phi::make_ddim(out_dims_v); - ctx->SetOutputDim("Out", out_dims); - ctx->ShareLoD("X", /*->*/ "Out"); - } - protected: framework::OpKernelType GetExpectedKernelType( const framework::ExecutionContext &ctx) const override { @@ -148,6 +139,9 @@ class AsRealGradMaker : public framework::SingleGradOpMaker { } // namespace paddle namespace ops = paddle::operators; +DECLARE_INFER_SHAPE_FUNCTOR(as_real, + AsRealInferShapeFunctor, + PD_INFER_META(phi::AsRealInferMeta)); REGISTER_OPERATOR(as_complex, ops::AsComplexOp, @@ -158,13 +152,10 @@ REGISTER_OPERATOR(as_complex, REGISTER_OPERATOR(as_real, ops::AsRealOp, ops::AsRealOpMaker, + AsRealInferShapeFunctor, ops::AsRealGradMaker, ops::AsRealGradMaker); REGISTER_OP_CPU_KERNEL(as_complex, ops::AsComplexKernel, ops::AsComplexKernel); - -REGISTER_OP_CPU_KERNEL(as_real, - ops::AsRealKernel, - ops::AsRealKernel); diff --git a/paddle/fluid/operators/complex_view_op.cu b/paddle/fluid/operators/complex_view_op.cu index 18d448fb75d..eb107814913 100644 --- a/paddle/fluid/operators/complex_view_op.cu +++ b/paddle/fluid/operators/complex_view_op.cu @@ -22,8 +22,3 @@ REGISTER_OP_CUDA_KERNEL( as_complex, ops::AsComplexKernel, ops::AsComplexKernel); - -REGISTER_OP_CUDA_KERNEL( - as_real, - ops::AsRealKernel, - ops::AsRealKernel); diff --git a/paddle/fluid/operators/complex_view_op.h b/paddle/fluid/operators/complex_view_op.h index 51abaa88f85..169b8b05a55 100644 --- a/paddle/fluid/operators/complex_view_op.h +++ b/paddle/fluid/operators/complex_view_op.h @@ -41,20 +41,5 @@ class AsComplexKernel : public framework::OpKernel { } }; -template -class AsRealKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& context) const override { - const auto* x = context.Input("X"); - auto* out = context.Output("Out"); - - out->mutable_data(context.GetPlace()); - const framework::DDim out_dims_original = out->dims(); - framework::TensorCopy(*x, context.GetPlace(), out); - out->Resize(out_dims_original); // restored the shape - out->mutable_data(context.GetPlace()); // restore the dtype - } -}; - } // namespace operators } // namespace paddle diff --git a/paddle/phi/api/yaml/legacy_api.yaml b/paddle/phi/api/yaml/legacy_api.yaml index ab82ce9473e..b9e7361abea 100644 --- a/paddle/phi/api/yaml/legacy_api.yaml +++ b/paddle/phi/api/yaml/legacy_api.yaml @@ -167,6 +167,15 @@ func : argsort backward : argsort_grad +- api : as_real + args : (Tensor x) + output : Tensor + infer_meta : + func : AsRealInferMeta + kernel : + func : as_real +# backward : as_complex + # asin - api : asin args : (Tensor x) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index f6e3b0d7247..02f812f9b17 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -148,6 +148,14 @@ void ArgsortInferMeta(const MetaTensor& input, indices->share_lod(input); } +void AsRealInferMeta(const MetaTensor& input, MetaTensor* output) { + auto out_dims_v = phi::vectorize(input.dims()); + out_dims_v.push_back(2); + auto out_dims = phi::make_ddim(out_dims_v); + output->set_dims(out_dims); + output->share_lod(input); +} + void BatchSizeLikeInferMeta(const MetaTensor& x, const std::vector& shape, int x_batch_size_dim, diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index fc36e1d4f85..30db8dcae98 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -48,6 +48,8 @@ void ArgsortInferMeta(const MetaTensor& input, MetaTensor* output, MetaTensor* indices); +void AsRealInferMeta(const MetaTensor& input, MetaTensor* output); + void BatchSizeLikeInferMeta(const MetaTensor& x, const std::vector& shape, int x_batch_size_dim, diff --git a/paddle/phi/kernels/as_real_kernel.h b/paddle/phi/kernels/as_real_kernel.h new file mode 100644 index 00000000000..e600f3ce39a --- /dev/null +++ b/paddle/phi/kernels/as_real_kernel.h @@ -0,0 +1,26 @@ +// 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/core/dense_tensor.h" + +namespace phi { + +template +void AsRealKernel(const Context& dev_ctx, + const DenseTensor& x, + DenseTensor* out); + +} // namespace phi diff --git a/paddle/phi/kernels/cpu/as_real_kernel.cc b/paddle/phi/kernels/cpu/as_real_kernel.cc new file mode 100644 index 00000000000..c4f6ec87af4 --- /dev/null +++ b/paddle/phi/kernels/cpu/as_real_kernel.cc @@ -0,0 +1,22 @@ +// 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/kernels/as_real_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/as_real_impl.h" + +PD_REGISTER_KERNEL(as_real, CPU, ALL_LAYOUT, phi::AsRealKernel, float, double) { +} diff --git a/paddle/phi/kernels/gpu/as_real_kernel.cu b/paddle/phi/kernels/gpu/as_real_kernel.cu new file mode 100644 index 00000000000..63227e7f0b1 --- /dev/null +++ b/paddle/phi/kernels/gpu/as_real_kernel.cu @@ -0,0 +1,22 @@ +// 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/kernels/as_real_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/as_real_impl.h" + +PD_REGISTER_KERNEL(as_real, GPU, ALL_LAYOUT, phi::AsRealKernel, float, double) { +} diff --git a/paddle/phi/kernels/impl/as_real_impl.h b/paddle/phi/kernels/impl/as_real_impl.h new file mode 100644 index 00000000000..0534b836e37 --- /dev/null +++ b/paddle/phi/kernels/impl/as_real_impl.h @@ -0,0 +1,35 @@ +// 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/kernels/as_real_kernel.h" + +#include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/tensor_utils.h" +#include "paddle/phi/kernels/funcs/for_range.h" + +namespace phi { +template +void AsRealKernel(const Context& ctx, const DenseTensor& x, DenseTensor* out) { + ctx.template Alloc(out); + auto out_dims_original = out->dims(); + Copy(ctx, x, ctx.GetPlace(), false, out); + out->Resize(out_dims_original); // restored the shape. + out->set_type( + paddle::experimental::CppTypeToDataType::Type()); // restored the + // dtype. +} + +} // namespace phi diff --git a/python/paddle/fluid/tests/unittests/test_complex_view_op.py b/python/paddle/fluid/tests/unittests/test_complex_view_op.py index 6b224209edc..a2fd77bcabf 100644 --- a/python/paddle/fluid/tests/unittests/test_complex_view_op.py +++ b/python/paddle/fluid/tests/unittests/test_complex_view_op.py @@ -67,6 +67,7 @@ class TestViewAsRealOp(OpTest): out_ref = ref_view_as_real(x) self.inputs = {'X': x} self.outputs = {'Out': out_ref} + self.python_api = paddle.as_real self.out_grad = np.ones([10, 10, 2], dtype="float64") def test_check_output(self): -- GitLab