From 0948dcd6077bc41f9d020846ace15940abed9814 Mon Sep 17 00:00:00 2001 From: AyiStar Date: Tue, 19 Mar 2024 12:04:55 +0000 Subject: [PATCH] cross compile to LoongArch64 --- README.md | 2 ++ ...ross_compilation.md => cross_compilation.md} | 10 +++++++++- docs/{cross_tools_setup.md => tools_setup.md} | 0 la-llama.cpp/Makefile | 17 +++++++++++++---- 4 files changed, 24 insertions(+), 5 deletions(-) rename docs/{riscv_cross_compilation.md => cross_compilation.md} (59%) rename docs/{cross_tools_setup.md => tools_setup.md} (100%) diff --git a/README.md b/README.md index 755131c..ce17f0d 100644 --- a/README.md +++ b/README.md @@ -49,3 +49,5 @@ At this stage, we get familiar with the concept of cross compilation, build and - [x] Set up cross compilation tools and QEMU environment for LoongArch. ### Porting Stage +- [x] Alter the makefile for LoongArch cross compilation. +- [x] Cross compile llama.cpp to LoongArch64. \ No newline at end of file diff --git a/docs/riscv_cross_compilation.md b/docs/cross_compilation.md similarity index 59% rename from docs/riscv_cross_compilation.md rename to docs/cross_compilation.md index 2821ce9..7981e9f 100644 --- a/docs/riscv_cross_compilation.md +++ b/docs/cross_compilation.md @@ -7,14 +7,22 @@ On Ubuntu 20.04, install the following packages for riscv64 cross compiler: Then we get riscv64-linux-gnu-gcc and riscv64-linux-gnu-g++. +For LoongArch, refer to `tools_setup.md`. + ## Cross Compilation Cross compile llama.cpp with Makefile: ```bash make clean +# riscv64 make RISCV_CROSS_COMPILE=1 RISCV=1 +# loongarch64 +make LOONGARCH_CROSS_COMPILE=1 LOONGARCH=1 ``` -## Run with QEMU-riscv64: +## Run with QEMU: ```bash +# riscv64 qemu-riscv64 -L /usr/riscv64-linux-gnu/ -cpu rv64,v=true,vlen=256,elen=64,vext_spec=v1.0 ./main -m $LLAMA_GGUF_PATH/llama-2-7b.Q4_0.gguf -n 512 -p "Building a website can be done in 10 simple steps:\nStep 1:" -e -t 1 +# loongarch64 +qemu-loongarch64 -L $LA_TOOLCHAIN/target/ -E LD_LIBRARY_PATH=$LA_TOOLCHAIN/loongarch64-unknown-linux-gnu/lib/:LD_LIBRARY_PATH ./main -m $LLAMA_GGUF_PATH/llama-2-7b.Q4_0.gguf -n 512 -p "Building a website can be done in 10 simple steps:\nStep 1:" -e -t 1 ``` \ No newline at end of file diff --git a/docs/cross_tools_setup.md b/docs/tools_setup.md similarity index 100% rename from docs/cross_tools_setup.md rename to docs/tools_setup.md diff --git a/la-llama.cpp/Makefile b/la-llama.cpp/Makefile index 1ceade8..268cda6 100644 --- a/la-llama.cpp/Makefile +++ b/la-llama.cpp/Makefile @@ -94,6 +94,11 @@ CC := riscv64-linux-gnu-gcc CXX := riscv64-linux-gnu-g++ endif +ifdef LOONGARCH_CROSS_COMPILE +CC := loongarch64-unknown-linux-gnu-gcc +CXX := loongarch64-unknown-linux-gnu-g++ +endif + # # Compile flags # @@ -277,8 +282,15 @@ endif # TODO: probably these flags need to be tweaked on some architectures # feel free to update the Makefile for your architecture and send a pull request or issue -ifndef RISCV +ifdef RISCV + MK_CFLAGS += -march=rv64gcv -mabi=lp64d + MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d + +else ifdef LOONGARCH + MK_CFLAGS += -march=loongarch64 -mabi=lp64d + MK_CXXFLAGS += -march=loongarch64 -mabi=lp64d +else ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64)) # Use all CPU extensions that are available: MK_CFLAGS += -march=native -mtune=native @@ -352,9 +364,6 @@ ifneq ($(filter ppc64le%,$(UNAME_M)),) CUDA_POWER_ARCH = 1 endif -else - MK_CFLAGS += -march=rv64gcv -mabi=lp64d - MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d endif ifdef LLAMA_QKK_64