forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_onnx_opset.py
654 lines (591 loc) · 21.2 KB
/
test_onnx_opset.py
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# Owner(s): ["module: onnx"]
import io
import itertools
import onnx
import pytorch_test_common
import torch
import torch.onnx
from torch.nn import Module
from torch.onnx import producer_name, producer_version
from torch.onnx._globals import GLOBALS
from torch.testing._internal import common_utils
def check_onnx_opset_operator(
model, ops, opset_version=GLOBALS.export_onnx_opset_version
):
# check_onnx_components
assert (
model.producer_name == producer_name
and model.producer_version == producer_version
and model.opset_import[0].version == opset_version
)
# check the schema with the onnx checker
onnx.checker.check_model(model)
# check target type and attributes
graph = model.graph
# ops should contain an object for each node
# in graph.node, in the right order.
# At least the op_name should be specified,
# but the op's attributes can optionally be
# specified as well
assert len(ops) == len(graph.node)
for i in range(0, len(ops)):
assert graph.node[i].op_type == ops[i]["op_name"]
if "attributes" in ops[i]:
attributes = ops[i]["attributes"]
assert len(attributes) == len(graph.node[i].attribute)
for j in range(0, len(attributes)):
for attribute_field in attributes[j].keys():
assert attributes[j][attribute_field] == getattr(
graph.node[i].attribute[j], attribute_field
)
def check_onnx_opsets_operator(
module,
x,
ops,
opset_versions,
training=torch.onnx.TrainingMode.EVAL,
input_names=None,
dynamic_axes=None,
):
for opset_version in opset_versions:
f = io.BytesIO()
torch.onnx.export(
module,
x,
f,
opset_version=opset_version,
training=training,
input_names=input_names,
dynamic_axes=dynamic_axes,
)
model = onnx.load(io.BytesIO(f.getvalue()))
check_onnx_opset_operator(model, ops[opset_version], opset_version)
class TestONNXOpset(pytorch_test_common.ExportTestCase):
def test_opset_fallback(self):
class MyModule(Module):
def forward(self, x):
return torch.isnan(x)
ops = [{"op_name": "IsNaN"}]
ops = {9: ops, 10: ops}
x = torch.tensor([1.0, float("nan"), 2.0])
check_onnx_opsets_operator(MyModule(), x, ops, opset_versions=[9, 10])
def test_topk(self):
class MyModule(Module):
def forward(self, x):
return torch.topk(x, 3)
ops_9 = [
{
"op_name": "TopK",
"attributes": [
{"name": "axis", "i": -1, "type": 2},
{"name": "k", "i": 3, "type": 2},
],
}
]
ops_10 = [
{"op_name": "Constant"},
{"op_name": "TopK", "attributes": [{"name": "axis", "i": -1, "type": 2}]},
]
ops = {9: ops_9, 10: ops_10}
x = torch.arange(1.0, 6.0, requires_grad=True)
check_onnx_opsets_operator(MyModule(), x, ops, opset_versions=[9, 10])
# test with dynamic k
class MyModuleDynamic(torch.jit.ScriptModule):
@torch.jit.script_method
def forward(self, input, k):
return torch.topk(input, k)
ops_10 = [
{"op_name": "Constant", "attributes": [{"name": "value", "type": 4}]},
{"op_name": "Reshape"},
{"op_name": "TopK", "attributes": [{"name": "axis", "i": -1, "type": 2}]},
]
ops = {10: ops_10}
x = torch.arange(1.0, 6.0, requires_grad=True)
k = torch.tensor(3)
module = MyModuleDynamic()
check_onnx_opsets_operator(module, (x, k), ops, opset_versions=[10])
def test_maxpool(self):
module = torch.nn.MaxPool1d(2, stride=1)
ops_9 = [
{
"op_name": "MaxPool",
"attributes": [
{"name": "kernel_shape", "ints": [2], "type": 7},
{"name": "pads", "ints": [0, 0], "type": 7},
{"name": "strides", "ints": [1], "type": 7},
],
}
]
ops_10 = [
{
"op_name": "MaxPool",
"attributes": [
{"name": "ceil_mode", "i": 0, "type": 2},
{"name": "dilations", "ints": [1], "type": 7},
{"name": "kernel_shape", "ints": [2], "type": 7},
{"name": "pads", "ints": [0, 0], "type": 7},
{"name": "strides", "ints": [1], "type": 7},
],
}
]
ops = {9: ops_9, 10: ops_10}
x = torch.randn(20, 16, 50)
check_onnx_opsets_operator(module, x, ops, opset_versions=[9, 10])
# add test with dilations
module = torch.nn.MaxPool1d(2, stride=1, dilation=2)
ops_10 = [
{
"op_name": "MaxPool",
"attributes": [
{"name": "ceil_mode", "i": 0, "type": 2},
{"name": "dilations", "ints": [2], "type": 7},
{"name": "kernel_shape", "ints": [2], "type": 7},
{"name": "pads", "ints": [0, 0], "type": 7},
{"name": "strides", "ints": [1], "type": 7},
],
}
]
ops = {10: ops_10}
x = torch.randn(20, 16, 50)
check_onnx_opsets_operator(module, x, ops, opset_versions=[10])
def test_upsample(self):
class MyModule(Module):
def forward(self, x):
size = [v * 2 for v in x.size()[2:]]
size = [int(i) for i in size]
return torch.nn.functional.interpolate(x, size=size, mode="nearest")
module = MyModule()
ops8 = [
{
"op_name": "Upsample",
"attributes": [
{"name": "mode", "s": (b"nearest"), "type": 3},
{"name": "scales", "floats": [1.0, 1.0, 2.0, 2.0], "type": 6},
],
}
]
ops9 = [
{"op_name": "Constant"},
{
"op_name": "Upsample",
"attributes": [{"name": "mode", "s": (b"nearest"), "type": 3}],
},
]
ops = {8: ops8, 9: ops9}
x = torch.randn(2, 2, 2, 2)
check_onnx_opsets_operator(module, x, ops, opset_versions=[8, 9])
def test_cast_constant(self):
class MyModule(Module):
def forward(self, x):
return x - 1
module = MyModule()
ops_8 = [
{"op_name": "Constant"},
{"op_name": "Cast", "attributes": [{"name": "to", "i": 7, "type": 2}]},
{"op_name": "Sub"},
]
ops_9 = [{"op_name": "Constant"}, {"op_name": "Sub"}]
ops = {8: ops_8, 9: ops_9}
x = torch.ones(5, 6, dtype=torch.long)
check_onnx_opsets_operator(module, x, ops, opset_versions=[8, 9])
def test_slice(self):
class MyModule(Module):
def forward(self, x):
return x[0:1]
ops_9 = [
{
"op_name": "Slice",
"attributes": [
{"name": "axes", "ints": [0], "type": 7},
{"name": "ends", "ints": [1], "type": 7},
{"name": "starts", "ints": [0], "type": 7},
],
}
]
ops_10 = [
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Slice", "attributes": []},
]
ops = {9: ops_9, 10: ops_10}
x = torch.randn(3)
check_onnx_opsets_operator(MyModule(), x, ops, opset_versions=[9, 10])
class DynamicSliceModel(torch.jit.ScriptModule):
@torch.jit.script_method
def forward(self, x):
return x[1 : x.size(0)]
module = DynamicSliceModel()
x = torch.rand(1, 2)
ops_10 = [
{"op_name": "Shape"},
{"op_name": "Constant"},
{"op_name": "Gather", "attributes": [{"name": "axis", "i": 0, "type": 2}]},
{"op_name": "Constant"},
{"op_name": "Constant"},
{
"op_name": "Unsqueeze",
"attributes": [{"name": "axes", "i": 0, "type": 7}],
},
{"op_name": "Constant"},
{"op_name": "Slice", "attributes": []},
]
ops = {10: ops_10}
check_onnx_opsets_operator(
module,
x,
ops,
opset_versions=[10],
input_names=["x"],
dynamic_axes={"x": [0, 1]},
)
ops_10 = [
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Slice", "attributes": []},
]
ops = {10: ops_10}
check_onnx_opsets_operator(module, x, ops, opset_versions=[10])
def test_flip(self):
class MyModule(Module):
def forward(self, x):
return torch.flip(x, dims=[0])
ops_10 = [
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Slice", "attributes": []},
]
ops = {10: ops_10}
import numpy
x = torch.tensor(numpy.arange(6.0).reshape(2, 3))
check_onnx_opsets_operator(MyModule(), x, ops, opset_versions=[10])
def test_dropout(self):
class MyModule(Module):
def __init__(self) -> None:
super().__init__()
self.dropout = torch.nn.Dropout(0.5)
def forward(self, x):
return self.dropout(x)
x = torch.randn(1, 2, 3)
# we should only export the onnx Dropout op in training mode; test both modes
# test training mode
ops = [
{
"op_name": "Dropout",
"attributes": [{"name": "ratio", "f": 0.5, "type": 1}],
}
]
ops = {9: ops, 10: ops}
check_onnx_opsets_operator(
MyModule(),
x,
ops,
opset_versions=[9, 10],
training=torch.onnx.TrainingMode.TRAINING,
)
# test eval mode
ops = [{"op_name": "Identity"}]
ops = {9: ops, 10: ops}
check_onnx_opsets_operator(
MyModule(),
x,
ops,
opset_versions=[9, 10],
training=torch.onnx.TrainingMode.EVAL,
)
def test_full(self):
class MyModule(Module):
def forward(self, x):
return torch.full((3, 4), x)
ops = [
{"op_name": "Constant"},
{"op_name": "ConstantOfShape"},
{"op_name": "Add"},
]
ops = {9: ops, 10: ops}
x = torch.tensor(12.0)
check_onnx_opsets_operator(MyModule(), x, ops, opset_versions=[9, 10])
def test_interpolate(self):
class MyModel(torch.nn.Module):
def forward(self, x):
size = [v * 2 for v in x.size()[2:]]
return torch.nn.functional.interpolate(x, size=size, mode="nearest")
ops_9 = [
{"op_name": "Shape"},
{"op_name": "Constant"},
{"op_name": "Gather"},
{"op_name": "Shape"},
{"op_name": "Constant"},
{"op_name": "Gather"},
{"op_name": "Constant"},
{"op_name": "Mul"},
{"op_name": "Constant"},
{"op_name": "Mul"},
{"op_name": "Unsqueeze"},
{"op_name": "Unsqueeze"},
{"op_name": "Concat"},
{"op_name": "Cast"},
{"op_name": "Shape"},
{"op_name": "Slice"},
{"op_name": "Cast"},
{"op_name": "Div"},
{"op_name": "Constant"},
{"op_name": "Concat"},
{
"op_name": "Upsample",
"attributes": [{"name": "mode", "s": (b"nearest"), "type": 3}],
},
]
ops_10 = [
{"op_name": "Shape"},
{"op_name": "Constant"},
{"op_name": "Gather"},
{"op_name": "Shape"},
{"op_name": "Constant"},
{"op_name": "Gather"},
{"op_name": "Constant"},
{"op_name": "Mul"},
{"op_name": "Constant"},
{"op_name": "Mul"},
{"op_name": "Unsqueeze"},
{"op_name": "Unsqueeze"},
{"op_name": "Concat"},
{"op_name": "Cast"},
{"op_name": "Shape"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Slice"},
{"op_name": "Cast"},
{"op_name": "Div"},
{"op_name": "Constant"},
{"op_name": "Concat"},
{
"op_name": "Resize",
"attributes": [{"name": "mode", "s": (b"nearest"), "type": 3}],
},
]
ops = {9: ops_9, 10: ops_10}
x = torch.randn(1, 2, 3, 4, requires_grad=True)
check_onnx_opsets_operator(
MyModel(),
x,
ops,
opset_versions=[9, 10],
input_names=["x"],
dynamic_axes={"x": [0, 1, 2, 3]},
)
ops_9 = [
{"op_name": "Constant"},
{"op_name": "Shape"},
{"op_name": "Slice"},
{"op_name": "Cast"},
{"op_name": "Div"},
{"op_name": "Constant"},
{"op_name": "Concat"},
{
"op_name": "Upsample",
"attributes": [{"name": "mode", "s": (b"nearest"), "type": 3}],
},
]
ops_10 = [
{"op_name": "Constant"},
{"op_name": "Shape"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Constant"},
{"op_name": "Slice"},
{"op_name": "Cast"},
{"op_name": "Div"},
{"op_name": "Constant"},
{"op_name": "Concat"},
{"op_name": "Resize"},
]
ops = {9: ops_9, 10: ops_10}
x = torch.randn(1, 2, 3, 4, requires_grad=True)
check_onnx_opsets_operator(MyModel(), x, ops, opset_versions=[9, 10])
class MyDynamicModel(torch.nn.Module):
def forward(self, x):
size = [v * 2 for v in x.size()[2:]]
# work around for now: turn the dynamic sizes into constant
size = [int(i) for i in size]
return torch.nn.functional.interpolate(x, size=size, mode="nearest")
ops_9 = [
{"op_name": "Constant"},
{
"op_name": "Upsample",
"attributes": [{"name": "mode", "s": (b"nearest"), "type": 3}],
},
]
ops_10 = [
{"op_name": "Constant"},
{
"op_name": "Resize",
"attributes": [{"name": "mode", "s": (b"nearest"), "type": 3}],
},
]
ops = {9: ops_9, 10: ops_10}
x = torch.randn(20, 16, 50)
check_onnx_opsets_operator(MyDynamicModel(), x, ops, opset_versions=[9, 10])
def test_affine_grid(self):
class MyModule(Module):
def __init__(self, align_corners):
super().__init__()
self.align_corners = align_corners
def forward(self, theta, size):
return torch.nn.functional.affine_grid(
theta, size, align_corners=self.align_corners
)
opset_version = 20
ops_2d = {
opset_version: [
{"op_name": "Constant"},
{"op_name": "Unsqueeze"},
{"op_name": "Constant"},
{"op_name": "Unsqueeze"},
{"op_name": "Constant"},
{"op_name": "Unsqueeze"},
{"op_name": "Constant"},
{"op_name": "Unsqueeze"},
{"op_name": "Concat"},
{"op_name": "AffineGrid"},
]
}
ops_3d = {
opset_version: [
{"op_name": "Constant"},
{"op_name": "Unsqueeze"},
{"op_name": "Constant"},
{"op_name": "Unsqueeze"},
{"op_name": "Constant"},
{"op_name": "Unsqueeze"},
{"op_name": "Constant"},
{"op_name": "Unsqueeze"},
{"op_name": "Constant"},
{"op_name": "Unsqueeze"},
{"op_name": "Concat"},
{"op_name": "AffineGrid"},
]
}
# 2D affine
theta_2d = torch.empty(1, 2, 3, dtype=torch.double)
size_2d = torch.Size([1, 1, 2, 2])
# 3D affine
theta_3d = torch.empty(1, 3, 4, dtype=torch.double)
size_3d = torch.Size([1, 1, 2, 2, 2])
for inputs, align_corners in itertools.product(
((theta_2d, size_2d, ops_2d), (theta_3d, size_3d, ops_3d)),
(True, False),
):
theta, size, ops = inputs
args = (
theta,
size,
)
check_onnx_opsets_operator(
MyModule(align_corners=align_corners),
args,
ops,
opset_versions=[opset_version],
training=torch.onnx.TrainingMode.TRAINING,
)
check_onnx_opsets_operator(
MyModule(align_corners=align_corners),
args,
ops,
opset_versions=[opset_version],
training=torch.onnx.TrainingMode.EVAL,
)
def test_grid_sample(self):
class MyModule(torch.nn.Module):
def __init__(self, mode, padding_mode, align_corners):
super().__init__()
self.mode = mode
self.padding_mode = padding_mode
self.align_corners = align_corners
def forward(self, x, grid):
return torch.nn.functional.grid_sample(
x,
grid,
mode=self.mode,
padding_mode=self.padding_mode,
align_corners=self.align_corners,
)
for mode, padding_mode, align_corners, opset_version in itertools.product(
("bilinear", "nearest", "bicubic"),
("zeros", "border", "reflection"),
(True, False),
(16, 20),
):
def test_eval_and_training(
ops, opset_version, mode, padding_mode, align_corners, x_shape, grid
):
args = (
torch.randn(*x_shape), # x
torch.randn(grid), # grid,
)
check_onnx_opsets_operator(
MyModule(
mode=mode,
padding_mode=padding_mode,
align_corners=align_corners,
),
args,
ops,
opset_versions=[opset_version],
training=torch.onnx.TrainingMode.TRAINING,
)
check_onnx_opsets_operator(
MyModule(
mode=mode,
padding_mode=padding_mode,
align_corners=align_corners,
),
args,
ops,
opset_versions=[opset_version],
training=torch.onnx.TrainingMode.EVAL,
)
ops = {opset_version: [{"op_name": "GridSample"}]}
# mode = convert_grid_sample_mode(mode) if opset_version == 20 else mode
n, c, d_in, h_in, w_in, d_out, h_out, w_out = 1, 1, 2, 3, 2, 3, 2, 4
test_eval_and_training(
ops,
opset_version,
mode,
padding_mode,
align_corners,
(n, c, h_in, w_in),
(n, h_out, w_out, 2),
)
if opset_version == 20 and mode != "bicubic":
test_eval_and_training(
ops,
opset_version,
mode,
padding_mode,
align_corners,
(n, c, d_in, h_in, w_in),
(n, d_out, h_out, w_out, 3),
)
def test_flatten(self):
class MyModule(Module):
def forward(self, x):
return torch.flatten(x)
module = MyModule()
ops_0d = [{"op_name": "Constant"}, {"op_name": "Reshape"}]
ops_1d = [{"op_name": "Identity"}]
for shape in ([], [3]):
x = torch.randn(shape)
for opset_version in [9, 10]:
ops = {opset_version: (ops_0d if len(shape) == 0 else ops_1d)}
check_onnx_opsets_operator(
module, x, ops, opset_versions=[opset_version]
)
if __name__ == "__main__":
common_utils.run_tests()