Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/transformers/models/auto/configuration_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1421,17 +1421,10 @@ def from_pretrained(cls, pretrained_model_name_or_path: str | os.PathLike[str],
"`pip install git+https://github.com/huggingface/transformers.git`"
)
return config_class.from_dict(config_dict, **unused_kwargs)
else:
# Fallback: use pattern matching on the string.
# We go from longer names to shorter names to catch roberta before bert (for instance)
for pattern in sorted(CONFIG_MAPPING.keys(), key=len, reverse=True):
if pattern in str(pretrained_model_name_or_path):
return CONFIG_MAPPING[pattern].from_dict(config_dict, **unused_kwargs)
Comment on lines -1424 to -1429

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking, right? We need to mention this explicitly in https://github.com/huggingface/transformers/releases/tag/v5.2.0, cc @ArthurZucker @LysandreJik

For example, this now fails:

from transformers import AutoModel

model = AutoModel.from_pretrained("prajjwal1/bert-tiny")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, for a super small minority of models but yeah. Let's put it in front sorry

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


raise ValueError(
f"Unrecognized model in {pretrained_model_name_or_path}. "
f"Should have a `model_type` key in its {CONFIG_NAME}, or contain one of the following strings "
f"in its name: {', '.join(CONFIG_MAPPING.keys())}"
f"Should have a `model_type` key in its {CONFIG_NAME}."
)

@staticmethod
Expand Down
15 changes: 9 additions & 6 deletions src/transformers/models/auto/tokenization_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,15 @@ def from_pretrained(
!= tokenizer_config_class.replace("Fast", "")
):
# new model, but we ignore it unless the model type is the same
try:
return TokenizersBackend.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
except Exception:
return tokenizer_class_from_name(tokenizer_config_class).from_pretrained(
pretrained_model_name_or_path, *inputs, **kwargs
)
if TokenizersBackend is not None:
try:
return TokenizersBackend.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
except Exception as e:
logger.debug(f"Failed to use TokenizersBackend: {e}")

return tokenizer_class_from_name(tokenizer_config_class).from_pretrained(
pretrained_model_name_or_path, *inputs, **kwargs
)

if "_commit_hash" in tokenizer_config:
kwargs["_commit_hash"] = tokenizer_config["_commit_hash"]
Expand Down
25 changes: 15 additions & 10 deletions tests/models/auto/test_configuration_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ def test_config_for_model_str(self):
config = AutoConfig.for_model("roberta")
self.assertIsInstance(config, RobertaConfig)

def test_pattern_matching_fallback(self):
with tempfile.TemporaryDirectory() as tmp_dir:
# This model name contains bert and roberta, but roberta ends up being picked.
folder = os.path.join(tmp_dir, "fake-roberta")
os.makedirs(folder, exist_ok=True)
with open(os.path.join(folder, "config.json"), "w") as f:
f.write(json.dumps({}))
config = AutoConfig.from_pretrained(folder)
self.assertEqual(type(config), RobertaConfig)

def test_new_config_registration(self):
try:
AutoConfig.register("custom", CustomConfig)
Expand Down Expand Up @@ -148,3 +138,18 @@ class NewModelConfigLocal(BertConfig):
finally:
if "new-model" in CONFIG_MAPPING._extra_content:
del CONFIG_MAPPING._extra_content["new-model"]

def test_config_missing_model_type(self):
with tempfile.TemporaryDirectory() as tmp_dir:
config_dict = {
"hidden_size": 768,
"num_attention_heads": 12,
"num_hidden_layers": 12,
}
config_path = os.path.join(tmp_dir, "config.json")

with open(config_path, "w") as f:
json.dump(config_dict, f)

with self.assertRaisesRegex(ValueError, "Should have a `model_type` key"):
AutoConfig.from_pretrained(tmp_dir)