onnx-diagnostic 0.8.1__py3-none-any.whl → 0.8.3__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 (51) hide show
  1. onnx_diagnostic/__init__.py +1 -1
  2. onnx_diagnostic/_command_lines_parser.py +387 -12
  3. onnx_diagnostic/export/api.py +118 -5
  4. onnx_diagnostic/export/control_flow.py +214 -0
  5. onnx_diagnostic/export/control_flow_onnx.py +528 -0
  6. onnx_diagnostic/export/control_flow_research.py +135 -0
  7. onnx_diagnostic/export/onnx_plug.py +396 -0
  8. onnx_diagnostic/ext_test_case.py +118 -25
  9. onnx_diagnostic/helpers/cache_helper.py +218 -204
  10. onnx_diagnostic/helpers/dot_helper.py +210 -0
  11. onnx_diagnostic/helpers/helper.py +92 -26
  12. onnx_diagnostic/helpers/log_helper.py +26 -4
  13. onnx_diagnostic/helpers/mini_onnx_builder.py +57 -3
  14. onnx_diagnostic/helpers/model_builder_helper.py +27 -0
  15. onnx_diagnostic/helpers/onnx_helper.py +115 -16
  16. onnx_diagnostic/helpers/ort_session.py +37 -11
  17. onnx_diagnostic/helpers/rt_helper.py +547 -0
  18. onnx_diagnostic/helpers/torch_fx_graph_helper.py +164 -0
  19. onnx_diagnostic/helpers/torch_helper.py +108 -6
  20. onnx_diagnostic/reference/ort_evaluator.py +233 -28
  21. onnx_diagnostic/tasks/feature_extraction.py +15 -14
  22. onnx_diagnostic/tasks/image_text_to_text.py +5 -1
  23. onnx_diagnostic/tasks/summarization.py +72 -137
  24. onnx_diagnostic/torch_export_patches/eval/model_cases.py +28 -0
  25. onnx_diagnostic/torch_export_patches/onnx_export_errors.py +1 -1
  26. onnx_diagnostic/torch_export_patches/onnx_export_serialization.py +11 -7
  27. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_attention.py +235 -0
  28. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_cache_utils.py +50 -0
  29. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_causal_mask.py +89 -0
  30. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_dynamic_cache.py +177 -0
  31. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_gemma3.py +54 -0
  32. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_generation_mixin.py +486 -0
  33. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_idefics.py +156 -0
  34. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_masking_utils.py +173 -0
  35. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_qwen2.py +99 -0
  36. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_qwen2_5.py +680 -0
  37. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_qwen3.py +106 -0
  38. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_rotary_embedding.py +412 -0
  39. onnx_diagnostic/torch_export_patches/patches/_patch_transformers_sam_mask_decoder.py +132 -0
  40. onnx_diagnostic/torch_export_patches/patches/patch_helper.py +28 -0
  41. onnx_diagnostic/torch_export_patches/patches/patch_transformers.py +65 -2107
  42. onnx_diagnostic/torch_models/hghub/hub_data_cached_configs.py +53 -0
  43. onnx_diagnostic/torch_models/hghub/model_inputs.py +15 -2
  44. onnx_diagnostic/torch_models/validate.py +50 -1
  45. onnx_diagnostic/torch_onnx/sbs.py +963 -312
  46. onnx_diagnostic/torch_onnx/sbs_dataclasses.py +491 -0
  47. {onnx_diagnostic-0.8.1.dist-info → onnx_diagnostic-0.8.3.dist-info}/METADATA +1 -1
  48. {onnx_diagnostic-0.8.1.dist-info → onnx_diagnostic-0.8.3.dist-info}/RECORD +51 -30
  49. {onnx_diagnostic-0.8.1.dist-info → onnx_diagnostic-0.8.3.dist-info}/WHEEL +0 -0
  50. {onnx_diagnostic-0.8.1.dist-info → onnx_diagnostic-0.8.3.dist-info}/licenses/LICENSE.txt +0 -0
  51. {onnx_diagnostic-0.8.1.dist-info → onnx_diagnostic-0.8.3.dist-info}/top_level.txt +0 -0
@@ -4903,3 +4903,56 @@ def _ccached_hf_internal_testing_tiny_random_gemma3_for_causal_lm():
4903
4903
  "vocab_size": 262144,
4904
4904
  }
4905
4905
  )
4906
+
4907
+
4908
+ def _ccached_qwen_qwen2_5_vl_7b_instruct():
4909
+ "Qwen/Qwen2.5-VL-7B-Instruct"
4910
+ return transformers.Qwen2_5_VLConfig(
4911
+ **{
4912
+ "architectures": ["Qwen2_5_VLForConditionalGeneration"],
4913
+ "attention_dropout": 0.0,
4914
+ "bos_token_id": 151643,
4915
+ "eos_token_id": 151645,
4916
+ "vision_start_token_id": 151652,
4917
+ "vision_end_token_id": 151653,
4918
+ "vision_token_id": 151654,
4919
+ "image_token_id": 151655,
4920
+ "video_token_id": 151656,
4921
+ "hidden_act": "silu",
4922
+ "hidden_size": 3584,
4923
+ "initializer_range": 0.02,
4924
+ "intermediate_size": 18944,
4925
+ "max_position_embeddings": 128000,
4926
+ "max_window_layers": 28,
4927
+ "model_type": "qwen2_5_vl",
4928
+ "num_attention_heads": 28,
4929
+ "num_hidden_layers": 28,
4930
+ "num_key_value_heads": 4,
4931
+ "rms_norm_eps": 1e-06,
4932
+ "rope_theta": 1000000.0,
4933
+ "sliding_window": 32768,
4934
+ "tie_word_embeddings": false,
4935
+ "torch_dtype": "bfloat16",
4936
+ "transformers_version": "4.41.2",
4937
+ "use_cache": true,
4938
+ "use_sliding_window": false,
4939
+ "vision_config": {
4940
+ "depth": 32,
4941
+ "hidden_act": "silu",
4942
+ "hidden_size": 1280,
4943
+ "intermediate_size": 3420,
4944
+ "num_heads": 16,
4945
+ "in_chans": 3,
4946
+ "out_hidden_size": 3584,
4947
+ "patch_size": 14,
4948
+ "spatial_merge_size": 2,
4949
+ "spatial_patch_size": 14,
4950
+ "window_size": 112,
4951
+ "fullatt_block_indexes": [7, 15, 23, 31],
4952
+ "tokens_per_second": 2,
4953
+ "temporal_patch_size": 2,
4954
+ },
4955
+ "rope_scaling": {"type": "mrope", "mrope_section": [16, 24, 24]},
4956
+ "vocab_size": 152064,
4957
+ }
4958
+ )
@@ -3,7 +3,7 @@ import inspect
3
3
  import os
4
4
  import pprint
5
5
  import time
6
- from typing import Any, Dict, Optional, Tuple
6
+ from typing import Any, Callable, Dict, Optional, Tuple
7
7
  import torch
8
8
  import transformers
9
9
  from ...helpers.config_helper import update_config, build_diff_config
@@ -53,6 +53,7 @@ def get_untrained_model_with_inputs(
53
53
  add_second_input: int = 1,
54
54
  subfolder: Optional[str] = None,
55
55
  use_only_preinstalled: bool = False,
56
+ config_reduction: Optional[Callable[[Any, str], Dict]] = None,
56
57
  ) -> Dict[str, Any]:
57
58
  """
58
59
  Gets a non initialized model similar to the original model
@@ -75,6 +76,12 @@ def get_untrained_model_with_inputs(
75
76
  supports different shapes
76
77
  :param subfolder: subfolder to use for this model id
77
78
  :param use_only_preinstalled: use only preinstalled version
79
+ :param config_reduction: if specified, this function is used to reduce the
80
+ model size by tweaking the configuration, it returns a dictionary with values
81
+ to update, if empty, function:`reduce_model_config
82
+ <onnx_diagnostic.torch_models.hghub.reduce_model_config>`,
83
+ this function takes a configuration and a task (string)
84
+ as arguments
78
85
  :return: dictionary with a model, inputs, dynamic shapes, and the configuration,
79
86
  some necessary rewriting as well
80
87
 
@@ -157,7 +164,13 @@ def get_untrained_model_with_inputs(
157
164
 
158
165
  # updating the configuration
159
166
  config0 = copy.deepcopy(config)
160
- mkwargs = reduce_model_config(config, task) if not same_as_pretrained else {}
167
+ if config_reduction:
168
+ assert (
169
+ not same_as_pretrained
170
+ ), "config_reduction should be None if same_as_pretrained is True"
171
+ mkwargs = config_reduction(config, task)
172
+ else:
173
+ mkwargs = reduce_model_config(config, task) if not same_as_pretrained else {}
161
174
  if model_kwargs:
162
175
  for k, v in model_kwargs.items():
163
176
  if isinstance(v, dict):
@@ -671,7 +671,16 @@ def _call_exporter(
671
671
  do_run,
672
672
  output_names,
673
673
  exporter_options,
674
+ save_ep,
674
675
  ):
676
+ if save_ep and dump_folder:
677
+ for name in data:
678
+ if name.startswith("inputs"):
679
+ if verbose:
680
+ print(f"[validate_model] -- dump {name!r}")
681
+ filename = os.path.join(dump_folder, f"{save_ep}.{name}.pt")
682
+ torch.save(data[name], filename)
683
+
675
684
  if exporter:
676
685
  expop = exporter_options or {}
677
686
  if verbose:
@@ -711,6 +720,7 @@ def _call_exporter(
711
720
  dump_folder=dump_folder,
712
721
  output_names=output_names,
713
722
  exporter_options=expop,
723
+ save_ep=save_ep,
714
724
  )
715
725
  else:
716
726
  data["inputs_export"] = data["inputs"]
@@ -831,6 +841,7 @@ def validate_model(
831
841
  output_names: Optional[List[str]] = None,
832
842
  ort_logs: bool = False,
833
843
  quiet_input_sets: Optional[Set[str]] = None,
844
+ save_ep: Optional[str] = None,
834
845
  ) -> Tuple[Dict[str, Union[int, float, str]], Dict[str, Any]]:
835
846
  """
836
847
  Validates a model.
@@ -889,6 +900,8 @@ def validate_model(
889
900
  :param ort_logs: increases onnxruntime verbosity when creating the session
890
901
  :param quiet_input_sets: avoid raising an exception if the inputs belongs to that set
891
902
  even if quiet is False
903
+ :param save_ep: if not empty, this can be used to save the input sets and
904
+ the exported program
892
905
  :return: two dictionaries, one with some metrics,
893
906
  another one with whatever the function produces
894
907
 
@@ -952,6 +965,7 @@ def validate_model(
952
965
  subfolder=subfolder,
953
966
  use_pretrained=use_pretrained,
954
967
  same_as_pretrained=same_as_pretrained,
968
+ save_ep=save_ep,
955
969
  )
956
970
  if dump_folder:
957
971
  with open(dump_stats, "w") as f:
@@ -1038,6 +1052,7 @@ def _validate_model_step1(
1038
1052
  subfolder,
1039
1053
  use_pretrained,
1040
1054
  same_as_pretrained,
1055
+ save_ep,
1041
1056
  ):
1042
1057
  assert not do_same or do_run, (
1043
1058
  f"Discrepancies cannot be measured if the model is not run, "
@@ -1153,6 +1168,7 @@ def _validate_model_step1(
1153
1168
  do_run=do_run,
1154
1169
  output_names=output_names,
1155
1170
  exporter_options=exporter_options,
1171
+ save_ep=save_ep,
1156
1172
  )
1157
1173
 
1158
1174
  cont, dump_stats = _dump_onnx_model(
@@ -1426,6 +1442,7 @@ def call_exporter(
1426
1442
  dump_folder: Optional[str] = None,
1427
1443
  output_names: Optional[List[str]] = None,
1428
1444
  exporter_options: Optional[Dict[str, Any]] = None,
1445
+ save_ep: Optional[str] = None,
1429
1446
  ) -> Tuple[Dict[str, Union[int, float, str]], Dict[str, Any]]:
1430
1447
  """
1431
1448
  Calls an exporter on a model;
@@ -1440,6 +1457,7 @@ def call_exporter(
1440
1457
  :param dump_folder: to dump additional information
1441
1458
  :param output_names: list of output names to use with the onnx exporter
1442
1459
  :param exporter_options: exporter options
1460
+ :param save_ep: saves the exported program
1443
1461
  :return: two dictionaries, one with some metrics,
1444
1462
  another one with whatever the function produces
1445
1463
  """
@@ -1456,6 +1474,8 @@ def call_exporter(
1456
1474
  optimization=optimization,
1457
1475
  do_run=do_run,
1458
1476
  exporter_options=exporter_options,
1477
+ save_ep=save_ep,
1478
+ dump_folder=dump_folder,
1459
1479
  )
1460
1480
  _restore_torch_export_export(summary)
1461
1481
  return summary, data
@@ -1469,6 +1489,8 @@ def call_exporter(
1469
1489
  optimization=optimization,
1470
1490
  output_names=output_names,
1471
1491
  exporter_options=exporter_options,
1492
+ dump_folder=dump_folder,
1493
+ save_ep=save_ep,
1472
1494
  )
1473
1495
  _restore_torch_export_export(summary)
1474
1496
  return summary, data
@@ -1483,6 +1505,7 @@ def call_exporter(
1483
1505
  dump_folder=dump_folder,
1484
1506
  output_names=output_names,
1485
1507
  exporter_options=exporter_options,
1508
+ save_ep=save_ep,
1486
1509
  )
1487
1510
  _restore_torch_export_export(summary)
1488
1511
  return summary, data
@@ -1516,6 +1539,8 @@ def call_torch_export_export(
1516
1539
  optimization: Optional[str] = None,
1517
1540
  do_run: bool = False,
1518
1541
  exporter_options: Optional[Dict[str, Any]] = None,
1542
+ dump_folder: Optional[str] = None,
1543
+ save_ep: Optional[str] = None,
1519
1544
  ):
1520
1545
  """
1521
1546
  Exports a model with :func:`torch.export.export`.
@@ -1529,6 +1554,8 @@ def call_torch_export_export(
1529
1554
  :param optimization: optimization to do
1530
1555
  :param do_run: runs and compute discrepancies
1531
1556
  :param exporter_options: additional options given to the exporter
1557
+ :param dump_folder: folder where to dump the exported program
1558
+ :param save_ep: to save the exported program
1532
1559
  :return: two dictionaries, one with some metrics,
1533
1560
  another one with whatever the function produces
1534
1561
  """
@@ -1604,6 +1631,12 @@ def call_torch_export_export(
1604
1631
  print(ep)
1605
1632
  print("[call_torch_export_export] -- End of ExportedProgram")
1606
1633
 
1634
+ if dump_folder and save_ep:
1635
+ fname = f"{save_ep}.pt2"
1636
+ if verbose:
1637
+ print(f"[call_torch_export_export] -- save the exported program in {fname!r}")
1638
+ torch.export.save(ep, os.path.join(dump_folder, fname))
1639
+
1607
1640
  if do_run:
1608
1641
  # We check for discrepancies.
1609
1642
  if verbose:
@@ -1880,6 +1913,8 @@ def call_torch_export_onnx(
1880
1913
  optimization: Optional[str] = None,
1881
1914
  output_names: Optional[List[str]] = None,
1882
1915
  exporter_options: Optional[Dict[str, Any]] = None,
1916
+ dump_folder: Optional[str] = None,
1917
+ save_ep: Optional[str] = None,
1883
1918
  ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
1884
1919
  """
1885
1920
  Exports a model into onnx.
@@ -1893,6 +1928,8 @@ def call_torch_export_onnx(
1893
1928
  :param optimization: optimization to do
1894
1929
  :param output_names: output names to use
1895
1930
  :param exporter_options: additional options to give the exporter
1931
+ :param dump_folder: to know where to dump the exported program
1932
+ :param save_ep: to save the exported program
1896
1933
  :return: two dictionaries, one with some metrics,
1897
1934
  another one with whatever the function produces
1898
1935
  """
@@ -1986,6 +2023,12 @@ def call_torch_export_onnx(
1986
2023
  return summary, data
1987
2024
 
1988
2025
  assert epo is not None, "no onnx export was found"
2026
+ if dump_folder and save_ep:
2027
+ fname = f"{save_ep}.pt2"
2028
+ if verbose:
2029
+ print(f"[call_torch_export_export] -- save the exported program in {fname!r}")
2030
+ torch.export.save(epo.exported_program, os.path.join(dump_folder, fname))
2031
+
1989
2032
  if verbose:
1990
2033
  print("[call_torch_export_onnx] done (export)")
1991
2034
  data["onnx_program"] = epo
@@ -2219,6 +2262,7 @@ def call_torch_export_custom(
2219
2262
  dump_folder: Optional[str] = None,
2220
2263
  output_names: Optional[List[str]] = None,
2221
2264
  exporter_options: Optional[Dict[str, Any]] = None,
2265
+ save_ep: Optional[str] = None,
2222
2266
  ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
2223
2267
  """
2224
2268
  Exports a model into onnx.
@@ -2233,6 +2277,7 @@ def call_torch_export_custom(
2233
2277
  :param dump_folder: to store additional information
2234
2278
  :param output_names: list of output names to use
2235
2279
  :param exporter_options: additional exporter options
2280
+ :param save_ep: to save the exported program
2236
2281
  :return: two dictionaries, one with some metrics,
2237
2282
  another one with whatever the function produces
2238
2283
  """
@@ -2345,7 +2390,11 @@ def call_torch_export_custom(
2345
2390
  export_options = ExportOptions(
2346
2391
  strict=strict,
2347
2392
  decomposition_table=decomposition_table,
2348
- save_ep=(os.path.join(dump_folder, f"{exporter}.ep") if dump_folder else None),
2393
+ save_ep=(
2394
+ (os.path.join(dump_folder, f"{exporter}.ep"), 2**35 if save_ep else 2**18)
2395
+ if dump_folder
2396
+ else None
2397
+ ),
2349
2398
  **exporter_options,
2350
2399
  )
2351
2400
  options = OptimizationOptions(patterns=optimization) if optimization else None