onnx-diagnostic 0.7.8__py3-none-any.whl → 0.7.10__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.
- onnx_diagnostic/__init__.py +1 -1
- onnx_diagnostic/_command_lines_parser.py +2 -2
- onnx_diagnostic/helpers/_log_helper.py +4 -2
- onnx_diagnostic/helpers/cache_helper.py +4 -4
- onnx_diagnostic/helpers/helper.py +8 -0
- onnx_diagnostic/helpers/log_helper.py +7 -1
- onnx_diagnostic/helpers/model_builder_helper.py +5 -0
- onnx_diagnostic/helpers/onnx_helper.py +1 -1
- onnx_diagnostic/helpers/torch_helper.py +14 -4
- onnx_diagnostic/reference/ops/op_scan.py +5 -5
- onnx_diagnostic/reference/ort_evaluator.py +2 -2
- onnx_diagnostic/tasks/automatic_speech_recognition.py +1 -1
- onnx_diagnostic/tasks/feature_extraction.py +1 -1
- onnx_diagnostic/tasks/fill_mask.py +1 -1
- onnx_diagnostic/tasks/image_text_to_text.py +2 -2
- onnx_diagnostic/tasks/sentence_similarity.py +1 -1
- onnx_diagnostic/tasks/summarization.py +1 -1
- onnx_diagnostic/tasks/text2text_generation.py +1 -1
- onnx_diagnostic/tasks/text_classification.py +1 -1
- onnx_diagnostic/tasks/text_generation.py +1 -1
- onnx_diagnostic/tasks/zero_shot_image_classification.py +1 -1
- onnx_diagnostic/torch_export_patches/eval/model_cases.py +3 -3
- onnx_diagnostic/torch_export_patches/onnx_export_errors.py +98 -4
- onnx_diagnostic/torch_export_patches/patches/patch_torch.py +4 -1
- onnx_diagnostic/torch_export_patches/patches/patch_transformers.py +37 -2
- onnx_diagnostic/torch_export_patches/serialization/transformers_impl.py +0 -1
- onnx_diagnostic/torch_models/hghub/hub_data.py +2 -0
- onnx_diagnostic/torch_models/hghub/hub_data_cached_configs.py +142 -0
- onnx_diagnostic/torch_models/hghub/model_inputs.py +139 -126
- onnx_diagnostic/torch_models/hghub/model_specific.py +49 -0
- onnx_diagnostic/torch_models/untrained/llm_phi2.py +11 -3
- onnx_diagnostic/torch_models/validate.py +44 -4
- onnx_diagnostic/torch_onnx/sbs.py +1 -1
- {onnx_diagnostic-0.7.8.dist-info → onnx_diagnostic-0.7.10.dist-info}/METADATA +2 -2
- {onnx_diagnostic-0.7.8.dist-info → onnx_diagnostic-0.7.10.dist-info}/RECORD +38 -37
- {onnx_diagnostic-0.7.8.dist-info → onnx_diagnostic-0.7.10.dist-info}/WHEEL +0 -0
- {onnx_diagnostic-0.7.8.dist-info → onnx_diagnostic-0.7.10.dist-info}/licenses/LICENSE.txt +0 -0
- {onnx_diagnostic-0.7.8.dist-info → onnx_diagnostic-0.7.10.dist-info}/top_level.txt +0 -0
|
@@ -4687,3 +4687,145 @@ def _ccached_zai_glm_45():
|
|
|
4687
4687
|
},
|
|
4688
4688
|
}
|
|
4689
4689
|
)
|
|
4690
|
+
|
|
4691
|
+
|
|
4692
|
+
def _ccached_microsoft_phi3_mini_128k_instruct():
|
|
4693
|
+
"microsoft/Phi-3-mini-128k-instruct"
|
|
4694
|
+
return transformers.Phi3Config(
|
|
4695
|
+
**{
|
|
4696
|
+
"_name_or_path": "Phi-3-mini-128k-instruct",
|
|
4697
|
+
"architectures": ["Phi3ForCausalLM"],
|
|
4698
|
+
"attention_dropout": 0.0,
|
|
4699
|
+
"auto_map": {
|
|
4700
|
+
"AutoConfig": "configuration_phi3.Phi3Config",
|
|
4701
|
+
"AutoModelForCausalLM": "modeling_phi3.Phi3ForCausalLM",
|
|
4702
|
+
},
|
|
4703
|
+
"bos_token_id": 1,
|
|
4704
|
+
"embd_pdrop": 0.0,
|
|
4705
|
+
"eos_token_id": 32000,
|
|
4706
|
+
"hidden_act": "silu",
|
|
4707
|
+
"hidden_size": 3072,
|
|
4708
|
+
"initializer_range": 0.02,
|
|
4709
|
+
"intermediate_size": 8192,
|
|
4710
|
+
"max_position_embeddings": 131072,
|
|
4711
|
+
"model_type": "phi3",
|
|
4712
|
+
"num_attention_heads": 32,
|
|
4713
|
+
"num_hidden_layers": 32,
|
|
4714
|
+
"num_key_value_heads": 32,
|
|
4715
|
+
"original_max_position_embeddings": 4096,
|
|
4716
|
+
"pad_token_id": 32000,
|
|
4717
|
+
"resid_pdrop": 0.0,
|
|
4718
|
+
"rms_norm_eps": 1e-05,
|
|
4719
|
+
"rope_scaling": {
|
|
4720
|
+
"long_factor": [
|
|
4721
|
+
1.0700000524520874,
|
|
4722
|
+
1.1200000047683716,
|
|
4723
|
+
1.149999976158142,
|
|
4724
|
+
1.4199999570846558,
|
|
4725
|
+
1.5699999332427979,
|
|
4726
|
+
1.7999999523162842,
|
|
4727
|
+
2.129999876022339,
|
|
4728
|
+
2.129999876022339,
|
|
4729
|
+
3.009999990463257,
|
|
4730
|
+
5.910000324249268,
|
|
4731
|
+
6.950000286102295,
|
|
4732
|
+
9.070000648498535,
|
|
4733
|
+
9.930000305175781,
|
|
4734
|
+
10.710000038146973,
|
|
4735
|
+
11.130000114440918,
|
|
4736
|
+
14.609999656677246,
|
|
4737
|
+
15.409998893737793,
|
|
4738
|
+
19.809999465942383,
|
|
4739
|
+
37.279998779296875,
|
|
4740
|
+
38.279998779296875,
|
|
4741
|
+
38.599998474121094,
|
|
4742
|
+
40.12000274658203,
|
|
4743
|
+
46.20000457763672,
|
|
4744
|
+
50.940006256103516,
|
|
4745
|
+
53.66000747680664,
|
|
4746
|
+
54.9373893737793,
|
|
4747
|
+
56.89738845825195,
|
|
4748
|
+
57.28738784790039,
|
|
4749
|
+
59.98738479614258,
|
|
4750
|
+
60.86738586425781,
|
|
4751
|
+
60.887386322021484,
|
|
4752
|
+
61.71739196777344,
|
|
4753
|
+
62.91739273071289,
|
|
4754
|
+
62.957393646240234,
|
|
4755
|
+
63.41739273071289,
|
|
4756
|
+
63.8173942565918,
|
|
4757
|
+
63.83739471435547,
|
|
4758
|
+
63.897396087646484,
|
|
4759
|
+
63.93739700317383,
|
|
4760
|
+
64.06739807128906,
|
|
4761
|
+
64.11434936523438,
|
|
4762
|
+
64.12435150146484,
|
|
4763
|
+
64.15435028076172,
|
|
4764
|
+
64.19435119628906,
|
|
4765
|
+
64.24435424804688,
|
|
4766
|
+
64.57435607910156,
|
|
4767
|
+
64.69000244140625,
|
|
4768
|
+
64.76000213623047,
|
|
4769
|
+
],
|
|
4770
|
+
"short_factor": [
|
|
4771
|
+
1.1,
|
|
4772
|
+
1.1,
|
|
4773
|
+
1.1,
|
|
4774
|
+
1.3000000000000003,
|
|
4775
|
+
1.3500000000000003,
|
|
4776
|
+
1.3500000000000003,
|
|
4777
|
+
1.4000000000000004,
|
|
4778
|
+
1.5500000000000005,
|
|
4779
|
+
2.000000000000001,
|
|
4780
|
+
2.000000000000001,
|
|
4781
|
+
2.000000000000001,
|
|
4782
|
+
2.000000000000001,
|
|
4783
|
+
2.000000000000001,
|
|
4784
|
+
2.000000000000001,
|
|
4785
|
+
2.000000000000001,
|
|
4786
|
+
2.000000000000001,
|
|
4787
|
+
2.000000000000001,
|
|
4788
|
+
2.000000000000001,
|
|
4789
|
+
2.000000000000001,
|
|
4790
|
+
2.000000000000001,
|
|
4791
|
+
2.000000000000001,
|
|
4792
|
+
2.000000000000001,
|
|
4793
|
+
2.000000000000001,
|
|
4794
|
+
2.000000000000001,
|
|
4795
|
+
2.000000000000001,
|
|
4796
|
+
2.0500000000000007,
|
|
4797
|
+
2.0500000000000007,
|
|
4798
|
+
2.0500000000000007,
|
|
4799
|
+
2.0500000000000007,
|
|
4800
|
+
2.0500000000000007,
|
|
4801
|
+
2.0500000000000007,
|
|
4802
|
+
2.1000000000000005,
|
|
4803
|
+
2.1000000000000005,
|
|
4804
|
+
2.1500000000000004,
|
|
4805
|
+
2.25,
|
|
4806
|
+
2.25,
|
|
4807
|
+
2.25,
|
|
4808
|
+
2.25,
|
|
4809
|
+
2.25,
|
|
4810
|
+
2.3999999999999995,
|
|
4811
|
+
2.4499999999999993,
|
|
4812
|
+
2.499999999999999,
|
|
4813
|
+
2.6999999999999984,
|
|
4814
|
+
2.6999999999999984,
|
|
4815
|
+
2.7499999999999982,
|
|
4816
|
+
2.799999999999998,
|
|
4817
|
+
2.8999999999999977,
|
|
4818
|
+
3.049999999999997,
|
|
4819
|
+
],
|
|
4820
|
+
"type": "longrope",
|
|
4821
|
+
},
|
|
4822
|
+
"rope_theta": 10000.0,
|
|
4823
|
+
"sliding_window": 262144,
|
|
4824
|
+
"tie_word_embeddings": false,
|
|
4825
|
+
"torch_dtype": "bfloat16",
|
|
4826
|
+
"transformers_version": "4.40.2",
|
|
4827
|
+
"use_cache": true,
|
|
4828
|
+
"attention_bias": false,
|
|
4829
|
+
"vocab_size": 32064,
|
|
4830
|
+
}
|
|
4831
|
+
)
|
|
@@ -8,6 +8,7 @@ import transformers
|
|
|
8
8
|
from ...helpers.config_helper import update_config, build_diff_config
|
|
9
9
|
from ...tasks import reduce_model_config, random_input_kwargs
|
|
10
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
12
|
|
|
12
13
|
|
|
13
14
|
def _code_needing_rewriting(model: Any) -> Any:
|
|
@@ -73,7 +74,7 @@ def get_untrained_model_with_inputs(
|
|
|
73
74
|
print("-- configuration:", pprint.pformat(data['configuration']))
|
|
74
75
|
"""
|
|
75
76
|
assert not use_preinstalled or not use_only_preinstalled, (
|
|
76
|
-
f"model_id={model_id!r},
|
|
77
|
+
f"model_id={model_id!r}, preinstalled model is only available "
|
|
77
78
|
f"if use_only_preinstalled is False."
|
|
78
79
|
)
|
|
79
80
|
if verbose:
|
|
@@ -89,145 +90,156 @@ def get_untrained_model_with_inputs(
|
|
|
89
90
|
**(model_kwargs or {}),
|
|
90
91
|
)
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
archs = [config["_class_name"]]
|
|
97
|
-
else:
|
|
98
|
-
archs = config.architectures # type: ignore
|
|
99
|
-
task = None
|
|
100
|
-
if archs is None:
|
|
101
|
-
task = task_from_id(model_id)
|
|
102
|
-
assert task is not None or (archs is not None and len(archs) == 1), (
|
|
103
|
-
f"Unable to determine the architecture for model {model_id!r}, "
|
|
104
|
-
f"architectures={archs!r}, conf={config}"
|
|
105
|
-
)
|
|
106
|
-
if verbose:
|
|
107
|
-
print(f"[get_untrained_model_with_inputs] architectures={archs!r}")
|
|
108
|
-
print(f"[get_untrained_model_with_inputs] cls={config.__class__.__name__!r}")
|
|
109
|
-
if task is None:
|
|
110
|
-
task = task_from_arch(archs[0], model_id=model_id, subfolder=subfolder)
|
|
111
|
-
if verbose:
|
|
112
|
-
print(f"[get_untrained_model_with_inputs] task={task!r}")
|
|
93
|
+
model, task, mkwargs, diff_config = None, None, {}, None
|
|
94
|
+
if use_pretrained and same_as_pretrained:
|
|
95
|
+
if model_id in HANDLED_MODELS:
|
|
96
|
+
model, task, config = load_specific_model(model_id, verbose=verbose)
|
|
113
97
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
98
|
+
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), (
|
|
112
|
+
f"Unable to determine the architecture for model {model_id!r}, "
|
|
113
|
+
f"architectures={archs!r}, conf={config}"
|
|
122
114
|
)
|
|
115
|
+
if verbose:
|
|
116
|
+
print(f"[get_untrained_model_with_inputs] architectures={archs!r}")
|
|
117
|
+
print(f"[get_untrained_model_with_inputs] cls={config.__class__.__name__!r}")
|
|
118
|
+
if task is None:
|
|
119
|
+
task = task_from_arch(archs[0], model_id=model_id, subfolder=subfolder)
|
|
120
|
+
if verbose:
|
|
121
|
+
print(f"[get_untrained_model_with_inputs] task={task!r}")
|
|
122
|
+
|
|
123
|
+
# model kwagrs
|
|
124
|
+
if dynamic_rope is not None:
|
|
125
|
+
assert (
|
|
126
|
+
type(config) is not dict
|
|
127
|
+
), f"Unable to set dynamic_rope if the configuration is a dictionary\n{config}"
|
|
128
|
+
assert hasattr(config, "rope_scaling"), f"Missing 'rope_scaling' in\n{config}"
|
|
129
|
+
config.rope_scaling = (
|
|
130
|
+
{"rope_type": "dynamic", "factor": 10.0} if dynamic_rope else None
|
|
131
|
+
)
|
|
123
132
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
133
|
+
# updating the configuration
|
|
134
|
+
config0 = copy.deepcopy(config)
|
|
135
|
+
mkwargs = reduce_model_config(config, task) if not same_as_pretrained else {}
|
|
136
|
+
if model_kwargs:
|
|
137
|
+
for k, v in model_kwargs.items():
|
|
138
|
+
if isinstance(v, dict):
|
|
139
|
+
if k in mkwargs:
|
|
140
|
+
mkwargs[k].update(v)
|
|
141
|
+
else:
|
|
142
|
+
mkwargs[k] = v
|
|
132
143
|
else:
|
|
133
144
|
mkwargs[k] = v
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
except (ValueError, AttributeError, TypeError) as e:
|
|
141
|
-
diff_config = f"DIFF CONFIG ERROR {e}"
|
|
142
|
-
if verbose:
|
|
143
|
-
if diff_config:
|
|
144
|
-
print("[get_untrained_model_with_inputs] -- updated config")
|
|
145
|
-
pprint.pprint(diff_config)
|
|
146
|
-
print("[get_untrained_model_with_inputs] --")
|
|
147
|
-
|
|
148
|
-
# SDPA
|
|
149
|
-
if model_kwargs and "attn_implementation" in model_kwargs:
|
|
150
|
-
if hasattr(config, "_attn_implementation_autoset"):
|
|
151
|
-
config._attn_implementation_autoset = False
|
|
152
|
-
config._attn_implementation = model_kwargs["attn_implementation"] # type: ignore[union-attr]
|
|
145
|
+
if mkwargs:
|
|
146
|
+
update_config(config, mkwargs)
|
|
147
|
+
try:
|
|
148
|
+
diff_config = build_diff_config(config0, config)
|
|
149
|
+
except (ValueError, AttributeError, TypeError) as e:
|
|
150
|
+
diff_config = f"DIFF CONFIG ERROR {e}"
|
|
153
151
|
if verbose:
|
|
152
|
+
if diff_config:
|
|
153
|
+
print("[get_untrained_model_with_inputs] -- updated config")
|
|
154
|
+
pprint.pprint(diff_config)
|
|
155
|
+
print("[get_untrained_model_with_inputs] --")
|
|
156
|
+
|
|
157
|
+
# SDPA
|
|
158
|
+
if model_kwargs and "attn_implementation" in model_kwargs:
|
|
159
|
+
if hasattr(config, "_attn_implementation_autoset"):
|
|
160
|
+
config._attn_implementation_autoset = False
|
|
161
|
+
config._attn_implementation = model_kwargs["attn_implementation"] # type: ignore[union-attr]
|
|
162
|
+
if verbose:
|
|
163
|
+
print(
|
|
164
|
+
f"[get_untrained_model_with_inputs] config._attn_implementation="
|
|
165
|
+
f"{config._attn_implementation!r}" # type: ignore[union-attr]
|
|
166
|
+
)
|
|
167
|
+
elif verbose:
|
|
154
168
|
print(
|
|
155
|
-
f"[get_untrained_model_with_inputs] config._attn_implementation="
|
|
156
|
-
f"{config
|
|
169
|
+
f"[get_untrained_model_with_inputs] default config._attn_implementation="
|
|
170
|
+
f"{getattr(config, '_attn_implementation', '?')!r}" # type: ignore[union-attr]
|
|
157
171
|
)
|
|
158
|
-
elif verbose:
|
|
159
|
-
print(
|
|
160
|
-
f"[get_untrained_model_with_inputs] default config._attn_implementation="
|
|
161
|
-
f"{getattr(config, '_attn_implementation', '?')!r}" # type: ignore[union-attr]
|
|
162
|
-
)
|
|
163
|
-
|
|
164
|
-
if type(config) is dict and "_diffusers_version" in config:
|
|
165
|
-
import diffusers
|
|
166
172
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
package_source = transformers
|
|
173
|
+
if type(config) is dict and "_diffusers_version" in config:
|
|
174
|
+
import diffusers
|
|
170
175
|
|
|
171
|
-
|
|
172
|
-
model = transformers.AutoModel.from_pretrained(model_id, **mkwargs)
|
|
173
|
-
else:
|
|
174
|
-
if archs is not None:
|
|
175
|
-
try:
|
|
176
|
-
cls_model = getattr(package_source, archs[0])
|
|
177
|
-
except AttributeError as e:
|
|
178
|
-
# The code of the models is not in transformers but in the
|
|
179
|
-
# repository of the model. We need to download it.
|
|
180
|
-
pyfiles = download_code_modelid(model_id, verbose=verbose)
|
|
181
|
-
if pyfiles:
|
|
182
|
-
if "." in archs[0]:
|
|
183
|
-
cls_name = archs[0]
|
|
184
|
-
else:
|
|
185
|
-
modeling = [_ for _ in pyfiles if "/modeling_" in _]
|
|
186
|
-
assert len(modeling) == 1, (
|
|
187
|
-
f"Unable to guess the main file implemented class {archs[0]!r} "
|
|
188
|
-
f"from {pyfiles}, found={modeling}."
|
|
189
|
-
)
|
|
190
|
-
last_name = os.path.splitext(os.path.split(modeling[0])[-1])[0]
|
|
191
|
-
cls_name = f"{last_name}.{archs[0]}"
|
|
192
|
-
if verbose:
|
|
193
|
-
print(
|
|
194
|
-
f"[get_untrained_model_with_inputs] custom code for {cls_name!r}"
|
|
195
|
-
)
|
|
196
|
-
print(
|
|
197
|
-
f"[get_untrained_model_with_inputs] from folder "
|
|
198
|
-
f"{os.path.split(pyfiles[0])[0]!r}"
|
|
199
|
-
)
|
|
200
|
-
cls_model = (
|
|
201
|
-
transformers.dynamic_module_utils.get_class_from_dynamic_module(
|
|
202
|
-
cls_name,
|
|
203
|
-
pretrained_model_name_or_path=os.path.split(pyfiles[0])[0],
|
|
204
|
-
)
|
|
205
|
-
)
|
|
206
|
-
else:
|
|
207
|
-
raise AttributeError(
|
|
208
|
-
f"Unable to find class 'tranformers.{archs[0]}'. "
|
|
209
|
-
f"The code needs to be downloaded, config="
|
|
210
|
-
f"\n{pprint.pformat(config)}."
|
|
211
|
-
) from e
|
|
176
|
+
package_source = diffusers
|
|
212
177
|
else:
|
|
213
|
-
|
|
214
|
-
f"Model {model_id!r} cannot be built, the model cannot be built. "
|
|
215
|
-
f"It must be downloaded. Use same_as_pretrained=True "
|
|
216
|
-
f"and use_pretrained=True."
|
|
217
|
-
)
|
|
178
|
+
package_source = transformers
|
|
218
179
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
180
|
+
if use_pretrained:
|
|
181
|
+
model = transformers.AutoModel.from_pretrained(
|
|
182
|
+
model_id, trust_remote_code=True, **mkwargs
|
|
183
|
+
)
|
|
184
|
+
else:
|
|
185
|
+
if archs is not None:
|
|
186
|
+
try:
|
|
187
|
+
cls_model = getattr(package_source, archs[0])
|
|
188
|
+
except AttributeError as e:
|
|
189
|
+
# The code of the models is not in transformers but in the
|
|
190
|
+
# repository of the model. We need to download it.
|
|
191
|
+
pyfiles = download_code_modelid(model_id, verbose=verbose)
|
|
192
|
+
if pyfiles:
|
|
193
|
+
if "." in archs[0]:
|
|
194
|
+
cls_name = archs[0]
|
|
195
|
+
else:
|
|
196
|
+
modeling = [_ for _ in pyfiles if "/modeling_" in _]
|
|
197
|
+
assert len(modeling) == 1, (
|
|
198
|
+
f"Unable to guess the main file implemented class "
|
|
199
|
+
f"{archs[0]!r} from {pyfiles}, found={modeling}."
|
|
200
|
+
)
|
|
201
|
+
last_name = os.path.splitext(os.path.split(modeling[0])[-1])[0]
|
|
202
|
+
cls_name = f"{last_name}.{archs[0]}"
|
|
203
|
+
if verbose:
|
|
204
|
+
print(
|
|
205
|
+
f"[get_untrained_model_with_inputs] "
|
|
206
|
+
f"custom code for {cls_name!r}"
|
|
207
|
+
)
|
|
208
|
+
print(
|
|
209
|
+
f"[get_untrained_model_with_inputs] from folder "
|
|
210
|
+
f"{os.path.split(pyfiles[0])[0]!r}"
|
|
211
|
+
)
|
|
212
|
+
cls_model = (
|
|
213
|
+
transformers.dynamic_module_utils.get_class_from_dynamic_module(
|
|
214
|
+
cls_name,
|
|
215
|
+
pretrained_model_name_or_path=os.path.split(pyfiles[0])[0],
|
|
216
|
+
)
|
|
217
|
+
)
|
|
218
|
+
else:
|
|
219
|
+
raise AttributeError(
|
|
220
|
+
f"Unable to find class 'tranformers.{archs[0]}'. "
|
|
221
|
+
f"The code needs to be downloaded, config="
|
|
222
|
+
f"\n{pprint.pformat(config)}."
|
|
223
|
+
) from e
|
|
222
224
|
else:
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
225
|
+
assert same_as_pretrained and use_pretrained, (
|
|
226
|
+
f"Model {model_id!r} cannot be built, the model cannot be built. "
|
|
227
|
+
f"It must be downloaded. Use same_as_pretrained=True "
|
|
228
|
+
f"and use_pretrained=True."
|
|
229
|
+
)
|
|
230
|
+
|
|
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
|
|
228
240
|
|
|
229
241
|
# input kwargs
|
|
230
|
-
kwargs, fct = random_input_kwargs(config, task)
|
|
242
|
+
kwargs, fct = random_input_kwargs(config, task) # type: ignore[arg-type]
|
|
231
243
|
if verbose:
|
|
232
244
|
print(f"[get_untrained_model_with_inputs] use fct={fct}")
|
|
233
245
|
if os.environ.get("PRINT_CONFIG") in (1, "1"):
|
|
@@ -243,7 +255,8 @@ def get_untrained_model_with_inputs(
|
|
|
243
255
|
|
|
244
256
|
res["input_kwargs"] = kwargs
|
|
245
257
|
res["model_kwargs"] = mkwargs
|
|
246
|
-
|
|
258
|
+
if diff_config is not None:
|
|
259
|
+
res["dump_info"] = dict(config_diff=diff_config)
|
|
247
260
|
|
|
248
261
|
sizes = compute_model_size(model)
|
|
249
262
|
res["model"] = model
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from typing import Any, Dict, Tuple
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SpecificConfig:
|
|
5
|
+
"""Creates a specific configuration for the loaded model."""
|
|
6
|
+
|
|
7
|
+
def __init__(self, **kwargs):
|
|
8
|
+
self._atts = set(kwargs)
|
|
9
|
+
for k, v in kwargs.items():
|
|
10
|
+
setattr(self, k, v)
|
|
11
|
+
|
|
12
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
13
|
+
return {k: getattr(self, k) for k in self._atts if k != "_atts"}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def load_specific_model(
|
|
17
|
+
model_id: str, verbose: int = 0, **kwargs
|
|
18
|
+
) -> Tuple[Any, str, SpecificConfig]:
|
|
19
|
+
"""
|
|
20
|
+
Some models do not have any generic to be loaded.
|
|
21
|
+
This functions
|
|
22
|
+
|
|
23
|
+
:param model_id: model id
|
|
24
|
+
:param verbose: verbosiy
|
|
25
|
+
:param kwargs: additional parameters
|
|
26
|
+
:return: the model, the task associated to it, a configuration
|
|
27
|
+
"""
|
|
28
|
+
assert model_id in HANDLED_MODELS, (
|
|
29
|
+
f"Unable to load model_id={model_id!r}, "
|
|
30
|
+
f"no function is mapped to this id in {sorted(HANDLED_MODELS)}"
|
|
31
|
+
)
|
|
32
|
+
return HANDLED_MODELS[model_id](model_id, verbose=verbose, **kwargs)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _load_bingsu_adetailer(model_id: str, verbose: int = 0) -> Tuple[Any, str, SpecificConfig]:
|
|
36
|
+
"""See `Bingsu/adetailer <https://huggingface.co/Bingsu/adetailer>`_."""
|
|
37
|
+
from huggingface_hub import hf_hub_download
|
|
38
|
+
from ultralytics import YOLO
|
|
39
|
+
|
|
40
|
+
path = hf_hub_download("Bingsu/adetailer", "face_yolov8n.pt")
|
|
41
|
+
model = YOLO(path)
|
|
42
|
+
return (
|
|
43
|
+
model,
|
|
44
|
+
"object-detection",
|
|
45
|
+
SpecificConfig(architecture=type(model), image_size=224, num_channels=3),
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
HANDLED_MODELS = {"Bingsu/adetailer": _load_bingsu_adetailer}
|
|
@@ -9,6 +9,7 @@ def get_phi2(
|
|
|
9
9
|
sequence_length: int = 30,
|
|
10
10
|
sequence_length2: int = 3,
|
|
11
11
|
dynamic_rope: bool = False,
|
|
12
|
+
use_dim_not_dynamic: bool = False,
|
|
12
13
|
**kwargs,
|
|
13
14
|
) -> Dict[str, Any]:
|
|
14
15
|
"""
|
|
@@ -18,6 +19,8 @@ def get_phi2(
|
|
|
18
19
|
:param sequence_length: sequence length
|
|
19
20
|
:param sequence_length2: new sequence length
|
|
20
21
|
:param dynamic_rope: use dynamic rope (see :class:`transformers.LlamaConfig`)
|
|
22
|
+
:param use_dim_not_dynamic: uses ``torch.export.Dim`` and not a string for the batch size,
|
|
23
|
+
the sequence length and the cache length
|
|
21
24
|
:param kwargs: to overwrite the configuration, example ``num_hidden_layers=1``
|
|
22
25
|
:return: dictionary
|
|
23
26
|
|
|
@@ -62,9 +65,14 @@ def get_phi2(
|
|
|
62
65
|
n_layers = config["num_hidden_layers"]
|
|
63
66
|
num_key_value_heads = config["num_key_value_heads"]
|
|
64
67
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
+
if use_dim_not_dynamic:
|
|
69
|
+
batch = torch.export.Dim("batch", min=1, max=1024)
|
|
70
|
+
seq_length = torch.export.Dim("seq_length", min=1, max=4096)
|
|
71
|
+
cache_length = torch.export.Dim("cache_length", min=1, max=4096)
|
|
72
|
+
else:
|
|
73
|
+
batch = "batch"
|
|
74
|
+
seq_length = "seq_length"
|
|
75
|
+
cache_length = "cache_length"
|
|
68
76
|
|
|
69
77
|
shapes = {
|
|
70
78
|
"input_ids": {0: batch, 1: seq_length},
|
|
@@ -352,7 +352,7 @@ def validate_model(
|
|
|
352
352
|
The following exporters are available:
|
|
353
353
|
|
|
354
354
|
* ``export-nostrict``: run :func:`torch.export.export` (..., strict=False)
|
|
355
|
-
* ``onnx-dynamo``: run :func:`torch.onnx.export` (
|
|
355
|
+
* ``onnx-dynamo``: run :func:`torch.onnx.export` (...),
|
|
356
356
|
models can be optimized with ``optimization`` in ``("ir", "os_ort")``
|
|
357
357
|
* ``modelbuilder``: use :epkg:`ModelBuilder` to builds the onnx model
|
|
358
358
|
* ``custom``: custom exporter (see :epkg:`experimental-experiment`),
|
|
@@ -496,9 +496,15 @@ def validate_model(
|
|
|
496
496
|
cpl = CoupleInputsDynamicShapes(
|
|
497
497
|
tuple(), data[k], dynamic_shapes=data["dynamic_shapes"]
|
|
498
498
|
)
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
499
|
+
if patch_kwargs.get("patch", False):
|
|
500
|
+
with torch_export_patches(**patch_kwargs): # type: ignore[arg-type]
|
|
501
|
+
data[k] = cpl.change_dynamic_dimensions(
|
|
502
|
+
desired_values=dict(batch=1), only_desired=True
|
|
503
|
+
)
|
|
504
|
+
else:
|
|
505
|
+
data[k] = cpl.change_dynamic_dimensions(
|
|
506
|
+
desired_values=dict(batch=1), only_desired=True
|
|
507
|
+
)
|
|
502
508
|
if verbose:
|
|
503
509
|
print(f"[validate_model] batch=1 --> {string_type(data[k], with_shape=True)}")
|
|
504
510
|
|
|
@@ -706,6 +712,7 @@ def validate_model(
|
|
|
706
712
|
print(f"[validate_model] done (dump onnx) in {duration}")
|
|
707
713
|
data["onnx_filename"] = onnx_filename
|
|
708
714
|
summary["time_onnx_save"] = duration
|
|
715
|
+
summary.update(compute_statistics(onnx_filename))
|
|
709
716
|
if verbose:
|
|
710
717
|
print(f"[validate_model] dumps statistics in {dump_folder!r}...")
|
|
711
718
|
dump_stats = os.path.join(dump_folder, f"{folder_name}.stats")
|
|
@@ -809,6 +816,39 @@ def validate_model(
|
|
|
809
816
|
return summary, data
|
|
810
817
|
|
|
811
818
|
|
|
819
|
+
def compute_statistics(onnx_filename: str) -> Dict[str, Union[float, int]]:
|
|
820
|
+
"""Computes some statistics on the model itself."""
|
|
821
|
+
onx = onnx.load(onnx_filename, load_external_data=False)
|
|
822
|
+
|
|
823
|
+
def node_iter(proto):
|
|
824
|
+
if isinstance(proto, onnx.ModelProto):
|
|
825
|
+
yield from node_iter(proto.graph)
|
|
826
|
+
for f in proto.functions:
|
|
827
|
+
yield from node_iter(f)
|
|
828
|
+
elif isinstance(proto, (onnx.FunctionProto, onnx.GraphProto)):
|
|
829
|
+
for node in proto.node:
|
|
830
|
+
yield node
|
|
831
|
+
for att in node.attribute:
|
|
832
|
+
if att.type == onnx.AttributeProto.GRAPH:
|
|
833
|
+
yield from node_iter(att.g)
|
|
834
|
+
if hasattr(proto, "initializer"):
|
|
835
|
+
yield from proto.initializer
|
|
836
|
+
else:
|
|
837
|
+
raise NotImplementedError(f"Unexpected type={type(proto)}")
|
|
838
|
+
|
|
839
|
+
counts: Dict[str, Union[float, int]] = {}
|
|
840
|
+
for proto in node_iter(onx):
|
|
841
|
+
if isinstance(proto, onnx.NodeProto):
|
|
842
|
+
key = f"n_node_{proto.op_type}"
|
|
843
|
+
else:
|
|
844
|
+
key = f"n_node_initializer_{proto.data_type}"
|
|
845
|
+
|
|
846
|
+
if key not in counts:
|
|
847
|
+
counts[key] = 0
|
|
848
|
+
counts[key] += 1
|
|
849
|
+
return counts
|
|
850
|
+
|
|
851
|
+
|
|
812
852
|
def _validate_do_run_model(
|
|
813
853
|
data, summary, key, tag, expected_tag, verbose, repeat, warmup, quiet
|
|
814
854
|
):
|
|
@@ -205,7 +205,7 @@ def run_aligned(
|
|
|
205
205
|
Model(), (x,), dynamic_shapes=({0: torch.export.Dim("batch")},)
|
|
206
206
|
)
|
|
207
207
|
onx = torch.onnx.export(
|
|
208
|
-
Model(), (x,), dynamic_shapes=({0: torch.export.Dim("batch")},)
|
|
208
|
+
Model(), (x,), dynamic_shapes=({0: torch.export.Dim("batch")},)
|
|
209
209
|
).model_proto
|
|
210
210
|
results = list(
|
|
211
211
|
map(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: onnx-diagnostic
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.10
|
|
4
4
|
Summary: Investigate ONNX models
|
|
5
5
|
Home-page: https://github.com/sdpython/onnx-diagnostic
|
|
6
6
|
Author: Xavier Dupré
|
|
@@ -95,7 +95,7 @@ Getting started
|
|
|
95
95
|
|
|
96
96
|
git clone https://github.com/sdpython/onnx-diagnostic.git
|
|
97
97
|
cd onnx-diagnostic
|
|
98
|
-
pip install -e .
|
|
98
|
+
pip install -e . -v
|
|
99
99
|
|
|
100
100
|
or
|
|
101
101
|
|