forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fx_to_onnx_with_onnxruntime.py
826 lines (719 loc) · 30.1 KB
/
test_fx_to_onnx_with_onnxruntime.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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
# Owner(s): ["module: onnx"]
from __future__ import annotations
import itertools
import os
import tempfile
import unittest
from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Type
import onnx_test_common
import onnxruntime # type: ignore[import]
import parameterized
import pytorch_test_common
import torch
import torch.onnx
import transformers # type: ignore[import]
from torch import nn
from torch._subclasses import fake_tensor
from torch.onnx._internal import _beartype
from torch.onnx._internal.fx import (
fx_symbolic_graph_extractor,
patcher,
serialization as fx_serialization,
)
from torch.testing._internal import common_utils
try:
import torchvision
HAS_TORCHVISION = True
except ImportError:
HAS_TORCHVISION = False
except RuntimeError:
HAS_TORCHVISION = False
skip_if_no_torchvision = unittest.skipIf(not HAS_TORCHVISION, "no torchvision")
def _parameterized_class_attrs_and_values():
input_values = []
input_values.extend(
itertools.product(
(True, False),
(True, False),
)
)
return {
"attrs": ["op_level_debug", "dynamic_shapes"],
"input_values": input_values,
}
def _parameterize_class_name(cls: Type, idx: int, input_dicts: Mapping[Any, Any]):
"""Combine class name with the parameterized arguments.
This function is passed to `parameterized.parameterized_class` as the
`class_name_func` argument.
"""
suffixes = []
for k, v in input_dicts.items():
suffixes.append(f"{k}_{v}")
return f"{cls.__name__}_{'_'.join(suffixes)}"
@parameterized.parameterized_class(
**_parameterized_class_attrs_and_values(),
class_name_func=_parameterize_class_name,
)
class TestFxToOnnxWithOnnxRuntime(onnx_test_common._TestONNXRuntime):
op_level_debug: bool
dynamic_shapes: bool
def setUp(self):
super().setUp()
self.opset_version = 18
self.ort_version = onnxruntime.__version__
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_simple_function(self):
def func(x):
# TODO(justinchuby): Replicate torch's type casting policy
# in the exporter for type promotion support
y = x + 1.0
z = y.relu()
return (y, z)
tensor_x = torch.randn(1, 1, 2, dtype=torch.float32)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(func, (tensor_x,))
@pytorch_test_common.xfail(
"AssertionError: Dynamo input/output is not consistent with traced input/output. "
"Ref: https://github.com/pytorch/pytorch/issues/96379"
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_func_with_args_and_tensor_kwargs(self):
# Non-tensor optional kwargs are always folded into constant and
# removed from input list in Dynamo-traced graph, if its value is not provided
# to tracer. So for a function like
# def func(x, b=1.0)
# here. E.g., if you first Dynamo-trace the model with arguments (x,),
# and then call the traced graph with arguments (x, b=2.0), it will complain
# somewhere that model is called with extra args because the modified
# function is traced into
# def forward(self, x : torch.Tensor):
# add = x + 1.0; x = None
# relu = add.relu()
# return (add, relu)
# To summarize, in order to be traced as graph input, the value of optional kwarg
# must be provided. Otherwise, they are treated as in-graph constants in Dynamo.
# Tensor optional kwargs are an exception. It is always traced as input.
# It is unclear if this behavior is intended or not. But in general it is bad
# practice to set mutable default values.
# `DynamoOptimizeExporter` applies a workaround by binding args and kwargs to
# model signature and fill in the default values of unprovided optional arguments.
def func(x, b=torch.tensor(1.0)):
y = x + b
z = y.relu()
return (y, z)
tensor_x = torch.randn(1, 2, 3, dtype=torch.float32)
# Test without providing optional kwarg.
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(func, (tensor_x,))
# Test with only positional args.
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
func, (tensor_x, torch.tensor(8.0))
)
# Test while specifying optional kwarg.
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
func, (tensor_x,), {"b": torch.tensor(5.0)}
)
@pytorch_test_common.xfail(
"https://github.com/pytorch/pytorch/issues/99534"
"Non-tensor input is not traceable in dynamo."
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_xfail_func_with_non_tensor_args(self):
def func(x, b=1.0):
y = x + b
z = y.relu()
return (y, z)
tensor_x = torch.randn(1, 1, 2, dtype=torch.float32)
export_output = torch.onnx.dynamo_export(
func,
tensor_x,
8.0,
export_options=torch.onnx.ExportOptions(
opset_version=self.opset_version,
op_level_debug=self.op_level_debug,
dynamic_shapes=self.dynamic_shapes,
),
)
onnx_format_args = export_output.adapt_torch_inputs_to_onnx(tensor_x, 8.0)
ref_outputs = export_output.adapt_torch_outputs_to_onnx(func(tensor_x, 8.0))
ort_outputs = onnx_test_common.run_ort(export_output, onnx_format_args)
for ref_output, ort_output in zip(ref_outputs, ort_outputs):
torch.testing.assert_close(ref_output, torch.tensor(ort_output))
# test on different non-tensor input - xfail
onnx_format_args = export_output.adapt_torch_inputs_to_onnx(tensor_x, 9.0)
ref_outputs = export_output.adapt_torch_outputs_to_onnx(func(tensor_x, 9.0))
_ = onnx_test_common.run_ort(export_output, onnx_format_args)
for ref_output, ort_output in zip(ref_outputs, ort_outputs):
torch.testing.assert_close(ref_output, torch.tensor(ort_output))
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_func_with_nested_input_structure(self):
def func(
x_dict: Dict[str, torch.Tensor],
y_tuple: Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]],
z_list: List[List[torch.Tensor]],
):
if "a" in x_dict:
x = x_dict["a"]
elif "b" in x_dict:
x = x_dict["b"]
else:
x = torch.randn(3)
y1, (y2, y3) = y_tuple
z = x + y1 + y2 + y3
for z_sub_list in z_list:
z = z + torch.stack(z_sub_list).sum()
return z
x_dict = {"a": torch.randn(3), "c": torch.randn(3)}
y_tuple = (torch.randn(3), (torch.randn(3), torch.randn(3)))
z_list = [
[torch.randn(3), torch.randn(3)],
[torch.randn(3), torch.randn(3), torch.randn(3)],
]
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
func, (x_dict, y_tuple, z_list)
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_func_with_nested_output_structure(self):
def func(x, y, z):
x = x + y
y = y + z
z = x + y
out1 = (x, (y, z))
out2 = [[x, y], [y, z]]
out3 = {"z": z, "x": x}
return out1, out2, out3
x = torch.randn(3)
y = torch.randn(3)
z = torch.randn(3)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(func, (x, y, z))
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_mnist(self):
class MNISTModel(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1, bias=True)
self.conv2 = nn.Conv2d(32, 64, 3, 1, bias=True)
self.fc1 = nn.Linear(9216, 128, bias=True)
self.fc2 = nn.Linear(128, 10, bias=True)
def forward(self, tensor_x: torch.Tensor):
tensor_x = self.conv1(tensor_x)
tensor_x = torch.sigmoid(tensor_x)
tensor_x = self.conv2(tensor_x)
tensor_x = torch.sigmoid(tensor_x)
tensor_x = torch.max_pool2d(tensor_x, 2)
tensor_x = torch.flatten(tensor_x, 1)
tensor_x = self.fc1(tensor_x)
tensor_x = torch.sigmoid(tensor_x)
tensor_x = self.fc2(tensor_x)
output = torch.log_softmax(tensor_x, dim=1)
return output
tensor_x = torch.rand((64, 1, 28, 28), dtype=torch.float32)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
MNISTModel(), (tensor_x,)
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_log_sigmoid(self):
# This produces op as `torch.ops.aten.log_sigmoid_forward`, instead of the more
# conventional `torch.ops.aten.log_sigmoid`.
class Model(torch.nn.Module):
def __init__(self):
super().__init__()
self.m = torch.nn.LogSigmoid()
def forward(self, x):
return self.m(x)
input = torch.randn(2)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(Model(), (input,))
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
@skip_if_no_torchvision
def test_resnet18(self):
# TODO(bowbao): Note [training vs eval in dynamo_export]
# So we are effectively exporting all models in traning mode by
# default. But for the sake of this export we are only interested in eval mode.
# The question is, should we call `model.eval()` in `dynamo_export`?
# This particular test fails 'functionalization' in training mode.
# So we are explicitly calling `model.eval()` for any model that contains
# batch norm.
# Ref: https://github.com/pytorch/pytorch/issues/99662#issuecomment-1528178221
model = torchvision.models.resnet18(pretrained=False).eval()
dummy_input = torch.randn(1, 3, 224, 224)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
model,
(dummy_input,),
)
@pytorch_test_common.xfail(
"RuntimeError: Unknown call_function target: aten.mean.dim"
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
@skip_if_no_torchvision
def test_shufflenet_v2(self):
# TODO(bowbao): see Note [training vs eval in dynamo_export]
model = torchvision.models.shufflenet_v2_x0_5(pretrained=False).eval()
dummy_input = torch.randn(1, 3, 224, 224, requires_grad=True)
test_inputs = torch.randn(3, 3, 224, 224, requires_grad=True)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
model,
(dummy_input,),
additional_test_inputs=[((test_inputs,),)],
rtol=1e-3,
atol=1e-5,
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_add(self):
class DynamicAdd(torch.nn.Module):
def forward(self, x, y):
return torch.ops.aten.add(x, y)
x = torch.randn(2, 3)
y = torch.randn(2, 3)
another_x = torch.randn(3, 4)
another_y = torch.randn(3, 4)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
DynamicAdd(),
(x, y),
additional_test_inputs=[((another_x, another_y),)],
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_sigmoid_add(self):
class DynamicAdd(torch.nn.Module):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x, y):
z = torch.ops.aten.add(x, y)
return self.sigmoid(z)
x = torch.randn(2, 3)
y = torch.randn(2, 3)
x = x[1:, :]
y = y[1:, :]
input_x = torch.randn(1, 4)
input_y = torch.randn(1, 4)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
DynamicAdd(), (x, y), additional_test_inputs=[((input_x, input_y),)]
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_matmul(self):
class DynamicMatMul(torch.nn.Module):
def forward(self, x, y):
return torch.ops.aten.matmul(x, y)
x = torch.randn(2, 3, 6)
y = torch.randn(2, 6, 4)
input_x = torch.randn(2, 3, 4)
input_y = torch.randn(2, 4, 4)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
DynamicMatMul(), (x, y), additional_test_inputs=[((input_x, input_y),)]
)
@pytorch_test_common.skip_dynamic_fx_test(
"fx graph does not capture symbolic value for aten::scalar_tensor."
)
def test_scalar_tensor(self):
class test(torch.nn.Module):
def forward(self, x):
return torch.scalar_tensor(x.size(0)), torch.scalar_tensor(
x.size(1), dtype=torch.int64
)
x = torch.randn(2, 3, 4)
y = torch.randn(7, 8, 9)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
test(),
(x,),
additional_test_inputs=[((y,),)],
)
def test_transpose_infer_shape(self):
class TransposeModule(torch.nn.Module):
def __init__(self):
super().__init__()
self.conv = torch.nn.Conv2d(3, 1, 3, stride=2)
def forward(self, x):
x = self.conv(x)
return x.transpose(0, 1)
x = torch.randn(32, 3, 64, 64)
y = torch.randn(16, 3, 8, 64)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
TransposeModule(),
(x,),
additional_test_inputs=[((y,),)],
)
@pytorch_test_common.xfail("torch._dynamo.exc.TorchRuntimeError")
def test_squeeze_runtime_dim(self):
class Squeeze(torch.nn.Module):
def forward(self, d1, d2):
t = torch.zeros(d1[0], d2[0])
return t.squeeze(0)
d1 = torch.tensor([1])
d3 = torch.tensor([3])
d4 = torch.tensor([4])
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
Squeeze(), (d1, d4), additional_test_inputs=[((d3, d4),)]
)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
Squeeze(), (d3, d4), additional_test_inputs=[((d1, d3),)]
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_slice(self):
class DynamicSliceExportMod(torch.nn.Module):
def forward(self, x):
results = []
for i in range(4):
results.append(x[: x.size(0) - i, i : x.size(2), i:3])
return tuple(results)
x = torch.rand(5, 5, 5)
y = torch.randn(6, 7, 8)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
DynamicSliceExportMod(),
(x,),
additional_test_inputs=[((y,),)],
)
# TODO(titaiwang): This is also detected flaky in static shape:
# https://github.com/pytorch/pytorch/issues/98622
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=False,
)
def test_mutation(self):
class MutationModel(torch.nn.Module):
def forward(self, x):
x.view(3, 2, -1).add_(2.0)
return x
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
MutationModel(), (torch.randn(12),), has_mutation=True
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=False,
)
def test_arange(self):
class ArangeModel(torch.nn.Module):
def forward(self, input):
return (
torch.arange(input.shape[0]),
torch.arange(12),
torch.arange(start=input.shape[0], end=input.shape[0] + 5),
)
x = torch.randn(5, 3, 2)
y = torch.randn(8, 3, 2)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
ArangeModel(),
(x,),
additional_test_inputs=[((y,),)],
)
@pytorch_test_common.xfail(
"fx.graph: torch._subclasses.fake_tensor.DataDependentOutputException: "
"aten._local_scalar_dense.default"
)
def test_expand_as_fill_zero(self):
class Model(torch.nn.Module):
def forward(self, x):
x[:, x.size(0) :] = 0
return x
x = torch.ones(2, 5)
x2 = torch.randn(3, 4)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
Model(),
(x,),
additional_test_inputs=[((x2,),)],
)
@pytorch_test_common.xfail(
"RuntimeError: Unknown call_function target: aten.lift_fresh_copy.default"
)
def test_expand_as_fill_tensor(self):
class Model(torch.nn.Module):
def forward(self, x):
x[:, x.size(0) :] = torch.tensor([1, 2, 3])
return x
x = torch.ones(2, 5, 3)
x2 = torch.randn(3, 4, 3)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
Model(),
(x,),
additional_test_inputs=[((x2,),)],
)
@pytorch_test_common.xfail(
"Unknown call_function target: aten.lift_fresh_copy.default"
)
def test_expand_as_fill_seperate_tensor(self):
class Model(torch.nn.Module):
def forward(self, x):
aa = torch.tensor([[0], [1], [2]])
return aa.expand_as(x)
x = torch.ones(3, 2)
x2 = torch.randn(3, 5)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
Model(),
(x,),
additional_test_inputs=[((x2,),)],
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_view_dynamic_zero_dim(self):
class ViewModel(torch.nn.Module):
def forward(self, input):
input = input.view(-1, 2)
return input.view(1, -1)
x = torch.ones(2)
another_x = torch.empty((0,))
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
ViewModel(),
(x,),
additional_test_inputs=[((another_x,),)],
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_flatten_dynamic_axes(self):
class MyModule(torch.nn.Module):
def forward(self, x):
return torch.flatten(x, start_dim=2, end_dim=3)
batch_size = 3
x = torch.randn(batch_size, 5, 4, 5)
y = torch.randn(5, 5, 4, 5)
model = MyModule()
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
model, (x,), additional_test_inputs=[((y,),)]
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_none_input(self):
class NoneInputModel(torch.nn.Module):
def forward(
self, x: torch.Tensor, y: Optional[torch.Tensor], z: torch.Tensor
):
if y is None:
return x + z
return x + y + z
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
NoneInputModel(), (torch.randn(1, 2), None, torch.randn(1, 2))
)
@pytorch_test_common.skip_min_ort_version(
reason="ORT doesn't support dynamic fx exporter yet making SegFault flaky test",
version="1.15",
dynamic_only=True,
)
def test_gpt2_tiny(self):
model_name = "sshleifer/tiny-gpt2"
# Download pytorch model
model = transformers.AutoModel.from_pretrained(model_name)
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
# Transform input tokens
inputs = tokenizer("Hello world!", return_tensors="pt")
another_inputs = tokenizer("Another Hello world!", return_tensors="pt")
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
model, [], inputs, additional_test_inputs=[((), another_inputs)]
)
@_beartype.beartype
def _test_fx_symbolic_tracer_large_scale_exporter(
self,
model_name: str,
create_model: Callable,
create_args: Callable,
create_pytorch_only_kwargs: Callable,
):
"""Test helper for large-scale exporter.
Arguments:
model_name: Name of the model. It used to name temporary files.
create_model: A function that creates a model. It should always create the same model.
create_args: A function that creates random input arguments for the model.
create_pytorch_only_kwargs: A function that creates kwargs for calling PyTorch model with real tensors.
This test contains several steps.
1. Create a toy model.
2. Save the toy's state (parameters) to a file. This is for simulating a checkpoint file.
3. Load it back and export it to ONNX with large-scale exporter.
All operations (including model loading) are done under
FakeTensorMode so no real tensor is created and no real
computation happens.
4. The ONNX model generated in step 3 doesn't contain parameters,
and this step adds them as external data and save a new ONNX model.
5. Run PyTorch and ONNX models and compare their results.
"""
# Create the toy model.
model = create_model()
with tempfile.NamedTemporaryFile(
prefix=model_name, suffix=".pt"
) as tmp_file, tempfile.TemporaryDirectory(
suffix="large_scale_export"
) as tmp_folder:
# Dump state_dict to a file to simulate how HuggingFace model is initialized.
# The file will be loaded via .load_state_dict(...)
torch.save(model.state_dict(), tmp_file.name)
ftm = fake_tensor.FakeTensorMode(
allow_non_fake_inputs=True, allow_fallback_kernels=False
)
ctx = patcher.ONNXTorchPatcher()
# NOTE: FakeTensorMode disallows symbolic shape of fx graph
# The following coed block does several things.
# 1. Create a model whose parameters and buffers are all FakeTensor's.
# 2. Convert nn.Module into ONNX model without initializers.
# 3. Record the file paths to find real initializers.
with ctx, ftm:
# Toy model with parameters and buffers as FakeTensor's.
fake_model = create_model()
fake_model.load_state_dict(torch.load(tmp_file.name))
# Toy inputs as FakeTensor's.
fake_args = create_args()
# Export ONNX model without initializers while ctx.paths records
# all files that contains real initializers.
options = torch.onnx.ExportOptions(
opset_version=self.opset_version,
dynamic_shapes=self.dynamic_shapes,
op_level_debug=self.op_level_debug,
)
export_options = torch.onnx._internal.exporter.ResolvedExportOptions(
options
)
export_options.fx_tracer = (
fx_symbolic_graph_extractor.FXSymbolicTracer()
)
export_output = torch.onnx.dynamo_export(
fake_model,
*fake_args,
export_options=export_options,
)
onnx_model = export_output.model_proto
# Tasks done by the following block.
# 1. Iterate through all tensors stored in ctx.paths (the file content is loaded torch.load)
# 2. If a tensor's name matches a "onnx_model"'s input name, an initializer is created and saved to
# a seperated folder.
# 3. A new ONNX model is saved into file with the initializers saved in the previous step.
# 4. ORT executes the new ONNX model and compares the results with the original GPT model.
# Model saved to tmp_folder/onnx_model_location
# Initializers are saved to tmp_folder/onnx_initializer_location/*.onnx
onnx_model_location = model_name + "_external_data.onnx"
onnx_initializer_location = model_name + "_initializers"
fx_serialization.save_model_with_external_data(
tmp_folder,
onnx_model_location,
onnx_initializer_location,
tuple(ctx.paths),
onnx_model,
)
# Generate random inputs.
args = create_args()
kwargs = create_pytorch_only_kwargs()
# Original outputs.
ref_outputs = export_output.adapt_torch_outputs_to_onnx(
model(*args, **kwargs)
)
# ORT outputs.
args_not_none = export_output.adapt_torch_inputs_to_onnx(*args)
# Drop Parameters and buffers added by fx_serialization.save_model_with_external_data
args_not_none = args_not_none[: len(args) - len(kwargs)]
ort_outputs = onnx_test_common.run_ort(
os.path.join(tmp_folder, onnx_model_location),
args_not_none,
)
assert len(ref_outputs) == len(ort_outputs)
for ref_output, ort_output in zip(ref_outputs, ort_outputs):
torch.testing.assert_close(ref_output, torch.tensor(ort_output))
@pytorch_test_common.skip_dynamic_fx_test(
"FakeTensor exporting is not supported by dynamic axes."
)
def test_fx_symbolic_tracer_large_scale_exporter_with_toy_mlp(self):
class MLPModel(nn.Module):
def __init__(self):
super().__init__()
self.fc0 = nn.Linear(8, 8, bias=True)
self.fc1 = nn.Linear(8, 4, bias=True)
self.fc2 = nn.Linear(4, 2, bias=True)
self.fc3 = nn.Linear(2, 2, bias=True)
def forward(self, tensor_x: torch.Tensor):
tensor_x = self.fc0(tensor_x)
tensor_x = torch.sigmoid(tensor_x)
tensor_x = self.fc1(tensor_x)
tensor_x = torch.sigmoid(tensor_x)
tensor_x = self.fc2(tensor_x)
tensor_x = torch.sigmoid(tensor_x)
output = self.fc3(tensor_x)
return output
def create_model() -> nn.Module:
return MLPModel()
def create_args():
return (torch.rand((97, 8), dtype=torch.float32),)
def create_pytorch_only_extra_kwargs():
return {}
self._test_fx_symbolic_tracer_large_scale_exporter(
"toy_mlp1",
create_model,
create_args,
create_pytorch_only_extra_kwargs,
)
@pytorch_test_common.skip_dynamic_fx_test(
"FakeTensor exporting is not supported by dynamic axes."
)
def test_large_scale_exporter_with_tiny_gpt2(self):
model_name = "sshleifer/tiny-gpt2"
def create_model() -> nn.Module:
return transformers.AutoModel.from_pretrained(model_name)
def create_args():
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
kwargs = tokenizer("Hello world!", return_tensors="pt")
input_ids = kwargs["input_ids"]
attention_mask = kwargs["attention_mask"]
return input_ids, None, attention_mask
def create_pytorch_only_extra_kwargs():
return {"return_dict": False}
self._test_fx_symbolic_tracer_large_scale_exporter(
"tiny_gpt2",
create_model,
create_args,
create_pytorch_only_extra_kwargs,
)
if __name__ == "__main__":
common_utils.run_tests()