onnxscript 0.1.0__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.
- onnxscript/__init__.py +131 -0
- onnxscript/_framework_apis/__init__.py +3 -0
- onnxscript/_framework_apis/torch_2_5.py +117 -0
- onnxscript/_framework_apis/torch_2_6.py +45 -0
- onnxscript/_internal/__init__.py +0 -0
- onnxscript/_internal/analysis.py +229 -0
- onnxscript/_internal/ast_utils.py +64 -0
- onnxscript/_internal/autocast.py +250 -0
- onnxscript/_internal/deprecation.py +78 -0
- onnxscript/_internal/param_manipulation.py +148 -0
- onnxscript/_internal/runtime_typing.py +43 -0
- onnxscript/_internal/utils.py +99 -0
- onnxscript/_internal/version_utils.py +118 -0
- onnxscript/_legacy_ir/__init__.py +341 -0
- onnxscript/_legacy_ir/visitor.py +937 -0
- onnxscript/_thirdparty/asciichartpy.py +313 -0
- onnxscript/backend/__init__.py +2 -0
- onnxscript/backend/onnx_backend.py +303 -0
- onnxscript/backend/onnx_export.py +885 -0
- onnxscript/converter.py +1470 -0
- onnxscript/evaluator.py +619 -0
- onnxscript/function_libs/tools/torch_lib/deduce_type_constraints.py +403 -0
- onnxscript/function_libs/tools/torch_lib/generate_aten_signatures.py +333 -0
- onnxscript/function_libs/tools/torch_lib/generate_prims_signatures.py +331 -0
- onnxscript/function_libs/torch_lib/__init__.py +12 -0
- onnxscript/function_libs/torch_lib/_constants.py +5 -0
- onnxscript/function_libs/torch_lib/_flags.py +58 -0
- onnxscript/function_libs/torch_lib/graph_building/__init__.py +56 -0
- onnxscript/function_libs/torch_lib/graph_building/_graph_building_ir.py +723 -0
- onnxscript/function_libs/torch_lib/graph_building/_graph_building_torch.py +1125 -0
- onnxscript/function_libs/torch_lib/ops/__init__.py +27 -0
- onnxscript/function_libs/torch_lib/ops/common.py +80 -0
- onnxscript/function_libs/torch_lib/ops/core.py +8935 -0
- onnxscript/function_libs/torch_lib/ops/fft.py +385 -0
- onnxscript/function_libs/torch_lib/ops/linalg.py +399 -0
- onnxscript/function_libs/torch_lib/ops/nested.py +25 -0
- onnxscript/function_libs/torch_lib/ops/nn.py +2713 -0
- onnxscript/function_libs/torch_lib/ops/prims.py +850 -0
- onnxscript/function_libs/torch_lib/ops/quantized_decomposed.py +63 -0
- onnxscript/function_libs/torch_lib/ops/sparse.py +23 -0
- onnxscript/function_libs/torch_lib/ops/special.py +387 -0
- onnxscript/function_libs/torch_lib/ops/vision.py +25 -0
- onnxscript/function_libs/torch_lib/registration.py +151 -0
- onnxscript/function_libs/torch_lib/tensor_typing.py +74 -0
- onnxscript/ir/__init__.py +153 -0
- onnxscript/ir/_convenience.py +447 -0
- onnxscript/ir/_core.py +3119 -0
- onnxscript/ir/_display.py +49 -0
- onnxscript/ir/_enums.py +163 -0
- onnxscript/ir/_graph_comparison.py +23 -0
- onnxscript/ir/_io.py +97 -0
- onnxscript/ir/_linked_list.py +276 -0
- onnxscript/ir/_metadata.py +44 -0
- onnxscript/ir/_name_authority.py +72 -0
- onnxscript/ir/_polyfill.py +25 -0
- onnxscript/ir/_protocols.py +598 -0
- onnxscript/ir/_schemas.py +548 -0
- onnxscript/ir/_tape.py +120 -0
- onnxscript/ir/_type_casting.py +106 -0
- onnxscript/ir/convenience.py +32 -0
- onnxscript/ir/external_data.py +396 -0
- onnxscript/ir/passes/__init__.py +33 -0
- onnxscript/ir/passes/_pass_infra.py +172 -0
- onnxscript/ir/serde.py +1620 -0
- onnxscript/ir/tensor_adapters.py +122 -0
- onnxscript/ir/traversal.py +82 -0
- onnxscript/irbuilder.py +542 -0
- onnxscript/main.py +167 -0
- onnxscript/onnx_opset/__init__.py +232 -0
- onnxscript/onnx_opset/_impl/opset1.py +4100 -0
- onnxscript/onnx_opset/_impl/opset10.py +1227 -0
- onnxscript/onnx_opset/_impl/opset11.py +4013 -0
- onnxscript/onnx_opset/_impl/opset12.py +1078 -0
- onnxscript/onnx_opset/_impl/opset13.py +3924 -0
- onnxscript/onnx_opset/_impl/opset14.py +999 -0
- onnxscript/onnx_opset/_impl/opset15.py +604 -0
- onnxscript/onnx_opset/_impl/opset16.py +1255 -0
- onnxscript/onnx_opset/_impl/opset17.py +561 -0
- onnxscript/onnx_opset/_impl/opset18.py +1803 -0
- onnxscript/onnx_opset/_impl/opset19.py +1942 -0
- onnxscript/onnx_opset/_impl/opset2.py +218 -0
- onnxscript/onnx_opset/_impl/opset20.py +675 -0
- onnxscript/onnx_opset/_impl/opset21.py +1976 -0
- onnxscript/onnx_opset/_impl/opset22.py +2588 -0
- onnxscript/onnx_opset/_impl/opset3.py +199 -0
- onnxscript/onnx_opset/_impl/opset4.py +77 -0
- onnxscript/onnx_opset/_impl/opset5.py +84 -0
- onnxscript/onnx_opset/_impl/opset6.py +944 -0
- onnxscript/onnx_opset/_impl/opset7.py +1243 -0
- onnxscript/onnx_opset/_impl/opset8.py +444 -0
- onnxscript/onnx_opset/_impl/opset9.py +1485 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml1.py +974 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml2.py +112 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml3.py +308 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml4.py +129 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_ml5.py +158 -0
- onnxscript/onnx_opset/_impl/opset_ai_onnx_preview_training1.py +577 -0
- onnxscript/onnx_types.py +229 -0
- onnxscript/optimizer/__init__.py +39 -0
- onnxscript/optimizer/_constant_folding.py +1083 -0
- onnxscript/optimizer/_inliner.py +312 -0
- onnxscript/optimizer/_legacy/_optimizer.py +98 -0
- onnxscript/optimizer/_legacy/_remove_unused_proto.py +144 -0
- onnxscript/optimizer/_legacy/_simple_function_folding.py +243 -0
- onnxscript/optimizer/_legacy/constant_folding.py +293 -0
- onnxscript/optimizer/_legacy/evaluator.py +439 -0
- onnxscript/optimizer/_optimizer.py +61 -0
- onnxscript/optimizer/_remove_unused.py +106 -0
- onnxscript/optimizer/_remove_unused_function.py +72 -0
- onnxscript/py.typed +1 -0
- onnxscript/rewriter/__init__.py +56 -0
- onnxscript/rewriter/_ir_utils.py +111 -0
- onnxscript/rewriter/broadcast_to_matmul.py +180 -0
- onnxscript/rewriter/cast_constant_of_shape.py +50 -0
- onnxscript/rewriter/collapse_slices.py +141 -0
- onnxscript/rewriter/erfgelu.py +27 -0
- onnxscript/rewriter/function_rule.py +232 -0
- onnxscript/rewriter/gemm_to_matmul_add.py +21 -0
- onnxscript/rewriter/generic_pattern.py +700 -0
- onnxscript/rewriter/llama_rule_sets.py +287 -0
- onnxscript/rewriter/no_op.py +56 -0
- onnxscript/rewriter/onnxruntime/__init__.py +50 -0
- onnxscript/rewriter/onnxruntime/bfloat16_utils/bfloat16_converter.py +99 -0
- onnxscript/rewriter/onnxruntime/fused_matmul_rule_sets.py +177 -0
- onnxscript/rewriter/onnxruntime/group_normalization_merge_silu.py +64 -0
- onnxscript/rewriter/onnxruntime/instance_to_group_normalization.py +155 -0
- onnxscript/rewriter/onnxruntime/softmax.py +63 -0
- onnxscript/rewriter/onnxruntime/transformers/__init__.py +21 -0
- onnxscript/rewriter/onnxruntime/transformers/biassplitgelu.py +31 -0
- onnxscript/rewriter/onnxruntime/transformers/fastgelu.py +29 -0
- onnxscript/rewriter/onnxruntime/transformers/layernorm.py +47 -0
- onnxscript/rewriter/onnxruntime/transformers/multihead_attention.py +715 -0
- onnxscript/rewriter/ort_fusions/__init__.py +9 -0
- onnxscript/rewriter/ort_fusions/_core.py +28 -0
- onnxscript/rewriter/ort_fusions/_smollm_1.py +253 -0
- onnxscript/rewriter/ort_fusions/_smollm_2.py +467 -0
- onnxscript/rewriter/ort_fusions/_test_models.py +122 -0
- onnxscript/rewriter/ort_fusions/_test_utils.py +42 -0
- onnxscript/rewriter/ort_fusions/cos_sin_cache.py +154 -0
- onnxscript/rewriter/ort_fusions/gqa.py +156 -0
- onnxscript/rewriter/ort_fusions/mha.py +198 -0
- onnxscript/rewriter/ort_fusions/rms_normalization.py +95 -0
- onnxscript/rewriter/ort_fusions/rotary_embedding.py +64 -0
- onnxscript/rewriter/ort_fusions/sdpa.py +75 -0
- onnxscript/rewriter/ort_fusions/skip_normalization.py +46 -0
- onnxscript/rewriter/pattern.py +1714 -0
- onnxscript/rewriter/testing.py +77 -0
- onnxscript/sourceinfo.py +59 -0
- onnxscript/tensor.py +227 -0
- onnxscript/testing/__init__.py +482 -0
- onnxscript/tools/__init__.py +4 -0
- onnxscript/tools/benchmark/__init__.py +23 -0
- onnxscript/tools/benchmark/benchmark_helpers.py +783 -0
- onnxscript/tools/benchmark/benchmark_run.py +140 -0
- onnxscript/tools/benchmark/export_model.py +207 -0
- onnxscript/tools/benchmark/export_model_batch.py +146 -0
- onnxscript/tools/memory_peak.py +244 -0
- onnxscript/tools/training_helper.py +50 -0
- onnxscript/tools/transformers_models/__init__.py +190 -0
- onnxscript/tools/transformers_models/llama.py +168 -0
- onnxscript/tools/transformers_models/mistral.py +238 -0
- onnxscript/tools/transformers_models/phi.py +248 -0
- onnxscript/tools/transformers_models/phi3.py +259 -0
- onnxscript/type_annotation.py +281 -0
- onnxscript/utils/__init__.py +0 -0
- onnxscript/utils/evaluation_utils.py +56 -0
- onnxscript/utils/timing_utils.py +33 -0
- onnxscript/utils/utils.py +84 -0
- onnxscript/values.py +790 -0
- onnxscript/version_converter/__init__.py +21 -0
- onnxscript/version_converter/_version_converter.py +314 -0
- onnxscript-0.1.0.dist-info/LICENSE +21 -0
- onnxscript-0.1.0.dist-info/METADATA +370 -0
- onnxscript-0.1.0.dist-info/RECORD +176 -0
- onnxscript-0.1.0.dist-info/WHEEL +5 -0
- onnxscript-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
# Copyright (c) Microsoft Corporation.
|
|
2
|
+
# Licensed under the MIT License.
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"assert_isomorphic",
|
|
7
|
+
"assert_isomorphic_graph",
|
|
8
|
+
"assert_isomorphic_function",
|
|
9
|
+
"assert_onnx_proto_equal",
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
import difflib
|
|
13
|
+
import math
|
|
14
|
+
from typing import Any, Collection, Sequence
|
|
15
|
+
|
|
16
|
+
import google.protobuf.message
|
|
17
|
+
import onnx
|
|
18
|
+
from onnx import parser
|
|
19
|
+
|
|
20
|
+
import onnxscript
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def assert_isomorphic(graph_or_function_1, graph_or_function_2):
|
|
24
|
+
"""Assert two graphs or functions are isomorphic."""
|
|
25
|
+
assert _isomorphic(
|
|
26
|
+
_to_function_or_graph(graph_or_function_1),
|
|
27
|
+
_to_function_or_graph(graph_or_function_2),
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def assert_isomorphic_graph(graph1, graph2):
|
|
32
|
+
"""Assert two graphs are isomorphic."""
|
|
33
|
+
assert _isomorphic(_to_graph_proto(graph1), _to_graph_proto(graph2))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def assert_isomorphic_function(fn1, fn2):
|
|
37
|
+
"""Assert two functions are isomorphic."""
|
|
38
|
+
assert _isomorphic(_to_function_proto(fn1), _to_function_proto(fn2))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _default_equality_op(x, y):
|
|
42
|
+
return x == y
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _same_optional(field, obj1, obj2, equals=_default_equality_op):
|
|
46
|
+
"""Check two proto object have same value for optional field.
|
|
47
|
+
This is restricted to simple field types where == comparison is sufficient.
|
|
48
|
+
"""
|
|
49
|
+
if obj1.HasField(field):
|
|
50
|
+
return obj2.HasField(field) and equals(getattr(obj1, field), getattr(obj2, field))
|
|
51
|
+
return not obj2.HasField(field)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _same_repeated(values1, values2, equals=_default_equality_op):
|
|
55
|
+
if len(values1) != len(values2):
|
|
56
|
+
return False
|
|
57
|
+
return all(equals(val1, val2) for val1, val2 in zip(values1, values2))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _same_string_string_map(proto1, proto2):
|
|
61
|
+
"""Compare repeated StringStringEntryProto as maps."""
|
|
62
|
+
|
|
63
|
+
def to_map(proto):
|
|
64
|
+
return {x.key: x.value for x in proto}
|
|
65
|
+
|
|
66
|
+
return to_map(proto1) == to_map(proto2)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _same_tensor(tp1, tp2):
|
|
70
|
+
if tp1.dims != tp2.dims:
|
|
71
|
+
return False
|
|
72
|
+
if not _same_optional("data_type", tp1, tp2):
|
|
73
|
+
return False
|
|
74
|
+
# Segmented representation not supported yet
|
|
75
|
+
if tp1.HasField("segment") or tp2.HasField("segment"):
|
|
76
|
+
return False
|
|
77
|
+
if tp1.float_data != tp2.float_data:
|
|
78
|
+
return False
|
|
79
|
+
if tp1.int32_data != tp2.int32_data:
|
|
80
|
+
return False
|
|
81
|
+
if tp1.string_data != tp2.string_data:
|
|
82
|
+
return False
|
|
83
|
+
if tp1.int64_data != tp2.int64_data:
|
|
84
|
+
return False
|
|
85
|
+
if tp1.uint64_data != tp2.uint64_data:
|
|
86
|
+
return False
|
|
87
|
+
if tp1.double_data != tp2.double_data:
|
|
88
|
+
return False
|
|
89
|
+
# Ignore name for comparison:
|
|
90
|
+
# if not _same_optional("name", tp1, tp2): return False
|
|
91
|
+
if not _same_optional("doc_string", tp1, tp2):
|
|
92
|
+
return False
|
|
93
|
+
if not _same_optional("data_location", tp1, tp2):
|
|
94
|
+
return False
|
|
95
|
+
if not _same_string_string_map(tp1.external_data, tp2.external_data):
|
|
96
|
+
return False
|
|
97
|
+
return True
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _same_dim(dim1, dim2):
|
|
101
|
+
return _same_optional("dim_value", dim1, dim2) and _same_optional("dim_param", dim1, dim2)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _same_shape(shape1, shape2):
|
|
105
|
+
return _same_repeated(shape1.dim, shape2.dim, _same_dim)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _same_tensor_type(tt1, tt2):
|
|
109
|
+
return (tt1.elem_type == tt2.elem_type) and _same_optional("shape", tt1, tt2, _same_shape)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _same_type(tp1, tp2):
|
|
113
|
+
# Handles only tensor type at this point.
|
|
114
|
+
return _same_optional("tensor_type", tp1, tp2, _same_tensor_type)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _same_value_info(vi1, vi2):
|
|
118
|
+
return (
|
|
119
|
+
_same_optional("name", vi1, vi2)
|
|
120
|
+
and _same_optional("type", vi1, vi2, _same_type)
|
|
121
|
+
and _same_optional("doc_string", vi1, vi2)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _same_attr(attr1, attr2, graph_equality):
|
|
126
|
+
# no name check; names used to match attributes already.
|
|
127
|
+
for field in ["type", "ref_attr_name", "f", "i", "s"]:
|
|
128
|
+
if not _same_optional(field, attr1, attr2):
|
|
129
|
+
return False
|
|
130
|
+
|
|
131
|
+
if not _same_optional("t", attr1, attr2, _same_tensor):
|
|
132
|
+
return False
|
|
133
|
+
|
|
134
|
+
if not _same_repeated(attr1.tensors, attr2.tensors, _same_tensor):
|
|
135
|
+
return False
|
|
136
|
+
|
|
137
|
+
for field in ["floats", "ints", "strings"]:
|
|
138
|
+
if getattr(attr1, field) != getattr(attr2, field):
|
|
139
|
+
return False
|
|
140
|
+
|
|
141
|
+
if not _same_optional("g", attr1, attr2, graph_equality):
|
|
142
|
+
return False
|
|
143
|
+
|
|
144
|
+
if not _same_repeated(attr1.graphs, attr2.graphs, graph_equality):
|
|
145
|
+
return False
|
|
146
|
+
|
|
147
|
+
for field in ["sparse_tensor", "tp"]:
|
|
148
|
+
# TODO(gramalingam): check for more complex fields
|
|
149
|
+
if attr1.HasField(field) or attr2.HasField(field):
|
|
150
|
+
return False
|
|
151
|
+
return True
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _same_attrs(attrs1, attrs2, graph_equality):
|
|
155
|
+
if len(attrs1) != len(attrs2):
|
|
156
|
+
return False
|
|
157
|
+
attrs1map = {a.name: a for a in attrs1}
|
|
158
|
+
for attr2 in attrs2:
|
|
159
|
+
if attr2.name not in attrs1map:
|
|
160
|
+
return False
|
|
161
|
+
attr1 = attrs1map[attr2.name]
|
|
162
|
+
if not _same_attr(attr1, attr2, graph_equality):
|
|
163
|
+
return False
|
|
164
|
+
return True
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _ioname(x):
|
|
168
|
+
"""Return the name of an input/output of a function or graph"""
|
|
169
|
+
return x.name if isinstance(x, onnx.ValueInfoProto) else x
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
class _Matcher:
|
|
173
|
+
"""An isomorphism matcher for two functions or two graphs."""
|
|
174
|
+
|
|
175
|
+
def __init__(self, fg1, fg2, outer_scope) -> None:
|
|
176
|
+
def defmap(f):
|
|
177
|
+
"""Compute a map from variables v to their definition-sites.
|
|
178
|
+
A definition-site (n, i) indicates the i-th output of n-th node
|
|
179
|
+
The special value (-1, i) is used to indicate the i-th input of a function/graph.
|
|
180
|
+
"""
|
|
181
|
+
result = {}
|
|
182
|
+
for i, x in enumerate(f.input):
|
|
183
|
+
result[_ioname(x)] = (-1, i)
|
|
184
|
+
for ni, n in enumerate(f.node):
|
|
185
|
+
for xi, x in enumerate(n.output):
|
|
186
|
+
result[x] = (ni, xi)
|
|
187
|
+
return result
|
|
188
|
+
|
|
189
|
+
self.defmap1 = defmap(fg1)
|
|
190
|
+
self.defmap2 = defmap(fg2)
|
|
191
|
+
self.fg1 = fg1
|
|
192
|
+
self.fg2 = fg2
|
|
193
|
+
self.node_mapping: dict[onnx.NodeProto, onnx.NodeProto] = {}
|
|
194
|
+
self.outer_scope = outer_scope
|
|
195
|
+
|
|
196
|
+
def same_value(self, var1, var2):
|
|
197
|
+
"""Match two variables (strings)."""
|
|
198
|
+
if var1 == "":
|
|
199
|
+
return var2 == ""
|
|
200
|
+
if var2 == "":
|
|
201
|
+
return False
|
|
202
|
+
if var1 not in self.defmap1 or var2 not in self.defmap2:
|
|
203
|
+
# If one of the variables is in current scope, or if there is no outer scope, fail
|
|
204
|
+
if (var1 in self.defmap1) or (var2 in self.defmap2) or (self.outer_scope is None):
|
|
205
|
+
return False
|
|
206
|
+
# Both variables are in outer-scopes. Delay check until later
|
|
207
|
+
return self.outer_scope.same_value(var1, var2)
|
|
208
|
+
(node1, index1) = self.defmap1[var1]
|
|
209
|
+
(node2, index2) = self.defmap2[var2]
|
|
210
|
+
return (index1 == index2) and self.same_node(node1, node2)
|
|
211
|
+
|
|
212
|
+
def same_node(self, n1, n2):
|
|
213
|
+
"""Match two node-indices. The special node-index -1 represents inputs."""
|
|
214
|
+
if (n1 == -1) and (n2 == -1):
|
|
215
|
+
return True # Both are inputs
|
|
216
|
+
if (n1 == -1) or (n2 == -1):
|
|
217
|
+
return False # Only one is input
|
|
218
|
+
if n1 in self.node_mapping:
|
|
219
|
+
return self.node_mapping[n1] == n2
|
|
220
|
+
node1 = self.fg1.node[n1]
|
|
221
|
+
node2 = self.fg2.node[n2]
|
|
222
|
+
if node1.op_type != node2.op_type:
|
|
223
|
+
return False
|
|
224
|
+
if node1.domain != node2.domain:
|
|
225
|
+
return False
|
|
226
|
+
# check attrs
|
|
227
|
+
if not _same_attrs(node1.attribute, node2.attribute, self.same_sub_graph):
|
|
228
|
+
return False
|
|
229
|
+
if not self.same_value_list(node1.input, node2.input):
|
|
230
|
+
return False
|
|
231
|
+
|
|
232
|
+
# Nodes represent same computation. Cache the comparison result.
|
|
233
|
+
self.node_mapping[n1] = n2
|
|
234
|
+
return True
|
|
235
|
+
|
|
236
|
+
def same_value_list(self, list1, list2):
|
|
237
|
+
"""Match two lists of variables (either a string or ValueInfoProto)"""
|
|
238
|
+
if len(list1) != len(list2):
|
|
239
|
+
return False
|
|
240
|
+
return all(self.same_value(_ioname(x), _ioname(y)) for x, y in zip(list1, list2))
|
|
241
|
+
|
|
242
|
+
def same_sub_graph(self, g1, g2):
|
|
243
|
+
"""Match two sub-graphs."""
|
|
244
|
+
sub_graph_matcher = _Matcher(g1, g2, self)
|
|
245
|
+
return sub_graph_matcher.same_graph()
|
|
246
|
+
|
|
247
|
+
def same_graph(self):
|
|
248
|
+
"""Match two sub-graphs."""
|
|
249
|
+
g1 = self.fg1
|
|
250
|
+
g2 = self.fg2
|
|
251
|
+
if not _same_repeated(g1.input, g2.input, _same_value_info):
|
|
252
|
+
return False
|
|
253
|
+
|
|
254
|
+
if g1.initializer or g2.initializer:
|
|
255
|
+
return False # TODO
|
|
256
|
+
if g1.sparse_initializer or g2.sparse_initializer:
|
|
257
|
+
return False # TODO
|
|
258
|
+
if not self.same_value_list(g1.output, g2.output):
|
|
259
|
+
return False
|
|
260
|
+
# TODO completeness tests!
|
|
261
|
+
return True
|
|
262
|
+
|
|
263
|
+
def same_function(self):
|
|
264
|
+
"""Match (top-level) two functions."""
|
|
265
|
+
|
|
266
|
+
# Ok for function names/domain to be different.
|
|
267
|
+
|
|
268
|
+
if len(self.fg1.input) != len(self.fg2.input):
|
|
269
|
+
return False
|
|
270
|
+
if set(self.fg1.attribute) != set(self.fg2.attribute):
|
|
271
|
+
return False
|
|
272
|
+
|
|
273
|
+
# Opset imports must be same (but possibly in different order):
|
|
274
|
+
# Convert opset-imports into a dictionary
|
|
275
|
+
def imports(f):
|
|
276
|
+
# Assumes each domain has only one entry in a valid FunctionProto
|
|
277
|
+
return {entry.domain: entry.version for entry in f.opset_import}
|
|
278
|
+
|
|
279
|
+
if imports(self.fg1) != imports(self.fg2):
|
|
280
|
+
return False
|
|
281
|
+
|
|
282
|
+
# Now do a specific form of isomorphism check: Both must compute the same
|
|
283
|
+
# set of operations, possibly in different order as long as they respect
|
|
284
|
+
# the topological-sort order requirement. The two may use different names
|
|
285
|
+
# for intermediate-values, as long as the computation is the same.
|
|
286
|
+
|
|
287
|
+
if len(self.fg1.node) != len(self.fg2.node):
|
|
288
|
+
return False
|
|
289
|
+
|
|
290
|
+
if not self.same_value_list(self.fg1.output, self.fg2.output):
|
|
291
|
+
return False
|
|
292
|
+
|
|
293
|
+
# We do not allow for unused values in the function, which are
|
|
294
|
+
# hard to handle in an isomorphism check.
|
|
295
|
+
if len(self.node_mapping) != len(self.fg1.node):
|
|
296
|
+
return False
|
|
297
|
+
if len(set(self.node_mapping.values())) != len(self.fg2.node):
|
|
298
|
+
return False
|
|
299
|
+
|
|
300
|
+
return True
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def _isomorphic(fg1, fg2):
|
|
304
|
+
"""Checks that two function/graph bodies are isomorphic.
|
|
305
|
+
Assumes that the inputs are valid FunctionProto/GraphProto.
|
|
306
|
+
Use a separate check to verify that the inputs satisfy
|
|
307
|
+
FunctionProto/GraphProto requirements (like no duplicate attributes).
|
|
308
|
+
"""
|
|
309
|
+
matcher = _Matcher(fg1, fg2, None)
|
|
310
|
+
if isinstance(fg1, onnx.FunctionProto):
|
|
311
|
+
if not isinstance(fg2, onnx.FunctionProto):
|
|
312
|
+
raise TypeError("Both inputs must be same type (function or graph)")
|
|
313
|
+
return matcher.same_function()
|
|
314
|
+
if isinstance(fg1, onnx.GraphProto):
|
|
315
|
+
if not isinstance(fg2, onnx.GraphProto):
|
|
316
|
+
raise TypeError("Both inputs must be same type (function or graph)")
|
|
317
|
+
return matcher.same_graph()
|
|
318
|
+
raise TypeError("Inputs must be either a FunctionProto or GraphProto")
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def _to_function_proto(f):
|
|
322
|
+
if isinstance(f, onnx.FunctionProto):
|
|
323
|
+
return f
|
|
324
|
+
if isinstance(f, onnxscript.OnnxFunction):
|
|
325
|
+
return f.to_function_proto()
|
|
326
|
+
if isinstance(f, str):
|
|
327
|
+
return parser.parse_function(f)
|
|
328
|
+
raise TypeError(f"Cannot convert {type(f)} to FunctionProto")
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def _to_graph_proto(g):
|
|
332
|
+
if isinstance(g, onnx.GraphProto):
|
|
333
|
+
return g
|
|
334
|
+
if isinstance(g, onnxscript.OnnxFunction):
|
|
335
|
+
return g.to_model_proto().graph
|
|
336
|
+
if isinstance(g, str):
|
|
337
|
+
return parser.parse_graph(g)
|
|
338
|
+
raise TypeError(f"Cannot convert {type(g)} to ModelProto")
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def _to_function_or_graph(obj):
|
|
342
|
+
if isinstance(obj, onnx.FunctionProto):
|
|
343
|
+
return obj
|
|
344
|
+
if isinstance(obj, onnx.GraphProto):
|
|
345
|
+
return obj
|
|
346
|
+
if isinstance(obj, onnx.ModelProto):
|
|
347
|
+
return obj.graph
|
|
348
|
+
if isinstance(obj, onnxscript.OnnxFunction):
|
|
349
|
+
return obj.to_function_proto()
|
|
350
|
+
raise TypeError(f"Cannot convert {type(obj)} to FunctionProto or GraphProto")
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _opset_import_key(opset_import: onnx.OperatorSetIdProto) -> tuple[str, int]:
|
|
354
|
+
return (opset_import.domain, opset_import.version)
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def _value_info_key(value_info: onnx.ValueInfoProto) -> str:
|
|
358
|
+
return value_info.name
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def _function_key(function: onnx.FunctionProto) -> tuple[str, str, str]:
|
|
362
|
+
return (function.domain, function.name, getattr(function, "overload", ""))
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _find_duplicates(with_duplicates: Collection[Any]) -> list[Any]:
|
|
366
|
+
"""Return a list of duplicated elements in a collection."""
|
|
367
|
+
seen = set()
|
|
368
|
+
duplicates = []
|
|
369
|
+
for x in with_duplicates:
|
|
370
|
+
if x in seen:
|
|
371
|
+
duplicates.append(x)
|
|
372
|
+
seen.add(x)
|
|
373
|
+
return duplicates
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def assert_onnx_proto_equal(
|
|
377
|
+
a: google.protobuf.message.Message | Any, b: google.protobuf.message.Message | Any
|
|
378
|
+
) -> None:
|
|
379
|
+
"""Assert that two ONNX protos are equal.
|
|
380
|
+
|
|
381
|
+
Equality is defined as having the same fields with the same values. When
|
|
382
|
+
a field takes the default value, it is considered equal to the field
|
|
383
|
+
not being set.
|
|
384
|
+
|
|
385
|
+
Sequential fields with name `opset_import`, `value_info`, and `functions` are
|
|
386
|
+
compared disregarding the order of their elements.
|
|
387
|
+
|
|
388
|
+
Args:
|
|
389
|
+
a: The first ONNX proto.
|
|
390
|
+
b: The second ONNX proto.
|
|
391
|
+
"""
|
|
392
|
+
assert type(a) is type(b), f"Type not equal: {type(a)} != {type(b)}"
|
|
393
|
+
|
|
394
|
+
a_fields = {field.name: value for field, value in a.ListFields()}
|
|
395
|
+
b_fields = {field.name: value for field, value in b.ListFields()}
|
|
396
|
+
all_fields = sorted(set(a_fields.keys()) | set(b_fields.keys()))
|
|
397
|
+
for field in all_fields:
|
|
398
|
+
# Obtain the default value if the field is not set. This way we can compare the two fields.
|
|
399
|
+
a_value = getattr(a, field)
|
|
400
|
+
b_value = getattr(b, field)
|
|
401
|
+
if (
|
|
402
|
+
isinstance(a_value, Sequence)
|
|
403
|
+
and isinstance(b_value, Sequence)
|
|
404
|
+
and not isinstance(a_value, (str, bytes))
|
|
405
|
+
and not isinstance(b_value, (str, bytes))
|
|
406
|
+
):
|
|
407
|
+
# Check length first
|
|
408
|
+
a_keys: list[Any] = []
|
|
409
|
+
b_keys: list[Any] = []
|
|
410
|
+
if field == "opset_import":
|
|
411
|
+
a_value = sorted(a_value, key=_opset_import_key)
|
|
412
|
+
b_value = sorted(b_value, key=_opset_import_key)
|
|
413
|
+
a_keys = [_opset_import_key(opset_import) for opset_import in a_value]
|
|
414
|
+
b_keys = [_opset_import_key(opset_import) for opset_import in b_value]
|
|
415
|
+
elif field == "value_info":
|
|
416
|
+
a_value = sorted(a_value, key=_value_info_key)
|
|
417
|
+
b_value = sorted(b_value, key=_value_info_key)
|
|
418
|
+
a_keys = [_value_info_key(value_info) for value_info in a_value]
|
|
419
|
+
b_keys = [_value_info_key(value_info) for value_info in b_value]
|
|
420
|
+
elif field == "functions":
|
|
421
|
+
a_value = sorted(a_value, key=_function_key)
|
|
422
|
+
b_value = sorted(b_value, key=_function_key)
|
|
423
|
+
a_keys = [_function_key(functions) for functions in a_value]
|
|
424
|
+
b_keys = [_function_key(functions) for functions in b_value]
|
|
425
|
+
|
|
426
|
+
if a_keys != b_keys:
|
|
427
|
+
keys_only_in_a = set(a_keys) - set(b_keys)
|
|
428
|
+
keys_only_in_b = set(b_keys) - set(a_keys)
|
|
429
|
+
error_message = (
|
|
430
|
+
f"Field {field} not equal: keys_only_in_a={keys_only_in_a}, keys_only_in_b={keys_only_in_b}. "
|
|
431
|
+
f"Field type: {type(a_value)}. "
|
|
432
|
+
f"Duplicated a_keys: {_find_duplicates(a_keys)}, duplicated b_keys: {_find_duplicates(b_keys)}"
|
|
433
|
+
)
|
|
434
|
+
raise AssertionError(error_message)
|
|
435
|
+
if len(a_value) != len(b_value):
|
|
436
|
+
error_message = (
|
|
437
|
+
f"Field {field} not equal: len(a)={len(a_value)}, len(b)={len(b_value)} "
|
|
438
|
+
f"Field type: {type(a_value)}"
|
|
439
|
+
)
|
|
440
|
+
raise AssertionError(error_message)
|
|
441
|
+
# Check every element
|
|
442
|
+
for i in range(len(a_value)): # pylint: disable=consider-using-enumerate
|
|
443
|
+
a_value_i = a_value[i]
|
|
444
|
+
b_value_i = b_value[i]
|
|
445
|
+
if isinstance(a_value_i, google.protobuf.message.Message) and isinstance(
|
|
446
|
+
b_value_i, google.protobuf.message.Message
|
|
447
|
+
):
|
|
448
|
+
try:
|
|
449
|
+
assert_onnx_proto_equal(a_value_i, b_value_i)
|
|
450
|
+
except AssertionError as e:
|
|
451
|
+
error_message = f"Field {field} index {i} in sequence not equal. type(a_value_i): {type(a_value_i)}, type(b_value_i): {type(b_value_i)}, a_value_i: {a_value_i}, b_value_i: {b_value_i}"
|
|
452
|
+
raise AssertionError(error_message) from e
|
|
453
|
+
elif a_value_i != b_value_i:
|
|
454
|
+
if (
|
|
455
|
+
isinstance(a_value_i, float)
|
|
456
|
+
and isinstance(b_value_i, float)
|
|
457
|
+
and math.isnan(a_value_i)
|
|
458
|
+
and math.isnan(b_value_i)
|
|
459
|
+
):
|
|
460
|
+
# Consider NaNs equal
|
|
461
|
+
continue
|
|
462
|
+
error_message = f"Field {field} index {i} in sequence not equal. type(a_value_i): {type(a_value_i)}, type(b_value_i): {type(b_value_i)}"
|
|
463
|
+
for line in difflib.ndiff(
|
|
464
|
+
str(a_value_i).splitlines(), str(b_value_i).splitlines()
|
|
465
|
+
):
|
|
466
|
+
error_message += "\n" + line
|
|
467
|
+
raise AssertionError(error_message)
|
|
468
|
+
elif isinstance(a_value, google.protobuf.message.Message) and isinstance(
|
|
469
|
+
b_value, google.protobuf.message.Message
|
|
470
|
+
):
|
|
471
|
+
assert_onnx_proto_equal(a_value, b_value)
|
|
472
|
+
elif a_value != b_value:
|
|
473
|
+
if (
|
|
474
|
+
isinstance(a_value, float)
|
|
475
|
+
and isinstance(b_value, float)
|
|
476
|
+
and math.isnan(a_value)
|
|
477
|
+
and math.isnan(b_value)
|
|
478
|
+
):
|
|
479
|
+
# Consider NaNs equal
|
|
480
|
+
continue
|
|
481
|
+
error_message = f"Field {field} not equal. field_a: {a_value}, field_b: {b_value}"
|
|
482
|
+
raise AssertionError(error_message)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# -------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# Licensed under the MIT License.
|
|
4
|
+
# --------------------------------------------------------------------------
|
|
5
|
+
from onnxscript.tools.benchmark.benchmark_helpers import (
|
|
6
|
+
common_export,
|
|
7
|
+
get_parsed_args,
|
|
8
|
+
make_configs,
|
|
9
|
+
make_dataframe_from_benchmark_data,
|
|
10
|
+
multi_run,
|
|
11
|
+
run_inference,
|
|
12
|
+
run_onnx_inference,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"get_parsed_args",
|
|
17
|
+
"common_export",
|
|
18
|
+
"make_configs",
|
|
19
|
+
"multi_run",
|
|
20
|
+
"make_dataframe_from_benchmark_data",
|
|
21
|
+
"run_inference",
|
|
22
|
+
"run_onnx_inference",
|
|
23
|
+
]
|