# 设置模型可见 GPU 范围. 注意使用 acceleate 后,默认模型会使用所有 GPU,但是 model.generate 时会报: # The `language_model` is not in the `hf_device_map` dictionary and you are running your script in a multi-GPU environment. this may lead to unexpected behavior when using `accelerate`. Please pass a `device_map` that contains `language_model` to remove this warning. Please refer to https://github.com/huggingface/blog/blob/main/accelerate-large-models.md for more details on creating a `device_map` for large models.
# RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0! # 可以在导入其他包前,现在模型的 GPU 可见范围来解决 import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# 导入依赖包 import torch import requests from PIL import Image from transformers import Blip2Processor, Blip2ForConditionalGeneration