llama.cpp 是一个C++库,用于简化LLM推理的设置,它使得在本地机器上运行大模型(GGUF格式)成为可能。
官网:https://github.com/ggerganov/llama.cpp
模型库:
https://huggingface.co/
HF-Mirror
魔搭社区
安装并且使用llama.cpp
0.安装llama.cpp
官方文档:https://github.com/ggerganov/llama.cpp/blob/master/docs/build.md
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make -j 8
1. 下载模型(HF)
# obtain the official LLaMA model weights and place them in ./models/mymodels
sudo yum install git-lfs
git lfs install
git clone 魔搭社区
2. 转换与量化
官方文档:https://github.com/ggerganov/llama.cpp/blob/master/examples/quantize/README.md
将HuggingFace的safetensors格式的模型文件转换成gguf格式才能使用llama.cpp推理。最新版本只能用convert-hf-to-gguf.py来转换,convert.py已经过期了。
#install Python dependencies
python -m pip install -r requirements.txt
#convert the model to ggml FP16 format
python3 convert_hf_to_gguf.py ./models/mymodels/glm-4-9b-chat
#quantize the model to 4-bits (using Q4_K_M method)
./llama-quantize ./models/mymodels/glm-4-9b-chat/glm-4-9B-chat-F16.gguf ./models/mymodels/glm-4-9b-chat/glm-4-9B-chat_Q4_K_M.gguf Q4_K_M
3.模型加载与推理
官方文档: https://github.com/ggerganov/llama.cpp
#You can run a basic completion using this command
./llama-cli -m ./models/mymodels/glm-4-9B-chat/glm-4-9B-chat_Q4_K_M.gguf -p "I believe the meaning of life is" -n 2048
#Like ChatGPT-like experience, you can run in conversation mode by passing -cnv as a parameter
./llama-cli -m ./models/mymodels/glm-4-9B-chat/glm-4-9B-chat_Q4_K_M.gguf -cnv -c 2048 --temp 0.2 -n 2048 -p "你是一个作家助手"
# If you want to use another chat template, pass --chat-template NAME as a parameter../llama-cli -m ./models/mymodels/glm-4-9B-chat/glm-4-9B-chat_Q4_K_M.gguf -p "You are a helpful assistant" -cnv --chat-template chatml -cnv -c 2048 --temp 0.2 -n 2048
#Web server
./llama-server -m ./models/mymodels/gemma_2_chinese_chat_q8_0.gguf --port 8080 --host 192.168.19.180 -c 2048 --temp 0.2 -n 2048
# Basic web UI can be accessed via browser: http://192.168.19.180:18080
# Chat completion endpoint: http://192.168.19.180:18080/v1/chat/completions
#关闭web server
使用 Ctrl+C 组合键 或者 ps aux | grep llama-server 再kill.