QwenLM / Qwen2-VL

Qwen2-VL is the multimodal large language model series developed by Qwen team, Alibaba Cloud.
Apache License 2.0
1.88k stars 108 forks source link

How do I run the Qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4 model? #98

Open satorugojos opened 1 week ago

satorugojos commented 1 week ago
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor, AutoConfig
from qwen_vl_utils import process_vision_info
import torch

model_name = "Qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4"

config = AutoConfig.from_pretrained(model_name)
config.quantization_config["disable_exllama"] = True

# # default: Load the model on the available device(s)
# model = Qwen2VLForConditionalGeneration.from_pretrained(model_name,config=config)

# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
model = Qwen2VLForConditionalGeneration.from_pretrained(
    model_name,
    torch_dtype=torch.bfloat16,
    config=config,
    #attn_implementation="flash_attention_2",
    attn_implementation="eager",
)

    # default processer
# processor = AutoProcessor.from_pretrained(model_name)

# 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.
# min_pixels = 256*28*28
# max_pixels = 1280*28*28
min_pixels = 256*28*28
max_pixels = 480*28*28
processor = AutoProcessor.from_pretrained(model_name, min_pixels=min_pixels, max_pixels=max_pixels)

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": "./1.jpg",
            },
            {"type": "text", "text": "Describe this image."},
        ],
    }
]

# Preparation for inference
text = processor.apply_chat_template(
    messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
    text=[text],
    images=image_inputs,
    videos=video_inputs,
    padding=True,
    return_tensors="pt",
)

# Inference: Generation of the output
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
    out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)

I cloned the files from the address https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4 to my computer, and when I ran the code block above, it did not produce any output. Is this because the model is not loaded in the Transformers library? Where am I making a mistake?

ShuaiBai623 commented 1 week ago

Maybe model_name should be your local path.

ShuaiBai623 commented 1 week ago

from modelscope import snapshot_download model_dir = snapshot_download(model_name)

satorugojos commented 1 week ago

Maybe model_name should be your local path.

thanks for the reply, it loads the model successfully but does not produce any output.