vision_language.py 24.3 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
"""
Cyrus Leung's avatar
Cyrus Leung committed
3
4
This example shows how to use vLLM for running offline inference with
the correct prompt format on vision language models for text generation.
5
6
7
8

For most models, the prompt format should follow corresponding examples
on HuggingFace model repository.
"""
9
10
import random

11
12
13
14
from transformers import AutoTokenizer

from vllm import LLM, SamplingParams
from vllm.assets.image import ImageAsset
15
from vllm.assets.video import VideoAsset
16
17
from vllm.utils import FlexibleArgumentParser

18
19
20
21
# NOTE: The default `max_num_seqs` and `max_model_len` may result in OOM on
# lower-end GPUs.
# Unless specified, these settings have been tested to work on a single L4.

22

23
24
25
26
27
# Aria
def run_aria(question: str, modality: str):
    assert modality == "image"
    model_name = "rhymes-ai/Aria"

28
    # NOTE: Need L40 (or equivalent) to avoid OOM
29
    llm = LLM(model=model_name,
30
31
              max_model_len=4096,
              max_num_seqs=2,
32
              dtype="bfloat16",
33
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
34

35
    prompt = (f"<|im_start|>user\n<fim_prefix><|img|><fim_suffix>{question}"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
              "<|im_end|>\n<|im_start|>assistant\n")

    stop_token_ids = [93532, 93653, 944, 93421, 1019, 93653, 93519]
    return llm, prompt, stop_token_ids


# BLIP-2
def run_blip2(question: str, modality: str):
    assert modality == "image"

    # BLIP-2 prompt format is inaccurate on HuggingFace model repository.
    # See https://huggingface.co/Salesforce/blip2-opt-2.7b/discussions/15#64ff02f3f8cf9e4f5b038262 #noqa
    prompt = f"Question: {question} Answer:"
    llm = LLM(model="Salesforce/blip2-opt-2.7b",
50
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
51
52
53
54
55
56
57
58
59
60
61
    stop_token_ids = None
    return llm, prompt, stop_token_ids


# Chameleon
def run_chameleon(question: str, modality: str):
    assert modality == "image"

    prompt = f"{question}<image>"
    llm = LLM(model="facebook/chameleon-7b",
              max_model_len=4096,
62
              max_num_seqs=2,
63
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
64
65
66
67
    stop_token_ids = None
    return llm, prompt, stop_token_ids


68
69
70
71
# Deepseek-VL2
def run_deepseek_vl2(question: str, modality: str):
    assert modality == "image"

72
    model_name = "deepseek-ai/deepseek-vl2-tiny"
73
74
75
76
77
78
79
80
81
82
83
84

    llm = LLM(model=model_name,
              max_model_len=4096,
              max_num_seqs=2,
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
              hf_overrides={"architectures": ["DeepseekVLV2ForCausalLM"]})

    prompt = f"<|User|>: <image>\n{question}\n\n<|Assistant|>:"
    stop_token_ids = None
    return llm, prompt, stop_token_ids


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Florence2
def run_florence2(question: str, modality: str):
    assert modality == "image"

    llm = LLM(model="microsoft/Florence-2-large",
              tokenizer="facebook/bart-large",
              max_num_seqs=8,
              trust_remote_code=True,
              dtype="bfloat16",
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)

    prompt = "<MORE_DETAILED_CAPTION>"
    stop_token_ids = None
    return llm, prompt, stop_token_ids


101
102
103
104
105
106
107
108
# Fuyu
def run_fuyu(question: str, modality: str):
    assert modality == "image"

    prompt = f"{question}\n"
    llm = LLM(model="adept/fuyu-8b",
              max_model_len=2048,
              max_num_seqs=2,
109
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    stop_token_ids = None
    return llm, prompt, stop_token_ids


# GLM-4v
def run_glm4v(question: str, modality: str):
    assert modality == "image"
    model_name = "THUDM/glm-4v-9b"

    llm = LLM(model=model_name,
              max_model_len=2048,
              max_num_seqs=2,
              trust_remote_code=True,
              enforce_eager=True,
124
              hf_overrides={"architectures": ["GLM4VForCausalLM"]},
125
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
126

127
128
129
    prompt = f"<|user|>\n<|begin_of_image|><|endoftext|><|end_of_image|>\
        {question}<|assistant|>"

130
131
132
133
134
135
136
137
    stop_token_ids = [151329, 151336, 151338]
    return llm, prompt, stop_token_ids


# H2OVL-Mississippi
def run_h2ovl(question: str, modality: str):
    assert modality == "image"

138
    model_name = "h2oai/h2ovl-mississippi-800m"
139
140
141
142
143

    llm = LLM(
        model=model_name,
        trust_remote_code=True,
        max_model_len=8192,
144
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
145
146
147
148
149
150
151
152
153
154
    )

    tokenizer = AutoTokenizer.from_pretrained(model_name,
                                              trust_remote_code=True)
    messages = [{'role': 'user', 'content': f"<image>\n{question}"}]
    prompt = tokenizer.apply_chat_template(messages,
                                           tokenize=False,
                                           add_generation_prompt=True)

    # Stop tokens for H2OVL-Mississippi
155
    # https://huggingface.co/h2oai/h2ovl-mississippi-800m
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    stop_token_ids = [tokenizer.eos_token_id]
    return llm, prompt, stop_token_ids


# Idefics3-8B-Llama3
def run_idefics3(question: str, modality: str):
    assert modality == "image"
    model_name = "HuggingFaceM4/Idefics3-8B-Llama3"

    llm = LLM(
        model=model_name,
        max_model_len=8192,
        max_num_seqs=2,
        enforce_eager=True,
        # if you are running out of memory, you can reduce the "longest_edge".
        # see: https://huggingface.co/HuggingFaceM4/Idefics3-8B-Llama3#model-optimizations
        mm_processor_kwargs={
            "size": {
                "longest_edge": 3 * 364
            },
        },
177
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    )
    prompt = (
        f"<|begin_of_text|>User:<image>{question}<end_of_utterance>\nAssistant:"
    )
    stop_token_ids = None
    return llm, prompt, stop_token_ids


# InternVL
def run_internvl(question: str, modality: str):
    assert modality == "image"

    model_name = "OpenGVLab/InternVL2-2B"

    llm = LLM(
        model=model_name,
        trust_remote_code=True,
        max_model_len=4096,
196
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    )

    tokenizer = AutoTokenizer.from_pretrained(model_name,
                                              trust_remote_code=True)
    messages = [{'role': 'user', 'content': f"<image>\n{question}"}]
    prompt = tokenizer.apply_chat_template(messages,
                                           tokenize=False,
                                           add_generation_prompt=True)

    # Stop tokens for InternVL
    # models variants may have different stop tokens
    # please refer to the model card for the correct "stop words":
    # https://huggingface.co/OpenGVLab/InternVL2-2B/blob/main/conversation.py
    stop_tokens = ["<|endoftext|>", "<|im_start|>", "<|im_end|>", "<|end|>"]
    stop_token_ids = [tokenizer.convert_tokens_to_ids(i) for i in stop_tokens]
    return llm, prompt, stop_token_ids


215
# LLaVA-1.5
216
def run_llava(question: str, modality: str):
217
    assert modality == "image"
218
219
220

    prompt = f"USER: <image>\n{question}\nASSISTANT:"

221
222
    llm = LLM(model="llava-hf/llava-1.5-7b-hf",
              max_model_len=4096,
223
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
224
225
    stop_token_ids = None
    return llm, prompt, stop_token_ids
226
227
228


# LLaVA-1.6/LLaVA-NeXT
229
def run_llava_next(question: str, modality: str):
230
    assert modality == "image"
231
232

    prompt = f"[INST] <image>\n{question} [/INST]"
233
234
    llm = LLM(model="llava-hf/llava-v1.6-mistral-7b-hf",
              max_model_len=8192,
235
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
236
237
238
239
240
241
    stop_token_ids = None
    return llm, prompt, stop_token_ids


# LlaVA-NeXT-Video
# Currently only support for video input
242
def run_llava_next_video(question: str, modality: str):
243
244
    assert modality == "video"

245
    prompt = f"USER: <video>\n{question} ASSISTANT:"
246
247
    llm = LLM(model="llava-hf/LLaVA-NeXT-Video-7B-hf",
              max_model_len=8192,
248
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
249
250
    stop_token_ids = None
    return llm, prompt, stop_token_ids
251
252


253
# LLaVA-OneVision
254
def run_llava_onevision(question: str, modality: str):
255
256
257
258
259
260
261
262
263
264

    if modality == "video":
        prompt = f"<|im_start|>user <video>\n{question}<|im_end|> \
        <|im_start|>assistant\n"

    elif modality == "image":
        prompt = f"<|im_start|>user <image>\n{question}<|im_end|> \
        <|im_start|>assistant\n"

    llm = LLM(model="llava-hf/llava-onevision-qwen2-7b-ov-hf",
265
              max_model_len=16384,
266
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
267
268
269
270
    stop_token_ids = None
    return llm, prompt, stop_token_ids


271
272
# Mantis
def run_mantis(question: str, modality: str):
273
    assert modality == "image"
274

275
276
    llama3_template = '<|start_header_id|>user<|end_header_id|>\n\n{}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n'  # noqa: E501
    prompt = llama3_template.format(f"{question}\n<image>")
277
278

    llm = LLM(
279
        model="TIGER-Lab/Mantis-8B-siglip-llama3",
280
        max_model_len=4096,
281
        hf_overrides={"architectures": ["MantisForConditionalGeneration"]},
282
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
283
    )
284
    stop_token_ids = [128009]
285
    return llm, prompt, stop_token_ids
286
287
288


# MiniCPM-V
289
290
291
def run_minicpmv_base(question: str, modality: str, model_name):
    assert modality in ["image", "video"]
    # If you want to use `MiniCPM-o-2_6` with audio inputs, check `audio_language.py` # noqa
292
293
294
295
296
297
298

    # 2.0
    # The official repo doesn't work yet, so we need to use a fork for now
    # For more details, please see: See: https://github.com/vllm-project/vllm/pull/4087#issuecomment-2250397630 # noqa
    # model_name = "HwwwH/MiniCPM-V-2"

    # 2.5
299
300
    # model_name = "openbmb/MiniCPM-Llama3-V-2_5"

301
    # 2.6
302
303
304
305
306
307
308
309
310
    # model_name = "openbmb/MiniCPM-V-2_6"
    # o2.6

    # modality supports
    # 2.0: image
    # 2.5: image
    # 2.6: image, video
    # o2.6: image, video, audio
    # model_name = "openbmb/MiniCPM-o-2_6"
311
312
313
314
    tokenizer = AutoTokenizer.from_pretrained(model_name,
                                              trust_remote_code=True)
    llm = LLM(
        model=model_name,
315
316
        max_model_len=4096,
        max_num_seqs=2,
317
        trust_remote_code=True,
318
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
319
    )
320
321
322
323
324
325
326
    # NOTE The stop_token_ids are different for various versions of MiniCPM-V
    # 2.0
    # stop_token_ids = [tokenizer.eos_id]

    # 2.5
    # stop_token_ids = [tokenizer.eos_id, tokenizer.eot_id]

327
    # 2.6 / o2.6
328
329
    stop_tokens = ['<|im_end|>', '<|endoftext|>']
    stop_token_ids = [tokenizer.convert_tokens_to_ids(i) for i in stop_tokens]
330

331
332
333
334
335
    modality_placeholder = {
        "image": "(<image>./</image>)",
        "video": "(<video>./</video>)",
    }

336
337
    messages = [{
        'role': 'user',
338
        'content': f'{modality_placeholder[modality]}\n{question}'
339
340
341
342
    }]
    prompt = tokenizer.apply_chat_template(messages,
                                           tokenize=False,
                                           add_generation_prompt=True)
343
    return llm, prompt, stop_token_ids
344
345


346
347
348
349
350
351
352
353
def run_minicpmo(question: str, modality: str):
    return run_minicpmv_base(question, modality, "openbmb/MiniCPM-o-2_6")


def run_minicpmv(question: str, modality: str):
    return run_minicpmv_base(question, modality, "openbmb/MiniCPM-V-2_6")


354
355
# LLama 3.2
def run_mllama(question: str, modality: str):
356
357
    assert modality == "image"

358
    model_name = "meta-llama/Llama-3.2-11B-Vision-Instruct"
359

360
361
362
363
364
    # Note: The default setting of max_num_seqs (256) and
    # max_model_len (131072) for this model may cause OOM.
    # You may lower either to run this example on lower-end GPUs.

    # The configuration below has been confirmed to launch on a single L40 GPU.
365
366
    llm = LLM(
        model=model_name,
367
368
        max_model_len=4096,
        max_num_seqs=16,
369
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
370
371
    )

372
373
374
375
376
377
378
379
380
381
382
383
384
385
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    messages = [{
        "role":
        "user",
        "content": [{
            "type": "image"
        }, {
            "type": "text",
            "text": f"{question}"
        }]
    }]
    prompt = tokenizer.apply_chat_template(messages,
                                           add_generation_prompt=True,
                                           tokenize=False)
386
    stop_token_ids = None
387
388
389
    return llm, prompt, stop_token_ids


390
391
# Molmo
def run_molmo(question, modality):
392
393
    assert modality == "image"

394
    model_name = "allenai/Molmo-7B-D-0924"
395

396
    llm = LLM(
397
        model=model_name,
398
        trust_remote_code=True,
399
        dtype="bfloat16",
400
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
401
    )
402

403
404
    prompt = question
    stop_token_ids = None
405
    return llm, prompt, stop_token_ids
406
407


408
409
410
411
412
413
414
415
416
417
418
419
# NVLM-D
def run_nvlm_d(question: str, modality: str):
    assert modality == "image"

    model_name = "nvidia/NVLM-D-72B"

    # Adjust this as necessary to fit in GPU
    llm = LLM(
        model=model_name,
        trust_remote_code=True,
        max_model_len=4096,
        tensor_parallel_size=4,
420
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
421
422
423
424
425
426
427
428
429
430
431
432
    )

    tokenizer = AutoTokenizer.from_pretrained(model_name,
                                              trust_remote_code=True)
    messages = [{'role': 'user', 'content': f"<image>\n{question}"}]
    prompt = tokenizer.apply_chat_template(messages,
                                           tokenize=False,
                                           add_generation_prompt=True)
    stop_token_ids = None
    return llm, prompt, stop_token_ids


433
434
# PaliGemma
def run_paligemma(question: str, modality: str):
435
    assert modality == "image"
436

437
438
439
    # PaliGemma has special prompt format for VQA
    prompt = "caption en"
    llm = LLM(model="google/paligemma-3b-mix-224",
440
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
441
442
    stop_token_ids = None
    return llm, prompt, stop_token_ids
443
444


445
446
# PaliGemma 2
def run_paligemma2(question: str, modality: str):
447
    assert modality == "image"
448

449
450
451
    # PaliGemma 2 has special prompt format for VQA
    prompt = "caption en"
    llm = LLM(model="google/paligemma2-3b-ft-docci-448",
452
              disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache)
453
454
455
456
    stop_token_ids = None
    return llm, prompt, stop_token_ids


457
458
# Phi-3-Vision
def run_phi3v(question: str, modality: str):
459
460
    assert modality == "image"

461
    prompt = f"<|user|>\n<|image_1|>\n{question}<|end|>\n<|assistant|>\n"
462

463
464
465
466
467
468
469
470
471
472
473
474
    # num_crops is an override kwarg to the multimodal image processor;
    # For some models, e.g., Phi-3.5-vision-instruct, it is recommended
    # to use 16 for single frame scenarios, and 4 for multi-frame.
    #
    # Generally speaking, a larger value for num_crops results in more
    # tokens per image instance, because it may scale the image more in
    # the image preprocessing. Some references in the model docs and the
    # formula for image tokens after the preprocessing
    # transform can be found below.
    #
    # https://huggingface.co/microsoft/Phi-3.5-vision-instruct#loading-the-model-locally
    # https://huggingface.co/microsoft/Phi-3.5-vision-instruct/blob/main/processing_phi3_v.py#L194
475
    llm = LLM(
476
477
        model="microsoft/Phi-3.5-vision-instruct",
        trust_remote_code=True,
478
        max_model_len=4096,
479
        max_num_seqs=2,
480
        # Note - mm_processor_kwargs can also be passed to generate/chat calls
481
        mm_processor_kwargs={"num_crops": 16},
482
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
483
484
485
486
487
    )
    stop_token_ids = None
    return llm, prompt, stop_token_ids


488
489
490
491
492
493
# Pixtral HF-format
def run_pixtral_hf(question: str, modality: str):
    assert modality == "image"

    model_name = "mistral-community/pixtral-12b"

494
    # NOTE: Need L40 (or equivalent) to avoid OOM
495
496
497
    llm = LLM(
        model=model_name,
        max_model_len=8192,
498
        max_num_seqs=2,
499
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
500
501
502
503
504
505
506
    )

    prompt = f"<s>[INST]{question}\n[IMG][/INST]"
    stop_token_ids = None
    return llm, prompt, stop_token_ids


507
508
# Qwen
def run_qwen_vl(question: str, modality: str):
509
510
511
    assert modality == "image"

    llm = LLM(
512
        model="Qwen/Qwen-VL",
513
        trust_remote_code=True,
514
515
        max_model_len=1024,
        max_num_seqs=2,
516
        hf_overrides={"architectures": ["QwenVLForConditionalGeneration"]},
517
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
518
519
    )

520
    prompt = f"{question}Picture 1: <img></img>\n"
521
522
523
524
    stop_token_ids = None
    return llm, prompt, stop_token_ids


525
526
# Qwen2-VL
def run_qwen2_vl(question: str, modality: str):
527

528
    model_name = "Qwen/Qwen2-VL-7B-Instruct"
529

530
531
    llm = LLM(
        model=model_name,
532
533
534
        max_model_len=4096,
        max_num_seqs=5,
        # Note - mm_processor_kwargs can also be passed to generate/chat calls
535
        mm_processor_kwargs={
536
537
            "min_pixels": 28 * 28,
            "max_pixels": 1280 * 28 * 28,
538
        },
539
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
540
    )
541

542
543
544
545
546
    if modality == "image":
        placeholder = "<|image_pad|>"
    elif modality == "video":
        placeholder = "<|video_pad|>"

547
    prompt = ("<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n"
548
              f"<|im_start|>user\n<|vision_start|>{placeholder}<|vision_end|>"
549
550
551
              f"{question}<|im_end|>\n"
              "<|im_start|>assistant\n")
    stop_token_ids = None
552
553
554
    return llm, prompt, stop_token_ids


Roger Wang's avatar
Roger Wang committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# Qwen2.5-VL
def run_qwen2_5_vl(question: str, modality: str):

    model_name = "Qwen/Qwen2.5-VL-3B-Instruct"

    llm = LLM(
        model=model_name,
        max_model_len=4096,
        max_num_seqs=5,
        mm_processor_kwargs={
            "min_pixels": 28 * 28,
            "max_pixels": 1280 * 28 * 28,
            "fps": 1,
        },
        disable_mm_preprocessor_cache=args.disable_mm_preprocessor_cache,
    )

    if modality == "image":
        placeholder = "<|image_pad|>"
    elif modality == "video":
        placeholder = "<|video_pad|>"

    prompt = ("<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n"
              f"<|im_start|>user\n<|vision_start|>{placeholder}<|vision_end|>"
              f"{question}<|im_end|>\n"
              "<|im_start|>assistant\n")
    stop_token_ids = None
    return llm, prompt, stop_token_ids


585
model_example_map = {
586
587
588
    "aria": run_aria,
    "blip-2": run_blip2,
    "chameleon": run_chameleon,
589
    "deepseek_vl_v2": run_deepseek_vl2,
590
    "florence2": run_florence2,
591
592
593
594
595
    "fuyu": run_fuyu,
    "glm4v": run_glm4v,
    "h2ovl_chat": run_h2ovl,
    "idefics3": run_idefics3,
    "internvl_chat": run_internvl,
596
597
    "llava": run_llava,
    "llava-next": run_llava_next,
598
    "llava-next-video": run_llava_next_video,
599
    "llava-onevision": run_llava_onevision,
600
    "mantis": run_mantis,
601
    "minicpmo": run_minicpmo,
602
    "minicpmv": run_minicpmv,
603
604
    "mllama": run_mllama,
    "molmo": run_molmo,
605
    "NVLM_D": run_nvlm_d,
606
607
608
609
    "paligemma": run_paligemma,
    "paligemma2": run_paligemma2,
    "phi3_v": run_phi3v,
    "pixtral_hf": run_pixtral_hf,
610
    "qwen_vl": run_qwen_vl,
611
    "qwen2_vl": run_qwen2_vl,
Roger Wang's avatar
Roger Wang committed
612
    "qwen2_5_vl": run_qwen2_5_vl,
613
614
615
}


616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
def get_multi_modal_input(args):
    """
    return {
        "data": image or video,
        "question": question,
    }
    """
    if args.modality == "image":
        # Input image and question
        image = ImageAsset("cherry_blossom") \
            .pil_image.convert("RGB")
        img_question = "What is the content of this image?"

        return {
            "data": image,
            "question": img_question,
        }

    if args.modality == "video":
        # Input video and question
        video = VideoAsset(name="sample_demo_1.mp4",
                           num_frames=args.num_frames).np_ndarrays
        vid_question = "Why is this video funny?"

        return {
            "data": video,
            "question": vid_question,
        }

    msg = f"Modality {args.modality} is not supported."
    raise ValueError(msg)


649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
def apply_image_repeat(image_repeat_prob, num_prompts, data, prompt, modality):
    """Repeats images with provided probability of "image_repeat_prob". 
    Used to simulate hit/miss for the MM preprocessor cache.
    """
    assert (image_repeat_prob <= 1.0 and image_repeat_prob >= 0)
    no_yes = [0, 1]
    probs = [1.0 - image_repeat_prob, image_repeat_prob]

    inputs = []
    cur_image = data
    for i in range(num_prompts):
        if image_repeat_prob is not None:
            res = random.choices(no_yes, probs)[0]
            if res == 0:
                # No repeat => Modify one pixel
                cur_image = cur_image.copy()
                new_val = (i // 256 // 256, i // 256, i % 256)
                cur_image.putpixel((0, 0), new_val)

        inputs.append({
            "prompt": prompt,
            "multi_modal_data": {
                modality: cur_image
            }
        })

    return inputs


678
679
680
681
682
def main(args):
    model = args.model_type
    if model not in model_example_map:
        raise ValueError(f"Model type {model} is not supported.")

683
684
685
686
687
    modality = args.modality
    mm_input = get_multi_modal_input(args)
    data = mm_input["data"]
    question = mm_input["question"]

688
    llm, prompt, stop_token_ids = model_example_map[model](question, modality)
689
690
691

    # We set temperature to 0.2 so that outputs can be different
    # even when all prompts are identical when running batch inference.
692
693
694
    sampling_params = SamplingParams(temperature=0.2,
                                     max_tokens=64,
                                     stop_token_ids=stop_token_ids)
695
696
697
698
699
700
701

    assert args.num_prompts > 0
    if args.num_prompts == 1:
        # Single inference
        inputs = {
            "prompt": prompt,
            "multi_modal_data": {
702
                modality: data
703
704
705
706
707
            },
        }

    else:
        # Batch inference
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
        if args.image_repeat_prob is not None:
            # Repeat images with specified probability of "image_repeat_prob"
            inputs = apply_image_repeat(args.image_repeat_prob,
                                        args.num_prompts, data, prompt,
                                        modality)
        else:
            # Use the same image for all prompts
            inputs = [{
                "prompt": prompt,
                "multi_modal_data": {
                    modality: data
                },
            } for _ in range(args.num_prompts)]

    if args.time_generate:
        import time
        start_time = time.time()
        outputs = llm.generate(inputs, sampling_params=sampling_params)
        elapsed_time = time.time() - start_time
        print("-- generate time = {}".format(elapsed_time))
728

729
730
    else:
        outputs = llm.generate(inputs, sampling_params=sampling_params)
731
732
733
734
735
736
737
738
739

    for o in outputs:
        generated_text = o.outputs[0].text
        print(generated_text)


if __name__ == "__main__":
    parser = FlexibleArgumentParser(
        description='Demo on using vLLM for offline inference with '
Cyrus Leung's avatar
Cyrus Leung committed
740
        'vision language models for text generation')
741
742
743
744
745
746
747
748
    parser.add_argument('--model-type',
                        '-m',
                        type=str,
                        default="llava",
                        choices=model_example_map.keys(),
                        help='Huggingface "model_type".')
    parser.add_argument('--num-prompts',
                        type=int,
749
                        default=4,
750
                        help='Number of prompts to run.')
751
752
753
    parser.add_argument('--modality',
                        type=str,
                        default="image",
754
                        choices=['image', 'video'],
755
756
757
758
759
                        help='Modality of the input.')
    parser.add_argument('--num-frames',
                        type=int,
                        default=16,
                        help='Number of frames to extract from the video.')
760
761
762
763
764
765
766
767
768

    parser.add_argument(
        '--image-repeat-prob',
        type=float,
        default=None,
        help='Simulates the hit-ratio for multi-modal preprocessor cache'
        ' (if enabled)')

    parser.add_argument(
769
        '--disable-mm-preprocessor-cache',
770
        action='store_true',
771
        help='If True, disables caching of multi-modal preprocessor/mapper.')
772
773
774
775
776
777

    parser.add_argument(
        '--time-generate',
        action='store_true',
        help='If True, then print the total generate() call time')

778
    args = parser.parse_args()
779
    main(args)