-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy path_version_converter_test.py
More file actions
330 lines (294 loc) · 15.1 KB
/
_version_converter_test.py
File metadata and controls
330 lines (294 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
from __future__ import annotations
import unittest
import onnx.defs
import onnx.parser
from onnxscript import ir, version_converter
class AdapterCoverageTest(unittest.TestCase):
def get_all_unique_schema_versions(self) -> dict[str, list]:
"""Collect all unique versions of ONNX standard domain ops"""
op_version_dict = {}
all_schemas = onnx.defs.get_all_schemas_with_history()
for schema in all_schemas:
if schema.name not in op_version_dict:
op_version_dict[schema.name] = [schema.since_version]
else:
if schema.since_version not in op_version_dict[schema.name]:
op_version_dict[schema.name].append(schema.since_version)
return op_version_dict
# TODO(shubhambhokare1) : Using existing onnx testing suite to verify operator adapter's functionality
def test_upstream_coverage(self):
op_version_dict = self.get_all_unique_schema_versions()
op_upgrades = []
for op_type in op_version_dict: # pylint: disable=consider-using-dict-items
for opset_version in op_version_dict[op_type]:
op_upgrades.append((op_type, opset_version))
adapter_list = version_converter._version_converter.registry.op_adapters # pylint: disable=protected-access
for adapter_sig in adapter_list:
adapter_info = list(adapter_sig)
domain, name, upgrade_version = (
adapter_info[0],
adapter_info[1],
adapter_info[2] + 1,
)
self.assertEqual(domain, "")
self.assertIn((name, upgrade_version), op_upgrades)
def test_version_convert_non_standard_onnx_domain(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 7, opset_import: [ "local" : 1]>
agraph (float[4, 512, 512] input_x, float[4, 1024, 1024] input_y) => (float[4, 1024, 1024] output)
{
shape_a = Constant<value: tensor = int64[5] {1, 4, 512, 512}>()
reshape_x = Reshape (input_x, shape_a)
shape_b = Constant<value: tensor = int64[5] {1, 4, 1024, 1024}>()
reshape_y = Reshape (input_x, shape_b)
gridsample = GridSample <mode = "bilinear"> (reshape_x, reshape_y)
shape_c = Constant<value: tensor = int64[4] {4, 1024, 1024}>()
output = Reshape (gridsample, shape_c)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
self.assertEqual(model.graph.node(4).op_type, "GridSample")
self.assertEqual(model.graph.node(4).attributes["mode"].value, "bilinear")
target_version = 20
version_converter.convert_version(model, target_version=target_version)
self.assertEqual(model.graph.node(0).op_type, "Constant")
self.assertEqual(model.graph.node(0).version, None)
self.assertEqual(model.graph.node(1).op_type, "Reshape")
self.assertEqual(model.graph.node(1).version, None)
self.assertEqual(model.graph.node(4).op_type, "GridSample")
self.assertEqual(model.graph.node(4).version, None)
self.assertEqual(model.graph.node(4).attributes["mode"].value, "bilinear")
class VersionConverter18to17Test(unittest.TestCase):
def test_version_convert_compatible(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 7, opset_import: [ "" : 18]>
agraph (float[1, 4, 512, 512] input_x, float[1, 4, 512, 64] input_y) => (float[1, 4, 512, 64] output)
{
shape_a = Constant<value: tensor = int64[3] {4, 512, 512}>()
reshape_x = Reshape (input_x, shape_a)
shape_b = Constant<value: tensor = int64[3] {4, 512, 64}>()
reshape_y = Reshape (input_y, shape_b)
matmul = MatMul (reshape_x, reshape_y)
shape_c = Constant<value: tensor = int64[4] {1, 4, 512, 64}>()
output = Reshape (matmul, shape_c)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
target_version = 17
version_converter.convert_version(model, target_version=target_version)
class VersionConverter18to19Test(unittest.TestCase):
def test_version_convert_compatible(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 7, opset_import: [ "" : 18]>
agraph (float[1, 4, 512, 512] input_x, float[1, 4, 512, 64] input_y) => (float[1, 4, 512, 64] output)
{
shape_a = Constant<value: tensor = int64[3] {4, 512, 512}>()
reshape_x = Reshape (input_x, shape_a)
shape_b = Constant<value: tensor = int64[3] {4, 512, 64}>()
reshape_y = Reshape (input_y, shape_b)
matmul = MatMul (reshape_x, reshape_y)
shape_c = Constant<value: tensor = int64[4] {1, 4, 512, 64}>()
output = Reshape (matmul, shape_c)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
target_version = 19
version_converter.convert_version(model, target_version=target_version)
self.assertEqual(model.graph.node(0).op_type, "Constant")
self.assertEqual(model.graph.node(0).version, 19)
self.assertEqual(model.graph.node(1).op_type, "Reshape")
self.assertEqual(model.graph.node(1).version, 19)
self.assertEqual(model.graph.node(4).op_type, "MatMul")
self.assertEqual(model.graph.node(4).version, 19)
class VersionConverter19to20Test(unittest.TestCase):
def test_version_convert_compatible(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 7, opset_import: [ "" : 18]>
agraph (float[4, 512, 512] input_x) => (float[4, 257, 64, 2] output)
{
shape_a = Constant<value: tensor = int64[5] {1, 4, 512, 512, 1}>()
reshape_x = Reshape (input_x, shape_a)
dft = DFT <axis = 2, onesided = 1> (reshape_x)
shape_c = Constant<value: tensor = int64[4] {4, 257, 64, 2}>()
output = Reshape (dft, shape_c)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
target_version = 20
version_converter.convert_version(model, target_version=target_version)
self.assertEqual(model.graph.node(0).op_type, "Constant")
self.assertEqual(model.graph.node(0).version, 20)
self.assertEqual(model.graph.node(1).op_type, "Reshape")
self.assertEqual(model.graph.node(1).version, 20)
self.assertEqual(model.graph.node(2).op_type, "Constant")
self.assertEqual(model.graph.node(3).version, 20)
self.assertEqual(model.graph.node(3).op_type, "DFT")
self.assertEqual(model.graph.node(3).version, 20)
self.assertEqual(len(model.graph.node(3).inputs), 2)
def test_version_convert_gridsample_linear(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 7, opset_import: [ "" : 18]>
agraph (float[4, 512, 512] input_x, float[4, 1024, 1024] input_y) => (float[4, 1024, 1024] output)
{
shape_a = Constant<value: tensor = int64[5] {1, 4, 512, 512}>()
reshape_x = Reshape (input_x, shape_a)
shape_b = Constant<value: tensor = int64[5] {1, 4, 1024, 1024}>()
reshape_y = Reshape (input_x, shape_b)
gridsample = GridSample <mode = "bilinear"> (reshape_x, reshape_y)
shape_c = Constant<value: tensor = int64[4] {4, 1024, 1024}>()
output = Reshape (gridsample, shape_c)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
self.assertEqual(model.graph.node(4).op_type, "GridSample")
self.assertEqual(model.graph.node(4).attributes["mode"].value, "bilinear")
target_version = 20
version_converter.convert_version(model, target_version=target_version)
self.assertEqual(model.graph.node(0).op_type, "Constant")
self.assertEqual(model.graph.node(0).version, 20)
self.assertEqual(model.graph.node(1).op_type, "Reshape")
self.assertEqual(model.graph.node(1).version, 20)
self.assertEqual(model.graph.node(4).op_type, "GridSample")
self.assertEqual(model.graph.node(4).version, 20)
self.assertEqual(model.graph.node(4).attributes["mode"].value, "linear")
def test_version_convert_gridsample_cubic(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 7, opset_import: [ "" : 18]>
agraph (float[4, 512, 512] input_x, float[4, 1024, 1024] input_y) => (float[4, 1024, 1024] output)
{
shape_a = Constant<value: tensor = int64[5] {1, 4, 512, 512}>()
reshape_x = Reshape (input_x, shape_a)
shape_b = Constant<value: tensor = int64[5] {1, 4, 1024, 1024}>()
reshape_y = Reshape (input_x, shape_b)
gridsample = GridSample <mode = "bicubic"> (reshape_x, reshape_y)
shape_c = Constant<value: tensor = int64[4] {4, 1024, 1024}>()
output = Reshape (gridsample, shape_c)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
self.assertEqual(model.graph.node(4).op_type, "GridSample")
self.assertEqual(model.graph.node(4).attributes["mode"].value, "bicubic")
target_version = 20
version_converter.convert_version(model, target_version=target_version)
self.assertEqual(model.graph.node(0).op_type, "Constant")
self.assertEqual(model.graph.node(0).version, 20)
self.assertEqual(model.graph.node(1).op_type, "Reshape")
self.assertEqual(model.graph.node(1).version, 20)
self.assertEqual(model.graph.node(4).op_type, "GridSample")
self.assertEqual(model.graph.node(4).version, 20)
self.assertEqual(model.graph.node(4).attributes["mode"].value, "cubic")
def test_version_convert_inline(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 8, opset_import: [ "" : 18]>
agraph (float[4, 512, 512] input_x, float[4, 1024, 1024] input_y) => (float[4, 257, 64, 2] output)
{
shape_a = Constant<value: tensor = int64[5] {1, 4, 512, 512}>()
reshape_x = Reshape (input_x, shape_a)
shape_b = Constant<value: tensor = int64[5] {1, 4, 1024, 1024}>()
reshape_y = Reshape (input_x, shape_b)
gridsample = GridSample <mode = "bilinear"> (reshape_x, reshape_y)
output = foo(gridsample)
}
<opset_import: [ "" : 18]>
foo (x) => (dft) {
dft = DFT <axis = 2, onesided = 1> (x)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
target_version = 20
version_converter.convert_version(model, target_version=target_version)
self.assertEqual(model.graph.node(0).op_type, "Constant")
self.assertEqual(model.graph.node(0).version, 20)
self.assertEqual(model.graph.node(1).op_type, "Reshape")
self.assertEqual(model.graph.node(1).version, 20)
self.assertEqual(model.graph.node(4).op_type, "GridSample")
self.assertEqual(model.graph.node(4).version, 20)
self.assertEqual(model.graph.node(4).attributes["mode"].value, "linear")
self.assertEqual(model.graph.node(6).op_type, "DFT")
self.assertEqual(model.graph.node(6).version, 20)
self.assertEqual(len(model.graph.node(6).inputs), 2)
class VersionConverter20to21Test(unittest.TestCase):
def test_version_groupnorm(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 7, opset_import: [ "" : 18]>
agraph (float[1, 4, 512, 512] input_x, float[2] scale, float[2] bias) => (float[4, 512, 512] output)
{
groupnorm = GroupNormalization <num_groups = 2> (input_x, scale, bias)
shape_c = Constant<value: tensor = int64[4] {4, 512, 512}>()
output = Reshape (groupnorm, shape_c)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
target_version = 21
version_converter.convert_version(model, target_version=target_version)
self.assertEqual(model.graph.node(3).op_type, "Reshape")
self.assertEqual(model.graph.node(3).version, 21)
self.assertEqual(model.graph.node(4).op_type, "Expand")
self.assertEqual(model.graph.node(4).version, 21)
self.assertEqual(model.graph.node(5).op_type, "Reshape")
self.assertEqual(model.graph.node(5).version, 21)
self.assertEqual(model.graph.node(6).op_type, "Reshape")
self.assertEqual(model.graph.node(6).version, 21)
self.assertEqual(model.graph.node(7).op_type, "Expand")
self.assertEqual(model.graph.node(7).version, 21)
self.assertEqual(model.graph.node(8).op_type, "Reshape")
self.assertEqual(model.graph.node(8).version, 21)
self.assertEqual(model.graph.node(9).op_type, "GroupNormalization")
self.assertEqual(model.graph.node(9).version, 21)
def test_version_groupnorm_no_bias(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 7, opset_import: [ "" : 18]>
agraph (float[1, 4, 512, 512] input_x, float[2] scale) => (float[4, 512, 512] output)
{
groupnorm = GroupNormalization <num_groups = 2> (input_x, scale)
shape_c = Constant<value: tensor = int64[4] {4, 512, 512}>()
output = Reshape (groupnorm, shape_c)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
target_version = 21
version_converter.convert_version(model, target_version=target_version)
self.assertEqual(model.graph.node(0).op_type, "GroupNormalization")
self.assertEqual(model.graph.node(0).version, 20)
class VersionConverter23to24Test(unittest.TestCase):
def test_version_convert_compatible(self):
model_proto = onnx.parser.parse_model(
"""
<ir_version: 7, opset_import: [ "" : 23]>
agraph (float[1, 4, 512, 512] input_x, float[1, 4, 512, 64] input_y) => (float[1, 4, 512, 64] output)
{
shape_a = Constant<value: tensor = int64[3] {4, 512, 512}>()
reshape_x = Reshape (input_x, shape_a)
shape_b = Constant<value: tensor = int64[3] {4, 512, 64}>()
reshape_y = Reshape (input_y, shape_b)
matmul = MatMul (reshape_x, reshape_y)
shape_c = Constant<value: tensor = int64[4] {1, 4, 512, 64}>()
output = Reshape (matmul, shape_c)
}
"""
)
model = ir.serde.deserialize_model(model_proto)
target_version = 24
version_converter.convert_version(model, target_version=target_version)
if __name__ == "__main__":
unittest.main()