📝 简介
本指南记录了如何在 Ubuntu 22.04 上编译和部署 Llama.cpp,启用 CUDA 加速和优化选项,并通过 llama-server
启动一个本地推理 API 服务(兼容 HuggingFace)。
✅ 环境信息
OS: Ubuntu 22.04
GPU: NVIDIA RTX 3090
CUDA: 12.6
GCC/G++: 11
CMake: 3.24
Llama.cpp commit: 最新版本(支持 llama-server)
📦 环境准备
sudo apt update
sudo apt install -y build-essential git cmake gcc-11 g++-11
配置 CUDA 环境变量:
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
⚙️ 编译 Llama.cpp(启用 CUDA 与优化选项)
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
mkdir build && cd build
cmake .. \
-DLLAMA_CUBLAS=on \
-DGGML_CUDA_F16=on \
-DGGML_CUDA_FA_ALL_QUANTS=on \
-DCMAKE_C_COMPILER=gcc-11 \
-DCMAKE_CXX_COMPILER=g++-11 \
-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc
cmake --build . -j
🚀 启动服务端部署(llama-server)
./llama-server \
-m <YOUR_MODEL_> \
--jinja \
--reasoning-format deepseek \
-ngl 99 \
-fa \
-sm row \
--temp 0.6 \
--top-k 20 \
--top-p 0.95 \
--min-p 0 \
-c 8192 \
-n 16384 \
--no-context-shift \
--host 0.0.0.0 \
--port <YOUR_PORT> \
--api-key <YOUR_TOKEN>
参数说明:
参数 | 说明 |
---|---|
-hf | HuggingFace 上的模型路径(格式:org/model:quant ) |
-m | 本地的模型路径 |
--jinja | 启用 Jinja 模板支持(用于系统提示语格式化) |
--reasoning-format | 使用 DeepSeek 样式的推理格式 |
-ngl 99 | 启用最大 GPU 图层并行度 |
-fa | 启用 FlashAttention |
-sm row | 使用 row-major 矩阵格式,开启FlashAttention的同时建议打开 |
--temp , --top-k , --top-p , --min-p | 控制采样策略 |
-c , -n | 上下文窗口大小与最大 token 数(支持扩展上下文) |
--no-context-shift | 固定上下文窗口 |
--host 0.0.0.0 | 监听所有网卡接口,供远程访问 |
--api-key | 需要访问 HuggingFace Hub 时设置 Token |
🔒 安全提示:若部署在公网环境,建议使用防火墙或网关限制访问。
🧪 示例调用
curl http://localhost:8080/completion \
-H "Content-Type: application/json" \
-d '{
"prompt": "Q: What is the capital of France?\nA:",
"n_predict": 64
}'
🧾 总结
项目 | 状态 |
---|---|
CUDA 加速 | ✅ cuBLAS |
FP16 支持 | ✅ GGML_CUDA_F16 |
FlashAttention | ✅ -fa 与全量量化 |
服务端部署 | ✅ llama-server |
上下文窗口扩展 | ✅ 支持 32K+ token |
HuggingFace 集成 | ✅ 可直接拉取模型 |