Skip to content

fix(exporters/tasks): support local timm model paths in ONNX export#2424

Open
Anai-Guo wants to merge 1 commit intohuggingface:mainfrom
Anai-Guo:fix/timm-local-model-loading
Open

fix(exporters/tasks): support local timm model paths in ONNX export#2424
Anai-Guo wants to merge 1 commit intohuggingface:mainfrom
Anai-Guo:fix/timm-local-model-loading

Conversation

@Anai-Guo
Copy link
Copy Markdown

Problem

When exporting locally fine-tuned timm models to ONNX, the export fails with:

Repository Not Found for url: https://huggingface.co/my-local-model/resolve/main/config.json

Root cause: get_model_from_task unconditionally prepends hf_hub: to the model path when loading timm models (line 1177 in optimum/exporters/tasks.py):

# Before fix — always uses HF Hub even for local paths
model = model_class(f"hf_hub:{model_name_or_path}", pretrained=True, exportable=True)

This forces timm's create_model to treat the path as an HF Hub model ID, breaking local directory paths.

Fix

Check if model_name_or_path is a local directory. If so, pass it directly to create_model; otherwise use the hf_hub: prefix for Hub models.

# After fix
if os.path.isdir(model_name_or_path):
    model = model_class(model_name_or_path, pretrained=True, exportable=True)
else:
    model = model_class(f"hf_hub:{model_name_or_path}", pretrained=True, exportable=True)

Testing

# Local timm model (should now work)
optimum-cli export onnx --model ./my-local-timm-model --task image-classification onnx_output/

# HF Hub timm model (should still work)
optimum-cli export onnx --model timm/efficientnet_b0.ra_in1k --task image-classification onnx_output/

Fixes #2423

Previously, `get_model_from_task` always prepended `hf_hub:` when loading
timm models, forcing them to be fetched from HF Hub even when a local
directory was provided. This caused a `Repository Not Found` error for
locally fine-tuned timm models.

Fix: check if `model_name_or_path` is an existing local directory.
If so, pass it directly to `create_model`; otherwise use the `hf_hub:`
prefix for Hub models.

Fixes huggingface#2423
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.

Can't convert local timm models

1 participant