onnx-diagnostic 0.7.12__py3-none-any.whl → 0.7.13__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.
Files changed (27) hide show
  1. onnx_diagnostic/__init__.py +1 -1
  2. onnx_diagnostic/export/dynamic_shapes.py +11 -2
  3. onnx_diagnostic/helpers/helper.py +11 -5
  4. onnx_diagnostic/helpers/mini_onnx_builder.py +17 -0
  5. onnx_diagnostic/helpers/model_builder_helper.py +1 -0
  6. onnx_diagnostic/helpers/rt_helper.py +2 -1
  7. onnx_diagnostic/helpers/torch_helper.py +31 -7
  8. onnx_diagnostic/reference/torch_evaluator.py +2 -2
  9. onnx_diagnostic/tasks/data/__init__.py +13 -0
  10. onnx_diagnostic/tasks/data/dummies_imagetext2text_generation_gemma3.onnx +0 -0
  11. onnx_diagnostic/tasks/image_text_to_text.py +256 -141
  12. onnx_diagnostic/tasks/text_generation.py +15 -0
  13. onnx_diagnostic/torch_export_patches/eval/__init__.py +177 -150
  14. onnx_diagnostic/torch_export_patches/eval/model_cases.py +19 -1
  15. onnx_diagnostic/torch_export_patches/onnx_export_errors.py +29 -14
  16. onnx_diagnostic/torch_export_patches/patch_inputs.py +10 -6
  17. onnx_diagnostic/torch_export_patches/patches/patch_torch.py +116 -10
  18. onnx_diagnostic/torch_export_patches/patches/patch_transformers.py +269 -4
  19. onnx_diagnostic/torch_models/hghub/hub_data_cached_configs.py +36 -0
  20. onnx_diagnostic/torch_models/hghub/model_inputs.py +31 -3
  21. onnx_diagnostic/torch_models/validate.py +114 -36
  22. onnx_diagnostic/torch_onnx/sbs.py +2 -1
  23. {onnx_diagnostic-0.7.12.dist-info → onnx_diagnostic-0.7.13.dist-info}/METADATA +11 -31
  24. {onnx_diagnostic-0.7.12.dist-info → onnx_diagnostic-0.7.13.dist-info}/RECORD +27 -25
  25. {onnx_diagnostic-0.7.12.dist-info → onnx_diagnostic-0.7.13.dist-info}/WHEEL +0 -0
  26. {onnx_diagnostic-0.7.12.dist-info → onnx_diagnostic-0.7.13.dist-info}/licenses/LICENSE.txt +0 -0
  27. {onnx_diagnostic-0.7.12.dist-info → onnx_diagnostic-0.7.13.dist-info}/top_level.txt +0 -0
@@ -3,5 +3,5 @@ Patches, Investigates onnx models.
3
3
  Functions, classes to dig into a model when this one is right, slow, wrong...
4
4
  """
5
5
 
6
- __version__ = "0.7.12"
6
+ __version__ = "0.7.13"
7
7
  __author__ = "Xavier Dupré"
@@ -56,6 +56,14 @@ class CoupleInputsDynamicShapes:
56
56
  self.kwargs = kwargs
57
57
  self.dynamic_shapes = dynamic_shapes
58
58
  self.args_names = args_names
59
+ if not self.kwargs and isinstance(self.dynamic_shapes, dict):
60
+ # This assumes the dictionary for the dynamic shapes is ordered
61
+ # the same way the args are. The input names are not known.
62
+ assert len(self.dynamic_shapes) == len(self.args), (
63
+ f"Length mismatch, kwargs is empty, len(dynamic_shapes)="
64
+ f"{len(self.dynamic_shapes)}, len(args)={len(self.args)}"
65
+ )
66
+ self.dynamic_shapes = tuple(self.dynamic_shapes.values())
59
67
 
60
68
  def __str__(self) -> str:
61
69
  return "\n".join(
@@ -232,8 +240,9 @@ class CoupleInputsDynamicShapes:
232
240
  """
233
241
  if not self.args:
234
242
  assert isinstance(self.kwargs, dict) and isinstance(self.dynamic_shapes, dict), (
235
- f"Type mismatch, args={string_type(self.args)} and "
236
- f"dynamic_shapes={self.dynamic_shapes} should have the same type."
243
+ f"Type mismatch, args={string_type(self.args)}, "
244
+ f"kwargs={string_type(self.kwargs)} and dynamic_shapes="
245
+ f"{string_type(self.dynamic_shapes)} should have the same type."
237
246
  )
238
247
  res = self._generic_walker_step(
239
248
  processor,
@@ -397,7 +397,7 @@ def string_type(
397
397
  return "AUTO"
398
398
  if verbose:
399
399
  print(f"[string_type] Y7:{type(obj)}")
400
- return str(obj)
400
+ return str(obj).replace("DimHint(DYNAMIC)", "DYNAMIC").replace("DimHint(AUTO)", "AUTO")
401
401
 
402
402
  if isinstance(obj, bool):
403
403
  if with_min_max:
@@ -516,8 +516,10 @@ def string_type(
516
516
  print(f"[string_type] V2:{type(obj)}")
517
517
  return "OV(NOTENSOR)"
518
518
  if with_min_max:
519
+ from .torch_helper import to_numpy
520
+
519
521
  try:
520
- t = obj.numpy()
522
+ t = to_numpy(obj)
521
523
  except Exception:
522
524
  # pass unable to convert into numpy (bfloat16, ...)
523
525
  if verbose:
@@ -939,7 +941,7 @@ def flatten_object(x: Any, drop_keys: bool = False) -> Any:
939
941
  return flatten_object(list(x.values()), drop_keys=drop_keys)
940
942
  return flatten_object(list(x.items()), drop_keys=drop_keys)
941
943
 
942
- if x.__class__.__name__ in {"DynamicCache", "StaticCache"}:
944
+ if x.__class__.__name__ in {"DynamicCache", "StaticCache", "HybridCache"}:
943
945
  from .cache_helper import CacheKeyValue
944
946
 
945
947
  kc = CacheKeyValue(x)
@@ -1233,9 +1235,13 @@ def max_diff(
1233
1235
 
1234
1236
  if isinstance(expected, np.ndarray) or isinstance(got, np.ndarray):
1235
1237
  if isinstance(expected, torch.Tensor):
1236
- expected = expected.detach().cpu().numpy()
1238
+ from .torch_helper import to_numpy
1239
+
1240
+ expected = to_numpy(expected)
1237
1241
  if isinstance(got, torch.Tensor):
1238
- got = got.detach().cpu().numpy()
1242
+ from .torch_helper import to_numpy
1243
+
1244
+ got = to_numpy(got)
1239
1245
  if verbose >= 6:
1240
1246
  print(f"[max_diff] tensor: {string_type(expected)} ? {string_type(got)}")
1241
1247
 
@@ -381,6 +381,23 @@ def _flatten_iterator(obj: Any, sep: str) -> Iterator:
381
381
  else:
382
382
  for p, o in _flatten_iterator(getattr(obj, att), sep):
383
383
  yield f"DynamicCache_{att}{sep}{p}", o
384
+ elif obj.__class__.__name__ == "StaticCache":
385
+ # transformers
386
+ import transformers
387
+ from .cache_helper import CacheKeyValue
388
+
389
+ assert isinstance(
390
+ obj, transformers.cache_utils.StaticCache
391
+ ), f"Unexpected type {type(obj)}"
392
+ obj = CacheKeyValue(obj)
393
+ atts = ["key_cache", "value_cache"]
394
+ for i, att in enumerate(atts):
395
+ if i == len(atts) - 1:
396
+ for p, o in _flatten_iterator(getattr(obj, att), sep):
397
+ yield f"StaticCache._{att}{sep}{p}", o
398
+ else:
399
+ for p, o in _flatten_iterator(getattr(obj, att), sep):
400
+ yield f"StaticCache_{att}{sep}{p}", o
384
401
  else:
385
402
  raise NotImplementedError(f"Unexpected type {type(obj)}")
386
403
 
@@ -203,6 +203,7 @@ def create_model_builder(
203
203
  "ChatGLMModel": builder.ChatGLMModel,
204
204
  "Ernie4_5_ForCausalLM": builder.ErnieModel,
205
205
  "GemmaForCausalLM": builder.Gemma2Model,
206
+ "Gemma2ForCausalLM": builder.Gemma2Model,
206
207
  "Gemma3ForCausalLM": builder.Gemma3Model,
207
208
  "Gemma3ForConditionalGeneration": builder.Gemma3Model,
208
209
  "GraniteForCausalLM": builder.GraniteModel,
@@ -3,6 +3,7 @@ import numpy as np
3
3
  import onnx
4
4
  import torch
5
5
  from .helper import string_type, flatten_object
6
+ from .torch_helper import to_numpy
6
7
  from .cache_helper import is_cache_dynamic_registered
7
8
 
8
9
 
@@ -56,7 +57,7 @@ def make_feeds(
56
57
  f"{string_type(torch.utils._pytree.tree_flatten(inputs)[0], with_shape=True)}"
57
58
  )
58
59
  if use_numpy:
59
- flat = [t.detach().cpu().numpy() if isinstance(t, torch.Tensor) else t for t in flat]
60
+ flat = [to_numpy(t) if isinstance(t, torch.Tensor) else t for t in flat]
60
61
  names = (
61
62
  [i.name for i in proto.graph.input]
62
63
  if isinstance(proto, onnx.ModelProto)
@@ -5,7 +5,7 @@ import os
5
5
  import sys
6
6
  import warnings
7
7
  from collections.abc import Iterable
8
- from typing import Any, Callable, Dict, List, Optional, Tuple, Union
8
+ from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
9
9
  import numpy as np
10
10
  import onnx
11
11
  from onnx.external_data_helper import load_external_data_for_tensor, uses_external_data
@@ -283,9 +283,11 @@ def steal_forward(
283
283
  ],
284
284
  fprint: Callable = string_type,
285
285
  dump_file: Optional[str] = None,
286
+ dump_drop: Optional[Set[str]] = None,
286
287
  submodules: bool = False,
287
288
  verbose: int = 0,
288
289
  storage_limit: int = 2**27,
290
+ save_as_external_data: bool = True,
289
291
  **kwargs,
290
292
  ):
291
293
  """
@@ -303,6 +305,9 @@ def steal_forward(
303
305
  :param dump_file: dumps stolen inputs and outputs in an onnx model,
304
306
  they can be restored with :func:`create_input_tensors_from_onnx_model
305
307
  <onnx_diagnostic.helpers.mini_onnx_builder.create_input_tensors_from_onnx_model>`
308
+ :param dump_drop: to drop some inputs too big (only if dump_file is specified)
309
+ :param save_as_external_data: True by default, but maybe better to have everything
310
+ in a single file if possible
306
311
  :param submodules: if True and model is a module, the list extended with all the submodules
307
312
  the module contains
308
313
  :param verbose: verbosity
@@ -411,6 +416,15 @@ def steal_forward(
411
416
  if verbose:
412
417
  size = torch_tensor_size(storage)
413
418
  print(f"-- gather stored {len(storage)} objects, size={size // 2 ** 20} Mb")
419
+ if dump_drop:
420
+ for k, v in storage.items():
421
+ if k[-1] == "I":
422
+ _args, kwargs = v
423
+ ii = set(kwargs) & dump_drop
424
+ if ii:
425
+ for i in ii:
426
+ print("---", i)
427
+ del kwargs[i]
414
428
  proto = create_onnx_model_from_input_tensors(storage)
415
429
  if verbose:
416
430
  print("-- dumps stored objects")
@@ -420,7 +434,7 @@ def steal_forward(
420
434
  onnx.save(
421
435
  proto,
422
436
  dump_file,
423
- save_as_external_data=True,
437
+ save_as_external_data=save_as_external_data,
424
438
  all_tensors_to_one_file=True,
425
439
  location=location,
426
440
  )
@@ -464,10 +478,10 @@ def is_torchdynamo_exporting() -> bool:
464
478
  return False
465
479
 
466
480
 
467
- def to_numpy(tensor: "torch.Tensor"): # noqa: F821
481
+ def to_numpy(tensor: "torch.Tensor") -> np.ndarray: # noqa: F821
468
482
  """Converts a :class:`torch.Tensor` to :class:`numpy.ndarray`."""
469
483
  try:
470
- return tensor.numpy()
484
+ return tensor.detach().cpu().numpy()
471
485
  except TypeError:
472
486
  # We try with ml_dtypes
473
487
  pass
@@ -476,7 +490,7 @@ def to_numpy(tensor: "torch.Tensor"): # noqa: F821
476
490
 
477
491
  conv = {torch.bfloat16: ml_dtypes.bfloat16}
478
492
  assert tensor.dtype in conv, f"Unsupported type {tensor.dtype}, not in {conv}"
479
- return tensor.to(torch.float32).numpy().astype(conv[tensor.dtype])
493
+ return tensor.detach().to(torch.float32).cpu().numpy().astype(conv[tensor.dtype])
480
494
 
481
495
 
482
496
  def replace_string_by_dynamic(dynamic_shapes: Any) -> Any:
@@ -765,7 +779,12 @@ def to_any(value: Any, to_value: Union[torch.dtype, torch.device, str]) -> Any:
765
779
 
766
780
 
767
781
  def torch_deepcopy(value: Any) -> Any:
768
- """Makes a deepcopy."""
782
+ """
783
+ Makes a deep copy.
784
+
785
+ :param value: any value
786
+ :return: a deep copy
787
+ """
769
788
  if value is None:
770
789
  return None
771
790
  if isinstance(value, (int, float, str)):
@@ -794,9 +813,14 @@ def torch_deepcopy(value: Any) -> Any:
794
813
  from .cache_helper import CacheKeyValue
795
814
 
796
815
  ca = CacheKeyValue(value)
816
+ if len(ca.key_cache) == 0:
817
+ # Use of deepcopy.
818
+ import copy
819
+
820
+ return copy.deepcopy(value)
797
821
  return make_static_cache(
798
822
  torch_deepcopy(list(zip(ca.key_cache, ca.value_cache))),
799
- max_cache_len=value.max_cache_len,
823
+ max_cache_len=max([value.max_cache_len, *[t.shape[2] for t in ca.key_cache]]),
800
824
  )
801
825
  if value.__class__.__name__ == "HybridCache":
802
826
  from .cache_helper import CacheKeyValue
@@ -3,7 +3,7 @@ from typing import Dict, List, Optional, Sequence, Tuple, Union
3
3
  import numpy as np
4
4
  import onnx
5
5
  import torch
6
- from ..helpers.torch_helper import to_tensor
6
+ from ..helpers.torch_helper import to_tensor, to_numpy
7
7
  from ..torch_onnx.runtime_info import first_used_last_used, RuntimeValue
8
8
  from .report_results_comparison import ReportResultComparison
9
9
  from . import torch_ops
@@ -578,7 +578,7 @@ class TorchOnnxEvaluator:
578
578
  print(f"- clean {o}")
579
579
 
580
580
  if use_numpy:
581
- return [None if a is None else a.detach().cpu().numpy() for a in fres]
581
+ return [None if a is None else to_numpy(a) for a in fres]
582
582
  return fres
583
583
 
584
584
  def run_with_values(
@@ -0,0 +1,13 @@
1
+ import os
2
+
3
+
4
+ def get_data(name: str):
5
+ """Returns data stored in this folder."""
6
+ filename = os.path.join(os.path.dirname(__file__), name)
7
+ assert os.path.exists(
8
+ filename
9
+ ), f"Unable to find a file with {name!r}, looked for {filename!r}"
10
+
11
+ from ...helpers.mini_onnx_builder import create_input_tensors_from_onnx_model
12
+
13
+ return create_input_tensors_from_onnx_model(filename)