Skip to content

fix(models): Forward timm model kwargs to timm.create_model for OmDet-Turbo#44611

Merged
zucchini-nlp merged 4 commits intohuggingface:mainfrom
harshaljanjani:fix/omdet-turbo-param-forwarding
Mar 13, 2026
Merged

fix(models): Forward timm model kwargs to timm.create_model for OmDet-Turbo#44611
zucchini-nlp merged 4 commits intohuggingface:mainfrom
harshaljanjani:fix/omdet-turbo-param-forwarding

Conversation

@harshaljanjani
Copy link
Contributor

@harshaljanjani harshaljanjani commented Mar 11, 2026

What does this PR do?

The following issue was identified and fixed in this PR:

This PR (🚨 Delete duplicate code in backbone utils) structured config loading to use BackboneMixin.consolidate_backbone_kwargs_to_config. For the DETR-family; the current state works correctly because timm_default_kwargs only contains keys that map to TimmBackboneConfig.init. There might be others; but OmDet-Turbo is a model that passes kwargs meant for timm.create_model itself, and which are not TimmBackboneConfig params and were dropped.
→ From the prev PR's diff, before the refactor, the implementation forwarded these params via **kwargs to timm.create_model and it worked before, but after the refactor they were stored as attributes on PreTrainedConfig and never forwarded, and parameters like img_size are ignored.

Fixes #44610.

cc: @zucchini-nlp

CI Failures

Before the fix (feel free to cross-check; these errors are reproducible):

Image

After the fix (feel free to cross-check):

2

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you fix any necessary existing tests?

Note

Medium Risk
Changes timm backbone construction and argument forwarding; while scoped to an optional timm_model_kwargs dict, it can affect any model relying on TimmBackbone if those kwargs are set or collide with existing **kwargs.

Overview
Fixes OmDet-Turbo’s timm backbone initialization by storing timm-only parameters (e.g. img_size, always_partition) under backbone_config.timm_model_kwargs instead of top-level config fields.

Updates TimmBackbone to forward config.timm_model_kwargs into timm.create_model, and adds a backward-compatibility shim to migrate older hub configs that had img_size/always_partition as direct attributes.

Written by Cursor Bugbot for commit 414ee30. This will update automatically on new commits. Configure here.

@harshaljanjani harshaljanjani marked this pull request as ready for review March 11, 2026 20:19
@zucchini-nlp
Copy link
Member

run-slow: omdet_turbo

@github-actions
Copy link
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/omdet_turbo"]
quantizations: []

out_indices=None,
freeze_batch_norm_2d=False,
output_stride=None,
timm_model_kwargs=None,
Copy link
Member

Choose a reason for hiding this comment

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

not really what we want for now. The above fix you did with passing it as timm_kwargs should be enough and consolidate_backbone_kwargs_to_config will put it inside timm config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense; reverted :)

Comment on lines +163 to +172
if getattr(backbone_config, "model_type", None) == "timm_backbone" and not getattr(
backbone_config, "timm_model_kwargs", None
):
timm_extra = {}
for attr in ("img_size", "always_partition"):
if hasattr(backbone_config, attr):
timm_extra[attr] = getattr(backbone_config, attr)
if timm_extra:
backbone_config.timm_model_kwargs = timm_extra

Copy link
Member

@zucchini-nlp zucchini-nlp Mar 12, 2026

Choose a reason for hiding this comment

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

same here, the consolidate_backbone_kwargs_to_config utility should work ootb. If not, can you check why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image

Actually, the omdet-turbo-swin-tiny-hf/config.json has backbone_config:null and backbone_kwargs: {"img_size": 640, "always_partition": true, ...}.

consolidate_backbone_kwargs_to_config takes this path bec backbone_kwargs is non-empty, the timm_default_kwargs path is skipped entirely, so img_size/always_partition end up as direct attrs with no timm_model_kwargs. I've added a corresponding comment in the code as well on why the block is required (happy to know if there's a better direction). So consolidate_backbone_kwargs_to_config works for fresh configs OOTB, but omdet-turbo-swin-tiny-hf seems to require the BC check :)
→ I thought about whether to change consolidate_backbone_kwargs_to_config or write the BC block, but writing a check of TimmBackboneConfig params vs timm.create_model params might be fragile, like, DETR passes use_pretrained_backbone in timm_default_kwargs, which is neither a TimmBackboneConfig param nor a valid timm.create_model kwarg, so it would get incorrectly forwarded to timm.create_model and break.

Copy link
Member

Choose a reason for hiding this comment

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

ahhh right, TimmBackbone doesn't accept arbitrary kwargs like TimmWrapper does so it gets popped, even if we pass it when creating the config

Good catch!

Copy link
Member

Choose a reason for hiding this comment

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

Actually I am working on subsequent PR here (#44252). It is blocked currently by a few other required fixes, so until then let's patch OmdetTurbo model code

We can change:

self.vision_backbone = load_backbone(config)

to smth like:

backbone = AutoBackbone.from_config(config=config.backbone_config, **config.timm_kwargs)

And make sure that timm_kwargs isn't saved when serializing the config. We don't want it in hub checkpoints, it'll cause more headache

Copy link
Member

Choose a reason for hiding this comment

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

after unification we'll have more freedom to pass any timm kwargs when creating the model by config.model_args

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your time @zucchini-nlp; attempted to resolve it accordingly 🤗

@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.

@github-actions
Copy link
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN ba3c4441 workflow commit (merge commit)
PR 357a5cfb branch commit (from PR)
main 3027515c base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

out_indices = config.out_indices if getattr(config, "out_indices", None) is not None else (-1,)
pretrained = kwargs.pop("pretrained", False)
in_chans = kwargs.pop("in_chans", config.num_channels)
timm_model_kwargs = getattr(config, "timm_model_kwargs", {})
Copy link

Choose a reason for hiding this comment

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

Unpacking None as kwargs causes TypeError at init

Medium Severity

getattr(config, "timm_model_kwargs", {}) returns None (not {}) when the attribute exists on the config but is explicitly None. This happens when a saved config JSON contains "timm_model_kwargs": null or when a backbone_config dict passes it as None. The subsequent **timm_model_kwargs unpacking then raises TypeError. Using getattr(config, "timm_model_kwargs", None) or {} would guard against this.

Fix in Cursor Fix in Web

Copy link
Contributor Author

@harshaljanjani harshaljanjani Mar 12, 2026

Choose a reason for hiding this comment

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

There's no codepath tmk that produces None here?

@harshaljanjani
Copy link
Contributor Author

harshaljanjani commented Mar 12, 2026

Updated with the suggested fix; happy to make further changes if needed :)

Comment on lines +164 to +169
timm_kwargs = {}
if getattr(backbone_config, "model_type", None) == "timm_backbone":
for attr in ("img_size", "always_partition"):
if hasattr(backbone_config, attr):
timm_kwargs[attr] = getattr(backbone_config, attr)

Copy link
Member

Choose a reason for hiding this comment

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

let's add a comment on why this is needed pls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added :)

Copy link
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Great, will merge after adding the comment

@zucchini-nlp
Copy link
Member

run-slow: omdet_turbo

@github-actions
Copy link
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/omdet_turbo"]
quantizations: []

@github-actions
Copy link
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 04863413 workflow commit (merge commit)
PR 94ce6381 branch commit (from PR)
main 745341d8 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@github-actions
Copy link
Contributor

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

run-slow: omdet_turbo

@zucchini-nlp zucchini-nlp enabled auto-merge March 13, 2026 11:39
@zucchini-nlp zucchini-nlp added this pull request to the merge queue Mar 13, 2026
Merged via the queue into huggingface:main with commit 3dd82fa Mar 13, 2026
23 checks passed
@harshaljanjani harshaljanjani deleted the fix/omdet-turbo-param-forwarding branch March 13, 2026 11:57
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.

[BUG] OmDet-Turbo processor produces 640px inputs but the model expects 224px

3 participants