llama.cpp 安裝使用(支持CPU、Metal及CUDA的單卡/多卡推理)

1. 詳細步驟

1.1 安裝 cuda 等 nvidia 依賴(非CUDA環(huán)境運行可跳過)

# 以 CUDA Toolkit 12.4: Ubuntu-22.04/24.04(x86_64) 為例,注意區(qū)分 WSL 和 Ubuntu,詳見 https://developer.nvidia.com/cuda-12-4-1-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=runfile_local
wget https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda_12.4.1_550.54.15_linux.run
sudo sh cuda_12.4.1_550.54.15_linux.run

注:通過 nvcc -V 來確定 cuda 等依賴是否安裝到位,缺少的話根據(jù)提示再安裝別的依賴,如 sudo apt install nvidia-cuda-toolkit 等

1.2 安裝 llama.cpp (C/C++環(huán)境)

# 手動下載也可以
git clone https://github.com/ggerganov/llama.cpp

cd llama.cpp

# 沒安裝 make,通過 brew/apt 安裝一下(cmake 也可以,但是沒有 make 命令更簡潔)
# Metal(MPS)/CPU
make
# CUDA
make GGML_CUDA=1

注:以前的版本好像一直編譯挺快的,現(xiàn)在最新的版本CUDA上編譯有點慢,多等一會

1.3 安裝 llama-cpp (Python 環(huán)境)

# 也可以手動安裝 torch 之后,再安裝剩下的依賴
pip install -r requirements.txt

1.4 轉(zhuǎn)換 HF 模型為 GGUF 文件

本步驟開始都以 Qwen2-7B-Instruct 為例

# 示例: 生成 FP-16 模型
python convert_hf_to_gguf.py /model_path/Qwen/Qwen-2.7B-Instruct/

1.5 GGUF 模型量化

# 示例: Q4_K_M 量化
./llama-quantize /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-f16.gguf /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-Q4_K_M.gguf Q4_K_M

1.6 GGUF 模型測試

# Metal(MPS)/CPU
./llama-cli -m /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-Q4_K_M.gguf -cnv -p "You are a helpful assistant"
# CUDA: 單卡推理
./llama-cli -m /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-Q4_K_M.gguf -cnv -p "You are a helpful assistant" -ngl 9999
# CUDA: 多卡推理(以雙卡為例),-ts等參數(shù)含義詳見 https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md
./llama-cli -m /model_path/Qwen/Qwen-2.7B-Instruct/ggml-model-Q4_K_M.gguf -cnv -p "You are a helpful assistant" -ngl 9999 -ts 1,1

注: ngl可以靈活調(diào)整,取 9999 不是常規(guī)做法,比大模型實際的 100 以內(nèi)的 ngl 大很多(不同模型的實際 ngl 也不一樣)來確保所有的 ngl 都在 GPU 上運行(當(dāng)然前提是顯存足夠)

2. 參考資料

2.1 llama.cpp

2.1.1 GitHub

官方頁面

https://github.com/ggerganov/llama.cpp

build

https://github.com/ggerganov/llama.cpp/blob/master/docs/build.md

quantize

https://github.com/ggerganov/llama.cpp/blob/master/examples/quantize/README.md

http server

https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md

2.2 NVIDIA DEVELOPER

CUDA Toolkit Archive

https://developer.nvidia.com/cuda-toolkit-archive

CUDA Toolkit 12.4: Ubuntu-22.04/24.04(x86_64)

https://developer.nvidia.com/cuda-12-4-1-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=runfile_local

3. 資源

3.1 llama.cpp

3.1.1 GitHub

Python Bindings for llama.cpp

https://github.com/abetlen/llama-cpp-python

3.2 NVIDIA DEVELOPER

官方頁面

https://developer.nvidia.com/

CUDA Toolkit

https://developer.nvidia.com/cuda-downloads

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容