Qwen2.5-VL-7B-Instruct的量化版本,支持视觉-文本输入和文本输出,权重量化为INT4,激活量化为FP16。
下载量 605
发布时间 : 2/7/2025
模型介绍
内容详情
替代品
模型简介
这是一个基于Qwen/Qwen2.5-VL-7B-Instruct的量化模型,专为高效推理而优化,适用于多模态任务。
模型特点
高效量化
权重量化为INT4,激活量化为FP16,显著减少模型大小和推理成本
多模态支持
支持视觉和文本输入,能够理解和生成与图像相关的文本内容
vLLM优化
专为vLLM推理引擎优化,支持高效部署
模型能力
视觉问答
图像描述生成
多模态对话
文档理解
使用案例
视觉问答
图像内容理解
回答关于图像内容的自然语言问题
在VQAv2数据集上达到73.90%准确率
文档处理
文档问答
从扫描文档或PDF中提取信息并回答问题
在DocVQA数据集上达到94.13% ANLS分数
标签:
- vllm
- 视觉
- w4a16 许可证: apache-2.0 许可证链接: >- https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md 语言:
- 英文 基础模型: Qwen/Qwen2.5-VL-7B-Instruct 库名称: transformers
Qwen2.5-VL-7B-Instruct-quantized-w4a16
模型概述
- 模型架构: Qwen/Qwen2.5-VL-7B-Instruct
- 输入: 视觉-文本
- 输出: 文本
- 模型优化:
- 权重量化: INT4
- 激活量化: FP16
- 发布日期: 2025年2月24日
- 版本: 1.0
- 模型开发者: Neural Magic
Qwen/Qwen2.5-VL-7B-Instruct的量化版本。
模型优化
该模型通过将Qwen/Qwen2.5-VL-7B-Instruct的权重量化为INT8数据类型获得,可用于vLLM >= 0.5.2的推理。
部署
使用vLLM
该模型可以使用vLLM后端高效部署,如下例所示。
from vllm.assets.image import ImageAsset
from vllm import LLM, SamplingParams
# 准备模型
llm = LLM(
model="neuralmagic/Qwen2.5-VL-7B-Instruct-quantized.w4a16",
trust_remote_code=True,
max_model_len=4096,
max_num_seqs=2,
)
# 准备输入
question = "这张图片的内容是什么?"
inputs = {
"prompt": f"<|user|>\n<|image_1|>\n{question}<|end|>\n<|assistant|>\n",
"multi_modal_data": {
"image": ImageAsset("cherry_blossom").pil_image.convert("RGB")
},
}
# 生成响应
print("========== 示例生成 ==============")
outputs = llm.generate(inputs, SamplingParams(temperature=0.2, max_tokens=64))
print(f"提示 : {outputs[0].prompt}")
print(f"响应: {outputs[0].outputs[0].text}")
print("==================================")
vLLM还支持OpenAI兼容的服务。更多详情请参阅文档。
创建
该模型是通过llm-compressor创建的,作为多模态公告博客的一部分运行了以下代码片段。
模型创建代码
import base64
from io import BytesIO
import torch
from datasets import load_dataset
from qwen_vl_utils import process_vision_info
from transformers import AutoProcessor
from llmcompressor.modifiers.quantization import GPTQModifier
from llmcompressor.transformers import oneshot
from llmcompressor.transformers.tracing import (
TraceableQwen2_5_VLForConditionalGeneration,
)
from compressed_tensors.quantization import QuantizationArgs, QuantizationType, QuantizationStrategy, ActivationOrdering, QuantizationScheme
# 加载模型
model_id = "Qwen/Qwen2.5-VL-7B-Instruct"
model = TraceableQwen2_5_VLForConditionalGeneration.from_pretrained(
model_id,
device_map="auto",
torch_dtype="auto",
)
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
# 单次参数
DATASET_ID = "lmms-lab/flickr30k"
DATASET_SPLIT = {"calibration": "test[:512]"}
NUM_CALIBRATION_SAMPLES = 512
MAX_SEQUENCE_LENGTH = 2048
# 加载数据集并预处理
ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
ds = ds.shuffle(seed=42)
dampening_frac=0.01
# 应用聊天模板并标记化输入
def preprocess_and_tokenize(example):
# 预处理
buffered = BytesIO()
example["image"].save(buffered, format="PNG")
encoded_image = base64.b64encode(buffered.getvalue())
encoded_image_text = encoded_image.decode("utf-8")
base64_qwen = f"data:image;base64,{encoded_image_text}"
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": base64_qwen},
{"type": "text", "text": "图片展示了什么?"},
],
}
]
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
# 标记化
return processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=False,
max_length=MAX_SEQUENCE_LENGTH,
truncation=True,
)
ds = ds.map(preprocess_and_tokenize, remove_columns=ds["calibration"].column_names)
# 为多模态输入定义单次数据收集器
def data_collator(batch):
assert len(batch) == 1
return {key: torch.tensor(value) for key, value in batch[0].items()}
recipe = GPTQModifier(
targets="Linear",
config_groups={
"config_group": QuantizationScheme(
targets=["Linear"],
weights=QuantizationArgs(
num_bits=4,
type=QuantizationType.INT,
strategy=QuantizationStrategy.GROUP,
group_size=128,
symmetric=True,
dynamic=False,
actorder=ActivationOrdering.WEIGHT,
),
),
},
sequential_targets=["Qwen2_5_VLDecoderLayer"],
ignore=["lm_head", "re:visual.*"],
update_size=NUM_CALIBRATION_SAMPLES,
dampening_frac=dampening_frac
)
SAVE_DIR=f"{model_id.split('/')[1]}-quantized.w4a16"
# 执行单次
oneshot(
model=model,
tokenizer=model_id,
dataset=ds,
recipe=recipe,
max_seq_length=MAX_SEQUENCE_LENGTH,
num_calibration_samples=NUM_CALIBRATION_SAMPLES,
trust_remote_code_model=True,
data_collator=data_collator,
output_dir=SAVE_DIR
)
评估
该模型使用mistral-evals进行视觉相关任务的评估,并使用lm_evaluation_harness进行部分基于文本的基准测试。评估通过以下命令进行:
评估命令
视觉任务
- vqav2
- docvqa
- mathvista
- mmmu
- chartqa
vllm serve neuralmagic/pixtral-12b-quantized.w8a8 --tensor_parallel_size 1 --max_model_len 25000 --trust_remote_code --max_num_seqs 8 --gpu_memory_utilization 0.9 --dtype float16 --limit_mm_per_prompt image=7
python -m eval.run eval_vllm \
--model_name neuralmagic/pixtral-12b-quantized.w8a8 \
--url http://0.0.0.0:8000 \
--output_dir ~/tmp \
--eval_name <vision_task_name>
基于文本的任务
MMLU
lm_eval \
--model vllm \
--model_args pretrained="<model_name>",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=<n>,gpu_memory_utilization=0.8,enable_chunked_prefill=True,trust_remote_code=True \
--tasks mmlu \
--num_fewshot 5 \
--batch_size auto \
--output_path output_dir
MGSM
lm_eval \
--model vllm \
--model_args pretrained="<model_name>",dtype=auto,max_model_len=4096,max_gen_toks=2048,max_num_seqs=128,tensor_parallel_size=<n>,gpu_memory_utilization=0.9 \
--tasks mgsm_cot_native \
--apply_chat_template \
--num_fewshot 0 \
--batch_size auto \
--output_path output_dir
准确率
类别 | 指标 | Qwen/Qwen2.5-VL-7B-Instruct | Qwen2.5-VL-7B-Instruct-quantized.W4A16 | 恢复率 (%) |
---|---|---|---|---|
视觉 | MMMU (val, CoT) explicit_prompt_relaxed_correctness |
52.00 | 51.11 | 98.29% |
VQAv2 (val) vqa_match |
75.59 | 73.90 | 97.76% | |
DocVQA (val) anls |
94.27 | 94.13 | 99.85% | |
ChartQA (test, CoT) anywhere_in_answer_relaxed_correctness |
86.44 | 85.64 | 99.07% | |
Mathvista (testmini, CoT) explicit_prompt_relaxed_correctness |
69.47 | 67.17 | 96.69% | |
平均分数 | 75.95 | 74.79 | 98.47% | |
文本 | MGSM (CoT) | 56.38 | 51.89 | 92.04% |
MMLU (5-shot) | 71.09 | 68.67 | 96.59% |
推理性能
该模型在单流部署中实现了高达2.35倍的加速,在多流异步部署中实现了高达2.02倍的加速,具体取决于硬件和使用场景。 以下性能基准测试使用vLLM版本0.7.2和GuideLLM进行。