QwenLM / Qwen2-VL

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

关于 Qwen2-VL-7B-Instruct-GPTQ-Int4部署应用的问题 #139

Open gy850222 opened 1 month ago

gy850222 commented 1 month ago

我严格参照https://huggingface.co/Qwen/Qwen2-VL-2B-Instruct-GPTQ-Int4的内容进行部署,结果都是提示 Traceback (most recent call last): File "//abc.py", line 1, in from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor ImportError: cannot import name 'Qwen2VLForConditionalGeneration' from 'transformers' (/myvenv/lib/python3.10/site-packages/transformers/init.py)

我使用了ubuntu2404 python3.12 和 ubuntu2204 python3.10 都是cannot import name 'Qwen2VLForConditionalGeneration' from 'transformers'

我执行的命令如下: 1 apt update
2 apt install vim nano git sudo 3 apt install python3 python3-pip python3-venv 4 mkdir /myvenv 5 python3 -m venv /myvenv 6 source /myvenv/bin/activate 7 history 8 pip install git+https://github.com/huggingface/transformers 9 pip install qwen-vl-utils 10 nano abc.py 11 python abc.py 12 pip install torch torchvision 13 pip install 'accelerate>=0.26.0' 14 pip install optimum

abc.py内容如下: from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor from qwen_vl_utils import process_vision_info

default: Load the model on the available device(s)

model = Qwen2VLForConditionalGeneration.from_pretrained( "Qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4", torch_dtype="auto", device_map="auto" )

We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.

model = Qwen2VLForConditionalGeneration.from_pretrained(

"Qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4",

torch_dtype=torch.bfloat16,

attn_implementation="flash_attention_2",

device_map="auto",

)

default processer

processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4")

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 = 2562828

max_pixels = 12802828

processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct-GPTQ-Int4", min_pixels=min_pixels, max_pixels=max_pixels)

messages = [ { "role": "user", "content": [ { "type": "image", "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg", }, {"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", ) inputs = inputs.to("cuda")

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)

Leo-Wu98 commented 1 month ago

依赖冲突了,导致最后transformers版本不对 试试先安装optimum,再装transformers

fyabc commented 1 month ago

@gy850222 您好,由于optimum会指定transformers版本,因此会覆盖掉之前的安装结果,请将transformers的安装移到最后一步再重新尝试一下。