llama.cpp
llama.cpp 是一个专为大型语言模型(LLMs)设计的高性能开源推理引擎,由开发者 Georgi Gerganov 基于 C/C++ 实现。它通过底层优化技术,实现在多种硬件(包括消费级设备)上高效运行大模型,尤其适合本地化部署场景。
llama.cpp 是一个库,可以基于其进行开发。
核心定位与功能
- 高效推理引擎
- 专注于将 Meta 的 LLaMA 系列模型(如 LLaMA-1/2/3)及其他开源大模型(如 Mistral、Falcon)转化为可在本地硬件运行的推理服务。
- 支持单轮文本生成、多轮对话、流式输出(逐 Token 返回结果)等基础功能。
- 硬件兼容性
- 跨平台支持:适配 macOS(Apple Silicon)、Linux、Windows、iOS/Android 甚至树莓派。
- 硬件加速:
- CPU 优化:利用 SIMD 指令(如 AVX2/NEON)加速计算。
- GPU 加速:支持 NVIDIA CUDA、Apple Metal、Vulkan 等框架,实现混合推理(CPU+GPU 协同)。
- 量化与压缩
- 提供 1.5-bit 至 8-bit 量化(如 Q4_K_M、Q8_0),将模型体积压缩至原大小的 20%-50%,显著降低内存需求。例如,LLaMA-7B 模型经 4-bit 量化后仅需 4GB 内存即可运行。
ollama 基于 llama.cpp 开发,提供了更易用的命令行界面和 API 接口,简化了模型的部署和管理。
与相关工具的关系
- LLaMA(模型本体)
LLaMA 是 Meta 发布的基座模型,提供预训练权重;llama.cpp 则是其本地化推理的“引擎”,负责将权重文件(如 GGUF 格式)转化为可执行计算。 - Ollama(应用封装)
Ollama 是基于 llama.cpp 的上层工具,提供类似 Docker 的模型管理功能(如 ollama run llama2),简化下载、运行和 API 调用流程。 - Hugging Face 生态
通过 GGUF 格式与 Hugging Face 模型库兼容,支持直接下载量化版模型文件(如 bartowski/Llama-3.2-3B-Instruct-GGUF)。
用法
- llama-cli 命令行工具
llama-cli
# 对话模式
llama-cli -m model.gguf
# > hi, who are you?
# Hi there! I'm your helpful assistant! I'm an AI-powered chatbot designed to assist and provide information to users like you. I'm here to help answer your questions, provide guidance, and offer support on a wide range of topics. I'm a friendly and knowledgeable AI, and I'm always happy to help with anything you need. What's on your mind, and how can I assist you today?
#
# > what is 1+1?
# Easy peasy! The answer to 1+1 is... 2!
# 以定制聊天模版进行对话
# use the "chatml" template (use -h to see the list of supported templates)
llama-cli -m model.gguf -cnv --chat-template chatml
# use a custom template
llama-cli -m model.gguf -cnv --in-prefix 'User: ' --reverse-prompt 'User:'
# 补全续写方式
llama-cli -m model.gguf -p "I believe the meaning of life is" -n 128 -no-cnv
# I believe the meaning of life is to find your own truth and to live in accordance with it. For me, this means being true to myself and following my passions, even if they don't align with societal expectations. I think that's what I love about yoga – it's not just a physical practice, but a spiritual one too. It's about connecting with yourself, listening to your inner voice, and honoring your own unique journey.
# 通过自定义指令限制输出
llama-cli -m model.gguf -n 256 --grammar-file grammars/json.gbnf -p 'Request: schedule a call at 8pm; Command:'
# {"appointmentTime": "8pm", "appointmentDetails": "schedule a a call"}
llama-server
llama-server 是一个轻量级的大模型web服务器。
# 基本启用
llama-server -m model.gguf --port 8080
# Basic web UI can be accessed via browser: http://localhost:8080
# Chat completion endpoint: http://localhost:8080/v1/chat/completions
# 多用户使用
# up to 4 concurrent requests, each with 4096 max context
llama-server -m model.gguf -c 16384 -np 4
# Enable speculative decoding(启用推测解码)是一种通过并行化推理过程来加速大语言模型(LLM)生成文本的技术,其核心思想是“先推测后验证”(Draft-then-Verify)。
# 双模型协作机制
# 目标模型(Target Model):主模型(如LLaMA-70B),负责最终生成结果的准确性。
# 草稿模型(Draft Model):小型辅助模型(如LLaMA-7B),快速生成候选词(draft tokens)。
# 两者需共享相同的分词器(Tokenizer)。
# the draft.gguf model should be a small variant of the target model.gguf
llama-server -m model.gguf -md draft.gguf
# “Serve an embedding model”(部署/服务嵌入模型)指将嵌入模型(Embedding Model)转化为可被其他系统调用的在线推理服务,使其能实时将文本、图像等非结构化数据转化为向量(Embeddings),供下游任务(如搜索、聚类、推荐)使用。
# 将嵌入模型封装为 RESTful 或 gRPC 接口,支持输入文本并返回向量。例如,使用 TensorFlow Serving 或 ONNX Runtime 将模型部署为 HTTP 端点。
# use the /embedding endpoint
llama-server -m model.gguf --embedding --pooling cls -ub 8192
# “Serve a reranking model”(部署/服务重排序模型)指将重排序模型(Reranking Model)转化为可被外部系统调用的在线推理服务,其核心功能是对初步检索到的候选结果(如文本段落、商品列表等)进行相关性评分与重新排序,以提升最终输出的精准度。
# use the /reranking endpoint
llama-server -m model.gguf --reranking
# 按照指令限制模型的所有输出
# custom grammar
llama-server -m model.gguf --grammar-file grammar.gbnf
# JSON
llama-server -m model.gguf --grammar-file grammars/json.gbnf
llama-perlexity
llama-perlexity 是对模型进行困惑度评估的工具
llama-bench
# 测试模型性能
llama-bench -m model.gguf
# Output:
# | model | size | params | backend | threads | test | t/s |
# | ------------------- | ---------: | ---------: | ---------- | ------: | ------------: | -------------------: |
# | qwen2 1.5B Q4_0 | 885.97 MiB | 1.54 B | Metal,BLAS | 16 | pp512 | 5765.41 ± 20.55 |
# | qwen2 1.5B Q4_0 | 885.97 MiB | 1.54 B | Metal,BLAS | 16 | tg128 | 197.71 ± 0.81 |
#
# build: 3e0ba0e60 (4229)
llama-run
运行llama.cpp模型的全面示例。用于推理。与RamaLama 3一起使用。
llama-run granite-code
llama-simple
使用llama.cpp实现应用程序的一个简单示例。对开发者很有用。
支持基本的文本续写。
llama-simple -m model.gguf
# Hello my name is Kaitlyn and I am a 16 year old girl. I am a junior in high school and I am currently taking a class called "The Art of
llama-quantize
# obtain the official LLaMA model weights and place them in ./models
ls ./models
llama-2-7b tokenizer_checklist.chk tokenizer.model
# [Optional] for models using BPE tokenizers
ls ./models
<folder containing weights and tokenizer json> vocab.json
# [Optional] for PyTorch .bin models like Mistral-7B
ls ./models
<folder containing weights and tokenizer json>
# install Python dependencies
python3 -m pip install -r requirements.txt
# convert the model to ggml FP16 format
python3 convert_hf_to_gguf.py models/mymodel/
# quantize the model to 4-bits (using Q4_K_M method)
./llama-quantize ./models/mymodel/ggml-model-f16.gguf ./models/mymodel/ggml-model-Q4_K_M.gguf Q4_K_M
# update the gguf filetype to current version if older version is now unsupported
./llama-quantize ./models/mymodel/ggml-model-Q4_K_M.gguf ./models/mymodel/ggml-model-Q4_K_M-v2.gguf COPY
# start inference on a gguf model
./llama-cli -m ./models/mymodel/ggml-model-Q4_K_M.gguf -cnv -p "You are a helpful assistant"
| Model | Original size | Quantized size (Q4_0) |
|---|---|---|
| 7B | 13 GB | 3.9 GB |
| 13B | 24 GB | 7.8 GB |
| 30B | 60 GB | 19.5 GB |
| 65B | 120 GB | 38.5 GB |
支持几种量化方法
- F16
- Q4_0
- Q4_1
- Q5_0
- Q5_1
-
Q8_0
-
Q2_K
- Q3_K_S
- Q3_K_M
- Q3_K_L
- Q4_K_S
- Q4_K_M
- Q5_K_S
- Q5_K_M
- Q6_K