From 5ed2320c35b973bf0a52a5bb3971a15030cb51c9 Mon Sep 17 00:00:00 2001 From: Zhang Na <44800775+loongson-zn@users.noreply.github.com> Date: Mon, 20 Mar 2023 16:36:00 +0800 Subject: [PATCH] Add LoongArch support (#51109) --- CMakeLists.txt | 11 +++++++++++ cmake/external/openblas.cmake | 4 ++++ cmake/flags.cmake | 3 ++- paddle/fluid/operators/search_compute.h | 8 ++++---- paddle/fluid/platform/denormal.cc | 3 ++- paddle/phi/backends/cpu/cpu_info.cc | 5 +++-- paddle/phi/backends/cpu/cpu_info.h | 5 +++-- 7 files changed, 29 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e720e09072d..fe10d96261a 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -298,6 +298,7 @@ option(WITH_CRYPTO "Compile PaddlePaddle with crypto support" ON) option(WITH_ARM "Compile PaddlePaddle with arm support" OFF) option(WITH_SW "Compile PaddlePaddle with sw support" OFF) option(WITH_MIPS "Compile PaddlePaddle with mips support" OFF) +option(WITH_LOONGARCH "Compile PaddlePaddle with loongarch support" OFF) option(WITH_MUSL "Compile with musl libc instead of gblic" OFF) option(WITH_UNITY_BUILD "Compile with UnityBuild mode" OFF) option(WITH_STRIP "Strip so files of Whl packages" OFF) @@ -659,6 +660,16 @@ if(WITH_MIPS) add_definitions(-DPADDLE_WITH_MIPS) endif() +if(WITH_LOONGARCH) + set(WITH_XBYAK + OFF + CACHE STRING "Disable XBYAK when compiling WITH_LOONGARCH=ON" FORCE) + set(WITH_MKL + OFF + CACHE STRING "Disable MKL when compiling WITH_LOONGARCH=ON." FORCE) + add_definitions(-DPADDLE_WITH_LOONGARCH) +endif() + if(WITH_ONEMKL) add_definitions(-DPADDLE_WITH_ONEMKL) endif() diff --git a/cmake/external/openblas.cmake b/cmake/external/openblas.cmake index ceb7e17993a..01b9371da15 100644 --- a/cmake/external/openblas.cmake +++ b/cmake/external/openblas.cmake @@ -26,6 +26,10 @@ if(WITH_MIPS) set(CBLAS_TAG v0.3.13) endif() +if(WITH_LOONGARCH) + set(CBLAS_TAG v0.3.18) +endif() + if(NOT WIN32) set(CBLAS_LIBRARIES "${CBLAS_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}openblas${CMAKE_STATIC_LIBRARY_SUFFIX}" diff --git a/cmake/flags.cmake b/cmake/flags.cmake index 7dbad9621f9..b880c8028a4 100644 --- a/cmake/flags.cmake +++ b/cmake/flags.cmake @@ -196,7 +196,8 @@ if(NOT WIN32) if(NOT WITH_NV_JETSON AND NOT WITH_ARM AND NOT WITH_SW - AND NOT WITH_MIPS) + AND NOT WITH_MIPS + AND NOT WITH_LOONGARCH) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64") endif() endif() diff --git a/paddle/fluid/operators/search_compute.h b/paddle/fluid/operators/search_compute.h index e2156483320..abf83651824 100644 --- a/paddle/fluid/operators/search_compute.h +++ b/paddle/fluid/operators/search_compute.h @@ -15,7 +15,7 @@ limitations under the License. */ #pragma once #if !defined(PADDLE_WITH_ARM) && !defined(PADDLE_WITH_SW) && \ - !defined(PADDLE_WITH_MIPS) + !defined(PADDLE_WITH_MIPS) && !defined(PADDLE_WITH_LOONGARCH) #include #endif #include @@ -103,7 +103,7 @@ void call_gemm_batched(const framework::ExecutionContext& ctx, } #if !defined(PADDLE_WITH_ARM) && !defined(PADDLE_WITH_SW) && \ - !defined(PADDLE_WITH_MIPS) + !defined(PADDLE_WITH_MIPS) && !defined(PADDLE_WITH_LOONGARCH) #define __m256x __m256 @@ -144,7 +144,7 @@ inline void axpy(const T* x, T* y, size_t len, const T alpha) { _mm256_mul_px(mm_alpha, _mm256_load_px(x + jjj)))); } #elif defined(PADDLE_WITH_ARM) || defined(PADDLE_WITH_SW) || \ - defined(PADDLE_WITH_MIPS) + defined(PADDLE_WITH_MIPS) || defined(PADDLE_WITH_LOONGARCH) PADDLE_THROW(platform::errors::Unimplemented("axpy is not supported")); #else lll = len & ~SSE_CUT_LEN_MASK; @@ -174,7 +174,7 @@ inline void axpy_noadd(const T* x, T* y, size_t len, const T alpha) { _mm256_store_px(y + jjj, _mm256_mul_px(mm_alpha, _mm256_load_px(x + jjj))); } #elif defined(PADDLE_WITH_ARM) || defined(PADDLE_WITH_SW) || \ - defined(PADDLE_WITH_MIPS) + defined(PADDLE_WITH_MIPS) || defined(PADDLE_WITH_LOONGARCH) PADDLE_THROW(platform::errors::Unimplemented("axpy_noadd is not supported")); #else lll = len & ~SSE_CUT_LEN_MASK; diff --git a/paddle/fluid/platform/denormal.cc b/paddle/fluid/platform/denormal.cc index 4cfb0825443..eca106af3dd 100644 --- a/paddle/fluid/platform/denormal.cc +++ b/paddle/fluid/platform/denormal.cc @@ -29,7 +29,8 @@ #endif #if !defined(GCC_WITHOUT_INTRINSICS) && !defined(PADDLE_WITH_ARM) && \ - !defined(PADDLE_WITH_SW) && !defined(PADDLE_WITH_MIPS) && !defined(_WIN32) + !defined(PADDLE_WITH_SW) && !defined(PADDLE_WITH_MIPS) && \ + !defined(_WIN32) && !defined(PADDLE_WITH_LOONGARCH) #define DENORM_USE_INTRINSICS #endif diff --git a/paddle/phi/backends/cpu/cpu_info.cc b/paddle/phi/backends/cpu/cpu_info.cc index 74a8df11920..9d6b4e46df0 100644 --- a/paddle/phi/backends/cpu/cpu_info.cc +++ b/paddle/phi/backends/cpu/cpu_info.cc @@ -165,8 +165,9 @@ bool MayIUse(const cpu_isa_t cpu_isa) { if (cpu_isa == isa_any) { return true; } else { -#if !defined(WITH_NV_JETSON) && !defined(PADDLE_WITH_ARM) && \ - !defined(PADDLE_WITH_SW) && !defined(PADDLE_WITH_MIPS) +#if !defined(WITH_NV_JETSON) && !defined(PADDLE_WITH_ARM) && \ + !defined(PADDLE_WITH_SW) && !defined(PADDLE_WITH_MIPS) && \ + !defined(PADDLE_WITH_LOONGARCH) int reg[4]; cpuid(reg, 0); int nIds = reg[0]; diff --git a/paddle/phi/backends/cpu/cpu_info.h b/paddle/phi/backends/cpu/cpu_info.h index 92a159c9b0a..3bc8551cd95 100644 --- a/paddle/phi/backends/cpu/cpu_info.h +++ b/paddle/phi/backends/cpu/cpu_info.h @@ -40,8 +40,9 @@ #ifdef _WIN32 #define cpuid(reg, x) __cpuidex(reg, x, 0) #else -#if !defined(WITH_NV_JETSON) && !defined(PADDLE_WITH_ARM) && \ - !defined(PADDLE_WITH_SW) && !defined(PADDLE_WITH_MIPS) +#if !defined(WITH_NV_JETSON) && !defined(PADDLE_WITH_ARM) && \ + !defined(PADDLE_WITH_SW) && !defined(PADDLE_WITH_MIPS) && \ + !defined(PADDLE_WITH_LOONGARCH) #include inline void cpuid(int reg[4], int x) { __cpuid_count(x, 0, reg[0], reg[1], reg[2], reg[3]); -- GitLab