ToB企服应用市场:ToB评测及商务社交产业平台

标题: llama.cpp在各个操作体系当地编译流程 [打印本页]

作者: 用多少眼泪才能让你相信    时间: 2025-2-13 08:15
标题: llama.cpp在各个操作体系当地编译流程
Build llama.cpp locally


To get the Code:
  1. git clone https://github.com/ggerganov/llama.cpp
  2. cd llama.cpp
复制代码
The following sections describe how to build with different backends and options.
CPU Build


Build llama.cpp using CMake:
  1. cmake -B build
  2. cmake --build build --config Release
复制代码
Notes:

BLAS Build


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:
Accelerate Framework


This is only available on Mac PCs and it's enabled by default. You can just build using the normal instructions.
OpenBLAS


This provides BLAS acceleration using only the CPU. Make sure to have OpenBLAS installed on your machine.

BLIS


Check BLIS.md for more information.
Intel oneMKL


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.

Check Optimizing and Running LLaMA2 on Intel® CPU for more information.
Other BLAS libraries


Any other BLAS library can be used by setting the GGML_BLAS_VENDOR option. See the CMake documentation for a list of supported vendors.
Metal Build


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


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.
CUDA


This provides GPU acceleration using an NVIDIA GPU. Make sure to have the CUDA toolkit installed. You can download it from your Linux distro's package manager (e.g. apt install nvidia-cuda-toolkit) or from the NVIDIA developer site.
If you are using Fedora (using Fedora Workstation, or an 'Atomic' variant such as Silverblue), or would like to set up CUDA in a toolbox, please consider our Fedora CUDA guide. Unfortunately, the process is not as simple as one might expect.

The environment variable CUDA_VISIBLE_DEVICES can be used to specify which GPU(s) will be used.
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 following compilation options are also available to tweak performance:
OptionLegal valuesDefaultDescriptionGGML_CUDA_FORCE_MMQBooleanfalseForce 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.GGML_CUDA_FORCE_CUBLASBooleanfalseForce the use of FP16 cuBLAS instead of custom matrix multiplication kernels for quantized modelsGGML_CUDA_F16BooleanfalseIf 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.GGML_CUDA_PEER_MAX_BATCH_SIZEPositive integer128Maximum 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_QUANTSBooleanfalseCompile 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. MUSA


This 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.

The environment variable MUSA_VISIBLE_DEVICES can be used to specify which GPU(s) will be used.
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.
Most of the compilation options available for CUDA should also be available for MUSA, though they haven't been thoroughly tested yet.
HIP


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).

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.
Vulkan


Windows
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:
  1. SDK_VERSION=1.3.283.0
  2. cp /VulkanSDK/$SDK_VERSION/Bin/glslc.exe $W64DEVKIT_HOME/bin/
  3. cp /VulkanSDK/$SDK_VERSION/Lib/vulkan-1.lib $W64DEVKIT_HOME/x86_64-w64-mingw32/lib/
  4. cp -r /VulkanSDK/$SDK_VERSION/Include/* $W64DEVKIT_HOME/x86_64-w64-mingw32/include/
  5. cat > $W64DEVKIT_HOME/x86_64-w64-mingw32/lib/pkgconfig/vulkan.pc <<EOF
  6. Name: Vulkan-Loader
  7. Description: Vulkan Loader
  8. Version: $SDK_VERSION
  9. Libs: -lvulkan-1
  10. EOF
复制代码
Switch into the llama.cpp directory and build using CMake.
  1. cmake -B build -DGGML_VULKAN=ON
  2. cmake --build build --config Release
复制代码
Git 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
  1. cmake -B build -DGGML_VULKAN=ON
  2. cmake --build build --config Release
复制代码
Now you can load the model in conversation mode using Vulkan
  1. build/bin/Release/llama-cli -m "[PATH TO MODEL]" -ngl 100 -c 16384 -t 10 -n -2 -cnv
复制代码
MSYS2


Install MSYS2 and then run the following commands in a UCRT terminal to install dependencies.
  1. pacman -S git \
  2.     mingw-w64-ucrt-x86_64-gcc \
  3.     mingw-w64-ucrt-x86_64-cmake \
  4.     mingw-w64-ucrt-x86_64-vulkan-devel \
  5.     mingw-w64-ucrt-x86_64-shaderc
复制代码
Switch into the llama.cpp directory and build using CMake.
  1. cmake -B build -DGGML_VULKAN=ON
  2. cmake --build build --config Release
复制代码
With docker:
You don't need to install Vulkan SDK. It will be installed inside the container.
  1. # Build the image
  2. docker build -t llama-cpp-vulkan --target light -f .devops/vulkan.Dockerfile .
  3. # Then, use it:
  4. 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 33
复制代码
Without docker:
Firstly, you need to make sure you have installed Vulkan SDK
For example, on Ubuntu 22.04 (jammy), use the command below:
  1. wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | apt-key add -
  2. wget -qO /etc/apt/sources.list.d/lunarg-vulkan-jammy.list https://packages.lunarg.com/vulkan/lunarg-vulkan-jammy.list
  3. apt update -y
  4. apt-get install -y vulkan-sdk
  5. # To verify the installation, use the command below:
  6. vulkaninfo
复制代码
Alternatively your package manager might be able to provide the appropriate libraries. For example for Ubuntu 22.04 you can install libvulkan-dev instead. For Fedora 40, you can install vulkan-devel, glslc and glslang packages.
Then, build llama.cpp using the cmake command below:
  1. cmake -B build -DGGML_VULKAN=1
  2. cmake --build build --config Release
  3. # Test the output binary (with "-ngl 33" to offload all layers to GPU)
  4. ./bin/llama-cli -m "PATH_TO_MODEL" -p "Hi you how are you" -n 50 -e -ngl 33 -t 4
  5. # You should see in the output, ggml_vulkan detected your GPU. For example:
  6. # ggml_vulkan: Using Intel(R) Graphics (ADL GT2) | uma: 1 | fp16: 1 | warp size: 32
复制代码
CANN


This 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.
  1. cmake -B build -DGGML_CANN=on -DCMAKE_BUILD_TYPE=release
  2. cmake --build build --config release
复制代码
You can test with:
  1. ./build/bin/llama-cli -m PATH_TO_MODEL -p "Building a website can be done in 10 steps:" -ngl 32
复制代码
If the following info is output on screen, you are using llama.cpp with the CANN backend:
  1. llm_load_tensors:       CANN model buffer size = 13313.00 MiB
  2. llama_new_context_with_model:       CANN compute buffer size =  1260.81 MiB
复制代码
For detailed info, such as model/device supports, CANN install, please refer to llama.cpp for CANN.
Android


To read documentation for how to build on Android, click here
Notes about GPU-accelerated backends


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.

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4