概述
llama.cpp 是一个高性能的 LLM 推理库,支持在各种硬件(包括 CPU 和 GPU)上运行量化后的大语言模型。本文档详细介绍如何使用 llama.cpp 将 HuggingFace 格式的模型转换为 GGUF 格式,并进行不同程度的量化。
GGUF 格式:GGUF(Georgi Gerganov Universal Format)是 llama.cpp 专门设计的模型文件格式,针对快速加载和保存模型进行了优化,支持单文件部署,包含加载模型所需的所有信息,无需依赖外部文件。
1. 安装 CMake
CMake 是跨平台的构建工具,用于编译 llama.cpp 项目。
下载地址:https://cmake.org/download/
安装建议:
- Windows 用户建议下载
cmake-3.x.x-windows-x86_64.msi安装包 - 安装时选择 'Add CMake to the system PATH',以便在命令行中直接使用
验证安装:
cmake --version
2. 安装 llama.cpp
git clone https://github.com/ggerganov/llama.cpp
convert_hf_to_gguf.py:HuggingFace 格式转 GGUF 的脚本 llama-quantize(或 quantize.exe):量化工具 main(或 main.exe):推理主程序 examples/:各种示例程序
3. 编译
cd llama.cpp
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r requirements/requirements-convert_hf_to_gguf.txt
cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -B build
cmake --build build --config Release
4. 模型转换
将 safetensors 转换为 gguf
convert-hf-to-gguf.py D:\\Project\\2026\\llama3-lora-merge --outtype f16 --outfile D:\\Project\\2026\\my_llama3.gguf
参数说明:
D:\Project\2026\llama3-lora-merge:输入模型路径(包含 config.json 和权重文件的目录) –outtype f16:输出类型,f16 表示半精度浮点数(16-bit),可选 f32(全精度)或 bf16 –outfile:输出 GGUF 文件路径
| 类型 | 精度 | 说明 |
|---|---|---|
f32 | 32-bit | 全精度,文件最大,精度最高 |
f16 | 16-bit | 半精度,平衡选择 |
bf16 | 16-bit | Brain Float,动态范围更大 |
q8_0 |


