FineTuneX 支持多种模型量化方法,可以将微调后的模型进一步压缩,减少显存占用和推理延迟。
| 方法 | 位数 | 压缩比 | 速度 | 精度 | 适用场景 |
|---|---|---|---|---|---|
| AWQ | 4bit | 4x | 快 | 高 | GPU 推理 |
| GPTQ | 4bit | 4x | 中 | 高 | GPU 推理 |
| GGUF | 2-8bit | 2-8x | 中 | 中 | CPU 推理 |
# 安装依赖
pip install autoawq
# 运行量化
python examples/quantize_awq.py --model_path ./outputs/qwen3.5-0.8b-finetuned
# 安装依赖
pip install auto-gptq
# 运行量化
python examples/quantize_gptq.py --model_path ./outputs/qwen3.5-0.8b-finetuned
# 运行量化(会自动克隆 llama.cpp)
python examples/quantize_gguf.py --model_path ./outputs/qwen3.5-0.8b-finetuned --quant_type Q4_K_M
# AWQ 量化
python scripts/quantize_model.py \
--model_path ./outputs/qwen3.5-0.8b-finetuned \
--method awq \
--bits 4
# GPTQ 量化
python scripts/quantize_model.py \
--model_path ./outputs/qwen3.5-0.8b-finetuned \
--method gptq \
--bits 4 \
--group_size 128
# GGUF 量化
python scripts/quantize_model.py \
--model_path ./outputs/qwen3.5-0.8b-finetuned \
--method gguf \
--quant_type Q4_K_M
# 仅估算大小,不执行量化
python scripts/quantize_model.py \
--model_path ./outputs/qwen3.5-0.8b-finetuned \
--estimate_only
输出示例:
4bit 量化:
原始大小:3.50 GB
压缩比:4.0x
估算大小:1.09 GB
节省空间:2.41 GB (68.8%)
优点:
缺点:
适用场景: 需要快速推理的生产环境
安装:
pip install autoawq
使用:
from finetunex.quantization import quantize_to_awq
quantize_to_awq(
model_path="./outputs/qwen3.5-0.8b-finetuned",
output_path="./outputs/qwen3.5-0.8b-awq",
quantization_config={
"w_bit": 4,
"q_group_size": 128,
}
)
优点:
缺点:
适用场景: 对精度要求高的场景
安装:
pip install auto-gptq
使用:
from finetunex.quantization import quantize_to_gptq
quantize_to_gptq(
model_path="./outputs/qwen3.5-0.8b-finetuned",
output_path="./outputs/qwen3.5-0.8b-gptq",
quantization_config={
"bits": 4,
"group_size": 128,
}
)
优点:
缺点:
适用场景: 无 GPU 或边缘设备
使用:
from finetunex.quantization import quantize_to_gguf
quantize_to_gguf(
model_path="./outputs/qwen3.5-0.8b-finetuned",
output_path="./outputs/qwen3.5-0.8b-Q4_K_M.gguf",
quantization_type="Q4_K_M"
)
| 类型 | 大小 | 速度 | 质量 | 推荐度 |
|---|---|---|---|---|
| Q2_K | 最小 | 最快 | 最低 | ⭐⭐ |
| Q3_K_S | 小 | 快 | 低 | ⭐⭐⭐ |
| Q3_K_M | 中小 | 快 | 中 | ⭐⭐⭐⭐ |
| Q4_K_S | 中 | 中 | 中高 | ⭐⭐⭐⭐ |
| Q4_K_M | 中 | 中 | 高 | ⭐⭐⭐⭐⭐ |
| Q5_K_S | 中大 | 中 | 高 | ⭐⭐⭐⭐ |
| Q5_K_M | 大 | 中慢 | 很高 | ⭐⭐⭐⭐ |
| Q6_K | 大 | 慢 | 很高 | ⭐⭐⭐ |
| Q8_0 | 最大 | 最慢 | 最高 | ⭐⭐⭐ |
推荐: 使用 Q4_K_M 平衡质量和大小
from transformers import AutoTokenizer
from awq import AutoAWQForCausalLM
# 加载量化模型
model = AutoAWQForCausalLM.from_quantized(
"./outputs/qwen3.5-0.8b-awq",
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained("./outputs/qwen3.5-0.8b-awq")
# 推理
prompt = "你好"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=100)
print(tokenizer.decode(outputs[0]))
from auto_gptq import AutoGPTQForCausalLM
from transformers import AutoTokenizer
# 加载量化模型
model = AutoGPTQForCausalLM.from_quantized(
"./outputs/qwen3.5-0.8b-gptq",
device="cuda:0",
)
tokenizer = AutoTokenizer.from_pretrained("./outputs/qwen3.5-0.8b-gptq")
# 推理
prompt = "你好"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=100)
print(tokenizer.decode(outputs[0]))
# 命令行推理
./llama.cpp/main -m ./outputs/qwen3.5-0.8b-Q4_K_M.gguf -p "你好" -n 512
# Python 推理
from llama_cpp import Llama
llm = Llama(model_path="./outputs/qwen3.5-0.8b-Q4_K_M.gguf")
output = llm("你好", max_tokens=100)
print(output)
python examples/qwen3.5_0.8b_local_finetune.py
python scripts/quantize_model.py \
--model_path ./outputs/qwen3.5-0.8b-finetuned \
--show_info \
--estimate_only
python examples/quantize_awq.py \
--model_path ./outputs/qwen3.5-0.8b-finetuned
python scripts/inference.py \
--model_path ./outputs/qwen3.5-0.8b-awq \
--interactive
| 版本 | 大小 | 显存占用 | 推理速度 |
|---|---|---|---|
| 原始 FP16 | 3.5 GB | ~7 GB | 100% |
| AWQ 4bit | 1.1 GB | ~3 GB | 120% |
| GPTQ 4bit | 1.0 GB | ~2.5 GB | 110% |
| GGUF Q4_K_M | 1.1 GB | CPU | 80% |
速度越快越好(相对于原始 FP16)
A: 会有一定影响,但通常很小。4bit 量化通常能保持 95%+ 的原始性能。
A:
A: 需要使用对应的库加载量化模型,不能直接用原始方式加载。
A:
取决于模型大小和硬件。
A: 4bit 量化通常损失 1-5% 的精度,取决于任务和量化方法。
# AWQ
pip install autoawq
# GPTQ
pip install auto-gptq
# GGUF (llama.cpp)
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
make
# Python binding
pip install llama-cpp-python
最后更新: 2026-03-30 版本: 0.1.0