forked from pytorch/pytorch.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonnx.html
2148 lines (1874 loc) · 202 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.8.1 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="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="stylesheet" href="_static/css/jit.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="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="torch.optim" href="optim.html" />
<link rel="prev" title="torch.nn.init" href="nn.init.html" />
<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">
<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/elastic/">
<span class="dropdown-title">TorchElastic</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/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.8.1 ▼</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"><span class="caption-text">Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="notes/amp_examples.html">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/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/multiprocessing.html">Multiprocessing best practices</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>
</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="autograd.html">torch.autograd</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="amp.html">torch.cuda.amp</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="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="torch.overrides.html">torch.overrides</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="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="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__.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/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="https://pytorch.org/elastic/">TorchElastic</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="http://pytorch.org/xla/">PyTorch on XLA Devices</a></li>
</ul>
<p class="caption"><span class="caption-text">Community</span></p>
<ul>
<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/governance.html">PyTorch Governance</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/persons_of_interest.html">PyTorch Governance | Persons of Interest</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-end-to-end-alexnet-from-pytorch-to-onnx" id="id3">Example: End-to-end AlexNet from PyTorch to ONNX</a></p></li>
<li><p><a class="reference internal" href="#tracing-vs-scripting" id="id4">Tracing vs Scripting</a></p></li>
<li><p><a class="reference internal" href="#write-pytorch-model-in-torch-way" id="id5">Write PyTorch model in Torch way</a></p></li>
<li><p><a class="reference internal" href="#using-dictionaries-to-handle-named-arguments-as-model-inputs" id="id6">Using dictionaries to handle Named Arguments as model inputs</a></p></li>
<li><p><a class="reference internal" href="#indexing" id="id7">Indexing</a></p>
<ul>
<li><p><a class="reference internal" href="#getter" id="id8">Getter</a></p></li>
<li><p><a class="reference internal" href="#setter" id="id9">Setter</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#torchvision-support" id="id10">TorchVision support</a></p></li>
<li><p><a class="reference internal" href="#limitations" id="id11">Limitations</a></p></li>
<li><p><a class="reference internal" href="#supported-operators" id="id12">Supported operators</a></p></li>
<li><p><a class="reference internal" href="#adding-support-for-operators" id="id13">Adding support for operators</a></p>
<ul>
<li><p><a class="reference internal" href="#aten-operators" id="id14">ATen operators</a></p></li>
<li><p><a class="reference internal" href="#non-aten-operators" id="id15">Non-ATen operators</a></p></li>
<li><p><a class="reference internal" href="#custom-operators" id="id16">Custom operators</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#operator-export-type" id="id17">Operator Export Type</a></p>
<ul>
<li><p><a class="reference internal" href="#id2" id="id18">ONNX</a></p></li>
<li><p><a class="reference internal" href="#onnx-aten" id="id19">ONNX_ATEN</a></p></li>
<li><p><a class="reference internal" href="#onnx-aten-fallback" id="id20">ONNX_ATEN_FALLBACK</a></p></li>
<li><p><a class="reference internal" href="#raw" id="id21">RAW</a></p></li>
<li><p><a class="reference internal" href="#onnx-fallthrough" id="id22">ONNX_FALLTHROUGH</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#frequently-asked-questions" id="id23">Frequently Asked Questions</a></p></li>
<li><p><a class="reference internal" href="#use-external-data-format" id="id24">Use external data format</a></p></li>
<li><p><a class="reference internal" href="#training" id="id25">Training</a></p></li>
<li><p><a class="reference internal" href="#functions" id="id26">Functions</a></p></li>
</ul>
</div>
<span class="target" id="module-torch.onnx"></span><div class="section" id="example-end-to-end-alexnet-from-pytorch-to-onnx">
<h2><a class="toc-backref" href="#id3">Example: End-to-end AlexNet from PyTorch to ONNX</a><a class="headerlink" href="#example-end-to-end-alexnet-from-pytorch-to-onnx" title="Permalink to this headline">¶</a></h2>
<p>Here is a simple script which exports a pretrained AlexNet as defined in
torchvision into ONNX. It runs a single round of inference and then
saves the resulting traced model to <code class="docutils literal notranslate"><span class="pre">alexnet.onnx</span></code>:</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="s1">'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> is a binary protobuf file which contains both
the network structure and parameters of the model you exported
(in this case, AlexNet). The keyword 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 network:</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 protobuf using the <a class="reference external" href="https://github.com/onnx/onnx/">ONNX</a> library.
You can install <code class="docutils literal notranslate"><span class="pre">ONNX</span></code> with conda:</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 IR 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="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>To run the exported script with <a class="reference external" href="https://caffe2.ai/">caffe2</a>, you will need to install <cite>caffe2</cite>: If you don’t have one already, Please <a class="reference external" href="https://caffe2.ai/docs/getting-started.html">follow the install instructions</a>.</p>
<p>Once these are installed, you can use the backend for Caffe2:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># ...continuing from above</span>
<span class="kn">import</span> <span class="nn">caffe2.python.onnx.backend</span> <span class="k">as</span> <span class="nn">backend</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">rep</span> <span class="o">=</span> <span class="n">backend</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="s2">"CUDA:0"</span><span class="p">)</span> <span class="c1"># or "CPU"</span>
<span class="c1"># For the Caffe2 backend:</span>
<span class="c1"># rep.predict_net is the Caffe2 protobuf for the network</span>
<span class="c1"># rep.workspace is the Caffe2 workspace for the network</span>
<span class="c1"># (see the class caffe2.python.onnx.backend.Workspace)</span>
<span class="n">outputs</span> <span class="o">=</span> <span class="n">rep</span><span class="o">.</span><span class="n">run</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="c1"># To run networks with more than one input, pass a tuple</span>
<span class="c1"># rather than a single numpy ndarray.</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>You can also run the exported model with <a class="reference external" href="https://github.com/microsoft/onnxruntime">ONNX Runtime</a>,
you will need to install <cite>ONNX Runtime</cite>: please <a class="reference external" href="https://github.com/microsoft/onnxruntime#installation">follow these instructions</a>.</p>
<p>Once these are installed, you can use the backend for ONNX Runtime:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># ...continuing from above</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="s1">'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="s1">'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="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 another <a class="reference external" href="https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html">tutorial of exporting the SuperResolution model to ONNX.</a>.</p>
<p>In the future, there will be backends for other frameworks as well.</p>
</div>
<div class="section" id="tracing-vs-scripting">
<h2><a class="toc-backref" href="#id4">Tracing vs Scripting</a><a class="headerlink" href="#tracing-vs-scripting" title="Permalink to this headline">¶</a></h2>
<p>The ONNX exporter can be both <em>trace-based</em> and <em>script-based</em> exporter.</p>
<ul class="simple">
<li><p><em>trace-based</em> means that it operates by executing your model once, and exporting the operators which
were actually run during this run. This means that if your model is
dynamic, e.g., changes behavior depending on input data, the export
won’t be accurate. Similarly, a trace is likely to be valid only
for a specific input size (which is one reason why we require explicit inputs
on tracing.) We recommend examining the model trace and making sure
the traced operators look reasonable. If your model contains control flows like
for loops and if conditions, <em>trace-based</em> exporter will unroll the loops and if conditions,
exporting a static graph that is exactly the same as this run. If you want
to export your model with dynamic control flows, you will need to use the <em>script-based</em> exporter.</p></li>
<li><p><em>script-based</em> means that the model you are trying to export is a <a class="reference external" href="jit.html">ScriptModule</a>.
<cite>ScriptModule</cite> is the core data structure in <cite>TorchScript</cite>, and <cite>TorchScript</cite> is a subset of Python language,
that creates serializable and optimizable models from PyTorch code.</p></li>
</ul>
<p>We allow mixing tracing and scripting. You can compose tracing and scripting to suit the particular requirements
of a part of a model. Checkout this example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="c1"># Trace-based only</span>
<span class="k">class</span> <span class="nc">LoopModel</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">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</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="n">y</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">i</span>
<span class="k">return</span> <span class="n">x</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">LoopModel</span><span class="p">()</span>
<span class="n">dummy_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">ones</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">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">long</span><span class="p">)</span>
<span class="n">loop_count</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">5</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">long</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">dummy_input</span><span class="p">,</span> <span class="n">loop_count</span><span class="p">),</span> <span class="s1">'loop.onnx'</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>With <em>trace-based</em> exporter, we get the result ONNX graph which unrolls the for loop:</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="mi">0</span> <span class="p">:</span> <span class="n">Long</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="mi">1</span> <span class="p">:</span> <span class="n">Long</span><span class="p">()):</span>
<span class="o">%</span><span class="mi">2</span> <span class="p">:</span> <span class="n">Tensor</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">1</span><span class="p">}]()</span>
<span class="o">%</span><span class="mi">3</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Add</span><span class="p">(</span><span class="o">%</span><span class="mi">0</span><span class="p">,</span> <span class="o">%</span><span class="mi">2</span><span class="p">)</span>
<span class="o">%</span><span class="mi">4</span> <span class="p">:</span> <span class="n">Tensor</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">2</span><span class="p">}]()</span>
<span class="o">%</span><span class="mi">5</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Add</span><span class="p">(</span><span class="o">%</span><span class="mi">3</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">6</span> <span class="p">:</span> <span class="n">Tensor</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">3</span><span class="p">}]()</span>
<span class="o">%</span><span class="mi">7</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Add</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="o">%</span><span class="mi">8</span> <span class="p">:</span> <span class="n">Tensor</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">4</span><span class="p">}]()</span>
<span class="o">%</span><span class="mi">9</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Add</span><span class="p">(</span><span class="o">%</span><span class="mi">7</span><span class="p">,</span> <span class="o">%</span><span class="mi">8</span><span class="p">)</span>
<span class="k">return</span> <span class="p">(</span><span class="o">%</span><span class="mi">9</span><span class="p">)</span>
</pre></div>
</div>
<p>To utilize <em>script-based</em> exporter for capturing the dynamic loop,
we can write the loop in script, and call it from the regular nn.Module:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Mixing tracing and scripting</span>
<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">loop</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">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">y</span><span class="p">)):</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">i</span>
<span class="k">return</span> <span class="n">x</span>
<span class="k">class</span> <span class="nc">LoopModel2</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">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">return</span> <span class="n">loop</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">model</span> <span class="o">=</span> <span class="n">LoopModel2</span><span class="p">()</span>
<span class="n">dummy_input</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">ones</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">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">long</span><span class="p">)</span>
<span class="n">loop_count</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">5</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">long</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">dummy_input</span><span class="p">,</span> <span class="n">loop_count</span><span class="p">),</span> <span class="s1">'loop.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="p">[</span><span class="s1">'input_data'</span><span class="p">,</span> <span class="s1">'loop_range'</span><span class="p">])</span>
</pre></div>
</div>
<p>Now the exported ONNX graph becomes:</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="n">input_data</span> <span class="p">:</span> <span class="n">Long</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">loop_range</span> <span class="p">:</span> <span class="n">Long</span><span class="p">()):</span>
<span class="o">%</span><span class="mi">2</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">1</span><span class="p">}](),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">LoopModel2</span><span class="o">/</span><span class="n">loop</span>
<span class="o">%</span><span class="mi">3</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Cast</span><span class="p">[</span><span class="n">to</span><span class="o">=</span><span class="mi">9</span><span class="p">](</span><span class="o">%</span><span class="mi">2</span><span class="p">)</span>
<span class="o">%</span><span class="mi">4</span> <span class="p">:</span> <span class="n">Long</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">onnx</span><span class="p">::</span><span class="n">Loop</span><span class="p">(</span><span class="o">%</span><span class="n">loop_range</span><span class="p">,</span> <span class="o">%</span><span class="mi">3</span><span class="p">,</span> <span class="o">%</span><span class="n">input_data</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">LoopModel2</span><span class="o">/</span><span class="n">loop</span> <span class="c1"># custom_loop.py:240:5</span>
<span class="n">block0</span><span class="p">(</span><span class="o">%</span><span class="n">i</span><span class="o">.</span><span class="mi">1</span> <span class="p">:</span> <span class="n">Long</span><span class="p">(),</span> <span class="o">%</span><span class="n">cond</span> <span class="p">:</span> <span class="nb">bool</span><span class="p">,</span> <span class="o">%</span><span class="n">x</span><span class="o">.</span><span class="mi">6</span> <span class="p">:</span> <span class="n">Long</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="mi">8</span> <span class="p">:</span> <span class="n">Long</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">onnx</span><span class="p">::</span><span class="n">Add</span><span class="p">(</span><span class="o">%</span><span class="n">x</span><span class="o">.</span><span class="mi">6</span><span class="p">,</span> <span class="o">%</span><span class="n">i</span><span class="o">.</span><span class="mi">1</span><span class="p">),</span> <span class="n">scope</span><span class="p">:</span> <span class="n">LoopModel2</span><span class="o">/</span><span class="n">loop</span> <span class="c1"># custom_loop.py:241:13</span>
<span class="o">%</span><span class="mi">9</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">onnx</span><span class="p">::</span><span class="n">Cast</span><span class="p">[</span><span class="n">to</span><span class="o">=</span><span class="mi">9</span><span class="p">](</span><span class="o">%</span><span class="mi">2</span><span class="p">)</span>
<span class="o">-></span> <span class="p">(</span><span class="o">%</span><span class="mi">9</span><span class="p">,</span> <span class="o">%</span><span class="mi">8</span><span class="p">)</span>
<span class="k">return</span> <span class="p">(</span><span class="o">%</span><span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
<p>The dynamic control flow is captured correctly. We can verify in backends with different loop range.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">caffe2.python.onnx.backend</span> <span class="k">as</span> <span class="nn">backend</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">onnx</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="s1">'loop.onnx'</span><span class="p">)</span>
<span class="n">rep</span> <span class="o">=</span> <span class="n">backend</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="n">outputs</span> <span class="o">=</span> <span class="n">rep</span><span class="o">.</span><span class="n">run</span><span class="p">((</span><span class="n">dummy_input</span><span class="o">.</span><span class="n">numpy</span><span class="p">(),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="mi">9</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">int64</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>
<span class="c1">#[[37 37 37]</span>
<span class="c1"># [37 37 37]]</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_sess</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="s1">'loop.onnx'</span><span class="p">)</span>
<span class="n">outputs</span> <span class="o">=</span> <span class="n">ort_sess</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="s1">'input_data'</span><span class="p">:</span> <span class="n">dummy_input</span><span class="o">.</span><span class="n">numpy</span><span class="p">(),</span>
<span class="s1">'loop_range'</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="mi">9</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">int64</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="c1">#[array([[37, 37, 37],</span>
<span class="c1"># [37, 37, 37]], dtype=int64)]</span>
</pre></div>
</div>
<p>To avoid exporting a variable scalar tensor as a fixed value constant as part of the ONNX model, please
avoid use of <code class="docutils literal notranslate"><span class="pre">torch.Tensor.item()</span></code>. Torch supports implicit cast of single-element tensors to numbers.
E.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">LoopModel</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">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="n">res</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">arr</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</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="nb">int</span><span class="p">(</span><span class="n">y</span><span class="p">)):</span>
<span class="n">res</span> <span class="o">+=</span> <span class="p">[</span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="kc">False</span><span class="p">)]</span>
<span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">stack</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">LoopModel</span><span class="p">())</span>
<span class="n">inputs</span> <span class="o">=</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">16</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">8</span><span class="p">))</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">model</span><span class="p">(</span><span class="o">*</span><span class="n">inputs</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">inputs</span><span class="p">,</span> <span class="s1">'loop_and_list.onnx'</span><span class="p">,</span> <span class="n">opset_version</span><span class="o">=</span><span class="mi">11</span><span class="p">,</span> <span class="n">example_outputs</span><span class="o">=</span><span class="n">out</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="write-pytorch-model-in-torch-way">
<h2><a class="toc-backref" href="#id5">Write PyTorch model in Torch way</a><a class="headerlink" href="#write-pytorch-model-in-torch-way" title="Permalink to this headline">¶</a></h2>
<p>PyTorch models can be written using numpy manipulations, but this is not proper when we convert to the ONNX model.
For the trace-based exporter, tracing treats the numpy values as the constant node,
therefore it calculates the wrong result if we change the input.
So the PyTorch model need implement using torch operators.
For example, do not use numpy operators on numpy tensors:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></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">z</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>do not convert to numpy types:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="n">x</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">int</span><span class="p">)</span>
</pre></div>
</div>
<p>Always use torch tensors and torch operators: torch.concat, etc.
In addition, Dropout layer need defined in init function so that inferencing can handle it properly, i.e.,</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</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="bp">self</span><span class="o">.</span><span class="n">dropout</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Dropout</span><span class="p">(</span><span class="mf">0.5</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">x</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dropout</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="using-dictionaries-to-handle-named-arguments-as-model-inputs">
<h2><a class="toc-backref" href="#id6">Using dictionaries to handle Named Arguments as model inputs</a><a class="headerlink" href="#using-dictionaries-to-handle-named-arguments-as-model-inputs" title="Permalink to this headline">¶</a></h2>
<p>There are two ways to handle models which consist of named parameters or keyword arguments as inputs:</p>
<ul class="simple">
<li><p>The first method is to pass all the inputs in the same order as required by the model and pass None
values for the keyword arguments that do not require a value to be passed</p></li>
<li><p>The second and more intuitive method is to represent the keyword arguments as key-value pairs where
the key represents the name of the argument in the model signature and the value represents the value
of the argument to be passed</p></li>
</ul>
<p>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">x</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="k">if</span> <span class="n">y</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="k">if</span> <span class="n">z</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">z</span>
<span class="k">return</span> <span class="n">x</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">Model</span><span class="p">()</span>
<span class="n">x</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">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">z</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">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
<p>There are two ways of exporting the model:</p>
<ul>
<li><p>Not using a dictionary for the keyword arguments and passing all the inputs in the same order
as required by the model</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>torch.onnx.export(model, (x, None, z), ‘test.onnx’)
</pre></div>
</div>
</li>
<li><p>Using a dictionary to represent the keyword arguments. This dictionary is always passed in
addition to the non-keyword arguments and is always the last argument in the args tuple.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>torch.onnx.export(model, (x, {'y': None, 'z': z}), ‘test.onnx’)
</pre></div>
</div>
</li>
</ul>
<p>For cases in which there are no keyword arguments, models can be exported with either an
empty or no dictionary. For example,</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>torch.onnx.export(model, (x, {}), ‘test.onnx’)
or
torch.onnx.export(model, (x, ), ‘test.onnx’)
</pre></div>
</div>
<p>An exception to this rule are cases in which the last input is also of a dictionary type.
In these cases it is mandatory to have an empty dictionary as the last argument in the
args tuple. For example,</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">k</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="o">...</span>
<span class="k">return</span> <span class="n">x</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">Model</span><span class="p">()</span>
<span class="n">k</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">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</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="mf">1.</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">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)}</span>
</pre></div>
</div>
<p>Without the presence of the empty dictionary, the export call assumes that the
‘x’ input is intended to represent the optional dictionary consisting of named arguments.
In order to prevent this from being an issue a constraint is placed to provide an empty
dictionary as the last input in the tuple args in such cases.
The new call would look like this.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>torch.onnx.export(model, (k, x, {}), ‘test.onnx’)
</pre></div>
</div>
</div>
<div class="section" id="indexing">
<h2><a class="toc-backref" href="#id7">Indexing</a><a class="headerlink" href="#indexing" title="Permalink to this headline">¶</a></h2>
<p>Tensor indexing in PyTorch is very flexible and complicated.
There are two categories of indexing. Both are largely supported in exporting today.
If you are experiencing issues exporting indexing that belongs to the supported patterns below,
please double check that you are exporting with the latest opset (opset_version=12).</p>
<div class="section" id="getter">
<h3><a class="toc-backref" href="#id8">Getter</a><a class="headerlink" href="#getter" title="Permalink to this headline">¶</a></h3>
<p>This type of indexing occurs on the RHS. Export is supported for ONNX opset version >= 9. E.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">data</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">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="n">index</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">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
<span class="c1"># RHS indexing is supported in ONNX opset >= 11.</span>
<span class="k">class</span> <span class="nc">RHSIndexing</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">data</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
<span class="k">return</span> <span class="n">data</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">RHSIndexing</span><span class="p">()(</span><span class="n">data</span><span class="p">,</span> <span class="n">index</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">RHSIndexing</span><span class="p">(),</span> <span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">index</span><span class="p">),</span> <span class="s1">'indexing.onnx'</span><span class="p">,</span> <span class="n">opset_version</span><span class="o">=</span><span class="mi">9</span><span class="p">)</span>
<span class="c1"># onnxruntime</span>
<span class="kn">import</span> <span class="nn">onnxruntime</span>
<span class="n">sess</span> <span class="o">=</span> <span class="n">onnxruntime</span><span class="o">.</span><span class="n">InferenceSession</span><span class="p">(</span><span class="s1">'indexing.onnx'</span><span class="p">)</span>
<span class="n">out_ort</span> <span class="o">=</span> <span class="n">sess</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="n">sess</span><span class="o">.</span><span class="n">get_inputs</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">data</span><span class="o">.</span><span class="n">numpy</span><span class="p">(),</span>
<span class="n">sess</span><span class="o">.</span><span class="n">get_inputs</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">index</span><span class="o">.</span><span class="n">numpy</span><span class="p">(),</span>
<span class="p">})</span>
<span class="k">assert</span> <span class="n">torch</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="n">out</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="n">out_ort</span><span class="p">)))</span>
</pre></div>
</div>
<p>Below is the list of supported patterns for RHS indexing.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Scalar indices</span>
<span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="c1"># Slice indices</span>
<span class="n">data</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span>
<span class="c1"># Tensor indices</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">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="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="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="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="c1"># Ellipsis</span>
<span class="c1"># Not supported in scripting</span>
<span class="c1"># i.e. torch.jit.script(model) will fail if model contains this pattern.</span>
<span class="c1"># Export is supported under tracing</span>
<span class="c1"># i.e. torch.onnx.export(model)</span>
<span class="n">data</span><span class="p">[</span><span class="o">...</span><span class="p">]</span>
<span class="c1"># The combination of above</span>
<span class="n">data</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="o">...</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">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]),</span> <span class="mi">2</span><span class="p">:</span><span class="mi">4</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="p">[</span><span class="mi">2</span><span class="p">]])]</span>
<span class="c1"># Boolean mask (supported for ONNX opset version >= 11)</span>
<span class="n">data</span><span class="p">[</span><span class="n">data</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">]</span>
</pre></div>
</div>
<p>And below is the list of unsupported patterns for RHS indexing.</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>
</pre></div>
</div>
</div>
<div class="section" id="setter">
<h3><a class="toc-backref" href="#id9">Setter</a><a class="headerlink" href="#setter" title="Permalink to this headline">¶</a></h3>
<p>In code, this type of indexing occurs on the LHS.
Export is supported for ONNX opset version >= 11. E.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="n">new_data</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="c1"># LHS indexing is supported in ONNX opset >= 11.</span>
<span class="k">class</span> <span class="nc">LHSIndexing</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">data</span><span class="p">,</span> <span class="n">new_data</span><span class="p">):</span>
<span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_data</span>
<span class="k">return</span> <span class="n">data</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">LHSIndexing</span><span class="p">()(</span><span class="n">data</span><span class="p">,</span> <span class="n">new_data</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="n">new_data</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">float32</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">LHSIndexing</span><span class="p">(),</span> <span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">new_data</span><span class="p">),</span> <span class="s1">'inplace_assign.onnx'</span><span class="p">,</span> <span class="n">opset_version</span><span class="o">=</span><span class="mi">11</span><span class="p">)</span>
<span class="c1"># onnxruntime</span>
<span class="kn">import</span> <span class="nn">onnxruntime</span>
<span class="n">sess</span> <span class="o">=</span> <span class="n">onnxruntime</span><span class="o">.</span><span class="n">InferenceSession</span><span class="p">(</span><span class="s1">'inplace_assign.onnx'</span><span class="p">)</span>
<span class="n">out_ort</span> <span class="o">=</span> <span class="n">sess</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="n">sess</span><span class="o">.</span><span class="n">get_inputs</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">numpy</span><span class="p">(),</span>
<span class="n">sess</span><span class="o">.</span><span class="n">get_inputs</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">name</span><span class="p">:</span> <span class="n">new_data</span><span class="o">.</span><span class="n">numpy</span><span class="p">(),</span>
<span class="p">})</span>
<span class="k">assert</span> <span class="n">torch</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="n">out</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="n">out_ort</span><span class="p">)))</span>
</pre></div>
</div>
<p>Below is the list of supported patterns for LHS indexing.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Scalar indices</span>
<span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_data</span>
<span class="c1"># Slice indices</span>
<span class="n">data</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"># Tensor indices</span>
<span class="c1"># If more than one tensor are used as indices, only consecutive 1-d tensor indices are supported.</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="o">=</span> <span class="n">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">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">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"># Ellipsis</span>
<span class="c1"># Not supported to export in script modules</span>
<span class="c1"># i.e. torch.onnx.export(torch.jit.script(model)) will fail if model contains this pattern.</span>
<span class="c1"># Export is supported under tracing</span>
<span class="c1"># i.e. torch.onnx.export(model)</span>
<span class="n">data</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_data</span>
<span class="c1"># The combination of above</span>
<span class="n">data</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="o">...</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">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]),</span> <span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">+=</span> <span class="n">update</span>
<span class="c1"># Boolean mask</span>
<span class="n">data</span><span class="p">[</span><span class="n">data</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_data</span>
</pre></div>
</div>
<p>And below is the list of unsupported patterns for LHS indexing.</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"># 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"># 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>
</pre></div>
</div>
<p>If you are experiencing issues exporting indexing that belongs to the above supported patterns, please double check that
you are exporting with the latest opset (opset_version=12).</p>
</div>
</div>
<div class="section" id="torchvision-support">
<h2><a class="toc-backref" href="#id10">TorchVision support</a><a class="headerlink" href="#torchvision-support" title="Permalink to this headline">¶</a></h2>
<p>All TorchVision models, except for quantized versions, are exportable to ONNX.
More details can be found in <a class="reference external" href="torchvision/models.html">TorchVision</a>.</p>
</div>
<div class="section" id="limitations">
<h2><a class="toc-backref" href="#id11">Limitations</a><a class="headerlink" href="#limitations" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p>Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted
but their usage is not recommended. Users need to verify their dict inputs carefully, and keep in mind that
dynamic lookups are not available.</p></li>
<li><p>PyTorch and ONNX backends(Caffe2, ONNX Runtime, etc) often have implementations of operators with some
numeric differences. Depending on model structure, these differences
may be negligible, but they can also cause major divergences in behavior
(especially on untrained models.) We allow Caffe2 to call directly to Torch implementations of operators, to
help you smooth over these differences when precision is important,
and to also document these differences.</p></li>
</ul>
</div>
<div class="section" id="supported-operators">
<h2><a class="toc-backref" href="#id12">Supported operators</a><a class="headerlink" href="#supported-operators" title="Permalink to this headline">¶</a></h2>
<p>The following operators are supported:</p>
<ul class="simple">
<li><p>BatchNorm</p></li>
<li><p>ConstantPadNd</p></li>
<li><p>Conv</p></li>
<li><p>Dropout</p></li>
<li><p>Embedding (no optional arguments supported)</p></li>
<li><p>EmbeddingBag</p></li>
<li><p>FeatureDropout (training mode not supported)</p></li>
<li><p>Index</p></li>
<li><p>MaxPool1d</p></li>
<li><p>MaxPool2d</p></li>
<li><p>MaxPool3d</p></li>
<li><p>RNN</p></li>
<li><p>abs</p></li>
<li><p>absolute</p></li>
<li><p>acos</p></li>
<li><p>adaptive_avg_pool1d</p></li>
<li><p>adaptive_avg_pool2d</p></li>
<li><p>adaptive_avg_pool3d</p></li>
<li><p>adaptive_max_pool1d</p></li>
<li><p>adaptive_max_pool2d</p></li>
<li><p>adaptive_max_pool3d</p></li>
<li><p>add (nonzero alpha not supported)</p></li>
<li><p>addmm</p></li>
<li><p>and</p></li>
<li><p>arange</p></li>
<li><p>argmax</p></li>
<li><p>argmin</p></li>
<li><p>asin</p></li>
<li><p>atan</p></li>
<li><p>avg_pool1d</p></li>
<li><p>avg_pool2d</p></li>
<li><p>avg_pool2d</p></li>
<li><p>avg_pool3d</p></li>
<li><p>as_strided</p></li>
<li><p>baddbmm</p></li>
<li><p>bitshift</p></li>
<li><p>cat</p></li>
<li><p>ceil</p></li>
<li><p>celu</p></li>
<li><p>clamp</p></li>
<li><p>clamp_max</p></li>
<li><p>clamp_min</p></li>
<li><p>concat</p></li>
<li><p>copy</p></li>
<li><p>cos</p></li>
<li><p>cumsum</p></li>
<li><p>det</p></li>
<li><p>dim_arange</p></li>
<li><p>div</p></li>
<li><p>dropout</p></li>
<li><p>einsum</p></li>
<li><p>elu</p></li>
<li><p>empty</p></li>
<li><p>empty_like</p></li>
<li><p>eq</p></li>
<li><p>erf</p></li>
<li><p>exp</p></li>
<li><p>expand</p></li>
<li><p>expand_as</p></li>
<li><p>eye</p></li>
<li><p>flatten</p></li>
<li><p>floor</p></li>
<li><p>floor_divide</p></li>
<li><p>frobenius_norm</p></li>
<li><p>full</p></li>
<li><p>full_like</p></li>
<li><p>gather</p></li>
<li><p>ge</p></li>
<li><p>gelu</p></li>
<li><p>glu</p></li>
<li><p>group_norm</p></li>
<li><p>gt</p></li>
<li><p>hardswish</p></li>
<li><p>hardtanh</p></li>
<li><p>im2col</p></li>
<li><p>index_copy</p></li>
<li><p>index_fill</p></li>
<li><p>index_put</p></li>
<li><p>index_select</p></li>
<li><p>instance_norm</p></li>
<li><p>interpolate</p></li>
<li><p>isnan</p></li>
<li><p>KLDivLoss</p></li>
<li><p>layer_norm</p></li>
<li><p>le</p></li>
<li><p>leaky_relu</p></li>
<li><p>len</p></li>
<li><p>log</p></li>
<li><p>log1p</p></li>
<li><p>log2</p></li>
<li><p>log_sigmoid</p></li>
<li><p>log_softmax</p></li>
<li><p>logdet</p></li>
<li><p>logsumexp</p></li>
<li><p>lt</p></li>
<li><p>masked_fill</p></li>
<li><p>masked_scatter</p></li>
<li><p>masked_select</p></li>
<li><p>max</p></li>
<li><p>mean</p></li>
<li><p>min</p></li>
<li><p>mm</p></li>
<li><p>mul</p></li>