Skip to content
Merged
Changes from 2 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
34 changes: 31 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,34 @@ 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):
if "mobilenetv5_300m_enc" in config.architecture:
raise ImportError(
f"You are trying to load a model that uses '{config.architecture}', the vision backbone for Gemma 3n. "
f"This architecture is not supported in your version of timm ({timm.__version__}). "
"Please upgrade to timm >= 1.0.16 with: `pip install -U timm`."
) from e
Comment thread
harshaljanjani marked this conversation as resolved.
Resolved
# A good general check for other unknown models too.
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 +166,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 +282,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