From 6f2bac7c8e876738eafc87c362bd0a5152eb1d91 Mon Sep 17 00:00:00 2001 From: Charles-hit <56987902+Charles-hit@users.noreply.github.com> Date: Wed, 31 Aug 2022 20:06:36 +0800 Subject: [PATCH] move elementwise XPU kernels to phi (#45603) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * move elementwise_floordiv、elementwise_max、elementwise_max_grad XPU kernel to phi,test=kunlun * move elementwise_min elementwise_min_grad kernels to phi,test=kunlun * delete elementwise_min_xpu.cc,test=kunlun * move elementwise_mod elementwise_pow XPU kernels to phi,test=kunlun --- .../elementwise_floordiv_op_xpu.cc | 42 ------- .../elementwise/elementwise_max_op_xpu.cc | 54 --------- .../elementwise/elementwise_min_op_xpu.cc | 54 --------- .../elementwise/elementwise_mod_op_xpu.cc | 50 -------- .../elementwise/elementwise_pow_op_xpu.cc | 43 ------- paddle/phi/kernels/elementwise_kernel.cc | 27 +++++ .../kernels/xpu/elementwise_grad_kernel.cc | 76 ++++++++++++ paddle/phi/kernels/xpu/elementwise_kernel.cc | 111 ++++++++++++++++++ 8 files changed, 214 insertions(+), 243 deletions(-) delete mode 100644 paddle/fluid/operators/elementwise/elementwise_floordiv_op_xpu.cc delete mode 100644 paddle/fluid/operators/elementwise/elementwise_max_op_xpu.cc delete mode 100644 paddle/fluid/operators/elementwise/elementwise_min_op_xpu.cc delete mode 100644 paddle/fluid/operators/elementwise/elementwise_mod_op_xpu.cc delete mode 100644 paddle/fluid/operators/elementwise/elementwise_pow_op_xpu.cc create mode 100644 paddle/phi/kernels/xpu/elementwise_grad_kernel.cc create mode 100644 paddle/phi/kernels/xpu/elementwise_kernel.cc diff --git a/paddle/fluid/operators/elementwise/elementwise_floordiv_op_xpu.cc b/paddle/fluid/operators/elementwise/elementwise_floordiv_op_xpu.cc deleted file mode 100644 index fed68b615c7..00000000000 --- a/paddle/fluid/operators/elementwise/elementwise_floordiv_op_xpu.cc +++ /dev/null @@ -1,42 +0,0 @@ -/* Copyright (c) 2020 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. */ - -#ifdef PADDLE_WITH_XPU -#include "paddle/fluid/operators/elementwise/elementwise_div_op.h" -#include "paddle/fluid/operators/elementwise/elementwise_op.h" -#include "paddle/fluid/operators/elementwise/elementwise_xpu.h" -namespace paddle { -namespace operators { - -template -class ElementwiseFloordivXPUKernel : public framework::OpKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - XPUElementwise(ctx, xpu::broadcast_floordiv); - } -}; - -} // namespace operators -} // namespace paddle -namespace ops = paddle::operators; -REGISTER_OP_XPU_KERNEL( - elementwise_floordiv, - ops::ElementwiseFloordivXPUKernel, - ops::ElementwiseFloordivXPUKernel); - -#endif diff --git a/paddle/fluid/operators/elementwise/elementwise_max_op_xpu.cc b/paddle/fluid/operators/elementwise/elementwise_max_op_xpu.cc deleted file mode 100644 index 8860985a5dd..00000000000 --- a/paddle/fluid/operators/elementwise/elementwise_max_op_xpu.cc +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright (c) 2020 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. */ - -#ifdef PADDLE_WITH_XPU - -#include "paddle/fluid/operators/elementwise/elementwise_op.h" -#include "paddle/fluid/operators/elementwise/elementwise_xpu.h" -namespace paddle { -namespace operators { - -template -class ElementwiseMaxXPUKernel : public framework::OpKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - XPUElementwise(ctx, xpu::broadcast_max); - } -}; - -template -class ElementwiseMaxGradXPUKernel : public ElemwiseGradKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - ElemwiseGradKernel::Compute(ctx); - XPUElementwiseGrad(ctx, xpu::broadcast_max_grad, true); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -REGISTER_OP_XPU_KERNEL(elementwise_max, - ops::ElementwiseMaxXPUKernel, - ops::ElementwiseMaxXPUKernel); -REGISTER_OP_XPU_KERNEL( - elementwise_max_grad, - ops::ElementwiseMaxGradXPUKernel, - ops::ElementwiseMaxGradXPUKernel); -#endif diff --git a/paddle/fluid/operators/elementwise/elementwise_min_op_xpu.cc b/paddle/fluid/operators/elementwise/elementwise_min_op_xpu.cc deleted file mode 100644 index d76508a06f0..00000000000 --- a/paddle/fluid/operators/elementwise/elementwise_min_op_xpu.cc +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright (c) 2020 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. */ - -#ifdef PADDLE_WITH_XPU - -#include "paddle/fluid/operators/elementwise/elementwise_op.h" -#include "paddle/fluid/operators/elementwise/elementwise_xpu.h" -namespace paddle { -namespace operators { - -template -class ElementwiseMinXPUKernel : public framework::OpKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - XPUElementwise(ctx, xpu::broadcast_min); - } -}; - -template -class ElementwiseMinGradXPUKernel : public ElemwiseGradKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - ElemwiseGradKernel::Compute(ctx); - XPUElementwiseGrad(ctx, xpu::broadcast_min_grad, true); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -REGISTER_OP_XPU_KERNEL(elementwise_min, - ops::ElementwiseMinXPUKernel, - ops::ElementwiseMinXPUKernel); -REGISTER_OP_XPU_KERNEL( - elementwise_min_grad, - ops::ElementwiseMinGradXPUKernel, - ops::ElementwiseMinGradXPUKernel); -#endif diff --git a/paddle/fluid/operators/elementwise/elementwise_mod_op_xpu.cc b/paddle/fluid/operators/elementwise/elementwise_mod_op_xpu.cc deleted file mode 100644 index 6869ab72993..00000000000 --- a/paddle/fluid/operators/elementwise/elementwise_mod_op_xpu.cc +++ /dev/null @@ -1,50 +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. */ - -#ifdef PADDLE_WITH_XPU -#include -#include - -#include "paddle/fluid/framework/data_layout.h" -#include "paddle/fluid/framework/op_version_registry.h" -#include "paddle/fluid/operators/common_infer_shape_functions.h" -#include "paddle/fluid/operators/elementwise/elementwise_op_function.h" -#include "paddle/fluid/operators/elementwise/elementwise_xpu.h" -#include "paddle/fluid/platform/device/device_wrapper.h" - -namespace paddle { -namespace operators { - -template -class ElementwiseModXPUKernel : public framework::OpKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - XPUElementwise(ctx, xpu::broadcast_mod); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -REGISTER_OP_XPU_KERNEL(elementwise_mod, - ops::ElementwiseModXPUKernel, - ops::ElementwiseModXPUKernel, - ops::ElementwiseModXPUKernel, - ops::ElementwiseModXPUKernel); - -#endif diff --git a/paddle/fluid/operators/elementwise/elementwise_pow_op_xpu.cc b/paddle/fluid/operators/elementwise/elementwise_pow_op_xpu.cc deleted file mode 100644 index 78855dd3957..00000000000 --- a/paddle/fluid/operators/elementwise/elementwise_pow_op_xpu.cc +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright (c) 2020 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. */ - -#ifdef PADDLE_WITH_XPU -#include "paddle/fluid/operators/elementwise/elementwise_op.h" -#include "paddle/fluid/operators/elementwise/elementwise_xpu.h" -#include "xpu/refactor/math.h" - -namespace paddle { -namespace operators { - -template -class ElementwisePowXPUKernel : public framework::OpKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - XPUElementwise(ctx, xpu::broadcast_pow); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -REGISTER_OP_XPU_KERNEL( - elementwise_pow, - ops::ElementwisePowXPUKernel, - ops::ElementwisePowXPUKernel); - -#endif diff --git a/paddle/phi/kernels/elementwise_kernel.cc b/paddle/phi/kernels/elementwise_kernel.cc index 6db4a036bac..95f7cc10709 100644 --- a/paddle/phi/kernels/elementwise_kernel.cc +++ b/paddle/phi/kernels/elementwise_kernel.cc @@ -336,3 +336,30 @@ PD_REGISTER_KERNEL(multiply, float) {} #endif + +#if defined PADDLE_WITH_XPU +PD_REGISTER_KERNEL(floor_divide, + XPU, + ALL_LAYOUT, + phi::FloorDivideKernel, + float, + phi::dtype::float16) {} +PD_REGISTER_KERNEL( + maximum, XPU, ALL_LAYOUT, phi::MaximumKernel, float, phi::dtype::float16) {} +PD_REGISTER_KERNEL( + minimum, XPU, ALL_LAYOUT, phi::MinimumKernel, float, phi::dtype::float16) {} +PD_REGISTER_KERNEL(remainder, + XPU, + ALL_LAYOUT, + phi::RemainderKernel, + float, + phi::dtype::float16, + int32_t, + int64_t) {} +PD_REGISTER_KERNEL(elementwise_pow, + XPU, + ALL_LAYOUT, + phi::ElementwisePowKernel, + float, + phi::dtype::float16) {} +#endif diff --git a/paddle/phi/kernels/xpu/elementwise_grad_kernel.cc b/paddle/phi/kernels/xpu/elementwise_grad_kernel.cc new file mode 100644 index 00000000000..9b1d2a6957f --- /dev/null +++ b/paddle/phi/kernels/xpu/elementwise_grad_kernel.cc @@ -0,0 +1,76 @@ +// 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/elementwise_grad_kernel.h" +#include "paddle/phi/kernels/xpu/elementwise.h" + +#include "paddle/phi/backends/xpu/xpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +namespace phi { + +template +void MaximumGradKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + const DenseTensor& dout, + int axis, + DenseTensor* dx, + DenseTensor* dy) { + using XPUType = typename XPUTypeTrait::Type; + XPUElementwiseGrad(dev_ctx, + x, + y, + dout, + axis, + dx, + dy, + xpu::broadcast_max_grad, + true); +} + +template +void MinimumGradKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + const DenseTensor& dout, + int axis, + DenseTensor* dx, + DenseTensor* dy) { + using XPUType = typename XPUTypeTrait::Type; + XPUElementwiseGrad(dev_ctx, + x, + y, + dout, + axis, + dx, + dy, + xpu::broadcast_min_grad, + true); +} + +} // namespace phi + +PD_REGISTER_KERNEL(maximum_grad, + XPU, + ALL_LAYOUT, + phi::MaximumGradKernel, + float, + phi::dtype::float16) {} +PD_REGISTER_KERNEL(minimum_grad, + XPU, + ALL_LAYOUT, + phi::MinimumGradKernel, + float, + phi::dtype::float16) {} diff --git a/paddle/phi/kernels/xpu/elementwise_kernel.cc b/paddle/phi/kernels/xpu/elementwise_kernel.cc new file mode 100644 index 00000000000..87edfb22e50 --- /dev/null +++ b/paddle/phi/kernels/xpu/elementwise_kernel.cc @@ -0,0 +1,111 @@ +// 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/elementwise_kernel.h" +#include "paddle/phi/kernels/xpu/elementwise.h" + +#include "paddle/phi/backends/xpu/xpu_context.h" +#include "paddle/phi/core/kernel_registry.h" + +namespace phi { + +template +void FloorDivideRawKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + int axis, + DenseTensor* out) { + using XPUType = typename XPUTypeTrait::Type; + XPUElementwise( + dev_ctx, x, y, axis, out, xpu::broadcast_floordiv); +} + +template +void MaximumRawKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + int axis, + DenseTensor* out) { + using XPUType = typename XPUTypeTrait::Type; + XPUElementwise( + dev_ctx, x, y, axis, out, xpu::broadcast_max); +} + +template +void MinimumRawKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + int axis, + DenseTensor* out) { + using XPUType = typename XPUTypeTrait::Type; + XPUElementwise( + dev_ctx, x, y, axis, out, xpu::broadcast_min); +} + +template +void RemainderRawKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + int axis, + DenseTensor* out) { + using XPUType = typename XPUTypeTrait::Type; + XPUElementwise( + dev_ctx, x, y, axis, out, xpu::broadcast_mod); +} + +template +void ElementwisePowRawKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + int axis, + DenseTensor* out) { + using XPUType = typename XPUTypeTrait::Type; + XPUElementwise( + dev_ctx, x, y, axis, out, xpu::broadcast_pow); +} + +} // namespace phi + +PD_REGISTER_KERNEL(floor_divide_raw, + XPU, + ALL_LAYOUT, + phi::FloorDivideRawKernel, + float, + phi::dtype::float16) {} +PD_REGISTER_KERNEL(maximum_raw, + XPU, + ALL_LAYOUT, + phi::MaximumRawKernel, + float, + phi::dtype::float16) {} +PD_REGISTER_KERNEL(minimum_raw, + XPU, + ALL_LAYOUT, + phi::MinimumRawKernel, + float, + phi::dtype::float16) {} +PD_REGISTER_KERNEL(remainder_raw, + XPU, + ALL_LAYOUT, + phi::RemainderRawKernel, + float, + phi::dtype::float16, + int32_t, + int64_t) {} +PD_REGISTER_KERNEL(elementwise_pow_raw, + XPU, + ALL_LAYOUT, + phi::ElementwisePowRawKernel, + float, + phi::dtype::float16) {} -- GitLab