Views
No views yet
1from vllm.assets.image import ImageAsset
2from vllm import LLM, SamplingParams
3
4# prepare model
5llm = LLM(
6 model="neuralmagic/Qwen2.5-VL-72B-Instruct-quantized.w4a16",
7 trust_remote_code=True,
8 max_model_len=4096,
9 max_num_seqs=2,
10)
11
12# prepare inputs
13question = "What is the content of this image?"
14inputs = {
15 "prompt": f"<|user|>\n<|image_1|>\n{question}<|end|>\n<|assistant|>\n",
16 "multi_modal_data": {
17 "image": ImageAsset("cherry_blossom").pil_image.convert("RGB")
18 },
19}
20
21# generate response
22print("========== SAMPLE GENERATION ==============")
23outputs = llm.generate(inputs, SamplingParams(temperature=0.2, max_tokens=64))
24print(f"PROMPT : {outputs[0].prompt}")
25print(f"RESPONSE: {outputs[0].outputs[0].text}")
26print("==========================================")1import base64
2from io import BytesIO
3import torch
4from datasets import load_dataset
5from qwen_vl_utils import process_vision_info
6from transformers import AutoProcessor
7from llmcompressor.modifiers.quantization import GPTQModifier
8from llmcompressor.transformers import oneshot
9from llmcompressor.transformers.tracing import (
10 TraceableQwen2_5_VLForConditionalGeneration,
11)
12from compressed_tensors.quantization import QuantizationArgs, QuantizationType, QuantizationStrategy, ActivationOrdering, QuantizationScheme
13
14# Load model.
15model_id = "Qwen/Qwen2.5-VL-72B-Instruct"
16
17model = TraceableQwen2_5_VLForConditionalGeneration.from_pretrained(
18 model_id,
19 device_map="auto",
20 torch_dtype="auto",
21)
22processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
23
24# Oneshot arguments
25DATASET_ID = "lmms-lab/flickr30k"
26DATASET_SPLIT = {"calibration": "test[:512]"}
27NUM_CALIBRATION_SAMPLES = 512
28MAX_SEQUENCE_LENGTH = 2048
29
30# Load dataset and preprocess.
31ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
32ds = ds.shuffle(seed=42)
33dampening_frac=0.01
34
35# Apply chat template and tokenize inputs.
36def preprocess_and_tokenize(example):
37 # preprocess
38 buffered = BytesIO()
39 example["image"].save(buffered, format="PNG")
40 encoded_image = base64.b64encode(buffered.getvalue())
41 encoded_image_text = encoded_image.decode("utf-8")
42 base64_qwen = f"data:image;base64,{encoded_image_text}"
43 messages = [
44 {
45 "role": "user",
46 "content": [
47 {"type": "image", "image": base64_qwen},
48 {"type": "text", "text": "What does the image show?"},
49 ],
50 }
51 ]
52 text = processor.apply_chat_template(
53 messages, tokenize=False, add_generation_prompt=True
54 )
55 image_inputs, video_inputs = process_vision_info(messages)
56
57 # tokenize
58 return processor(
59 text=[text],
60 images=image_inputs,
61 videos=video_inputs,
62 padding=False,
63 max_length=MAX_SEQUENCE_LENGTH,
64 truncation=True,
65 )
66ds = ds.map(preprocess_and_tokenize, remove_columns=ds["calibration"].column_names)
67
68# Define a oneshot data collator for multimodal inputs.
69def data_collator(batch):
70 assert len(batch) == 1
71 return {key: torch.tensor(value) for key, value in batch[0].items()}
72
73recipe = GPTQModifier(
74 targets="Linear",
75 config_groups={
76 "config_group": QuantizationScheme(
77 targets=["Linear"],
78 weights=QuantizationArgs(
79 num_bits=4,
80 type=QuantizationType.INT,
81 strategy=QuantizationStrategy.GROUP,
82 group_size=128,
83 symmetric=True,
84 dynamic=False,
85 actorder=ActivationOrdering.WEIGHT,
86 ),
87 ),
88 },
89 sequential_targets=["Qwen2_5_VLDecoderLayer"],
90 ignore=["lm_head", "re:visual.*"],
91 update_size=NUM_CALIBRATION_SAMPLES,
92 dampening_frac=dampening_frac
93)
94
95SAVE_DIR=f"{model_id.split('/')[1]}-quantized.w4a16"
96
97# Perform oneshot
98oneshot(
99 model=model,
100 tokenizer=model_id,
101 dataset=ds,
102 recipe=recipe,
103 max_seq_length=MAX_SEQUENCE_LENGTH,
104 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
105 trust_remote_code_model=True,
106 data_collator=data_collator,
107 output_dir=SAVE_DIR
108)
109vllm serve RedHatAI/Qwen2.5-VL-72B-Instruct-quantized.w4a16 --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 RedHatAI/Qwen2.5-VL-72B-Instruct-quantized.w4a16 \
--url http://0.0.0.0:8000 \
--output_dir ~/tmp \
--eval_name <vision_task_name>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
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
| Category | Metric | Qwen/Qwen2.5-VL-72B-Instruct | neuralmagic/Qwen2.5-VL-72B-Instruct-quantized.w4a16 | Recovery (%) |
|---|---|---|---|---|
| Vision | MMMU (val, CoT) explicit_prompt_relaxed_correctness | 64.33 | 62.89 | 97.76% |
| VQAv2 (val) vqa_match | 81.94 | 81.87 | 99.91% | |
| DocVQA (val) anls | 94.71 | 94.72 | 100.01% | |
| ChartQA (test, CoT) anywhere_in_answer_relaxed_correctness | 88.96 | 88.96 | 100.00% | |
| Mathvista (testmini, CoT) explicit_prompt_relaxed_correctness | 78.18 | 77.68 | 99.36% | |
| Average Score | 81.62 | 81.22 | 99.51 | |
| Text | MGSM (CoT) | 75.45 | 75.13 | 99.58% |
| MMLU (5-shot) | 86.16 | 85.36 | 99.07% |
| Document Visual Question Answering 1680W x 2240H 64/128 | Visual Reasoning 640W x 480H 128/128 | Image Captioning 480W x 360H 0/128 | |||||||
|---|---|---|---|---|---|---|---|---|---|
| Hardware | Number of GPUs | Model | Average Cost Reduction | Latency (s) | Queries Per Dollar | Latency (s)th> | Queries Per Dollar | Latency (s) | Queries Per Dollar |
| A100 | 4 | Qwen/Qwen2.5-VL-72B-Instruct | 6.4 | 78 | 4.5 | 111 | 4.4 | 113 | |
| 2 | neuralmagic/Qwen2.5-VL-72B-Instruct-quantized.w8a8 | 1.85 | 7.0 | 143 | 4.9 | 205 | 4.8 | 211 | |
| 1 | neuralmagic/Qwen2.5-VL-72B-Instruct-quantized.w4a16 | 3.33 | 9.4 | 213 | 5.1 | 396 | 4.8 | 420 | |
| H100 | 4 | Qwen/Qwen2.5-VL-72B-Instruct | 4.3 | 68 | 3.0 | 97 | 2.9 | 100 | |
| 2 | neuralmagic/Qwen2.5-VL-72B-Instruct-FP8-Dynamic | 1.79 | 4.6 | 122 | 3.3 | 173 | 3.2 | 177 | |
| 1 | neuralmagic/Qwen2.5-VL-72B-Instruct-quantized.w4a16 | 5.66 | 4.3 | 252 | 4.4 | 251 | 4.2 | 259 |
| Document Visual Question Answering 1680W x 2240H 64/128 | Visual Reasoning 640W x 480H 128/128 | Image Captioning 480W x 360H 0/128 | ||||||
|---|---|---|---|---|---|---|---|---|
| Hardware | Model | Average Cost Reduction | Maximum throughput (QPS) | Queries Per Dollar | Maximum throughput (QPS) | Queries Per Dollar | Maximum throughput (QPS) | Queries Per Dollar |
| A100x4 | Qwen/Qwen2.5-VL-72B-Instruct | 0.4 | 180 | 1.1 | 539 | 1.2 | 595 | |
| neuralmagic/Qwen2.5-VL-72B-Instruct-quantized.w8a8 | 1.80 | 0.6 | 289 | 2.0 | 1020 | 2.3 | 1133 | |
| neuralmagic/Qwen2.5-VL-72B-Instruct-quantized.w4a16 | 2.75 | 0.7 | 341 | 3.2 | 1588 | 4.1 | 2037 | |
| H100x4 | Qwen/Qwen2.5-VL-72B-Instruct | 0.5 | 134 | 1.2 | 357 | 1.3 | 379 | |
| neuralmagic/Qwen2.5-VL-72B-Instruct-FP8-Dynamic | 1.73 | 0.9 | 247 | 2.2 | 621 | 2.4 | 669 | |
| neuralmagic/Qwen2.5-VL-72B-Instruct-quantized.w4a16 | 8.27 | 3.3 | 913 | 3.3 | 898 | 3.6 | 991 |