Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Handle and warn on incompatible kwargs during model creation to enhance the robustness and flexibility of create_model #2516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from

Conversation

developer0hye
Copy link
Contributor

Motivation

When using timm as a backbone library, users often switch between different model architectures, such as CNNs and Vision Transformers. These models frequently have different __init__ signatures. For instance, a ViT might accept an img_size argument, while many CNN models do not.

Currently, passing an unsupported keyword argument to timm.create_model results in a TypeError, causing the program to crash. This forces users to write boilerplate conditional logic to manage keyword arguments for each model type, which undermines the simple, abstract interface that create_model is designed to provide.

This PR aims to enhance the robustness and flexibility of create_model by allowing it to handle varied model arguments gracefully, thus improving the user experience.

Changes

  1. Improved _ignore_kwargs Utility: The _ignore_kwargs utility function in timm.models._builder has been updated. It now identifies and logs a warning message specifying which keyword arguments are being filtered out because they are not accepted by the target function (model_cls.__init__).

  2. Proactive Kwarg Filtering: The build_model_with_cfg function was modified to call _ignore_kwargs immediately before the model class is instantiated. This preemptively removes any extraneous arguments from the kwargs dictionary that would otherwise cause a TypeError.

Outcome

With these changes, create_model now behaves as follows instead of crashing with a TypeError:

  • It successfully creates the model by silently ignoring unsupported keyword arguments.
  • It logs a clear warning message that informs the user which arguments were ignored.

This improves compatibility across different model types and strengthens the abstraction of the create_model factory. As a result, users can more easily experiment with various architectures without writing cumbersome boilerplate code.

Test Code

import timm

vit_model = timm.create_model('vit_base_patch16_224', pretrained=False, img_size=(256, 256))
conv_model = timm.create_model('convnext_base_in22ft1k', pretrained=False, img_size=(256, 256), good="key")

log

Ignored attempt to pass arguments (img_size, good) to function <function ConvNeXt.__init__ at 0x000001A734F26FC0>.

This new utility function filters keyword arguments to only those accepted by the specified function, logging any ignored keys. It is integrated into the model building process to ensure compatibility with model initialization.
@developer0hye developer0hye changed the title Handle and warn on incompatible kwargs during model creation Handle and warn on incompatible kwargs during model creation to enhance the robustness and flexibility of create_model Jun 13, 2025
@rwightman
Copy link
Collaborator

interesting idea but it doesn't really work as there's kwarg handling through that build process that wouldn't be picked up by using inspect on the model class.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@developer0hye
Copy link
Contributor Author

developer0hye commented Jun 14, 2025

@rwightman Thanks for your review!
Would it make sense to apply it to a specific argument, like img_size?

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.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.