huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
132.99k stars 26.53k forks source link

Python suffix stripping in `transformers.dynamic_model_utils.get_class_in_module` strips additional characters #31061

Closed gmastrapas closed 4 months ago

gmastrapas commented 4 months ago

System Info

Who can help?

No response

Information

Tasks

Reproduction

Try loading a model from the hub that has custom code files ending in p or y

For example

import torch
from transformers import AutoModel

model = AutoModel.from_pretrained(
    'BAAI/EVA-CLIP-18B', 
    torch_dtype=torch.float16,
    trust_remote_code=True,
)

Expected behavior

The model should be loading fine. But what happens is the following error

ValueError: The model class you are passing has a `config_class` attribute that is not consistent with the config class you passed (model has <class 'transformers_modules.BAAI.EVA-CLIP-18B.ecb774f7cbba81ad031555b951e126e78f978186.configuration_evaclip.EvaCLIPConfig'> and you passed <class 'transformers_modules.BAAI.EVA-CLIP-18B.ecb774f7cbba81ad031555b951e126e78f978186.configuration_evacli.EvaCLIPConfig'>. Fix one of those so they match!

Notice how the filename configuration_evaclip is cropped to configuration_evacli

The buggy code is here https://github.com/huggingface/transformers/blob/0a064dc0fcba31092868f911772df087901d90fb/src/transformers/dynamic_module_utils.py#L201

rstrip removes all trailing characters that match these provided and not the .py suffix

LysandreJik commented 4 months ago

cc @Rocketknight1

LysandreJik commented 4 months ago

Also cc @leot13 who is impacted by this