From fb42ba7055402425dff76ac1848ad6085b679802 Mon Sep 17 00:00:00 2001 From: risemeup1 <62429225+risemeup1@users.noreply.github.com> Date: Mon, 5 Sep 2022 09:52:12 +0800 Subject: [PATCH] move elementwise_sub and elementwise_sub_grad XPU kernel to PHI,test=kunlun (#45623) * move elementwise_sub and elementwise_sub_grad XPU kernel to PHI,test=kunlun * modify code style,test=kunlun * modify elementwise_subtract_grad_kernel.cc,test=kunlun * modify elementwise_subtract_kernel.cc,test=kunlun * modify elementwise_subtract_grad_kernel.cc,test=kunlun * modify elementwise_kernel.cc and elementwise_subtract_kernel.cc,test=kunlun * modify codestyle,test=kunlun * modify elementwise_kernel.cc,test=kunlun --- .../elementwise/elementwise_sub_op_xpu.cc | 57 ------------------- paddle/phi/kernels/elementwise_kernel.cc | 7 ++- .../xpu/elementwise_subtract_grad_kernel.cc | 49 ++++++++++++++++ .../xpu/elementwise_subtract_kernel.cc | 39 +++++++++++++ 4 files changed, 94 insertions(+), 58 deletions(-) delete mode 100644 paddle/fluid/operators/elementwise/elementwise_sub_op_xpu.cc create mode 100644 paddle/phi/kernels/xpu/elementwise_subtract_grad_kernel.cc create mode 100644 paddle/phi/kernels/xpu/elementwise_subtract_kernel.cc diff --git a/paddle/fluid/operators/elementwise/elementwise_sub_op_xpu.cc b/paddle/fluid/operators/elementwise/elementwise_sub_op_xpu.cc deleted file mode 100644 index b586db4601..0000000000 --- a/paddle/fluid/operators/elementwise/elementwise_sub_op_xpu.cc +++ /dev/null @@ -1,57 +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 ElementwiseSubXPUKernel : public framework::OpKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - XPUElementwise(ctx, xpu::broadcast_sub); - } -}; - -template -class ElementwiseSubGradXPUKernel : public ElemwiseGradKernel { - using XPUType = typename XPUTypeTrait::Type; - - public: - void Compute(const framework::ExecutionContext& ctx) const override { - ElemwiseGradKernel::Compute(ctx); - XPUElementwiseGrad( - ctx, xpu::broadcast_sub_grad, false); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -REGISTER_OP_XPU_KERNEL(elementwise_sub, - ops::ElementwiseSubXPUKernel, - ops::ElementwiseSubXPUKernel); -REGISTER_OP_XPU_KERNEL( - elementwise_sub_grad, - ops::ElementwiseSubGradXPUKernel, - ops::ElementwiseSubGradXPUKernel); - -#endif diff --git a/paddle/phi/kernels/elementwise_kernel.cc b/paddle/phi/kernels/elementwise_kernel.cc index 95f7cc1070..0c208d2db0 100644 --- a/paddle/phi/kernels/elementwise_kernel.cc +++ b/paddle/phi/kernels/elementwise_kernel.cc @@ -334,7 +334,12 @@ PD_REGISTER_KERNEL(multiply, phi::MultiplyKernel, phi::dtype::float16, float) {} - +PD_REGISTER_KERNEL(subtract, + XPU, + ALL_LAYOUT, + phi::SubtractKernel, + float, + phi::dtype::float16) {} #endif #if defined PADDLE_WITH_XPU diff --git a/paddle/phi/kernels/xpu/elementwise_subtract_grad_kernel.cc b/paddle/phi/kernels/xpu/elementwise_subtract_grad_kernel.cc new file mode 100644 index 0000000000..0fb0ced46b --- /dev/null +++ b/paddle/phi/kernels/xpu/elementwise_subtract_grad_kernel.cc @@ -0,0 +1,49 @@ +/* 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_subtract_grad_kernel.h" +#include "paddle/phi/backends/xpu/xpu_context.h" +#include "paddle/phi/backends/xpu/xpu_header.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/xpu/elementwise.h" + +namespace phi { +template +void SubtractGradKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + const DenseTensor& dout, + int axis, + DenseTensor* dx, + DenseTensor* dy) { + using XPUType = typename XPUTypeTrait::Type; + phi::XPUElementwiseGrad(dev_ctx, + x, + y, + dout, + axis, + dx, + dy, + xpu::broadcast_sub_grad, + false); +} + +} // namespace phi + +PD_REGISTER_KERNEL(subtract_grad, + XPU, + ALL_LAYOUT, + phi::SubtractGradKernel, + phi::dtype::float16, + float) {} diff --git a/paddle/phi/kernels/xpu/elementwise_subtract_kernel.cc b/paddle/phi/kernels/xpu/elementwise_subtract_kernel.cc new file mode 100644 index 0000000000..299b5f80d7 --- /dev/null +++ b/paddle/phi/kernels/xpu/elementwise_subtract_kernel.cc @@ -0,0 +1,39 @@ +/* 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_subtract_kernel.h" +#include "paddle/phi/backends/xpu/xpu_context.h" +#include "paddle/phi/backends/xpu/xpu_header.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/xpu/elementwise.h" +namespace phi { + +template +void SubtractRawKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + int axis, + DenseTensor* out) { + using XPUType = typename XPUTypeTrait::Type; + phi::XPUElementwise( + dev_ctx, x, y, axis, out, xpu::broadcast_sub); +} + +} // namespace phi +PD_REGISTER_KERNEL(subtract_raw, + XPU, + ALL_LAYOUT, + phi::SubtractRawKernel, + float, + phi::dtype::float16) {} -- GitLab