diff --git a/paddle/phi/kernels/xpu/activation_grad_kernel.cc b/paddle/phi/kernels/xpu/activation_grad_kernel.cc index 8aae57b1e498ba5f41240570773dcf3d472c5feb..62548434eb2870b1869106a3f18f85b3631f7bda 100644 --- a/paddle/phi/kernels/xpu/activation_grad_kernel.cc +++ b/paddle/phi/kernels/xpu/activation_grad_kernel.cc @@ -378,9 +378,15 @@ struct XPUSiluGradFunctor : public funcs::BaseActivationFunctor { const XPUType* y_grad = reinterpret_cast(dout->data()); XPUType* x_grad = reinterpret_cast(dx->data()); - int r = xpu::swish_grad( - dev_ctx.x_context(), x_data, y_grad, x_grad, dx->numel()); - PADDLE_ENFORCE_XDNN_SUCCESS(r, "swish_grad"); + if (std::getenv("XPU_PADDLE_ACT_LUT") != nullptr) { + int r = xpu::fast_swish_grad( + dev_ctx.x_context(), x_data, y_grad, x_grad, dx->numel()); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "fast_swish_grad"); + } else { + int r = xpu::swish_grad( + dev_ctx.x_context(), x_data, y_grad, x_grad, dx->numel()); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "swish_grad"); + } } }; diff --git a/paddle/phi/kernels/xpu/activation_kernel.cc b/paddle/phi/kernels/xpu/activation_kernel.cc index cc2c76c93a3873125b6dd965539566d59ea9ae44..1d1f33204bc5dc40f4b03f4a0ad5f84accb030e4 100644 --- a/paddle/phi/kernels/xpu/activation_kernel.cc +++ b/paddle/phi/kernels/xpu/activation_kernel.cc @@ -333,9 +333,15 @@ struct XPUSiluFunctor : public funcs::BaseActivationFunctor { XPUType* y_data = reinterpret_cast(out->data()); auto xpu_context = dev_ctx.x_context(); - int r = - xpu::swish(xpu_context, x_data, y_data, x.numel(), nullptr, nullptr); - PADDLE_ENFORCE_XDNN_SUCCESS(r, "swish"); + if (std::getenv("XPU_PADDLE_ACT_LUT") != nullptr) { + int r = xpu::fast_swish( + xpu_context, x_data, y_data, x.numel(), nullptr, nullptr); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "fast_swish"); + } else { + int r = + xpu::swish(xpu_context, x_data, y_data, x.numel(), nullptr, nullptr); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "swish"); + } } }; diff --git a/test/xpu/test_activation_op_xpu.py b/test/xpu/test_activation_op_xpu.py index a877b09bbc95728ad66da934a79b8ad77cac06ff..c0934cdded1255541931e92768b82515ace02804 100644 --- a/test/xpu/test_activation_op_xpu.py +++ b/test/xpu/test_activation_op_xpu.py @@ -17,6 +17,8 @@ import unittest sys.path.append('../../python/paddle/fluid/tests/unittests') +import os + import numpy as np from eager_op_test import OpTest from get_test_cover_info import ( @@ -105,16 +107,38 @@ class XPUTestSiluOP(XPUOpTestWrapper): self.outputs = {'Out': out} self.attrs = {'use_xpu': True} + def test_check_output(self): + self.set_env() + self.check_output_with_place(self.place) + self.delete_env() + def test_check_grad(self): + self.set_env() self.check_grad_with_place(self.place, ['X'], 'Out') + self.delete_env() def init_shape(self): self.shape = [11, 17] + def set_env(self): + pass + + def delete_env(self): + pass + class TestSilu_ZeroDim(XPUTestSilu): def init_shape(self): self.shape = [] + class TestSilu_LUT(XPUTestSilu): + def set_env(self): + # set "XPU_PADDLE_ACT_LUT" env to enable lut + os.environ['XPU_PADDLE_ACT_LUT'] = "1" + + def delete_env(self): + if os.getenv('XPU_PADDLE_ACT_LUT'): + del os.environ['XPU_PADDLE_ACT_LUT'] + class TestSiluAPI(unittest.TestCase): # test paddle.nn.Silu, paddle.nn.functional.silu