FineTuneX 现已支持对微调后的大模型进行量化,提供三种主流量化方法,可将模型大小减少 75%,推理速度提升 20%。
# AWQ - 推荐(快速、高精度)
pip install autoawq
python examples/quantize_awq.py --model_path ./outputs/qwen3.5-0.8b-finetuned
# GPTQ - 高精度
pip install auto-gptq
python examples/quantize_gptq.py --model_path ./outputs/qwen3.5-0.8b-finetuned
# GGUF - CPU 推理
python examples/quantize_gguf.py --model_path ./outputs/qwen3.5-0.8b-finetuned --quant_type Q4_K_M
python scripts/quantize_model.py \
--model_path ./outputs/qwen3.5-0.8b-finetuned \
--method awq \
--bits 4
python examples/quantization_workflow.py
| 方法 | 位数 | 压缩比 | 速度 | 精度 | 场景 |
|---|---|---|---|---|---|
| AWQ | 4bit | 4x | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | GPU 推理 |
| GPTQ | 4bit | 4x | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | GPU 推理 |
| GGUF | 2-8bit | 2-8x | ⭐⭐⭐ | ⭐⭐⭐⭐ | CPU 推理 |
| 版本 | 大小 | 显存 | 速度 |
|---|---|---|---|
| 原始 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% |
finetunex/quantization/
├── __init__.py # 模块导出
├── quantize.py # 量化实现
│ ├── quantize_to_awq()
│ ├── quantize_to_gptq()
│ ├── quantize_to_gguf()
│ └── quantize_model()
└── utils.py # 工具函数
├── get_model_size()
├── estimate_quantized_size()
├── compare_models()
└── ...
examples/
├── quantize_awq.py # AWQ 示例
├── quantize_gptq.py # GPTQ 示例
├── quantize_gguf.py # GGUF 示例
└── quantization_workflow.py # 完整流程
scripts/
└── quantize_model.py # 通用量化脚本
docs/
└── quantization.md # 详细文档
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,
}
)
# AWQ
from awq import AutoAWQForCausalLM
model = AutoAWQForCausalLM.from_quantized("./outputs/qwen3.5-0.8b-awq")
# GPTQ
from auto_gptq import AutoGPTQForCausalLM
model = AutoGPTQForCausalLM.from_quantized("./outputs/qwen3.5-0.8b-gptq")
# GGUF (命令行)
./llama.cpp/main -m ./outputs/qwen3.5-0.8b-Q4_K_M.gguf -p "你好"
# AWQ
pip install autoawq
# GPTQ
pip install auto-gptq
# GGUF
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp && make
pip install llama-cpp-python
from finetunex.quantization import get_model_size
size = get_model_size("./outputs/qwen3.5-0.8b-finetuned")
print(f"模型大小:{size['total_size_formatted']}")
from finetunex.quantization import estimate_quantized_size
estimate = estimate_quantized_size(
"./outputs/qwen3.5-0.8b-finetuned",
quantization_bits=4
)
print(f"4bit 量化后:{estimate['estimated_size']}")
print(f"节省空间:{estimate['space_saved']}")
from finetunex.quantization import compare_models
comparison = compare_models(
"./outputs/qwen3.5-0.8b-finetuned",
"./outputs/qwen3.5-0.8b-awq",
"原始模型",
"AWQ 量化"
)
print(f"大小差异:{comparison['difference']}")
python scripts/quantize_model.py \
--model_path ./outputs/model \
--method awq \
--bits 4 \
--group_size 128
python scripts/quantize_model.py \
--model_path ./outputs/model \
--estimate_only
python scripts/quantize_model.py \
--model_path ./outputs/model \
--show_info
| 类型 | 大小 | 质量 | 推荐度 | | ------------ | -- | ----- | ----- | | Q2_K | 最小 | 低 | ⭐⭐ | | Q3_K_M | 小 | 中 | ⭐⭐⭐⭐ | | Q4_K_M | 中 | 高 | ⭐⭐⭐⭐⭐ | | Q5_K_M | 大 | 很高 | ⭐⭐⭐⭐ | | Q8_0 | 最大 | 最高 | ⭐⭐⭐ |
微调模型 → 检查大小 → 估算量化 → 选择方法 → 执行量化 → 测试使用
Q: 量化需要多长时间? A: AWQ 5-15 分钟,GPTQ 15-60 分钟,GGUF 10-30 分钟
Q: 量化会损失多少精度? A: 4bit 量化通常损失 1-5% 精度
Q: 应该选择哪种方法? A:
FineTuneX 提供完整的量化支持:
添加日期: 2026-03-30 版本: 0.1.0 状态: ✅ 完成