phi3v_example.py 1.77 KB
Newer Older
1
2
3
4
5
6
7
import os
import subprocess

from PIL import Image

from vllm import LLM, SamplingParams

8
9
10
# The assets are located at `s3://air-example-data-2/vllm_opensource_llava/`.
# You can use `.buildkite/download-images.sh` to download them

11
12
13

def run_phi3v():
    model_path = "microsoft/Phi-3-vision-128k-instruct"
14

15
16
17
18
    # Note: The default setting of max_num_seqs (256) and
    # max_model_len (128k) for this model may cause OOM.
    # In this example, we override max_num_seqs to 5 while
    # keeping the original context length of 128k.
19
20
21
22
23
    llm = LLM(
        model=model_path,
        trust_remote_code=True,
        image_token_id=32044,
        image_input_shape="1,3,1008,1344",
24
25
        # Use the maximum possible value for memory profiling
        image_feature_size=2653,
26
        max_num_seqs=5,
27
28
29
30
31
32
33
34
    )

    image = Image.open("images/cherry_blossom.jpg")

    # single-image prompt
    prompt = "<|user|>\n<|image_1|>\nWhat is the season?<|end|>\n<|assistant|>\n"  # noqa: E501
    sampling_params = SamplingParams(temperature=0, max_tokens=64)

35
36
37
    outputs = llm.generate(
        {
            "prompt": prompt,
38
39
40
            "multi_modal_data": {
                "image": image
            },
41
42
        },
        sampling_params=sampling_params)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    for o in outputs:
        generated_text = o.outputs[0].text
        print(generated_text)


if __name__ == "__main__":
    s3_bucket_path = "s3://air-example-data-2/vllm_opensource_llava/"
    local_directory = "images"

    # Make sure the local directory exists or create it
    os.makedirs(local_directory, exist_ok=True)

    # Use AWS CLI to sync the directory, assume anonymous access
    subprocess.check_call([
        "aws",
        "s3",
        "sync",
        s3_bucket_path,
        local_directory,
        "--no-sign-request",
    ])
    run_phi3v()