AutoModel.from_pretrained fails to load model from a local folder - FileNotFoundError: [Errno 2] No such file or directory image_processing_minicpmv.py

#58
by sysk8sworker - opened

When a model is first downloaded from huggingface to a local folder and then used for simple inference it fails on model loading (AutoModel.from_pretrained)

Script for simple inference:

import requests
from PIL import Image
from io import BytesIO

#Download image from URL
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg"
response = requests.get(url)
image = Image.open(BytesIO(response.content))

#Now use with minicpm-o-2_6
from transformers import AutoModel, AutoTokenizer

#model_location="openbmb/MiniCPM-o-2_6"
model_location="/nfs/minicpm-o-2_6/pytorch/NATIVE"
model = AutoModel.from_pretrained(model_location, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_location, trust_remote_code=True)
msgs = [

{"role": "user", "content": "Describe this image"}
]
response = model.chat(image=image, msgs=msgs, tokenizer=tokenizer)
print(response)

The script failed on line
model = AutoModel.from_pretrained(model_location, trust_remote_code=True)
with this error:
Traceback (most recent call last):
File "", line 1, in
File "/home/user1/venv/lib/python3.10/site-packages/transformers/models/auto/auto_factory.py", line 558, in from_pretrained
model_class = get_class_from_dynamic_module(
File "/home/user1/venv/lib/python3.10/site-packages/transformers/dynamic_module_utils.py", line 570, in get_class_from_dynamic_module
return get_class_in_module(class_name, final_module, force_reload=force_download)
File "/home/user1/venv/lib/python3.10/site-packages/transformers/dynamic_module_utils.py", line 255, in get_class_in_module
module_files: list[Path] = [module_file] + sorted(map(Path, get_relative_import_files(module_file)))
File "/home/user1/venv/lib/python3.10/site-packages/transformers/dynamic_module_utils.py", line 127, in get_relative_import_files
new_imports.extend(get_relative_imports(f))
File "/home/user1/venv/lib/python3.10/site-packages/transformers/dynamic_module_utils.py", line 96, in get_relative_imports
with open(module_file, encoding="utf-8") as f:
FileNotFoundError: [Errno 2] No such file or directory: '/home/user1/.cache/huggingface/modules/transformers_modules/NATIVE/image_processing_minicpmv.py'

The folder /home/user1/.cache/huggingface/modules/transformers_modules/NATIVE contains the following files when this error happened:
configuration_minicpm.py
init.py
modeling_minicpmo.py
modeling_navit_siglip.py
processing_minicpmo.py
pychache
resampler.py
tokenization_minicpmo_fast.py
utils.py

This issue is not visible when using model from huggingface location in the above script:
model_location="openbmb/MiniCPM-o-2_6"

The following packages versions were used:
torch==2.9.0
torchaudio==2.9.0
torchvision==0.24.0
transformers==4.51.3
librosa==0.9.0
soundfile==0.12.1
vector-quantize-pytorch==1.18.5
vocos==0.1.0
decord==0.6.0
moviepy==2.2.1
Pillow==10.1.0

sysk8sworker changed discussion title from Issue in transformers with model loaded from a local folder - AutoModel.from_pretrained fails - FileNotFoundError: [Errno 2] No such file or directory image_processing_minicpmv.py to AutoModel.from_pretrained fails to load model from a local folder - FileNotFoundError: [Errno 2] No such file or directory image_processing_minicpmv.py

Sign up or log in to comment