onnx-diagnostic 0.7.10__py3-none-any.whl → 0.7.12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -177,6 +177,51 @@ def task_from_arch(
177
177
  return data[arch]
178
178
 
179
179
 
180
+ def _trygetattr(config, attname):
181
+ try:
182
+ return getattr(config, attname)
183
+ except AttributeError:
184
+ return None
185
+
186
+
187
+ def architecture_from_config(config) -> Optional[str]:
188
+ """Guesses the architecture (class) of the model described by this config."""
189
+ if isinstance(config, dict):
190
+ if "_class_name" in config:
191
+ return config["_class_name"]
192
+ if "architecture" in config:
193
+ return config["architecture"]
194
+ if config.get("architectures", []):
195
+ return config["architectures"][0]
196
+ if hasattr(config, "_class_name"):
197
+ return config._class_name
198
+ if hasattr(config, "architecture"):
199
+ return config.architecture
200
+ if hasattr(config, "architectures") and config.architectures:
201
+ return config.architectures[0]
202
+ if hasattr(config, "__dict__"):
203
+ if "_class_name" in config.__dict__:
204
+ return config.__dict__["_class_name"]
205
+ if "architecture" in config.__dict__:
206
+ return config.__dict__["architecture"]
207
+ if config.__dict__.get("architectures", []):
208
+ return config.__dict__["architectures"][0]
209
+ return None
210
+
211
+
212
+ def find_package_source(config) -> Optional[str]:
213
+ """Guesses the package the class models from."""
214
+ if isinstance(config, dict):
215
+ if "_diffusers_version" in config:
216
+ return "diffusers"
217
+ if hasattr(config, "_diffusers_version"):
218
+ return "diffusers"
219
+ if hasattr(config, "__dict__"):
220
+ if "_diffusers_version" in config.__dict__:
221
+ return "diffusers"
222
+ return "transformers"
223
+
224
+
180
225
  def task_from_id(
181
226
  model_id: str,
182
227
  default_value: Optional[str] = None,
@@ -202,28 +247,30 @@ def task_from_id(
202
247
  if not fall_back_to_pretrained:
203
248
  raise
204
249
  config = get_pretrained_config(model_id, subfolder=subfolder)
205
- try:
206
- return config.pipeline_tag
207
- except AttributeError:
208
- guess = _guess_task_from_config(config)
209
- if guess is not None:
210
- return guess
211
- data = load_architecture_task()
212
- if model_id in data:
213
- return data[model_id]
214
- if type(config) is dict and "_class_name" in config:
215
- return task_from_arch(config["_class_name"], default_value=default_value)
216
- if not config.architectures or not config.architectures:
217
- # Some hardcoded values until a better solution is found.
218
- if model_id.startswith("google/bert_"):
219
- return "fill-mask"
220
- assert config.architectures is not None and len(config.architectures) == 1, (
221
- f"Cannot return the task of {model_id!r}, pipeline_tag is not setup, "
222
- f"architectures={config.architectures} in config={config}. "
223
- f"The task can be added in "
224
- f"``onnx_diagnostic.torch_models.hghub.hub_data.__data_arch__``."
225
- )
226
- return task_from_arch(config.architectures[0], default_value=default_value)
250
+ tag = _trygetattr(config, "pipeline_tag")
251
+ if tag is not None:
252
+ return tag
253
+
254
+ guess = _guess_task_from_config(config)
255
+ if guess is not None:
256
+ return guess
257
+ data = load_architecture_task()
258
+ if subfolder:
259
+ full_id = f"{model_id}//{subfolder}"
260
+ if full_id in data:
261
+ return data[full_id]
262
+ if model_id in data:
263
+ return data[model_id]
264
+ arch = architecture_from_config(config)
265
+ if arch is None:
266
+ if model_id.startswith("google/bert_"):
267
+ return "fill-mask"
268
+ assert arch is not None, (
269
+ f"Cannot return the task of {model_id!r}, pipeline_tag is not setup, "
270
+ f"config={config}. The task can be added in "
271
+ f"``onnx_diagnostic.torch_models.hghub.hub_data.__data_arch__``."
272
+ )
273
+ return task_from_arch(arch, default_value=default_value)
227
274
 
228
275
 
229
276
  def task_from_tags(tags: Union[str, List[str]]) -> str:
@@ -242,21 +289,17 @@ def task_from_tags(tags: Union[str, List[str]]) -> str:
242
289
 
243
290
  def enumerate_model_list(
244
291
  n: int = 50,
245
- task: Optional[str] = None,
246
- library: Optional[str] = None,
247
- tags: Optional[Union[str, List[str]]] = None,
292
+ pipeline_tag: Optional[str] = None,
248
293
  search: Optional[str] = None,
249
294
  dump: Optional[str] = None,
250
- filter: Optional[str] = None,
295
+ filter: Optional[Union[str, List[str]]] = None,
251
296
  verbose: int = 0,
252
297
  ):
253
298
  """
254
299
  Enumerates models coming from :epkg:`huggingface_hub`.
255
300
 
256
301
  :param n: number of models to retrieve (-1 for all)
257
- :param task: see :meth:`huggingface_hub.HfApi.list_models`
258
- :param tags: see :meth:`huggingface_hub.HfApi.list_models`
259
- :param library: see :meth:`huggingface_hub.HfApi.list_models`
302
+ :param pipeline_tag: see :meth:`huggingface_hub.HfApi.list_models`
260
303
  :param search: see :meth:`huggingface_hub.HfApi.list_models`
261
304
  :param filter: see :meth:`huggingface_hub.HfApi.list_models`
262
305
  :param dump: dumps the result in this csv file
@@ -264,9 +307,7 @@ def enumerate_model_list(
264
307
  """
265
308
  api = HfApi()
266
309
  models = api.list_models(
267
- task=task,
268
- library=library,
269
- tags=tags,
310
+ pipeline_tag=pipeline_tag,
270
311
  search=search,
271
312
  full=True,
272
313
  filter=filter,
@@ -30,6 +30,7 @@ __data_arch__ = textwrap.dedent(
30
30
  ConvBertModel,feature-extraction
31
31
  ConvNextForImageClassification,image-classification
32
32
  ConvNextV2Model,image-feature-extraction
33
+ CosmosTransformer3DModel,image-to-video
33
34
  CvtModel,feature-extraction
34
35
  DPTModel,image-feature-extraction
35
36
  Data2VecAudioModel,feature-extraction
@@ -156,7 +157,8 @@ __data_arch__ = textwrap.dedent(
156
157
  YolosForObjectDetection,object-detection
157
158
  YolosModel,image-feature-extraction
158
159
  Alibaba-NLP/gte-large-en-v1.5,sentence-similarity
159
- emilyalsentzer/Bio_ClinicalBERT,fill-mask"""
160
+ emilyalsentzer/Bio_ClinicalBERT,fill-mask
161
+ nvidia/Cosmos-Predict2-2B-Video2World//transformer,image-to-video"""
160
162
  )
161
163
 
162
164
  __data_tasks__ = [
@@ -2,13 +2,21 @@ import copy
2
2
  import inspect
3
3
  import os
4
4
  import pprint
5
+ import time
5
6
  from typing import Any, Dict, Optional, Tuple
6
7
  import torch
7
8
  import transformers
8
9
  from ...helpers.config_helper import update_config, build_diff_config
9
10
  from ...tasks import reduce_model_config, random_input_kwargs
10
- from .hub_api import task_from_arch, task_from_id, get_pretrained_config, download_code_modelid
11
- from .model_specific import HANDLED_MODELS, load_specific_model
11
+ from .hub_api import (
12
+ task_from_arch,
13
+ task_from_id,
14
+ get_pretrained_config,
15
+ download_code_modelid,
16
+ architecture_from_config,
17
+ find_package_source,
18
+ )
19
+ from .model_specific import HANDLED_MODELS, load_specific_model, instantiate_specific_model
12
20
 
13
21
 
14
22
  def _code_needing_rewriting(model: Any) -> Any:
@@ -96,27 +104,18 @@ def get_untrained_model_with_inputs(
96
104
  model, task, config = load_specific_model(model_id, verbose=verbose)
97
105
 
98
106
  if model is None:
99
- if hasattr(config, "architecture") and config.architecture:
100
- archs = [config.architecture]
101
- if type(config) is dict:
102
- assert (
103
- "_class_name" in config
104
- ), f"Unable to get the architecture from config={config}"
105
- archs = [config["_class_name"]]
106
- else:
107
- archs = config.architectures # type: ignore
108
- task = None
109
- if archs is None:
110
- task = task_from_id(model_id)
111
- assert task is not None or (archs is not None and len(archs) == 1), (
107
+ arch = architecture_from_config(config)
108
+ if arch is None:
109
+ task = task_from_id(model_id, subfolder=subfolder)
110
+ assert task is not None or arch is not None, (
112
111
  f"Unable to determine the architecture for model {model_id!r}, "
113
- f"architectures={archs!r}, conf={config}"
112
+ f"archs={arch!r}, conf={config}"
114
113
  )
115
114
  if verbose:
116
- print(f"[get_untrained_model_with_inputs] architectures={archs!r}")
115
+ print(f"[get_untrained_model_with_inputs] architecture={arch!r}")
117
116
  print(f"[get_untrained_model_with_inputs] cls={config.__class__.__name__!r}")
118
117
  if task is None:
119
- task = task_from_arch(archs[0], model_id=model_id, subfolder=subfolder)
118
+ task = task_from_arch(arch, model_id=model_id, subfolder=subfolder)
120
119
  if verbose:
121
120
  print(f"[get_untrained_model_with_inputs] task={task!r}")
122
121
 
@@ -170,36 +169,58 @@ def get_untrained_model_with_inputs(
170
169
  f"{getattr(config, '_attn_implementation', '?')!r}" # type: ignore[union-attr]
171
170
  )
172
171
 
173
- if type(config) is dict and "_diffusers_version" in config:
172
+ if find_package_source(config) == "diffusers":
174
173
  import diffusers
175
174
 
176
175
  package_source = diffusers
177
176
  else:
178
177
  package_source = transformers
179
178
 
179
+ if verbose:
180
+ print(
181
+ f"[get_untrained_model_with_inputs] package_source={package_source.__name__} é"
182
+ f"from {package_source.__file__}"
183
+ )
180
184
  if use_pretrained:
185
+ begin = time.perf_counter()
186
+ if verbose:
187
+ print(
188
+ f"[get_untrained_model_with_inputs] pretrained model_id {model_id!r}, "
189
+ f"subfolder={subfolder!r}"
190
+ )
181
191
  model = transformers.AutoModel.from_pretrained(
182
- model_id, trust_remote_code=True, **mkwargs
192
+ model_id, subfolder=subfolder or "", trust_remote_code=True, **mkwargs
183
193
  )
194
+ if verbose:
195
+ print(
196
+ f"[get_untrained_model_with_inputs] -- done in "
197
+ f"{time.perf_counter() - begin}s"
198
+ )
184
199
  else:
185
- if archs is not None:
200
+ begin = time.perf_counter()
201
+ if verbose:
202
+ print(
203
+ f"[get_untrained_model_with_inputs] instantiate model_id {model_id!r}, "
204
+ f"subfolder={subfolder!r}"
205
+ )
206
+ if arch is not None:
186
207
  try:
187
- cls_model = getattr(package_source, archs[0])
208
+ cls_model = getattr(package_source, arch)
188
209
  except AttributeError as e:
189
210
  # The code of the models is not in transformers but in the
190
211
  # repository of the model. We need to download it.
191
212
  pyfiles = download_code_modelid(model_id, verbose=verbose)
192
213
  if pyfiles:
193
- if "." in archs[0]:
194
- cls_name = archs[0]
214
+ if "." in arch:
215
+ cls_name = arch
195
216
  else:
196
217
  modeling = [_ for _ in pyfiles if "/modeling_" in _]
197
218
  assert len(modeling) == 1, (
198
219
  f"Unable to guess the main file implemented class "
199
- f"{archs[0]!r} from {pyfiles}, found={modeling}."
220
+ f"{arch!r} from {pyfiles}, found={modeling}."
200
221
  )
201
222
  last_name = os.path.splitext(os.path.split(modeling[0])[-1])[0]
202
- cls_name = f"{last_name}.{archs[0]}"
223
+ cls_name = f"{last_name}.{arch}"
203
224
  if verbose:
204
225
  print(
205
226
  f"[get_untrained_model_with_inputs] "
@@ -217,7 +238,7 @@ def get_untrained_model_with_inputs(
217
238
  )
218
239
  else:
219
240
  raise AttributeError(
220
- f"Unable to find class 'tranformers.{archs[0]}'. "
241
+ f"Unable to find class 'tranformers.{arch}'. "
221
242
  f"The code needs to be downloaded, config="
222
243
  f"\n{pprint.pformat(config)}."
223
244
  ) from e
@@ -225,20 +246,31 @@ def get_untrained_model_with_inputs(
225
246
  assert same_as_pretrained and use_pretrained, (
226
247
  f"Model {model_id!r} cannot be built, the model cannot be built. "
227
248
  f"It must be downloaded. Use same_as_pretrained=True "
228
- f"and use_pretrained=True."
249
+ f"and use_pretrained=True, arch={arch!r}, config={config}"
250
+ )
251
+ if verbose:
252
+ print(
253
+ f"[get_untrained_model_with_inputs] -- done in "
254
+ f"{time.perf_counter() - begin}s"
229
255
  )
230
256
 
231
- try:
232
- if type(config) is dict:
233
- model = cls_model(**config)
234
- else:
235
- model = cls_model(config)
236
- except RuntimeError as e:
237
- raise RuntimeError(
238
- f"Unable to instantiate class {cls_model.__name__} with\n{config}"
239
- ) from e
257
+ seed = int(os.environ.get("SEED", "17"))
258
+ torch.manual_seed(seed)
259
+ model = instantiate_specific_model(cls_model, config)
260
+ if model is None:
261
+ try:
262
+ if type(config) is dict:
263
+ model = cls_model(**config)
264
+ else:
265
+ model = cls_model(config)
266
+ except RuntimeError as e:
267
+ raise RuntimeError(
268
+ f"Unable to instantiate class {cls_model.__name__} with\n{config}"
269
+ ) from e
240
270
 
241
271
  # input kwargs
272
+ seed = int(os.environ.get("SEED", "17")) + 1
273
+ torch.manual_seed(seed)
242
274
  kwargs, fct = random_input_kwargs(config, task) # type: ignore[arg-type]
243
275
  if verbose:
244
276
  print(f"[get_untrained_model_with_inputs] use fct={fct}")
@@ -250,7 +282,7 @@ def get_untrained_model_with_inputs(
250
282
 
251
283
  # This line is important. Some models may produce different
252
284
  # outputs even with the same inputs in training mode.
253
- model.eval()
285
+ model.eval() # type: ignore[union-attr]
254
286
  res = fct(model, config, add_second_input=add_second_input, **kwargs)
255
287
 
256
288
  res["input_kwargs"] = kwargs
@@ -1,6 +1,33 @@
1
1
  from typing import Any, Dict, Tuple
2
2
 
3
3
 
4
+ def instantiate_specific_model(cls_model: type, config: Any) -> object:
5
+ """
6
+ Instantiates some model requiring some specific code.
7
+ """
8
+ if cls_model.__name__ == "CosmosTransformer3DModel":
9
+ return instantiate_CosmosTransformer3DModel(cls_model, config)
10
+ return None
11
+
12
+
13
+ def instantiate_CosmosTransformer3DModel(cls_model: type, config: Any) -> object:
14
+ kwargs = dict(
15
+ in_channels=config.in_channels,
16
+ out_channels=config.out_channels,
17
+ attention_head_dim=config.attention_head_dim,
18
+ mlp_ratio=config.mlp_ratio,
19
+ num_layers=config.num_layers,
20
+ text_embed_dim=config.text_embed_dim,
21
+ adaln_lora_dim=config.adaln_lora_dim,
22
+ max_size=config.max_size,
23
+ patch_size=config.patch_size,
24
+ rope_scale=config.rope_scale,
25
+ concat_padding_mask=config.concat_padding_mask,
26
+ extra_pos_embed_type=config.extra_pos_embed_type,
27
+ )
28
+ return cls_model(**kwargs)
29
+
30
+
4
31
  class SpecificConfig:
5
32
  """Creates a specific configuration for the loaded model."""
6
33