mcpField
L
llama-cpp

llama-cpp

github.com/firecrawl开发2245 次安装38 周前更新
代码仓库

关于 llama-cpp

llama-cpp 是基于 C/C++ 的 LLM 推理工具,面向 CPU、Apple Silicon 及非 NVIDIA 硬件环境。它支持 GGUF 模型量化,并可通过命令行或兼容 OpenAI 的服务器运行推理。

主要功能

  1. 01使用 C/C++ 执行 LLM 推理
  2. 02运行 GGUF 格式模型
  3. 03支持 CPU 与多种 GPU 加速
  4. 04提供命令行和服务器模式
  5. 05支持批处理及语法约束生成

适用场景

  1. 01在仅有 CPU 的机器上运行模型
  2. 02在 Apple Silicon 设备上进行本地推理
  3. 03在边缘设备部署模型
  4. 04通过服务器接口提供模型推理

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/firecrawl/ai-research-skills --skill llama-cpp

安装后在 Claude Code 中输入/llama-cpp确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
optimization.md
quantization.md
server.md

详细介绍

llama.cpp

纯 C/C++ 大模型推理,依赖极少,针对 CPU 和非 NVIDIA 硬件优化。

何时使用 llama.cpp

在以下情况下使用 llama.cpp:

  • 在仅有 CPU 的机器上运行
  • 在 Apple Silicon(M1/M2/M3/M4)上部署
  • 使用 AMD 或 Intel GPU(无 CUDA)
  • 边缘部署(Raspberry Pi、嵌入式系统)
  • 需要无需 Docker/Python 的简单部署

在以下情况下改用 TensorRT-LLM:

  • 拥有 NVIDIA GPU(A100/H100)
  • 需要最大吞吐量(100K+ tok/s)
  • 在配备 CUDA 的数据中心中运行

在以下情况下改用 vLLM:

  • 拥有 NVIDIA GPU
  • 需要 Python 优先的 API
  • 想要 PagedAttention

快速开始

安装
# macOS/Linux
brew install llama.cpp

# Or build from source
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
make

# With Metal (Apple Silicon)
make LLAMA_METAL=1

# With CUDA (NVIDIA)
make LLAMA_CUDA=1

# With ROCm (AMD)
make LLAMA_HIP=1
下载模型
# Download from HuggingFace (GGUF format)
huggingface-cli download \
    TheBloke/Llama-2-7B-Chat-GGUF \
    llama-2-7b-chat.Q4_K_M.gguf \
    --local-dir models/

# Or convert from HuggingFace
python convert_hf_to_gguf.py models/llama-2-7b-chat/
运行推理
# Simple chat
./llama-cli \
    -m models/llama-2-7b-chat.Q4_K_M.gguf \
    -p "Explain quantum computing" \
    -n 256  # Max tokens

# Interactive chat
./llama-cli \
    -m models/llama-2-7b-chat.Q4_K_M.gguf \
    --interactive
服务器模式
# Start OpenAI-compatible server
./llama-server \
    -m models/llama-2-7b-chat.Q4_K_M.gguf \
    --host 0.0.0.0 \
    --port 8080 \
    -ngl 32  # Offload 32 layers to GPU

# Client request
curl http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama-2-7b-chat",
    "messages": [{"role": "user", "content": "Hello!"}],
    "temperature": 0.7,
    "max_tokens": 100
  }'

量化格式

GGUF 格式概览
格式位数大小(7B)速度质量使用场景
Q4_K_M4.54.1 GB快良好推荐的默认选择
Q4_K_S4.33.9 GB更快较低速度关键
Q5_K_M5.54.8 GB中等更好质量关键
Q6_K6.55.5 GB较慢最佳最高质量
Q8_08.07.0 GB慢出色退化最小
Q2_K2.52.7 GB最快较差仅用于测试
选择量化
# General use (balanced)
Q4_K_M  # 4-bit, medium quality

# Maximum speed (more degradation)
Q2_K or Q3_K_M

# Maximum quality (slower)
Q6_K or Q8_0

# Very large models (70B, 405B)
Q3_K_M or Q4_K_S  # Lower bits to fit in memory

硬件加速

Apple Silicon(Metal)
# Build with Metal
make LLAMA_METAL=1

# Run with GPU acceleration (automatic)
./llama-cli -m model.gguf -ngl 999  # Offload all layers

# Performance: M3 Max 40-60 tokens/sec (Llama 2-7B Q4_K_M)
NVIDIA GPU(CUDA)
# Build with CUDA
make LLAMA_CUDA=1

# Offload layers to GPU
./llama-cli -m model.gguf -ngl 35  # Offload 35/40 layers

# Hybrid CPU+GPU for large models
./llama-cli -m llama-70b.Q4_K_M.gguf -ngl 20  # GPU: 20 layers, CPU: rest
AMD GPU(ROCm)
# Build with ROCm
make LLAMA_HIP=1

# Run with AMD GPU
./llama-cli -m model.gguf -ngl 999

常见模式

批处理
# Process multiple prompts from file
cat prompts.txt | ./llama-cli \
    -m model.gguf \
    --batch-size 512 \
    -n 100
约束生成
# JSON output with grammar
./llama-cli \
    -m model.gguf \
    -p "Generate a person: " \
    --grammar-file grammars/json.gbnf

# Outputs valid JSON only
上下文大小
# Increase context (default 512)
./llama-cli \
    -m model.gguf \
    -c 4096  # 4K context window

# Very long context (if model supports)
./llama-cli -m model.gguf -c 32768  # 32K context

性能基准

CPU 性能(Llama 2-7B Q4_K_M)
CPU线程数速度成本
Apple M3 Max1650 tok/s$0(本地)
AMD Ryzen 9 7950X3235 tok/s$0.50/小时
Intel i9-13900K3230 tok/s$0.40/小时
AWS c7i.16xlarge6440 tok/s$2.88/小时
GPU 加速(Llama 2-7B Q4_K_M)
GPU速度对比 CPU成本
NVIDIA RTX 4090120 tok/s3-4×$0(本地)
NVIDIA A1080 tok/s2-3×$1.00/小时
AMD MI25070 tok/s2×$2.00/小时
Apple M3 Max(Metal)50 tok/s~相同$0(本地)

支持的模型

LLaMA 系列:

  • Llama 2(7B、13B、70B)
  • Llama 3(8B、70B、405B)
  • Code Llama

Mistral 系列:

  • Mistral 7B
  • Mixtral 8x7B、8x22B

其他:

  • Falcon、BLOOM、GPT-J
  • Phi-3、Gemma、Qwen
  • LLaVA(视觉)、Whisper(音频)

查找模型:https://huggingface.co/models?library=gguf

参考资料

资源

评论

支持 Markdown