Skip to content

Commit

Permalink
cross compile to LoongArch64
Browse files Browse the repository at this point in the history
  • Loading branch information
AyiStar committed Mar 19, 2024
1 parent a446ad1 commit 0948dcd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 9 additions & 1 deletion docs/riscv_cross_compilation.md → docs/cross_compilation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
File renamed without changes.
17 changes: 13 additions & 4 deletions la-llama.cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0948dcd

Please sign in to comment.