From 9121115b172c5e24cf023c3957231c5f7a8c6685 Mon Sep 17 00:00:00 2001 From: xiongkun Date: Wed, 23 Mar 2022 10:37:55 +0800 Subject: [PATCH] [phi] transfer unsqueeze to phi (#40596) * transfer unsqueeze to phi * fix conflict * add squeeze * add infershape * fix xpu and npu error --- paddle/fluid/operators/squeeze_op.cc | 90 +++----------- paddle/fluid/operators/squeeze_op.cu.cc | 30 ----- paddle/fluid/operators/unsqueeze_op.cc | 53 ++------- paddle/fluid/operators/unsqueeze_op.cu.cc | 34 ------ paddle/phi/core/compat/op_utils.h | 4 + paddle/phi/infermeta/unary.cc | 71 ++++++++++- paddle/phi/infermeta/unary.h | 10 ++ paddle/phi/kernels/cpu/squeeze_grad_kernel.cc | 34 ++++++ paddle/phi/kernels/cpu/squeeze_kernel.cc | 34 ++++++ .../phi/kernels/cpu/unsqueeze_grad_kernel.cc | 35 ++++++ paddle/phi/kernels/cpu/unsqueeze_kernel.cc | 35 ++++++ paddle/phi/kernels/funcs/unsqueeze.h | 112 ++++++++++++++++++ paddle/phi/kernels/gpu/squeeze_grad_kernel.cu | 35 ++++++ paddle/phi/kernels/gpu/squeeze_kernel.cu | 35 ++++++ .../phi/kernels/gpu/unsqueeze_grad_kernel.cu | 36 ++++++ paddle/phi/kernels/gpu/unsqueeze_kernel.cu | 36 ++++++ .../kernels/impl/squeeze_grad_kernel_impl.h | 33 ++++++ paddle/phi/kernels/impl/squeeze_kernel_impl.h | 34 ++++++ .../kernels/impl/unsqueeze_grad_kernel_impl.h | 31 +++++ .../phi/kernels/impl/unsqueeze_kernel_impl.h | 42 +++++++ paddle/phi/kernels/squeeze_grad_kernel.h | 28 +++++ paddle/phi/kernels/squeeze_kernel.h | 28 +++++ paddle/phi/kernels/unsqueeze_grad_kernel.h | 27 +++++ paddle/phi/kernels/unsqueeze_kernel.h | 29 +++++ paddle/phi/ops/compat/squeeze_sig.cc | 36 ++++++ paddle/phi/ops/compat/unsqueeze_sig.cc | 46 +++++++ 26 files changed, 833 insertions(+), 185 deletions(-) create mode 100644 paddle/phi/kernels/cpu/squeeze_grad_kernel.cc create mode 100644 paddle/phi/kernels/cpu/squeeze_kernel.cc create mode 100644 paddle/phi/kernels/cpu/unsqueeze_grad_kernel.cc create mode 100644 paddle/phi/kernels/cpu/unsqueeze_kernel.cc create mode 100644 paddle/phi/kernels/gpu/squeeze_grad_kernel.cu create mode 100644 paddle/phi/kernels/gpu/squeeze_kernel.cu create mode 100644 paddle/phi/kernels/gpu/unsqueeze_grad_kernel.cu create mode 100644 paddle/phi/kernels/gpu/unsqueeze_kernel.cu create mode 100644 paddle/phi/kernels/impl/squeeze_grad_kernel_impl.h create mode 100644 paddle/phi/kernels/impl/squeeze_kernel_impl.h create mode 100644 paddle/phi/kernels/impl/unsqueeze_grad_kernel_impl.h create mode 100644 paddle/phi/kernels/impl/unsqueeze_kernel_impl.h create mode 100644 paddle/phi/kernels/squeeze_grad_kernel.h create mode 100644 paddle/phi/kernels/squeeze_kernel.h create mode 100644 paddle/phi/kernels/unsqueeze_grad_kernel.h create mode 100644 paddle/phi/kernels/unsqueeze_kernel.h create mode 100644 paddle/phi/ops/compat/squeeze_sig.cc create mode 100644 paddle/phi/ops/compat/unsqueeze_sig.cc diff --git a/paddle/fluid/operators/squeeze_op.cc b/paddle/fluid/operators/squeeze_op.cc index b3403a960a1..ff378396b18 100644 --- a/paddle/fluid/operators/squeeze_op.cc +++ b/paddle/fluid/operators/squeeze_op.cc @@ -19,7 +19,9 @@ limitations under the License. */ #include #include +#include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/phi/infermeta/unary.h" namespace paddle { namespace operators { @@ -113,13 +115,13 @@ class SqueezeOp : public framework::OperatorWithKernel { auto input_data_type = framework::OperatorWithKernel::IndicateVarDataType(ctx, "X"); - //#ifdef PADDLE_WITH_MKLDNN + // #ifdef PADDLE_WITH_MKLDNN // if (this->CanMKLDNNBeUsed(ctx, input_data_type)) { // return framework::OpKernelType(input_data_type, ctx.GetPlace(), // framework::DataLayout::kMKLDNN, // framework::LibraryType::kMKLDNN); // } - //#endif + // #endif return framework::OpKernelType(input_data_type, ctx.GetPlace()); } }; @@ -140,13 +142,13 @@ class SqueezeGradOp : public framework::OperatorWithKernel { auto input_data_type = framework::OperatorWithKernel::IndicateVarDataType( ctx, framework::GradVarName("Out")); - //#ifdef PADDLE_WITH_MKLDNN + // #ifdef PADDLE_WITH_MKLDNN // if (this->CanMKLDNNBeUsed(ctx, input_data_type)) { // return framework::OpKernelType(input_data_type, ctx.GetPlace(), // framework::DataLayout::kMKLDNN, // framework::LibraryType::kMKLDNN); // } - //#endif + // #endif return framework::OpKernelType(input_data_type, ctx.GetPlace()); } }; @@ -201,53 +203,18 @@ class SqueezeOpMaker : public framework::OpProtoAndCheckerMaker { class Squeeze2Op : public framework::OperatorWithKernel { public: using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext *ctx) const override { - OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Squeeze2"); - OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Squeeze2"); - - const auto &x_dims = ctx->GetInputDim("X"); - // Check input tensor dims (<6) Eigen limit. - PADDLE_ENFORCE_LE(x_dims.size(), 6, - platform::errors::InvalidArgument( - "The dimensions of Input(X) " - "should be in the range of [1, 6] (Eigen limit)." - "But received X's dimensions = %d, X's shape = [%s].", - x_dims.size(), x_dims)); - - const auto &axes = ctx->Attrs().Get>("axes"); - - auto out_dims = GetOutputShape(axes, x_dims, false); - ctx->SetOutputDim("Out", out_dims); - if (x_dims[0] == out_dims[0]) { - // Only pass LoD when the first dimension of output and Input(X) - // are the same. - ctx->ShareLoD("X", "Out"); - } - - if (!ctx->HasOutput("XShape")) return; - - std::vector xshape_dims(x_dims.size() + 1); - xshape_dims[0] = 0; - for (int i = 0; i < x_dims.size(); ++i) { - xshape_dims[i + 1] = x_dims[i]; - } - ctx->SetOutputDim("XShape", phi::make_ddim(xshape_dims)); - ctx->ShareLoD("X", /*->*/ "XShape"); - } - framework::OpKernelType GetExpectedKernelType( const framework::ExecutionContext &ctx) const override { auto input_data_type = framework::OperatorWithKernel::IndicateVarDataType(ctx, "X"); - //#ifdef PADDLE_WITH_MKLDNN + // #ifdef PADDLE_WITH_MKLDNN // if (this->CanMKLDNNBeUsed(ctx, input_data_type)) { // return framework::OpKernelType(input_data_type, ctx.GetPlace(), // framework::DataLayout::kMKLDNN, // framework::LibraryType::kMKLDNN); // } - //#endif + // #endif return framework::OpKernelType(input_data_type, ctx.GetPlace()); } }; @@ -287,13 +254,13 @@ class Squeeze2GradOp : public framework::OperatorWithKernel { auto input_data_type = framework::OperatorWithKernel::IndicateVarDataType( ctx, framework::GradVarName("Out")); - //#ifdef PADDLE_WITH_MKLDNN + // #ifdef PADDLE_WITH_MKLDNN // if (this->CanMKLDNNBeUsed(ctx, input_data_type)) { // return framework::OpKernelType(input_data_type, ctx.GetPlace(), // framework::DataLayout::kMKLDNN, // framework::LibraryType::kMKLDNN); // } - //#endif + // #endif return framework::OpKernelType(input_data_type, ctx.GetPlace()); } }; @@ -365,6 +332,10 @@ DECLARE_NO_NEED_BUFFER_VARS_INFERER(SqueezeGradNoNeedBufferVarsInferer, "X"); } // namespace paddle namespace ops = paddle::operators; + +DECLARE_INFER_SHAPE_FUNCTOR(squeeze2, SqueezeInferShapeFunctor, + PD_INFER_META(phi::SqueezeInferMeta)); + REGISTER_OPERATOR(squeeze, ops::SqueezeOp, ops::SqueezeOpMaker, ops::SqueezeGradOpMaker, ops::SqueezeGradOpMaker); @@ -376,7 +347,7 @@ REGISTER_OPERATOR(squeeze_grad, ops::SqueezeGradOp, REGISTER_OPERATOR(squeeze2, ops::Squeeze2Op, ops::Squeeze2OpMaker, ops::Squeeze2GradOpMaker, ops::Squeeze2GradOpMaker, - ops::SqueezeInplaceInferer); + ops::SqueezeInplaceInferer, SqueezeInferShapeFunctor); REGISTER_OPERATOR(squeeze2_grad, ops::Squeeze2GradOp, ops::Squeeze2DoubleGradOpMaker, ops::Squeeze2DoubleGradOpMaker, @@ -411,34 +382,3 @@ REGISTER_OP_CPU_KERNEL( paddle::platform::complex>, ops::SqueezeGradKernel); - -REGISTER_OP_CPU_KERNEL( - squeeze2, ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel>, - ops::Squeeze2Kernel>, - ops::Squeeze2Kernel); - -REGISTER_OP_CPU_KERNEL( - squeeze2_grad, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel>, - ops::Squeeze2GradKernel>, - ops::Squeeze2GradKernel); diff --git a/paddle/fluid/operators/squeeze_op.cu.cc b/paddle/fluid/operators/squeeze_op.cu.cc index 8d7c0e5b4ff..19aa12cb55e 100644 --- a/paddle/fluid/operators/squeeze_op.cu.cc +++ b/paddle/fluid/operators/squeeze_op.cu.cc @@ -46,33 +46,3 @@ REGISTER_OP_CUDA_KERNEL( paddle::platform::complex>, ops::SqueezeGradKernel>); -REGISTER_OP_CUDA_KERNEL( - squeeze2, ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel>, - ops::Squeeze2Kernel>); -REGISTER_OP_CUDA_KERNEL( - squeeze2_grad, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel>, - ops::Squeeze2GradKernel>); diff --git a/paddle/fluid/operators/unsqueeze_op.cc b/paddle/fluid/operators/unsqueeze_op.cc index 6389c5b2680..445e8cd468b 100644 --- a/paddle/fluid/operators/unsqueeze_op.cc +++ b/paddle/fluid/operators/unsqueeze_op.cc @@ -18,7 +18,9 @@ limitations under the License. */ #include #include +#include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/phi/infermeta/unary.h" namespace paddle { namespace operators { @@ -251,19 +253,6 @@ class UnsqueezeDoubleGradOpMaker : public framework::SingleGradOpMaker { class Unsqueeze2Op : public UnsqueezeOp { public: using UnsqueezeOp::UnsqueezeOp; - void InferShape(framework::InferShapeContext *ctx) const override { - UnsqueezeOp::InferShape(ctx); - const auto &x_dims = ctx->GetInputDim("X"); - - if (!ctx->HasOutput("XShape")) return; - std::vector xshape_dims(x_dims.size() + 1); - xshape_dims[0] = 0; - for (int i = 0; i < x_dims.size(); ++i) { - xshape_dims[i + 1] = x_dims[i]; - } - ctx->SetOutputDim("XShape", phi::make_ddim(xshape_dims)); - ctx->ShareLoD("X", /*->*/ "XShape"); - } }; class Unsqueeze2OpMaker : public UnsqueezeOpMaker { @@ -339,10 +328,14 @@ DECLARE_NO_NEED_BUFFER_VARS_INFERER(UnsqueezeGradOpNoNeedBufferVarInferer, "X"); } // namespace operators } // namespace paddle +DECLARE_INFER_SHAPE_FUNCTOR(unsqueeze2, Unsqueeze2InferShapeFunctor, + PD_INFER_META(phi::UnsqueezeInferMeta)); + namespace ops = paddle::operators; REGISTER_OPERATOR(unsqueeze, ops::UnsqueezeOp, ops::UnsqueezeOpMaker, ops::UnsqueezeGradOpMaker, ops::UnsqueezeGradOpMaker); + REGISTER_OPERATOR(unsqueeze_grad, ops::UnsqueezeGradOp, ops::UnsqueezeDoubleGradOpMaker, ops::UnsqueezeDoubleGradOpMaker, @@ -351,7 +344,8 @@ REGISTER_OPERATOR(unsqueeze_grad, ops::UnsqueezeGradOp, REGISTER_OPERATOR(unsqueeze2, ops::Unsqueeze2Op, ops::Unsqueeze2OpMaker, ops::Unsqueeze2GradOpMaker, ops::Unsqueeze2GradOpMaker, - ops::UnsqueezeInplaceInferer); + Unsqueeze2InferShapeFunctor, ops::UnsqueezeInplaceInferer); + REGISTER_OPERATOR(unsqueeze2_grad, ops::Unsqueeze2GradOp, ops::Unsqueeze2DoubleGradOpMaker, ops::Unsqueeze2DoubleGradOpMaker, @@ -388,34 +382,3 @@ REGISTER_OP_CPU_KERNEL( paddle::platform::complex>, ops::UnsqueezeGradKernel); -REGISTER_OP_CPU_KERNEL( - unsqueeze2, ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel>, - ops::UnsqueezeKernel>, - ops::UnsqueezeKernel); -REGISTER_OP_CPU_KERNEL( - unsqueeze2_grad, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel>, - ops::Unsqueeze2GradKernel>, - ops::Unsqueeze2GradKernel); diff --git a/paddle/fluid/operators/unsqueeze_op.cu.cc b/paddle/fluid/operators/unsqueeze_op.cu.cc index 2dcc4d2152a..f20ddb5c881 100644 --- a/paddle/fluid/operators/unsqueeze_op.cu.cc +++ b/paddle/fluid/operators/unsqueeze_op.cu.cc @@ -50,37 +50,3 @@ REGISTER_OP_CUDA_KERNEL( paddle::platform::complex>, ops::UnsqueezeGradKernel>); -REGISTER_OP_CUDA_KERNEL( - unsqueeze2, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel, - ops::UnsqueezeKernel>, - ops::UnsqueezeKernel>); -REGISTER_OP_CUDA_KERNEL( - unsqueeze2_grad, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel, - ops::Unsqueeze2GradKernel>, - ops::Unsqueeze2GradKernel>); diff --git a/paddle/phi/core/compat/op_utils.h b/paddle/phi/core/compat/op_utils.h index 946230cb169..613a2f9960a 100644 --- a/paddle/phi/core/compat/op_utils.h +++ b/paddle/phi/core/compat/op_utils.h @@ -42,6 +42,10 @@ const std::unordered_set deprecated_op_names({"diag", "flatten_grad", "isinf", "isnan", + "unsqueeze", + "unsqueeze_grad", + "squeeze", + "squeeze_grad", "isfinite", "matmul", "matmul_grad", diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 80503dd2430..e44032285ac 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -24,6 +24,7 @@ limitations under the License. */ #include "paddle/phi/core/infermeta_utils.h" #include "paddle/phi/kernels/funcs/pooling.h" #include "paddle/phi/kernels/funcs/unfold_functor.h" +#include "paddle/phi/kernels/funcs/unsqueeze.h" namespace phi { @@ -1497,6 +1498,40 @@ void SplitInferMeta(const MetaTensor& x, } } +void SqueezeInferMeta(const MetaTensor& x, + const std::vector& axes, + MetaTensor* xshape, + MetaTensor* out) { + const auto& x_dims = x.dims(); + // Check input tensor dims (<6) Eigen limit. + PADDLE_ENFORCE_LE(x_dims.size(), + 6, + phi::errors::InvalidArgument( + "The dimensions of Input(X) " + "should be in the range of [1, 6] (Eigen limit)." + "But received X's dimensions = %d, X's shape = [%s].", + x_dims.size(), + x_dims)); + + auto out_dims = funcs::GetOutputSqueezeShape(axes, x_dims, false); + out->set_dims(out_dims); + if (x_dims[0] == out_dims[0]) { + // Only pass LoD when the first dimension of output and Input(X) + // are the same. + out->share_lod(x); + } + + std::vector xshape_dims(x_dims.size() + 1); + xshape_dims[0] = 0; + for (int i = 0; i < x_dims.size(); ++i) { + xshape_dims[i + 1] = x_dims[i]; + } + xshape->set_dims(phi::make_ddim(xshape_dims)); + xshape->share_lod(x); + xshape->set_dtype(x.dtype()); + out->set_dtype(x.dtype()); +} + /* Why not use SumRawInferMeta directly? Because we need make InferMetaFunction's args follow the design of api.yaml */ @@ -1982,6 +2017,41 @@ void UnfoldInferMeta(const MetaTensor& x, out->set_dims(phi::make_ddim(out_dims)); } +void UnsqueezeInferMeta(const MetaTensor& x, + const ScalarArray& axes, + MetaTensor* xshape, + MetaTensor* out) { + const auto& x_dims = x.dims(); + // Validity Check: input tensor dims (<6). + PADDLE_ENFORCE_LE(x_dims.size(), + 6, + phi::errors::InvalidArgument( + "Invalid " + "dimensions, the rank of Input(X) " + "should be in the range of [1, 6] (Eigen limit)")); + if (!axes.GetData().empty()) { + std::vector tmp; + tmp.reserve(axes.GetData().size()); + std::for_each(axes.GetData().begin(), + axes.GetData().end(), + [&tmp](const int64_t& t) { tmp.push_back(t); }); + auto out_dims = funcs::GetUnsqueezeShape(tmp, x_dims); + out->set_dims(out_dims); + if (x_dims[0] == out_dims[0]) { + out->share_lod(x); + } + } + std::vector xshape_dims(x_dims.size() + 1); + xshape_dims[0] = 0; + for (int i = 0; i < x_dims.size(); ++i) { + xshape_dims[i + 1] = x_dims[i]; + } + xshape->set_dims(phi::make_ddim(xshape_dims)); + xshape->share_lod(x); + out->set_dtype(x.dtype()); + xshape->set_dtype(x.dtype()); +} + void OneHotRawInferMeta(const MetaTensor& x, int32_t depth, DataType dtype, @@ -1992,7 +2062,6 @@ void OneHotRawInferMeta(const MetaTensor& x, x_dims.size(), 1, phi::errors::InvalidArgument("Rank of Input(X) should be at least 1.")); - auto out_dims_vec = phi::vectorize(x_dims); out_dims_vec.push_back(depth); auto out_dims = phi::make_ddim(out_dims_vec); diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index 0322a18fc31..f623f14a709 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -229,6 +229,11 @@ void SplitInferMeta(const MetaTensor& x_meta, std::vector out, MetaConfig config = MetaConfig()); +void SqueezeInferMeta(const MetaTensor& x, + const std::vector& axes, + MetaTensor* xshape, + MetaTensor* out); + void SumInferMeta(const MetaTensor& x, const std::vector& axis, DataType dtype, @@ -290,6 +295,11 @@ void UnfoldInferMeta(const MetaTensor& x, MetaTensor* out, MetaConfig config = MetaConfig()); +void UnsqueezeInferMeta(const MetaTensor& x, + const ScalarArray& axes, + MetaTensor* xshape, + MetaTensor* out); + void OneHotRawInferMeta(const MetaTensor& x, int32_t depth, DataType dtype, diff --git a/paddle/phi/kernels/cpu/squeeze_grad_kernel.cc b/paddle/phi/kernels/cpu/squeeze_grad_kernel.cc new file mode 100644 index 00000000000..5f605e6c250 --- /dev/null +++ b/paddle/phi/kernels/cpu/squeeze_grad_kernel.cc @@ -0,0 +1,34 @@ +// 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/squeeze_grad_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/squeeze_grad_kernel_impl.h" + +PD_REGISTER_KERNEL(squeeze_grad, + CPU, + ALL_LAYOUT, + phi::SqueezeGradKernel, + float, + double, + phi::dtype::bfloat16, + bool, + int, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/cpu/squeeze_kernel.cc b/paddle/phi/kernels/cpu/squeeze_kernel.cc new file mode 100644 index 00000000000..7d5a6ca4e88 --- /dev/null +++ b/paddle/phi/kernels/cpu/squeeze_kernel.cc @@ -0,0 +1,34 @@ +// 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/squeeze_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/squeeze_kernel_impl.h" + +PD_REGISTER_KERNEL(squeeze, + CPU, + ALL_LAYOUT, + phi::SqueezeKernel, + float, + double, + phi::dtype::bfloat16, + bool, + int, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/cpu/unsqueeze_grad_kernel.cc b/paddle/phi/kernels/cpu/unsqueeze_grad_kernel.cc new file mode 100644 index 00000000000..0cbccac4734 --- /dev/null +++ b/paddle/phi/kernels/cpu/unsqueeze_grad_kernel.cc @@ -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. + +#include "paddle/phi/kernels/unsqueeze_grad_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/unsqueeze_grad_kernel_impl.h" + +PD_REGISTER_KERNEL(unsqueeze_grad, + CPU, + ALL_LAYOUT, + phi::UnsqueezeGradKernel, + phi::dtype::bfloat16, + bool, + int, + int16_t, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex, + float, + double) {} diff --git a/paddle/phi/kernels/cpu/unsqueeze_kernel.cc b/paddle/phi/kernels/cpu/unsqueeze_kernel.cc new file mode 100644 index 00000000000..0152a31f80b --- /dev/null +++ b/paddle/phi/kernels/cpu/unsqueeze_kernel.cc @@ -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. + +#include "paddle/phi/kernels/unsqueeze_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/unsqueeze_kernel_impl.h" + +PD_REGISTER_KERNEL(unsqueeze, + CPU, + ALL_LAYOUT, + phi::UnsqueezeKernel, + float, + double, + phi::dtype::bfloat16, + bool, + int, + int16_t, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/funcs/unsqueeze.h b/paddle/phi/kernels/funcs/unsqueeze.h index 7b8a81471ef..2d77c809bf9 100644 --- a/paddle/phi/kernels/funcs/unsqueeze.h +++ b/paddle/phi/kernels/funcs/unsqueeze.h @@ -21,6 +21,118 @@ namespace phi { namespace funcs { +inline DDim GetOutputSqueezeShape(const std::vector squeeze_dims, + const DDim& in_dims, + bool is_runtime) { + size_t num_squeeze_dims = squeeze_dims.size(); + std::vector should_squeeze(in_dims.size(), false); + + // Mark dimensions need to be squeezed. + if (num_squeeze_dims == 0) { + for (int i = 0; i < in_dims.size(); ++i) { + if (in_dims[i] == 1) { + should_squeeze[i] = true; + } + } + } else { + for (size_t i = 0; i < num_squeeze_dims; ++i) { + int current = squeeze_dims[i] < 0 ? squeeze_dims[i] + in_dims.size() + : squeeze_dims[i]; + + PADDLE_ENFORCE_GE( + current, + 0, + phi::errors::InvalidArgument( + "Each axis in Attr(axes) should be in the range of [%d, %d]" + "But current axis is:%d, input tensor's shape = [%s].", + -in_dims.size(), + in_dims.size() - 1, + current, + in_dims)); + PADDLE_ENFORCE_LT( + current, + in_dims.size(), + phi::errors::InvalidArgument( + "Each axis in Attr(axes) should be in the range of [%d, %d]" + "But current axis is:%d, input tensor's shape = [%s].", + -in_dims.size(), + in_dims.size() - 1, + current, + in_dims)); + + if (!should_squeeze[current]) { + if (is_runtime) { + // At run time, dim of 1 is allowed to squeeze + if (in_dims[current] == 1) { + should_squeeze[current] = true; + } + } else { + // At compile time, dim of -1 or 1 is allowed to squeeze + if (in_dims[current] == 1 || in_dims[current] == -1) { + should_squeeze[current] = true; + } + } + } + } + } + // Make output dimensions + std::vector output_shape; + for (int i = 0; i < in_dims.size(); ++i) { + if (!should_squeeze[i]) { + output_shape.push_back(in_dims[i]); + } + } + return phi::make_ddim(output_shape); +} + +inline DDim GetUnsqueezeShape(const std::vector unsqz_dims, + const DDim& in_dims) { + int output_size = in_dims.size() + static_cast(unsqz_dims.size()); + int cur_output_size = in_dims.size(); + std::vector output_shape(output_size, 0); + + // Validity Check: rank range. + PADDLE_ENFORCE_LE( + output_size, + 6, + phi::errors::InvalidArgument("The output " + "tensor's rank should be less than 6.")); + + for (int axis : unsqz_dims) { + int cur = axis < 0 ? axis + cur_output_size + 1 : axis; + // Vaildity Check: the axis bound + PADDLE_ENFORCE_GE( + cur, + 0, + phi::errors::InvalidArgument("The insert dimension value should " + "not be less than 0")); + PADDLE_ENFORCE_LE(cur, + cur_output_size, + phi::errors::InvalidArgument( + "The insert dimension value shoule not be larger " + "than the dimension size of input tensor")); + // Move old axis, and insert new axis + for (int i = cur_output_size; i >= cur; --i) { + if (output_shape[i] == 1) { + // Move axis + output_shape[i + 1] = 1; + output_shape[i] = 0; + } + } + output_shape[cur] = 1; + // Add the output size. + cur_output_size++; + } + + // Make output shape + for (int in_idx = 0, out_idx = 0; out_idx < output_size; ++out_idx) { + if (output_shape[out_idx] == 0) { + output_shape[out_idx] = in_dims[in_idx++]; + } + } + + return phi::make_ddim(output_shape); +} inline const DenseTensor Unsqueeze(const DenseTensor& x, int axis = 0) { // don't copy data, only change the dims diff --git a/paddle/phi/kernels/gpu/squeeze_grad_kernel.cu b/paddle/phi/kernels/gpu/squeeze_grad_kernel.cu new file mode 100644 index 00000000000..c5a243f45bd --- /dev/null +++ b/paddle/phi/kernels/gpu/squeeze_grad_kernel.cu @@ -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. + +#include "paddle/phi/kernels/squeeze_grad_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/squeeze_grad_kernel_impl.h" + +PD_REGISTER_KERNEL(squeeze_grad, + GPU, + ALL_LAYOUT, + phi::SqueezeGradKernel, + float, + double, + phi::dtype::bfloat16, + phi::dtype::float16, + bool, + int, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/gpu/squeeze_kernel.cu b/paddle/phi/kernels/gpu/squeeze_kernel.cu new file mode 100644 index 00000000000..ae15e210a02 --- /dev/null +++ b/paddle/phi/kernels/gpu/squeeze_kernel.cu @@ -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. + +#include "paddle/phi/kernels/squeeze_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/squeeze_kernel_impl.h" + +PD_REGISTER_KERNEL(squeeze, + GPU, + ALL_LAYOUT, + phi::SqueezeKernel, + float, + double, + phi::dtype::bfloat16, + phi::dtype::float16, + bool, + int, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/gpu/unsqueeze_grad_kernel.cu b/paddle/phi/kernels/gpu/unsqueeze_grad_kernel.cu new file mode 100644 index 00000000000..6c3a2066f0f --- /dev/null +++ b/paddle/phi/kernels/gpu/unsqueeze_grad_kernel.cu @@ -0,0 +1,36 @@ +// 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/unsqueeze_grad_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/unsqueeze_grad_kernel_impl.h" + +PD_REGISTER_KERNEL(unsqueeze_grad, + GPU, + ALL_LAYOUT, + phi::UnsqueezeGradKernel, + phi::dtype::bfloat16, + phi::dtype::float16, + bool, + int, + int16_t, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex, + float, + double) {} diff --git a/paddle/phi/kernels/gpu/unsqueeze_kernel.cu b/paddle/phi/kernels/gpu/unsqueeze_kernel.cu new file mode 100644 index 00000000000..86b44622546 --- /dev/null +++ b/paddle/phi/kernels/gpu/unsqueeze_kernel.cu @@ -0,0 +1,36 @@ +// 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/unsqueeze_kernel.h" + +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/unsqueeze_kernel_impl.h" + +PD_REGISTER_KERNEL(unsqueeze, + GPU, + ALL_LAYOUT, + phi::UnsqueezeKernel, + float, + double, + phi::dtype::float16, + phi::dtype::bfloat16, + bool, + int, + int16_t, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} diff --git a/paddle/phi/kernels/impl/squeeze_grad_kernel_impl.h b/paddle/phi/kernels/impl/squeeze_grad_kernel_impl.h new file mode 100644 index 00000000000..c74aa5c7243 --- /dev/null +++ b/paddle/phi/kernels/impl/squeeze_grad_kernel_impl.h @@ -0,0 +1,33 @@ +// 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" +#include "paddle/phi/kernels/copy_kernel.h" + +namespace phi { +template +void SqueezeGradKernel(const Context& dev_ctx, + const DenseTensor& xshape, + const DenseTensor& dout, + const std::vector& axes, + DenseTensor* dx) { + auto xshape_dims = xshape.dims(); + auto x_dims = phi::slice_ddim(xshape_dims, 1, xshape_dims.size()); + + dev_ctx.template Alloc(dx); + phi::Copy(dev_ctx, dout, dev_ctx.GetPlace(), false, dx); + dx->Resize(x_dims); +} +} // namespace phi diff --git a/paddle/phi/kernels/impl/squeeze_kernel_impl.h b/paddle/phi/kernels/impl/squeeze_kernel_impl.h new file mode 100644 index 00000000000..d2b40824a91 --- /dev/null +++ b/paddle/phi/kernels/impl/squeeze_kernel_impl.h @@ -0,0 +1,34 @@ +// 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" +#include "paddle/phi/kernels/copy_kernel.h" +#include "paddle/phi/kernels/funcs/unsqueeze.h" + +namespace phi { +template +void SqueezeKernel(const Context& dev_ctx, + const DenseTensor& x, + const std::vector& axes, + DenseTensor* xshape, + DenseTensor* out) { + auto x_dims = x.dims(); + auto out_dims = funcs::GetOutputSqueezeShape(axes, x_dims, true); + + dev_ctx.template Alloc(out); + phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out); + out->Resize(out_dims); +} +} // namespace phi diff --git a/paddle/phi/kernels/impl/unsqueeze_grad_kernel_impl.h b/paddle/phi/kernels/impl/unsqueeze_grad_kernel_impl.h new file mode 100644 index 00000000000..54b332ea4c8 --- /dev/null +++ b/paddle/phi/kernels/impl/unsqueeze_grad_kernel_impl.h @@ -0,0 +1,31 @@ +// 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" +#include "paddle/phi/kernels/copy_kernel.h" + +namespace phi { +template +void UnsqueezeGradKernel(const Context& dev_ctx, + const DenseTensor& x_shape, + const DenseTensor& dout, + DenseTensor* dx) { + auto xshape_dims = x_shape.dims(); + auto x_dims = phi::slice_ddim(xshape_dims, 1, xshape_dims.size()); + dev_ctx.template Alloc(dx); + phi::Copy(dev_ctx, dout, dev_ctx.GetPlace(), true, dx); + dx->Resize(x_dims); +} +} // namespace phi diff --git a/paddle/phi/kernels/impl/unsqueeze_kernel_impl.h b/paddle/phi/kernels/impl/unsqueeze_kernel_impl.h new file mode 100644 index 00000000000..884fa26df45 --- /dev/null +++ b/paddle/phi/kernels/impl/unsqueeze_kernel_impl.h @@ -0,0 +1,42 @@ +// 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" +#include "paddle/phi/kernels/copy_kernel.h" +#include "paddle/phi/kernels/funcs/unsqueeze.h" + +namespace phi { +template +void UnsqueezeKernel(const Context& dev_ctx, + const DenseTensor& x, + const ScalarArray& axes, + DenseTensor* xshape, + DenseTensor* out) { + auto x_dims = x.dims(); + auto out_dims = out->dims(); + if (axes.FromTensor()) { + std::vector tmp; + tmp.reserve(axes.GetData().size()); + std::for_each(axes.GetData().begin(), + axes.GetData().end(), + [&tmp](const int64_t& t) { tmp.push_back(t); }); + out_dims = funcs::GetUnsqueezeShape(tmp, x_dims); + } + out->Resize(out_dims); + dev_ctx.template Alloc(out); + phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out); + out->Resize(out_dims); // copy will reset the dims. +} +} // namespace phi diff --git a/paddle/phi/kernels/squeeze_grad_kernel.h b/paddle/phi/kernels/squeeze_grad_kernel.h new file mode 100644 index 00000000000..52b02bdbb95 --- /dev/null +++ b/paddle/phi/kernels/squeeze_grad_kernel.h @@ -0,0 +1,28 @@ + +// 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 SqueezeGradKernel(const Context& dev_ctx, + const DenseTensor& xshape, + const DenseTensor& dout, + const std::vector& axes, + DenseTensor* dx); +} // namespace phi diff --git a/paddle/phi/kernels/squeeze_kernel.h b/paddle/phi/kernels/squeeze_kernel.h new file mode 100644 index 00000000000..22254eacfce --- /dev/null +++ b/paddle/phi/kernels/squeeze_kernel.h @@ -0,0 +1,28 @@ + +// 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 SqueezeKernel(const Context& dev_ctx, + const DenseTensor& x, + const std::vector& axes, + DenseTensor* xshape, + DenseTensor* out); +} // namespace phi diff --git a/paddle/phi/kernels/unsqueeze_grad_kernel.h b/paddle/phi/kernels/unsqueeze_grad_kernel.h new file mode 100644 index 00000000000..0c5afe7be60 --- /dev/null +++ b/paddle/phi/kernels/unsqueeze_grad_kernel.h @@ -0,0 +1,27 @@ + +// 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 UnsqueezeGradKernel(const Context& dev_ctx, + const DenseTensor& x_shape, + const DenseTensor& dout, + DenseTensor* dx); +} // namespace phi diff --git a/paddle/phi/kernels/unsqueeze_kernel.h b/paddle/phi/kernels/unsqueeze_kernel.h new file mode 100644 index 00000000000..8f818a1b490 --- /dev/null +++ b/paddle/phi/kernels/unsqueeze_kernel.h @@ -0,0 +1,29 @@ + +// 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/common/scalar_array.h" +#include "paddle/phi/core/dense_tensor.h" + +namespace phi { + +template +void UnsqueezeKernel(const Context& dev_ctx, + const DenseTensor& x, + const ScalarArray& axes, + DenseTensor* xshape, + DenseTensor* out); +} // namespace phi diff --git a/paddle/phi/ops/compat/squeeze_sig.cc b/paddle/phi/ops/compat/squeeze_sig.cc new file mode 100644 index 00000000000..276246533e8 --- /dev/null +++ b/paddle/phi/ops/compat/squeeze_sig.cc @@ -0,0 +1,36 @@ + +// 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/core/compat/op_utils.h" + +namespace phi { + +KernelSignature SqueezeOpArgumentMapping(const ArgumentMappingContext& ctx) { + return KernelSignature("squeeze", {"X"}, {"axes"}, {"XShape", "Out"}); +} + +KernelSignature SqueezeGradOpArgumentMapping( + const ArgumentMappingContext& ctx) { + return KernelSignature("squeeze_grad", + {"XShape", GradVarName("Out")}, + {"axes"}, + {GradVarName("X")}); +} + +} // namespace phi +PD_REGISTER_BASE_KERNEL_NAME(squeeze2, squeeze); +PD_REGISTER_BASE_KERNEL_NAME(squeeze2_grad, squeeze_grad); +PD_REGISTER_ARG_MAPPING_FN(squeeze2, phi::SqueezeOpArgumentMapping); +PD_REGISTER_ARG_MAPPING_FN(squeeze2_grad, phi::SqueezeGradOpArgumentMapping); diff --git a/paddle/phi/ops/compat/unsqueeze_sig.cc b/paddle/phi/ops/compat/unsqueeze_sig.cc new file mode 100644 index 00000000000..20cd9701e83 --- /dev/null +++ b/paddle/phi/ops/compat/unsqueeze_sig.cc @@ -0,0 +1,46 @@ + +// 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/core/compat/op_utils.h" + +namespace phi { + +KernelSignature UnsqueezeOpArgumentMapping(const ArgumentMappingContext& ctx) { + if (ctx.InputSize("AxesTensorList") > 0) { + VLOG(2) << "unsqueeze2 in AxesTensorList"; + return KernelSignature( + "unsqueeze", {"X"}, {"AxesTensorList"}, {"XShape", "Out"}); + } else if (ctx.InputSize("AxesTensor") > 0) { + VLOG(2) << "unsqueeze2 in AxesTensor"; + return KernelSignature( + "unsqueeze", {"X"}, {"AxesTensor"}, {"XShape", "Out"}); + } else { + VLOG(2) << "unsqueeze2 in axes"; + return KernelSignature("unsqueeze", {"X"}, {"axes"}, {"XShape", "Out"}); + } +} + +KernelSignature UnsqueezeGradOpArgumentMapping( + const ArgumentMappingContext& ctx) { + return KernelSignature( + "unsqueeze_grad", {"XShape", GradVarName("Out")}, {}, {GradVarName("X")}); +} +} // namespace phi +PD_REGISTER_BASE_KERNEL_NAME(unsqueeze2, unsqueeze); +PD_REGISTER_BASE_KERNEL_NAME(unsqueeze2_grad, unsqueeze_grad); + +PD_REGISTER_ARG_MAPPING_FN(unsqueeze2, phi::UnsqueezeOpArgumentMapping); +PD_REGISTER_ARG_MAPPING_FN(unsqueeze2_grad, + phi::UnsqueezeGradOpArgumentMapping); -- GitLab