Skip to content
Merged
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
28 changes: 25 additions & 3 deletions src/transformers/models/timm_wrapper/modeling_timm_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ class TimmWrapperModelOutput(ModelOutput):
attentions: Optional[tuple[torch.FloatTensor, ...]] = None


def _create_timm_model_with_error_handling(config: "TimmWrapperConfig", **model_kwargs):
"""
Creates a timm model and provides a clear error message if the model is not found,
suggesting a library update.
"""
try:
model = timm.create_model(
config.architecture,
pretrained=False,
**model_kwargs,
)
return model
except RuntimeError as e:
if "Unknown model" in str(e):
# A good general check for unknown models.
raise ImportError(
f"The model architecture '{config.architecture}' is not supported in your version of timm ({timm.__version__}). "
"Please upgrade timm to a more recent version with `pip install -U timm`."
) from e
raise e


@auto_docstring
class TimmWrapperPreTrainedModel(PreTrainedModel):
main_input_name = "pixel_values"
Expand Down Expand Up @@ -138,7 +160,7 @@ def __init__(self, config: TimmWrapperConfig):
super().__init__(config)
# using num_classes=0 to avoid creating classification head
extra_init_kwargs = config.model_args or {}
self.timm_model = timm.create_model(config.architecture, pretrained=False, num_classes=0, **extra_init_kwargs)
self.timm_model = _create_timm_model_with_error_handling(config, num_classes=0, **extra_init_kwargs)
self.post_init()

@auto_docstring
Expand Down Expand Up @@ -254,8 +276,8 @@ def __init__(self, config: TimmWrapperConfig):
)

extra_init_kwargs = config.model_args or {}
self.timm_model = timm.create_model(
config.architecture, pretrained=False, num_classes=config.num_labels, **extra_init_kwargs
self.timm_model = _create_timm_model_with_error_handling(
config, num_classes=config.num_labels, **extra_init_kwargs
)
self.num_labels = config.num_labels
self.post_init()
Expand Down