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 gguf主要量化方法
https://blog.csdn.net/liliang199/article/details/149254634
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
https://juejin.cn/post/7371365854012293131