From a4d4c1161745935dc341d3a2b3ce66fc624ca7ce Mon Sep 17 00:00:00 2001 From: houj04 <35131887+houj04@users.noreply.github.com> Date: Wed, 13 Apr 2022 10:09:51 +0800 Subject: [PATCH] Update sign op xpu (#41685) * update sign op on xpu. test=kunlun * fix typo. test=kunlun --- cmake/external/xpu.cmake | 46 +++++++---- paddle/fluid/operators/sign_op_xpu.cc | 8 +- .../fluid/platform/device/xpu/xpu2_op_list.h | 1 + .../tests/unittests/xpu/test_sign_op_xpu.py | 82 +++++++++++++------ 4 files changed, 93 insertions(+), 44 deletions(-) diff --git a/cmake/external/xpu.cmake b/cmake/external/xpu.cmake index 90cb686700e..d453e9d2a2a 100644 --- a/cmake/external/xpu.cmake +++ b/cmake/external/xpu.cmake @@ -7,46 +7,62 @@ SET(XPU_PROJECT "extern_xpu") SET(XPU_API_LIB_NAME "libxpuapi.so") SET(XPU_RT_LIB_NAME "libxpurt.so") +if(NOT DEFINED XPU_BASE_URL) + SET(XPU_BASE_URL_WITHOUT_DATE "https://baidu-kunlun-product.cdn.bcebos.com/KL-SDK/klsdk-dev") + SET(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220411") +else() + SET(XPU_BASE_URL "${XPU_BASE_URL}") +endif() + +# ubuntu and centos: use output by XDNN API team +if(NOT DEFINED XPU_XDNN_BASE_URL) + SET(XPU_XDNN_BASE_URL_WITHOUT_DATE "https://klx-sdk-release-public.su.bcebos.com/xdnn/dev") + SET(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL_WITHOUT_DATE}/20220412") +else() + SET(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL}") +endif() + IF(WITH_AARCH64) SET(XPU_XRE_DIR_NAME "xre-kylin_aarch64") SET(XPU_XDNN_DIR_NAME "xdnn-kylin_aarch64") SET(XPU_XCCL_DIR_NAME "xccl-kylin_aarch64") + SET(XPU_XDNN_URL "${XPU_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz" CACHE STRING "" FORCE) ELSEIF(WITH_SUNWAY) SET(XPU_XRE_DIR_NAME "xre-deepin_sw6_64") SET(XPU_XDNN_DIR_NAME "xdnn-deepin_sw6_64") SET(XPU_XCCL_DIR_NAME "xccl-deepin_sw6_64") + SET(XPU_XDNN_URL "${XPU_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz" CACHE STRING "" FORCE) ELSEIF(WITH_BDCENTOS) SET(XPU_XRE_DIR_NAME "xre-bdcentos_x86_64") - SET(XPU_XDNN_DIR_NAME "xdnn-bdcentos_x86_64") + SET(XPU_XDNN_DIR_NAME "XDNN-bdcentos_x86_64") SET(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") + # ubuntu and centos: use output by XDNN API team + SET(XPU_XDNN_URL "${XPU_XDNN_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz" CACHE STRING "" FORCE) ELSEIF(WITH_UBUNTU) SET(XPU_XRE_DIR_NAME "xre-ubuntu_x86_64") - SET(XPU_XDNN_DIR_NAME "xdnn-ubuntu_x86_64") + SET(XPU_XDNN_DIR_NAME "XDNN-ubuntu_x86_64") SET(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") + # ubuntu and centos: use output by XDNN API team + SET(XPU_XDNN_URL "${XPU_XDNN_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz" CACHE STRING "" FORCE) ELSEIF(WITH_CENTOS) SET(XPU_XRE_DIR_NAME "xre-centos7_x86_64") - SET(XPU_XDNN_DIR_NAME "xdnn-centos7_x86_64") + SET(XPU_XDNN_DIR_NAME "XDNN-bdcentos_x86_64") SET(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") - -ELSE () + # ubuntu and centos: use output by XDNN API team + SET(XPU_XDNN_URL "${XPU_XDNN_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz" CACHE STRING "" FORCE) +ELSE() SET(XPU_XRE_DIR_NAME "xre-ubuntu_x86_64") - SET(XPU_XDNN_DIR_NAME "xdnn-ubuntu_x86_64") + SET(XPU_XDNN_DIR_NAME "XDNN-bdcentos_x86_64") SET(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") + # default: use output by XDNN API team + SET(XPU_XDNN_URL "${XPU_XDNN_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz" CACHE STRING "" FORCE) ENDIF() -if(NOT DEFINED XPU_BASE_URL) - SET(XPU_BASE_URL_WITHOUT_DATE "https://baidu-kunlun-product.cdn.bcebos.com/KL-SDK/klsdk-dev") - SET(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220411") -else() - SET(XPU_BASE_URL "${XPU_BASE_URL}") -endif() - SET(XPU_XRE_URL "${XPU_BASE_URL}/${XPU_XRE_DIR_NAME}.tar.gz" CACHE STRING "" FORCE) -SET(XPU_XDNN_URL "${XPU_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz" CACHE STRING "" FORCE) SET(XPU_XCCL_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220411/${XPU_XCCL_DIR_NAME}.tar.gz" CACHE STRING "" FORCE) SET(XPU_PACK_DEPENCE_URL "https://baidu-kunlun-public.su.bcebos.com/paddle_depence/pack_paddle_depence.sh" CACHE STRING "" FORCE) -SET(SNAPPY_PREFIX_DIR "${THIRD_PARTY_PATH}/xpu") +SET(SNAPPY_PREFIX_DIR "${THIRD_PARTY_PATH}/xpu") SET(XPU_DOWNLOAD_DIR "${SNAPPY_PREFIX_DIR}/src/${XPU_PROJECT}") SET(XPU_INSTALL_DIR "${THIRD_PARTY_PATH}/install/xpu") SET(XPU_INC_DIR "${THIRD_PARTY_PATH}/install/xpu/include") diff --git a/paddle/fluid/operators/sign_op_xpu.cc b/paddle/fluid/operators/sign_op_xpu.cc index 22934cf4821..a00aa4bb7ce 100644 --- a/paddle/fluid/operators/sign_op_xpu.cc +++ b/paddle/fluid/operators/sign_op_xpu.cc @@ -14,6 +14,7 @@ limitations under the License. */ #ifdef PADDLE_WITH_XPU #include "paddle/fluid/framework/op_registry.h" +#include "paddle/fluid/platform/device/device_wrapper.h" #include "paddle/fluid/platform/device/xpu/xpu_header.h" namespace paddle { namespace operators { @@ -26,10 +27,9 @@ class SignXPUKernel : public framework::OpKernel { auto* in = context.Input("X"); out->mutable_data(in->place()); auto xpu_context = context.device_context().x_context(); - int r = xpu::activation_forward(xpu_context, xpu::Activation_t::SIGN, - in->numel(), in->data(), out->data()); - PADDLE_ENFORCE_EQ(r, xpu::Error_t::SUCCESS, - platform::errors::Fatal("XPU sign kernel error!")); + // int sign(Context* ctx, const T* x , T* y, int len); + int r = xpu::sign(xpu_context, in->data(), out->data(), in->numel()); + PADDLE_ENFORCE_XDNN_SUCCESS(r, "sign"); } }; diff --git a/paddle/fluid/platform/device/xpu/xpu2_op_list.h b/paddle/fluid/platform/device/xpu/xpu2_op_list.h index 08a7f080069..65273710598 100644 --- a/paddle/fluid/platform/device/xpu/xpu2_op_list.h +++ b/paddle/fluid/platform/device/xpu/xpu2_op_list.h @@ -316,6 +316,7 @@ XPUOpMap& get_kl2_ops() { {"sigmoid", XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace())})}, {"sigmoid_grad", XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace())})}, + {"sign", XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace())})}, {"slice_grad", XPUKernelSet({pOpKernelType(vartype::FP32, XPUPlace()), pOpKernelType(vartype::FP16, XPUPlace()), pOpKernelType(vartype::INT32, XPUPlace())})}, diff --git a/python/paddle/fluid/tests/unittests/xpu/test_sign_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_sign_op_xpu.py index ab07221a070..9254a84ec42 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_sign_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_sign_op_xpu.py @@ -18,37 +18,69 @@ import unittest import numpy as np import sys sys.path.append("..") -from op_test import OpTest -import paddle -import paddle.fluid as fluid -from paddle.fluid import Program, program_guard + import paddle +from op_test import OpTest +from op_test_xpu import XPUOpTest +from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper + paddle.enable_static() -@unittest.skipIf(not paddle.is_compiled_with_xpu(), - "core is not compiled with XPU") -class TestXPUSignOp(OpTest): - def setUp(self): - self.op_type = "sign" - self.dtype = np.float32 - self.inputs = { - 'X': np.random.uniform(-10, 10, (10, 10)).astype(self.dtype) - } - self.outputs = {'Out': np.sign(self.inputs['X'])} - self.attrs = {'use_xpu': True} - - def test_check_output(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_output_with_place(place) - - def test_check_grad(self): - if paddle.is_compiled_with_xpu(): - place = paddle.XPUPlace(0) - self.check_grad_with_place(place, ['X'], 'Out') +class XPUTestSignOP(XPUOpTestWrapper): + def __init__(self): + self.op_name = 'sign' + self.use_dynamic_create_class = False + + class TestSignOPBase(XPUOpTest): + def setUp(self): + self.place = paddle.XPUPlace(0) + self.init_dtype() + self.set_case() + + def set_case(self): + self.op_type = 'sign' + self.dtype = self.in_type + self.init_config() + self.x = np.random.uniform(-10, 10, + self.input_shape).astype(self.dtype) + self.inputs = {'X': self.x} + self.outputs = {'Out': np.sign(self.x)} + self.attrs = {'use_xpu': True} + + def init_dtype(self): + self.dtype = np.float32 + + def test_check_output(self): + self.check_output_with_place(self.place) + + def test_check_grad(self): + self.check_grad_with_place(self.place, ['X'], 'Out') + + def init_config(self): + self.input_shape = [864] + + class XPUTestSign1(TestSignOPBase): + def init_config(self): + self.input_shape = [2, 768] + + class XPUTestSign2(TestSignOPBase): + def init_config(self): + self.input_shape = [3, 8, 4096] + + class XPUTestSign3(TestSignOPBase): + def init_config(self): + self.input_shape = [1024] + + class XPUTestSign4(TestSignOPBase): + def init_config(self): + self.input_shape = [2, 2, 255] + +support_types = get_xpu_op_support_types('sign') +for stype in support_types: + create_test_class(globals(), XPUTestSignOP, stype) if __name__ == "__main__": unittest.main() -- GitLab