Skip to content

AutoModel.from_pretrained() doesn't work for models with '.' in their name when there's a relative import #40496

@zachmoshe

Description

@zachmoshe

System Info

For models with:

  • A dot in their name (e.g. "saved_model_v1.0")
  • AND custom code with another module other than config.py and model.py

AutoModel.from_pretrained(..) fails with: ModuleNotFoundError: No module named 'transformers_modules.saved_model_v1'

This happens when trying to relatively import "another_module" from one of the other modules (i.e. from .another_module import ...)

Who can help?

@sgugger @XuehaiPan

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

See this example: https://github.com/zachmoshe/transformers-dotted-models-example.git

uv run save.py will save a model with custom code that has another module to a folder with ".".
uv run load.py will fail loading the saved model.
If we remove the relative import from model.py it works. If we remove the dot from the model name it also works.

Expected behavior

AutoModel.from_pretrained("MyModel-v1.0") should work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions