在 4 张 RTX 4090D 显卡的机器上,运行 README 中的 demo 代码,出现错误
代码做了以下修改:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def load_image(image_file, input_size=448, max_num=12):
image = Image.open(image_file).convert('RGB')
transform = build_transform(input_size=input_size)
images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
pixel_values = [transform(image) for image in images]
pixel_values = torch.stack(pixel_values).to(torch.bfloat16) # Ensure tensor is in bfloat16
pixel_values = pixel_values.to(device)
return pixel_values
错误为:
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat2 in method wrapper_CUDA_bmm)
上述的 pixel_values 改成:
pixel_values = pixel_values.cuda()
pixel_values = load_image('./pic.png', max_num=12)
generation_config = dict(max_new_tokens=1024, do_sample=False)
print(f'Model device: {next(model.parameters()).device}')
print(f'Pixel values device: {pixel_values.device}')
// Model device: cuda:0
// Pixel values device: cuda:0
response = model.chat(tokenizer, pixel_values, question, generation_config)
print(f'User: {question}\nAssistant: {response}')
错误也是一样
使用的是 Model Card 中的代码
问题原因找到了。是因为我装的一些库的版本是最新的,导致的错误。
如果将以下库版本降到与
https://github.com/OpenGVLab/InternVL/blob/main/requirements/internvl_chat.txt
版本一致,就没问题了。
transformers==4.37.2 tokenizers==0.15.1 sentencepiece==0.1.99 bitsandbytes==0.41.0