The main product of this project is the llama library. Its C-style interface can be found in include/llama.h.
The project also includes many example programs and tools using the llama library. The examples range from simple, minimal code snippets to sophisticated sub-projects such as an OpenAI-compatible HTTP server.
To get the Code:
git clone https://github.com/ggml-org/llama.cpp
cd llama.cppThe following sections describe how to build with different backends and options.
- CPU Build
- BLAS Build
- Metal Build
- SYCL
- CUDA
- MUSA
- HIP
- Vulkan
- CANN
- ZenDNN
- Arm® KleidiAI™
- OpenCL
- Android
- OpenVINO
- Notes about GPU-accelerated backends
Build llama.cpp using CMake:
cmake -B build
cmake --build build --config ReleaseNotes:
-
For faster compilation, add the
-jargument to run multiple jobs in parallel, or use a generator that does this automatically such as Ninja. For example,cmake --build build --config Release -j 8will run 8 jobs in parallel. -
For faster repeated compilation, install ccache
-
For debug builds, there are two cases:
-
Single-config generators (e.g. default =
Unix Makefiles; note that they just ignore the--configflag):cmake -B build -DCMAKE_BUILD_TYPE=Debug cmake --build build
-
Multi-config generators (
-Gparam set to Visual Studio, XCode...):cmake -B build -G "Xcode" cmake --build build --config Debug
For more details and a list of supported generators, see the CMake documentation.
-
-
For static builds, add
-DBUILD_SHARED_LIBS=OFF:cmake -B build -DBUILD_SHARED_LIBS=OFF cmake --build build --config Release -
Building for Windows (x86, x64 and arm64) with MSVC or clang as compilers:
- Install Visual Studio 2022, e.g. via the Community Edition. In the installer, select at least the following options (this also automatically installs the required additional tools like CMake,...):
- Tab Workload: Desktop-development with C++
- Tab Components (select quickly via search): C++-CMake Tools for Windows, Git for Windows, C++-Clang Compiler for Windows, MS-Build Support for LLVM-Toolset (clang)
- Please remember to always use a Developer Command Prompt / PowerShell for VS2022 for git, build, test
- For Windows on ARM (arm64, WoA), build with:
cmake --preset arm64-windows-llvm-release -D GGML_OPENMP_FETCH=ON cmake --build build-arm64-windows-llvm-release
- Use
ARM64 Native Tools Command Prompt for VS 2022if you are building on an ARM64 machine. GGML_OPENMP_FETCHdownloads the official LLVM OpenMP runtime and requires Clang, 7-Zip and network access during configuration. CMake selects the runtime from the target architecture, so this also works when cross-compiling for WoA from x64. The extracted header, import library, DLL and OpenMP license are placed underbuild/_deps. The build copieslibomp.dllandLICENSE-LLVM-OpenMPto the runtime output directory and installs them together. Omit the option to use CMake's normal OpenMP detection, or pass-D GGML_OPENMP=OFFto disable OpenMP.
- Use
- For building with ninja generator and clang compiler as default:
- Set path:
set LIB=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\lib\x64\uwp;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64 - Run:
cmake --preset x64-windows-llvm-release cmake --build build-x64-windows-llvm-release
- Set path:
-
If you want HTTPS/TLS features, you may install OpenSSL development libraries. If not installed, the project will build and run without SSL support.
- Debian / Ubuntu:
sudo apt-get install libssl-dev - Fedora / RHEL / Rocky / Alma:
sudo dnf install openssl-devel - Arch / Manjaro:
sudo pacman -S openssl
- Debian / Ubuntu:
Building the program with BLAS support may lead to some performance improvements in prompt processing using batch sizes higher than 32 (the default is 512). Using BLAS doesn't affect the generation performance. There are currently several different BLAS implementations available for build and use:
This is only available on Mac PCs and it's enabled by default. You can just build using the normal instructions.
This provides BLAS acceleration using only the CPU. Make sure to have OpenBLAS installed on your machine.
-
Using
CMakeon Linux:cmake -B build -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS cmake --build build --config Release
Check BLIS.md for more information.
Building through oneAPI compilers will make avx_vnni instruction set available for intel processors that do not support avx512 and avx512_vnni. Please note that this build config does not support Intel GPU. For Intel GPU support, please refer to llama.cpp for SYCL.
-
Using manual oneAPI installation: By default,
GGML_BLAS_VENDORis set toGeneric, so if you already sourced intel environment script and assign-DGGML_BLAS=ONin cmake, the mkl version of Blas will automatically been selected. Otherwise please install oneAPI and follow the below steps:source /opt/intel/oneapi/setvars.sh # You can skip this step if in oneapi-basekit docker image, only required for manual installation cmake -B build -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=Intel10_64lp -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DGGML_NATIVE=ON cmake --build build --config Release
-
Using oneAPI docker image: If you do not want to source the environment vars and install oneAPI manually, you can also build the code using intel docker container: oneAPI-basekit. Then, you can use the commands given above.
Check Optimizing and Running LLaMA2 on Intel® CPU for more information.
Any other BLAS library can be used by setting the GGML_BLAS_VENDOR option. See the CMake documentation for a list of supported vendors.
On MacOS, Metal is enabled by default. Using Metal makes the computation run on the GPU.
To disable the Metal build at compile time use the -DGGML_METAL=OFF cmake option.
When built with Metal support, you can explicitly disable GPU inference with the --n-gpu-layers 0 command-line argument.
SYCL is a higher-level programming model to improve programming productivity on various hardware accelerators.
llama.cpp based on SYCL is used to support Intel GPU (Data Center Max series, Flex series, Arc series, Built-in GPU and iGPU).
For detailed info, please refer to llama.cpp for SYCL.
This provides GPU acceleration using an NVIDIA GPU. Make sure to have the CUDA toolkit installed.
You may find the official downloads here: NVIDIA developer site.
We also have a guide for setting up CUDA toolkit in a Fedora toolbox container.
Recommended for:
- Necessary for users of Atomic Desktops for Fedora; such as: Silverblue and Kinoite.
- (there are no supported CUDA packages for these systems)
- Necessary for users that have a host that is not a: Supported Nvidia CUDA Release Platform.
- (for example, you may have Fedora 42 Beta as your host operating system)
- Convenient For those running Fedora Workstation or Fedora KDE Plasma Desktop, and want to keep their host system clean.
- Optionally toolbox packages are available: Arch Linux, Red Hat Enterprise Linux >= 8.5, or Ubuntu
Make sure to read the notes about the CPU build for general instructions for e.g. speeding up the compilation.
cmake -B build -DGGML_CUDA=ON
cmake --build build --config ReleaseBy default llama.cpp will be built for the hardware that is connected to the system at that time.
For a build covering all CUDA GPUs, disable GGML_NATIVE:
cmake -B build -DGGML_CUDA=ON -DGGML_NATIVE=OFFThe resulting binary should run on all CUDA GPUs with optimal performance, though some just-in-time compilation may be required.
If nvcc cannot detect your gpu, you may get compile warnings such as:
nvcc warning : Cannot find valid GPU for '-arch=native', default arch is used
One option is to do a non-native build as described above.
However, this will result in a large binary that takes a long time to compile.
Alternatively it is also possible to explicitly specify CUDA architectures.
This may also make sense for a non-native build, for that one should look at the logic in ggml/src/ggml-cuda/CMakeLists.txt as a starting point.
To override the default CUDA architectures:
1. Take note of the Compute Capability of your NVIDIA devices: "CUDA: Your GPU Compute > Capability".
GeForce RTX 4090 8.9
GeForce RTX 3080 Ti 8.6
GeForce RTX 3070 8.6
cmake -B build -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES="86;89"If you have multiple CUDA installations on your system and want to compile llama.cpp for a specific one, e.g. for CUDA 11.7 installed under /opt/cuda-11.7:
cmake -B build -DGGML_CUDA=ON -DCMAKE_CUDA_COMPILER=/opt/cuda-11.7/bin/nvcc -DCMAKE_INSTALL_RPATH="/opt/cuda-11.7/lib64;\$ORIGIN" -DCMAKE_BUILD_WITH_INSTALL_RPATH=ONIf you try to use an old CUDA version (e.g. v11.7) with a new glibc version you can get errors like this:
/usr/include/bits/mathcalls.h(83): error: exception specification is
incompatible with that of previous function "cospi"
/opt/cuda-11.7/bin/../targets/x86_64-linux/include/crt/math_functions.h(5545):
here
It seems the least bad solution is to patch the CUDA installation to declare the correct signatures.
Replace the following lines in /path/to/your/cuda/installation/targets/x86_64-linux/include/crt/math_functions.h:
// original lines
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double cospi(double x);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float cospif(float x);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double sinpi(double x);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sinpif(float x);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double rsqrt(double x);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float rsqrtf(float x);
// edited lines
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double cospi(double x) noexcept (true);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float cospif(float x) noexcept (true);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double sinpi(double x) noexcept (true);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float sinpif(float x) noexcept (true);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ double rsqrt(double x) noexcept (true);
extern __DEVICE_FUNCTIONS_DECL__ __device_builtin__ float rsqrtf(float x) noexcept (true);You may set the cuda environmental variables at runtime.
# Use `CUDA_VISIBLE_DEVICES` to hide the first compute device.
CUDA_VISIBLE_DEVICES="-0" ./build/bin/llama-server --model /srv/models/llama.ggufThe environment variable CUDA_SCALE_LAUNCH_QUEUES controls the size of CUDA's command buffer, which determines how many GPU operations can be queued before the CPU must wait for the GPU to catch up. A larger buffer reduces CPU-side stalls and allows more work to be queued on a GPU.
Consider setting CUDA_SCALE_LAUNCH_QUEUES=4x, which increases the CUDA command buffer to 4 times its default size. This optimization is particularly beneficial for Multi-GPU setups with pipeline parallelism, where it significantly improves prompt processing throughput by allowing more operations to be enqueued across GPUs.
Override default, speed-optimized compute types for cuBLAS matrix multiplications.
Legal values: auto, f16, fp16, bf16, f32, fp32.
The environment variable GGML_CUDA_ENABLE_UNIFIED_MEMORY=1 can be used to enable unified memory in Linux. This allows swapping to system RAM instead of crashing when the GPU VRAM is exhausted. In Windows this setting is available in the NVIDIA control panel as System Memory Fallback.
The environment variable GGML_CUDA_P2P can be set to enable peer-to-peer access between multiple GPUs, allowing them to transfer data directly rather than to go through system memory.
Requires driver support (usually restricted to workstation/datacenter GPUs).
May cause crashes or corrupted outputs for some motherboards and BIOS settings (e.g. IOMMU).
The following compilation options are also available to tweak performance:
| Option | Legal values | Default | Description |
|---|---|---|---|
| GGML_CUDA_FORCE_MMQ | Boolean | false | Force the use of custom matrix multiplication kernels for quantized models instead of FP16 cuBLAS even if there is no int8 tensor core implementation available (affects V100, CDNA and RDNA3+). MMQ kernels are enabled by default on GPUs with int8 tensor core support. With MMQ force enabled, speed for large batch sizes will be worse but VRAM consumption will be lower. |
| GGML_CUDA_FORCE_CUBLAS | Boolean | false | Force the use of FP16 cuBLAS instead of custom matrix multiplication kernels for quantized models. There may be issues with numerical overflows (except for V100, CDNA and RDNA4 which use FP32 compute type by default) and memory use will be higher. Prompt processing may become faster on recent datacenter GPUs (the custom kernels were tuned primarily for RTX 3000/4000). |
| GGML_CUDA_PEER_MAX_BATCH_SIZE | Positive integer | 128 | Maximum batch size for which to enable peer access between multiple GPUs. Peer access requires either Linux or NVLink. When using NVLink enabling peer access for larger batch sizes is potentially beneficial. |
| GGML_CUDA_FA_ALL_QUANTS | Boolean | false | Compile support for all KV cache quantization type (combinations) for the FlashAttention CUDA kernels. More fine-grained control over KV cache size but compilation takes much longer. |
This provides GPU acceleration using a Moore Threads GPU. Make sure to have the MUSA SDK installed.
You may find the official downloads here: Moore Threads developer site.
cmake -B build -DGGML_MUSA=ON
cmake --build build --config ReleaseBy default, all supported compute capabilities are enabled. To customize this behavior, you can specify the MUSA_ARCHITECTURES option in the CMake command:
cmake -B build -DGGML_MUSA=ON -DMUSA_ARCHITECTURES="21"
cmake --build build --config ReleaseThis configuration enables only compute capability 2.1 (MTT S80) during compilation, which can help reduce compilation time.
Most of the compilation options available for CUDA should also be available for MUSA, though they haven't been thoroughly tested yet.
- For static builds, add
-DBUILD_SHARED_LIBS=OFFand-DCMAKE_POSITION_INDEPENDENT_CODE=ON:cmake -B build -DGGML_MUSA=ON \ -DBUILD_SHARED_LIBS=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON cmake --build build --config Release
You may set the musa environmental variables at runtime.
# Use `MUSA_VISIBLE_DEVICES` to hide the first compute device.
MUSA_VISIBLE_DEVICES="-0" ./build/bin/llama-server --model /srv/models/llama.ggufThe environment variable GGML_CUDA_ENABLE_UNIFIED_MEMORY=1 can be used to enable unified memory in Linux. This allows swapping to system RAM instead of crashing when the GPU VRAM is exhausted.
This provides GPU acceleration on HIP-supported AMD GPUs. Make sure to have ROCm installed. You can download it from your Linux distro's package manager or from here: ROCm Quick Start (Linux).
-
Using
CMakefor Linux (assuming a gfx1030-compatible AMD GPU):HIPCXX="$(hipconfig -l)/clang" HIP_PATH="$(hipconfig -R)" \ cmake -S . -B build -DGGML_HIP=ON -DGPU_TARGETS=gfx1030 -DCMAKE_BUILD_TYPE=Release \ && cmake --build build --config Release -- -j 16
Note:
GPU_TARGETSis optional, omitting it will build the code for all GPUs in the current system.Note that if you get the following error:
clang: error: cannot find ROCm device library; provide its path via '--rocm-path' or '--rocm-device-lib-path', or pass '-nogpulib' to build without ROCm device libraryTry searching for a directory under
HIP_PATHthat contains the fileoclc_abi_version_400.bc. Then, add the following to the start of the command:HIP_DEVICE_LIB_PATH=<directory-you-just-found>, so something like:HIPCXX="$(hipconfig -l)/clang" HIP_PATH="$(hipconfig -p)" \ HIP_DEVICE_LIB_PATH=<directory-you-just-found> \ cmake -S . -B build -DGGML_HIP=ON -DGPU_TARGETS=gfx1030 -DCMAKE_BUILD_TYPE=Release \ && cmake --build build -- -j 16
-
Using
CMakefor Windows (using x64 Native Tools Command Prompt for VS, and assuming a gfx1100-compatible AMD GPU):set PATH=%HIP_PATH%\bin;%PATH% cmake -S . -B build -G Ninja -DGPU_TARGETS=gfx1100 -DGGML_HIP=ON -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release cmake --build build
If necessary, adapt
GPU_TARGETSto the GPU arch you want to compile for. The above example usesgfx1100that corresponds to Radeon RX 7900XTX/XT/GRE. You can find a list of targets here Find your gpu version string by matching the most significant version information fromrocminfo | grep gfx | head -1 | awk '{print $2}'with the list of processors, e.g.gfx1035maps togfx1030.
The environment variable HIP_VISIBLE_DEVICES can be used to specify which GPU(s) will be used.
If your GPU is not officially supported you can use the environment variable [HSA_OVERRIDE_GFX_VERSION] set to a similar GPU, for example 10.3.0 on RDNA2 (e.g. gfx1030, gfx1031, or gfx1035) or 11.0.0 on RDNA3. Note that [HSA_OVERRIDE_GFX_VERSION] is not supported on Windows
On Linux it is possible to use unified memory architecture (UMA) to share main memory between the CPU and integrated GPU by setting environment variable GGML_CUDA_ENABLE_UNIFIED_MEMORY=1. However, this hurts performance for non-integrated GPUs (but enables working with integrated GPUs).
w64devkit
Download and extract w64devkit.
Download and install the Vulkan SDK with the default settings.
Launch w64devkit.exe and run the following commands to copy Vulkan dependencies:
SDK_VERSION=1.3.283.0
cp /VulkanSDK/$SDK_VERSION/Bin/glslc.exe $W64DEVKIT_HOME/bin/
cp /VulkanSDK/$SDK_VERSION/Lib/vulkan-1.lib $W64DEVKIT_HOME/x86_64-w64-mingw32/lib/
cp -r /VulkanSDK/$SDK_VERSION/Include/* $W64DEVKIT_HOME/x86_64-w64-mingw32/include/
cat > $W64DEVKIT_HOME/x86_64-w64-mingw32/lib/pkgconfig/vulkan.pc <<EOF
Name: Vulkan-Loader
Description: Vulkan Loader
Version: $SDK_VERSION
Libs: -lvulkan-1
EOF
Switch into the llama.cpp directory and build using CMake.
cmake -B build -DGGML_VULKAN=ON
cmake --build build --config ReleaseGit Bash MINGW64
Download and install Git-SCM with the default settings
Download and install Visual Studio Community Edition and make sure you select C++
Download and install CMake with the default settings
Download and install the Vulkan SDK with the default settings.
Go into your llama.cpp directory and right click, select Open Git Bash Here and then run the following commands
cmake -B build -DGGML_VULKAN=ON
cmake --build build --config Release
Now you can load the model in conversation mode using Vulkan
build/bin/Release/llama-cli -m "[PATH TO MODEL]" -ngl 100 -c 16384 -t 10 -n -2 -cnvMSYS2
Install MSYS2 and then run the following commands in a UCRT terminal to install dependencies.
pacman -S git \
mingw-w64-ucrt-x86_64-gcc \
mingw-w64-ucrt-x86_64-cmake \
mingw-w64-ucrt-x86_64-vulkan-devel \
mingw-w64-ucrt-x86_64-shaderc \
mingw-w64-ucrt-x86_64-spirv-headersSwitch into the llama.cpp directory and build using CMake.
cmake -B build -DGGML_VULKAN=ON
cmake --build build --config ReleaseYou don't need to install the Vulkan SDK. It will be installed inside the container.
# Build the image
docker build -t llama-cpp-vulkan --target light -f .devops/vulkan.Dockerfile .
# Then, use it:
docker run -it --rm -v "$(pwd):/app:Z" --device /dev/dri/renderD128:/dev/dri/renderD128 --device /dev/dri/card1:/dev/dri/card1 llama-cpp-vulkan -m "/app/models/YOUR_MODEL_FILE" -p "Building a website can be done in 10 simple steps:" -n 400 -e -ngl 33First, follow the official LunarG instructions for the installation and setup of the Vulkan SDK in the Getting Started with the Linux Tarball Vulkan SDK guide.
Important
After completing the first step, ensure that you have used the source command on the setup_env.sh file inside of the Vulkan SDK in your current terminal session. Otherwise, the build won't work. Additionally, if you close out of your terminal, you must perform this step again if you intend to perform a build. However, there are ways to make this persistent. Refer to the Vulkan SDK guide linked in the first step for more information about any of this.
On Debian / Ubuntu, you can install the required dependencies using:
sudo apt-get install libvulkan-dev glslc spirv-headersSPIRV-Headers (spirv/unified1/spirv.hpp) are required for the Vulkan backend and are not always pulled in by the Vulkan loader dev package alone. Other distros use names such as spirv-headers (Ubuntu / Debian / Arch), or spirv-headers-devel (Fedora / openSUSE). On Windows, the LunarG Vulkan SDK’s Include directory already contains these headers.
Second, after verifying that you have followed all of the SDK installation/setup steps, use this command to make sure before proceeding:
vulkaninfoThen, assuming you have cd into your llama.cpp folder and there are no errors with running vulkaninfo, you can proceed to build llama.cpp using the CMake commands below:
cmake -B build -DGGML_VULKAN=1
cmake --build build --config ReleaseFinally, after finishing your build, you should be able to do something like this:
# Test the output binary
# "-ngl 99" should offload all of the layers to GPU for most (if not all) models.
./build/bin/llama-cli -m "PATH_TO_MODEL" -p "Hi you how are you" -ngl 99
# You should see in the output, ggml_vulkan detected your GPU. For example:
# ggml_vulkan: Using Intel(R) Graphics (ADL GT2) | uma: 1 | fp16: 1 | warp size: 32Generally, follow LunarG's Getting Started with the MacOS Vulkan SDK guide for installation and setup of the Vulkan SDK. There are two options of Vulkan drivers on macOS, both of which implement translation layers to map Vulkan to Metal. They can be hot-swapped by setting the VK_ICD_FILENAMES environment variable to point to the respective ICD JSON file.
Check the box for "KosmicKrisp" during the LunarG Vulkan SDK installation.
Set environment variable for the LunarG Vulkan SDK after installation (and optionally add to your shell profile for persistence):
source /path/to/vulkan-sdk/setup-env.shMoltenVK is the default Vulkan driver installed with the LunarG Vulkan SDK on macOS, so you can use the above environment variable settings as is.
Override the environment variable for KosmicKrisp:
export VK_ICD_FILENAMES=$VULKAN_SDK/share/vulkan/icd.d/libkosmickrisp_icd.json
export VK_DRIVER_FILES=$VULKAN_SDK/share/vulkan/icd.d/libkosmickrisp_icd.jsonThis is the only step different from above instructions.
cmake -B build -DGGML_VULKAN=1 -DGGML_METAL=OFF
cmake --build build --config ReleaseThis provides NPU acceleration using the AI cores of your Ascend NPU. And CANN is a hierarchical APIs to help you to quickly build AI applications and service based on Ascend NPU.
For more information about Ascend NPU in Ascend Community.
Make sure to have the CANN toolkit installed. You can download it from here: CANN Toolkit
Go to llama.cpp directory and build using CMake.
cmake -B build -DGGML_CANN=on -DCMAKE_BUILD_TYPE=release
cmake --build build --config releaseYou can test with:
./build/bin/llama-cli -m PATH_TO_MODEL -p "Building a website can be done in 10 steps:" -ngl 32If the following info is output on screen, you are using llama.cpp with the CANN backend:
llm_load_tensors: CANN model buffer size = 13313.00 MiB
llama_new_context_with_model: CANN compute buffer size = 1260.81 MiBFor detailed info, such as model/device supports, CANN install, please refer to llama.cpp for CANN.
ZenDNN provides optimized deep learning primitives for AMD EPYC™ CPUs. It accelerates matrix multiplication operations for inference workloads.
-
Using
CMakeon Linux (automatic build):cmake -B build -DGGML_ZENDNN=ON cmake --build build --config Release
The first build will automatically download and build ZenDNN, which may take 5-10 minutes. Subsequent builds will be much faster.
-
Using
CMakewith custom ZenDNN installation:cmake -B build -DGGML_ZENDNN=ON -DZENDNN_ROOT=/path/to/zendnn/install cmake --build build --config Release
You can test with:
./build/bin/llama-cli -m PATH_TO_MODEL -p "Building a website can be done in 10 steps:" -n 50For detailed information about hardware support, setup instructions, and performance optimization, refer to llama.cpp for ZenDNN.
KleidiAI provides optimized Arm CPU microkernels used by the ggml CPU backend. Enabling it at build time makes those kernels available; it does not force every operation to use KleidiAI. At runtime, llama.cpp selects the best compatible CPU kernel from the detected CPU features, tensor type, operation shape, and active backend priority.
Supported targets:
| Platform | Supported ABI / architecture | Notes |
|---|---|---|
| Linux | AArch64 / arm64 | Runtime CPU feature detection is automatic. |
| Android | arm64-v8a |
Use the Android NDK command below for a portable build. |
| Apple | arm64 | Runtime CPU feature detection is automatic. Non-streaming SVE vector length is treated as unavailable. |
| Windows | arm64 | Runtime CPU feature detection is automatic. SMCU count is treated as unknown until a detection path is verified. |
GGML_CPU_KLEIDIAI=ON is valid only for AArch64/arm64 builds. Do not enable it for x86, 32-bit Arm, or Android ABIs other than arm64-v8a.
From the llama.cpp source directory:
cmake -S . -B build -DGGML_CPU_KLEIDIAI=ON
cmake --build build --config ReleaseSet ANDROID_NDK to the Android NDK root, then run the following from the llama.cpp source directory. This command configures a portable Android arm64-v8a build with KleidiAI enabled and avoids Android dependencies that are not part of the NDK stable native API set.
cmake -S . -B build-android \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
-DANDROID_ABI=arm64-v8a \
-DANDROID_PLATFORM=android-28 \
-DGGML_CPU_KLEIDIAI=ON \
-DGGML_NATIVE=OFF \
-DGGML_OPENMP=OFF \
-DGGML_LLAMAFILE=OFF \
-DLLAMA_OPENSSL=OFF
cmake --build build-android --config Release --parallel
cmake --install build-android --prefix {install-dir} --config ReleaseImportant Android options:
GGML_CPU_KLEIDIAI=ONenables KleidiAI for Androidarm64-v8a.GGML_NATIVE=OFFis required for cross-compilation because the build host CPU is not the Android target CPU.GGML_OPENMP=OFFavoids adding an OpenMP runtime dependency to this NDK command-line build.GGML_LLAMAFILE=OFFavoids the llamafile backend, which is not supported on Android.LLAMA_OPENSSL=OFFavoids depending on OpenSSL, which is not part of the Android NDK stable native API set.
The Android Studio project under examples/llama.android enables KleidiAI automatically for arm64-v8a. For Android command-line CMake builds on arm64-v8a, pass -DGGML_CPU_KLEIDIAI=ON explicitly.
Global -march flags such as -march=armv8.7a flag are not required for a portable Android arm64-v8a build. Global -march flags raise the baseline instruction set for generic code. No manual architecture-specific source selection is required; llama.cpp selects compatible KleidiAI kernels at runtime. The KleidiAI libraries internal CMake handles the -march flags for each particular kernel.
Run an installed or in-tree binary:
./build/bin/llama-cli -m PATH_TO_MODEL -p "What is a car?"If KleidiAI is enabled, the output contains a line similar to:
load_tensors: CPU_KLEIDIAI model buffer size = 3474.00 MiB
This confirms that the model has tensors allocated through the KleidiAI CPU buffer. It does not prove that every operation, or any specific SME-family operation, used a KleidiAI microkernel. Runtime CPU features, tensor type, operation shape, and backend priority still control dispatch.
Depending on the build target, another backend may have higher priority than the CPU backend. To force CPU execution for a run, disable higher priority backends at build time, for example -DGGML_METAL=OFF, or use a runtime device option such as --device none where supported.
KleidiAI microkernels use Arm CPU features such as dotprod, i8mm, SVE, and SME/SME2. Build-time configuration makes the kernels available. Runtime dispatch selects a compatible kernel for the detected CPU and operation. Older or lower-feature CPUs fall back automatically to compatible kernels.
KleidiAI accelerates selected GGML_OP_MUL_MAT paths for F32 and common quantized formats. Exact coverage depends on the bundled KleidiAI version and the llama.cpp runtime selector, so unsupported tensor types, unsupported operation shapes, or higher priority backends may bypass KleidiAI even when the CPU supports the required Arm feature. This is also why a model may not use SME-family kernels on SME-capable hardware.
The current llama.cpp KleidiAI SVE selector only enables SVE kernels when the runtime SVE vector length is known to be QK8_0 bytes, currently 32 bytes. Linux and Android query this at runtime. Apple reports SVE capability separately from userspace non-streaming SVE availability, so llama.cpp treats the SVE vector length as unknown there. Windows exposes SVE feature presence but not the runtime SVE vector length used by this selector, so that value is also treated as unknown. Windows arm64 also treats SMCU count as unknown until a detection mechanism is verified.
The set of available SME-family kernels depends on the bundled KleidiAI version and the detected CPU capabilities. Production configuration does not require any KleidiAI runtime environment variables.
KleidiAI runtime environment variables are diagnostics/debug overrides, not production configuration. Leave them unset for normal use.
GGML_KLEIDIAI_SME controls SME-family kernel selection and overrides the maximum number of threads assigned to selected quantized SME-family kernels:
- Not set: use automatic runtime detection.
0: disable SME-family kernels.<n> > 0: enable compatible SME-family kernels and allow up to<n>threads for quantized SME-family kernels.
On Windows arm64, use GGML_KLEIDIAI_SME=<n> as the temporary diagnostics/debug override for SME thread-cap calibration until automatic SMCU count detection is verified.
If the CPU does not support the required SME-family capability for a bundled kernel, that kernel is disabled regardless of the environment variable.
This provides GPU acceleration through OpenCL on recent Adreno GPU. More information about OpenCL backend can be found in OPENCL.md for more information.
Assume NDK is available in $ANDROID_NDK. First, install OpenCL headers and ICD loader library if not available,
mkdir -p ~/dev/llm
cd ~/dev/llm
git clone https://github.com/KhronosGroup/OpenCL-Headers && \
cd OpenCL-Headers && \
cp -r CL $ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
cd ~/dev/llm
git clone https://github.com/KhronosGroup/OpenCL-ICD-Loader && \
cd OpenCL-ICD-Loader && \
mkdir build_ndk && cd build_ndk && \
cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
-DOPENCL_ICD_LOADER_HEADERS_DIR=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include \
-DANDROID_ABI=arm64-v8a \
-DANDROID_PLATFORM=24 \
-DANDROID_STL=c++_shared && \
ninja && \
cp libOpenCL.so $ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-androidThen build llama.cpp with OpenCL enabled,
cd ~/dev/llm
git clone https://github.com/ggml-org/llama.cpp && \
cd llama.cpp && \
mkdir build-android && cd build-android
cmake .. -G Ninja \
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
-DANDROID_ABI=arm64-v8a \
-DANDROID_PLATFORM=android-28 \
-DBUILD_SHARED_LIBS=OFF \
-DGGML_OPENCL=ON
ninjaFirst, install OpenCL headers and ICD loader library if not available,
mkdir -p ~/dev/llm
cd ~/dev/llm
git clone https://github.com/KhronosGroup/OpenCL-Headers && cd OpenCL-Headers
mkdir build && cd build
cmake .. -G Ninja `
-DBUILD_TESTING=OFF `
-DOPENCL_HEADERS_BUILD_TESTING=OFF `
-DOPENCL_HEADERS_BUILD_CXX_TESTS=OFF `
-DCMAKE_INSTALL_PREFIX="$HOME/dev/llm/opencl"
cmake --build . --target install
cd ~/dev/llm
git clone https://github.com/KhronosGroup/OpenCL-ICD-Loader && cd OpenCL-ICD-Loader
mkdir build && cd build
cmake .. -G Ninja `
-DCMAKE_BUILD_TYPE=Release `
-DCMAKE_PREFIX_PATH="$HOME/dev/llm/opencl" `
-DCMAKE_INSTALL_PREFIX="$HOME/dev/llm/opencl"
cmake --build . --target installThen build llama.cpp with OpenCL enabled,
cmake .. -G Ninja `
-DCMAKE_TOOLCHAIN_FILE="$HOME/dev/llm/llama.cpp/cmake/arm64-windows-llvm.cmake" `
-DCMAKE_BUILD_TYPE=Release `
-DCMAKE_PREFIX_PATH="$HOME/dev/llm/opencl" `
-DBUILD_SHARED_LIBS=OFF `
-DGGML_OPENCL=ON
ninjaTo read documentation for how to build on Android, click here
The WebGPU backend relies on Dawn. Follow the instructions here to install Dawn locally so that llama.cpp can find it using CMake. The current implementation is up-to-date with Dawn commit 18eb229.
In the llama.cpp directory, build with CMake:
cmake -B build -DGGML_WEBGPU=ON
cmake --build build --config Release
WebGPU allows cross-platform access to the GPU from supported browsers. We utilize Emscripten to compile ggml's WebGPU backend to WebAssembly. Emscripten does not officially support WebGPU bindings yet, but Dawn currently maintains its own WebGPU bindings called emdawnwebgpu.
Follow the instructions here to download or build the emdawnwebgpu package (Note that it might be safer to build the emdawnwebgpu package locally, so that it stays in sync with the version of Dawn you have installed above). When building using CMake, the path to the emdawnwebgpu port file needs to be set with the flag EMDAWNWEBGPU_DIR.
To read documentation for how to build on IBM Z & LinuxONE, click here
OpenVINO is an open-source toolkit for optimizing and deploying high-performance AI inference, specifically designed for Intel hardware (CPUs, GPUs, and NPUs).
For build instructions and usage examples, refer to OPENVINO.md.
The GPU may still be used to accelerate some parts of the computation even when using the -ngl 0 option. You can fully disable GPU acceleration by using --device none.
In most cases, it is possible to build and use multiple backends at the same time. For example, you can build llama.cpp with both CUDA and Vulkan support by using the -DGGML_CUDA=ON -DGGML_VULKAN=ON options with CMake. At runtime, you can specify which backend devices to use with the --device option. To see a list of available devices, use the --list-devices option.
Backends can be built as dynamic libraries that can be loaded dynamically at runtime. This allows you to use the same llama.cpp binary on different machines with different GPUs. To enable this feature, use the GGML_BACKEND_DL option when building.