Appearance
采样、量化与部署 — 概念
采样器链 (Sampler Chain)
sampler-chain 采样器链是一个管道,logits 依次经过多个采样器处理:
常见采样器
| 采样器 | 作用 |
|---|---|
| Temperature | 缩放 logits 温度 |
| Top-K | 只保留概率最高的 K 个 |
| Top-P | 只保留累积概率前 P 的 token |
| Min-P | 过滤概率低于最大概率 × min_p 的 token |
| Typical | 基于信息熵的采样 |
| Mirostat | 自适应温度控制 |
| Grammar | 基于语法约束输出格式 |
| Penalties | 重复惩罚、频率惩罚 |
| Logit Bias | 手动调整特定 token 的概率 |
量化 (Quantization)
quantization 将模型权重从 F16 压缩为低比特整数:
量化类型对比
| 类型 | 比特/权重 | 模型大小 (7B) | 质量损失 |
|---|---|---|---|
| F16 | 16 | 13.5 GB | 基准 |
| Q8_0 | 8.5 | 7.2 GB | 极小 |
| Q5_1 | 5.5 | 4.7 GB | 小 |
| Q5_0 | 5.0 | 4.3 GB | 小 |
| Q4_1 | 4.5 | 3.9 GB | 中等 |
| Q4_0 | 4.5 | 3.8 GB | 中等 |
| IQ4_XS | 4.25 | 3.6 GB | 中等 |
| Q3_K_S | 3.5 | 3.0 GB | 较大 |
| Q2_K | 2.75 | 2.4 GB | 大 |
Block Quantization
量化以 block 为单位(通常 32 个权重):
Block Q4_0 (18 bytes for 32 weights):
┌──────────┬──────────────────────┐
│ d (F16) │ 16 × uint8 (4-bit × 32) │
│ scale │ quantized values │
└──────────┴──────────────────────┘
dequantize: weight = (quantized - 8) × dImportance Matrix (imatrix)
imatrix 通过在校准数据上统计各张量的重要性,指导量化时保留关键权重:
bash
# 生成 imatrix
./llama-imatrix -m model.gguf -f calibration.txt -o imatrix.dat
# 使用 imatrix 量化
./llama-quantize --imatrix imatrix.dat model.gguf model-Q4_K_M.gguf Q4_K_Mllama-server
llama-server 提供 OpenAI 兼容 API:
bash
# 启动服务器
./llama-server -m model.gguf --port 8080
# 调用 chat completion
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model":"model","messages":[{"role":"user","content":"Hello"}]}'支持的端点:
/v1/chat/completions— Chat Completion/v1/completions— Text Completion/v1/embeddings— Embeddings/v1/models— 模型列表/health— 健康检查
相关概念
- sampler-chain — 采样器链设计
- quantization — 量化算法
- imatrix — 重要性矩阵