Skip to content

Commit 57fe015

Browse files
hexagon: add support for linux on snapdragon (ggml-org#21707)
* hexagon: add support for debian on ex2 * hexagon: add -fvectotize to c/c++ cmake flags * hexagon: remove trailing white space * update onboarding steps * hexagon: update linux setup documentation * hexagon: update intallation scripts * Hexagon: update docs * hexagon: update onboarding scripts --------- Co-authored-by: Zack Li <zackli@qti.qualcomm.com>
1 parent 0fa49de commit 57fe015

3 files changed

Lines changed: 105 additions & 1 deletion

File tree

cmake/arm64-linux-clang.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
set( CMAKE_SYSTEM_NAME Linux )
2+
set( CMAKE_SYSTEM_PROCESSOR arm64 )
3+
4+
set( target aarch64-linux-gnu )
5+
6+
set( CMAKE_C_COMPILER clang )
7+
set( CMAKE_CXX_COMPILER clang++ )
8+
9+
set( CMAKE_C_COMPILER_TARGET ${target} )
10+
set( CMAKE_CXX_COMPILER_TARGET ${target} )
11+
12+
set( arch_c_flags "-march=armv8.7-a -fvectorize -ffp-model=fast -fno-finite-math-only" )
13+
set( warn_c_flags "-Wno-format -Wno-unused-variable -Wno-unused-function -Wno-gnu-zero-variadic-macro-arguments" )
14+
15+
set( CMAKE_C_FLAGS_INIT "${arch_c_flags} ${warn_c_flags}" )
16+
set( CMAKE_CXX_FLAGS_INIT "${arch_c_flags} ${warn_c_flags}" )
17+

docs/backend/snapdragon/CMakeUserPresets.json

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,39 @@
5252
}
5353
},
5454

55+
{
56+
"name": "arm64-linux-snapdragon",
57+
"hidden": true,
58+
"architecture": { "value": "arm64", "strategy": "external" },
59+
"toolset": { "value": "host=x86_64", "strategy": "external" },
60+
"cacheVariables": {
61+
"CMAKE_TOOLCHAIN_FILE": "cmake/arm64-linux-clang.cmake",
62+
"CMAKE_C_FLAGS": "-march=armv8 -fno-finite-math-only -flto -D_GNU_SOURCE",
63+
"CMAKE_CXX_FLAGS": "-march=armv8 -fno-finite-math-only -flto -D_GNU_SOURCE",
64+
"CMAKE_C_FLAGS_RELEASE": "-O3 -DNDEBUG",
65+
"CMAKE_CXX_FLAGS_RELEASE": "-O3 -DNDEBUG",
66+
"CMAKE_C_FLAGS_RELWITHDEBINFO": "-O3 -DNDEBUG -g",
67+
"CMAKE_CXX_FLAGS_RELWITHDEBINFO": "-O3 -DNDEBUG -g",
68+
"CMAKE_PREFIX_PATH": "$env{OPENCL_SDK_ROOT}",
69+
"HEXAGON_SDK_ROOT": "$env{HEXAGON_SDK_ROOT}",
70+
"HEXAGON_TOOLS_ROOT": "$env{HEXAGON_TOOLS_ROOT}",
71+
"PREBUILT_LIB_DIR": "linux_aarch64",
72+
"GGML_OPENMP": "OFF",
73+
"GGML_LLAMAFILE": "OFF",
74+
"GGML_OPENCL": "OFF",
75+
"GGML_HEXAGON": "ON",
76+
"GGML_HEXAGON_FP32_QUANTIZE_GROUP_SIZE": "128",
77+
"LLAMA_OPENSSL": "OFF"
78+
}
79+
},
80+
5581
{ "name": "arm64-android-snapdragon-debug" , "inherits": [ "base", "arm64-android-snapdragon", "debug" ] },
5682
{ "name": "arm64-android-snapdragon-release", "inherits": [ "base", "arm64-android-snapdragon", "release" ] },
5783

5884
{ "name": "arm64-windows-snapdragon-debug" , "inherits": [ "base", "arm64-windows-snapdragon", "debug" ] },
59-
{ "name": "arm64-windows-snapdragon-release", "inherits": [ "base", "arm64-windows-snapdragon", "release" ] }
85+
{ "name": "arm64-windows-snapdragon-release", "inherits": [ "base", "arm64-windows-snapdragon", "release" ] },
86+
87+
{ "name": "arm64-linux-snapdragon-debug" , "inherits": [ "base", "arm64-linux-snapdragon", "debug" ] },
88+
{ "name": "arm64-linux-snapdragon-release", "inherits": [ "base", "arm64-linux-snapdragon", "release" ] }
6089
]
6190
}

docs/backend/snapdragon/linux.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Snapdragon-based Linux devices
2+
3+
## Docker Setup
4+
5+
The easiest way to build llama.cpp for a Snapdragon-based Linux device is using the toolchain Docker image (see [github.com/snapdragon-toolchain](https://github.com/snapdragon-toolchain)).
6+
This image includes OpenCL SDK, Hexagon SDK, CMake, and the ARM64 Linux cross-compilation toolchain.
7+
8+
Cross-compilation is supported on **Linux X86** hosts. The resulting binaries are deployed to and run on the target **Qualcomm Snapdragon ARM64 Linux** device.
9+
10+
```
11+
~/src/llama.cpp$ docker run -it -u $(id -u):$(id -g) --volume $(pwd):/workspace --platform linux/amd64 ghcr.io/snapdragon-toolchain/arm64-linux:v0.1
12+
[d]/> cd /workspace
13+
```
14+
15+
Note: The rest of the **Linux** build process assumes that you're running inside the toolchain container.
16+
17+
18+
## How to Build
19+
20+
Let's build llama.cpp with CPU, OpenCL, and Hexagon backends via CMake presets:
21+
22+
```
23+
[d]/workspace> cp docs/backend/snapdragon/CMakeUserPresets.json .
24+
25+
[d]/workspace> cmake --preset arm64-linux-snapdragon-release -B build-snapdragon
26+
27+
[d]/workspace> cmake --build build-snapdragon -j $(nproc)
28+
```
29+
30+
To generate an installable "package" simply use cmake --install, then zip it:
31+
32+
```
33+
[d]/workspace> cmake --install build-snapdragon --prefix pkg-snapdragon
34+
[d]/workspace> zip -r pkg-snapdragon.zip pkg-snapdragon
35+
```
36+
37+
## How to Install
38+
39+
For this step, you will deploy the built binaries and libraries to the target Linux device. Transfer `pkg-snapdragon.zip` to the target device, then unzip it and set up the environment variables:
40+
41+
```
42+
$ unzip pkg-snapdragon.zip
43+
$ cd pkg-snapdragon
44+
$ export LD_LIBRARY_PATH=./lib
45+
$ export ADSP_LIBRARY_PATH=./lib
46+
```
47+
48+
At this point, you should also download some models onto the device:
49+
50+
```
51+
$ wget https://huggingface.co/bartowski/Llama-3.2-3B-Instruct-GGUF/resolve/main/Llama-3.2-3B-Instruct-Q4_0.gguf
52+
```
53+
54+
## How to Run
55+
Next, since we have setup the environment variables, we can run the llama-cli with the Hexagon backends:
56+
```
57+
$ ./bin/llama-cli -m Llama-3.2-3B-Instruct-Q4_0.gguf --device HTP0 -ngl 99 -p "what is the most popular cookie in the world?"
58+
```

0 commit comments

Comments
 (0)