From d3366853632ec4602067654f8e2372a5c99af0f6 Mon Sep 17 00:00:00 2001 From: ykkk2333 <77383312+ykkk2333@users.noreply.github.com> Date: Tue, 13 Sep 2022 16:21:01 +0800 Subject: [PATCH] migrate squeeze kernel to phi, test=kunlun (#45968) --- paddle/fluid/operators/squeeze_op_xpu.cc | 62 -------- paddle/phi/kernels/cpu/squeeze_grad_kernel.cc | 34 ----- paddle/phi/kernels/cpu/squeeze_kernel.cc | 49 ------- paddle/phi/kernels/gpu/squeeze_grad_kernel.cu | 35 ----- .../kernels/impl/squeeze_grad_kernel_impl.h | 34 ----- paddle/phi/kernels/impl/squeeze_kernel_impl.h | 45 ------ ...ueeze_kernel.cu => squeeze_grad_kernel.cc} | 54 +++++-- paddle/phi/kernels/squeeze_kernel.cc | 138 ++++++++++++++++++ 8 files changed, 182 insertions(+), 269 deletions(-) delete mode 100644 paddle/fluid/operators/squeeze_op_xpu.cc delete mode 100644 paddle/phi/kernels/cpu/squeeze_grad_kernel.cc delete mode 100644 paddle/phi/kernels/cpu/squeeze_kernel.cc delete mode 100644 paddle/phi/kernels/gpu/squeeze_grad_kernel.cu delete mode 100644 paddle/phi/kernels/impl/squeeze_grad_kernel_impl.h delete mode 100644 paddle/phi/kernels/impl/squeeze_kernel_impl.h rename paddle/phi/kernels/{gpu/squeeze_kernel.cu => squeeze_grad_kernel.cc} (54%) create mode 100644 paddle/phi/kernels/squeeze_kernel.cc diff --git a/paddle/fluid/operators/squeeze_op_xpu.cc b/paddle/fluid/operators/squeeze_op_xpu.cc deleted file mode 100644 index 9dbe0c2aed2..00000000000 --- a/paddle/fluid/operators/squeeze_op_xpu.cc +++ /dev/null @@ -1,62 +0,0 @@ -/* Copyright (c) 2019 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/fluid/operators/squeeze_op.h" -#ifdef PADDLE_WITH_XPU - -namespace ops = paddle::operators; -namespace plat = paddle::platform; - -REGISTER_OP_XPU_KERNEL( - squeeze, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel, - ops::SqueezeKernel); -REGISTER_OP_XPU_KERNEL( - squeeze_grad, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel, - ops::SqueezeGradKernel); -REGISTER_OP_XPU_KERNEL( - squeeze2, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel, - ops::Squeeze2Kernel); -REGISTER_OP_XPU_KERNEL( - squeeze2_grad, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel, - ops::Squeeze2GradKernel); - -#endif diff --git a/paddle/phi/kernels/cpu/squeeze_grad_kernel.cc b/paddle/phi/kernels/cpu/squeeze_grad_kernel.cc deleted file mode 100644 index 5f605e6c250..00000000000 --- a/paddle/phi/kernels/cpu/squeeze_grad_kernel.cc +++ /dev/null @@ -1,34 +0,0 @@ -// 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 deleted file mode 100644 index d22efdf9694..00000000000 --- a/paddle/phi/kernels/cpu/squeeze_kernel.cc +++ /dev/null @@ -1,49 +0,0 @@ -// 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) {} - -PD_REGISTER_KERNEL(squeeze_with_xshape, - CPU, - ALL_LAYOUT, - phi::SqueezeWithXShapeKernel, - 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/gpu/squeeze_grad_kernel.cu b/paddle/phi/kernels/gpu/squeeze_grad_kernel.cu deleted file mode 100644 index c5a243f45bd..00000000000 --- a/paddle/phi/kernels/gpu/squeeze_grad_kernel.cu +++ /dev/null @@ -1,35 +0,0 @@ -// 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/impl/squeeze_grad_kernel_impl.h b/paddle/phi/kernels/impl/squeeze_grad_kernel_impl.h deleted file mode 100644 index 790de83050f..00000000000 --- a/paddle/phi/kernels/impl/squeeze_grad_kernel_impl.h +++ /dev/null @@ -1,34 +0,0 @@ -// 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/int_array.h" -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/tensor_utils.h" - -namespace phi { -template -void SqueezeGradKernel(const Context& dev_ctx, - const DenseTensor& xshape, - const DenseTensor& dout, - const IntArray& 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 deleted file mode 100644 index cb5eed521cd..00000000000 --- a/paddle/phi/kernels/impl/squeeze_kernel_impl.h +++ /dev/null @@ -1,45 +0,0 @@ -// 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/core/tensor_utils.h" -#include "paddle/phi/kernels/funcs/unsqueeze.h" - -namespace phi { -template -void SqueezeKernel(const Context& dev_ctx, - const DenseTensor& x, - const IntArray& axes, - DenseTensor* out) { - auto x_dims = x.dims(); - std::vector tmp(axes.GetData().begin(), axes.GetData().end()); - auto out_dims = funcs::GetOutputSqueezeShape(tmp, x_dims, true); - 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. -} - -template -void SqueezeWithXShapeKernel(const Context& dev_ctx, - const DenseTensor& x, - const IntArray& axes, - DenseTensor* out, - DenseTensor* xshape) { - SqueezeKernel(dev_ctx, x, axes, out); -} - -} // namespace phi diff --git a/paddle/phi/kernels/gpu/squeeze_kernel.cu b/paddle/phi/kernels/squeeze_grad_kernel.cc similarity index 54% rename from paddle/phi/kernels/gpu/squeeze_kernel.cu rename to paddle/phi/kernels/squeeze_grad_kernel.cc index 06ddba2ef1c..0fe2f790734 100644 --- a/paddle/phi/kernels/gpu/squeeze_kernel.cu +++ b/paddle/phi/kernels/squeeze_grad_kernel.cc @@ -12,20 +12,35 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/phi/kernels/squeeze_kernel.h" +#include "paddle/phi/kernels/squeeze_grad_kernel.h" -#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/backends/all_context.h" #include "paddle/phi/core/kernel_registry.h" -#include "paddle/phi/kernels/impl/squeeze_kernel_impl.h" +#include "paddle/phi/core/tensor_utils.h" -PD_REGISTER_KERNEL(squeeze, - GPU, +namespace phi { +template +void SqueezeGradKernel(const Context& dev_ctx, + const DenseTensor& xshape, + const DenseTensor& dout, + const IntArray& 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 + +PD_REGISTER_KERNEL(squeeze_grad, + CPU, ALL_LAYOUT, - phi::SqueezeKernel, + phi::SqueezeGradKernel, float, double, phi::dtype::bfloat16, - phi::dtype::float16, bool, int, uint8_t, @@ -34,14 +49,15 @@ PD_REGISTER_KERNEL(squeeze, phi::dtype::complex, phi::dtype::complex) {} -PD_REGISTER_KERNEL(squeeze_with_xshape, +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PD_REGISTER_KERNEL(squeeze_grad, GPU, ALL_LAYOUT, - phi::SqueezeWithXShapeKernel, + phi::SqueezeGradKernel, float, double, - phi::dtype::bfloat16, phi::dtype::float16, + phi::dtype::bfloat16, bool, int, uint8_t, @@ -49,3 +65,21 @@ PD_REGISTER_KERNEL(squeeze_with_xshape, int64_t, phi::dtype::complex, phi::dtype::complex) {} + +#endif + +#ifdef PADDLE_WITH_XPU +PD_REGISTER_KERNEL(squeeze_grad, + XPU, + ALL_LAYOUT, + phi::SqueezeGradKernel, + float, + double, + phi::dtype::float16, + bool, + int, + uint8_t, + int8_t, + int64_t) {} + +#endif diff --git a/paddle/phi/kernels/squeeze_kernel.cc b/paddle/phi/kernels/squeeze_kernel.cc new file mode 100644 index 00000000000..46cbbb174b8 --- /dev/null +++ b/paddle/phi/kernels/squeeze_kernel.cc @@ -0,0 +1,138 @@ +// 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/all_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_utils.h" +#include "paddle/phi/kernels/funcs/unsqueeze.h" + +namespace phi { +template +void SqueezeKernel(const Context& dev_ctx, + const DenseTensor& x, + const IntArray& axes, + DenseTensor* out) { + auto x_dims = x.dims(); + std::vector tmp(axes.GetData().begin(), axes.GetData().end()); + auto out_dims = funcs::GetOutputSqueezeShape(tmp, x_dims, true); + 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. +} + +template +void SqueezeWithXShapeKernel(const Context& dev_ctx, + const DenseTensor& x, + const IntArray& axes, + DenseTensor* out, + DenseTensor* xshape) { + SqueezeKernel(dev_ctx, x, axes, out); +} + +} // namespace phi + +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) {} + +PD_REGISTER_KERNEL(squeeze_with_xshape, + CPU, + ALL_LAYOUT, + phi::SqueezeWithXShapeKernel, + float, + double, + phi::dtype::bfloat16, + bool, + int, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PD_REGISTER_KERNEL(squeeze, + GPU, + ALL_LAYOUT, + phi::SqueezeKernel, + float, + double, + phi::dtype::float16, + phi::dtype::bfloat16, + bool, + int, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} + +PD_REGISTER_KERNEL(squeeze_with_xshape, + GPU, + ALL_LAYOUT, + phi::SqueezeWithXShapeKernel, + float, + double, + phi::dtype::float16, + phi::dtype::bfloat16, + bool, + int, + uint8_t, + int8_t, + int64_t, + phi::dtype::complex, + phi::dtype::complex) {} +#endif + +#ifdef PADDLE_WITH_XPU +PD_REGISTER_KERNEL(squeeze, + XPU, + ALL_LAYOUT, + phi::SqueezeKernel, + float, + double, + phi::dtype::float16, + bool, + int, + uint8_t, + int8_t, + int64_t) {} + +PD_REGISTER_KERNEL(squeeze_with_xshape, + XPU, + ALL_LAYOUT, + phi::SqueezeWithXShapeKernel, + float, + double, + phi::dtype::float16, + bool, + int, + uint8_t, + int8_t, + int64_t) {} +#endif -- GitLab