# Llama.cpp

**llama.cpp** 是一个开源软件项目，用于在本地和云端运行大语言模型（LLM）与视觉语言模型（VLM）推理，以纯 C 和 C++ 编写，无需任何依赖，构建于 ggml 张量库之上。其公开目标是实现配置极简、在从 Apple MacBook 到 Nvidia GPU、移动端骁龙芯片乃至 IBM 大型机等广泛硬件上高性能运行的 LLM 推理。<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup> 代码以 MIT 许可证发布。<sup>[3](https://github.com/ggml-org/llama.cpp?ref=toolpotion.ai)</sup>

| 关键事实 | 详情 |
|---|---|
| 实现方式 | 纯 C/C++，无依赖；构建于 ggml 库之上<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup> |
| 模型格式 | GGUF，一种存储张量及标准化元数据的二进制格式，所有模型均须使用<sup>[4](https://github.com/ggml-org/llama.cpp/blob/master/docs/models.md)</sup><sup> • </sup><sup>[5](https://huggingface.co/docs/hub/gguf)</sup> |
| 量化 | 1.5 位、2 位、3 位、4 位、5 位、6 位和 8 位整数格式，用于加速推理并降低内存占用<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup> |
| 体积示例 | Llama 3.1 8B 从 32.1 GB（FP16）缩减至 Q4_K_M 下的 4.9 GB；405B 模型从 1,625.1 GB 缩减至 249.1 GB<sup>[6](https://github.com/ggml-org/llama.cpp/blob/master/tools/quantize/README.md?ref=hiteshsharma.dev)</sup> |
| 硬件后端 | CUDA（Nvidia）、HIP（AMD）、Metal（Apple Silicon）、Vulkan、SYCL（Intel GPU）、CANN（昇腾 NPU）、Hexagon（骁龙）、IBM zDNN、MUSA（摩尔线程）等<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup> |
| 服务器 | `llama-server` 在 `/v1/...` 下提供 OpenAI 兼容端点，另有一个 Anthropic 兼容的 `/v1/messages` 端点<sup>[7](https://llama.app/docs/api)</sup> |
| 许可证 | MIT<sup>[3](https://github.com/ggml-org/llama.cpp?ref=toolpotion.ai)</sup> |

## 设计与硬件支持

项目围绕 ggml 库组织，该库提供张量运算和 GGUF 文件格式，而 llama.cpp 则提供模型实现、转换工具和面向用户的程序。<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup><sup> • </sup><sup>[8](https://github.com/ggml-org/ggml/blob/master/docs/gguf.md?plain=1)</sup> GGUF 是一种为快速加载和保存模型而设计的二进制格式；模型传统上在 PyTorch 或其他框架中开发，然后转换为 GGUF 以供基于 GGML 的执行器使用。<sup>[8](https://github.com/ggml-org/ggml/blob/master/docs/gguf.md?plain=1)</sup> 与 safetensors 等纯张量格式不同，GGUF 同时编码张量和一组标准化元数据，[Hugging Face Hub](https://www.edgepedia.org/zh/hugging-face-hub) 为其内置了相关功能，包括元数据与张量查看器。<sup>[5](https://huggingface.co/docs/hub/gguf)</sup>

对于推理引擎而言，其硬件覆盖范围异常广泛。在 Apple Silicon 上，项目将该平台视为一等公民，通过 ARM NEON、Accelerate 框架和 Metal 进行优化。x86 构建使用 AVX、AVX2、AVX512 和 AMX 指令，RISC-V 构建支持 RVV、ZVFH、ZFH、ZICBOP 和 ZIHINTPAUSE。GPU 支持通过面向 Nvidia 的自定义 CUDA 内核、面向 AMD 的 HIP、面向摩尔线程的 MUSA，以及面向其他厂商的 Vulkan 和 SYCL 后端实现。<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup> 后端列表还包括面向昇腾 NPU 的 CANN、面向骁龙的 Hexagon、面向 [IBM Z](https://www.edgepedia.org/zh/ibm-z) 和 LinuxONE 的 IBM zDNN、面向 Adreno GPU 的 OpenCL、WebGPU，以及面向 AMD CPU 的 ZenDNN。<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup>

这种设计的一个实际结果是 CPU+GPU 混合推理：大于总显存容量的模型可以通过将选定层卸载到 GPU 而其余部分在 CPU 上运行来实现部分加速。<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup> 转移到 GPU 的层数是一个用户可设置的选项，线程数和上下文大小同样可调。<sup>[9](https://cug.org/proceedings/cug2024_proceedings/views/includes/files/pap145s2-file1.pdf)</sup>

## 量化

量化是 llama.cpp 让大模型适配消费级硬件的核心手段。`llama-quantize` 工具接收一个 GGUF 文件（通常为 F32、BF16 或 F16 精度），将其转换为更低精度的格式，从而缩小模型，且往往能加速推理，代价是损失一定的精度——精度通常以困惑度或 [Kullback–Leibler 散度](https://www.edgepedia.org/zh/kullback-leibler-divergence)衡量。<sup>[6](https://github.com/ggml-org/llama.cpp/blob/master/tools/quantize/README.md?ref=hiteshsharma.dev)</sup> 支持的格式涵盖 1.5 位至 8 位整数，包括 K-quant 系列（Q2_K 至 Q6_K）和 IQ 系列重要性矩阵量化。<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup><sup> • </sup><sup>[6](https://github.com/ggml-org/llama.cpp/blob/master/tools/quantize/README.md?ref=hiteshsharma.dev)</sup>

节省的幅度很大。对于 Llama 3.1，8B 模型在 Q4_K_M 下从 32.1 GB 降至 4.9 GB，70B 模型从 280.9 GB 降至 43.1 GB，405B 模型从 1,625.1 GB 降至 249.1 GB。<sup>[6](https://github.com/ggml-org/llama.cpp/blob/master/tools/quantize/README.md?ref=hiteshsharma.dev)</sup> 精度损失可以通过重要性矩阵（imatrix）来降低——这是一个由校准文本计算出的逐张量重要性统计文件，通过 `--imatrix` 传给量化器；该工具还允许逐张量选择量化方案、层剪枝和元数据覆盖。<sup>[6](https://github.com/ggml-org/llama.cpp/blob/master/tools/quantize/README.md?ref=hiteshsharma.dev)</sup>

一项针对 Llama-3.1-8B-Instruct 上 llama.cpp 量化的统一实证研究，在推理、知识、指令遵循和真实性基准上评估了 3 位至 8 位 K-quant 及旧版格式，并结合困惑度和 CPU 吞吐量，旨在为在资源预算内选择量化方案提供实用指南。<sup>[10](https://arxiv.org/abs/2601.14277)</sup> 其他模型上的相关工作发现，4 位和 6 位量化与非量化原版相比基本能保持性能，其中 4 位所需内存和计算量更少。<sup>[11](https://proceedings.iclr.cc/paper_files/paper/2025/file/a647405740b28a61311ac9cff28772e5-Paper-Conference.pdf)</sup>

## 工具与服务器

项目的主要产品是 `llama` 库，在 `include/llama.h` 中提供 C 风格接口，并附带示例程序和工具，从最小片段到完整的 OpenAI 兼容 HTTP 服务器一应俱全。<sup>[12](https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md)</sup> `llama-cli` 工具可以用一条命令直接从 Hugging Face 下载并运行模型，例如 `llama cli -hf ggml-org/Qwen3.5-0.8B-GGUF`；其他站点的模型可以通过将 `MODEL_ENDPOINT` 环境变量指向任何 Hugging Face API 兼容端点来获取。<sup>[1](https://github.com/ggml-org/llama.cpp/blob/master/README.md)</sup><sup> • </sup><sup>[4](https://github.com/ggml-org/llama.cpp/blob/master/docs/models.md)</sup>

**llama-server** 暴露 REST API，默认端口为 8080，支持多种格式：`/v1/...` 下的 OpenAI 兼容端点可与现有 OpenAI SDK 配合使用，一个 Anthropic 兼容的 `/v1/messages` 端点，以及带有更底层选项的 llama.cpp 专有端点。<sup>[7](https://llama.app/docs/api)</sup> 除聊天补全外，服务器还支持约束到 JSON schema 的结构化输出、OpenAI 风格的工具调用、包含图像和音频内容部分的多模态输入，以及一个报告每秒 token 统计和提示缓存复用情况的 `timings` 对象。<sup>[7](https://llama.app/docs/api)</sup> 更底层的端点包括 `/tokenize`、`/detokenize`、`/apply-template`、`/embedding` 和 `/reranking`。<sup>[13](https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md?plain=1)</sup> 服务器支持连续批处理（并发请求的动态批处理，默认启用）、可配置的并行槽位，以及用于 CPU 和 CUDA 部署的 Docker 镜像。<sup>[14](https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md)</sup>

**推测解码**通过用较小的草稿模型在主模型之前预测多个 token 来加速 token 生成。服务器支持多种实现，通过 `--spec-type` 选择，包括简单的草稿模型解码、EAGLE3、多 token 预测（MTP）草稿，以及若干无需单独草稿模型的基于 ngram 的变体；草稿相关选项控制线程放置、设备卸载和草稿 token 数量。<sup>[15](https://github.com/openalchemy/llama.cpp/blob/main/docs/speculative.md)</sup><sup> • </sup><sup>[14](https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md)</sup>

**多模态输入**通过 `libmtmd` 库处理，在 `llama-cli`、通过 OpenAI 兼容聊天 API 的 `llama-server`，以及用于测试的专用 `llama-mtmd-cli` 中支持图像、音频和视频输入。视觉和音频组件打包为单独的多模态投影器（mmproj）GGUF 文件，通常保持 BF16 或 Q8_0 等高质量格式，因为其质量直接影响生成质量，而对体积或速度影响甚微。<sup>[16](https://github.com/ggml-org/llama.cpp/blob/master/docs/multimodal.md)</sup><sup> • </sup><sup>[6](https://github.com/ggml-org/llama.cpp/blob/master/tools/quantize/README.md?ref=hiteshsharma.dev)</sup> ggml-org 分发的预量化多模态模型包括 [Gemma 3](https://www.edgepedia.org/zh/gemma-3)、[SmolVLM](https://www.edgepedia.org/zh/smolvlm)、Qwen2.5-VL、InternVL3、Llama 4 Scout、Voxtral 和 Qwen3-ASR 等。<sup>[16](https://github.com/ggml-org/llama.cpp/blob/master/docs/multimodal.md)</sup>

## 语境中的性能

独立测量将 llama.cpp 置于一个各有权衡的本地推理运行时家族之中。一项在配备 M2 Ultra 和 192 GB 统一内存的 [Mac Studio](https://www.edgepedia.org/zh/mac-studio) 上对 MLX、MLC-LLM、llama.cpp、Ollama 和 PyTorch MPS 的系统性比较，使用 Qwen-2.5 系列、提示长度最高达 100,000 token，发现 MLX 实现了最高的持续生成吞吐量，MLC-LLM 的首 token 延迟最低，而 llama.cpp 在轻量级单流使用中效率极高；封装 llama.cpp 的 Ollama 强调开发者易用性，但在吞吐量和首 token 延迟方面落后。<sup>[17](https://arxiv.org/abs/2511.05502)</sup> Tom's Hardware 对 Mac Studio M4 Max 的一项测试使用 llama.cpp（凭借其原生 Metal 支持）作为基准测试引擎，覆盖混合专家模型和稠密模型。<sup>[18](https://www.tomshardware.com/desktops/exploring-apple-silicons-local-ai-performance-with-the-mac-studio-and-m4-max-m4-max-beats-gb10-and-strix-halo-in-decode-throughput-but-memory-bandwidth-isnt-everything)</sup> 在移动硬件上，一项跨越五个框架和 CPU、GPU、NPU 后端的跨层测量研究发现，框架导致的性能差距在 NPU 上被放大，由于卸载和量化策略的差异，使用自定义算子时可达 10 倍。<sup>[19](https://arxiv.org/abs/2607.05475)</sup> 该引擎也被研究用于无服务器部署，研究分析了通过 llama.cpp 在 Google Cloud Run 的纯 CPU 基础设施上提供服务的量化小语言模型的冷启动延迟。<sup>[20](https://research.google/pubs/cold-start-latency-of-quantized-small-language-models-on-serverless-cpu-infrastructure/)</sup>

由于 llama.cpp 被广泛用作研究基线，它经常被量化和效率文献引用；ICLR 2025 的文献指出它支持移动设备以及仿射和非均匀量化格式，学术论文通常引用 [Georgi Gerganov](https://www.edgepedia.org/zh/georgi-gerganov) 及贡献者于 2023 年发布的代码仓库。<sup>[21](https://proceedings.iclr.cc/paper_files/paper/2025/file/57ccc284de6f060c8dcde8f9352f70a5-Paper-Conference.pdf)</sup><sup> • </sup><sup>[22](https://proceedings.neurips.cc/paper_files/paper/2024/file/496720b3c860111b95ac8634349dcc88-Paper-Conference.pdf)</sup>

## 生态系统

GGUF 已成为超越 llama.cpp 本身的共享格式：Hugging Face 将 llama.cpp、[LM Studio](https://www.edgepedia.org/zh/lm-studio)、GPT4All 和 Ollama 列为使用该格式的开源工具，提供 `gguf-my-repo` 空间以便无需本地环境即可将模型转换和量化为 GGUF，并附带一个 [JavaScript](https://www.edgepedia.org/zh/javascript) 解析器用于从远程托管的文件读取 GGUF 元数据。<sup>[5](https://huggingface.co/docs/hub/gguf)</sup> ggml-org 组织发布了许多模型的预量化 GGUF 版本，大多数默认采用 Q4_K_M 量化。<sup>[16](https://github.com/ggml-org/llama.cpp/blob/master/docs/multimodal.md)</sup>

## References

1. [llama.cpp README, ggml-org/llama.cpp, GitHub](https://github.com/ggml-org/llama.cpp/blob/master/README.md)
2. [Expanding GGML Hardware Support using the Vulkan API, FOSDEM 2025 slides](https://fosdem.org/2025/events/attachments/fosdem-2025-6772-expanding-ggml-hardware-support-using-the-vulkan-api/slides/238809/Expanding_MUSUQGT.pdf)
3. [ggml-org/llama.cpp repository page (MIT license)](https://github.com/ggml-org/llama.cpp?ref=toolpotion.ai)
4. [llama.cpp docs: models.md, GitHub](https://github.com/ggml-org/llama.cpp/blob/master/docs/models.md)
5. [GGUF, Hugging Face documentation](https://huggingface.co/docs/hub/gguf)
6. [llama.cpp tools/quantize README, GitHub](https://github.com/ggml-org/llama.cpp/blob/master/tools/quantize/README.md?ref=hiteshsharma.dev)
7. [API server, llama.app documentation](https://llama.app/docs/api)
8. [gguf.md, ggml-org/ggml, GitHub](https://github.com/ggml-org/ggml/blob/master/docs/gguf.md?plain=1)
9. [CUG 2024 proceedings paper on llama.cpp throughput](https://cug.org/proceedings/cug2024_proceedings/views/includes/files/pap145s2-file1.pdf)
10. [Which Quantization Should I Use? A Unified Evaluation of llama.cpp Quantization on Llama-3.1-8B-Instruct, arXiv](https://arxiv.org/abs/2601.14277)
11. [Published conference paper at ICLR 2025 (quantization comparison)](https://proceedings.iclr.cc/paper_files/paper/2025/file/a647405740b28a61311ac9cff28772e5-Paper-Conference.pdf)
12. [llama.cpp docs: build.md, GitHub](https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md)
13. [llama.cpp tools/server README, GitHub](https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md?plain=1)
14. [llama.cpp tools/server README (full), GitHub](https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md)
15. [llama.cpp docs: speculative.md, GitHub](https://github.com/openalchemy/llama.cpp/blob/main/docs/speculative.md)
16. [llama.cpp docs: multimodal.md, GitHub](https://github.com/ggml-org/llama.cpp/blob/master/docs/multimodal.md)
17. [Production-Grade Local LLM Inference on Apple Silicon, arXiv](https://arxiv.org/abs/2511.05502)
18. [Exploring Apple Silicon's local AI performance with the Mac Studio and M4 Max, Tom's Hardware](https://www.tomshardware.com/desktops/exploring-apple-silicons-local-ai-performance-with-the-mac-studio-and-m4-max-m4-max-beats-gb10-and-strix-halo-in-decode-throughput-but-memory-bandwidth-isnt-everything)
19. [Is Your NPU Ready for LLMs? arXiv](https://arxiv.org/abs/2607.05475)
20. [Cold Start Latency of Quantized Small Language Models on Serverless CPU Infrastructure, Google Research](https://research.google/pubs/cold-start-latency-of-quantized-small-language-models-on-serverless-cpu-infrastructure/)
21. [Published conference paper at ICLR 2025 (llama.cpp quantization formats)](https://proceedings.iclr.cc/paper_files/paper/2025/file/57ccc284de6f060c8dcde8f9352f70a5-Paper-Conference.pdf)
22. [NeurIPS 2024 proceedings citation: Georgi Gerganov and Contributors, llama.cpp (2023)](https://proceedings.neurips.cc/paper_files/paper/2024/file/496720b3c860111b95ac8634349dcc88-Paper-Conference.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Inference, serving, and efficiency of foundation models

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/llama-cpp

English original: https://www.edgepedia.org/llama-cpp
