🚀 Qwen2.5-7B-Instruct INT4 量化模型
这是基于 Qwen/Qwen2.5-7B-Instruct 的 INT4 量化版本,使用 bitsandbytes
库进行量化。
📊 模型信息
- 基础模型: Qwen/Qwen2.5-7B-Instruct
- 量化类型: INT4 (4-bit)
- 量化方法: BitsAndBytesConfig with NF4
- 压缩比率: ~3.5x
- 显存节省: ~75%
⚙️ 量化配置
from transformers import BitsAndBytesConfig
import torch
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_quant_storage=torch.uint8,
)
🚀 使用方法
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
# 量化配置
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_quant_storage=torch.uint8,
)
# 加载模型和分词器
model_name = "nikodoz/qwen2.5-7b-instruct-int4"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True,
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True
)
# 推理示例
messages = [
{"role": "system", "content": "你是一个有用的AI助手。"},
{"role": "user", "content": "请介绍一下机器学习。"}
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=512,
temperature=0.7,
top_p=0.8,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
)
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
print(response)
📈 性能对比
指标 | 原始模型 (FP16) | 量化模型 (INT4) | 提升 |
---|---|---|---|
模型大小 | ~14GB | ~4GB | 3.5x 压缩 |
显存使用 | ~14GB | ~4GB | 75% 减少 |
推理速度 | 基准 | 略快 | ~10% |
生成质量 | 100% | ~95% | 轻微损失 |
🔧 环境要求
- Python >= 3.8
- PyTorch >= 2.0.0
- transformers >= 4.40.0
- bitsandbytes >= 0.43.0
- CUDA >= 11.0
💡 注意事项
- 首次加载时需要进行量化,可能需要几分钟时间
- 需要支持 bitsandbytes 的 CUDA 环境
- 量化会带来轻微的精度损失,但显存使用显著减少
- 适合在资源受限的环境中部署大型语言模型
📄 许可证
本模型基于原始 Qwen2.5 模型,遵循 Apache-2.0 许可证。
🙏 致谢
- Qwen团队 提供的优秀基础模型
- BitsAndBytes 提供的量化技术
- Hugging Face 提供的模型托管平台
- Downloads last month
- 26
Inference Providers
NEW
This model isn't deployed by any Inference Provider.
🙋
Ask for provider support