antimatter15 / alpaca.cpp

Locally run an Instruction-Tuned Chat-Style LLM
MIT License
10.25k stars 907 forks source link

Adding risc-v architecture support #231

Open dhood opened 1 year ago

dhood commented 1 year ago

Hi there, thanks for the great library!! :bow:

Motivated by a pretty specific application (to get alpaca results "verifiable" by running on Cartesi which is ontop of blockchain), I'm trying to compile alpaca.cpp for RISC-V architecture.

It's my first foray into the joys of such, and I have gotten stuck. But before I give up I thought I'd check if anyone happens to have tried it already.

FWICT the main thing is the vector intrinsics:

diff --git a/ggml.c b/ggml.c
index 535c7b7..b6828a8 100644
--- a/ggml.c
+++ b/ggml.c
@@ -151,10 +151,18 @@ typedef double ggml_float;
 #include <altivec.h>
 #undef bool
 #define bool _Bool
+#else
+// RISC-V support
+#if defined(__riscv) || defined(__riscv__)
+#include <riscv_vector.h>
+// Having issues finding this: #include <gnu/stubs-lp64.h>
+// this exists though: #include <gnu/stubs-lp64d.h>
+
 #else
 #include <immintrin.h>
 #endif
 #endif
+#endif

 #ifdef __F16C__

But to get that right I had to switch to LLVM/clang and I'm still tweaking the build flags.

diff --git a/Makefile b/Makefile
index 93da626..7617e28 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,8 @@ ifndef UNAME_M
 UNAME_M := $(shell uname -m)
 endif

+CXX := clang++-15
+CC := clang-15
 CCV := $(shell $(CC) --version | head -n 1)
 CXXV := $(shell $(CXX) --version | head -n 1)

@@ -160,6 +162,9 @@ ifneq ($(filter armv8%,$(UNAME_M)),)
        # Raspberry Pi 4
        CFLAGS += -mfp16-format=ieee -mno-unaligned-access
 endif
+       # TODO: conditional on riscv
+       CFLAGS += -march=rv64i -mabi=lp64

 #
 # Print build information

if RISC-V happens to be on your todo list, I'd be happy to follow any pointers! :grin:

pikalover6 commented 1 year ago

This repo outdated and obsolete. Check out the ggml or llama.cpp repos.