forked from pytorch/pytorch.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonnx.html
1783 lines (1563 loc) · 157 KB
/
onnx.html
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
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta name="robots" content="noindex">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>torch.onnx — PyTorch 1.12 documentation</title>
<link rel="canonical" href="https://pytorch.org/docs/stable/onnx.html"/>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<!-- <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> -->
<link rel="stylesheet" href="_static/copybutton.css" type="text/css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="stylesheet" href="_static/katex-math.css" type="text/css" />
<link rel="stylesheet" href="_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" type="text/css" />
<link rel="stylesheet" href="_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" type="text/css" />
<link rel="stylesheet" href="_static/css/jit.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="SymbolicContext" href="generated/torch.onnx.SymbolicContext.html" />
<link rel="prev" title="torch.nn.init" href="nn.init.html" />
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-117752657-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-117752657-2');
</script>
<!-- End Google Analytics -->
<script src="_static/js/modernizr.min.js"></script>
<!-- Preload the theme fonts -->
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<!-- Preload the katex fonts -->
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.2/css/all.css" integrity="sha384-vSIIfh2YWi9wW0r9iZe7RJPrKwp6bG+s9QZMoITbCckVJqGCCRhc+ccxNcdpHuYu" crossorigin="anonymous">
</head>
<div class="container-fluid header-holder tutorials-header" id="header-holder">
<div class="container">
<div class="header-container">
<a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
<div class="main-menu">
<ul>
<li>
<a href="https://pytorch.org/get-started">Get Started</a>
</li>
<li>
<a href="https://pytorch.org/ecosystem">Ecosystem</a>
</li>
<li>
<a href="https://pytorch.org/mobile">Mobile</a>
</li>
<li>
<a href="https://pytorch.org/blog/">Blog</a>
</li>
<li>
<a href="https://pytorch.org/tutorials">Tutorials</a>
</li>
<li class="active docs-active">
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="resource-option with-down-orange-arrow">
Docs
</a>
<div class="resources-dropdown-menu">
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/docs/stable/index.html">
<span class="dropdown-title">PyTorch</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/audio/stable/index.html">
<span class="dropdown-title">torchaudio</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/text/stable/index.html">
<span class="dropdown-title">torchtext</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/vision/stable/index.html">
<span class="dropdown-title">torchvision</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/torchrec">
<span class="dropdown-title">TorchRec</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/data">
<span class="dropdown-title">TorchData</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/serve/">
<span class="dropdown-title">TorchServe</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/torchx/">
<span class="dropdown-title">TorchX</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/xla">
<span class="dropdown-title">PyTorch on XLA Devices</span>
<p></p>
</a>
</div>
</li>
<li>
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="resource-option with-down-arrow">
Resources
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/features">
<span class="dropdown-title">About</span>
<p>Learn about PyTorch’s features and capabilities</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/#community-module">
<span class="dropdown-title">Community</span>
<p>Join the PyTorch developer community to contribute, learn, and get your questions answered.</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/resources">
<span class="dropdown-title">Developer Resources</span>
<p>Find resources and get questions answered</p>
</a>
<a class="nav-dropdown-item" href="https://discuss.pytorch.org/" target="_blank">
<span class="dropdown-title">Forums</span>
<p>A place to discuss PyTorch code, issues, install, research</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/hub">
<span class="dropdown-title">Models (Beta)</span>
<p>Discover, publish, and reuse pre-trained models</p>
</a>
</div>
</div>
</li>
<li>
<a href="https://github.com/pytorch/pytorch">GitHub</a>
</li>
</ul>
</div>
<a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
</div>
</div>
</div>
<body class="pytorch-body">
<div class="table-of-contents-link-wrapper">
<span>Table of Contents</span>
<a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
</div>
<nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
<div class="pytorch-side-scroll">
<div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<div class="pytorch-left-menu-search">
<div class="version">
<a href='https://pytorch.org/docs/versions.html'>1.12 ▼</a>
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search Docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p class="caption" role="heading"><span class="caption-text">Community</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="community/build_ci_governance.html">PyTorch Governance | Build + CI</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/contribution_guide.html">PyTorch Contribution Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/design.html">PyTorch Design Philosophy</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/governance.html">PyTorch Governance | Mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/persons_of_interest.html">PyTorch Governance | Maintainers</a></li>
</ul>
<p class="caption"><span class="caption-text">Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="notes/amp_examples.html">CUDA Automatic Mixed Precision examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/autograd.html">Autograd mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/broadcasting.html">Broadcasting semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cpu_threading_torchscript_inference.html">CPU threading and TorchScript inference</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cuda.html">CUDA semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/ddp.html">Distributed Data Parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/extending.html">Extending PyTorch</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/gradcheck.html">Gradcheck mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/hip.html">HIP (ROCm) semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/large_scale_deployments.html">Features for large-scale deployments</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/modules.html">Modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/mps.html">MPS backend</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/multiprocessing.html">Multiprocessing best practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/numerical_accuracy.html">Numerical accuracy</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/randomness.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/serialization.html">Serialization semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/windows.html">Windows FAQ</a></li>
</ul>
<p class="caption"><span class="caption-text">Language Bindings</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="cpp_index.html">C++</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/javadoc/">Javadoc</a></li>
<li class="toctree-l1"><a class="reference internal" href="deploy.html">torch::deploy</a></li>
</ul>
<p class="caption"><span class="caption-text">Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="torch.html">torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.html">torch.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.functional.html">torch.nn.functional</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensors.html">torch.Tensor</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_attributes.html">Tensor Attributes</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_view.html">Tensor Views</a></li>
<li class="toctree-l1"><a class="reference internal" href="amp.html">torch.amp</a></li>
<li class="toctree-l1"><a class="reference internal" href="autograd.html">torch.autograd</a></li>
<li class="toctree-l1"><a class="reference internal" href="library.html">torch.library</a></li>
<li class="toctree-l1"><a class="reference internal" href="cuda.html">torch.cuda</a></li>
<li class="toctree-l1"><a class="reference internal" href="backends.html">torch.backends</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.html">torch.distributed</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.algorithms.join.html">torch.distributed.algorithms.join</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.elastic.html">torch.distributed.elastic</a></li>
<li class="toctree-l1"><a class="reference internal" href="fsdp.html">torch.distributed.fsdp</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.optim.html">torch.distributed.optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributions.html">torch.distributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="fft.html">torch.fft</a></li>
<li class="toctree-l1"><a class="reference internal" href="futures.html">torch.futures</a></li>
<li class="toctree-l1"><a class="reference internal" href="fx.html">torch.fx</a></li>
<li class="toctree-l1"><a class="reference internal" href="hub.html">torch.hub</a></li>
<li class="toctree-l1"><a class="reference internal" href="jit.html">torch.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="linalg.html">torch.linalg</a></li>
<li class="toctree-l1"><a class="reference internal" href="monitor.html">torch.monitor</a></li>
<li class="toctree-l1"><a class="reference internal" href="special.html">torch.special</a></li>
<li class="toctree-l1"><a class="reference internal" href="torch.overrides.html">torch.overrides</a></li>
<li class="toctree-l1"><a class="reference internal" href="package.html">torch.package</a></li>
<li class="toctree-l1"><a class="reference internal" href="profiler.html">torch.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.init.html">torch.nn.init</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">torch.onnx</a></li>
<li class="toctree-l1"><a class="reference internal" href="optim.html">torch.optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="complex_numbers.html">Complex Numbers</a></li>
<li class="toctree-l1"><a class="reference internal" href="ddp_comm_hooks.html">DDP Communication Hooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="pipeline.html">Pipeline Parallelism</a></li>
<li class="toctree-l1"><a class="reference internal" href="quantization.html">Quantization</a></li>
<li class="toctree-l1"><a class="reference internal" href="rpc.html">Distributed RPC Framework</a></li>
<li class="toctree-l1"><a class="reference internal" href="random.html">torch.random</a></li>
<li class="toctree-l1"><a class="reference internal" href="nested.html">torch.nested</a></li>
<li class="toctree-l1"><a class="reference internal" href="sparse.html">torch.sparse</a></li>
<li class="toctree-l1"><a class="reference internal" href="storage.html">torch.Storage</a></li>
<li class="toctree-l1"><a class="reference internal" href="testing.html">torch.testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="benchmark_utils.html">torch.utils.benchmark</a></li>
<li class="toctree-l1"><a class="reference internal" href="bottleneck.html">torch.utils.bottleneck</a></li>
<li class="toctree-l1"><a class="reference internal" href="checkpoint.html">torch.utils.checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="cpp_extension.html">torch.utils.cpp_extension</a></li>
<li class="toctree-l1"><a class="reference internal" href="data.html">torch.utils.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="dlpack.html">torch.utils.dlpack</a></li>
<li class="toctree-l1"><a class="reference internal" href="mobile_optimizer.html">torch.utils.mobile_optimizer</a></li>
<li class="toctree-l1"><a class="reference internal" href="model_zoo.html">torch.utils.model_zoo</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensorboard.html">torch.utils.tensorboard</a></li>
<li class="toctree-l1"><a class="reference internal" href="type_info.html">Type Info</a></li>
<li class="toctree-l1"><a class="reference internal" href="named_tensor.html">Named Tensors</a></li>
<li class="toctree-l1"><a class="reference internal" href="name_inference.html">Named Tensors operator coverage</a></li>
<li class="toctree-l1"><a class="reference internal" href="config_mod.html">torch.__config__</a></li>
</ul>
<p class="caption"><span class="caption-text">Libraries</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/audio/stable">torchaudio</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/data">TorchData</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/torchrec">TorchRec</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/serve">TorchServe</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/text/stable">torchtext</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/vision/stable">torchvision</a></li>
<li class="toctree-l1"><a class="reference external" href="http://pytorch.org/xla/">PyTorch on XLA Devices</a></li>
</ul>
</div>
</div>
</nav>
<div class="pytorch-container">
<div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
<div class="pytorch-breadcrumbs-wrapper">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="pytorch-breadcrumbs">
<li>
<a href="index.html">
Docs
</a> >
</li>
<li>torch.onnx</li>
<li class="pytorch-breadcrumbs-aside">
<a href="_sources/onnx.rst.txt" rel="nofollow"><img src="_static/images/view-page-source-icon.svg"></a>
</li>
</ul>
</div>
</div>
<div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
Shortcuts
</div>
</div>
<section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
<div class="pytorch-content-left">
<div class="rst-content">
<div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
<article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
<div class="section" id="torch-onnx">
<h1>torch.onnx<a class="headerlink" href="#torch-onnx" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#example-alexnet-from-pytorch-to-onnx" id="id4">Example: AlexNet from PyTorch to ONNX</a></p></li>
<li><p><a class="reference internal" href="#tracing-vs-scripting" id="id5">Tracing vs Scripting</a></p></li>
<li><p><a class="reference internal" href="#avoiding-pitfalls" id="id6">Avoiding Pitfalls</a></p>
<ul>
<li><p><a class="reference internal" href="#avoid-numpy-and-built-in-python-types" id="id7">Avoid NumPy and built-in Python types</a></p></li>
<li><p><a class="reference internal" href="#avoid-tensor-data" id="id8">Avoid Tensor.data</a></p></li>
<li><p><a class="reference internal" href="#avoid-in-place-operations-when-using-tensor-shape-in-tracing-mode" id="id9">Avoid in-place operations when using tensor.shape in tracing mode</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#limitations" id="id10">Limitations</a></p>
<ul>
<li><p><a class="reference internal" href="#types" id="id11">Types</a></p></li>
<li><p><a class="reference internal" href="#differences-in-operator-implementations" id="id12">Differences in Operator Implementations</a></p></li>
<li><p><a class="reference internal" href="#unsupported-tensor-indexing-patterns" id="id13">Unsupported Tensor Indexing Patterns</a></p>
<ul>
<li><p><a class="reference internal" href="#reads-gets" id="id14">Reads / Gets</a></p></li>
<li><p><a class="reference internal" href="#writes-sets" id="id15">Writes / Sets</a></p></li>
</ul>
</li>
</ul>
</li>
<li><p><a class="reference internal" href="#adding-support-for-operators" id="id16">Adding support for operators</a></p>
<ul>
<li><p><a class="reference internal" href="#aten-operators" id="id17">ATen operators</a></p>
<ul>
<li><p><a class="reference internal" href="#list-of-supported-operators" id="id18">List of supported operators</a></p></li>
<li><p><a class="reference internal" href="#adding-support-for-an-operator" id="id19">Adding support for an operator</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#torch-autograd-functions" id="id20">torch.autograd.Functions</a></p>
<ul>
<li><p><a class="reference internal" href="#static-symbolic-method" id="id21">Static Symbolic Method</a></p></li>
<li><p><a class="reference internal" href="#pythonop-symbolic" id="id22">PythonOp Symbolic</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#custom-operators" id="id23">Custom operators</a></p></li>
<li><p><a class="reference internal" href="#discovering-all-unconvertible-aten-ops-at-once" id="id24">Discovering all unconvertible ATen ops at once</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#frequently-asked-questions" id="id25">Frequently Asked Questions</a></p></li>
<li><p><a class="reference internal" href="#contributing-developing" id="id26">Contributing / developing</a></p></li>
<li><p><a class="reference internal" href="#functions" id="id27">Functions</a></p></li>
<li><p><a class="reference internal" href="#classes" id="id28">Classes</a></p></li>
</ul>
</div>
<span class="target" id="module-torch.onnx"></span><p><a class="reference external" href="https://onnx.ai/">Open Neural Network eXchange (ONNX)</a> is an open standard
format for representing machine learning models. The torch.onnx module can export
PyTorch models to ONNX. The model can then be consumed by any of the many
<a class="reference external" href="https://onnx.ai/supported-tools.html#deployModel">runtimes that support ONNX</a>.</p>
<div class="section" id="example-alexnet-from-pytorch-to-onnx">
<h2><a class="toc-backref" href="#id4">Example: AlexNet from PyTorch to ONNX</a><a class="headerlink" href="#example-alexnet-from-pytorch-to-onnx" title="Permalink to this headline">¶</a></h2>
<p>Here is a simple script which exports a pretrained AlexNet to an ONNX file named <code class="docutils literal notranslate"><span class="pre">alexnet.onnx</span></code>.
The call to <code class="docutils literal notranslate"><span class="pre">torch.onnx.export</span></code> runs the model once to trace its execution and then exports the
traced model to the specified file:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torchvision</span>
<span class="n">dummy_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">224</span><span class="p">,</span> <span class="mi">224</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="s2">"cuda"</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">torchvision</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">alexnet</span><span class="p">(</span><span class="n">pretrained</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="o">.</span><span class="n">cuda</span><span class="p">()</span>
<span class="c1"># Providing input and output names sets the display names for values</span>
<span class="c1"># within the model's graph. Setting these does not change the semantics</span>
<span class="c1"># of the graph; it is only for readability.</span>
<span class="c1">#</span>
<span class="c1"># The inputs to the network consist of the flat list of inputs (i.e.</span>
<span class="c1"># the values you would pass to the forward() method) followed by the</span>
<span class="c1"># flat list of parameters. You can partially specify names, i.e. provide</span>
<span class="c1"># a list here shorter than the number of inputs to the model, and we will</span>
<span class="c1"># only set that subset of names, starting from the beginning.</span>
<span class="n">input_names</span> <span class="o">=</span> <span class="p">[</span> <span class="s2">"actual_input_1"</span> <span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="s2">"learned_</span><span class="si">%d</span><span class="s2">"</span> <span class="o">%</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span> <span class="p">]</span>
<span class="n">output_names</span> <span class="o">=</span> <span class="p">[</span> <span class="s2">"output1"</span> <span class="p">]</span>
<span class="n">torch</span><span class="o">.</span><span class="n">onnx</span><span class="o">.</span><span class="n">export</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">dummy_input</span><span class="p">,</span> <span class="s2">"alexnet.onnx"</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">input_names</span><span class="o">=</span><span class="n">input_names</span><span class="p">,</span> <span class="n">output_names</span><span class="o">=</span><span class="n">output_names</span><span class="p">)</span>
</pre></div>
</div>
<p>The resulting <code class="docutils literal notranslate"><span class="pre">alexnet.onnx</span></code> file contains a binary <a class="reference external" href="https://developers.google.com/protocol-buffers/">protocol buffer</a>
which contains both the network structure and parameters of the model you exported
(in this case, AlexNet). The argument <code class="docutils literal notranslate"><span class="pre">verbose=True</span></code> causes the
exporter to print out a human-readable representation of the model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># These are the inputs and parameters to the network, which have taken on</span>
<span class="c1"># the names we specified earlier.</span>
<span class="n">graph</span><span class="p">(</span><span class="o">%</span><span class="n">actual_input_1</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">224</span><span class="p">,</span> <span class="mi">224</span><span class="p">)</span>
<span class="o">%</span><span class="n">learned_0</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">64</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">11</span><span class="p">)</span>
<span class="o">%</span><span class="n">learned_1</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">64</span><span class="p">)</span>
<span class="o">%</span><span class="n">learned_2</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">192</span><span class="p">,</span> <span class="mi">64</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="o">%</span><span class="n">learned_3</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">192</span><span class="p">)</span>
<span class="c1"># ---- omitted for brevity ----</span>
<span class="o">%</span><span class="n">learned_14</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">4096</span><span class="p">)</span>
<span class="o">%</span><span class="n">learned_15</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span> <span class="p">{</span>
<span class="c1"># Every statement consists of some output tensors (and their types),</span>
<span class="c1"># the operator to be run (with its attributes, e.g., kernels, strides,</span>
<span class="c1"># etc.), its input tensors (%actual_input_1, %learned_0, %learned_1)</span>
<span class="o">%</span><span class="mi">17</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">64</span><span class="p">,</span> <span class="mi">55</span><span class="p">,</span> <span class="mi">55</span><span class="p">)</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Conv</span><span class="p">[</span><span class="n">dilations</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">group</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">kernel_shape</span><span class="o">=</span><span class="p">[</span><span class="mi">11</span><span class="p">,</span> <span class="mi">11</span><span class="p">],</span> <span class="n">pads</span><span class="o">=</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">strides</span><span class="o">=</span><span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">]](</span><span class="o">%</span><span class="n">actual_input_1</span><span class="p">,</span> <span class="o">%</span><span class="n">learned_0</span><span class="p">,</span> <span class="o">%</span><span class="n">learned_1</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">AlexNet</span><span class="o">/</span><span class="n">Sequential</span><span class="p">[</span><span class="n">features</span><span class="p">]</span><span class="o">/</span><span class="n">Conv2d</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="o">%</span><span class="mi">18</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">64</span><span class="p">,</span> <span class="mi">55</span><span class="p">,</span> <span class="mi">55</span><span class="p">)</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Relu</span><span class="p">(</span><span class="o">%</span><span class="mi">17</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">AlexNet</span><span class="o">/</span><span class="n">Sequential</span><span class="p">[</span><span class="n">features</span><span class="p">]</span><span class="o">/</span><span class="n">ReLU</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="o">%</span><span class="mi">19</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">64</span><span class="p">,</span> <span class="mi">27</span><span class="p">,</span> <span class="mi">27</span><span class="p">)</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">MaxPool</span><span class="p">[</span><span class="n">kernel_shape</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">pads</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">strides</span><span class="o">=</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">]](</span><span class="o">%</span><span class="mi">18</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">AlexNet</span><span class="o">/</span><span class="n">Sequential</span><span class="p">[</span><span class="n">features</span><span class="p">]</span><span class="o">/</span><span class="n">MaxPool2d</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="c1"># ---- omitted for brevity ----</span>
<span class="o">%</span><span class="mi">29</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">256</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">MaxPool</span><span class="p">[</span><span class="n">kernel_shape</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">pads</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">strides</span><span class="o">=</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">]](</span><span class="o">%</span><span class="mi">28</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">AlexNet</span><span class="o">/</span><span class="n">Sequential</span><span class="p">[</span><span class="n">features</span><span class="p">]</span><span class="o">/</span><span class="n">MaxPool2d</span><span class="p">[</span><span class="mi">12</span><span class="p">]</span>
<span class="c1"># Dynamic means that the shape is not known. This may be because of a</span>
<span class="c1"># limitation of our implementation (which we would like to fix in a</span>
<span class="c1"># future release) or shapes which are truly dynamic.</span>
<span class="o">%</span><span class="mi">30</span> <span class="p">:</span> <span class="n">Dynamic</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Shape</span><span class="p">(</span><span class="o">%</span><span class="mi">29</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">AlexNet</span>
<span class="o">%</span><span class="mi">31</span> <span class="p">:</span> <span class="n">Dynamic</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Slice</span><span class="p">[</span><span class="n">axes</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ends</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">starts</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">]](</span><span class="o">%</span><span class="mi">30</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">AlexNet</span>
<span class="o">%</span><span class="mi">32</span> <span class="p">:</span> <span class="n">Long</span><span class="p">()</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Squeeze</span><span class="p">[</span><span class="n">axes</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">]](</span><span class="o">%</span><span class="mi">31</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">AlexNet</span>
<span class="o">%</span><span class="mi">33</span> <span class="p">:</span> <span class="n">Long</span><span class="p">()</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Constant</span><span class="p">[</span><span class="n">value</span><span class="o">=</span><span class="p">{</span><span class="mi">9216</span><span class="p">}](),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">AlexNet</span>
<span class="c1"># ---- omitted for brevity ----</span>
<span class="o">%</span><span class="n">output1</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">1000</span><span class="p">)</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Gemm</span><span class="p">[</span><span class="n">alpha</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">beta</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">broadcast</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">transB</span><span class="o">=</span><span class="mi">1</span><span class="p">](</span><span class="o">%</span><span class="mi">45</span><span class="p">,</span> <span class="o">%</span><span class="n">learned_14</span><span class="p">,</span> <span class="o">%</span><span class="n">learned_15</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">AlexNet</span><span class="o">/</span><span class="n">Sequential</span><span class="p">[</span><span class="n">classifier</span><span class="p">]</span><span class="o">/</span><span class="n">Linear</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span>
<span class="k">return</span> <span class="p">(</span><span class="o">%</span><span class="n">output1</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You can also verify the output using the <a class="reference external" href="https://github.com/onnx/onnx/">ONNX</a> library,
which you can install using <a class="reference external" href="https://anaconda.org">conda</a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">conda</span> <span class="n">install</span> <span class="o">-</span><span class="n">c</span> <span class="n">conda</span><span class="o">-</span><span class="n">forge</span> <span class="n">onnx</span>
</pre></div>
</div>
<p>Then, you can run:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">onnx</span>
<span class="c1"># Load the ONNX model</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">onnx</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s2">"alexnet.onnx"</span><span class="p">)</span>
<span class="c1"># Check that the model is well formed</span>
<span class="n">onnx</span><span class="o">.</span><span class="n">checker</span><span class="o">.</span><span class="n">check_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="c1"># Print a human readable representation of the graph</span>
<span class="nb">print</span><span class="p">(</span><span class="n">onnx</span><span class="o">.</span><span class="n">helper</span><span class="o">.</span><span class="n">printable_graph</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">graph</span><span class="p">))</span>
</pre></div>
</div>
<p>You can also run the exported model with one of the many
<a class="reference external" href="https://onnx.ai/supported-tools.html#deployModel">runtimes that support ONNX</a>.
For example after installing <a class="reference external" href="https://www.onnxruntime.ai">ONNX Runtime</a>, you can
load and run the model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">onnxruntime</span> <span class="k">as</span> <span class="nn">ort</span>
<span class="n">ort_session</span> <span class="o">=</span> <span class="n">ort</span><span class="o">.</span><span class="n">InferenceSession</span><span class="p">(</span><span class="s2">"alexnet.onnx"</span><span class="p">)</span>
<span class="n">outputs</span> <span class="o">=</span> <span class="n">ort_session</span><span class="o">.</span><span class="n">run</span><span class="p">(</span>
<span class="kc">None</span><span class="p">,</span>
<span class="p">{</span><span class="s2">"actual_input_1"</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">224</span><span class="p">,</span> <span class="mi">224</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)},</span>
<span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">outputs</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
</pre></div>
</div>
<p>Here is a more involved <a class="reference external" href="https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html">tutorial on exporting a model and running it with ONNX Runtime</a>.</p>
</div>
<div class="section" id="tracing-vs-scripting">
<span id="id2"></span><h2><a class="toc-backref" href="#id5">Tracing vs Scripting</a><a class="headerlink" href="#tracing-vs-scripting" title="Permalink to this headline">¶</a></h2>
<p>Internally, <code class="docutils literal notranslate"><span class="pre">torch.onnx.export()</span></code> requires a <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">torch.jit.ScriptModule</span></code></a> rather than
a <a class="reference internal" href="generated/torch.nn.Module.html#torch.nn.Module" title="torch.nn.Module"><code class="xref py py-class docutils literal notranslate"><span class="pre">torch.nn.Module</span></code></a>. If the passed-in model is not already a <code class="docutils literal notranslate"><span class="pre">ScriptModule</span></code>,
<code class="docutils literal notranslate"><span class="pre">export()</span></code> will use <em>tracing</em> to convert it to one:</p>
<ul class="simple">
<li><p><strong>Tracing</strong>: If <code class="docutils literal notranslate"><span class="pre">torch.onnx.export()</span></code> is called with a Module that is not already a
<code class="docutils literal notranslate"><span class="pre">ScriptModule</span></code>, it first does the equivalent of <a class="reference internal" href="generated/torch.jit.trace.html#torch.jit.trace" title="torch.jit.trace"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.trace()</span></code></a>, which executes the model
once with the given <code class="docutils literal notranslate"><span class="pre">args</span></code> and records all operations that happen during that execution. This
means that if your model is dynamic, e.g., changes behavior depending on input data, the exported
model will <em>not</em> capture this dynamic behavior. Similarly, a trace is likely to be valid only for
a specific input size. We recommend examining the exported model and making sure the operators look
reasonable. Tracing will unroll loops and if statements, exporting a static graph that is exactly
the same as the traced run. If you want to export your model with dynamic control flow, you will
need to use <em>scripting</em>.</p></li>
<li><p><strong>Scripting</strong>: Compiling a model via scripting preserves dynamic control flow and is valid for inputs
of different sizes. To use scripting:</p>
<ul>
<li><p>Use <a class="reference internal" href="generated/torch.jit.script.html#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.script()</span></code></a> to produce a <code class="docutils literal notranslate"><span class="pre">ScriptModule</span></code>.</p></li>
<li><p>Call <code class="docutils literal notranslate"><span class="pre">torch.onnx.export()</span></code> with the <code class="docutils literal notranslate"><span class="pre">ScriptModule</span></code> as the model. The <code class="docutils literal notranslate"><span class="pre">args</span></code> are still required,
but they will be used internally only to produce example outputs, so that the types and shapes of the
outputs can be captured. No tracing will be performed.</p></li>
</ul>
</li>
</ul>
<p>See <a class="reference external" href="https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html">Introduction to TorchScript</a>
and <a class="reference external" href="jit.html">TorchScript</a> for more details, including how to compose tracing and scripting to suit the
particular requirements of different models.</p>
</div>
<div class="section" id="avoiding-pitfalls">
<h2><a class="toc-backref" href="#id6">Avoiding Pitfalls</a><a class="headerlink" href="#avoiding-pitfalls" title="Permalink to this headline">¶</a></h2>
<div class="section" id="avoid-numpy-and-built-in-python-types">
<h3><a class="toc-backref" href="#id7">Avoid NumPy and built-in Python types</a><a class="headerlink" href="#avoid-numpy-and-built-in-python-types" title="Permalink to this headline">¶</a></h3>
<p>PyTorch models can be written using NumPy or Python types and functions, but
during <a class="reference internal" href="#tracing-vs-scripting"><span class="std std-ref">tracing</span></a>, any variables of NumPy or Python
types (rather than torch.Tensor) are converted to constants, which will produce
the wrong result if those values should change depending on the inputs.</p>
<p>For example, rather than using numpy functions on numpy.ndarrays:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Bad! Will be replaced with constants during tracing.</span>
<span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>Use torch operators on torch.Tensors:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Good! Tensor operations will be captured during tracing.</span>
<span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">),</span> <span class="n">dim</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>And rather than using <a class="reference internal" href="generated/torch.Tensor.item.html#torch.Tensor.item" title="torch.Tensor.item"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.Tensor.item()</span></code></a> (which converts a Tensor to a Python
built-in number):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Bad! y.item() will be replaced with a constant during tracing.</span>
<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">item</span><span class="p">(),</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>Use torch’s support for implicit casting of single-element tensors:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Good! y will be preserved as a variable during tracing.</span>
<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="avoid-tensor-data">
<h3><a class="toc-backref" href="#id8">Avoid Tensor.data</a><a class="headerlink" href="#avoid-tensor-data" title="Permalink to this headline">¶</a></h3>
<p>Using the Tensor.data field can produce an incorrect trace and therefore an incorrect ONNX graph.
Use <a class="reference internal" href="generated/torch.Tensor.detach.html#torch.Tensor.detach" title="torch.Tensor.detach"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.Tensor.detach()</span></code></a> instead. (Work is ongoing to
<a class="reference external" href="https://github.com/pytorch/pytorch/issues/30987">remove Tensor.data entirely</a>).</p>
</div>
<div class="section" id="avoid-in-place-operations-when-using-tensor-shape-in-tracing-mode">
<h3><a class="toc-backref" href="#id9">Avoid in-place operations when using tensor.shape in tracing mode</a><a class="headerlink" href="#avoid-in-place-operations-when-using-tensor-shape-in-tracing-mode" title="Permalink to this headline">¶</a></h3>
<p>In tracing mode, shape values obtained from tensor.shape are traced as tensors,
and share the same memory. This might cause a mismatch in values of the final outputs.
As a workaround, avoid use of inplace operations in these scenarios.
For example, in the model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Model</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">states</span><span class="p">):</span>
<span class="n">batch_size</span><span class="p">,</span> <span class="n">seq_length</span> <span class="o">=</span> <span class="n">states</span><span class="o">.</span><span class="n">shape</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>
<span class="n">real_seq_length</span> <span class="o">=</span> <span class="n">seq_length</span>
<span class="n">real_seq_length</span> <span class="o">+=</span> <span class="mi">2</span>
<span class="k">return</span> <span class="n">real_seq_length</span> <span class="o">+</span> <span class="n">seq_length</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">real_seq_length</span></code> and <code class="docutils literal notranslate"><span class="pre">seq_length</span></code> share the same memory in tracing mode.
This could be avoided by rewriting the inplace operation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">real_seq_length</span> <span class="o">=</span> <span class="n">real_seq_length</span> <span class="o">+</span> <span class="mi">2</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="limitations">
<h2><a class="toc-backref" href="#id10">Limitations</a><a class="headerlink" href="#limitations" title="Permalink to this headline">¶</a></h2>
<div class="section" id="types">
<h3><a class="toc-backref" href="#id11">Types</a><a class="headerlink" href="#types" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li><p>Only torch.Tensors, numeric types that can be trivially converted to torch.Tensors (e.g. float, int),
and tuples and lists of those types are supported as model inputs or outputs. Dict and str inputs and
outputs are accepted in <a class="reference internal" href="#tracing-vs-scripting"><span class="std std-ref">tracing</span></a> mode, but:</p>
<ul>
<li><p>Any computation that depends on the value of a dict or a str input will be replaced with the
constant value seen during the one traced execution.</p></li>
<li><p>Any output that is a dict will be silently replaced with a flattened sequence of its values
(keys will be removed). E.g. <code class="docutils literal notranslate"><span class="pre">{"foo":</span> <span class="pre">1,</span> <span class="pre">"bar":</span> <span class="pre">2}</span></code> becomes <code class="docutils literal notranslate"><span class="pre">(1,</span> <span class="pre">2)</span></code>.</p></li>
<li><p>Any output that is a str will be silently removed.</p></li>
</ul>
</li>
<li><p>Certain operations involving tuples and lists are not supported in
<a class="reference internal" href="#tracing-vs-scripting"><span class="std std-ref">scripting</span></a> mode due to limited support in ONNX for nested sequences.
In particular appending a tuple to a list is not supported. In tracing mode, the nested sequences
will be flattened automatically during the tracing.</p></li>
</ul>
</div>
<div class="section" id="differences-in-operator-implementations">
<h3><a class="toc-backref" href="#id12">Differences in Operator Implementations</a><a class="headerlink" href="#differences-in-operator-implementations" title="Permalink to this headline">¶</a></h3>
<p>Due to differences in implementations of operators, running the exported model on different runtimes
may produce different results from each other or from PyTorch. Normally these differences are
numerically small, so this should only be a concern if your application is sensitive to these
small differences.</p>
</div>
<div class="section" id="unsupported-tensor-indexing-patterns">
<span id="tensor-indexing"></span><h3><a class="toc-backref" href="#id13">Unsupported Tensor Indexing Patterns</a><a class="headerlink" href="#unsupported-tensor-indexing-patterns" title="Permalink to this headline">¶</a></h3>
<p>Tensor indexing patterns that cannot be exported are listed below.
If you are experiencing issues exporting a model that does not include any of
the unsupported patterns below, please double check that you are exporting with
the latest <code class="docutils literal notranslate"><span class="pre">opset_version</span></code>.</p>
<div class="section" id="reads-gets">
<h4><a class="toc-backref" href="#id14">Reads / Gets</a><a class="headerlink" href="#reads-gets" title="Permalink to this headline">¶</a></h4>
<p>When indexing into a tensor for reading, the following patterns are not supported:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Tensor indices that includes negative values.</span>
<span class="n">data</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">]]),</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])]</span>
<span class="c1"># Workarounds: use positive index values.</span>
</pre></div>
</div>
</div>
<div class="section" id="writes-sets">
<h4><a class="toc-backref" href="#id15">Writes / Sets</a><a class="headerlink" href="#writes-sets" title="Permalink to this headline">¶</a></h4>
<p>When indexing into a Tensor for writing, the following patterns are not supported:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Multiple tensor indices if any has rank >= 2</span>
<span class="n">data</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]]),</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])]</span> <span class="o">=</span> <span class="n">new_data</span>
<span class="c1"># Workarounds: use single tensor index with rank >= 2,</span>
<span class="c1"># or multiple consecutive tensor indices with rank == 1.</span>
<span class="c1"># Multiple tensor indices that are not consecutive</span>
<span class="n">data</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]),</span> <span class="p">:,</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])]</span> <span class="o">=</span> <span class="n">new_data</span>
<span class="c1"># Workarounds: transpose `data` such that tensor indices are consecutive.</span>
<span class="c1"># Tensor indices that includes negative values.</span>
<span class="n">data</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">]),</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])]</span> <span class="o">=</span> <span class="n">new_data</span>
<span class="c1"># Workarounds: use positive index values.</span>
<span class="c1"># Implicit broadcasting required for new_data.</span>
<span class="n">data</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]),</span> <span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_data</span>
<span class="c1"># Workarounds: expand new_data explicitly.</span>
<span class="c1"># Example:</span>
<span class="c1"># data shape: [3, 4, 5]</span>
<span class="c1"># new_data shape: [5]</span>
<span class="c1"># expected new_data shape after broadcasting: [2, 2, 2, 5]</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="adding-support-for-operators">
<h2><a class="toc-backref" href="#id16">Adding support for operators</a><a class="headerlink" href="#adding-support-for-operators" title="Permalink to this headline">¶</a></h2>
<p>When exporting a model that includes unsupported operators, you’ll see an error message like:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>RuntimeError: ONNX export failed: Couldn't export operator foo
</pre></div>
</div>
<p>When that happens, you’ll need to either change the model to not use that operator,
or add support for the operator.</p>
<p>Adding support for operators requires contributing a change to PyTorch’s source code.
See <a class="reference external" href="https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md">CONTRIBUTING</a>
for general instructions on that, and below for specific instructions on the code
changes required for supporting an operator.</p>
<p>During export, each node in the TorchScript graph is visited in topological order.
Upon visiting a node, the exporter tries to find a registered symbolic functions for
that node. Symbolic functions are implemented in Python. A symbolic function for
an op named <code class="docutils literal notranslate"><span class="pre">foo</span></code> would look something like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span>
<span class="n">g</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">Graph</span><span class="p">,</span>
<span class="n">input_0</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">Value</span><span class="p">,</span>
<span class="n">input_1</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">Value</span><span class="p">)</span> <span class="o">-></span> <span class="n">Union</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">Value</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">Value</span><span class="p">]]:</span>
<span class="sd">"""</span>
<span class="sd"> Modifies g (e.g., using "g.op()"), adding the ONNX operations representing</span>
<span class="sd"> this PyTorch function.</span>
<span class="sd"> Args:</span>
<span class="sd"> g (Graph): graph to write the ONNX representation into.</span>
<span class="sd"> input_0 (Value): value representing the variables which contain</span>
<span class="sd"> the first input for this operator.</span>
<span class="sd"> input_1 (Value): value representing the variables which contain</span>
<span class="sd"> the second input for this operator.</span>
<span class="sd"> Returns:</span>
<span class="sd"> A Value or List of Values specifying the ONNX nodes that compute something</span>
<span class="sd"> equivalent to the original PyTorch operator with the given inputs.</span>
<span class="sd"> Returns None if it cannot be converted to ONNX.</span>
<span class="sd"> """</span>
<span class="o">...</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">torch._C</span></code> types are Python wrappers around the types defined in C++ in
<a class="reference external" href="https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/ir/ir.h">ir.h</a>.</p>
<p>The process for adding a symbolic function depends on the type of operator.</p>
<div class="section" id="aten-operators">
<span id="adding-support-aten"></span><h3><a class="toc-backref" href="#id17">ATen operators</a><a class="headerlink" href="#aten-operators" title="Permalink to this headline">¶</a></h3>
<p><a class="reference external" href="https://pytorch.org/cppdocs/#aten">ATen</a> is PyTorch’s built-in tensor library.
If the operator is an ATen operator (shows up in the TorchScript graph with the prefix
<code class="docutils literal notranslate"><span class="pre">aten::</span></code>), make sure it is not supported already.</p>
<div class="section" id="list-of-supported-operators">
<h4><a class="toc-backref" href="#id18">List of supported operators</a><a class="headerlink" href="#list-of-supported-operators" title="Permalink to this headline">¶</a></h4>
<p>Visit the auto generated <a class="reference internal" href="onnx_supported_aten_ops.html"><span class="doc">list of supported ATen operators</span></a>
for details on which operator are supported in each <code class="docutils literal notranslate"><span class="pre">opset_version</span></code>.</p>
</div>
<div class="section" id="adding-support-for-an-operator">
<h4><a class="toc-backref" href="#id19">Adding support for an operator</a><a class="headerlink" href="#adding-support-for-an-operator" title="Permalink to this headline">¶</a></h4>
<p>If the operator is not in the list above:</p>
<ul class="simple">
<li><p>Define the symbolic function in <code class="docutils literal notranslate"><span class="pre">torch/onnx/symbolic_opset<version>.py</span></code>, for example
<a class="reference external" href="https://github.com/pytorch/pytorch/blob/master/torch/onnx/symbolic_opset9.py">torch/onnx/symbolic_opset9.py</a>.
Make sure the function has the same name as the ATen function, which may be declared in
<code class="docutils literal notranslate"><span class="pre">torch/_C/_VariableFunctions.pyi</span></code> or <code class="docutils literal notranslate"><span class="pre">torch/nn/functional.pyi</span></code> (these files are generated at
build time, so will not appear in your checkout until you build PyTorch).</p></li>
<li><p>By default, the first arg is the ONNX graph.
Other arg names must EXACTLY match the names in the <code class="docutils literal notranslate"><span class="pre">.pyi</span></code> file,
because dispatch is done with keyword arguments.</p></li>
<li><p>A symbolic function that has a first arg (before the Graph object) with the
type annotation of torch.onnx.SymbolicContext will be called with that additional context.
See examples below.</p></li>
<li><p>In the symbolic function, if the operator is in the
<a class="reference external" href="https://github.com/onnx/onnx/blob/master/docs/Operators.md">ONNX standard operator set</a>,
we only need to create a node to represent the ONNX operator in the graph.
If not, we can create a graph of several standard operators that have
equivalent semantics to the ATen operator.</p></li>
<li><p>If an input argument is a Tensor, but ONNX asks for a scalar, we have to
explicitly do the conversion. <code class="xref py py-func docutils literal notranslate"><span class="pre">symbolic_helper._scalar()</span></code> can convert a
scalar tensor into a python scalar, and
<code class="xref py py-func docutils literal notranslate"><span class="pre">symbolic_helper._if_scalar_type_as()</span></code> can turn a Python scalar into a
PyTorch tensor.</p></li>
</ul>
<p>Here is an example of handling missing symbolic function for the <code class="docutils literal notranslate"><span class="pre">ELU</span></code> operator.</p>
<p>If we run the following code:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">ELU</span><span class="p">(),</span> <span class="c1"># module</span>
<span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># example input</span>
<span class="p">)</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
</pre></div>
</div>
<p>We see something like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">graph</span><span class="p">(</span><span class="o">%</span><span class="bp">self</span> <span class="p">:</span> <span class="n">__torch__</span><span class="o">.</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">modules</span><span class="o">.</span><span class="n">activation</span><span class="o">.</span><span class="n">___torch_mangle_0</span><span class="o">.</span><span class="n">ELU</span><span class="p">,</span>
<span class="o">%</span><span class="nb">input</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">strides</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">requires_grad</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">cpu</span><span class="p">)):</span>
<span class="o">%</span><span class="mi">4</span> <span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="n">prim</span><span class="p">::</span><span class="n">Constant</span><span class="p">[</span><span class="n">value</span><span class="o">=</span><span class="mf">1.</span><span class="p">]()</span>
<span class="o">%</span><span class="mi">5</span> <span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">prim</span><span class="p">::</span><span class="n">Constant</span><span class="p">[</span><span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">]()</span>
<span class="o">%</span><span class="mi">6</span> <span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">prim</span><span class="p">::</span><span class="n">Constant</span><span class="p">[</span><span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">]()</span>
<span class="o">%</span><span class="mi">7</span> <span class="p">:</span> <span class="n">Float</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">strides</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">requires_grad</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">cpu</span><span class="p">)</span> <span class="o">=</span> <span class="n">aten</span><span class="p">::</span><span class="n">elu</span><span class="p">(</span><span class="o">%</span><span class="nb">input</span><span class="p">,</span> <span class="o">%</span><span class="mi">4</span><span class="p">,</span> <span class="o">%</span><span class="mi">5</span><span class="p">,</span> <span class="o">%</span><span class="mi">6</span><span class="p">)</span>
<span class="k">return</span> <span class="p">(</span><span class="o">%</span><span class="mi">7</span><span class="p">)</span>
</pre></div>
</div>
<p>Since we see <code class="docutils literal notranslate"><span class="pre">aten::elu</span></code> in the graph, we know this is an ATen operator.</p>
<p>We check the <a class="reference external" href="https://github.com/onnx/onnx/blob/master/docs/Operators.md">ONNX operator list</a>,
and confirm that <code class="docutils literal notranslate"><span class="pre">Elu</span></code> is standardized in ONNX.</p>
<p>We find a signature for <code class="docutils literal notranslate"><span class="pre">elu</span></code> in <code class="docutils literal notranslate"><span class="pre">torch/nn/functional.pyi</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">elu</span><span class="p">(</span><span class="nb">input</span><span class="p">:</span> <span class="n">Tensor</span><span class="p">,</span> <span class="n">alpha</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="o">...</span><span class="p">,</span> <span class="n">inplace</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="o">...</span><span class="p">)</span> <span class="o">-></span> <span class="n">Tensor</span><span class="p">:</span> <span class="o">...</span>
</pre></div>
</div>
<p>We add the following lines to <code class="docutils literal notranslate"><span class="pre">symbolic_opset9.py</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">elu</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="nb">input</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="k">return</span> <span class="n">g</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="s2">"Elu"</span><span class="p">,</span> <span class="nb">input</span><span class="p">,</span> <span class="n">alpha_f</span><span class="o">=</span><span class="n">_scalar</span><span class="p">(</span><span class="n">alpha</span><span class="p">))</span>
</pre></div>
</div>
<p>Now PyTorch is able to export models containing the <code class="docutils literal notranslate"><span class="pre">aten::elu</span></code> operator!</p>
<p>See the <code class="docutils literal notranslate"><span class="pre">symbolic_opset*.py</span></code> files for more examples.</p>
</div>
</div>
<div class="section" id="torch-autograd-functions">
<h3><a class="toc-backref" href="#id20">torch.autograd.Functions</a><a class="headerlink" href="#torch-autograd-functions" title="Permalink to this headline">¶</a></h3>
<p>If the operator is a sub-class of <a class="reference internal" href="autograd.html#torch.autograd.Function" title="torch.autograd.Function"><code class="xref py py-class docutils literal notranslate"><span class="pre">torch.autograd.Function</span></code></a>, there are two ways
to export it.</p>
<div class="section" id="static-symbolic-method">
<h4><a class="toc-backref" href="#id21">Static Symbolic Method</a><a class="headerlink" href="#static-symbolic-method" title="Permalink to this headline">¶</a></h4>
<p>You can add a static method named <code class="docutils literal notranslate"><span class="pre">symbolic</span></code> to your function class. It should return
ONNX operators that represent the function’s behavior in ONNX. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyRelu</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">autograd</span><span class="o">.</span><span class="n">Function</span><span class="p">):</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="nb">input</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">)</span> <span class="o">-></span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">:</span>
<span class="n">ctx</span><span class="o">.</span><span class="n">save_for_backward</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">input</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">symbolic</span><span class="p">(</span><span class="n">g</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">graph</span><span class="p">,</span> <span class="nb">input</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">Value</span><span class="p">)</span> <span class="o">-></span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">Value</span><span class="p">:</span>
<span class="k">return</span> <span class="n">g</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="s2">"Clip"</span><span class="p">,</span> <span class="nb">input</span><span class="p">,</span> <span class="n">g</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="s2">"Constant"</span><span class="p">,</span> <span class="n">value_t</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">float</span><span class="p">)))</span>
</pre></div>
</div>
</div>
<div class="section" id="pythonop-symbolic">
<h4><a class="toc-backref" href="#id22">PythonOp Symbolic</a><a class="headerlink" href="#pythonop-symbolic" title="Permalink to this headline">¶</a></h4>
<p>Alternatively, you can register a custom symbolic function.
This gives the symbolic function access to more info through the
<code class="docutils literal notranslate"><span class="pre">torch.onnx.SymbolicContext</span></code> object, which gets passed in as the first
argument (before the <code class="docutils literal notranslate"><span class="pre">Graph</span></code> object).</p>
<p>All autograd <code class="docutils literal notranslate"><span class="pre">Function</span></code>s appear in the TorchScript graph as <code class="docutils literal notranslate"><span class="pre">prim::PythonOp</span></code> nodes.
In order to differentiate between different <code class="docutils literal notranslate"><span class="pre">Function</span></code> subclasses, the
symbolic function should use the <code class="docutils literal notranslate"><span class="pre">name</span></code> kwarg which gets set to the name of the class.</p>
<p>Custom symbolic functions should add type and shape information by calling <code class="docutils literal notranslate"><span class="pre">setType(...)</span></code>
on Value objects before returning them (implemented in C++ by
<code class="docutils literal notranslate"><span class="pre">torch::jit::Value::setType</span></code>). This is not required, but it can help the exporter’s
shape and type inference for down-stream nodes. For a non-trivial example of <code class="docutils literal notranslate"><span class="pre">setType</span></code>, see
<code class="docutils literal notranslate"><span class="pre">test_aten_embedding_2</span></code> in
<a class="reference external" href="https://github.com/pytorch/pytorch/blob/master/test/onnx/test_operators.py">test_operators.py</a>.</p>
<p>The example below shows how you can access <code class="docutils literal notranslate"><span class="pre">requires_grad</span></code> via the <code class="docutils literal notranslate"><span class="pre">Node</span></code> object:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyClip</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">autograd</span><span class="o">.</span><span class="n">Function</span><span class="p">):</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="nb">input</span><span class="p">,</span> <span class="nb">min</span><span class="p">):</span>
<span class="n">ctx</span><span class="o">.</span><span class="n">save_for_backward</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">input</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="nb">min</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MyRelu</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">autograd</span><span class="o">.</span><span class="n">Function</span><span class="p">):</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
<span class="n">ctx</span><span class="o">.</span><span class="n">save_for_backward</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">input</span><span class="o">.</span><span class="n">clamp</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">symbolic_python_op</span><span class="p">(</span><span class="n">ctx</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">onnx</span><span class="o">.</span><span class="n">SymbolicContext</span><span class="p">,</span> <span class="n">g</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">_C</span><span class="o">.</span><span class="n">Graph</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">n</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">cur_node</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"original node: "</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">out</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">n</span><span class="o">.</span><span class="n">outputs</span><span class="p">()):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"original output </span><span class="si">{}</span><span class="s2">: </span><span class="si">{}</span><span class="s2">, requires grad: </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="n">out</span><span class="o">.</span><span class="n">requiresGrad</span><span class="p">()))</span>
<span class="kn">import</span> <span class="nn">torch.onnx.symbolic_helper</span> <span class="k">as</span> <span class="nn">sym_helper</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">arg</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">args</span><span class="p">):</span>
<span class="n">requires_grad</span> <span class="o">=</span> <span class="n">arg</span><span class="o">.</span><span class="n">requiresGrad</span><span class="p">()</span> <span class="k">if</span> <span class="n">sym_helper</span><span class="o">.</span><span class="n">_is_value</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="k">else</span> <span class="kc">False</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"arg </span><span class="si">{}</span><span class="s2">: </span><span class="si">{}</span><span class="s2">, requires grad: </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">arg</span><span class="p">,</span> <span class="n">requires_grad</span><span class="p">))</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s2">"name"</span><span class="p">]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s2">"MyClip"</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="s2">"Clip"</span><span class="p">,</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="k">elif</span> <span class="n">name</span> <span class="o">==</span> <span class="s2">"MyRelu"</span><span class="p">:</span>
<span class="n">ret</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="s2">"Relu"</span><span class="p">,</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># Logs a warning and returns None</span>
<span class="k">return</span> <span class="n">_unimplemented</span><span class="p">(</span><span class="s2">"prim::PythonOp"</span><span class="p">,</span> <span class="s2">"unknown node kind: "</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span>
<span class="c1"># Copy type and shape from original node.</span>
<span class="n">ret</span><span class="o">.</span><span class="n">setType</span><span class="p">(</span><span class="n">n</span><span class="o">.</span><span class="n">type</span><span class="p">())</span>
<span class="k">return</span> <span class="n">ret</span>
<span class="kn">from</span> <span class="nn">torch.onnx</span> <span class="kn">import</span> <span class="n">register_custom_op_symbolic</span>
<span class="n">register_custom_op_symbolic</span><span class="p">(</span><span class="s2">"prim::PythonOp"</span><span class="p">,</span> <span class="n">symbolic_python_op</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="custom-operators">
<h3><a class="toc-backref" href="#id23">Custom operators</a><a class="headerlink" href="#custom-operators" title="Permalink to this headline">¶</a></h3>
<p>If a model uses a custom operator implemented in C++ as described in
<a class="reference external" href="https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html">Extending TorchScript with Custom C++ Operators</a>,
you can export it by following this example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">torch.onnx</span> <span class="kn">import</span> <span class="n">register_custom_op_symbolic</span>
<span class="kn">from</span> <span class="nn">torch.onnx.symbolic_helper</span> <span class="kn">import</span> <span class="n">parse_args</span>
<span class="c1"># Define custom symbolic function</span>
<span class="nd">@parse_args</span><span class="p">(</span><span class="s2">"v"</span><span class="p">,</span> <span class="s2">"v"</span><span class="p">,</span> <span class="s2">"f"</span><span class="p">,</span> <span class="s2">"i"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">symbolic_foo_forward</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">input1</span><span class="p">,</span> <span class="n">input2</span><span class="p">,</span> <span class="n">attr1</span><span class="p">,</span> <span class="n">attr2</span><span class="p">):</span>
<span class="k">return</span> <span class="n">g</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="s2">"custom_domain::Foo"</span><span class="p">,</span> <span class="n">input1</span><span class="p">,</span> <span class="n">input2</span><span class="p">,</span> <span class="n">attr1_f</span><span class="o">=</span><span class="n">attr1</span><span class="p">,</span> <span class="n">attr2_i</span><span class="o">=</span><span class="n">attr2</span><span class="p">)</span>
<span class="c1"># Register custom symbolic function</span>
<span class="n">register_custom_op_symbolic</span><span class="p">(</span><span class="s2">"custom_ops::foo_forward"</span><span class="p">,</span> <span class="n">symbolic_foo_forward</span><span class="p">,</span> <span class="mi">9</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">FooModel</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr1</span><span class="p">,</span> <span class="n">attr2</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">FooModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">attr1</span> <span class="o">=</span> <span class="n">attr1</span>
<span class="bp">self</span><span class="o">.</span><span class="n">attr2</span> <span class="o">=</span> <span class="n">attr2</span>
<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input1</span><span class="p">,</span> <span class="n">input2</span><span class="p">):</span>
<span class="c1"># Calling custom op</span>
<span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">custom_ops</span><span class="o">.</span><span class="n">foo_forward</span><span class="p">(</span><span class="n">input1</span><span class="p">,</span> <span class="n">input2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">attr1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">attr2</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">FooModel</span><span class="p">(</span><span class="n">attr1</span><span class="p">,</span> <span class="n">attr2</span><span class="p">)</span>
<span class="n">torch</span><span class="o">.</span><span class="n">onnx</span><span class="o">.</span><span class="n">export</span><span class="p">(</span>
<span class="n">model</span><span class="p">,</span>
<span class="p">(</span><span class="n">example_input1</span><span class="p">,</span> <span class="n">example_input1</span><span class="p">),</span>
<span class="s2">"model.onnx"</span><span class="p">,</span>
<span class="c1"># only needed if you want to specify an opset version > 1.</span>
<span class="n">custom_opsets</span><span class="o">=</span><span class="p">{</span><span class="s2">"custom_domain"</span><span class="p">:</span> <span class="mi">2</span><span class="p">})</span>
</pre></div>
</div>
<p>You can export it as one or a combination of standard ONNX ops, or as a custom operator.
The example above exports it as a custom operator in the “custom_domain” opset.
When exporting a custom operator, you can specify the custom domain version using the
<code class="docutils literal notranslate"><span class="pre">custom_opsets</span></code> dictionary at export. If not specified, the custom opset version defaults to 1.
The runtime that consumes the model needs to support the custom op. See
<a class="reference external" href="https://caffe2.ai/docs/custom-operators.html">Caffe2 custom ops</a>,
<a class="reference external" href="https://onnxruntime.ai/docs/reference/operators/add-custom-op.html">ONNX Runtime custom ops</a>,
or your runtime of choice’s documentation.</p>
</div>
<div class="section" id="discovering-all-unconvertible-aten-ops-at-once">
<h3><a class="toc-backref" href="#id24">Discovering all unconvertible ATen ops at once</a><a class="headerlink" href="#discovering-all-unconvertible-aten-ops-at-once" title="Permalink to this headline">¶</a></h3>
<p>When export fails due to an unconvertible ATen op, there may in fact be more
than one such op but the error message only mentions the first. To discover
all of the unconvertible ops in one go you can:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">torch.onnx</span> <span class="kn">import</span> <span class="n">utils</span> <span class="k">as</span> <span class="n">onnx_utils</span>
<span class="c1"># prepare model, args, opset_version</span>
<span class="o">...</span>
<span class="n">torch_script_graph</span><span class="p">,</span> <span class="n">unconvertible_ops</span> <span class="o">=</span> <span class="n">onnx_utils</span><span class="o">.</span><span class="n">unconvertible_ops</span><span class="p">(</span>
<span class="n">model</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">opset_version</span><span class="o">=</span><span class="n">opset_version</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">unconvertible_ops</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="frequently-asked-questions">
<h2><a class="toc-backref" href="#id25">Frequently Asked Questions</a><a class="headerlink" href="#frequently-asked-questions" title="Permalink to this headline">¶</a></h2>
<p>Q: I have exported my LSTM model, but its input size seems to be fixed?</p>
<blockquote>
<div><p>The tracer records the shapes of the example inputs. If the model should accept
inputs of dynamic shapes, set <code class="docutils literal notranslate"><span class="pre">dynamic_axes</span></code> when calling <a class="reference internal" href="#torch.onnx.export" title="torch.onnx.export"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.onnx.export()</span></code></a>.</p>
</div></blockquote>
<p>Q: How to export models containing loops?</p>
<blockquote>
<div><p>See <a class="reference internal" href="#id2">Tracing vs Scripting</a>.</p>
</div></blockquote>
<p>Q: How to export models with primitive type inputs (e.g. int, float)?</p>
<blockquote>
<div><p>Support for primitive numeric type inputs was added in PyTorch 1.9.
However, the exporter does not support models with str inputs.</p>
</div></blockquote>
<p>Q: Does ONNX support implicit scalar datatype casting?</p>
<blockquote>
<div><p>No, but the exporter will try to handle that part. Scalars are exported as constant tensors.
The exporter will try to figure out the right datatype for scalars. However when it is unable
to do so, you will need to manually specify the datatype. This often happens with
scripted models, where the datatypes are not recorded. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ImplicitCastType</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">ScriptModule</span><span class="p">):</span>