FineTuneX 的量化功能完全支持对 LoRA 微调的模型进行量化!
1. LoRA 微调
↓
2. 合并 LoRA 权重到基础模型
↓
3. 对合并后的模型进行量化
↓
4. 部署量化模型
LoRA 微调只训练少量参数,权重是分离的:
量化需要对完整的模型权重进行操作,所以需要先合并。
python examples/quantize_lora_model.py \
--base_model Qwen/Qwen3.5-0.5B \
--lora_path ./outputs/qwen3.5-0.8b-finetuned \
--method awq \
--bits 4
# 步骤 1: 仅合并 LoRA 权重
python examples/quantize_lora_model.py \
--base_model Qwen/Qwen3.5-0.5B \
--lora_path ./outputs/qwen3.5-0.8b-finetuned \
--merge_only
# 步骤 2: 量化合并后的模型
python scripts/quantize_model.py \
--model_path ./outputs/qwen3.5-0.8b-finetuned-merged \
--method awq \
--bits 4
# AWQ 量化(推荐)
python examples/quantize_lora_model.py \
--base_model Qwen/Qwen3.5-0.5B \
--lora_path ./outputs/qwen3.5-0.8b-finetuned \
--method awq \
--bits 4 \
--output_path ./outputs/qwen3.5-0.8b-awq
# GPTQ 量化
python examples/quantize_lora_model.py \
--base_model Qwen/Qwen3.5-0.5B \
--lora_path ./outputs/qwen3.5-0.8b-finetuned \
--method gptq \
--bits 4
# GGUF 量化
python examples/quantize_lora_model.py \
--base_model Qwen/Qwen3.5-0.5B \
--lora_path ./outputs/qwen3.5-0.8b-finetuned \
--method gguf \
--quant_type Q4_K_M
--base_model # 基础模型路径或名称
--lora_path # LoRA 微调后的权重路径
--output_path # 量化模型输出路径(可选)
--method # 量化方法:awq/gptq/gguf
--bits # 量化位数:4 或 8
--merge_only # 仅合并 LoRA 权重
--quantize_only # 仅量化(跳过合并)
import subprocess
# 执行 LoRA 量化
subprocess.run([
"python", "examples/quantize_lora_model.py",
"--base_model", "Qwen/Qwen3.5-0.5B",
"--lora_path", "./outputs/qwen3.5-0.8b-finetuned",
"--method", "awq",
"--bits", "4"
])
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
from finetunex.quantization import quantize_model
# 1. 加载基础模型和 LoRA 权重
base_model = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen3.5-0.5B",
device_map="auto",
torch_dtype=torch.float16,
)
tokenizer = AutoTokenizer.from_pretrained("./outputs/qwen3.5-0.8b-finetuned")
# 2. 加载 LoRA 模型
lora_model = PeftModel.from_pretrained(
base_model,
"./outputs/qwen3.5-0.8b-finetuned"
)
# 3. 合并权重
merged_model = lora_model.merge_and_unload()
# 4. 保存合并后的模型
merged_model.save_pretrained("./outputs/qwen3.5-0.8b-merged")
tokenizer.save_pretrained("./outputs/qwen3.5-0.8b-merged")
# 5. 量化合并后的模型
result = quantize_model(
model_path="./outputs/qwen3.5-0.8b-merged",
output_path="./outputs/qwen3.5-0.8b-awq",
method="awq",
bits=4,
)
print(f"量化完成:{result['output_path']}")
| 阶段 | 大小 | 显存 | 说明 |
|---|---|---|---|
| 基础模型 + LoRA | 3.5 GB + 100 MB | ~7 GB | 微调后 |
| 合并后 | 3.5 GB | ~7 GB | LoRA 权重合并 |
| AWQ 4bit 量化 | 1.1 GB | ~3 GB | 推荐 |
| GPTQ 4bit 量化 | 1.0 GB | ~2.5 GB | 高精度 |
| GGUF Q4_K_M | 1.1 GB | CPU | CPU 推理 |
A: LoRA 模型的权重是分离的:
量化算法需要对完整的模型权重进行操作,所以需要先合并。
A: 不会。合并只是将 LoRA 的增量权重加到基础模型上,是数学上的等价操作。
A: 会有轻微影响(1-5% 精度损失),但量化带来的速度和显存优势通常值得这个代价。
A:
A: 不建议。量化是有损压缩,应该在完整精度模型上微调,然后再量化。
# 步骤 1: LoRA 微调
python examples/qwen3.5_0.8b_local_finetune.py
# 步骤 2: 合并并量化
python examples/quantize_lora_model.py \
--base_model Qwen/Qwen3.5-0.5B \
--lora_path ./outputs/qwen3.5-0.8b-finetuned \
--method awq \
--bits 4
# 步骤 3: 测试量化模型
python scripts/inference.py \
--model_path ./outputs/qwen3.5-0.8b-awq \
--interactive
outputs/
├── qwen3.5-0.8b-finetuned/ # LoRA 权重(保留)
├── qwen3.5-0.8b-merged/ # 合并后的模型(可选)
└── qwen3.5-0.8b-awq/ # 量化模型(部署)
from transformers import AutoTokenizer
from awq import AutoAWQForCausalLM
# 加载量化模型
model = AutoAWQForCausalLM.from_quantized("./outputs/qwen3.5-0.8b-awq")
tokenizer = AutoTokenizer.from_pretrained("./outputs/qwen3.5-0.8b-awq")
# 测试
test_prompts = [
"请解释什么是机器学习",
"写一首关于春天的诗",
]
for prompt in test_prompts:
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=100)
print(f"输入:{prompt}")
print(f"输出:{tokenizer.decode(outputs[0])}\n")
# 问题:显存只有 4GB,需要部署 LoRA 微调的模型
# 解决:AWQ 4bit 量化
python examples/quantize_lora_model.py \
--base_model Qwen/Qwen3.5-0.5B \
--lora_path ./outputs/qwen3.5-0.8b-finetuned \
--method awq \
--bits 4
# 结果:显存占用从 7GB 降到 3GB
# 问题:只有 CPU 服务器,需要部署模型
# 解决:GGUF 量化
python examples/quantize_lora_model.py \
--base_model Qwen/Qwen3.5-0.5B \
--lora_path ./outputs/qwen3.5-0.8b-finetuned \
--method gguf \
--quant_type Q4_K_M
# 结果:可以在 CPU 上高效推理
# 问题:需要快速推理,保持高精度
# 解决:AWQ 4bit 量化
python examples/quantize_lora_model.py \
--base_model Qwen/Qwen3.5-0.5B \
--lora_path ./outputs/qwen3.5-0.8b-finetuned \
--method awq \
--bits 4 \
--output_path ./deploy/qwen3.5-0.8b-awq
# 结果:推理速度提升 20%,精度保持 95%+
FineTuneX 完全支持对 LoRA 微调模型的量化:
推荐使用 AWQ 4bit 量化,在速度和精度之间取得最佳平衡!
最后更新: 2026-03-30 版本: 0.1.0