未验证 提交 4d7a9eef 编写于 作者: Y YuanRisheng 提交者: GitHub

[Phi]Move grad_add op kernel into phi and delete elementwise_add_op file (#42903)

* move grad_add

* fix unittest bugs

* fix compile bugs
上级 9e5acc1f
...@@ -15,9 +15,11 @@ limitations under the License. */ ...@@ -15,9 +15,11 @@ limitations under the License. */
#pragma once #pragma once
#include <vector> #include <vector>
#include "dgc/dgc.h" #include "dgc/dgc.h"
#include "paddle/fluid/framework/eigen.h" #include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/memory/malloc.h" #include "paddle/fluid/memory/malloc.h"
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h" #include "paddle/fluid/operators/elementwise/elementwise_op_function.h"
#include "paddle/phi/kernels/funcs/elementwise_functor.h"
namespace paddle { namespace paddle {
namespace operators { namespace operators {
...@@ -153,18 +155,18 @@ class DGCOpKernel : public framework::OpKernel<T> { ...@@ -153,18 +155,18 @@ class DGCOpKernel : public framework::OpKernel<T> {
u_out_e.device(eigen_ctx) = m * (u_e + grad_out_e); u_out_e.device(eigen_ctx) = m * (u_e + grad_out_e);
// v = u + v + g // v = u + v + g
ElementwiseComputeEx<AddFunctor<T>, DeviceContext, T>( ElementwiseComputeEx<phi::funcs::AddFunctor<T>, DeviceContext, T>(
ctx, u, v, 0, AddFunctor<T>(), v_out); ctx, u, v, 0, phi::funcs::AddFunctor<T>(), v_out);
ElementwiseComputeEx<AddFunctor<T>, DeviceContext, T>( ElementwiseComputeEx<phi::funcs::AddFunctor<T>, DeviceContext, T>(
ctx, g, v, 0, AddFunctor<T>(), v_out); ctx, g, v, 0, phi::funcs::AddFunctor<T>(), v_out);
} else { } else {
// u = m * u + g // u = m * u + g
u_out_e.device(eigen_ctx) = m * u_e + grad_out_e; u_out_e.device(eigen_ctx) = m * u_e + grad_out_e;
// v = u + v // v = u + v
ElementwiseComputeEx<AddFunctor<T>, DeviceContext, T>( ElementwiseComputeEx<phi::funcs::AddFunctor<T>, DeviceContext, T>(
ctx, u, v, 0, AddFunctor<T>(), v_out); ctx, u, v, 0, phi::funcs::AddFunctor<T>(), v_out);
} }
T* v_out_data = v_out->mutable_data<T>(ctx.GetPlace()); T* v_out_data = v_out->mutable_data<T>(ctx.GetPlace());
......
...@@ -12,8 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -12,8 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#include <string> #include <string>
#include "paddle/fluid/operators/elementwise/elementwise_op.h" #include "paddle/fluid/operators/elementwise/elementwise_op.h"
...@@ -125,17 +123,6 @@ REGISTER_OPERATOR( ...@@ -125,17 +123,6 @@ REGISTER_OPERATOR(
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>, paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>); paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OP_CPU_KERNEL(
grad_add,
ops::ElementwiseAddKernel<paddle::platform::CPUDeviceContext, float>,
ops::ElementwiseAddKernel<paddle::platform::CPUDeviceContext, double>,
ops::ElementwiseAddKernel<paddle::platform::CPUDeviceContext, int>,
ops::ElementwiseAddKernel<paddle::platform::CPUDeviceContext, int64_t>,
ops::ElementwiseAddKernel<paddle::platform::CPUDeviceContext,
paddle::platform::complex<float>>,
ops::ElementwiseAddKernel<paddle::platform::CPUDeviceContext,
paddle::platform::complex<double>>);
REGISTER_OP_VERSION(elementwise_add) REGISTER_OP_VERSION(elementwise_add)
.AddCheckpoint( .AddCheckpoint(
R"ROC(Register elementwise_add for adding the attribute of R"ROC(Register elementwise_add for adding the attribute of
......
/* Copyright (c) 2016 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
#ifdef __xpu__
#include <memory>
#include <string>
#include "paddle/fluid/operators/elementwise/elementwise_op.h"
#include "paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h"
#include "paddle/fluid/operators/elementwise/elementwise_xpu.h"
#include "paddle/fluid/platform/device/device_wrapper.h"
#else
#include <algorithm>
#include <utility>
#include "paddle/fluid/operators/elementwise/elementwise_op.h"
// only can include the headers in paddle/phi/include dirs
#include "paddle/phi/kernels/elementwise_add_grad_kernel.h"
#include "paddle/phi/kernels/elementwise_add_kernel.h"
#endif
namespace paddle {
namespace operators {
template <typename DeviceContext, typename T>
class ElementwiseAddKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
#ifdef __xpu__
std::vector<const framework::Tensor*> ins;
std::vector<framework::Tensor*> outs;
int axis = PackTensorsIntoVector<T>(ctx, &ins, &outs);
const auto& xpu_ctx =
ctx.template device_context<paddle::platform::XPUDeviceContext>();
paddle::operators::LaunchElementwiseCudaKernel<ElementwiseType::kBinary, T,
T, kps::AddFunctor<T>, 1>(
xpu_ctx, ins, &outs, axis, kps::AddFunctor<T>());
#else
auto *x = ctx.Input<framework::LoDTensor>("X");
auto *y = ctx.Input<framework::LoDTensor>("Y");
auto *z = ctx.Output<framework::LoDTensor>("Out");
z->mutable_data<T>(ctx.GetPlace());
auto &dev_ctx = ctx.device_context<DeviceContext>();
int axis = ctx.Attr<int>("axis");
phi::AddRawKernel<T>(
static_cast<const typename framework::ConvertToPhiContext<
DeviceContext>::TYPE &>(dev_ctx),
*x, *y, axis, z);
#endif
}
};
} // namespace operators
} // namespace paddle
/* 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_KP
// Please do not modify the following code
#if defined(__CUDA_ARCH__)
#undef __CUDA_ARCH__
#endif
#if defined(__CUDACC__)
#undef __CUDACC__
#endif
#if defined(__CUDA__)
#undef __CUDA__
#endif
#if defined(__NVCC__)
#undef __NVCC__
#endif
#include <xpu/runtime.h> // NOLINT
#include "xpu/kernel/cluster_header.h" // NOLINT
#include "xpu/kernel/debug.h" // NOLINT
#include "xpu/kernel/math.h" // NOLINT
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#else
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#include "paddle/phi/kernels/gpu/elementwise_grad.h"
#endif
namespace ops = paddle::operators;
namespace plat = paddle::platform;
#ifdef PADDLE_WITH_XPU_KP
REGISTER_OP_KERNEL(elementwise_add, KP, plat::XPUPlace,
ops::ElementwiseAddKernel<plat::XPUDeviceContext, float>);
#else
REGISTER_OP_CUDA_KERNEL(
grad_add, ops::ElementwiseAddKernel<plat::CUDADeviceContext, float>,
ops::ElementwiseAddKernel<plat::CUDADeviceContext, double>,
ops::ElementwiseAddKernel<plat::CUDADeviceContext, int>,
ops::ElementwiseAddKernel<plat::CUDADeviceContext, int64_t>,
ops::ElementwiseAddKernel<plat::CUDADeviceContext, plat::float16>,
ops::ElementwiseAddKernel<plat::CUDADeviceContext, plat::bfloat16>,
ops::ElementwiseAddKernel<plat::CUDADeviceContext, plat::complex<float>>,
ops::ElementwiseAddKernel<plat::CUDADeviceContext, plat::complex<double>>);
#endif
...@@ -16,7 +16,6 @@ limitations under the License. */ ...@@ -16,7 +16,6 @@ limitations under the License. */
#include <string> #include <string>
#include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#include "paddle/fluid/operators/elementwise/elementwise_npu.h" #include "paddle/fluid/operators/elementwise/elementwise_npu.h"
#include "paddle/fluid/platform/device/npu/npu_op_runner.h" #include "paddle/fluid/platform/device/npu/npu_op_runner.h"
......
...@@ -13,7 +13,6 @@ See the License for the specific language governing permissions and ...@@ -13,7 +13,6 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#ifdef PADDLE_WITH_XPU #ifdef PADDLE_WITH_XPU
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#include <memory> #include <memory>
#include <string> #include <string>
#include "paddle/fluid/operators/elementwise/elementwise_op.h" #include "paddle/fluid/operators/elementwise/elementwise_op.h"
......
...@@ -14,9 +14,10 @@ limitations under the License. */ ...@@ -14,9 +14,10 @@ limitations under the License. */
#include "paddle/fluid/platform/float16.h" #include "paddle/fluid/platform/float16.h"
#include "paddle/phi/kernels/funcs/blas/blas.h" #include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h"
#include "paddle/fluid/operators/kernel_primitives/kernel_primitives.h" #include "paddle/fluid/operators/kernel_primitives/kernel_primitives.h"
#include "paddle/fluid/operators/reduce_ops/reduce_op.cu.h" #include "paddle/fluid/operators/reduce_ops/reduce_op.cu.h"
#include "paddle/phi/kernels/funcs/broadcast_function.h"
#include "paddle/phi/kernels/funcs/elementwise_functor.h"
namespace paddle { namespace paddle {
namespace operators { namespace operators {
...@@ -67,9 +68,8 @@ class AttnMatMul { ...@@ -67,9 +68,8 @@ class AttnMatMul {
ins.emplace_back(bias); ins.emplace_back(bias);
outs.emplace_back(bias_out); outs.emplace_back(bias_out);
int elewise_add_axis = -1; int elewise_add_axis = -1;
paddle::operators::LaunchElementwiseCudaKernel<ElementwiseType::kBinary, phi::funcs::BroadcastKernel<phi::ElementwiseType::kBinary, T, T>(
T, T>( dev_ctx_, ins, &outs, elewise_add_axis, phi::funcs::AddFunctor<T>());
dev_ctx_, ins, &outs, elewise_add_axis, AddFunctor<T>());
} }
} }
......
...@@ -12,12 +12,12 @@ limitations under the License. */ ...@@ -12,12 +12,12 @@ limitations under the License. */
#pragma once #pragma once
#include "paddle/fluid/operators/dropout_impl.cu.h" #include "paddle/fluid/operators/dropout_impl.cu.h"
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#include "paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h"
#include "paddle/fluid/operators/fused/fused_softmax_mask.cu.h" #include "paddle/fluid/operators/fused/fused_softmax_mask.cu.h"
#include "paddle/fluid/operators/transpose_op.cu.h" #include "paddle/fluid/operators/transpose_op.cu.h"
#include "paddle/phi/kernels/funcs/broadcast_function.h"
#include "paddle/phi/kernels/funcs/concat_and_split_functor.h" #include "paddle/phi/kernels/funcs/concat_and_split_functor.h"
#include "paddle/phi/kernels/funcs/elementwise_base.h" #include "paddle/phi/kernels/funcs/elementwise_base.h"
#include "paddle/phi/kernels/funcs/elementwise_functor.h"
#include "paddle/phi/kernels/funcs/functors.h" #include "paddle/phi/kernels/funcs/functors.h"
#include "paddle/phi/kernels/gpudnn/softmax_gpudnn.h" #include "paddle/phi/kernels/gpudnn/softmax_gpudnn.h"
...@@ -160,9 +160,9 @@ class FMHARef { ...@@ -160,9 +160,9 @@ class FMHARef {
ins.emplace_back(src_mask_tensor); ins.emplace_back(src_mask_tensor);
outs.emplace_back(src_mask_out_tensor); outs.emplace_back(src_mask_out_tensor);
int elewise_add_axis = -1; int elewise_add_axis = -1;
paddle::operators::LaunchElementwiseCudaKernel<ElementwiseType::kBinary, phi::funcs::BroadcastKernel<phi::ElementwiseType::kBinary, T, T>(
T, T>( dev_ctx_, ins, &outs, elewise_add_axis,
dev_ctx_, ins, &outs, elewise_add_axis, AddFunctor<T>()); phi::funcs::AddFunctor<T>());
phi::SoftmaxForwardCUDAKernelDriver<T>( phi::SoftmaxForwardCUDAKernelDriver<T>(
dev_ctx_, *src_mask_out_tensor, softmax_axis, softmax_out_tensor); dev_ctx_, *src_mask_out_tensor, softmax_axis, softmax_out_tensor);
......
...@@ -19,7 +19,8 @@ limitations under the License. */ ...@@ -19,7 +19,8 @@ limitations under the License. */
#include "paddle/fluid/platform/device/gpu/gpu_device_function.h" #include "paddle/fluid/platform/device/gpu/gpu_device_function.h"
#include "paddle/fluid/platform/device/gpu/gpu_dnn.h" #include "paddle/fluid/platform/device/gpu/gpu_dnn.h"
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h" #include "paddle/phi/kernels/funcs/broadcast_function.h"
#include "paddle/phi/kernels/funcs/elementwise_functor.h"
#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/fluid/operators/fused/attention_layer_norm.h" #include "paddle/fluid/operators/fused/attention_layer_norm.h"
...@@ -543,10 +544,9 @@ class FusedAttentionGradKernel : public framework::OpKernel<T> { ...@@ -543,10 +544,9 @@ class FusedAttentionGradKernel : public framework::OpKernel<T> {
ins.emplace_back(d_x); ins.emplace_back(d_x);
outs.emplace_back(d_x); outs.emplace_back(d_x);
int elewise_add_axis = -1; int elewise_add_axis = -1;
paddle::operators::LaunchElementwiseCudaKernel<ElementwiseType::kBinary, T, phi::funcs::BroadcastKernel<phi::ElementwiseType::kBinary, T, T>(
T>(
ctx.cuda_device_context(), ins, &outs, elewise_add_axis, ctx.cuda_device_context(), ins, &outs, elewise_add_axis,
AddFunctor<T>()); phi::funcs::AddFunctor<T>());
} }
}; };
......
...@@ -17,9 +17,10 @@ limitations under the License. */ ...@@ -17,9 +17,10 @@ limitations under the License. */
#include "paddle/fluid/operators/matmul_v2_op.h" #include "paddle/fluid/operators/matmul_v2_op.h"
#include "paddle/phi/kernels/funcs/blas/blas.h" #include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#include "paddle/fluid/operators/fused/fused_dropout_helper.h" #include "paddle/fluid/operators/fused/fused_dropout_helper.h"
#include "paddle/fluid/operators/layer_norm_kernel.cu.h" #include "paddle/fluid/operators/layer_norm_kernel.cu.h"
#include "paddle/phi/kernels/funcs/broadcast_function.h"
#include "paddle/phi/kernels/funcs/elementwise_functor.h"
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) #if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
#include "paddle/fluid/platform/collective_helper.h" #include "paddle/fluid/platform/collective_helper.h"
...@@ -345,9 +346,8 @@ class FusedFeedForwardGradKernel : public framework::OpKernel<T> { ...@@ -345,9 +346,8 @@ class FusedFeedForwardGradKernel : public framework::OpKernel<T> {
ins[1] = d_x; ins[1] = d_x;
outs[0] = d_x; outs[0] = d_x;
int elewise_add_axis = -1; int elewise_add_axis = -1;
paddle::operators::LaunchElementwiseCudaKernel<ElementwiseType::kBinary, T, phi::funcs::BroadcastKernel<phi::ElementwiseType::kBinary, T, T>(
T>( ctx, ins, &outs, elewise_add_axis, phi::funcs::AddFunctor<T>());
ctx, ins, &outs, elewise_add_axis, AddFunctor<T>());
} }
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
......
...@@ -24,7 +24,6 @@ limitations under the License. */ ...@@ -24,7 +24,6 @@ limitations under the License. */
#include "paddle/fluid/platform/device/gpu/gpu_device_function.h" #include "paddle/fluid/platform/device/gpu/gpu_device_function.h"
#include "paddle/fluid/platform/device/gpu/gpu_dnn.h" #include "paddle/fluid/platform/device/gpu/gpu_dnn.h"
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/fluid/operators/fused/attention_layer_norm.h" #include "paddle/fluid/operators/fused/attention_layer_norm.h"
......
...@@ -34,6 +34,14 @@ void AddKernel(const Context& dev_ctx, ...@@ -34,6 +34,14 @@ void AddKernel(const Context& dev_ctx,
AddRawKernel<T>(dev_ctx, x, y, axis, out); AddRawKernel<T>(dev_ctx, x, y, axis, out);
} }
template <typename T, typename Context>
void GradAddKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& y,
DenseTensor* out) {
AddKernel<T>(dev_ctx, x, y, out);
}
} // namespace phi } // namespace phi
using complex64 = ::phi::dtype::complex<float>; using complex64 = ::phi::dtype::complex<float>;
...@@ -65,3 +73,15 @@ PD_REGISTER_KERNEL(add, ...@@ -65,3 +73,15 @@ PD_REGISTER_KERNEL(add,
int64_t, int64_t,
complex64, complex64,
complex128) {} complex128) {}
PD_REGISTER_KERNEL(grad_add,
CPU,
ALL_LAYOUT,
phi::GradAddKernel,
float,
double,
int16_t,
int,
int64_t,
complex64,
complex128) {}
...@@ -33,6 +33,14 @@ void AddKernel(const Context& dev_ctx, ...@@ -33,6 +33,14 @@ void AddKernel(const Context& dev_ctx,
AddRawKernel<T>(dev_ctx, x, y, axis, out); AddRawKernel<T>(dev_ctx, x, y, axis, out);
} }
template <typename T, typename Context>
void GradAddKernel(const Context& dev_ctx,
const DenseTensor& x,
const DenseTensor& y,
DenseTensor* out) {
AddKernel<T>(dev_ctx, x, y, out);
}
} // namespace phi } // namespace phi
#ifdef PADDLE_WITH_XPU_KP #ifdef PADDLE_WITH_XPU_KP
...@@ -71,4 +79,18 @@ PD_REGISTER_KERNEL(add, ...@@ -71,4 +79,18 @@ PD_REGISTER_KERNEL(add,
phi::dtype::bfloat16, phi::dtype::bfloat16,
complex64, complex64,
complex128) {} complex128) {}
PD_REGISTER_KERNEL(grad_add,
KPS,
ALL_LAYOUT,
phi::GradAddKernel,
float,
double,
int16_t,
int,
int64_t,
phi::dtype::float16,
phi::dtype::bfloat16,
complex64,
complex128) {}
#endif #endif
...@@ -25,6 +25,11 @@ KernelSignature ElementwiseAddOpArgumentMapping( ...@@ -25,6 +25,11 @@ KernelSignature ElementwiseAddOpArgumentMapping(
return KernelSignature("add_raw", {"X", "Y"}, {"axis"}, {"Out"}); return KernelSignature("add_raw", {"X", "Y"}, {"axis"}, {"Out"});
} }
KernelSignature ElementwiseGradAddOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature("grad_add", {"X", "Y"}, {}, {"Out"});
}
KernelSignature ElementwiseSubOpArgumentMapping( KernelSignature ElementwiseSubOpArgumentMapping(
const ArgumentMappingContext& ctx) { const ArgumentMappingContext& ctx) {
int axis = paddle::any_cast<int>(ctx.Attr("axis")); int axis = paddle::any_cast<int>(ctx.Attr("axis"));
...@@ -317,3 +322,4 @@ PD_REGISTER_ARG_MAPPING_FN(elementwise_heaviside_grad, ...@@ -317,3 +322,4 @@ PD_REGISTER_ARG_MAPPING_FN(elementwise_heaviside_grad,
phi::ElementwiseHeavisideGradOpArgumentMapping); phi::ElementwiseHeavisideGradOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(elementwise_pow_grad, PD_REGISTER_ARG_MAPPING_FN(elementwise_pow_grad,
phi::ElementwisePowGradOpArgumentMapping); phi::ElementwisePowGradOpArgumentMapping);
PD_REGISTER_ARG_MAPPING_FN(grad_add, phi::ElementwiseGradAddOpArgumentMapping);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册