llama.cpp进行模型格式转换和量化1. 什么是llama.cpp2. 核心底层GGML GGUF3. 拉取项目4. 安装依赖5. 编译6. 转换模型7. 量化本文介绍如何使用llama将safetensor格式的模型装成gguf并对转换后的模型进行量化。1. 什么是llama.cppllama.cpp 是由开发者 Georgi Gerganov 开源、基于纯 C/C 实现的轻量化大语言模型本地推理引擎MIT 开源协议免费商用最初为在普通消费硬件运行 Meta LLaMA 模型而生现已成为本地 / 边缘跑量化大模型的工业事实标准。核心目标零 Python 依赖、极致轻量化、全硬件兼容、低内存量化推理让 7B/13B/34B/70B 大模型在普通 CPU、轻薄本、树莓派、手机、无独显电脑流畅运行。2. 核心底层GGML GGUFGGML底层张量库llama.cpp 内置自研 C 张量计算库 GGML是高性能的根源无第三方数学库依赖手写硬件原生算子深度优化 CPU 指令集x86 AVX2/AVX512/AMX、ARM NEON延迟计算图、算子融合、栈内存分配、mmap 文件内存映射减少内存开销统一硬件抽象层插拔式后端CPU/CUDA/Metal/Vulkan/ROCm支持 CPUGPU 混合推理。GGUF标准模型格式替代旧 GGMLGGUF GGML Universal Format当前唯一推荐模型格式本地量化模型通用标准Hugging Face、Ollama、LM Studio、KoboldCpp 全部支持单文件打包一切权重、分词器、模型架构、上下文长度、Prompt 模板、量化参数全部存在一个 .gguf 文件无需额外配置原生支持内存映射 mmap模型文件不完整载入内存低配设备4GB/8GB 内存也能加载超大模型完整向后兼容支持 FP16、FP32、各类 2~8bit 量化权重结构化元数据自动识别 LLaMA3、Qwen、Mistral、Gemma、Yi、Mamba 等几乎所有主流开源模型架构。3. 拉取项目gitclone https://github.com/ggerganov/llama.cpp.git# 假设根目录是/home/wengadcdllama.cpp4. 安装依赖pipinstall-rrequirements.txt5. 编译make#或者 cmake -B build cmake --build build --config Releasecmake-Bbuildcmake--buildbuild--configRelease6. 转换模型cd/home/wengad/llama.cpp python convert-hf-to-gguf.py\/mnt/f/vproject/Moment/models/qwen3_5-4b\--outfile/mnt/f/vproject/Moment/models/qwen3_5-4b-f16.gguf\--outtypef16INFO:gguf.gguf_writer:Writing the following files:INFO:gguf.gguf_writer:/mnt/e/models/Qwen3.5-4B.gguf: n_tensors 441,total_size 8.7G Writing:100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████|8.65G/8.65G [00:5900:00, 145Mbyte/s] INFO:hf-to-gguf:Model successfully exported to /mnt/e/models/Qwen3.5-4B.gguf7. 量化# 量化./llama-quantize /mnt/e/models/Qwen3.5-4B.gguf /mnt/e/models/Qwen3.5-4B_q8_0.gguf q8_0[ 434/ 441] blk.32.ffn_down.weight - [ 9216, 2560,1, 1], type f16, converting to q8_0 … size 45.00 MiB- 23.91 MiB [ 435/ 441] blk.32.ffn_gate.weight - [ 2560, 9216, 1, 1], type f16, converting to q8_0 …size 45.00 MiB - 23.91 MiB [ 436/ 441] blk.32.ffn_up.weight[ 2560, 9216, 1, 1], type f16, converting to q8_0 … size 45.00 MiB - 23.91 MiB [ 437/ 441]blk.32.nextn.eh_proj.weight - [ 5120, 2560, 1,1], type f16, converting to q8_0 … size 25.00 MiB -13.28 MiB [ 438/ 441] blk.32.nextn.enorm.weight - [ 2560, 1, 1, 1], type f32, size 0.010 MiB [ 439/ 441]blk.32.nextn.hnorm.weight - [ 2560, 1, 1,1], type f32, size 0.010 MiB [ 440/ 441]blk.32.nextn.shared_head_norm.weight - [ 2560, 1, 1,1], type f32, size 0.010 MiB [ 441/ 441]blk.32.post_attention_norm.weight - [ 2560, 1, 1,1], type f32, size 0.010 MiB llama_model_quantize_impl:model size 8253.72 MiB (16.00 BPW) llama_model_quantize_impl:quant size 4386.53 MiB (8.51 BPW)llama_quantize: quantize time 68303.38 ms llama_quantize: totaltime 68303.38 ms
llama.cpp进行模型格式转换和量化
发布时间:2026/6/11 9:47:06
llama.cpp进行模型格式转换和量化1. 什么是llama.cpp2. 核心底层GGML GGUF3. 拉取项目4. 安装依赖5. 编译6. 转换模型7. 量化本文介绍如何使用llama将safetensor格式的模型装成gguf并对转换后的模型进行量化。1. 什么是llama.cppllama.cpp 是由开发者 Georgi Gerganov 开源、基于纯 C/C 实现的轻量化大语言模型本地推理引擎MIT 开源协议免费商用最初为在普通消费硬件运行 Meta LLaMA 模型而生现已成为本地 / 边缘跑量化大模型的工业事实标准。核心目标零 Python 依赖、极致轻量化、全硬件兼容、低内存量化推理让 7B/13B/34B/70B 大模型在普通 CPU、轻薄本、树莓派、手机、无独显电脑流畅运行。2. 核心底层GGML GGUFGGML底层张量库llama.cpp 内置自研 C 张量计算库 GGML是高性能的根源无第三方数学库依赖手写硬件原生算子深度优化 CPU 指令集x86 AVX2/AVX512/AMX、ARM NEON延迟计算图、算子融合、栈内存分配、mmap 文件内存映射减少内存开销统一硬件抽象层插拔式后端CPU/CUDA/Metal/Vulkan/ROCm支持 CPUGPU 混合推理。GGUF标准模型格式替代旧 GGMLGGUF GGML Universal Format当前唯一推荐模型格式本地量化模型通用标准Hugging Face、Ollama、LM Studio、KoboldCpp 全部支持单文件打包一切权重、分词器、模型架构、上下文长度、Prompt 模板、量化参数全部存在一个 .gguf 文件无需额外配置原生支持内存映射 mmap模型文件不完整载入内存低配设备4GB/8GB 内存也能加载超大模型完整向后兼容支持 FP16、FP32、各类 2~8bit 量化权重结构化元数据自动识别 LLaMA3、Qwen、Mistral、Gemma、Yi、Mamba 等几乎所有主流开源模型架构。3. 拉取项目gitclone https://github.com/ggerganov/llama.cpp.git# 假设根目录是/home/wengadcdllama.cpp4. 安装依赖pipinstall-rrequirements.txt5. 编译make#或者 cmake -B build cmake --build build --config Releasecmake-Bbuildcmake--buildbuild--configRelease6. 转换模型cd/home/wengad/llama.cpp python convert-hf-to-gguf.py\/mnt/f/vproject/Moment/models/qwen3_5-4b\--outfile/mnt/f/vproject/Moment/models/qwen3_5-4b-f16.gguf\--outtypef16INFO:gguf.gguf_writer:Writing the following files:INFO:gguf.gguf_writer:/mnt/e/models/Qwen3.5-4B.gguf: n_tensors 441,total_size 8.7G Writing:100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████|8.65G/8.65G [00:5900:00, 145Mbyte/s] INFO:hf-to-gguf:Model successfully exported to /mnt/e/models/Qwen3.5-4B.gguf7. 量化# 量化./llama-quantize /mnt/e/models/Qwen3.5-4B.gguf /mnt/e/models/Qwen3.5-4B_q8_0.gguf q8_0[ 434/ 441] blk.32.ffn_down.weight - [ 9216, 2560,1, 1], type f16, converting to q8_0 … size 45.00 MiB- 23.91 MiB [ 435/ 441] blk.32.ffn_gate.weight - [ 2560, 9216, 1, 1], type f16, converting to q8_0 …size 45.00 MiB - 23.91 MiB [ 436/ 441] blk.32.ffn_up.weight[ 2560, 9216, 1, 1], type f16, converting to q8_0 … size 45.00 MiB - 23.91 MiB [ 437/ 441]blk.32.nextn.eh_proj.weight - [ 5120, 2560, 1,1], type f16, converting to q8_0 … size 25.00 MiB -13.28 MiB [ 438/ 441] blk.32.nextn.enorm.weight - [ 2560, 1, 1, 1], type f32, size 0.010 MiB [ 439/ 441]blk.32.nextn.hnorm.weight - [ 2560, 1, 1,1], type f32, size 0.010 MiB [ 440/ 441]blk.32.nextn.shared_head_norm.weight - [ 2560, 1, 1,1], type f32, size 0.010 MiB [ 441/ 441]blk.32.post_attention_norm.weight - [ 2560, 1, 1,1], type f32, size 0.010 MiB llama_model_quantize_impl:model size 8253.72 MiB (16.00 BPW) llama_model_quantize_impl:quant size 4386.53 MiB (8.51 BPW)llama_quantize: quantize time 68303.38 ms llama_quantize: totaltime 68303.38 ms