-
Notifications
You must be signed in to change notification settings - Fork 4
feat(timm): enable timm image-classification models via library routing #790
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8db0fc6
feat(timm): enable timm image-classification models via library routing
e1701f3
Merge branch 'main' into reny/timm
vortex-captain 261b81a
fix(timm): route the public inspect path (resolver) for wrapped libra…
c946661
Merge branch 'main' into reny/timm
vortex-captain 7ff3660
docs(timm): fix resolve_optimum_library docstring + export it (review…
84fb696
refactor(timm): drop redundant `or ""` in wrapped-library lookup (rev…
93402f6
Merge branch 'main' into reny/timm
vortex-captain ce268b9
docs(timm): clarify supported[0] default + name the wrapped-library s…
c4d80ea
fix(timm): warn and list tasks when a wrapped library exposes >1 expo…
76796ba
Merge branch 'main' into reny/timm
vortex-captain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
| # -------------------------------------------------------------------------- | ||
| """Tests for timm library routing during OnnxConfig resolution. | ||
|
|
||
| timm checkpoints load through transformers' TimmWrapper (model_type= | ||
| "timm_wrapper"), but Optimum registers their OnnxConfig (TimmDefaultOnnxConfig) | ||
| only under library_name="timm". ``resolve_optimum_library`` reroutes the lookup | ||
| so ``resolve_io_specs`` / ``_get_onnx_config`` resolve it under the default | ||
| "transformers" library, with no --library flag. See loader/task.py and | ||
| export/io.py. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| # Trigger OnnxConfig registration with TasksManager | ||
| import winml.modelkit.models # noqa: F401 | ||
|
vortex-captain marked this conversation as resolved.
Dismissed
|
||
| from winml.modelkit.export import resolve_io_specs | ||
| from winml.modelkit.export.io import _get_onnx_config # internal: routing under test | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def timm_wrapper_config(): | ||
| """Minimal offline TimmWrapperConfig (no hub download).""" | ||
| from transformers import TimmWrapperConfig | ||
|
|
||
| return TimmWrapperConfig(num_labels=10) | ||
|
|
||
|
|
||
| class TestTimmLibraryRouting: | ||
| """timm_wrapper resolves to Optimum's TimmDefaultOnnxConfig via library routing.""" | ||
|
|
||
| def test_get_onnx_config_routes_to_timm_default(self, timm_wrapper_config) -> None: | ||
| """A default (transformers) lookup reroutes to Optimum's TimmDefaultOnnxConfig.""" | ||
| from optimum.exporters.onnx.model_configs import TimmDefaultOnnxConfig | ||
|
|
||
| onnx_config = _get_onnx_config("timm_wrapper", "image-classification", timm_wrapper_config) | ||
| assert isinstance(onnx_config, TimmDefaultOnnxConfig), ( | ||
| "timm_wrapper did not route to Optimum's timm OnnxConfig; " | ||
| "resolve_optimum_library routing may be inactive." | ||
| ) | ||
|
|
||
| def test_io_specs_pixel_values_to_logits(self, timm_wrapper_config) -> None: | ||
| """resolve_io_specs yields the timm image-classifier I/O without a --library flag.""" | ||
| specs = resolve_io_specs("timm_wrapper", "image-classification", timm_wrapper_config) | ||
| assert specs["input_names"] == ["pixel_values"] | ||
| assert "logits" in specs["output_names"] | ||
|
|
||
| def test_pixel_values_is_4d_nchw(self, timm_wrapper_config) -> None: | ||
| specs = resolve_io_specs("timm_wrapper", "image-classification", timm_wrapper_config) | ||
| shape = specs["input_shapes"][0] | ||
| assert len(shape) == 4, f"pixel_values should be 4D NCHW, got {shape}" | ||
| assert shape[1] == 3, f"expected 3 channels, got {shape[1]}" | ||
|
vortex-captain marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
| # -------------------------------------------------------------------------- | ||
| """timm (wrapped-library) resolution in the public `inspect` path. | ||
|
|
||
| `inspect_model` resolves task/exporter via `resolver.detect_task` + | ||
| `resolver.resolve_exporter` — a separate path from the CLI's `_inspect_model_v2`. | ||
| timm checkpoints load as `TimmWrapperConfig` (model_type="timm_wrapper", | ||
| architectures=None). Without wrapped-library handling, `detect_task` mislabels | ||
| the task (HF_TASK_DEFAULTS fallback) and `resolve_exporter` hardcodes | ||
| library_name="transformers" so the OnnxConfig lookup fails (UNSUPPORTED). | ||
|
|
||
| These cover the fix that routes both through the timm library, matching the CLI. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from winml.modelkit.inspect import SupportLevel, detect_task, resolve_exporter | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def timm_wrapper_config(): | ||
| """Minimal offline TimmWrapperConfig (no hub download).""" | ||
| from transformers import TimmWrapperConfig | ||
|
|
||
| return TimmWrapperConfig(num_labels=10) | ||
|
|
||
|
|
||
| class TestDetectTaskTimm: | ||
| def test_timm_detects_image_classification(self, timm_wrapper_config) -> None: | ||
| """timm_wrapper (no architectures) resolves to image-classification, not a fallback.""" | ||
| task, source = detect_task(timm_wrapper_config) | ||
| assert task == "image-classification", f"got task={task!r} source={source!r}" | ||
|
|
||
|
|
||
| class TestResolveExporterTimm: | ||
| def test_timm_resolves_optimum_onnx_config(self, timm_wrapper_config) -> None: | ||
| """resolve_exporter routes timm_wrapper to Optimum's timm OnnxConfig + real I/O.""" | ||
| info = resolve_exporter( | ||
| "timm_wrapper", "image-classification", hf_config=timm_wrapper_config | ||
| ) | ||
| assert info.onnx_config_class == "TimmDefaultOnnxConfig", info.onnx_config_class | ||
| assert info.support_level is not SupportLevel.UNSUPPORTED | ||
| names = [t.name for t in info.input_tensors] | ||
| assert "pixel_values" in names, names |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.