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-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 TraceableQwen2VLForConditionalGeneration
10from llmcompressor.transformers.utils.data_collator import qwen2_vl_data_collator
11from compressed_tensors.quantization import QuantizationArgs, QuantizationType, QuantizationStrategy, ActivationOrdering, QuantizationScheme
12
13# Load model.
14model_id = "Qwen/Qwen2-VL-72B-Instruct"
15
16model = TraceableQwen2VLForConditionalGeneration.from_pretrained(
17 model_id,
18 device_map="auto",
19 torch_dtype="auto",
20)
21processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
22
23# Oneshot arguments
24DATASET_ID = "lmms-lab/flickr30k"
25DATASET_SPLIT = {"calibration": "test[:512]"}
26NUM_CALIBRATION_SAMPLES = 512
27MAX_SEQUENCE_LENGTH = 2048
28
29# Load dataset and preprocess.
30ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
31ds = ds.shuffle(seed=42)
32dampening_frac=0.01
33
34# Apply chat template and tokenize inputs.
35def preprocess_and_tokenize(example):
36 # preprocess
37 buffered = BytesIO()
38 example["image"].save(buffered, format="PNG")
39 encoded_image = base64.b64encode(buffered.getvalue())
40 encoded_image_text = encoded_image.decode("utf-8")
41 base64_qwen = f"data:image;base64,{encoded_image_text}"
42 messages = [
43 {
44 "role": "user",
45 "content": [
46 {"type": "image", "image": base64_qwen},
47 {"type": "text", "text": "What does the image show?"},
48 ],
49 }
50 ]
51 text = processor.apply_chat_template(
52 messages, tokenize=False, add_generation_prompt=True
53 )
54 image_inputs, video_inputs = process_vision_info(messages)
55
56 # tokenize
57 return processor(
58 text=[text],
59 images=image_inputs,
60 videos=video_inputs,
61 padding=False,
62 max_length=MAX_SEQUENCE_LENGTH,
63 truncation=True,
64 )
65
66ds = ds.map(preprocess_and_tokenize, remove_columns=ds["calibration"].column_names)
67
68# Recipe
69recipe = GPTQModifier(
70 targets="Linear",
71 config_groups={
72 "config_group": QuantizationScheme(
73 targets=["Linear"],
74 weights=QuantizationArgs(
75 num_bits=4,
76 type=QuantizationType.INT,
77 strategy=QuantizationStrategy.GROUP,
78 group_size=128,
79 symmetric=True,
80 dynamic=False,
81 actorder=ActivationOrdering.WEIGHT,
82 ),
83 ),
84 },
85 sequential_targets=["Qwen2VLDecoderLayer"],
86 ignore=["lm_head", "re:visual.*"],
87 update_size=NUM_CALIBRATION_SAMPLES,
88 dampening_frac=dampening_frac
89)
90
91SAVE_DIR=f"{model_id.split('/')[1]}-quantized.w4a16
92
93# Perform oneshot
94oneshot(
95 model=model,
96 tokenizer=model_id,
97 dataset=ds,
98 recipe=recipe,
99 max_seq_length=MAX_SEQUENCE_LENGTH,
100 num_calibration_samples=NUM_CALIBRATION_SAMPLES,
101 trust_remote_code_model=True,
102 data_collator=qwen2_vl_data_collator,
103 output_dir=SAVE_DIR
104)
105vllm 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>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-VL-72B-Instruct | nm-testing/Qwen2-VL-72B-Instruct-quantized.W4A16 | Recovery (%) |
|---|---|---|---|---|
| Vision | MMMU (val, CoT) explicit_prompt_relaxed_correctness | 62.11 | 60.11 | 96.78% |
| VQAv2 (val) vqa_match | 82.51 | 82.38 | 99.84% | |
| DocVQA (val) anls | 95.01 | 94.94 | 99.93% | |
| ChartQA (test, CoT) anywhere_in_answer_relaxed_correctness | 83.40 | 80.72 | 96.78% | |
| Mathvista (testmini, CoT) explicit_prompt_relaxed_correctness | 66.57 | 64.66 | 97.13% | |
| Average Score | 77.92 | 76.56 | 98.26 | |
| Text | MGSM (CoT) | 68.60 | 66.45 | 96.87% |
| MMLU (5-shot) | 82.70 | 82.35 | 99.58% |
| 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) | QPD | Latency (s)th> | QPD | Latency (s) | QPD |
| A100 | 4 | Qwen/Qwen2-VL-72B-Instruct | 6.5 | 77 | 4.6 | 110 | 4.4 | 113 | |
| 2 | neuralmagic/Qwen2-VL-72B-Instruct-quantized.w8a8 | 1.85 | 7.2 | 139 | 4.9 | 206 | 4.8 | 211 | |
| 1 | neuralmagic/Qwen2-VL-72B-Instruct-quantized.w4a16 | 3.32 | 10.0 | 202 | 5.0 | 398 | 4.8 | 419 | |
| H100 | 4 | Qwen/Qwen2-VL-72B-Instruct | 4.4 | 66 | 3.0 | 97 | 2.9 | 99 | |
| 2 | neuralmagic/Qwen2-VL-72B-Instruct-FP8-Dynamic | 1.79 | 4.7 | 119 | 3.3 | 173 | 3.2 | 177 | |
| 1 | neuralmagic/Qwen2-VL-72B-Instruct-quantized.w4a16 | 2.60 | 6.4 | 172 | 4.3 | 253 | 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) | QPD | Maximum throughput (QPS) | QPD | Maximum throughput (QPS) | QPD |
| A100x4 | Qwen/Qwen2-VL-72B-Instruct | 0.3 | 169 | 1.1 | 538 | 1.2 | 595 | |
| neuralmagic/Qwen2-VL-72B-Instruct-quantized.w8a8 | 1.84 | 0.6 | 293 | 2.0 | 1021 | 2.3 | 1135 | |
| neuralmagic/Qwen2-VL-72B-Instruct-quantized.w4a16 | 2.73 | 0.6 | 314 | 3.2 | 1591 | 4.0 | 2019 | |
| H100x4 | Qwen/Qwen2-VL-72B-Instruct | 0.5 | 137 | 1.2 | 356 | 1.3 | 377 | |
| neuralmagic/Qwen2-VL-72B-Instruct-FP8-Dynamic | 1.70 | 0.8 | 236 | 2.2 | 623 | 2.4 | 669 | |
| neuralmagic/Qwen2-VL-72B-Instruct-quantized.w4a16 | 2.35 | 1.3 | 350 | 3.3 | 910 | 3.6 | 994 |