Skip to content

Commit 95026c2

Browse files
committed
fix a few things
1 parent f45f29e commit 95026c2

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

_doc/examples/plot_export_tiny_phi2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
data["n_weights"],
5151
)
5252

53-
print(f"model {size / 2**20:1.3f} Mb with {n_weights // 1000} mille parameters.")
53+
print(f"model {size / 2**20:1.1f} Mb with {n_weights // 1000} thousands of parameters.")
5454
# %%
5555
# The original model has 2.7 billion parameters. It was divided by more than 10.
5656
# However, it can still be used with
@@ -209,6 +209,7 @@
209209
# Every mixture of models goes through a control flow (a test).
210210
# It also happens when a cache is truncated. The code of the model
211211
# needs to be changed. See example :ref:`l-plot-export-cond`.
212+
# Loops are not supported yet.
212213
#
213214
# Issue with dynamic shapes
214215
# +++++++++++++++++++++++++

onnx_diagnostic/tasks/image_classification.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
def reduce_model_config(config: Any) -> Dict[str, Any]:
99
"""Reduces a model size."""
10+
if (
11+
hasattr(config, "model_type")
12+
and config.model_type == "timm_wrapper"
13+
and not hasattr(config, "num_hidden_layers")
14+
):
15+
# We cannot reduce.
16+
return {}
1017
check_hasattr(config, ("num_hidden_layers", "hidden_sizes"))
1118
kwargs = dict(
1219
num_hidden_layers=(
@@ -82,6 +89,20 @@ def random_input_kwargs(config: Any) -> Tuple[Dict[str, Any], Callable]:
8289
If the configuration is None, the function selects typical dimensions.
8390
"""
8491
if config is not None:
92+
if (
93+
hasattr(config, "model_type")
94+
and config.model_type == "timm_wrapper"
95+
and not hasattr(config, "num_hidden_layers")
96+
):
97+
input_size = config.pretrained_cfg["input_size"]
98+
kwargs = dict(
99+
batch_size=2,
100+
input_width=input_size[-2],
101+
input_height=input_size[-1],
102+
input_channels=input_size[-3],
103+
)
104+
return kwargs, get_inputs
105+
85106
check_hasattr(config, ("image_size", "architectures"), "num_channels")
86107
if config is not None:
87108
if hasattr(config, "image_size"):

onnx_diagnostic/torch_models/hghub/hub_api.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ def get_model_info(model_id) -> Any:
8888
return model_info(model_id)
8989

9090

91+
def _guess_task_from_config(config: Any) -> Optional[str]:
92+
"""Tries to infer a task from the configuration."""
93+
if hasattr(config, "bbox_loss_coefficient") and hasattr(config, "giou_loss_coefficient"):
94+
return "object-detection"
95+
if hasattr(config, "architecture") and config.architecture:
96+
return task_from_arch(config.architecture)
97+
return None
98+
99+
91100
@functools.cache
92101
def task_from_arch(arch: str, default_value: Optional[str] = None) -> str:
93102
"""
@@ -126,7 +135,7 @@ def task_from_id(
126135
:param default_value: if specified, the function returns this value
127136
if the task cannot be determined
128137
:param pretrained: uses the config
129-
:param fall_back_to_pretrained: balls back to pretrained config
138+
:param fall_back_to_pretrained: falls back to pretrained config
130139
:return: task
131140
"""
132141
if not pretrained:
@@ -139,6 +148,9 @@ def task_from_id(
139148
try:
140149
return config.pipeline_tag
141150
except AttributeError:
151+
guess = _guess_task_from_config(config)
152+
if guess is not None:
153+
return guess
142154
assert config.architectures is not None and len(config.architectures) == 1, (
143155
f"Cannot return the task of {model_id!r}, pipeline_tag is not setup, "
144156
f"architectures={config.architectures} in config={config}"

onnx_diagnostic/torch_models/hghub/hub_data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
MobileBertModel,feature-extraction
7979
MobileNetV1Model,image-feature-extraction
8080
MobileNetV2Model,image-feature-extraction
81+
mobilenetv3_small_100,image-classification
8182
MobileViTForImageClassification,image-classification
8283
ModernBertForMaskedLM,fill-mask
8384
Phi4MMForCausalLM,MoE
@@ -202,7 +203,7 @@ def load_models_testing() -> List[str]:
202203
@functools.cache
203204
def load_architecture_task() -> Dict[str, str]:
204205
"""
205-
Returns a dictionary mapping architecture to task.
206+
Returns a dictionary mapping architectures to tasks.
206207
207208
import pprint
208209
from onnx_diagnostic.torch_models.hghub.hub_data import load_architecture_task

onnx_diagnostic/torch_models/hghub/model_inputs.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import transformers
55
from ...helpers.config_helper import update_config
66
from ...tasks import reduce_model_config, random_input_kwargs
7-
from .hub_api import task_from_arch, get_pretrained_config
7+
from .hub_api import task_from_arch, task_from_id, get_pretrained_config
88

99

1010
def get_untrained_model_with_inputs(
@@ -64,17 +64,21 @@ def get_untrained_model_with_inputs(
6464
config = get_pretrained_config(
6565
model_id, use_preinstalled=use_preinstalled, **(model_kwargs or {})
6666
)
67+
if hasattr(config, "architecture") and config.architecture:
68+
archs = [config.architecture]
6769
archs = config.architectures # type: ignore
68-
assert archs is not None and len(archs) == 1, (
70+
task = None
71+
if archs is None:
72+
task = task_from_id(model_id)
73+
assert task is not None or (archs is not None and len(archs) == 1), (
6974
f"Unable to determine the architecture for model {model_id!r}, "
7075
f"architectures={archs!r}, conf={config}"
7176
)
72-
arch = archs[0]
73-
if verbose:
74-
print(f"[get_untrained_model_with_inputs] architecture={arch!r}")
7577
if verbose:
78+
print(f"[get_untrained_model_with_inputs] architectures={archs!r}")
7679
print(f"[get_untrained_model_with_inputs] cls={config.__class__.__name__!r}")
77-
task = task_from_arch(arch)
80+
if task is None:
81+
task = task_from_arch(archs[0])
7882
if verbose:
7983
print(f"[get_untrained_model_with_inputs] task={task!r}")
8084

@@ -106,7 +110,15 @@ def get_untrained_model_with_inputs(
106110
if inputs_kwargs:
107111
kwargs.update(inputs_kwargs)
108112

109-
model = getattr(transformers, arch)(config)
113+
if archs is not None:
114+
model = getattr(transformers, archs[0])(config)
115+
else:
116+
assert same_as_pretrained, (
117+
f"Model {model_id!r} cannot be built, the model cannot be built. "
118+
f"It must be downloaded. Use same_as_pretrained=True."
119+
)
120+
model = None
121+
110122
# This line is important. Some models may produce different
111123
# outputs even with the same inputs in training mode.
112124
model.eval()

0 commit comments

Comments
 (0)