Back to Llama Cpp

Build Riscv64 Spacemit

docs/build-riscv64-spacemit.md

latest4.0 KB
Original Source

[!IMPORTANT] This build documentation is specific only to RISC-V SpacemiT SOCs.

Build llama.cpp locally (for riscv64)

  1. Prepare Toolchain For RISCV
wget https://archive.spacemit.com/toolchain/spacemit-toolchain-linux-glibc-x86_64-v1.1.2.tar.xz
  1. Build Below is the build script: it requires utilizing RISC-V vector instructions for acceleration. Ensure the GGML_CPU_RISCV64_SPACEMIT compilation option is enabled. The currently supported optimization version is RISCV64_SPACEMIT_IME1, corresponding to the RISCV64_SPACEMIT_IME_SPEC compilation option. Compiler configurations are defined in the riscv64-spacemit-linux-gnu-gcc.cmake file. Please ensure you have installed the RISC-V compiler and set the environment variable via export RISCV_ROOT_PATH={your_compiler_path}.
bash

cmake -B build \
    -DCMAKE_BUILD_TYPE=Release \
    -DGGML_CPU_RISCV64_SPACEMIT=ON \
    -DLLAMA_OPENSSL=OFF \
    -DGGML_RVV=ON \
    -DGGML_RV_ZFH=ON \
    -DGGML_RV_ZICBOP=ON \
    -DGGML_RV_ZIHINTPAUSE=ON \
    -DRISCV64_SPACEMIT_IME_SPEC=RISCV64_SPACEMIT_IME1 \
    -DCMAKE_TOOLCHAIN_FILE=${PWD}/cmake/riscv64-spacemit-linux-gnu-gcc.cmake \
    -DCMAKE_INSTALL_PREFIX=build/installed

cmake --build build --parallel $(nproc) --config Release

pushd build
make install
popd

Simulation

You can use QEMU to perform emulation on non-RISC-V architectures.

  1. Download QEMU
wget https://archive.spacemit.com/spacemit-ai/qemu/jdsk-qemu-v0.0.14.tar.gz
  1. Run Simulation After build your llama.cpp, you can run the executable file via QEMU for simulation, for example:
export QEMU_ROOT_PATH={your QEMU file path}
export RISCV_ROOT_PATH_IME1={your RISC-V compiler path}

${QEMU_ROOT_PATH}/bin/qemu-riscv64 -L ${RISCV_ROOT_PATH_IME1}/sysroot -cpu max,vlen=256,elen=64,vext_spec=v1.0 ${PWD}/build/bin/llama-cli -m ${PWD}/models/Qwen2.5-0.5B-Instruct-Q4_0.gguf -t 1

Performance

Quantization Support For Matrix

model name      : Spacemit(R) X60
isa             : rv64imafdcv_zicbom_zicboz_zicntr_zicond_zicsr_zifencei_zihintpause_zihpm_zfh_zfhmin_zca_zcd_zba_zbb_zbc_zbs_zkt_zve32f_zve32x_zve64d_zve64f_zve64x_zvfh_zvfhmin_zvkt_sscofpmf_sstc_svinval_svnapot_svpbmt
mmu             : sv39
uarch           : spacemit,x60
mvendorid       : 0x710
marchid         : 0x8000000058000001

Q4_0

ModelSizeParamsbackendthreadstestt/s
Qwen2.5 0.5B403.20 MiB630.17 Mcpu4pp51264.12 ± 0.26
Qwen2.5 0.5B403.20 MiB630.17 Mcpu4tg12810.03 ± 0.01
Qwen2.5 1.5B1011.16 MiB1.78 Bcpu4pp51224.16 ± 0.02
Qwen2.5 1.5B1011.16 MiB1.78 Bcpu4tg1283.83 ± 0.06
Qwen2.5 3B1.86 GiB3.40 Bcpu4pp51212.08 ± 0.02
Qwen2.5 3B1.86 GiB3.40 Bcpu4tg1282.23 ± 0.02

Q4_1

ModelSizeParamsbackendthreadstestt/s
Qwen2.5 0.5B351.50 MiB494.03 Mcpu4pp51262.07 ± 0.12
Qwen2.5 0.5B351.50 MiB494.03 Mcpu4tg1289.91 ± 0.01
Qwen2.5 1.5B964.06 MiB1.54 Bcpu4pp51222.95 ± 0.25
Qwen2.5 1.5B964.06 MiB1.54 Bcpu4tg1284.01 ± 0.15
Qwen2.5 3B1.85 GiB3.09 Bcpu4pp51211.55 ± 0.16
Qwen2.5 3B1.85 GiB3.09 Bcpu4tg1282.25 ± 0.04

Q4_K

ModelSizeParamsbackendthreadstestt/s
Qwen2.5 0.5B462.96 MiB630.17 Mcpu4pp5129.29 ± 0.05
Qwen2.5 0.5B462.96 MiB630.17 Mcpu4tg1285.67 ± 0.04
Qwen2.5 1.5B1.04 GiB1.78 Bcpu4pp51210.38 ± 0.10
Qwen2.5 1.5B1.04 GiB1.78 Bcpu4tg1283.17 ± 0.08
Qwen2.5 3B1.95 GiB3.40 Bcpu4pp5124.23 ± 0.04
Qwen2.5 3B1.95 GiB3.40 Bcpu4tg1281.73 ± 0.00