许可证:apache-2.0
许可证链接:https://huggingface.co/Qwen/Qwen2.5-7B/blob/main/LICENSE
语言:
- 英文
任务标签:文本生成
基础模型:Qwen/Qwen2.5-7B-Instruct
标签:
- 聊天
- neuralmagic
- llmcompressor
- int8
Qwen2.5-7B-Instruct-quantized.w8a8
模型概述
- 模型架构: Qwen2
- 模型优化:
- 预期用途: 适用于商业和研究用途的多语言场景。与Qwen2.5-7B类似,该模型旨在用于类似助手的聊天功能。
- 非适用范围: 任何违反适用法律或法规(包括贸易合规法律)的使用方式。
- 发布日期: 2024年10月9日
- 版本: 1.0
- 许可证: apache-2.0
- 模型开发者: Neural Magic
模型优化
该模型通过对Qwen2.5-7B-Instruct的激活和权重进行INT8量化获得。
此优化将权重和激活的表示位数从16位减少到8位,从而降低了GPU内存需求(约减少50%)并提高了矩阵乘法计算吞吐量(约提升2倍)。
权重量化还减少了磁盘空间需求约50%。
仅对Transformer块内线性算子的权重和激活进行量化。
权重采用对称静态逐通道方案量化,而激活采用对称动态逐令牌方案量化。
结合SmoothQuant和GPTQ算法进行量化,实现方式基于llm-compressor库。
部署
该模型可以使用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兼容的服务。更多详情请参阅文档。
创建
创建详情
该模型通过运行以下代码片段使用[llm-compressor](https://github.com/vllm-project/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"模型和分词器保存至:{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-quantized.w8a8 (本模型)
|
恢复率
|
MMLU(5-shot)
|
74.24
|
73.87
|
99.5%
|
ARC挑战赛(25-shot)
|
63.40
|
63.23
|
99.7%
|
GSM-8K(5-shot,严格匹配)
|
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%
|