forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fx_to_onnx_with_onnxruntime.py
1493 lines (1286 loc) · 56.2 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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Owner(s): ["module: onnx"]
from __future__ import annotations
import itertools
import math
import operator
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 # type: ignore[import]
import pytorch_test_common
import transformers # type: ignore[import]
import torch
import torch.onnx
from torch import nn
from torch._subclasses import fake_tensor
from torch.onnx._internal import _beartype, exporter
from torch.onnx._internal.fx import (
diagnostics,
fx_symbolic_graph_extractor,
patcher,
serialization as fx_serialization,
)
from torch.testing._internal import common_utils
try:
import torchvision # type: ignore[import]
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),
(
pytorch_test_common.TorchModelType.TORCH_NN_MODULE,
pytorch_test_common.TorchModelType.TORCH_EXPORT_EXPORTEDPROGRAM,
),
)
)
return {
"attrs": ["op_level_debug", "dynamic_shapes", "model_type"],
"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
model_type: pytorch_test_common.TorchModelType
def setUp(self):
super().setUp()
self.ort_version = onnxruntime.__version__
def test_simple_function(self):
class Foo(torch.nn.Module):
def forward(self, 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)
func = Foo()
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(
error_message="Unexpectedly found a <class 'torch.Tensor'> in the inputs.",
reason="https://github.com/pytorch/pytorch/issues/96379",
)
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.
class Foo(torch.nn.Module):
def forward(self, x, b=torch.tensor(1.0)):
y = x + b
z = y.relu()
return (y, z)
func = Foo()
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,), input_kwargs={"b": torch.tensor(5.0)}
)
@pytorch_test_common.skip_dynamic_fx_test(
"sympy operation tests don't need dynamic shape"
)
def test_sympy_operatons_return_numeric(self):
class Foo(torch.nn.Module):
def forward(self, x, y):
# TODO: add boolean tests when SymBool is supported
# to infer types
return (
torch.tensor([operator.add(x.item(), y.item())]),
torch.tensor([operator.sub(x.item(), y.item())]),
torch.tensor([operator.mul(x.item(), y.item())]),
torch.tensor([operator.truediv(x.item(), y.item())]),
# This requires torch.sym_float, probably easy to lower to
# ONNX but I don't know where to put it
# torch.tensor([operator.floordiv(x.item(), y.item())]),
# NB: abs so that the base and exponent are provably
# non-negative, so we don't generate runtime asserts
torch.tensor([operator.pow(abs(x.item()), abs(y.item()))]),
torch.tensor([operator.abs(x.item())]),
torch.tensor([operator.neg(x.item())]),
torch.tensor([math.ceil(x.item())]),
torch.tensor([math.floor(x.item())]),
)
func = Foo()
x = torch.randn(1, dtype=torch.float32)
y = torch.randn(1, dtype=torch.float32)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
func,
(
x,
y,
),
)
@pytorch_test_common.xfail(
error_message="Model inputs incompatible with the format that was exported",
reason="https://github.com/pytorch/pytorch/issues/99534",
)
def test_xfail_func_with_non_tensor_args(self):
class Foo(torch.nn.Module):
def forward(self, x, b=1.0):
y = x + b
z = y.relu()
return (y, z)
func = Foo()
tensor_x = torch.randn(1, 1, 2, dtype=torch.float32)
onnx_program = torch.onnx.dynamo_export(
func,
tensor_x,
8.0,
export_options=torch.onnx.ExportOptions(
op_level_debug=self.op_level_debug,
dynamic_shapes=self.dynamic_shapes,
),
)
onnx_test_common.assert_dynamic_shapes(onnx_program, self.dynamic_shapes)
onnx_format_args = onnx_program.adapt_torch_inputs_to_onnx(tensor_x, b=8.0)
ref_outputs = onnx_program.adapt_torch_outputs_to_onnx(func(tensor_x, 8.0))
ort_outputs = onnx_test_common.run_ort(onnx_program, 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 = onnx_program.adapt_torch_inputs_to_onnx(tensor_x, b=9.0)
ref_outputs = onnx_program.adapt_torch_outputs_to_onnx(func(tensor_x, 9.0))
_ = onnx_test_common.run_ort(onnx_program, onnx_format_args)
for ref_output, ort_output in zip(ref_outputs, ort_outputs):
torch.testing.assert_close(ref_output, torch.tensor(ort_output))
def test_func_with_nested_input_structure(self):
class Foo(torch.nn.Module):
def forward(
self,
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
func = Foo()
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)
)
def test_func_with_nested_output_structure(self):
class Foo(torch.nn.Module):
def forward(self, 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
func = Foo()
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))
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,)
)
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,))
@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(weights=None).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_dynamic_fx_test(
error_message="[ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input"
)
@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(weights=None).eval()
dummy_input = torch.randn(1, 3, 224, 224, requires_grad=False)
test_inputs = torch.randn(3, 3, 224, 224, requires_grad=False)
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,
)
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),)],
)
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),)]
)
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.xfail_dynamic_fx_test(
error_message="The values for attribute 'shape' do not match: torch.Size([]) != torch.Size([1])"
)
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(
error_message=("Unsupported FX nodes: {'call_function': [")
)
def test_squeeze_runtime_dim(self):
class Squeeze(torch.nn.Module):
def forward(self, d1, d2):
t = torch.zeros(d1[0], d2[0]) # problematic user code for dynamo
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),)]
)
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,),)],
)
@pytorch_test_common.xfail_if_model_type_is_exportedprogram(
error_message="Expected 1 outputs, got 2",
)
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
)
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_dynamic_fx_test(
error_message="[ONNXRuntimeError] : 1 : FAIL : Non-zero status code returned while running Slice node. "
)
@pytorch_test_common.xfail_if_model_type_is_exportedprogram(
error_message="Expected 1 outputs, got 2"
)
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_dynamic_fx_test(
error_message="[ONNXRuntimeError] : 1 : FAIL : Non-zero status code returned while running Slice node. "
)
@pytorch_test_common.xfail_if_model_type_is_exportedprogram(
error_message="Expected 1 outputs, got 2"
)
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_if_model_type_is_not_exportedprogram(
error_message="at::functionalization::impl::isFunctionalTensor(self_) INTERNAL ASSERT FAILED"
)
def test_expand_as_fill_separate_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.skipIfNoCuda
def test__scaled_dot_product_flash_attention(self):
class Foo(torch.nn.Module):
def forward(self, x):
(
output,
_,
_,
_,
_,
_,
_,
_,
_,
) = torch.ops.aten._scaled_dot_product_flash_attention(x, x, x)
return output
func = Foo()
x = torch.randn(1, 1, 1, 32, device=torch.device("cuda"))
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(func, (x,))
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)
y = torch.empty(0)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
ViewModel(),
(x,),
additional_test_inputs=[((y,),)],
)
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,),)]
)
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))
)
def test_operator_with_data_dependent_output(self):
class Foo(torch.nn.Module):
def forward(self, x):
# Repro from llama. Emits `torch.ops.aten._local_scalar_dense`.
return x + torch.full(x.shape, torch.tensor(torch.finfo(x.dtype).min))
func = Foo()
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
func, (torch.randn(3, 4),)
)
@pytorch_test_common.xfail_if_model_type_is_exportedprogram(
error_message="Unsupported FX nodes: {'call_function': ['aten._assert_async.msg']}.",
reason="https://github.com/pytorch/pytorch/issues/112622",
)
def test_operator_with_scalar_output(self):
class Foo(torch.nn.Module):
def forward(self, x, y):
return x.item() + y
func = Foo()
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
func, (torch.tensor([1]), torch.randn(3, 4))
)
@pytorch_test_common.xfail_if_model_type_is_exportedprogram(
error_message="Unsupported FX nodes: {'call_function': ['aten._assert_async.msg']}",
reason="https://github.com/pytorch/pytorch/issues/112622",
)
def test_operator_with_dynamic_output_shape(self):
class Foo(torch.nn.Module):
def forward(self, x):
return x.nonzero()
func = Foo()
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
func, (torch.randn(3, 4),)
)
@pytorch_test_common.xfail_if_model_type_is_exportedprogram(
error_message="Trying to flatten user inputs with exported input tree spec"
)
@pytorch_test_common.xfail_dynamic_fx_test(
error_message="!(it.GetName().empty())",
reason="With after onnx==1.16, constant folding in optimizer causes this error.",
model_type=pytorch_test_common.TorchModelType.TORCH_NN_MODULE,
)
def test_gpt2_tiny_from_config(self):
# Model
config = transformers.GPT2Config(
num_hidden_layers=4,
vocab_size=8096,
hidden_size=16,
intermediate_size=16,
max_position_embeddings=512,
num_attention_heads=2,
hidden_dropout_prob=0.0,
attention_dropout_prob=0.0,
)
model = transformers.GPT2Model(config).eval()
def input_generator(batch: int, seq: int):
input_ids = torch.randint(0, 8096, (batch, seq))
attention_mask = torch.ones(batch, seq, dtype=torch.bool)
position_ids = torch.arange(0, seq, dtype=torch.long)
position_ids = position_ids.unsqueeze(0).view(-1, seq)
return input_ids, attention_mask, position_ids
# Encoded inputs
input_ids, attention_mask, position_ids = input_generator(2, 128)
# Another encoded inputs to test dynamic shapes
(
another_input_ids,
another_attention_mask,
another_position_ids,
) = input_generator(3, 256)
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
model,
(input_ids,),
input_kwargs={
"attention_mask": attention_mask,
"position_ids": position_ids,
},
additional_test_inputs=[
(
(another_input_ids,),
{
"attention_mask": another_attention_mask,
"position_ids": another_position_ids,
},
)
],
)
def test_prims_device_put(self):
class CustomModule(nn.Module):
def forward(self, x):
# Assuming x is a tensor on the CPU, move it to the desired device using device_put()
x = torch.ops.prims.device_put(x, "cpu")
return x
self.run_test_with_fx_to_onnx_exporter_and_onnx_runtime(
CustomModule(), (torch.randn(1, 2, 3),)
)
@_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(
dynamic_shapes=self.dynamic_shapes,
op_level_debug=self.op_level_debug,
)
export_options = exporter.ResolvedExportOptions(options)
export_options.fx_tracer = (
fx_symbolic_graph_extractor.FXSymbolicTracer()
)
onnx_program = torch.onnx.dynamo_export(
fake_model,
*fake_args,
export_options=export_options,
)
onnx_model = onnx_program.model_proto
onnx_test_common.assert_dynamic_shapes(onnx_program, self.dynamic_shapes)
# 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"
# TODO: We are using the internal `save_model_with_external_data` instead of public
# `ONNXProgram.save` because we need to rename ONNX initializers before saving.
# This is only needed/allowed because we are using `fx_tracer=FXSymbolicTracer`,
# which is not an official FX tracer.
fx_serialization.save_model_with_external_data(
tmp_folder,
onnx_model_location,
onnx_initializer_location,
tuple(ctx.paths),
onnx_model,
rename_initializer=True,
)
# Generate random inputs.
args = create_args()
kwargs = create_pytorch_only_kwargs()
# Original outputs.
ref_outputs = onnx_program.adapt_torch_outputs_to_onnx(
model(*args, **kwargs)
)
# ORT outputs.
args_not_none = onnx_program.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.xfail_dynamic_fx_test(
error_message="shape_env should be set if tracing with 'symbolic'"
)
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.xfail_dynamic_fx_test(
error_message="shape_env should be set if tracing with 'symbolic'"
)
def test_fx_symbolic_tracer_large_scale_exporter_with_tiny_gpt2(self):
model_name = "sshleifer/tiny-gpt2"
device = "cpu"
def create_model() -> nn.Module:
return transformers.AutoModel.from_pretrained(model_name).to(device).eval()
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,
)
def _parameterized_class_attrs_and_values_with_fake_options():
input_values = []
input_values.extend(
itertools.product(
(True, False),
(True, False),
(True, False),
(True, False),
(
pytorch_test_common.TorchModelType.TORCH_NN_MODULE,
pytorch_test_common.TorchModelType.TORCH_EXPORT_EXPORTEDPROGRAM,
),
)
)
return {
"attrs": [
"op_level_debug",
"dynamic_shapes",
"load_checkpoint_during_init",
"export_within_fake_mode",
"model_type",
],
"input_values": input_values,
}
@parameterized.parameterized_class(
**_parameterized_class_attrs_and_values_with_fake_options(),
class_name_func=_parameterize_class_name,
)
class TestFxToOnnxFakeTensorWithOnnxRuntime(onnx_test_common._TestONNXRuntime):
"""ONNX export test for specific Fake Tensor scenarios
TODO: Should we merge this with `TestFxToOnnxWithOnnxRuntime`? Considerably increases export time
"""
op_level_debug: bool
dynamic_shapes: bool
load_checkpoint_during_init: bool
export_within_fake_mode: bool
model_type: pytorch_test_common.TorchModelType
def setUp(self):
super().setUp()
self.ort_version = onnxruntime.__version__
@_beartype.beartype
def _test_fake_tensor_mode_exporter(
self,
model_name: str,
create_model: Callable,
create_args: Callable,
create_kwargs: Callable,
load_checkpoint_during_init: bool,
export_within_fake_mode: bool,
model_type: pytorch_test_common.TorchModelType,
):
"""Test helper for FakeTensorMode-enabled exporter.
Arguments:
model_name: Name of the model. It used to name temporary files.
create_model: A function that creates a model.
create_args: A function that creates positional inputs for the model.
create_kwargs: A function that creates keyword inputs for ther model.
load_checkpoint_during_init: Whether to load a checkpoint during model initialization.
(after or during model creation, but before exporting starts)
export_within_fake_mode: Whether to call torch.onnx._dynamo_export within torch._subclasses.FakeTensorMode
model_type: Type of user model. Used to determine whether the user model must be exported to
torch.export.ExportedProgram before passing it to torch.onnx.dynamo_export
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 Fake Mode enabled.
Because all operations (including model and input loading) are done under
FakeTensorMode, no real tensor are 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 on an ONNX model.