1. 克隆并安装 llama.cpp
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
安装 Python 依赖(如需要):
pip install -r requirements.txt
或者自行安装所需依赖(如 torch
、transformers
等)。
2. 转换模型为 GGUF 格式
llama.cpp 提供几个转换脚本,可根据使用场景选择:
convert_hf_to_gguf.py
:将 Hugging Face Transformer 格式转换成 GGUFconvert_hf_to_gguf_update.py
:升级已转换过的旧格式至 GGUFconvert_llama_ggml_to_gguf.py
:将已有 LLAMA GGML 格式模型转换为 GGUFconvert_lora_to_gguf.py
:将 LoRA 格式转换为 GGUF
示例(将 Hugging Face 模型转换为 GGUF):
python convert_hf_to_gguf.py --model /path/to/hf/model -o /path/to/output/gguf-model
完成后,可以在 /path/to/output/
目录找到生成的 .gguf
文件。
在 convert_hf_to_gguf.py
中,可通过 --target
参数指定量化方式,一般可选:
q2_k
,q3_k
,q4_0
,q4_1
,q5_0
,q5_1
,q8_0
等
也可根据需要设定其他参数,如 --outfile
重新命名输出、--batch-size
优化转换速度等。示例:
python convert_hf_to_gguf.py \
--model /path/to/hf/model \
--outfile /path/to/output/model.gguf \
--target q4_0 \
--batch-size 1024
完成后即可得到指定量化方式的 .gguf
文件
3. 在 Ollama 部署 GGUF 模型
- 创建一个名为
Modelfile
的文件,内容类似:FROM ./your-model.gguf
- 通过以下命令将
.gguf
模型文件导入到 Ollama,这里example是你模型的名字ollama create example -f Modelfile
- 部署并运行模型:
ollama run example
以上就是从 Hugging Face 格式到 GGUF,并在 Ollama 中部署的基本流程。可根据实际需求对量化类型(Q4_0、Q4_1、Q8_0 等)或脚本参数进行修改,以获得更合适的模型体积与性能表现。