Skip to content

llama.cpp

llama.cpp 是用于本地大语言模型推理的 C/C++ 实现。

llama.cpp 面向较少依赖和较细粒度的推理控制,支持 CPU 与多种 GPU 后端。常用模型格式为 GGUF;量化可在降低内存占用的同时提升推理效率。

安装

Homebrew

bash
brew install llama.cpp

从源码构建

bash
git clone https://github.com/ggml-org/llama.cpp.git
cd llama.cpp
cmake -B build
cmake --build build --config Release

构建后的可执行文件位于 build/bin/。若需使用 CUDA、Vulkan 等后端,应在 CMake 配置阶段开启对应选项。

运行模型

本地 GGUF 文件

bash
llama-cli -m ./model.gguf

从 Hugging Face 下载并运行

bash
llama-cli -hf ggml-org/gemma-3-1b-it-GGUF

-hf 会下载模型到 Hugging Face 缓存目录;模型仓库需提供兼容的 GGUF 文件。

启动 API 服务

llama-server 提供 OpenAI 兼容的 HTTP API。

bash
llama-server -m ./model.gguf --port 8080

也可让它自动下载模型:

bash
llama-server -hf ggml-org/gemma-3-1b-it-GGUF --port 8080

服务启动后,可通过 http://localhost:8080 访问。

常用参数

参数含义
-m <路径>指定本地 GGUF 模型文件
-hf <仓库>指定 Hugging Face 模型仓库并自动下载
-c <长度>设置上下文长度
-ngl <层数>将指定数量的模型层卸载到 GPU
-t <线程数>设置 CPU 线程数

参数会影响内存占用和速度。模型、上下文长度与 GPU 卸载层数应结合设备内存逐步调整。

基于 MIT 许可发布