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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ pipeline_tag: image-text-to-text
6
+ tags:
7
+ - multimodal
8
+ library_name: transformers
9
+ ---
10
+
11
+ # Qwen2.5-VL-32B-Instruct-AWQ
12
+ <a href="https://chat.qwenlm.ai/" target="_blank" style="margin: 2px;">
13
+ <img alt="Chat" src="https://img.shields.io/badge/%F0%9F%92%9C%EF%B8%8F%20Qwen%20Chat%20-536af5" style="display: inline-block; vertical-align: middle;"/>
14
+ </a>
15
+
16
+
17
+ ## Latest Updates:
18
+ In addition to the original formula, we have further enhanced Qwen2.5-VL-32B's mathematical and problem-solving abilities through reinforcement learning. This has also significantly improved the model's subjective user experience, with response styles adjusted to better align with human preferences. Particularly for objective queries such as mathematics, logical reasoning, and knowledge-based Q&A, the level of detail in responses and the clarity of formatting have been noticeably enhanced.
19
+
20
+ ## Introduction
21
+
22
+ In the past five months since Qwen2-VL’s release, numerous developers have built new models on the Qwen2-VL vision-language models, providing us with valuable feedback. During this period, we focused on building more useful vision-language models. Today, we are excited to introduce the latest addition to the Qwen family: Qwen2.5-VL.
23
+
24
+ #### Key Enhancements:
25
+ * **Understand things visually**: Qwen2.5-VL is not only proficient in recognizing common objects such as flowers, birds, fish, and insects, but it is highly capable of analyzing texts, charts, icons, graphics, and layouts within images.
26
+
27
+ * **Being agentic**: Qwen2.5-VL directly plays as a visual agent that can reason and dynamically direct tools, which is capable of computer use and phone use.
28
+
29
+ * **Understanding long videos and capturing events**: Qwen2.5-VL can comprehend videos of over 1 hour, and this time it has a new ability of cpaturing event by pinpointing the relevant video segments.
30
+
31
+ * **Capable of visual localization in different formats**: Qwen2.5-VL can accurately localize objects in an image by generating bounding boxes or points, and it can provide stable JSON outputs for coordinates and attributes.
32
+
33
+ * **Generating structured outputs**: for data like scans of invoices, forms, tables, etc. Qwen2.5-VL supports structured outputs of their contents, benefiting usages in finance, commerce, etc.
34
+
35
+
36
+ #### Model Architecture Updates:
37
+
38
+ * **Dynamic Resolution and Frame Rate Training for Video Understanding**:
39
+
40
+ We extend dynamic resolution to the temporal dimension by adopting dynamic FPS sampling, enabling the model to comprehend videos at various sampling rates. Accordingly, we update mRoPE in the time dimension with IDs and absolute time alignment, enabling the model to learn temporal sequence and speed, and ultimately acquire the ability to pinpoint specific moments.
41
+
42
+ <p align="center">
43
+ <img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-VL/qwen2.5vl_arc.jpeg" width="80%"/>
44
+ <p>
45
+
46
+
47
+ * **Streamlined and Efficient Vision Encoder**
48
+
49
+ We enhance both training and inference speeds by strategically implementing window attention into the ViT. The ViT architecture is further optimized with SwiGLU and RMSNorm, aligning it with the structure of the Qwen2.5 LLM.
50
+
51
+
52
+ We have three models with 3, 7 and 72 billion parameters. This repository contains the quantized instruction-tuned 32B Qwen2.5-VL model. For more information, visit our [Blog](https://qwenlm.github.io/blog/qwen2.5-vl/) and [GitHub](https://github.com/QwenLM/Qwen2.5-VL).
53
+
54
+
55
+
56
+ ## Evaluation
57
+
58
+ | Model | MMMU | DocVQA_VAL | MMBench_DEV_EN | MathVista_MINI |
59
+ |---------------------------|--------------------|------------|------------------------|----------------|
60
+ | Qwen2.5-VL-32B-Instruct | 70.0 | 93.9107 | 87.3 | 74.7 |
61
+ | Qwen2.5-VL-32B-Instruct-AWQ | 67.8 | 94.1489 | 86.9 | 73.6 |
62
+
63
+
64
+
65
+ ## Requirements
66
+ The code of Qwen2.5-VL has been in the latest Hugging face transformers and we advise you to build from source with command:
67
+ ```
68
+
69
+ pip install git+https://github.com/huggingface/transformers accelerate
70
+
71
+ ```
72
+ or you might encounter the following error:
73
+ ```
74
+
75
+ KeyError: 'qwen2_5_vl'
76
+
77
+ ```
78
+ ## Quickstart
79
+
80
+ Below, we provide simple examples to show how to use Qwen2.5-VL with πŸ€– ModelScope and πŸ€— Transformers.
81
+
82
+ The code of Qwen2.5-VL has been in the latest Hugging face transformers and we advise you to build from source with command:
83
+ ```
84
+
85
+ pip install git+https://github.com/huggingface/transformers accelerate
86
+
87
+ ```
88
+ or you might encounter the following error:
89
+ ```
90
+
91
+ KeyError: 'qwen2_5_vl'
92
+
93
+ ```
94
+ We offer a toolkit to help you handle various types of visual input more conveniently, as if you were using an API. This includes base64, URLs, and interleaved images and videos. You can install it using the following command:
95
+
96
+ ```bash
97
+ # It's highly recommanded to use `[decord]` feature for faster video loading.
98
+ pip install qwen-vl-utils[decord]==0.0.8
99
+ ```
100
+
101
+ If you are not using Linux, you might not be able to install `decord` from PyPI. In that case, you can use `pip install qwen-vl-utils` which will fall back to using torchvision for video processing. However, you can still [install decord from source](https://github.com/dmlc/decord?tab=readme-ov-file#install-from-source) to get decord used when loading video.
102
+
103
+ ### Using πŸ€— Transformers to Chat
104
+
105
+ Here we show a code snippet to show you how to use the chat model with `transformers` and `qwen_vl_utils`:
106
+
107
+ ```python
108
+ from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
109
+ from qwen_vl_utils import process_vision_info
110
+
111
+ # default: Load the model on the available device(s)
112
+ model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
113
+ "Qwen/Qwen2.5-VL-32B-Instruct-AWQ", torch_dtype="auto", device_map="auto"
114
+ )
115
+
116
+ # We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
117
+ # model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
118
+ # "Qwen/Qwen2.5-VL-32B-Instruct-AWQ",
119
+ # torch_dtype=torch.bfloat16,
120
+ # attn_implementation="flash_attention_2",
121
+ # device_map="auto",
122
+ # )
123
+
124
+ # default processer
125
+ processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-32B-Instruct-AWQ")
126
+
127
+ # The default range for the number of visual tokens per image in the model is 4-16384.
128
+ # You can set min_pixels and max_pixels according to your needs, such as a token range of 256-1280, to balance performance and cost.
129
+ # min_pixels = 256*28*28
130
+ # max_pixels = 1280*28*28
131
+ # processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-32B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels)
132
+
133
+ messages = [
134
+ {
135
+ "role": "user",
136
+ "content": [
137
+ {
138
+ "type": "image",
139
+ "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
140
+ },
141
+ {"type": "text", "text": "Describe this image."},
142
+ ],
143
+ }
144
+ ]
145
+
146
+ # Preparation for inference
147
+ text = processor.apply_chat_template(
148
+ messages, tokenize=False, add_generation_prompt=True
149
+ )
150
+ image_inputs, video_inputs = process_vision_info(messages)
151
+ inputs = processor(
152
+ text=[text],
153
+ images=image_inputs,
154
+ videos=video_inputs,
155
+ padding=True,
156
+ return_tensors="pt",
157
+ )
158
+ inputs = inputs.to("cuda")
159
+
160
+ # Inference: Generation of the output
161
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
162
+ generated_ids_trimmed = [
163
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
164
+ ]
165
+ output_text = processor.batch_decode(
166
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
167
+ )
168
+ print(output_text)
169
+ ```
170
+
171
+ <details>
172
+ <summary>Multi image inference</summary>
173
+
174
+ ```python
175
+ # Messages containing multiple images and a text query
176
+ messages = [
177
+ {
178
+ "role": "user",
179
+ "content": [
180
+ {"type": "image", "image": "file:///path/to/image1.jpg"},
181
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
182
+ {"type": "text", "text": "Identify the similarities between these images."},
183
+ ],
184
+ }
185
+ ]
186
+
187
+ # Preparation for inference
188
+ text = processor.apply_chat_template(
189
+ messages, tokenize=False, add_generation_prompt=True
190
+ )
191
+ image_inputs, video_inputs = process_vision_info(messages)
192
+ inputs = processor(
193
+ text=[text],
194
+ images=image_inputs,
195
+ videos=video_inputs,
196
+ padding=True,
197
+ return_tensors="pt",
198
+ )
199
+ inputs = inputs.to("cuda")
200
+
201
+ # Inference
202
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
203
+ generated_ids_trimmed = [
204
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
205
+ ]
206
+ output_text = processor.batch_decode(
207
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
208
+ )
209
+ print(output_text)
210
+ ```
211
+
212
+ </details>
213
+
214
+ <details>
215
+ <summary>Video inference</summary>
216
+
217
+ ```python
218
+ # Messages containing a images list as a video and a text query
219
+ messages = [
220
+ {
221
+ "role": "user",
222
+ "content": [
223
+ {
224
+ "type": "video",
225
+ "video": [
226
+ "file:///path/to/frame1.jpg",
227
+ "file:///path/to/frame2.jpg",
228
+ "file:///path/to/frame3.jpg",
229
+ "file:///path/to/frame4.jpg",
230
+ ],
231
+ },
232
+ {"type": "text", "text": "Describe this video."},
233
+ ],
234
+ }
235
+ ]
236
+
237
+ # Messages containing a local video path and a text query
238
+ messages = [
239
+ {
240
+ "role": "user",
241
+ "content": [
242
+ {
243
+ "type": "video",
244
+ "video": "file:///path/to/video1.mp4",
245
+ "max_pixels": 360 * 420,
246
+ "fps": 1.0,
247
+ },
248
+ {"type": "text", "text": "Describe this video."},
249
+ ],
250
+ }
251
+ ]
252
+
253
+ # Messages containing a video url and a text query
254
+ messages = [
255
+ {
256
+ "role": "user",
257
+ "content": [
258
+ {
259
+ "type": "video",
260
+ "video": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-VL/space_woaudio.mp4",
261
+ },
262
+ {"type": "text", "text": "Describe this video."},
263
+ ],
264
+ }
265
+ ]
266
+
267
+ #In Qwen 2.5 VL, frame rate information is also input into the model to align with absolute time.
268
+ # Preparation for inference
269
+ text = processor.apply_chat_template(
270
+ messages, tokenize=False, add_generation_prompt=True
271
+ )
272
+ image_inputs, video_inputs, video_kwargs = process_vision_info(messages, return_video_kwargs=True)
273
+ inputs = processor(
274
+ text=[text],
275
+ images=image_inputs,
276
+ videos=video_inputs,
277
+ fps=fps,
278
+ padding=True,
279
+ return_tensors="pt",
280
+ **video_kwargs,
281
+ )
282
+ inputs = inputs.to("cuda")
283
+
284
+ # Inference
285
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
286
+ generated_ids_trimmed = [
287
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
288
+ ]
289
+ output_text = processor.batch_decode(
290
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
291
+ )
292
+ print(output_text)
293
+ ```
294
+
295
+ Video URL compatibility largely depends on the third-party library version. The details are in the table below. change the backend by `FORCE_QWENVL_VIDEO_READER=torchvision` or `FORCE_QWENVL_VIDEO_READER=decord` if you prefer not to use the default one.
296
+
297
+ | Backend | HTTP | HTTPS |
298
+ |-------------|------|-------|
299
+ | torchvision >= 0.19.0 | βœ… | βœ… |
300
+ | torchvision < 0.19.0 | ❌ | ❌ |
301
+ | decord | βœ… | ❌ |
302
+
303
+ </details>
304
+
305
+ <details>
306
+ <summary>Batch inference</summary>
307
+
308
+ ```python
309
+ # Sample messages for batch inference
310
+ messages1 = [
311
+ {
312
+ "role": "user",
313
+ "content": [
314
+ {"type": "image", "image": "file:///path/to/image1.jpg"},
315
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
316
+ {"type": "text", "text": "What are the common elements in these pictures?"},
317
+ ],
318
+ }
319
+ ]
320
+ messages2 = [
321
+ {"role": "system", "content": "You are a helpful assistant."},
322
+ {"role": "user", "content": "Who are you?"},
323
+ ]
324
+ # Combine messages for batch processing
325
+ messages = [messages1, messages2]
326
+
327
+ # Preparation for batch inference
328
+ texts = [
329
+ processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
330
+ for msg in messages
331
+ ]
332
+ image_inputs, video_inputs = process_vision_info(messages)
333
+ inputs = processor(
334
+ text=texts,
335
+ images=image_inputs,
336
+ videos=video_inputs,
337
+ padding=True,
338
+ return_tensors="pt",
339
+ )
340
+ inputs = inputs.to("cuda")
341
+
342
+ # Batch Inference
343
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
344
+ generated_ids_trimmed = [
345
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
346
+ ]
347
+ output_texts = processor.batch_decode(
348
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
349
+ )
350
+ print(output_texts)
351
+ ```
352
+
353
+ </details>
354
+
355
+ ### πŸ€– ModelScope
356
+
357
+ We strongly advise users especially those in mainland China to use ModelScope. `snapshot_download` can help you solve issues concerning downloading checkpoints.
358
+
359
+ ### More Usage Tips
360
+
361
+ For input images, we support local files, base64, and URLs. For videos, we currently only support local files.
362
+
363
+ ```python
364
+ # You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
365
+ ## Local file path
366
+ messages = [
367
+ {
368
+ "role": "user",
369
+ "content": [
370
+ {"type": "image", "image": "file:///path/to/your/image.jpg"},
371
+ {"type": "text", "text": "Describe this image."},
372
+ ],
373
+ }
374
+ ]
375
+ ## Image URL
376
+ messages = [
377
+ {
378
+ "role": "user",
379
+ "content": [
380
+ {"type": "image", "image": "http://path/to/your/image.jpg"},
381
+ {"type": "text", "text": "Describe this image."},
382
+ ],
383
+ }
384
+ ]
385
+ ## Base64 encoded image
386
+ messages = [
387
+ {
388
+ "role": "user",
389
+ "content": [
390
+ {"type": "image", "image": "data:image;base64,/9j/..."},
391
+ {"type": "text", "text": "Describe this image."},
392
+ ],
393
+ }
394
+ ]
395
+ ```
396
+
397
+ #### Image Resolution for performance boost
398
+
399
+ The model supports a wide range of resolution inputs. By default, it uses the native resolution for input, but higher resolutions can enhance performance at the cost of more computation. Users can set the minimum and maximum number of pixels to achieve an optimal configuration for their needs, such as a token count range of 256-1280, to balance speed and memory usage.
400
+
401
+ ```python
402
+ min_pixels = 256 * 28 * 28
403
+ max_pixels = 1280 * 28 * 28
404
+ processor = AutoProcessor.from_pretrained(
405
+ "Qwen/Qwen2.5-VL-32B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels
406
+ )
407
+ ```
408
+
409
+ Besides, We provide two methods for fine-grained control over the image size input to the model:
410
+
411
+ 1. Define min_pixels and max_pixels: Images will be resized to maintain their aspect ratio within the range of min_pixels and max_pixels.
412
+ 2. Specify exact dimensions: Directly set `resized_height` and `resized_width`. These values will be rounded to the nearest multiple of 28.
413
+
414
+ ```python
415
+ # min_pixels and max_pixels
416
+ messages = [
417
+ {
418
+ "role": "user",
419
+ "content": [
420
+ {
421
+ "type": "image",
422
+ "image": "file:///path/to/your/image.jpg",
423
+ "resized_height": 280,
424
+ "resized_width": 420,
425
+ },
426
+ {"type": "text", "text": "Describe this image."},
427
+ ],
428
+ }
429
+ ]
430
+ # resized_height and resized_width
431
+ messages = [
432
+ {
433
+ "role": "user",
434
+ "content": [
435
+ {
436
+ "type": "image",
437
+ "image": "file:///path/to/your/image.jpg",
438
+ "min_pixels": 50176,
439
+ "max_pixels": 50176,
440
+ },
441
+ {"type": "text", "text": "Describe this image."},
442
+ ],
443
+ }
444
+ ]
445
+ ```
446
+
447
+ ### Processing Long Texts
448
+
449
+ The current `config.json` is set for context length up to 32,768 tokens.
450
+ To handle extensive inputs exceeding 32,768 tokens, we utilize [YaRN](https://arxiv.org/abs/2309.00071), a technique for enhancing model length extrapolation, ensuring optimal performance on lengthy texts.
451
+
452
+ For supported frameworks, you could add the following to `config.json` to enable YaRN:
453
+
454
+ {
455
+ ...,
456
+ "type": "yarn",
457
+ "mrope_section": [
458
+ 16,
459
+ 24,
460
+ 24
461
+ ],
462
+ "factor": 4,
463
+ "original_max_position_embeddings": 32768
464
+ }
465
+
466
+ However, it should be noted that this method has a significant impact on the performance of temporal and spatial localization tasks, and is therefore not recommended for use.
467
+
468
+ At the same time, for long video inputs, since MRoPE itself is more economical with ids, the max_position_embeddings can be directly modified to a larger value, such as 64k.
469
+
470
+ ## Citation
471
+
472
+ If you find our work helpful, feel free to give us a cite.
473
+
474
+ ```
475
+ @article{Qwen2.5-VL,
476
+ title={Qwen2.5-VL Technical Report},
477
+ author={Bai, Shuai and Chen, Keqin and Liu, Xuejing and Wang, Jialin and Ge, Wenbin and Song, Sibo and Dang, Kai and Wang, Peng and Wang, Shijie and Tang, Jun and Zhong, Humen and Zhu, Yuanzhi and Yang, Mingkun and Li, Zhaohai and Wan, Jianqiang and Wang, Pengfei and Ding, Wei and Fu, Zheren and Xu, Yiheng and Ye, Jiabo and Zhang, Xi and Xie, Tianbao and Cheng, Zesen and Zhang, Hang and Yang, Zhibo and Xu, Haiyang and Lin, Junyang},
478
+ journal={arXiv preprint arXiv:2502.13923},
479
+ year={2025}
480
+ }
481
+ ```
482
+