QwenLM / Qwen2-VL

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

多卡推理出现tensor 不在同一张卡的异常提示 #234

Open carcloudfly opened 1 month ago

carcloudfly commented 1 month ago

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:3 and cuda:0! (when checking argument for argument mask in method wrapper_CUDA__maskedscatter)

请问是什么原因,如何修正?

ShuaiBai623 commented 1 month ago

可以分享下出现这个问题的代码以及运行的环境吗

carcloudfly commented 1 month ago

CUDA_AVIBLE_DEVICES=0,1,2,3 python web_demo_mm.py -c ../Qwen2-VL-2B-Instruct/ --server-port 8350 --server-name 0.0.0.0 谢谢,环境是这样的。如果只指定 gpu 0 是可以正常运行的。但是容易爆显存。 requirements_web_demo_locl.txt

carcloudfly commented 1 month ago

不用web demo,用本地脚本,也会出现同样的问题。

min_pixels = 2562828

max_pixels = 12802828

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

thuBingo commented 1 week ago

你好 请问这个问题解决了嘛? 遇到了相同的问题

jiah-li commented 1 week ago

你好 请问这个问题解决了嘛? 遇到了相同的问题

jiah-li commented 1 week ago

我遇到了相同的问题,运行硬件环境为Tesla V100*4,运行指令CUDA_VISIBLE_DEVICES=0,1,2,3 python start.py。start.py来自readme中的faststart。 具体代码如下:

from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info
import torch

# default: Load the model on the available device(s)
model = Qwen2VLForConditionalGeneration.from_pretrained(
    "/Qwen2-VL-7B-Instruct", torch_dtype=torch.float16, 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",
#     torch_dtype=torch.bfloat16,
#     attn_implementation="flash_attention_2",
#     device_map="auto",
# )

# default processer
processor = AutoProcessor.from_pretrained("/Qwen2-VL-7B-Instruct")

# 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 range of 256-1280, to balance performance and cost.
# min_pixels = 256*28*28
# max_pixels = 1280*28*28
# processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": "/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)
YAwei666 commented 14 hours ago

解决了吗