ggml: add RISC-V cpu-feats (#17461)
* ggml: add RISC-V cpu-feats Signed-off-by: Wang Yang <yangwang@iscas.ac.cn> * fix comment[1] --------- Signed-off-by: Wang Yang <yangwang@iscas.ac.cn>
This commit is contained in:
parent
4902eebe33
commit
5f55c385cb
|
|
@ -328,6 +328,14 @@ function(ggml_add_cpu_backend_variant tag_name)
|
||||||
set(GGML_INTERNAL_${feat} OFF)
|
set(GGML_INTERNAL_${feat} OFF)
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
|
foreach (feat ${ARGN})
|
||||||
|
set(GGML_INTERNAL_${feat} ON)
|
||||||
|
endforeach()
|
||||||
|
elseif (GGML_SYSTEM_ARCH STREQUAL "riscv64")
|
||||||
|
foreach (feat RVV)
|
||||||
|
set(GGML_INTERNAL_${feat} OFF)
|
||||||
|
endforeach()
|
||||||
|
|
||||||
foreach (feat ${ARGN})
|
foreach (feat ${ARGN})
|
||||||
set(GGML_INTERNAL_${feat} ON)
|
set(GGML_INTERNAL_${feat} ON)
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
@ -402,6 +410,13 @@ if (GGML_CPU_ALL_VARIANTS)
|
||||||
else()
|
else()
|
||||||
message(FATAL_ERROR "Unsupported s390x target OS: ${CMAKE_SYSTEM_NAME}")
|
message(FATAL_ERROR "Unsupported s390x target OS: ${CMAKE_SYSTEM_NAME}")
|
||||||
endif()
|
endif()
|
||||||
|
elseif (GGML_SYSTEM_ARCH STREQUAL "riscv64")
|
||||||
|
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
|
||||||
|
ggml_add_cpu_backend_variant(riscv64_0)
|
||||||
|
ggml_add_cpu_backend_variant(riscv64_v RVV)
|
||||||
|
else()
|
||||||
|
message(FATAL_ERROR "Unsupported RISC-V target OS: ${CMAKE_SYSTEM_NAME}")
|
||||||
|
endif()
|
||||||
else()
|
else()
|
||||||
message(FATAL_ERROR "GGML_CPU_ALL_VARIANTS not yet supported with ${GGML_SYSTEM_ARCH} on ${CMAKE_SYSTEM_NAME}")
|
message(FATAL_ERROR "GGML_CPU_ALL_VARIANTS not yet supported with ${GGML_SYSTEM_ARCH} on ${CMAKE_SYSTEM_NAME}")
|
||||||
endif()
|
endif()
|
||||||
|
|
|
||||||
|
|
@ -452,22 +452,35 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
|
||||||
ggml-cpu/spacemit/ime_kernels.h
|
ggml-cpu/spacemit/ime_kernels.h
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
set(MARCH_STR "rv64gc")
|
if(NOT GGML_CPU_ALL_VARIANTS)
|
||||||
if (GGML_RV_ZFH)
|
set(MARCH_STR "rv64gc")
|
||||||
string(APPEND MARCH_STR "_zfh")
|
if (GGML_RV_ZFH)
|
||||||
endif()
|
string(APPEND MARCH_STR "_zfh")
|
||||||
if (GGML_XTHEADVECTOR)
|
|
||||||
string(APPEND MARCH_STR "_xtheadvector")
|
|
||||||
elseif (GGML_RVV)
|
|
||||||
string(APPEND MARCH_STR "_v")
|
|
||||||
if (GGML_RV_ZVFH)
|
|
||||||
string(APPEND MARCH_STR "_zvfh")
|
|
||||||
endif()
|
endif()
|
||||||
|
if (GGML_XTHEADVECTOR)
|
||||||
|
string(APPEND MARCH_STR "_xtheadvector")
|
||||||
|
elseif (GGML_RVV)
|
||||||
|
string(APPEND MARCH_STR "_v")
|
||||||
|
if (GGML_RV_ZVFH)
|
||||||
|
string(APPEND MARCH_STR "_zvfh")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
if (GGML_RV_ZICBOP)
|
||||||
|
string(APPEND MARCH_STR "_zicbop")
|
||||||
|
endif()
|
||||||
|
list(APPEND ARCH_FLAGS "-march=${MARCH_STR}" -mabi=lp64d)
|
||||||
|
else()
|
||||||
|
# Begin with the lowest baseline
|
||||||
|
set(ARCH_DEFINITIONS "")
|
||||||
|
|
||||||
|
if (GGML_INTERNAL_RVV)
|
||||||
|
message(STATUS "RVV enabled")
|
||||||
|
list(APPEND ARCH_DEFINITIONS GGML_USE_RVV)
|
||||||
|
list(APPEND ARCH_FLAGS -march=rv64gc_v -mabi=lp64d)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
ggml_add_cpu_backend_features(${GGML_CPU_NAME} riscv ${ARCH_DEFINITIONS})
|
||||||
endif()
|
endif()
|
||||||
if (GGML_RV_ZICBOP)
|
|
||||||
string(APPEND MARCH_STR "_zicbop")
|
|
||||||
endif()
|
|
||||||
list(APPEND ARCH_FLAGS "-march=${MARCH_STR}" -mabi=lp64d)
|
|
||||||
elseif (GGML_SYSTEM_ARCH STREQUAL "s390x")
|
elseif (GGML_SYSTEM_ARCH STREQUAL "s390x")
|
||||||
message(STATUS "s390x detected")
|
message(STATUS "s390x detected")
|
||||||
list(APPEND GGML_CPU_SOURCES
|
list(APPEND GGML_CPU_SOURCES
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
#include "ggml-backend-impl.h"
|
||||||
|
|
||||||
|
#if defined(__riscv) && __riscv_xlen == 64
|
||||||
|
#include <sys/auxv.h>
|
||||||
|
|
||||||
|
//https://github.com/torvalds/linux/blob/master/arch/riscv/include/uapi/asm/hwcap.h#L24
|
||||||
|
#ifndef COMPAT_HWCAP_ISA_V
|
||||||
|
#define COMPAT_HWCAP_ISA_V (1 << ('V' - 'A'))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct riscv64_features {
|
||||||
|
bool has_rvv = false;
|
||||||
|
|
||||||
|
riscv64_features() {
|
||||||
|
uint32_t hwcap = getauxval(AT_HWCAP);
|
||||||
|
|
||||||
|
has_rvv = !!(hwcap & COMPAT_HWCAP_ISA_V);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static int ggml_backend_cpu_riscv64_score() {
|
||||||
|
int score = 1;
|
||||||
|
riscv64_features rf;
|
||||||
|
|
||||||
|
#ifdef GGML_USE_RVV
|
||||||
|
if (!rf.has_rvv) { return 0; }
|
||||||
|
score += 1 << 1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
GGML_BACKEND_DL_SCORE_IMPL(ggml_backend_cpu_riscv64_score)
|
||||||
|
|
||||||
|
#endif // __riscv && __riscv_xlen == 64
|
||||||
Loading…
Reference in New Issue