From d38d9f0877a5872daa3c5f06fb9a86376bf15d50 Mon Sep 17 00:00:00 2001 From: Aaron Teo Date: Sun, 2 Nov 2025 08:48:23 +0800 Subject: [PATCH] ggml: add s390x cpu-feats (#16774) --- .github/workflows/release.yml | 4 +- ggml/src/CMakeLists.txt | 9 ++-- ggml/src/ggml-cpu/CMakeLists.txt | 13 ++++-- ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp | 50 +++++++++++++++++++++++ 4 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cab3ba9e68..e72caa423b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -134,8 +134,8 @@ jobs: include: - build: 'x64' os: ubuntu-22.04 - - build: 's390x-z15' # z15 because our CI runners are on z15 - os: ubuntu-22.04-s390x + - build: 's390x' + os: ubuntu-24.04-s390x # GGML_BACKEND_DL and GGML_CPU_ALL_VARIANTS are not currently supported on arm # - build: 'arm64' # os: ubuntu-22.04-arm diff --git a/ggml/src/CMakeLists.txt b/ggml/src/CMakeLists.txt index ba281b8e6d..f30e4ac902 100644 --- a/ggml/src/CMakeLists.txt +++ b/ggml/src/CMakeLists.txt @@ -308,6 +308,10 @@ function(ggml_add_cpu_backend_variant tag_name) set(GGML_INTERNAL_${feat} ON) endforeach() elseif (GGML_SYSTEM_ARCH STREQUAL "s390x") + foreach (feat VXE2 NNPA) + set(GGML_INTERNAL_${feat} OFF) + endforeach() + foreach (feat ${ARGN}) set(GGML_INTERNAL_${feat} ON) endforeach() @@ -377,9 +381,8 @@ if (GGML_CPU_ALL_VARIANTS) endif() elseif (GGML_SYSTEM_ARCH STREQUAL "s390x") if (CMAKE_SYSTEM_NAME MATCHES "Linux") - ggml_add_cpu_backend_variant(s390x_z15 Z15 VXE) - # ggml_add_cpu_backend_variant(s390x_z16 Z16 VXE) - # ggml_add_cpu_backend_variant(s390x_z17 Z17 VXE) + ggml_add_cpu_backend_variant(z15 Z15 VXE2) + ggml_add_cpu_backend_variant(z16 Z16 VXE2 NNPA) else() message(FATAL_ERROR "Unsupported s390x target OS: ${CMAKE_SYSTEM_NAME}") endif() diff --git a/ggml/src/ggml-cpu/CMakeLists.txt b/ggml/src/ggml-cpu/CMakeLists.txt index 34323afa07..23ec8bb08a 100644 --- a/ggml/src/ggml-cpu/CMakeLists.txt +++ b/ggml/src/ggml-cpu/CMakeLists.txt @@ -504,11 +504,18 @@ function(ggml_add_cpu_backend_variant_impl tag_name) endforeach() endif() - if (GGML_VXE OR GGML_INTERNAL_VXE) - message(STATUS "VX/VXE/VXE2 enabled") + if (GGML_VXE OR GGML_INTERNAL_VXE2) + message(STATUS "VXE2 enabled") list(APPEND ARCH_FLAGS -mvx -mzvector) - list(APPEND ARCH_DEFINITIONS GGML_VXE) + list(APPEND ARCH_DEFINITIONS GGML_USE_VXE2) endif() + + if (GGML_INTERNAL_NNPA) + message(STATUS "NNPA enabled") + list(APPEND ARCH_DEFINITIONS GGML_USE_NNPA) + endif() + + ggml_add_cpu_backend_features(${GGML_CPU_NAME} s390 ${ARCH_DEFINITIONS}) elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "wasm") message(STATUS "Wasm detected") list (APPEND GGML_CPU_SOURCES ggml-cpu/arch/wasm/quants.c) diff --git a/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp b/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp new file mode 100644 index 0000000000..5f4405a7f3 --- /dev/null +++ b/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp @@ -0,0 +1,50 @@ +#include "ggml-backend-impl.h" + +#if defined(__s390x__) +#include + +// find hwcap bits in asm/elf.h +#ifndef HWCAP_VXRS_EXT2 +#define HWCAP_VXRS_EXT2 (1 << 15) +#endif + +#ifndef HWCAP_NNPA +#define HWCAP_NNPA (1 << 20) +#endif + +struct s390x_features { + bool has_vxe2 = false; + bool has_nnpa = false; + + s390x_features() { + uint32_t hwcap = getauxval(AT_HWCAP); + // NOTE: use hwcap2 with DFLT for z17 and later + // uint32_t hwcap2 = getauxval(AT_HWCAP2); + + has_vxe2 = !!(hwcap & HWCAP_VXRS_EXT2); + has_nnpa = !!(hwcap & HWCAP_NNPA); + } +}; + +static int ggml_backend_cpu_s390x_score() { + int score = 1; + s390x_features sf; + +// IBM z15 / LinuxONE 3 +#ifdef GGML_USE_VXE2 + if (!sf.has_vxe2) { return 0; } + score += 1 << 1; +#endif + +// IBM z16 / LinuxONE 4 and z17 / LinuxONE 5 +#ifdef GGML_USE_NNPA + if (!sf.has_nnpa) { return 0; } + score += 1 << 2; +#endif + + return score; +} + +GGML_BACKEND_DL_SCORE_IMPL(ggml_backend_cpu_s390x_score) + +#endif // __s390x__