Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
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.
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 commentedJun 13, 2025
The docs for this PR livehere. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
developer0hye commentedJun 14, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@rwightman Thanks for your review! |
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 animg_size
argument, while many CNN models do not.Currently, passing an unsupported keyword argument to
timm.create_model
results in aTypeError
, 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 thatcreate_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
Improved
_ignore_kwargs
Utility: The_ignore_kwargs
utility function intimm.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__
).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 thekwargs
dictionary that would otherwise cause aTypeError
.Outcome
With these changes,
create_model
now behaves as follows instead of crashing with aTypeError
: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
log