Team Ai
Apppublic

KBaba7/llama.cpp

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
build.md409 linesDownload Raw Back to docs
1# Build llama.cpp locally2 3**To get the Code:**4 5```bash6git clone https://github.com/ggerganov/llama.cpp7cd llama.cpp8```9 10The following sections describe how to build with different backends and options.11 12## CPU Build13 14Build llama.cpp using `CMake`:15 16```bash17cmake -B build18cmake --build build --config Release19```20 21**Notes**:22 23- For faster compilation, add the `-j` argument 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 8` will run 8 jobs in parallel.24- For faster repeated compilation, install [ccache](https://ccache.dev/)25- For debug builds, there are two cases:26 27    1. Single-config generators (e.g. default = `Unix Makefiles`; note that they just ignore the `--config` flag):28 29       ```bash30       cmake -B build -DCMAKE_BUILD_TYPE=Debug31       cmake --build build32       ```33 34    2. Multi-config generators (`-G` param set to Visual Studio, XCode...):35 36       ```bash37       cmake -B build -G "Xcode"38       cmake --build build --config Debug39       ```40 41    For more details and a list of supported generators, see the [CMake documentation](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html).42- For static builds, add `-DBUILD_SHARED_LIBS=OFF`:43  ```44  cmake -B build -DBUILD_SHARED_LIBS=OFF45  cmake --build build --config Release46  ```47 48- Building for Windows (x86, x64 and arm64) with MSVC or clang as compilers:49    - Install Visual Studio 2022, e.g. via the [Community Edition](https://visualstudio.microsoft.com/de/vs/community/). In the installer, select at least the following options (this also automatically installs the required additional tools like CMake,...):50    - Tab Workload: Desktop-development with C++51    - 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)52    - Please remember to always use a Developer Command Prompt / PowerShell for VS2022 for git, build, test53    - For Windows on ARM (arm64, WoA) build with:54    ```bash55    cmake --preset arm64-windows-llvm-release -D GGML_OPENMP=OFF56    cmake --build build-arm64-windows-llvm-release57    ```58    Building for arm64 can also be done with the MSVC compiler with the build-arm64-windows-MSVC preset, or the standard CMake build instructions. However, note that the MSVC compiler does not support inline ARM assembly code, used e.g. for the accelerated Q4_0_N_M CPU kernels.59 60    For building with ninja generator and clang compiler as default:61      -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\x6462      ```bash63      cmake --preset x64-windows-llvm-release64      cmake --build build-x64-windows-llvm-release65      ```66 67## BLAS Build68 69Building 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:70 71### Accelerate Framework72 73This is only available on Mac PCs and it's enabled by default. You can just build using the normal instructions.74 75### OpenBLAS76 77This provides BLAS acceleration using only the CPU. Make sure to have OpenBLAS installed on your machine.78 79- Using `CMake` on Linux:80 81    ```bash82    cmake -B build -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS83    cmake --build build --config Release84    ```85 86### BLIS87 88Check [BLIS.md](./backend/BLIS.md) for more information.89 90### Intel oneMKL91 92Building 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](./backend/SYCL.md).93 94- Using manual oneAPI installation:95  By default, `GGML_BLAS_VENDOR` is set to `Generic`, so if you already sourced intel environment script and assign `-DGGML_BLAS=ON` in cmake, the mkl version of Blas will automatically been selected. Otherwise please install oneAPI and follow the below steps:96    ```bash97    source /opt/intel/oneapi/setvars.sh # You can skip this step if  in oneapi-basekit docker image, only required for manual installation98    cmake -B build -DGGML_BLAS=ON -DGGML_BLAS_VENDOR=Intel10_64lp -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DGGML_NATIVE=ON99    cmake --build build --config Release100    ```101 102- Using oneAPI docker image:103  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](https://hub.docker.com/r/intel/oneapi-basekit). Then, you can use the commands given above.104 105Check [Optimizing and Running LLaMA2 on Intel® CPU](https://www.intel.com/content/www/us/en/content-details/791610/optimizing-and-running-llama2-on-intel-cpu.html) for more information.106 107### Other BLAS libraries108 109Any other BLAS library can be used by setting the `GGML_BLAS_VENDOR` option. See the [CMake documentation](https://cmake.org/cmake/help/latest/module/FindBLAS.html#blas-lapack-vendors) for a list of supported vendors.110 111## Metal Build112 113On MacOS, Metal is enabled by default. Using Metal makes the computation run on the GPU.114To disable the Metal build at compile time use the `-DGGML_METAL=OFF` cmake option.115 116When built with Metal support, you can explicitly disable GPU inference with the `--n-gpu-layers 0` command-line argument.117 118## SYCL119 120SYCL is a higher-level programming model to improve programming productivity on various hardware accelerators.121 122llama.cpp based on SYCL is used to **support Intel GPU** (Data Center Max series, Flex series, Arc series, Built-in GPU and iGPU).123 124For detailed info, please refer to [llama.cpp for SYCL](./backend/SYCL.md).125 126## CUDA127 128This provides GPU acceleration using an NVIDIA GPU. Make sure to have the [CUDA toolkit](https://developer.nvidia.com/cuda-toolkit) installed.129 130#### Download directly from NVIDIA131You may find the official downloads here: [NVIDIA developer site](https://developer.nvidia.com/cuda-downloads).132 133 134#### Compile and run inside a Fedora Toolbox Container135We also have a [guide](./cuda-fedora.md) for setting up CUDA toolkit in a Fedora [toolbox container](https://containertoolbx.org/).136 137**Recommended for:**138 139- ***Particularly*** *convenient* for users of [Atomic Desktops for Fedora](https://fedoraproject.org/atomic-desktops/); such as: [Silverblue](https://fedoraproject.org/atomic-desktops/silverblue/) and [Kinoite](https://fedoraproject.org/atomic-desktops/kinoite/).140- Toolbox is installed by default: [Fedora Workstation](https://fedoraproject.org/workstation/) or [Fedora KDE Plasma Desktop](https://fedoraproject.org/spins/kde).141- *Optionally* toolbox packages are available: [Arch Linux](https://archlinux.org/), [Red Hat Enterprise Linux >= 8.5](https://www.redhat.com/en/technologies/linux-platforms/enterprise-linux), or [Ubuntu](https://ubuntu.com/download)142 143 144### Compilation145```bash146cmake -B build -DGGML_CUDA=ON147cmake --build build --config Release148```149 150### Override Compute Capability Specifications151 152If `nvcc` cannot detect your gpu, you may get compile-warnings such as:153 ```text154nvcc warning : Cannot find valid GPU for '-arch=native', default arch is used155```156 157To override the `native` GPU detection:158 159#### 1. Take note of the `Compute Capability` of your NVIDIA devices: ["CUDA: Your GPU Compute > Capability"](https://developer.nvidia.com/cuda-gpus).160 161```text162GeForce RTX 4090      8.9163GeForce RTX 3080 Ti   8.6164GeForce RTX 3070      8.6165```166 167#### 2. Manually list each varying `Compute Capability` in the `CMAKE_CUDA_ARCHITECTURES` list.168 169```bash170cmake -B build -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES="86;89"171```172 173### Runtime CUDA environmental variables174 175You may set the [cuda environmental variables](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#env-vars) at runtime.176 177```bash178# Use `CUDA_VISIBLE_DEVICES` to hide the first compute device.179CUDA_VISIBLE_DEVICES="-0" ./build/bin/llama-server --model /srv/models/llama.gguf180```181 182### Unified Memory183 184The 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`.185 186### Performance Tuning187 188The following compilation options are also available to tweak performance:189 190| Option                        | Legal values           | Default | Description                                                                                                                                                                                                                                                                             |191|-------------------------------|------------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|192| 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, 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.                       |193| GGML_CUDA_FORCE_CUBLAS        | Boolean                | false   | Force the use of FP16 cuBLAS instead of custom matrix multiplication kernels for quantized models                                                                                                                                                                                       |194| GGML_CUDA_F16                 | Boolean                | false   | If enabled, use half-precision floating point arithmetic for the CUDA dequantization + mul mat vec kernels and for the q4_1 and q5_1 matrix matrix multiplication kernels. Can improve performance on relatively recent GPUs.                                                           |195| 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.                                                                         |196| 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.                                                                                                  |197 198## MUSA199 200This provides GPU acceleration using the MUSA cores of your Moore Threads MTT GPU. Make sure to have the MUSA SDK installed. You can download it from here: [MUSA SDK](https://developer.mthreads.com/sdk/download/musa).201 202- Using `CMake`:203 204  ```bash205  cmake -B build -DGGML_MUSA=ON206  cmake --build build --config Release207  ```208 209The environment variable [`MUSA_VISIBLE_DEVICES`](https://docs.mthreads.com/musa-sdk/musa-sdk-doc-online/programming_guide/Z%E9%99%84%E5%BD%95/) can be used to specify which GPU(s) will be used.210 211The 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.212 213Most of the compilation options available for CUDA should also be available for MUSA, though they haven't been thoroughly tested yet.214 215## HIP216 217This provides GPU acceleration on HIP-supported AMD GPUs.218Make sure to have ROCm installed.219You can download it from your Linux distro's package manager or from here: [ROCm Quick Start (Linux)](https://rocm.docs.amd.com/projects/install-on-linux/en/latest/tutorial/quick-start.html#rocm-install-quick).220 221- Using `CMake` for Linux (assuming a gfx1030-compatible AMD GPU):222  ```bash223  HIPCXX="$(hipconfig -l)/clang" HIP_PATH="$(hipconfig -R)" \224      cmake -S . -B build -DGGML_HIP=ON -DAMDGPU_TARGETS=gfx1030 -DCMAKE_BUILD_TYPE=Release \225      && cmake --build build --config Release -- -j 16226  ```227  On Linux it is also possible to use unified memory architecture (UMA) to share main memory between the CPU and integrated GPU by setting `-DGGML_HIP_UMA=ON`.228  However, this hurts performance for non-integrated GPUs (but enables working with integrated GPUs).229 230  Note that if you get the following error:231  ```232  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 library233  ```234  Try searching for a directory under `HIP_PATH` that contains the file235  `oclc_abi_version_400.bc`. Then, add the following to the start of the236  command: `HIP_DEVICE_LIB_PATH=<directory-you-just-found>`, so something237  like:238  ```bash239  HIPCXX="$(hipconfig -l)/clang" HIP_PATH="$(hipconfig -p)" \240  HIP_DEVICE_LIB_PATH=<directory-you-just-found> \241      cmake -S . -B build -DGGML_HIP=ON -DAMDGPU_TARGETS=gfx1030 -DCMAKE_BUILD_TYPE=Release \242      && cmake --build build -- -j 16243  ```244 245- Using `CMake` for Windows (using x64 Native Tools Command Prompt for VS, and assuming a gfx1100-compatible AMD GPU):246  ```bash247  set PATH=%HIP_PATH%\bin;%PATH%248  cmake -S . -B build -G Ninja -DAMDGPU_TARGETS=gfx1100 -DGGML_HIP=ON -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release249  cmake --build build250  ```251  Make sure that `AMDGPU_TARGETS` is set to the GPU arch you want to compile for. The above example uses `gfx1100` that corresponds to Radeon RX 7900XTX/XT/GRE. You can find a list of targets [here](https://llvm.org/docs/AMDGPUUsage.html#processors)252  Find your gpu version string by matching the most significant version information from `rocminfo | grep gfx | head -1 | awk '{print $2}'` with the list of processors, e.g. `gfx1035` maps to `gfx1030`.253 254 255The environment variable [`HIP_VISIBLE_DEVICES`](https://rocm.docs.amd.com/en/latest/understand/gpu_isolation.html#hip-visible-devices) can be used to specify which GPU(s) will be used.256If 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.257 258## Vulkan259 260**Windows**261 262### w64devkit263 264Download and extract [`w64devkit`](https://github.com/skeeto/w64devkit/releases).265 266Download and install the [`Vulkan SDK`](https://vulkan.lunarg.com/sdk/home#windows) with the default settings.267 268Launch `w64devkit.exe` and run the following commands to copy Vulkan dependencies:269```sh270SDK_VERSION=1.3.283.0271cp /VulkanSDK/$SDK_VERSION/Bin/glslc.exe $W64DEVKIT_HOME/bin/272cp /VulkanSDK/$SDK_VERSION/Lib/vulkan-1.lib $W64DEVKIT_HOME/x86_64-w64-mingw32/lib/273cp -r /VulkanSDK/$SDK_VERSION/Include/* $W64DEVKIT_HOME/x86_64-w64-mingw32/include/274cat > $W64DEVKIT_HOME/x86_64-w64-mingw32/lib/pkgconfig/vulkan.pc <<EOF275Name: Vulkan-Loader276Description: Vulkan Loader277Version: $SDK_VERSION278Libs: -lvulkan-1279EOF280 281```282 283Switch into the `llama.cpp` directory and build using CMake.284```sh285cmake -B build -DGGML_VULKAN=ON286cmake --build build --config Release287```288 289### Git Bash MINGW64290 291Download and install [`Git-SCM`](https://git-scm.com/downloads/win) with the default settings292 293Download and install [`Visual Studio Community Edition`](https://visualstudio.microsoft.com/) and make sure you select `C++`294 295Download and install [`CMake`](https://cmake.org/download/) with the default settings296 297Download and install the [`Vulkan SDK`](https://vulkan.lunarg.com/sdk/home#windows) with the default settings.298 299Go into your `llama.cpp` directory and right click, select `Open Git Bash Here` and then run the following commands300 301```302cmake -B build -DGGML_VULKAN=ON303cmake --build build --config Release304```305 306Now you can load the model in conversation mode using `Vulkan`307 308```sh309build/bin/Release/llama-cli -m "[PATH TO MODEL]" -ngl 100 -c 16384 -t 10 -n -2 -cnv310```311 312### MSYS2313Install [MSYS2](https://www.msys2.org/) and then run the following commands in a UCRT terminal to install dependencies.314```sh315pacman -S git \316    mingw-w64-ucrt-x86_64-gcc \317    mingw-w64-ucrt-x86_64-cmake \318    mingw-w64-ucrt-x86_64-vulkan-devel \319    mingw-w64-ucrt-x86_64-shaderc320```321 322Switch into the `llama.cpp` directory and build using CMake.323```sh324cmake -B build -DGGML_VULKAN=ON325cmake --build build --config Release326```327 328**With docker**:329 330You don't need to install Vulkan SDK. It will be installed inside the container.331 332```sh333# Build the image334docker build -t llama-cpp-vulkan --target light -f .devops/vulkan.Dockerfile .335 336# Then, use it:337docker 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 33338```339 340**Without docker**:341 342Firstly, you need to make sure you have installed [Vulkan SDK](https://vulkan.lunarg.com/doc/view/latest/linux/getting_started_ubuntu.html)343 344For example, on Ubuntu 22.04 (jammy), use the command below:345 346```bash347wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | apt-key add -348wget -qO /etc/apt/sources.list.d/lunarg-vulkan-jammy.list https://packages.lunarg.com/vulkan/lunarg-vulkan-jammy.list349apt update -y350apt-get install -y vulkan-sdk351# To verify the installation, use the command below:352vulkaninfo353```354 355Alternatively your package manager might be able to provide the appropriate libraries.356For example for Ubuntu 22.04 you can install `libvulkan-dev` instead.357For Fedora 40, you can install `vulkan-devel`, `glslc` and `glslang` packages.358 359Then, build llama.cpp using the cmake command below:360 361```bash362cmake -B build -DGGML_VULKAN=1363cmake --build build --config Release364# Test the output binary (with "-ngl 33" to offload all layers to GPU)365./bin/llama-cli -m "PATH_TO_MODEL" -p "Hi you how are you" -n 50 -e -ngl 33 -t 4366 367# You should see in the output, ggml_vulkan detected your GPU. For example:368# ggml_vulkan: Using Intel(R) Graphics (ADL GT2) | uma: 1 | fp16: 1 | warp size: 32369```370 371## CANN372This provides NPU acceleration using the AI cores of your Ascend NPU. And [CANN](https://www.hiascend.com/en/software/cann) is a hierarchical APIs to help you to quickly build AI applications and service based on Ascend NPU.373 374For more information about Ascend NPU in [Ascend Community](https://www.hiascend.com/en/).375 376Make sure to have the CANN toolkit installed. You can download it from here: [CANN Toolkit](https://www.hiascend.com/developer/download/community/result?module=cann)377 378Go to `llama.cpp` directory and build using CMake.379```bash380cmake -B build -DGGML_CANN=on -DCMAKE_BUILD_TYPE=release381cmake --build build --config release382```383 384You can test with:385 386```bash387./build/bin/llama-cli -m PATH_TO_MODEL -p "Building a website can be done in 10 steps:" -ngl 32388```389 390If the following info is output on screen, you are using `llama.cpp` with the CANN backend:391```bash392llm_load_tensors:       CANN model buffer size = 13313.00 MiB393llama_new_context_with_model:       CANN compute buffer size =  1260.81 MiB394```395 396For detailed info, such as model/device supports, CANN install, please refer to [llama.cpp for CANN](./backend/CANN.md).397 398## Android399 400To read documentation for how to build on Android, [click here](./android.md)401 402## Notes about GPU-accelerated backends403 404The 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`.405 406In 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.407 408Backends 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.409