当大语言模型训练好后,一般是model.safetensors,类型,很多时候,需要导出为GGUF格式,并且需要进行量化,此时就要用到llama.cpp
流程步骤
- 更新系统
1 2
| sudo apt update sudo apt upgrade -y
|
- 安装基础依赖
1 2 3 4 5 6 7 8
| sudo apt install -y \ git \ build-essential \ cmake \ python3 \ python3-pip \ wget \ unzip
|
- 获取 llama.cpp
1 2
| git clone https://github.com/ggerganov/llama.cpp cd llama.cpp
|
1 2 3 4
| wget https://codeload.github.com/ggerganov/llama.cpp/zip/refs/heads/master -O llama.zip unzip llama.zip mv llama.cpp-master llama.cpp cd llama.cpp
|
- 编译
先编译 CPU 版本(稳定)
1 2
| cmake -B build cmake --build build -j4
|
编译完成后关键文件在build/bin/,主要用llama-quantize这个工具
- 转换为 GGUF
1 2 3 4
| cd ~/llama.cpp python3 convert_hf_to_gguf.py \ models/Qwen2-7B \ --outfile models/qwen2-7b-f16.gguf
|
不可直接从 safetensors 转 q4,必须先生成 f16 或 f32
- 量化为 q4_k_m
1 2 3 4
| ./build/bin/llama-quantize \ models/qwen2-7b-f16.gguf \ models/qwen2-7b-q4_k_m.gguf \ q4_k_m
|