Skip to content

Fix estimate-memory for timm>=1.0.29 by adding the hf-hub: prefix - #4213

Open
iamsharduld wants to merge 2 commits into
huggingface:mainfrom
iamsharduld:estimate-timm-hf-hub-prefix
Open

Fix estimate-memory for timm>=1.0.29 by adding the hf-hub: prefix#4213
iamsharduld wants to merge 2 commits into
huggingface:mainfrom
iamsharduld:estimate-timm-hf-hub-prefix

Conversation

@iamsharduld

Copy link
Copy Markdown

What does this PR do?

accelerate estimate-memory timm/resnet50.a1_in1k --library_name timm fails with timm 1.0.29:

ValueError: Model name 'timm/resnet50.a1_in1k' has no source prefix but looks like a Hugging Face Hub repo id or a local path. Use 'hf-hub:timm/resnet50.a1_in1k' to load from the Hub or 'local-dir:timm/resnet50.a1_in1k' to load from a local folder.

Since huggingface/pytorch-image-models#2727 (shipped in timm 1.0.29), timm needs a source prefix for anything that looks like a repo id. Older versions dropped the repo owner and looked the rest up in the registry, which only worked because timm names its Hub repos after the architecture. The two timm tests in tests/test_cli.py fail on main because of this, see https://github.com/huggingface/accelerate/actions/runs/33763859075/job/100676635381.

I now add the hf-hub: prefix before calling timm.create_model when the name looks like a repo id, and leave bare architecture names like resnet50 alone. With the prefix, timm reads config.json from the Hub and raises its own errors when the file is missing or belongs to another library. I map those to the existing "Tried to load ... with timm but ..." message, so the user sees the same error as with timm 1.0.28.

Note that timm names now resolve through the Hub config.json rather than the registry entry with the same name. That means user-owned timm repos work as well.

I tested with timm 1.0.28 and 1.0.29. I added unit tests for the prefix rule and the error mapping, and one end-to-end test for a repo whose config.json belongs to another library. No version pin is needed, timm has supported the hf-hub: prefix for a long time.

Fixes #4212

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? Nothing user-facing changed, the documented timm/resnet50.a1_in1k example works as before.
  • Did you write any new necessary tests?

Who can review?

@SunMarc (Command Line Interface)

timm 1.0.29 no longer accepts a Hub repo id without a source prefix,
so `accelerate estimate-memory timm/resnet50.a1_in1k --library_name
timm` fails with "Model name ... has no source prefix". Older timm
versions dropped the repo owner and looked the rest up in the registry,
which is why this used to work.

I now add the hf-hub: prefix before calling timm.create_model when the
name looks like a repo id. Bare architecture names like resnet50 are
left alone.

With the prefix, timm reads config.json from the Hub and raises its own
errors when the file is missing or belongs to another library. I map
those to the existing "Tried to load ... with timm but ..." message so
the user sees the same error as before.

Tested with timm 1.0.28 and 1.0.29. Added unit tests for the prefix
rule and the error mapping, plus one end-to-end test for a non-timm
repo.

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, just a couple of nits. Also, does this still works with older version of timm ?

Comment thread tests/test_cli.py Outdated
Comment on lines +578 to +585
@require_timm
def test_check_has_model_timm(self):
# Unknown architecture in the `timm` registry
assert check_has_model(RuntimeError("Unknown model (dummy)")) == "timm"
# Hub repo without a `timm` `config.json`, raised by `timm.create_model("hf-hub:...")`
assert check_has_model(EntryNotFoundError("Entry Not Found for url: .../config.json.")) == "timm"
# Hub repo whose `config.json` belongs to another library, raised by `timm.create_model("hf-hub:...")`
assert check_has_model(KeyError("architecture")) == "timm"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
@require_timm
def test_check_has_model_timm(self):
# Unknown architecture in the `timm` registry
assert check_has_model(RuntimeError("Unknown model (dummy)")) == "timm"
# Hub repo without a `timm` `config.json`, raised by `timm.create_model("hf-hub:...")`
assert check_has_model(EntryNotFoundError("Entry Not Found for url: .../config.json.")) == "timm"
# Hub repo whose `config.json` belongs to another library, raised by `timm.create_model("hf-hub:...")`
assert check_has_model(KeyError("architecture")) == "timm"

Comment thread src/accelerate/commands/estimate.py Outdated
Comment on lines +55 to +56
# `timm.create_model("hf-hub:...")` reads `config.json` from the Hub without wrapping the errors: a missing file
# raises `EntryNotFoundError` and a `config.json` from another library has no `architecture` key (`KeyError`)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
# `timm.create_model("hf-hub:...")` reads `config.json` from the Hub without wrapping the errors: a missing file
# raises `EntryNotFoundError` and a `config.json` from another library has no `architecture` key (`KeyError`)

Comment thread src/accelerate/commands/estimate.py Outdated
Comment on lines +75 to +81
Adds the `hf-hub:` source prefix that `timm.create_model` needs to load `model_name` from the Hub. Bare
architecture names from the `timm` registry (such as `resnet50`) are returned unchanged. So are names that already
carry a source prefix (such as `hf-hub:timm/resnet50.a1_in1k`), which only direct callers can pass: `verify_on_hub`
in `create_empty_model` rejects them before this function runs.

`timm>=1.0.29` refuses a Hub repo id without the prefix. Earlier versions dropped the repo owner and resolved the
rest of the name through the registry.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

keep it short

Drop the check_has_model unit test, remove the comment above the timm
branch in check_has_model, and shorten the add_timm_hub_prefix
docstring, as suggested in review.
@iamsharduld

Copy link
Copy Markdown
Author

Applied all three suggestions: dropped the unit test and the comment, and shortened the docstring.

On older timm versions: yes, it still works. The hf-hub: prefix has been accepted since timm 0.6 (the hf_hub: spelling even earlier), and I ran the estimator tests with timm 1.0.28 as well as 1.0.29 after this change.

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.

accelerate estimate-memory fails with timm 1.0.29: model name has no source prefix

2 participants