NotImplementedError: Cannot copy out of meta tensor

#3
by obushy - opened

NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty() instead of torch.nn.Module.to() when moving module from meta to a different device.

OpenGVLab org

Could you provide a snippet to reproduce this?

Alright, here's the code I ran and the error message I got.

code

import os
from transformers import AutoProcessor, AutoModelForImageTextToText
import torch

torch_device = "cuda"
model_checkpoint = "../../models/OpenGVLab/InternVL3-8B-hf"
processor = AutoProcessor.from_pretrained(model_checkpoint)
model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)

messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "http://images.cocodataset.org/val2017/000000039769.jpg"},
{"type": "text", "text": "Please describe the image explicitly."},
],
}
]

inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)

generate_ids = model.generate(**inputs, max_new_tokens=50)
decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)

print(decoded_output)

error

Initially, my transformers library version was 4.51.3. When I ran the code, I encountered the following error:

ValueError: The checkpoint you are trying to load has model type internvl but Transformers does not recognize this architecture. This could be because of an issue with the checkpoint, or because your version of Transformers is out of date.

You can update Transformers with the command pip install --upgrade transformers. If this does not work, and the checkpoint is very new, then there may not be a release version that supports this model yet. In this case, you can get the most up-to-date code by installing Transformers from source with the command pip install git+https://github.com/huggingface/transformers.git

After running pip install git+https://github.com/huggingface/transformers.git, it installed transformers-4.52.0.dev0. However, when I ran the code again, I encountered the following error:

Loading checkpoint shards: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 4/4 [00:30<00:00, 7.55s/it]
Traceback (most recent call last):
File "/data/qa_text/VQA/Code/v7-intern3/test.py", line 9, in
model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/transformers/models/auto/auto_factory.py", line 571, in from_pretrained
return model_class.from_pretrained(
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/transformers/modeling_utils.py", line 280, in _wrapper
return func(*args, **kwargs)
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/transformers/modeling_utils.py", line 4592, in from_pretrained
dispatch_model(model, **device_map_kwargs)
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/accelerate/big_modeling.py", line 501, in dispatch_model
model.to(device)
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/transformers/modeling_utils.py", line 3767, in to
return super().to(*args, **kwargs)
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1174, in to
return self._apply(convert)
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/torch/nn/modules/module.py", line 780, in _apply
module._apply(fn)
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/torch/nn/modules/module.py", line 780, in _apply
module._apply(fn)
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/torch/nn/modules/module.py", line 780, in _apply
module._apply(fn)
[Previous line repeated 3 more times]
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/torch/nn/modules/module.py", line 805, in _apply
param_applied = fn(param)
File "/data/qa_text/anaconda3/envs/physics/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1167, in convert
raise NotImplementedError(
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty() instead of torch.nn.Module.to() when moving module from meta to a different device.

Your need to confirm your account before you can post a new comment.

Sign up or log in to comment