forked from pytorch/pytorch.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjit.html
1675 lines (1424 loc) · 139 KB
/
jit.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 charset="utf-8">
<meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TorchScript — PyTorch 1.13 documentation</title>
<link rel="canonical" href="https://pytorch.org/docs/stable/jit.html"/>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<!-- <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> -->
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/copybutton.css" type="text/css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="stylesheet" href="_static/katex-math.css" type="text/css" />
<link rel="stylesheet" href="_static/sphinx-dropdown.css" type="text/css" />
<link rel="stylesheet" href="_static/panels-bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="_static/css/jit.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="TorchScript Builtins" href="jit_builtin_functions.html" />
<link rel="prev" title="torch.hub" href="hub.html" />
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-117752657-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-117752657-2');
</script>
<!-- End Google Analytics -->
<script src="_static/js/modernizr.min.js"></script>
<!-- Preload the theme fonts -->
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<!-- Preload the katex fonts -->
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.2/css/all.css" integrity="sha384-vSIIfh2YWi9wW0r9iZe7RJPrKwp6bG+s9QZMoITbCckVJqGCCRhc+ccxNcdpHuYu" crossorigin="anonymous">
</head>
<div class="container-fluid header-holder tutorials-header" id="header-holder">
<div class="container">
<div class="header-container">
<a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
<div class="main-menu">
<ul>
<li>
<a href="https://pytorch.org/get-started">Get Started</a>
</li>
<li>
<a href="https://pytorch.org/ecosystem">Ecosystem</a>
</li>
<li>
<a href="https://pytorch.org/mobile">Mobile</a>
</li>
<li>
<a href="https://pytorch.org/blog/">Blog</a>
</li>
<li>
<a href="https://pytorch.org/tutorials">Tutorials</a>
</li>
<li class="active docs-active">
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="resource-option with-down-orange-arrow">
Docs
</a>
<div class="resources-dropdown-menu">
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/docs/stable/index.html">
<span class="dropdown-title">PyTorch</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/audio/stable/index.html">
<span class="dropdown-title">torchaudio</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/text/stable/index.html">
<span class="dropdown-title">torchtext</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/vision/stable/index.html">
<span class="dropdown-title">torchvision</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/torcharrow">
<span class="dropdown-title">torcharrow</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/data">
<span class="dropdown-title">TorchData</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/torchrec">
<span class="dropdown-title">TorchRec</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/serve/">
<span class="dropdown-title">TorchServe</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/torchx/">
<span class="dropdown-title">TorchX</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/xla">
<span class="dropdown-title">PyTorch on XLA Devices</span>
<p></p>
</a>
</div>
</li>
<li>
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="resource-option with-down-arrow">
Resources
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/features">
<span class="dropdown-title">About</span>
<p>Learn about PyTorch’s features and capabilities</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/foundation">
<span class="dropdown-title">PyTorch Foundation</span>
<p>Learn about the PyTorch foundation</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/community-stories">
<span class="dropdown-title">Community Stories</span>
<p>Learn how our community solves real, everyday machine learning problems with PyTorch.</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://pytorch.org/events">
<span class="dropdown-title">Events</span>
<p>Find events, webinars, and podcasts</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.13 ▼</a>
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search Docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p class="caption" role="heading"><span class="caption-text">Community</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="community/build_ci_governance.html">PyTorch Governance | Build + CI</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/contribution_guide.html">PyTorch Contribution Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/design.html">PyTorch Design Philosophy</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/governance.html">PyTorch Governance | Mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/persons_of_interest.html">PyTorch Governance | Maintainers</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Developer Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="notes/amp_examples.html">CUDA Automatic Mixed Precision examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/autograd.html">Autograd mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/broadcasting.html">Broadcasting semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cpu_threading_torchscript_inference.html">CPU threading and TorchScript inference</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cuda.html">CUDA semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/ddp.html">Distributed Data Parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/extending.html">Extending PyTorch</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/gradcheck.html">Gradcheck mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/hip.html">HIP (ROCm) semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/large_scale_deployments.html">Features for large-scale deployments</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/modules.html">Modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/mps.html">MPS backend</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/multiprocessing.html">Multiprocessing best practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/numerical_accuracy.html">Numerical accuracy</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/randomness.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/serialization.html">Serialization semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/windows.html">Windows FAQ</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Language Bindings</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="cpp_index.html">C++</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/javadoc/">Javadoc</a></li>
<li class="toctree-l1"><a class="reference internal" href="deploy.html">torch::deploy</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="torch.html">torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.html">torch.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.functional.html">torch.nn.functional</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensors.html">torch.Tensor</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_attributes.html">Tensor Attributes</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_view.html">Tensor Views</a></li>
<li class="toctree-l1"><a class="reference internal" href="amp.html">torch.amp</a></li>
<li class="toctree-l1"><a class="reference internal" href="autograd.html">torch.autograd</a></li>
<li class="toctree-l1"><a class="reference internal" href="library.html">torch.library</a></li>
<li class="toctree-l1"><a class="reference internal" href="cuda.html">torch.cuda</a></li>
<li class="toctree-l1"><a class="reference internal" href="backends.html">torch.backends</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.html">torch.distributed</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.algorithms.join.html">torch.distributed.algorithms.join</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.elastic.html">torch.distributed.elastic</a></li>
<li class="toctree-l1"><a class="reference internal" href="fsdp.html">torch.distributed.fsdp</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.optim.html">torch.distributed.optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributions.html">torch.distributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="fft.html">torch.fft</a></li>
<li class="toctree-l1"><a class="reference internal" href="futures.html">torch.futures</a></li>
<li class="toctree-l1"><a class="reference internal" href="fx.html">torch.fx</a></li>
<li class="toctree-l1"><a class="reference internal" href="hub.html">torch.hub</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">torch.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="linalg.html">torch.linalg</a></li>
<li class="toctree-l1"><a class="reference internal" href="monitor.html">torch.monitor</a></li>
<li class="toctree-l1"><a class="reference internal" href="special.html">torch.special</a></li>
<li class="toctree-l1"><a class="reference internal" href="torch.overrides.html">torch.overrides</a></li>
<li class="toctree-l1"><a class="reference internal" href="package.html">torch.package</a></li>
<li class="toctree-l1"><a class="reference internal" href="profiler.html">torch.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.init.html">torch.nn.init</a></li>
<li class="toctree-l1"><a class="reference internal" href="onnx.html">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="masked.html">torch.masked</a></li>
<li class="toctree-l1"><a class="reference internal" href="nested.html">torch.nested</a></li>
<li class="toctree-l1"><a class="reference internal" href="sparse.html">torch.sparse</a></li>
<li class="toctree-l1"><a class="reference internal" href="storage.html">torch.Storage</a></li>
<li class="toctree-l1"><a class="reference internal" href="testing.html">torch.testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="benchmark_utils.html">torch.utils.benchmark</a></li>
<li class="toctree-l1"><a class="reference internal" href="bottleneck.html">torch.utils.bottleneck</a></li>
<li class="toctree-l1"><a class="reference internal" href="checkpoint.html">torch.utils.checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="cpp_extension.html">torch.utils.cpp_extension</a></li>
<li class="toctree-l1"><a class="reference internal" href="data.html">torch.utils.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="jit_utils.html">torch.utils.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="dlpack.html">torch.utils.dlpack</a></li>
<li class="toctree-l1"><a class="reference internal" href="mobile_optimizer.html">torch.utils.mobile_optimizer</a></li>
<li class="toctree-l1"><a class="reference internal" href="model_zoo.html">torch.utils.model_zoo</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensorboard.html">torch.utils.tensorboard</a></li>
<li class="toctree-l1"><a class="reference internal" href="type_info.html">Type Info</a></li>
<li class="toctree-l1"><a class="reference internal" href="named_tensor.html">Named Tensors</a></li>
<li class="toctree-l1"><a class="reference internal" href="name_inference.html">Named Tensors operator coverage</a></li>
<li class="toctree-l1"><a class="reference internal" href="config_mod.html">torch.__config__</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Libraries</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/audio/stable">torchaudio</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/data">TorchData</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/torchrec">TorchRec</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/serve">TorchServe</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/text/stable">torchtext</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/vision/stable">torchvision</a></li>
<li class="toctree-l1"><a class="reference external" href="http://pytorch.org/xla/">PyTorch on XLA Devices</a></li>
</ul>
</div>
</div>
</nav>
<div class="pytorch-container">
<div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
<div class="pytorch-breadcrumbs-wrapper">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="pytorch-breadcrumbs">
<li>
<a href="index.html">
Docs
</a> >
</li>
<li>TorchScript</li>
<li class="pytorch-breadcrumbs-aside">
<a href="_sources/jit.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">
<section id="torchscript">
<h1>TorchScript<a class="headerlink" href="#torchscript" title="Permalink to this heading">¶</a></h1>
<div class="toctree-wrapper compound">
</div>
<div class="toctree-wrapper compound">
</div>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="jit_language_reference_v2.html">TorchScript Language Reference</a></li>
</ul>
</div>
<nav class="contents local" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#creating-torchscript-code" id="id4">Creating TorchScript Code</a></p></li>
<li><p><a class="reference internal" href="#mixing-tracing-and-scripting" id="id5">Mixing Tracing and Scripting</a></p></li>
<li><p><a class="reference internal" href="#torchscript-language" id="id6">TorchScript Language</a></p></li>
<li><p><a class="reference internal" href="#built-in-functions-and-modules" id="id7">Built-in Functions and Modules</a></p>
<ul>
<li><p><a class="reference internal" href="#pytorch-functions-and-modules" id="id8">PyTorch Functions and Modules</a></p></li>
<li><p><a class="reference internal" href="#python-functions-and-modules" id="id9">Python Functions and Modules</a></p></li>
<li><p><a class="reference internal" href="#python-language-reference-comparison" id="id10">Python Language Reference Comparison</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#debugging" id="id11">Debugging</a></p>
<ul>
<li><p><a class="reference internal" href="#disable-jit-for-debugging" id="id12">Disable JIT for Debugging</a></p></li>
<li><p><a class="reference internal" href="#inspecting-code" id="id13">Inspecting Code</a></p></li>
<li><p><a class="reference internal" href="#interpreting-graphs" id="id14">Interpreting Graphs</a></p></li>
<li><p><a class="reference internal" href="#tracer" id="id15">Tracer</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#frequently-asked-questions" id="id16">Frequently Asked Questions</a></p></li>
<li><p><a class="reference internal" href="#known-issues" id="id17">Known Issues</a></p></li>
<li><p><a class="reference internal" href="#appendix" id="id18">Appendix</a></p>
<ul>
<li><p><a class="reference internal" href="#migrating-to-pytorch-1-2-recursive-scripting-api" id="id19">Migrating to PyTorch 1.2 Recursive Scripting API</a></p></li>
<li><p><a class="reference internal" href="#fusion-backends" id="id20">Fusion Backends</a></p></li>
<li><p><a class="reference internal" href="#references" id="id21">References</a></p></li>
</ul>
</li>
</ul>
</nav>
<span class="target" id="module-torch.jit"></span><p>TorchScript is a way to create serializable and optimizable models from PyTorch code.
Any TorchScript program can be saved from a Python
process and loaded in a process where there is no Python dependency.</p>
<p>We provide tools to incrementally transition a model from a pure Python program
to a TorchScript program that can be run independently from Python, such as in a standalone C++ program.
This makes it possible to train models in PyTorch using familiar tools in Python and then export
the model via TorchScript to a production environment where Python programs may be disadvantageous
for performance and multi-threading reasons.</p>
<p>For a gentle introduction to TorchScript, see the <a class="reference external" href="https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html">Introduction to TorchScript</a> tutorial.</p>
<p>For an end-to-end example of converting a PyTorch model to TorchScript and running it in C++, see the
<a class="reference external" href="https://pytorch.org/tutorials/advanced/cpp_export.html">Loading a PyTorch Model in C++</a> tutorial.</p>
<section id="creating-torchscript-code">
<h2><a class="toc-backref" href="#id4" role="doc-backlink">Creating TorchScript Code</a><a class="headerlink" href="#creating-torchscript-code" title="Permalink to this heading">¶</a></h2>
<table class="autosummary longtable docutils align-default">
<tbody>
<tr class="row-odd"><td><p><p id="torch.jit.script"/><a class="reference internal" href="generated/torch.jit.script.html#torch.jit.script" title="torch.jit.script"><code class="xref py py-obj docutils literal notranslate"><span class="pre">script</span></code></a></p></td>
<td><p>Scripting a function or <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code> will inspect the source code, compile it as TorchScript code using the TorchScript compiler, and return a <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> or <a class="reference internal" href="generated/torch.jit.ScriptFunction.html#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a>.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.trace"/><a class="reference internal" href="generated/torch.jit.trace.html#torch.jit.trace" title="torch.jit.trace"><code class="xref py py-obj docutils literal notranslate"><span class="pre">trace</span></code></a></p></td>
<td><p>Trace a function and return an executable or <a class="reference internal" href="generated/torch.jit.ScriptFunction.html#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a> that will be optimized using just-in-time compilation.</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.script_if_tracing"/><a class="reference internal" href="generated/torch.jit.script_if_tracing.html#torch.jit.script_if_tracing" title="torch.jit.script_if_tracing"><code class="xref py py-obj docutils literal notranslate"><span class="pre">script_if_tracing</span></code></a></p></td>
<td><p>Compiles <code class="docutils literal notranslate"><span class="pre">fn</span></code> when it is first called during tracing.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.trace_module"/><a class="reference internal" href="generated/torch.jit.trace_module.html#torch.jit.trace_module" title="torch.jit.trace_module"><code class="xref py py-obj docutils literal notranslate"><span class="pre">trace_module</span></code></a></p></td>
<td><p>Trace a module and return an executable <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> that will be optimized using just-in-time compilation.</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.fork"/><a class="reference internal" href="generated/torch.jit.fork.html#torch.jit.fork" title="torch.jit.fork"><code class="xref py py-obj docutils literal notranslate"><span class="pre">fork</span></code></a></p></td>
<td><p>Creates an asynchronous task executing <cite>func</cite> and a reference to the value of the result of this execution.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.wait"/><a class="reference internal" href="generated/torch.jit.wait.html#torch.jit.wait" title="torch.jit.wait"><code class="xref py py-obj docutils literal notranslate"><span class="pre">wait</span></code></a></p></td>
<td><p>Forces completion of a <cite>torch.jit.Future[T]</cite> asynchronous task, returning the result of the task.</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.ScriptModule"/><a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ScriptModule</span></code></a></p></td>
<td><p>A wrapper around C++ <code class="docutils literal notranslate"><span class="pre">torch::jit::Module</span></code>.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.ScriptFunction"/><a class="reference internal" href="generated/torch.jit.ScriptFunction.html#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a></p></td>
<td><p>Functionally equivalent to a <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>, but represents a single function and does not have any attributes or Parameters.</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.freeze"/><a class="reference internal" href="generated/torch.jit.freeze.html#torch.jit.freeze" title="torch.jit.freeze"><code class="xref py py-obj docutils literal notranslate"><span class="pre">freeze</span></code></a></p></td>
<td><p>Freezing a <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> will clone it and attempt to inline the cloned module's submodules, parameters, and attributes as constants in the TorchScript IR Graph.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.optimize_for_inference"/><a class="reference internal" href="generated/torch.jit.optimize_for_inference.html#torch.jit.optimize_for_inference" title="torch.jit.optimize_for_inference"><code class="xref py py-obj docutils literal notranslate"><span class="pre">optimize_for_inference</span></code></a></p></td>
<td><p>Performs a set of optimization passes to optimize a model for the purposes of inference.</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.enable_onednn_fusion"/><a class="reference internal" href="generated/torch.jit.enable_onednn_fusion.html#torch.jit.enable_onednn_fusion" title="torch.jit.enable_onednn_fusion"><code class="xref py py-obj docutils literal notranslate"><span class="pre">enable_onednn_fusion</span></code></a></p></td>
<td><p>Enables or disables onednn JIT fusion based on the parameter <cite>enabled</cite>.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.onednn_fusion_enabled"/><a class="reference internal" href="generated/torch.jit.onednn_fusion_enabled.html#torch.jit.onednn_fusion_enabled" title="torch.jit.onednn_fusion_enabled"><code class="xref py py-obj docutils literal notranslate"><span class="pre">onednn_fusion_enabled</span></code></a></p></td>
<td><p>Returns whether onednn JIT fusion is enabled</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.set_fusion_strategy"/><a class="reference internal" href="generated/torch.jit.set_fusion_strategy.html#torch.jit.set_fusion_strategy" title="torch.jit.set_fusion_strategy"><code class="xref py py-obj docutils literal notranslate"><span class="pre">set_fusion_strategy</span></code></a></p></td>
<td><p>Sets the type and number of specializations that can occur during fusion.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.strict_fusion"/><a class="reference internal" href="generated/torch.jit.strict_fusion.html#torch.jit.strict_fusion" title="torch.jit.strict_fusion"><code class="xref py py-obj docutils literal notranslate"><span class="pre">strict_fusion</span></code></a></p></td>
<td><p>This class errors if not all nodes have been fused in inference, or symbolically differentiated in training.</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.save"/><a class="reference internal" href="generated/torch.jit.save.html#torch.jit.save" title="torch.jit.save"><code class="xref py py-obj docutils literal notranslate"><span class="pre">save</span></code></a></p></td>
<td><p>Save an offline version of this module for use in a separate process.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.load"/><a class="reference internal" href="generated/torch.jit.load.html#torch.jit.load" title="torch.jit.load"><code class="xref py py-obj docutils literal notranslate"><span class="pre">load</span></code></a></p></td>
<td><p>Load a <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> or <a class="reference internal" href="generated/torch.jit.ScriptFunction.html#torch.jit.ScriptFunction" title="torch.jit.ScriptFunction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptFunction</span></code></a> previously saved with <a class="reference internal" href="generated/torch.jit.save.html#torch.jit.save" title="torch.jit.save"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.save</span></code></a></p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.ignore"/><a class="reference internal" href="generated/torch.jit.ignore.html#torch.jit.ignore" title="torch.jit.ignore"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ignore</span></code></a></p></td>
<td><p>This decorator indicates to the compiler that a function or method should be ignored and left as a Python function.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.unused"/><a class="reference internal" href="generated/torch.jit.unused.html#torch.jit.unused" title="torch.jit.unused"><code class="xref py py-obj docutils literal notranslate"><span class="pre">unused</span></code></a></p></td>
<td><p>This decorator indicates to the compiler that a function or method should be ignored and replaced with the raising of an exception.</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.isinstance"/><a class="reference internal" href="generated/torch.jit.isinstance.html#torch.jit.isinstance" title="torch.jit.isinstance"><code class="xref py py-obj docutils literal notranslate"><span class="pre">isinstance</span></code></a></p></td>
<td><p>This function provides for container type refinement in TorchScript.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.jit.Attribute"/><a class="reference internal" href="generated/torch.jit.Attribute.html#torch.jit.Attribute" title="torch.jit.Attribute"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Attribute</span></code></a></p></td>
<td><p>This method is a pass-through function that returns <cite>value</cite>, mostly used to indicate to the TorchScript compiler that the left-hand side expression is a class instance attribute with type of <cite>type</cite>.</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.jit.annotate"/><a class="reference internal" href="generated/torch.jit.annotate.html#torch.jit.annotate" title="torch.jit.annotate"><code class="xref py py-obj docutils literal notranslate"><span class="pre">annotate</span></code></a></p></td>
<td><p>This method is a pass-through function that returns <cite>the_value</cite>, used to hint TorchScript compiler the type of <cite>the_value</cite>.</p></td>
</tr>
</tbody>
</table>
</section>
<section id="mixing-tracing-and-scripting">
<h2><a class="toc-backref" href="#id5" role="doc-backlink">Mixing Tracing and Scripting</a><a class="headerlink" href="#mixing-tracing-and-scripting" title="Permalink to this heading">¶</a></h2>
<p>In many cases either tracing or scripting is an easier approach for converting a model to TorchScript.
Tracing and scripting can be composed to suit the particular requirements
of a part of a model.</p>
<p>Scripted functions can call traced functions. This is particularly useful when you need
to use control-flow around a simple feed-forward model. For instance the beam search
of a sequence to sequence model will typically be written in script but can call an
encoder module generated using tracing.</p>
<p>Example (calling a traced function in script):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="k">def</span> <span class="nf">foo</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="mi">2</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="n">traced_foo</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">trace</span><span class="p">(</span><span class="n">foo</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</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">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">)))</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">bar</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">return</span> <span class="n">traced_foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<p>Traced functions can call script functions. This is useful when a small part of
a model requires some control-flow even though most of the model is just a feed-forward
network. Control-flow inside of a script function called by a traced function is
preserved correctly.</p>
<p>Example (calling a script function in a traced function):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</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">foo</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">if</span> <span class="n">x</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="o">></span> <span class="n">y</span><span class="o">.</span><span class="n">max</span><span class="p">():</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">x</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">y</span>
<span class="k">return</span> <span class="n">r</span>
<span class="k">def</span> <span class="nf">bar</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="k">return</span> <span class="n">foo</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="o">+</span> <span class="n">z</span>
<span class="n">traced_bar</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">trace</span><span class="p">(</span><span class="n">bar</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</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">rand</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">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">)))</span>
</pre></div>
</div>
<p>This composition also works for <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>s as well, where it can be used to generate
a submodule using tracing that can be called from the methods of a script module.</p>
<p>Example (using a traced module):</p>
<div class="highlight-python3 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="k">class</span> <span class="nc">MyScriptModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">MyScriptModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">means</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Parameter</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">103.939</span><span class="p">,</span> <span class="mf">116.779</span><span class="p">,</span> <span class="mf">123.68</span><span class="p">])</span>
<span class="o">.</span><span class="n">resize_</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">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">resnet</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">trace</span><span class="p">(</span><span class="n">torchvision</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">resnet18</span><span class="p">(),</span>
<span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">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="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">resnet</span><span class="p">(</span><span class="nb">input</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">means</span><span class="p">)</span>
<span class="n">my_script_module</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">MyScriptModule</span><span class="p">())</span>
</pre></div>
</div>
</section>
<section id="torchscript-language">
<h2><a class="toc-backref" href="#id6" role="doc-backlink">TorchScript Language</a><a class="headerlink" href="#torchscript-language" title="Permalink to this heading">¶</a></h2>
<p>TorchScript is a statically typed subset of Python, so many Python features apply
directly to TorchScript. See the full <a class="reference internal" href="jit_language_reference.html#language-reference"><span class="std std-ref">TorchScript Language Reference</span></a> for details.</p>
</section>
<section id="built-in-functions-and-modules">
<span id="builtin-functions"></span><h2><a class="toc-backref" href="#id7" role="doc-backlink">Built-in Functions and Modules</a><a class="headerlink" href="#built-in-functions-and-modules" title="Permalink to this heading">¶</a></h2>
<p>TorchScript supports the use of most PyTorch functions and many Python built-ins.
See <a class="reference internal" href="jit_builtin_functions.html#builtin-functions"><span class="std std-ref">TorchScript Builtins</span></a> for a full reference of supported functions.</p>
<section id="pytorch-functions-and-modules">
<h3><a class="toc-backref" href="#id8" role="doc-backlink">PyTorch Functions and Modules</a><a class="headerlink" href="#pytorch-functions-and-modules" title="Permalink to this heading">¶</a></h3>
<p>TorchScript supports a subset of the tensor and neural network
functions that PyTorch provides. Most methods on Tensor as well as functions in
the <code class="docutils literal notranslate"><span class="pre">torch</span></code> namespace, all functions in <code class="docutils literal notranslate"><span class="pre">torch.nn.functional</span></code> and
most modules from <code class="docutils literal notranslate"><span class="pre">torch.nn</span></code> are supported in TorchScript.</p>
<p>See <a class="reference internal" href="jit_unsupported.html#jit-unsupported"><span class="std std-ref">TorchScript Unsupported Pytorch Constructs</span></a> for a list of unsupported PyTorch functions and modules.</p>
</section>
<section id="python-functions-and-modules">
<h3><a class="toc-backref" href="#id9" role="doc-backlink">Python Functions and Modules</a><a class="headerlink" href="#python-functions-and-modules" title="Permalink to this heading">¶</a></h3>
<p>Many of Python’s <a class="reference external" href="https://docs.python.org/3/library/functions.html">built-in functions</a> are supported in TorchScript.
The <a class="reference external" href="https://docs.python.org/3/library/math.html#module-math" title="(in Python v3.10)"><code class="xref any docutils literal notranslate"><span class="pre">math</span></code></a> module is also supported (see <a class="reference internal" href="jit_builtin_functions.html#math-module"><span class="std std-ref">math Module</span></a> for details), but no other Python modules
(built-in or third party) are supported.</p>
</section>
<section id="python-language-reference-comparison">
<h3><a class="toc-backref" href="#id10" role="doc-backlink">Python Language Reference Comparison</a><a class="headerlink" href="#python-language-reference-comparison" title="Permalink to this heading">¶</a></h3>
<p>For a full listing of supported Python features, see <a class="reference internal" href="jit_python_reference.html#python-language-reference"><span class="std std-ref">Python Language Reference Coverage</span></a>.</p>
</section>
</section>
<section id="debugging">
<h2><a class="toc-backref" href="#id11" role="doc-backlink">Debugging</a><a class="headerlink" href="#debugging" title="Permalink to this heading">¶</a></h2>
<section id="disable-jit-for-debugging">
<span id="disable-torchscript"></span><h3><a class="toc-backref" href="#id12" role="doc-backlink">Disable JIT for Debugging</a><a class="headerlink" href="#disable-jit-for-debugging" title="Permalink to this heading">¶</a></h3>
<dl class="std envvar">
<dt class="sig sig-object std" id="envvar-PYTORCH_JIT">
<span class="sig-name descname"><span class="pre">PYTORCH_JIT</span></span><a class="headerlink" href="#envvar-PYTORCH_JIT" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<p>Setting the environment variable <code class="docutils literal notranslate"><span class="pre">PYTORCH_JIT=0</span></code> will disable all script
and tracing annotations. If there is hard-to-debug error in one of your
TorchScript models, you can use this flag to force everything to run using native
Python. Since TorchScript (scripting and tracing) is disabled with this flag,
you can use tools like <code class="docutils literal notranslate"><span class="pre">pdb</span></code> to debug the model code. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></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">scripted_fn</span><span class="p">(</span><span class="n">x</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="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">12</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">x</span>
<span class="k">return</span> <span class="n">x</span>
<span class="k">def</span> <span class="nf">fn</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="n">torch</span><span class="o">.</span><span class="n">neg</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">pdb</span><span class="p">;</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
<span class="k">return</span> <span class="n">scripted_fn</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">traced_fn</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">trace</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">),))</span>
<span class="n">traced_fn</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
</pre></div>
</div>
<p>Debugging this script with <code class="docutils literal notranslate"><span class="pre">pdb</span></code> works except for when we invoke the
<a class="reference internal" href="generated/torch.jit.script.html#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">@torch.jit.script</span></code></a> function. We can globally disable
JIT, so that we can call the <a class="reference internal" href="generated/torch.jit.script.html#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">@torch.jit.script</span></code></a>
function as a normal Python function and not compile it. If the above script
is called <code class="docutils literal notranslate"><span class="pre">disable_jit_example.py</span></code>, we can invoke it like so:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ PYTORCH_JIT=0 python disable_jit_example.py
</pre></div>
</div>
<p>and we will be able to step into the <a class="reference internal" href="generated/torch.jit.script.html#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">@torch.jit.script</span></code></a> function as a normal Python function. To disable the
TorchScript compiler for a specific function, see
<a class="reference internal" href="generated/torch.jit.ignore.html#torch.jit.ignore" title="torch.jit.ignore"><code class="xref py py-func docutils literal notranslate"><span class="pre">@torch.jit.ignore</span></code></a>.</p>
</section>
<section id="inspecting-code">
<span id="id1"></span><h3><a class="toc-backref" href="#id13" role="doc-backlink">Inspecting Code</a><a class="headerlink" href="#inspecting-code" title="Permalink to this heading">¶</a></h3>
<p>TorchScript provides a code pretty-printer for all <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> instances. This
pretty-printer gives an interpretation of the script method’s code as valid
Python syntax. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></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">foo</span><span class="p">(</span><span class="nb">len</span><span class="p">):</span>
<span class="c1"># type: (int) -> torch.Tensor</span>
<span class="n">rv</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="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">len</span><span class="p">):</span>
<span class="k">if</span> <span class="n">i</span> <span class="o"><</span> <span class="mi">10</span><span class="p">:</span>
<span class="n">rv</span> <span class="o">=</span> <span class="n">rv</span> <span class="o">-</span> <span class="mf">1.0</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">rv</span> <span class="o">=</span> <span class="n">rv</span> <span class="o">+</span> <span class="mf">1.0</span>
<span class="k">return</span> <span class="n">rv</span>
<span class="nb">print</span><span class="p">(</span><span class="n">foo</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>
</pre></div>
</div>
<p>A <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> with a single <code class="docutils literal notranslate"><span class="pre">forward</span></code> method will have an attribute
<code class="docutils literal notranslate"><span class="pre">code</span></code>, which you can use to inspect the <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>’s code.
If the <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> has more than one method, you will need to access
<code class="docutils literal notranslate"><span class="pre">.code</span></code> on the method itself and not the module. We can inspect the
code of a method named <code class="docutils literal notranslate"><span class="pre">foo</span></code> on a <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> by accessing <code class="docutils literal notranslate"><span class="pre">.foo.code</span></code>.
The example above produces this output:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="nb">len</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="n">Tensor</span><span class="p">:</span>
<span class="n">rv</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">dtype</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">layout</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">pin_memory</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">rv0</span> <span class="o">=</span> <span class="n">rv</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">len</span><span class="p">):</span>
<span class="k">if</span> <span class="n">torch</span><span class="o">.</span><span class="n">lt</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">10</span><span class="p">):</span>
<span class="n">rv1</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">rv0</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">rv1</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">rv0</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">rv0</span> <span class="o">=</span> <span class="n">rv1</span>
<span class="k">return</span> <span class="n">rv0</span>
</pre></div>
</div>
<p>This is TorchScript’s compilation of the code for the <code class="docutils literal notranslate"><span class="pre">forward</span></code> method.
You can use this to ensure TorchScript (tracing or scripting) has captured
your model code correctly.</p>
</section>
<section id="interpreting-graphs">
<span id="id2"></span><h3><a class="toc-backref" href="#id14" role="doc-backlink">Interpreting Graphs</a><a class="headerlink" href="#interpreting-graphs" title="Permalink to this heading">¶</a></h3>
<p>TorchScript also has a representation at a lower level than the code pretty-
printer, in the form of IR graphs.</p>
<p>TorchScript uses a static single assignment (SSA) intermediate representation
(IR) to represent computation. The instructions in this format consist of
ATen (the C++ backend of PyTorch) operators and other primitive operators,
including control flow operators for loops and conditionals. As an example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></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">foo</span><span class="p">(</span><span class="nb">len</span><span class="p">):</span>
<span class="c1"># type: (int) -> torch.Tensor</span>
<span class="n">rv</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="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">len</span><span class="p">):</span>
<span class="k">if</span> <span class="n">i</span> <span class="o"><</span> <span class="mi">10</span><span class="p">:</span>
<span class="n">rv</span> <span class="o">=</span> <span class="n">rv</span> <span class="o">-</span> <span class="mf">1.0</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">rv</span> <span class="o">=</span> <span class="n">rv</span> <span class="o">+</span> <span class="mf">1.0</span>
<span class="k">return</span> <span class="n">rv</span>
<span class="nb">print</span><span class="p">(</span><span class="n">foo</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">graph</span></code> follows the same rules described in the <a class="reference internal" href="#inspecting-code"><span class="std std-ref">Inspecting Code</span></a> section
with regard to <code class="docutils literal notranslate"><span class="pre">forward</span></code> method lookup.</p>
<p>The example script above produces the graph:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>graph(%len.1 : int):
%24 : int = prim::Constant[value=1]()
%17 : bool = prim::Constant[value=1]() # test.py:10:5
%12 : bool? = prim::Constant()
%10 : Device? = prim::Constant()
%6 : int? = prim::Constant()
%1 : int = prim::Constant[value=3]() # test.py:9:22
%2 : int = prim::Constant[value=4]() # test.py:9:25
%20 : int = prim::Constant[value=10]() # test.py:11:16
%23 : float = prim::Constant[value=1]() # test.py:12:23
%4 : int[] = prim::ListConstruct(%1, %2)
%rv.1 : Tensor = aten::zeros(%4, %6, %6, %10, %12) # test.py:9:10
%rv : Tensor = prim::Loop(%len.1, %17, %rv.1) # test.py:10:5
block0(%i.1 : int, %rv.14 : Tensor):
%21 : bool = aten::lt(%i.1, %20) # test.py:11:12
%rv.13 : Tensor = prim::If(%21) # test.py:11:9
block0():
%rv.3 : Tensor = aten::sub(%rv.14, %23, %24) # test.py:12:18
-> (%rv.3)
block1():
%rv.6 : Tensor = aten::add(%rv.14, %23, %24) # test.py:14:18
-> (%rv.6)
-> (%17, %rv.13)
return (%rv)
</pre></div>
</div>
<p>Take the instruction <code class="docutils literal notranslate"><span class="pre">%rv.1</span> <span class="pre">:</span> <span class="pre">Tensor</span> <span class="pre">=</span> <span class="pre">aten::zeros(%4,</span> <span class="pre">%6,</span> <span class="pre">%6,</span> <span class="pre">%10,</span> <span class="pre">%12)</span> <span class="pre">#</span> <span class="pre">test.py:9:10</span></code> for
example.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">%rv.1</span> <span class="pre">:</span> <span class="pre">Tensor</span></code> means we assign the output to a (unique) value named <code class="docutils literal notranslate"><span class="pre">rv.1</span></code>, that value is of <code class="docutils literal notranslate"><span class="pre">Tensor</span></code> type and that we do not know its concrete shape.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">aten::zeros</span></code> is the operator (equivalent to <code class="docutils literal notranslate"><span class="pre">torch.zeros</span></code>) and the input list <code class="docutils literal notranslate"><span class="pre">(%4,</span> <span class="pre">%6,</span> <span class="pre">%6,</span> <span class="pre">%10,</span> <span class="pre">%12)</span></code> specifies which values in scope should be passed as inputs. The schema for built-in functions like <code class="docutils literal notranslate"><span class="pre">aten::zeros</span></code> can be found at <a class="reference internal" href="#builtin-functions">Builtin Functions</a>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">#</span> <span class="pre">test.py:9:10</span></code> is the location in the original source file that generated this instruction. In this case, it is a file named <cite>test.py</cite>, on line 9, and at character 10.</p></li>
</ul>
<p>Notice that operators can also have associated <code class="docutils literal notranslate"><span class="pre">blocks</span></code>, namely the
<code class="docutils literal notranslate"><span class="pre">prim::Loop</span></code> and <code class="docutils literal notranslate"><span class="pre">prim::If</span></code> operators. In the graph print-out, these
operators are formatted to reflect their equivalent source code forms
to facilitate easy debugging.</p>
<p>Graphs can be inspected as shown to confirm that the computation described
by a <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a> is correct, in both automated and manual fashion, as
described below.</p>
</section>
<section id="tracer">
<h3><a class="toc-backref" href="#id15" role="doc-backlink">Tracer</a><a class="headerlink" href="#tracer" title="Permalink to this heading">¶</a></h3>
<section id="tracing-edge-cases">
<h4>Tracing Edge Cases<a class="headerlink" href="#tracing-edge-cases" title="Permalink to this heading">¶</a></h4>
<p>There are some edge cases that exist where the trace of a given Python
function/module will not be representative of the underlying code. These
cases can include:</p>
<ul class="simple">
<li><p>Tracing of control flow that is dependent on inputs (e.g. tensor shapes)</p></li>
<li><p>Tracing of in-place operations of tensor views (e.g. indexing on the left-hand side of an assignment)</p></li>
</ul>
<p>Note that these cases may in fact be traceable in the future.</p>
</section>
<section id="automatic-trace-checking">
<h4>Automatic Trace Checking<a class="headerlink" href="#automatic-trace-checking" title="Permalink to this heading">¶</a></h4>
<p>One way to automatically catch many errors in traces is by using <code class="docutils literal notranslate"><span class="pre">check_inputs</span></code>
on the <code class="docutils literal notranslate"><span class="pre">torch.jit.trace()</span></code> API. <code class="docutils literal notranslate"><span class="pre">check_inputs</span></code> takes a list of tuples
of inputs that will be used to re-trace the computation and verify the
results. For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">loop_in_traced_fn</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">x</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="n">x</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">)):</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">result</span> <span class="o">*</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="k">return</span> <span class="n">result</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">rand</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="mi">5</span><span class="p">),)</span>
<span class="n">check_inputs</span> <span class="o">=</span> <span class="p">[(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">),),</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</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="mi">4</span><span class="p">),)]</span>
<span class="n">traced</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">trace</span><span class="p">(</span><span class="n">loop_in_traced_fn</span><span class="p">,</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">check_inputs</span><span class="o">=</span><span class="n">check_inputs</span><span class="p">)</span>
</pre></div>
</div>
<p>Gives us the following diagnostic information:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>ERROR: Graphs differed across invocations!
Graph diff:
graph(%x : Tensor) {
%1 : int = prim::Constant[value=0]()
%2 : int = prim::Constant[value=0]()
%result.1 : Tensor = aten::select(%x, %1, %2)
%4 : int = prim::Constant[value=0]()
%5 : int = prim::Constant[value=0]()
%6 : Tensor = aten::select(%x, %4, %5)
%result.2 : Tensor = aten::mul(%result.1, %6)
%8 : int = prim::Constant[value=0]()
%9 : int = prim::Constant[value=1]()
%10 : Tensor = aten::select(%x, %8, %9)
- %result : Tensor = aten::mul(%result.2, %10)
+ %result.3 : Tensor = aten::mul(%result.2, %10)
? ++
%12 : int = prim::Constant[value=0]()
%13 : int = prim::Constant[value=2]()
%14 : Tensor = aten::select(%x, %12, %13)
+ %result : Tensor = aten::mul(%result.3, %14)
+ %16 : int = prim::Constant[value=0]()
+ %17 : int = prim::Constant[value=3]()
+ %18 : Tensor = aten::select(%x, %16, %17)
- %15 : Tensor = aten::mul(%result, %14)
? ^ ^
+ %19 : Tensor = aten::mul(%result, %18)
? ^ ^
- return (%15);
? ^
+ return (%19);
? ^
}
</pre></div>
</div>
<p>This message indicates to us that the computation differed between when
we first traced it and when we traced it with the <code class="docutils literal notranslate"><span class="pre">check_inputs</span></code>. Indeed,
the loop within the body of <code class="docutils literal notranslate"><span class="pre">loop_in_traced_fn</span></code> depends on the shape
of the input <code class="docutils literal notranslate"><span class="pre">x</span></code>, and thus when we try another <code class="docutils literal notranslate"><span class="pre">x</span></code> with a different
shape, the trace differs.</p>
<p>In this case, data-dependent control flow like this can be captured using
<a class="reference internal" href="generated/torch.jit.script.html#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.jit.script()</span></code></a> instead:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fn</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">x</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="n">x</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">)):</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">result</span> <span class="o">*</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="k">return</span> <span class="n">result</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">rand</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="mi">5</span><span class="p">),)</span>
<span class="n">check_inputs</span> <span class="o">=</span> <span class="p">[(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">),),</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</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="mi">4</span><span class="p">),)]</span>
<span class="n">scripted_fn</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">fn</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">scripted_fn</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
<span class="c1">#print(str(scripted_fn.graph).strip())</span>
<span class="k">for</span> <span class="n">input_tuple</span> <span class="ow">in</span> <span class="p">[</span><span class="n">inputs</span><span class="p">]</span> <span class="o">+</span> <span class="n">check_inputs</span><span class="p">:</span>
<span class="n">torch</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">assert_close</span><span class="p">(</span><span class="n">fn</span><span class="p">(</span><span class="o">*</span><span class="n">input_tuple</span><span class="p">),</span> <span class="n">scripted_fn</span><span class="p">(</span><span class="o">*</span><span class="n">input_tuple</span><span class="p">))</span>
</pre></div>
</div>
<p>Which produces:</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">x</span> <span class="p">:</span> <span class="n">Tensor</span><span class="p">)</span> <span class="p">{</span>
<span class="o">%</span><span class="mi">5</span> <span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="n">prim</span><span class="p">::</span><span class="n">Constant</span><span class="p">[</span><span class="n">value</span><span class="o">=</span><span class="mi">1</span><span class="p">]()</span>
<span class="o">%</span><span class="mi">1</span> <span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">prim</span><span class="p">::</span><span class="n">Constant</span><span class="p">[</span><span class="n">value</span><span class="o">=</span><span class="mi">0</span><span class="p">]()</span>
<span class="o">%</span><span class="n">result</span><span class="mf">.1</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">aten</span><span class="p">::</span><span class="n">select</span><span class="p">(</span><span class="o">%</span><span class="n">x</span><span class="p">,</span> <span class="o">%</span><span class="mi">1</span><span class="p">,</span> <span class="o">%</span><span class="mi">1</span><span class="p">)</span>
<span class="o">%</span><span class="mi">4</span> <span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="n">aten</span><span class="p">::</span><span class="n">size</span><span class="p">(</span><span class="o">%</span><span class="n">x</span><span class="p">,</span> <span class="o">%</span><span class="mi">1</span><span class="p">)</span>
<span class="o">%</span><span class="n">result</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">prim</span><span class="p">::</span><span class="n">Loop</span><span class="p">(</span><span class="o">%</span><span class="mi">4</span><span class="p">,</span> <span class="o">%</span><span class="mi">5</span><span class="p">,</span> <span class="o">%</span><span class="n">result</span><span class="mf">.1</span><span class="p">)</span>
<span class="n">block0</span><span class="p">(</span><span class="o">%</span><span class="n">i</span> <span class="p">:</span> <span class="nb">int</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="p">)</span> <span class="p">{</span>
<span class="o">%</span><span class="mi">10</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">aten</span><span class="p">::</span><span class="n">select</span><span class="p">(</span><span class="o">%</span><span class="n">x</span><span class="p">,</span> <span class="o">%</span><span class="mi">1</span><span class="p">,</span> <span class="o">%</span><span class="n">i</span><span class="p">)</span>
<span class="o">%</span><span class="n">result</span><span class="mf">.2</span> <span class="p">:</span> <span class="n">Tensor</span> <span class="o">=</span> <span class="n">aten</span><span class="p">::</span><span class="n">mul</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">10</span><span class="p">)</span>
<span class="o">-></span> <span class="p">(</span><span class="o">%</span><span class="mi">5</span><span class="p">,</span> <span class="o">%</span><span class="n">result</span><span class="mf">.2</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">return</span> <span class="p">(</span><span class="o">%</span><span class="n">result</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="tracer-warnings">
<h4>Tracer Warnings<a class="headerlink" href="#tracer-warnings" title="Permalink to this heading">¶</a></h4>
<p>The tracer produces warnings for several problematic patterns in traced
computation. As an example, take a trace of a function that contains an
in-place assignment on a slice (a view) of a Tensor:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fill_row_zero</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="o">*</span><span class="n">x</span><span class="o">.</span><span class="n">shape</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="k">return</span> <span class="n">x</span>
<span class="n">traced</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">trace</span><span class="p">(</span><span class="n">fill_row_zero</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</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="nb">print</span><span class="p">(</span><span class="n">traced</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
</pre></div>
</div>
<p>Produces several warnings and a graph which simply returns the input:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>fill_row_zero.py:4: TracerWarning: There are 2 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe.
x[0] = torch.rand(*x.shape[1:2])
fill_row_zero.py:6: TracerWarning: Output nr 1. of the traced function does not match the corresponding output of the Python function. Detailed error:
Not within tolerance rtol=1e-05 atol=1e-05 at input[0, 1] (0.09115803241729736 vs. 0.6782537698745728) and 3 other locations (33.00%)
traced = torch.jit.trace(fill_row_zero, (torch.rand(3, 4),))
graph(%0 : Float(3, 4)) {
return (%0);
}
</pre></div>
</div>
<p>We can fix this by modifying the code to not use the in-place update, but
rather build up the result tensor out-of-place with <code class="docutils literal notranslate"><span class="pre">torch.cat</span></code>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fill_row_zero</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="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">((</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="n">x</span><span class="o">.</span><span class="n">shape</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">x</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">dim</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="k">return</span> <span class="n">x</span>
<span class="n">traced</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">trace</span><span class="p">(</span><span class="n">fill_row_zero</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</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="nb">print</span><span class="p">(</span><span class="n">traced</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
</pre></div>
</div>
</section>
</section>
</section>
<section id="frequently-asked-questions">
<h2><a class="toc-backref" href="#id16" role="doc-backlink">Frequently Asked Questions</a><a class="headerlink" href="#frequently-asked-questions" title="Permalink to this heading">¶</a></h2>
<p>Q: I would like to train a model on GPU and do inference on CPU. What are the
best practices?</p>
<blockquote>
<div><p>First convert your model from GPU to CPU and then save it, like so:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">cpu_model</span> <span class="o">=</span> <span class="n">gpu_model</span><span class="o">.</span><span class="n">cpu</span><span class="p">()</span>
<span class="n">sample_input_cpu</span> <span class="o">=</span> <span class="n">sample_input_gpu</span><span class="o">.</span><span class="n">cpu</span><span class="p">()</span>
<span class="n">traced_cpu</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">trace</span><span class="p">(</span><span class="n">cpu_model</span><span class="p">,</span> <span class="n">sample_input_cpu</span><span class="p">)</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">traced_cpu</span><span class="p">,</span> <span class="s2">"cpu.pt"</span><span class="p">)</span>
<span class="n">traced_gpu</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">trace</span><span class="p">(</span><span class="n">gpu_model</span><span class="p">,</span> <span class="n">sample_input_gpu</span><span class="p">)</span>
<span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">traced_gpu</span><span class="p">,</span> <span class="s2">"gpu.pt"</span><span class="p">)</span>
<span class="c1"># ... later, when using the model:</span>
<span class="k">if</span> <span class="n">use_gpu</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">load</span><span class="p">(</span><span class="s2">"gpu.pt"</span><span class="p">)</span>
<span class="k">else</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">load</span><span class="p">(</span><span class="s2">"cpu.pt"</span><span class="p">)</span>
<span class="n">model</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
</pre></div>
</div>
<p>This is recommended because the tracer may witness tensor creation on a
specific device, so casting an already-loaded model may have unexpected
effects. Casting the model <em>before</em> saving it ensures that the tracer has
the correct device information.</p>
</div></blockquote>
<p>Q: How do I store attributes on a <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>?</p>
<blockquote>
<div><p>Say we have a model like:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</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="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">Model</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span>
<span class="n">m</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">Model</span><span class="p">())</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">Model</span></code> is instantiated it will result in a compilation error
since the compiler doesn’t know about <code class="docutils literal notranslate"><span class="pre">x</span></code>. There are 4 ways to inform the
compiler of attributes on <a class="reference internal" href="generated/torch.jit.ScriptModule.html#torch.jit.ScriptModule" title="torch.jit.ScriptModule"><code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code></a>:</p>
<p>1. <code class="docutils literal notranslate"><span class="pre">nn.Parameter</span></code> - Values wrapped in <code class="docutils literal notranslate"><span class="pre">nn.Parameter</span></code> will work as they
do on <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>s</p>
<p>2. <code class="docutils literal notranslate"><span class="pre">register_buffer</span></code> - Values wrapped in <code class="docutils literal notranslate"><span class="pre">register_buffer</span></code> will work as
they do on <code class="docutils literal notranslate"><span class="pre">nn.Module</span></code>s. This is equivalent to an attribute (see 4) of type
<code class="docutils literal notranslate"><span class="pre">Tensor</span></code>.</p>
<p>3. Constants - Annotating a class member as <code class="docutils literal notranslate"><span class="pre">Final</span></code> (or adding it to a list called
<code class="docutils literal notranslate"><span class="pre">__constants__</span></code> at the class definition level) will mark the contained names
as constants. Constants are saved directly in the code of the model. See