🚀 Qwen2.5-7B-Instruct量化模型(w8a8)
本项目是对Qwen2.5-7B-Instruct模型进行量化处理后的版本,将权重和激活值量化为INT8数据类型,有效降低了GPU内存需求和磁盘空间占用,同时提升了计算吞吐量,可用于多语言的商业和研究场景。
🚀 快速开始
本模型可以使用 vLLM 后端进行高效部署,示例代码如下:
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
model_id = "RedHatAI/Qwen2.5-7B-Instruct-quantized.w8a8"
number_gpus = 1
max_model_len = 8192
sampling_params = SamplingParams(temperature=0.7, top_p=0.8, max_tokens=256)
tokenizer = AutoTokenizer.from_pretrained(model_id)
messages = [
{"role": "user", "content": "Give me a short introduction to large language model."},
]
prompts = tokenizer.apply_chat_template(messages, tokenize=False)
llm = LLM(model=model_id, tensor_parallel_size=number_gpus, max_model_len=max_model_len)
outputs = llm.generate(prompts, sampling_params)
generated_text = outputs[0].outputs[0].text
print(generated_text)
vLLM 还支持兼容 OpenAI 的服务,更多详细信息请参阅 文档。
✨ 主要特性
- 模型架构:基于 Qwen2 架构,输入和输出均为文本。
- 模型优化:将 Qwen2.5-7B-Instruct 模型的激活值和权重量化为 INT8 数据类型,减少了 GPU 内存需求(约 50%),提高了矩阵乘法的计算吞吐量(约 2 倍),同时磁盘空间需求也减少了约 50%。
- 预期用例:适用于多语言的商业和研究用途,类似于 Qwen2.5-7B,可用于类似助手的聊天场景。
- 适用范围:不得用于任何违反适用法律法规(包括贸易合规法律)的方式。
📚 详细文档
模型优化
本模型是通过将 Qwen2.5-7B-Instruct 模型的激活值和权重量化为 INT8 数据类型得到的。仅对 Transformer 块内线性算子的权重和激活值进行量化,权重采用对称静态逐通道量化方案,激活值采用对称动态逐令牌量化方案。量化过程结合了 SmoothQuant 和 GPTQ 算法,具体实现使用了 llm-compressor 库。
模型创建
本模型使用 llm-compressor 创建,具体代码如下:
from transformers import AutoModelForCausalLM, AutoTokenizer
from llmcompressor.modifiers.quantization import GPTQModifier
from llmcompressor.modifiers.smoothquant import SmoothQuantModifier
from llmcompressor.transformers import oneshot
from datasets import load_dataset
model_stub = "Qwen/Qwen2.5-7B-Instruct"
model_name = model_stub.split("/")[-1]
num_samples = 512
max_seq_len = 8192
tokenizer = AutoTokenizer.from_pretrained(model_stub)
model = AutoModelForCausalLM.from_pretrained(
model_stub,
device_map="auto",
torch_dtype="auto",
)
def preprocess_fn(example):
return {"text": tokenizer.apply_chat_template(example["messages"], add_generation_prompt=False, tokenize=False)}
ds = load_dataset("neuralmagic/LLM_compression_calibration", split="train")
ds = ds.map(preprocess_fn)
recipe = [
SmoothQuantModifier(
smoothing_strength=0.8,
mappings=[
[["re:.*q_proj", "re:.*k_proj", "re:.*v_proj"], "re:.*input_layernorm"],
[["re:.*gate_proj", "re:.*up_proj"], "re:.*post_attention_layernorm"],
[["re:.*down_proj"], "re:.*up_proj"],
],
),
GPTQModifier(
ignore=["lm_head"],
sequential_targets=["Qwen2DecoderLayer"],
dampening_frac=0.01,
targets="Linear",
scheme="W8A8",
),
]
oneshot(
model=model,
dataset=ds,
recipe=recipe,
max_seq_length=max_seq_len,
num_calibration_samples=num_samples,
)
save_path = model_name + "-quantized.w8a8"
model.save_pretrained(save_path)
tokenizer.save_pretrained(save_path)
print(f"Model and tokenizer saved to: {save_path}")
模型评估
本模型在 OpenLLM 排行榜任务(版本 1)上进行了评估,使用 lm-evaluation-harness(提交版本 387Bbd54bc621086e05aa1b030d8d4d5635b25e6)和 vLLM 引擎,评估命令如下:
lm_eval \
--model vllm \
--model_args pretrained="neuralmagic/Qwen2.5-7B-Instruct-quantized.w8a8",dtype=auto,gpu_memory_utilization=0.5,max_model_len=4096,add_bos_token=True,enable_chunk_prefill=True,tensor_parallel_size=1 \
--tasks openllm \
--batch_size auto
准确率
以下是在 Open LLM 排行榜上的评估得分:
基准测试 |
Qwen2.5-7B-Instruct |
Qwen2.5-7B-Instruct量化模型(w8a8) |
恢复率 |
MMLU (5-shot) |
74.24 |
73.87 |
99.5% |
ARC Challenge (25-shot) |
63.40 |
63.23 |
99.7% |
GSM-8K (5-shot, strict-match) |
80.36 |
80.74 |
100.5% |
Hellaswag (10-shot) |
81.52 |
81.06 |
99.4% |
Winogrande (5-shot) |
74.66 |
74.82 |
100.2% |
TruthfulQA (0-shot, mc2) |
64.76 |
64.58 |
99.7% |
平均 |
73.16 |
73.05 |
99.4% |
🔧 技术细节
- 量化方案:仅对 Transformer 块内线性算子的权重和激活值进行量化,权重采用对称静态逐通道量化方案,激活值采用对称动态逐令牌量化方案。
- 量化算法:结合了 SmoothQuant 和 GPTQ 算法。
📄 许可证
本模型采用 Apache-2.0 许可证。