Gabriel commited on
Commit
06e9a0e
·
verified ·
1 Parent(s): e706c52

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +1 -509
README.md CHANGED
@@ -9,513 +9,5 @@ base_model: Qwen/Qwen2-VL-7B-Instruct
9
  library_name: transformers
10
  ---
11
 
12
- # Qwen2-VL-7B-Instruct-AWQ
13
 
14
- ## Introduction
15
-
16
- We're excited to unveil **Qwen2-VL**, the latest iteration of our Qwen-VL model, representing nearly a year of innovation.
17
-
18
- ### What’s New in Qwen2-VL?
19
-
20
- #### Key Enhancements:
21
-
22
-
23
- * **SoTA understanding of images of various resolution & ratio**: Qwen2-VL achieves state-of-the-art performance on visual understanding benchmarks, including MathVista, DocVQA, RealWorldQA, MTVQA, etc.
24
-
25
- * **Understanding videos of 20min+**: Qwen2-VL can understand videos over 20 minutes for high-quality video-based question answering, dialog, content creation, etc.
26
-
27
- * **Agent that can operate your mobiles, robots, etc.**: with the abilities of complex reasoning and decision making, Qwen2-VL can be integrated with devices like mobile phones, robots, etc., for automatic operation based on visual environment and text instructions.
28
-
29
- * **Multilingual Support**: to serve global users, besides English and Chinese, Qwen2-VL now supports the understanding of texts in different languages inside images, including most European languages, Japanese, Korean, Arabic, Vietnamese, etc.
30
-
31
-
32
- #### Model Architecture Updates:
33
-
34
- * **Naive Dynamic Resolution**: Unlike before, Qwen2-VL can handle arbitrary image resolutions, mapping them into a dynamic number of visual tokens, offering a more human-like visual processing experience.
35
-
36
- <p align="center">
37
- <img src="https://qianwen-res.oss-accelerate-overseas.aliyuncs.com/Qwen2-VL/qwen2_vl.jpg" width="80%"/>
38
- <p>
39
-
40
- * **Multimodal Rotary Position Embedding (M-ROPE)**: Decomposes positional embedding into parts to capture 1D textual, 2D visual, and 3D video positional information, enhancing its multimodal processing capabilities.
41
-
42
- <p align="center">
43
- <img src="http://qianwen-res.oss-accelerate-overseas.aliyuncs.com/Qwen2-VL/mrope.png" width="80%"/>
44
- <p>
45
-
46
- We have three models with 2, 7 and 72 billion parameters. This repo contains the instruction-tuned 7B Qwen2-VL model. For more information, visit our [Blog](https://qwenlm.github.io/blog/qwen2-vl/) and [GitHub](https://github.com/QwenLM/Qwen2-VL).
47
-
48
-
49
-
50
- ### Benchmark
51
- #### Performance of Quantized Models
52
- This section reports the generation performance of quantized models (including GPTQ and AWQ) of the Qwen2-VL series. Specifically, we report:
53
-
54
- - MMMU_VAL (Accuracy)
55
- - DocVQA_VAL (Accuracy)
56
- - MMBench_DEV_EN (Accuracy)
57
- - MathVista_MINI (Accuracy)
58
-
59
- We use [VLMEvalkit](https://github.com/kq-chen/VLMEvalKit/tree/add_qwen2vl) to evaluate all models.
60
-
61
-
62
- | Model Size | Quantization | MMMU | DocVQA | MMBench | MathVista |
63
- | --- | --- | --- | --- | --- | --- |
64
- | Qwen2-VL-7B-Instruct | BF16<br><sup>([🤗](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct)[🤖](https://modelscope.cn/models/qwen/Qwen2-VL-7B-Instruct)) | 53.77 | 93.89 | 81.78 | 58.20 |
65
- | | GPTQ-Int8<br><sup>([🤗](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct-GPTQ-Int8)[🤖](https://modelscope.cn/models/qwen/Qwen2-VL-7B-Instruct-GPTQ-Int8)) | 53.00 | 93.94 | 82.38 | 57.90 |
66
- | | GPTQ-Int4<br><sup>([🤗](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4)[🤖](https://modelscope.cn/models/qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4)) | 52.55 | 93.16 | 81.27 | 60.30 |
67
- | | AWQ<br><sup>([🤗](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct-AWQ)[🤖](https://modelscope.cn/models/qwen/Qwen2-VL-7B-Instruct-AWQ)) | 53.66 | 93.10 | 81.61 | 56.80 |
68
-
69
-
70
-
71
- #### Speed Benchmark
72
- This section reports the speed performance of bf16 models, quantized models (including GPTQ-Int4, GPTQ-Int8 and AWQ) of the Qwen2-VL series. Specifically, we report the inference speed (tokens/s) as well as memory footprint (GB) under the conditions of different context lengths.
73
-
74
- The environment of the evaluation with huggingface transformers is:
75
-
76
- - NVIDIA A100 80GB
77
- - CUDA 11.8
78
- - Pytorch 2.2.1+cu118
79
- - Flash Attention 2.6.1
80
- - Transformers 4.38.2
81
- - AutoGPTQ 0.6.0+cu118
82
- - AutoAWQ 0.2.5+cu118 (autoawq_kernels 0.0.6+cu118)
83
-
84
- Note:
85
-
86
- - We use the batch size of 1 and the least number of GPUs as possible for the evalution.
87
- - We test the speed and memory of generating 2048 tokens with the input lengths of 1, 6144, 14336, 30720, 63488, and 129024 tokens (>32k is only avaliable for Qwen2-72B-Instuct and Qwen2-7B-Instuct).
88
-
89
- - 7B (transformers)
90
-
91
- | Model | Input Length | Quantization | GPU Num | Speed(tokens/s) | GPU Memory(GB) |
92
- | --- | --- | --- | --- | --- | --- |
93
- | Qwen2-VL-7B-Instruct | 1 | BF16 | 1 | 39.02 | 16.07 |
94
- | | | GPTQ-Int8 | 1 | 31.60 | 10.11 |
95
- | | | GPTQ-Int4 | 1 | 42.76 | 7.20 |
96
- | | | AWQ | 1 | 32.08 | 7.07 |
97
- | | 6144 | BF16 | 1 | 38.75 | 21.56 |
98
- | | | GPTQ-Int8 | 1 | 31.31 | 15.61 |
99
- | | | GPTQ-Int4 | 1 | 39.75 | 12.69 |
100
- | | | AWQ | 1 | 32.66 | 12.56 |
101
- | | 14336 | BF16 | 1 | 30.65 | 29.07 |
102
- | | | GPTQ-Int8 | 1 | 27.96 | 23.11 |
103
- | | | GPTQ-Int4 | 1 | 29.72 | 20.20 |
104
- | | | AWQ | 1 | 31.42 | 20.07 |
105
- | | 30720 | BF16 | 1 | 19.53 | 44.08 |
106
- | | | GPTQ-Int8 | 1 | 18.37 | 38.13 |
107
- | | | GPTQ-Int4 | 1 | 19.15 | 35.22 |
108
- | | | AWQ | 1 | 19.95 | 35.08 |
109
-
110
-
111
-
112
-
113
- ## Requirements
114
- The code of Qwen2-VL has been in the latest Hugging face transformers and we advise you to build from source with command `pip install git+https://github.com/huggingface/transformers`, or you might encounter the following error:
115
- ```
116
- KeyError: 'qwen2_vl'
117
- ```
118
-
119
- ## Quickstart
120
- We offer a toolkit to help you handle various types of visual input more conveniently. This includes base64, URLs, and interleaved images and videos. You can install it using the following command:
121
-
122
- ```bash
123
- pip install qwen-vl-utils
124
- ```
125
-
126
- Here we show a code snippet to show you how to use the chat model with `transformers` and `qwen_vl_utils`:
127
-
128
- ```python
129
- from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
130
- from qwen_vl_utils import process_vision_info
131
-
132
- # default: Load the model on the available device(s)
133
- model = Qwen2VLForConditionalGeneration.from_pretrained(
134
- "Qwen/Qwen2-VL-7B-Instruct-AWQ", torch_dtype="auto", device_map="auto"
135
- )
136
-
137
- # We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
138
- # model = Qwen2VLForConditionalGeneration.from_pretrained(
139
- # "Qwen/Qwen2-VL-7B-Instruct-AWQ",
140
- # torch_dtype=torch.bfloat16,
141
- # attn_implementation="flash_attention_2",
142
- # device_map="auto",
143
- # )
144
-
145
- # default processer
146
- processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct-AWQ")
147
-
148
- # The default range for the number of visual tokens per image in the model is 4-16384. You can set min_pixels and max_pixels according to your needs, such as a token count range of 256-1280, to balance speed and memory usage.
149
- # min_pixels = 256*28*28
150
- # max_pixels = 1280*28*28
151
- # processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels)
152
-
153
- messages = [
154
- {
155
- "role": "user",
156
- "content": [
157
- {
158
- "type": "image",
159
- "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
160
- },
161
- {"type": "text", "text": "Describe this image."},
162
- ],
163
- }
164
- ]
165
-
166
- # Preparation for inference
167
- text = processor.apply_chat_template(
168
- messages, tokenize=False, add_generation_prompt=True
169
- )
170
- image_inputs, video_inputs = process_vision_info(messages)
171
- inputs = processor(
172
- text=[text],
173
- images=image_inputs,
174
- videos=video_inputs,
175
- padding=True,
176
- return_tensors="pt",
177
- )
178
- inputs = inputs.to("cuda")
179
-
180
- # Inference: Generation of the output
181
- generated_ids = model.generate(**inputs, max_new_tokens=128)
182
- generated_ids_trimmed = [
183
- out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
184
- ]
185
- output_text = processor.batch_decode(
186
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
187
- )
188
- print(output_text)
189
- ```
190
- <details>
191
- <summary>Without qwen_vl_utils</summary>
192
-
193
- ```python
194
- from PIL import Image
195
- import requests
196
- import torch
197
- from torchvision import io
198
- from typing import Dict
199
- from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
200
-
201
- # Load the model in half-precision on the available device(s)
202
- model = Qwen2VLForConditionalGeneration.from_pretrained(
203
- "Qwen/Qwen2-VL-7B-Instruct-AWQ", torch_dtype="auto", device_map="auto"
204
- )
205
- processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct-AWQ")
206
-
207
- # Image
208
- url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
209
- image = Image.open(requests.get(url, stream=True).raw)
210
-
211
- conversation = [
212
- {
213
- "role": "user",
214
- "content": [
215
- {
216
- "type": "image",
217
- },
218
- {"type": "text", "text": "Describe this image."},
219
- ],
220
- }
221
- ]
222
-
223
-
224
- # Preprocess the inputs
225
- text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
226
- # Excepted output: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Describe this image.<|im_end|>\n<|im_start|>assistant\n'
227
-
228
- inputs = processor(
229
- text=[text_prompt], images=[image], padding=True, return_tensors="pt"
230
- )
231
- inputs = inputs.to("cuda")
232
-
233
- # Inference: Generation of the output
234
- output_ids = model.generate(**inputs, max_new_tokens=128)
235
- generated_ids = [
236
- output_ids[len(input_ids) :]
237
- for input_ids, output_ids in zip(inputs.input_ids, output_ids)
238
- ]
239
- output_text = processor.batch_decode(
240
- generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
241
- )
242
- print(output_text)
243
- ```
244
- </details>
245
- <details>
246
- <summary>Multi image inference</summary>
247
-
248
- ```python
249
- # Messages containing multiple images and a text query
250
- messages = [
251
- {
252
- "role": "user",
253
- "content": [
254
- {"type": "image", "image": "file:///path/to/image1.jpg"},
255
- {"type": "image", "image": "file:///path/to/image2.jpg"},
256
- {"type": "text", "text": "Identify the similarities between these images."},
257
- ],
258
- }
259
- ]
260
-
261
- # Preparation for inference
262
- text = processor.apply_chat_template(
263
- messages, tokenize=False, add_generation_prompt=True
264
- )
265
- image_inputs, video_inputs = process_vision_info(messages)
266
- inputs = processor(
267
- text=[text],
268
- images=image_inputs,
269
- videos=video_inputs,
270
- padding=True,
271
- return_tensors="pt",
272
- )
273
- inputs = inputs.to("cuda")
274
-
275
- # Inference
276
- generated_ids = model.generate(**inputs, max_new_tokens=128)
277
- generated_ids_trimmed = [
278
- out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
279
- ]
280
- output_text = processor.batch_decode(
281
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
282
- )
283
- print(output_text)
284
- ```
285
- </details>
286
-
287
- <details>
288
- <summary>Video inference</summary>
289
-
290
- ```python
291
- # Messages containing a images list as a video and a text query
292
- messages = [
293
- {
294
- "role": "user",
295
- "content": [
296
- {
297
- "type": "video",
298
- "video": [
299
- "file:///path/to/frame1.jpg",
300
- "file:///path/to/frame2.jpg",
301
- "file:///path/to/frame3.jpg",
302
- "file:///path/to/frame4.jpg",
303
- ],
304
- "fps": 1.0,
305
- },
306
- {"type": "text", "text": "Describe this video."},
307
- ],
308
- }
309
- ]
310
- # Messages containing a video and a text query
311
- messages = [
312
- {
313
- "role": "user",
314
- "content": [
315
- {
316
- "type": "video",
317
- "video": "file:///path/to/video1.mp4",
318
- "max_pixels": 360 * 420,
319
- "fps": 1.0,
320
- },
321
- {"type": "text", "text": "Describe this video."},
322
- ],
323
- }
324
- ]
325
-
326
- # Preparation for inference
327
- text = processor.apply_chat_template(
328
- messages, tokenize=False, add_generation_prompt=True
329
- )
330
- image_inputs, video_inputs = process_vision_info(messages)
331
- inputs = processor(
332
- text=[text],
333
- images=image_inputs,
334
- videos=video_inputs,
335
- padding=True,
336
- return_tensors="pt",
337
- )
338
- inputs = inputs.to("cuda")
339
-
340
- # Inference
341
- generated_ids = model.generate(**inputs, max_new_tokens=128)
342
- generated_ids_trimmed = [
343
- out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
344
- ]
345
- output_text = processor.batch_decode(
346
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
347
- )
348
- print(output_text)
349
- ```
350
- </details>
351
-
352
- <details>
353
- <summary>Batch inference</summary>
354
-
355
- ```python
356
- # Sample messages for batch inference
357
- messages1 = [
358
- {
359
- "role": "user",
360
- "content": [
361
- {"type": "image", "image": "file:///path/to/image1.jpg"},
362
- {"type": "image", "image": "file:///path/to/image2.jpg"},
363
- {"type": "text", "text": "What are the common elements in these pictures?"},
364
- ],
365
- }
366
- ]
367
- messages2 = [
368
- {"role": "system", "content": "You are a helpful assistant."},
369
- {"role": "user", "content": "Who are you?"},
370
- ]
371
- # Combine messages for batch processing
372
- messages = [messages1, messages1]
373
-
374
- # Preparation for batch inference
375
- texts = [
376
- processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
377
- for msg in messages
378
- ]
379
- image_inputs, video_inputs = process_vision_info(messages)
380
- inputs = processor(
381
- text=texts,
382
- images=image_inputs,
383
- videos=video_inputs,
384
- padding=True,
385
- return_tensors="pt",
386
- )
387
- inputs = inputs.to("cuda")
388
-
389
- # Batch Inference
390
- generated_ids = model.generate(**inputs, max_new_tokens=128)
391
- generated_ids_trimmed = [
392
- out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
393
- ]
394
- output_texts = processor.batch_decode(
395
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
396
- )
397
- print(output_texts)
398
- ```
399
- </details>
400
-
401
- ### More Usage Tips
402
-
403
- For input images, we support local files, base64, and URLs. For videos, we currently only support local files.
404
-
405
- ```python
406
- # You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
407
- ## Local file path
408
- messages = [
409
- {
410
- "role": "user",
411
- "content": [
412
- {"type": "image", "image": "file:///path/to/your/image.jpg"},
413
- {"type": "text", "text": "Describe this image."},
414
- ],
415
- }
416
- ]
417
- ## Image URL
418
- messages = [
419
- {
420
- "role": "user",
421
- "content": [
422
- {"type": "image", "image": "http://path/to/your/image.jpg"},
423
- {"type": "text", "text": "Describe this image."},
424
- ],
425
- }
426
- ]
427
- ## Base64 encoded image
428
- messages = [
429
- {
430
- "role": "user",
431
- "content": [
432
- {"type": "image", "image": "data:image;base64,/9j/..."},
433
- {"type": "text", "text": "Describe this image."},
434
- ],
435
- }
436
- ]
437
- ```
438
- #### Image Resolution for performance boost
439
-
440
- 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.
441
-
442
- ```python
443
- min_pixels = 256 * 28 * 28
444
- max_pixels = 1280 * 28 * 28
445
- processor = AutoProcessor.from_pretrained(
446
- "Qwen/Qwen2-VL-7B-Instruct-AWQ", min_pixels=min_pixels, max_pixels=max_pixels
447
- )
448
- ```
449
-
450
- Besides, We provide two methods for fine-grained control over the image size input to the model:
451
-
452
- 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.
453
-
454
- 2. Specify exact dimensions: Directly set `resized_height` and `resized_width`. These values will be rounded to the nearest multiple of 28.
455
-
456
- ```python
457
- # min_pixels and max_pixels
458
- messages = [
459
- {
460
- "role": "user",
461
- "content": [
462
- {
463
- "type": "image",
464
- "image": "file:///path/to/your/image.jpg",
465
- "resized_height": 280,
466
- "resized_width": 420,
467
- },
468
- {"type": "text", "text": "Describe this image."},
469
- ],
470
- }
471
- ]
472
- # resized_height and resized_width
473
- messages = [
474
- {
475
- "role": "user",
476
- "content": [
477
- {
478
- "type": "image",
479
- "image": "file:///path/to/your/image.jpg",
480
- "min_pixels": 50176,
481
- "max_pixels": 50176,
482
- },
483
- {"type": "text", "text": "Describe this image."},
484
- ],
485
- }
486
- ]
487
- ```
488
-
489
- ## Limitations
490
-
491
- While Qwen2-VL are applicable to a wide range of visual tasks, it is equally important to understand its limitations. Here are some known restrictions:
492
-
493
- 1. Lack of Audio Support: The current model does **not comprehend audio information** within videos.
494
- 2. Data timeliness: Our image dataset is **updated until June 2023**, and information subsequent to this date may not be covered.
495
- 3. Constraints in Individuals and Intellectual Property (IP): The model's capacity to recognize specific individuals or IPs is limited, potentially failing to comprehensively cover all well-known personalities or brands.
496
- 4. Limited Capacity for Complex Instruction: When faced with intricate multi-step instructions, the model's understanding and execution capabilities require enhancement.
497
- 5. Insufficient Counting Accuracy: Particularly in complex scenes, the accuracy of object counting is not high, necessitating further improvements.
498
- 6. Weak Spatial Reasoning Skills: Especially in 3D spaces, the model's inference of object positional relationships is inadequate, making it difficult to precisely judge the relative positions of objects.
499
-
500
- These limitations serve as ongoing directions for model optimization and improvement, and we are committed to continually enhancing the model's performance and scope of application.
501
-
502
-
503
- ## Citation
504
-
505
- If you find our work helpful, feel free to give us a cite.
506
-
507
- ```
508
- @article{Qwen2VL,
509
- title={Qwen2-VL: Enhancing Vision-Language Model's Perception of the World at Any Resolution},
510
- author={Wang, Peng and Bai, Shuai and Tan, Sinan and Wang, Shijie and Fan, Zhihao and Bai, Jinze and Chen, Keqin and Liu, Xuejing and Wang, Jialin and Ge, Wenbin and Fan, Yang and Dang, Kai and Du, Mengfei and Ren, Xuancheng and Men, Rui and Liu, Dayiheng and Zhou, Chang and Zhou, Jingren and Lin, Junyang},
511
- journal={arXiv preprint arXiv:2409.12191},
512
- year={2024}
513
- }
514
-
515
- @article{Qwen-VL,
516
- title={Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond},
517
- author={Bai, Jinze and Bai, Shuai and Yang, Shusheng and Wang, Shijie and Tan, Sinan and Wang, Peng and Lin, Junyang and Zhou, Chang and Zhou, Jingren},
518
- journal={arXiv preprint arXiv:2308.12966},
519
- year={2023}
520
- }
521
- ```
 
9
  library_name: transformers
10
  ---
11
 
12
+ # Qwen2-VL-7B-Instruct-AWQ -fork
13