diff --git a/paddle/fluid/operators/squeeze_op_xpu.cc b/paddle/fluid/operators/squeeze_op_xpu.cc deleted file mode 100644 index 9dbe0c2aed2dfcdedc3d662221086594ade23a55..0000000000000000000000000000000000000000 --- 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 5f605e6c2504baec2276ed9288c72f466f3c40f0..0000000000000000000000000000000000000000 --- 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 d22efdf96944042c70e172add0e6e359a03db967..0000000000000000000000000000000000000000 --- 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 c5a243f45bd9769b3f6a052248e979dad10e0a4f..0000000000000000000000000000000000000000 --- 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 790de83050f8387edabbffb01aa1e326b522e590..0000000000000000000000000000000000000000 --- 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 cb5eed521cd226d6d390c86a32092bebc1e6905c..0000000000000000000000000000000000000000 --- 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 06ddba2ef1c2bc0c815342c24868b5b9334c49ec..0fe2f79073430d6f1cabff002c9c448d565413e9 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 0000000000000000000000000000000000000000..46cbbb174b85540604c36e5cd374606ab1697238 --- /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