/* 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/activation_op.h" #include #include "paddle/fluid/platform/xpu_header.h" namespace paddle { namespace operators { using paddle::framework::Tensor; template class XPUActivationKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &context) const override { Functor functor; auto attrs = functor.GetAttrs(); for (auto &attr : attrs) { *attr.second = context.Attr(attr.first); } functor(context); } }; template class XPUActivationGradKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &context) const override { Functor functor; auto attrs = functor.GetAttrs(); for (auto &attr : attrs) { *attr.second = context.Attr(attr.first); } functor(context); } }; template void xpu_activation_forward(const framework::ExecutionContext &ctx, xpu::Activation_t type) { const auto *x = ctx.Input("X"); auto *y = ctx.Output("Out"); const T *x_data = x->data(); T *y_data = y->mutable_data(ctx.GetPlace()); int r = 0; if (xpu::Activation_t::ACT_POW == type.type) { type.pow_factor = ctx.Attr("factor"); } auto xpu_context = ctx.device_context().x_context(); r = xpu::activation_forward(xpu_context, type, x->numel(), reinterpret_cast(x_data), reinterpret_cast(y_data)); PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, platform::errors::External( "XPU API return wrong value[%d], please check whether " "Baidu Kunlun Card is properly installed.", r)); } template void xpu_activation_backward(const framework::ExecutionContext &ctx, xpu::Activation_t type) { /* TODO: relu tanh sigmoid are inplace */ const auto *x = ctx.Input("X"); auto *y = ctx.Input("Out"); auto *dOut = ctx.Input(framework::GradVarName("Out")); auto *dX = ctx.Output(framework::GradVarName("X")); const T *x_data = nullptr; const T *y_data = nullptr; const T *y_grad = nullptr; if (x != nullptr) x_data = x->data(); if (y != nullptr) y_data = y->data(); if (dOut != nullptr) y_grad = dOut->data(); T *x_grad = dX->mutable_data(ctx.GetPlace()); auto xpu_context = ctx.device_context().x_context(); int r = xpu::activation_backward(xpu_context, type, dX->numel(), reinterpret_cast(x_data), reinterpret_cast(y_data), reinterpret_cast(y_grad), reinterpret_cast(x_grad)); PADDLE_ENFORCE_EQ(r, XPU_SUCCESS, platform::errors::External( "XPU API return wrong value[%d], please check whether " "Baidu Kunlun Card is properly installed.", r)); } template struct XPUActivationFunc : public BaseActivationFunctor { void operator()(const framework::ExecutionContext &ctx) const { xpu_activation_forward(ctx, algorithm); } }; template struct XPUActivationGradFunc : public BaseActivationFunctor { void operator()(const framework::ExecutionContext &ctx) const { xpu_activation_backward(ctx, algorithm); } }; template using XPUReluFunctor = XPUActivationFunc; template using XPUSigmoidFunctor = XPUActivationFunc; template using XPUTanhFunctor = XPUActivationFunc; template using XPUGeluFunctor = XPUActivationFunc; template using XPULogFunctor = XPUActivationFunc; template using XPUSquareFunctor = XPUActivationFunc; template using XPUSuareGradFunctor = XPUActivationGradFunc; template using XPUReluGradFunctor = XPUActivationGradFunc; template using XPUSigmoidGradFunctor = XPUActivationGradFunc; template using XPUTanhGradFunctor = XPUActivationGradFunc; template using XPUGeluGradFunctor = XPUActivationGradFunc; template using XPUSqrtFunctor = XPUActivationFunc; template using XPUSqrtGradFunctor = XPUActivationGradFunc; template using XPUACTPowFunctor = XPUActivationFunc; template using XPUABSFunctor = XPUActivationFunc; } // namespace operators } // namespace paddle namespace ops = paddle::operators; #define REGISTER_ACTIVATION_XPU_KERNEL(act_type, functor, grad_functor) \ REGISTER_OP_XPU_KERNEL(act_type, \ ops::XPUActivationKernel>); \ REGISTER_OP_XPU_KERNEL( \ act_type##_grad, \ ops::XPUActivationGradKernel>); REGISTER_ACTIVATION_XPU_KERNEL(relu, XPUReluFunctor, XPUReluGradFunctor) REGISTER_ACTIVATION_XPU_KERNEL(tanh, XPUTanhFunctor, XPUTanhGradFunctor) REGISTER_ACTIVATION_XPU_KERNEL(sigmoid, XPUSigmoidFunctor, XPUSigmoidGradFunctor) REGISTER_ACTIVATION_XPU_KERNEL(gelu, XPUGeluFunctor, XPUGeluGradFunctor) REGISTER_ACTIVATION_XPU_KERNEL(sqrt, XPUSqrtFunctor, XPUSqrtGradFunctor) REGISTER_ACTIVATION_XPU_KERNEL(square, XPUSquareFunctor, XPUSuareGradFunctor) REGISTER_OP_XPU_KERNEL(log, ops::XPUActivationKernel>); REGISTER_OP_XPU_KERNEL(pow, ops::XPUActivationKernel>); REGISTER_OP_XPU_KERNEL(abs, ops::XPUActivationKernel>); #endif // PADDLE_WITH_XPU