From 91bff45b98830f76bc11400465d4fb6a158aefd4 Mon Sep 17 00:00:00 2001 From: ebraraktas Date: Fri, 12 Jan 2024 12:05:06 +0300 Subject: [PATCH] build: add armv7 support --- CMakeLists.txt | 8 ++++---- src/cpu/cpu_info.cc | 2 +- src/cpu/cpu_info.h | 2 +- src/cpu/cpu_isa.cc | 6 +++--- src/cpu/cpu_isa.h | 4 ++-- src/cpu/vec_neon.h | 16 ++++++++++++++++ src/utils.cc | 2 +- 7 files changed, 28 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f4a23737..e384d57bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -228,10 +228,10 @@ macro(ct2_compile_kernels_for_isa isa flag) list(APPEND SOURCES ${CMAKE_CURRENT_BINARY_DIR}/kernels_${isa}.cc) endmacro() -if(CMAKE_SYSTEM_PROCESSOR MATCHES "(arm64)|(aarch64)" +if(CMAKE_SYSTEM_PROCESSOR MATCHES "(arm64)|(aarch64)|(armv7-a)" OR (APPLE AND CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")) - add_definitions(-DCT2_ARM64_BUILD) - set(CT2_BUILD_ARCH "arm64") + add_definitions(-DCT2_ARM_BUILD) + set(CT2_BUILD_ARCH "arm") elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(amd64)|(AMD64)") add_definitions(-DCT2_X86_BUILD) set(CT2_BUILD_ARCH "x86_64") @@ -260,7 +260,7 @@ if(ENABLE_CPU_DISPATCH) ct2_compile_kernels_for_isa(avx2 "-mavx2 -mfma") ct2_compile_kernels_for_isa(avx512 "-mavx512f -mavx512cd -mavx512vl -mavx512bw -mavx512dq") endif() - elseif(CT2_BUILD_ARCH STREQUAL "arm64") + elseif(CT2_BUILD_ARCH STREQUAL "arm") ct2_compile_kernels_for_isa(neon "-DUSE_NEON") endif() endif() diff --git a/src/cpu/cpu_info.cc b/src/cpu/cpu_info.cc index 9030ac7a4..f2e58085e 100644 --- a/src/cpu/cpu_info.cc +++ b/src/cpu/cpu_info.cc @@ -42,7 +42,7 @@ namespace ctranslate2 { } } -#elif defined(CT2_ARM64_BUILD) +#elif defined(CT2_ARM_BUILD) namespace ctranslate2 { namespace cpu { diff --git a/src/cpu/cpu_info.h b/src/cpu/cpu_info.h index c2951bcc0..0ece94ced 100644 --- a/src/cpu/cpu_info.h +++ b/src/cpu/cpu_info.h @@ -12,7 +12,7 @@ namespace ctranslate2 { bool cpu_supports_avx(); bool cpu_supports_avx2(); bool cpu_supports_avx512(); -#elif defined(CT2_ARM64_BUILD) +#elif defined(CT2_ARM_BUILD) bool cpu_supports_neon(); #endif diff --git a/src/cpu/cpu_isa.cc b/src/cpu/cpu_isa.cc index c16aeda22..c07268ce8 100644 --- a/src/cpu/cpu_isa.cc +++ b/src/cpu/cpu_isa.cc @@ -32,7 +32,7 @@ namespace ctranslate2 { return "AVX2"; case CpuIsa::AVX512: return "AVX512"; -#elif defined(CT2_ARM64_BUILD) +#elif defined(CT2_ARM_BUILD) case CpuIsa::NEON: return "NEON"; #endif @@ -51,7 +51,7 @@ namespace ctranslate2 { return try_isa(env_isa, CpuIsa::AVX2, cpu_supports_avx2()); if (env_isa == "AVX") return try_isa(env_isa, CpuIsa::AVX, cpu_supports_avx()); -#elif defined(CT2_ARM64_BUILD) +#elif defined(CT2_ARM_BUILD) if (env_isa == "NEON") return try_isa(env_isa, CpuIsa::NEON, cpu_supports_neon()); #endif @@ -68,7 +68,7 @@ namespace ctranslate2 { return CpuIsa::AVX2; if (cpu_supports_avx()) return CpuIsa::AVX; -# elif defined(CT2_ARM64_BUILD) +# elif defined(CT2_ARM_BUILD) if (cpu_supports_neon()) return CpuIsa::NEON; # endif diff --git a/src/cpu/cpu_isa.h b/src/cpu/cpu_isa.h index 4f42bdf26..85f8b16c3 100644 --- a/src/cpu/cpu_isa.h +++ b/src/cpu/cpu_isa.h @@ -11,7 +11,7 @@ namespace ctranslate2 { AVX, AVX2, AVX512, -#elif defined(CT2_ARM64_BUILD) +#elif defined(CT2_ARM_BUILD) NEON, #endif }; @@ -48,7 +48,7 @@ namespace ctranslate2 { CPU_ISA_CASE(cpu::CpuIsa::AVX, SINGLE_ARG(STMTS)) \ CPU_ISA_DEFAULT(cpu::CpuIsa::GENERIC, SINGLE_ARG(STMTS)) \ } -#elif defined(CT2_ARM64_BUILD) +#elif defined(CT2_ARM_BUILD) # define CPU_ISA_DISPATCH(STMTS) \ switch (cpu::get_cpu_isa()) { \ CPU_ISA_CASE(cpu::CpuIsa::NEON, SINGLE_ARG(STMTS)) \ diff --git a/src/cpu/vec_neon.h b/src/cpu/vec_neon.h index 4a407a5f1..fcc70353d 100644 --- a/src/cpu/vec_neon.h +++ b/src/cpu/vec_neon.h @@ -144,19 +144,35 @@ namespace ctranslate2 { } static inline value_type div(value_type a, value_type b) { +#ifdef __aarch64__ return vdivq_f32(a, b); +#else + return a / b; +#endif } static inline value_type mul_add(value_type a, value_type b, value_type c) { +#ifdef __aarch64__ return vfmaq_f32(c, a, b); +#else + return a * b + c; +#endif } static inline float reduce_add(value_type a) { +#ifdef __aarch64__ return vaddvq_f32(a); +#else + return a[0] + a[1] + a[2] + a[3]; +#endif } static inline float reduce_max(value_type a) { +#ifdef __aarch64__ return vmaxvq_f32(a); +#else + return std::max({a[0], a[1], a[2], a[3]}); +#endif } static inline value_type round(value_type v) { diff --git a/src/utils.cc b/src/utils.cc index 4f8bde57c..80b564c15 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -38,7 +38,7 @@ namespace ctranslate2 { cpu::cpu_supports_avx(), cpu::cpu_supports_avx2(), cpu::cpu_supports_avx512()); -#elif defined(CT2_ARM64_BUILD) +#elif defined(CT2_ARM_BUILD) spdlog::info("CPU: {} (NEON={})", cpu::cpu_vendor(), cpu::cpu_supports_neon());