Skip to content

Commit 5f55c38

Browse files
authored
ggml: add RISC-V cpu-feats (ggml-org#17461)
* ggml: add RISC-V cpu-feats Signed-off-by: Wang Yang <[email protected]> * fix comment[1] --------- Signed-off-by: Wang Yang <[email protected]>
1 parent 4902eeb commit 5f55c38

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

ggml/src/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ function(ggml_add_cpu_backend_variant tag_name)
328328
set(GGML_INTERNAL_${feat} OFF)
329329
endforeach()
330330

331+
foreach (feat ${ARGN})
332+
set(GGML_INTERNAL_${feat} ON)
333+
endforeach()
334+
elseif (GGML_SYSTEM_ARCH STREQUAL "riscv64")
335+
foreach (feat RVV)
336+
set(GGML_INTERNAL_${feat} OFF)
337+
endforeach()
338+
331339
foreach (feat ${ARGN})
332340
set(GGML_INTERNAL_${feat} ON)
333341
endforeach()
@@ -402,6 +410,13 @@ if (GGML_CPU_ALL_VARIANTS)
402410
else()
403411
message(FATAL_ERROR "Unsupported s390x target OS: ${CMAKE_SYSTEM_NAME}")
404412
endif()
413+
elseif (GGML_SYSTEM_ARCH STREQUAL "riscv64")
414+
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
415+
ggml_add_cpu_backend_variant(riscv64_0)
416+
ggml_add_cpu_backend_variant(riscv64_v RVV)
417+
else()
418+
message(FATAL_ERROR "Unsupported RISC-V target OS: ${CMAKE_SYSTEM_NAME}")
419+
endif()
405420
else()
406421
message(FATAL_ERROR "GGML_CPU_ALL_VARIANTS not yet supported with ${GGML_SYSTEM_ARCH} on ${CMAKE_SYSTEM_NAME}")
407422
endif()

ggml/src/ggml-cpu/CMakeLists.txt

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -452,22 +452,35 @@ function(ggml_add_cpu_backend_variant_impl tag_name)
452452
ggml-cpu/spacemit/ime_kernels.h
453453
)
454454
endif()
455-
set(MARCH_STR "rv64gc")
456-
if (GGML_RV_ZFH)
457-
string(APPEND MARCH_STR "_zfh")
458-
endif()
459-
if (GGML_XTHEADVECTOR)
460-
string(APPEND MARCH_STR "_xtheadvector")
461-
elseif (GGML_RVV)
462-
string(APPEND MARCH_STR "_v")
463-
if (GGML_RV_ZVFH)
464-
string(APPEND MARCH_STR "_zvfh")
455+
if(NOT GGML_CPU_ALL_VARIANTS)
456+
set(MARCH_STR "rv64gc")
457+
if (GGML_RV_ZFH)
458+
string(APPEND MARCH_STR "_zfh")
465459
endif()
460+
if (GGML_XTHEADVECTOR)
461+
string(APPEND MARCH_STR "_xtheadvector")
462+
elseif (GGML_RVV)
463+
string(APPEND MARCH_STR "_v")
464+
if (GGML_RV_ZVFH)
465+
string(APPEND MARCH_STR "_zvfh")
466+
endif()
467+
endif()
468+
if (GGML_RV_ZICBOP)
469+
string(APPEND MARCH_STR "_zicbop")
470+
endif()
471+
list(APPEND ARCH_FLAGS "-march=${MARCH_STR}" -mabi=lp64d)
472+
else()
473+
# Begin with the lowest baseline
474+
set(ARCH_DEFINITIONS "")
475+
476+
if (GGML_INTERNAL_RVV)
477+
message(STATUS "RVV enabled")
478+
list(APPEND ARCH_DEFINITIONS GGML_USE_RVV)
479+
list(APPEND ARCH_FLAGS -march=rv64gc_v -mabi=lp64d)
480+
endif()
481+
482+
ggml_add_cpu_backend_features(${GGML_CPU_NAME} riscv ${ARCH_DEFINITIONS})
466483
endif()
467-
if (GGML_RV_ZICBOP)
468-
string(APPEND MARCH_STR "_zicbop")
469-
endif()
470-
list(APPEND ARCH_FLAGS "-march=${MARCH_STR}" -mabi=lp64d)
471484
elseif (GGML_SYSTEM_ARCH STREQUAL "s390x")
472485
message(STATUS "s390x detected")
473486
list(APPEND GGML_CPU_SOURCES
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "ggml-backend-impl.h"
2+
3+
#if defined(__riscv) && __riscv_xlen == 64
4+
#include <sys/auxv.h>
5+
6+
//https://github.com/torvalds/linux/blob/master/arch/riscv/include/uapi/asm/hwcap.h#L24
7+
#ifndef COMPAT_HWCAP_ISA_V
8+
#define COMPAT_HWCAP_ISA_V (1 << ('V' - 'A'))
9+
#endif
10+
11+
struct riscv64_features {
12+
bool has_rvv = false;
13+
14+
riscv64_features() {
15+
uint32_t hwcap = getauxval(AT_HWCAP);
16+
17+
has_rvv = !!(hwcap & COMPAT_HWCAP_ISA_V);
18+
}
19+
};
20+
21+
static int ggml_backend_cpu_riscv64_score() {
22+
int score = 1;
23+
riscv64_features rf;
24+
25+
#ifdef GGML_USE_RVV
26+
if (!rf.has_rvv) { return 0; }
27+
score += 1 << 1;
28+
#endif
29+
30+
return score;
31+
}
32+
33+
GGML_BACKEND_DL_SCORE_IMPL(ggml_backend_cpu_riscv64_score)
34+
35+
#endif // __riscv && __riscv_xlen == 64

0 commit comments

Comments
 (0)