Skip to content

Fix: Add version check for timm to support mobilenetv5 models (fixes #39208)#39264

Open
VIGNESH15103 wants to merge 9 commits intohuggingface:mainfrom
VIGNESH15103:fix-unknown-mobilenetv5
Open

Fix: Add version check for timm to support mobilenetv5 models (fixes #39208)#39264
VIGNESH15103 wants to merge 9 commits intohuggingface:mainfrom
VIGNESH15103:fix-unknown-mobilenetv5

Conversation

@VIGNESH15103
Copy link
Copy Markdown

What does this PR do?

Fixes issue #39208: Adds a version check for timm to ensure support for models like mobilenetv5_300m_enc, which require timm >= 0.9.10.

Motivation

The issue occurred because the mobilenetv5_300m_enc model name was not recognized in older versions of timm. This PR adds a conditional check to raise an informative ImportError if an unsupported version is detected.

Changes

  • Added a version check using packaging.version inside TimmWrapperImageProcessor.__init__.
  • Raises an error if a mobilenetv5 model is used with timm < 0.9.10.

Issue Link

Closes #39208

Reviewer Suggestion

@amyeroberts @qubvel (vision models)

Copy link
Copy Markdown
Contributor

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Hey @VIGNESH15103, thanks a lot for the fix, I would suggest we add a check for config loading, not for the image processor

Comment on lines +63 to +70
# mobilenetv5 models require timm >= 0.9.10
if architecture is not None and architecture.startswith("mobilenetv5"):
if version.parse(timm.__version__) < version.parse("0.9.10"):
raise ImportError(
f"`mobilenetv5` models require `timm >= 0.9.10`, but found {timm.__version__}. "
"Please upgrade `timm` to the latest version."
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The version for mibilenetv5 should be timm >= 1.0.16

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks @qubvel! Just to confirm — would it be appropriate to add the version check inside TimmWrapperModel.init and TimmWrapperForImageClassification.init right before timm.create_model(...) based on config.architecture? I’ll update accordingly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sound's good, but let's make it a bit more general, let's wrap timm.create_model to catch the RuntimeError and suggest to update timm with clear message

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

smt like

You are trying to instantiate `{architecture}`, but it's not supported in timm={timm.__version__}. Please, try updating to the latest timm version with `pip install -U timm`.

@VIGNESH15103 VIGNESH15103 force-pushed the fix-unknown-mobilenetv5 branch from 50ae8c5 to 22a2cee Compare July 8, 2025 21:02
@VIGNESH15103
Copy link
Copy Markdown
Author

Following the feedback:

  • I removed the version check from image_processing_timm_wrapper.py.
  • Instead, I now catch RuntimeError during timm.create_model(...) inside both TimmWrapperModel and TimmWrapperForImageClassification, as suggested.
  • Clear ImportError is raised when the architecture is unsupported, prompting users to upgrade timm.
  • Applied ruff formatting to resolve CI failures.

Kindly requesting a review and workflow approval when convenient. Thanks a lot!

Copy link
Copy Markdown
Contributor

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Thanks for iterating, a few more comments 🤗

Comment on lines +124 to +128
except RuntimeError as e:
raise ImportError(
f"You are trying to instantiate `{config.architecture}`, but it's not supported in timm={timm.__version__}. "
"Please try updating to the latest timm version with `pip install -U timm`."
) from e
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should catch an exact exception that the model is unsupported, not a general one

Comment on lines +63 to +68
# mobilenetv5 models require timm >= 0.9.10
if architecture is not None and architecture.startswith("mobilenetv5"):
if version.parse(timm.__version__) < version.parse("0.9.10"):
raise ImportError(
f"`mobilenetv5` models require `timm >= 0.9.10`, but found {timm.__version__}. "
"Please upgrade `timm` to the latest version."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we still need this in image processor?

Comment on lines +244 to +253
try:
self.timm_model = timm.create_model(
config.architecture, pretrained=False, num_classes=config.num_labels, **extra_init_kwargs
)
except RuntimeError as e:
raise ImportError(
f"You are trying to instantiate `{config.architecture}`, but it's not supported in timm=={timm.__version__}. "
"Please try updating to the latest timm version with `pip install -U timm`."
) from e

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe wrap this in a function

@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: timm_wrapper

@VIGNESH15103 VIGNESH15103 force-pushed the fix-unknown-mobilenetv5 branch from 6afd22c to 5bfcdab Compare July 13, 2025 01:17
@VIGNESH15103
Copy link
Copy Markdown
Author

Hi! I've addressed all your feedback:

  • Wrapped model creation in a helper method as suggested.
  • Added specific exception handling.
  • Removed the redundant version check in the image processor.
  • Ran ruff formatting and all CI checks pass now.

Could you please take another look and let me know if there's anything else to improve? If everything looks good, it would be awesome if you could approve and merge it 😊

Copy link
Copy Markdown
Contributor

@qubvel qubvel left a comment

Choose a reason for hiding this comment

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

Hey! I guess you are using some AI model to create PR, please review changes before submitting them 🤗

Comment on lines +62 to +63
# mobilenetv5 models require timm >= 0.9.10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

no needed

Comment on lines +135 to +142
try:
self.timm_model = safe_create_model(config.architecture, 0, **extra_init_kwargs)

except RuntimeError as e:
raise ImportError(
f"You are trying to instantiate `{config.architecture}`, but it's not supported in timm={timm.__version__}. "
"Please try updating to the latest timm version with `pip install -U timm`."
) from e
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we should just call for safe_create_model with named args, no need for try catch anymore

Comment on lines +59 to +71
def safe_create_model(architecture, num_classes, **kwargs):
try:
return timm.create_model(
architecture,
pretrained=False,
num_classes=num_classes,
**kwargs,
)
except RuntimeError as e:
raise ImportError(
f"You are trying to instantiate `{architecture}`, but it's not supported in timm=={timm.__version__}. "
"Please try updating to the latest timm version with `pip install -U timm`."
) from e
Copy link
Copy Markdown
Contributor

@qubvel qubvel Jul 14, 2025

Choose a reason for hiding this comment

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

we should check for a specific message in the exception, not just catch RuntimeError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unknown Model (mobilenetv5_300m_enc) when loading Gemma 3n

2 participants