lama.cpp是一个高效的机器学习推理库,目标是在各种硬件上实现LLM推断,保持最小设置和最先进性能。llama.cpp支持1.5位、2位、3位、4位、5位、6位和8位整数量化,通过ARM NEON、Accelerate和Metal支持Apple芯片,使得在MAC M1处理器上运行Deepseek大模型成为可能。
1 下载llama.cpp
git clone https://github.com/ggerganov/llama.cpp.git
如果clone异常,直接下载release版本,链接如下,然后解压缩https://github.com/ggml-org/llama.cpp/archive/refs/tags/b5857.tar.gz
2 安装llama.cpp
创建环境
conda create -n llama.cpp python=3.12
conda activate llama.cpp
安装依赖
pip install -r requirements.txt
编译
conda install cmake
mkdir build
cd build
cmake .. -DLLAMA_METAL=ON
cmake --build . --config Release
-DLLAMA_METAL=ON
启用Metal支持,利用mac的GPU加速
3 测试llama.cpp
1) GGUF转化
提前下载hf格式的模型文件../DeepSeek-R1-Distill-Qwen-7B,将模型文件转化为GGUF模型文件。GGUF是一种用于GGML推断的文件格式。转化代码convert_hf_to_gguf.py在llama.cpp主目录。
cd .. # 切换到llama.cpp主目录
python convert_hf_to_gguf.py ../DeepSeek-R1-Distill-Qwen-7B
转化后GGUF模型文件../DeepSeek-R1-Distill-Qwen-7B/DeepSeek-R1-Distill-Qwen-7B-F16.gguf
2) int4量化
刚编译好的量化程序llama-quantize在build/bin目录。
cd build
./bin/llama-quantize ../../DeepSeek-R1-Distill-Qwen-7B/DeepSeek-R1-Distill-Qwen-7B-F16.gguf ../../DeepSeek-R1-Distill-Qwen-7B/model-q4_0.gguf Q4_0
量化后的gguf int4文件../../DeepSeek-R1-Distill-Qwen-7B/model-q4_0.gguf
3)测试量化
量化推理命令/llama-cli也在build/bin目录,运行示例如下。
./bin/llama-cli -m ../../DeepSeek-R1-Distill-Qwen-7B/model-q4_0.gguf -p "你好?新加坡首都在哪里" -n 128
reference
---
llama.cpp release
https://github.com/ggml-org/llama.cpp/releases
llama.cpp里面的Q8_0,Q6_K_M,Q4_K_M量化原理是什么?
https://www.zhihu.com/question/633365088
[LLM-Llama]在 MAC M1上体验Llama.cpp和通义千问Qwen 1.5-7B