-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautograd.html
1828 lines (1635 loc) · 160 KB
/
autograd.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta name="robots" content="noindex">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Automatic differentiation package - torch.autograd — PyTorch 2.3 documentation</title>
<link rel="canonical" href="https://pytorch.org/docs/stable/autograd.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="torch.autograd.backward" href="generated/torch.autograd.backward.html" />
<link rel="prev" title="Automatic Mixed Precision package - torch.amp" href="amp.html" />
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-T8XT4PS');</script>
<!-- End Google Tag Manager -->
<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 class="main-menu-item">
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Learn
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/get-started">
<span class=dropdown-title>Get Started</span>
<p>Run PyTorch locally or get started quickly with one of the supported cloud platforms</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/tutorials">
<span class="dropdown-title">Tutorials</span>
<p>Whats new in PyTorch tutorials</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/tutorials/beginner/basics/intro.html">
<span class="dropdown-title">Learn the Basics</span>
<p>Familiarize yourself with PyTorch concepts and modules</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/tutorials/recipes/recipes_index.html">
<span class="dropdown-title">PyTorch Recipes</span>
<p>Bite-size, ready-to-deploy PyTorch code examples</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/tutorials/beginner/introyt.html">
<span class="dropdown-title">Intro to PyTorch - YouTube Series</span>
<p>Master PyTorch basics with our engaging YouTube tutorial series</p>
</a>
</div>
</div>
</li>
<li>
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Ecosystem
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/ecosystem">
<span class="dropdown-title">Tools</span>
<p>Learn about the tools and frameworks in the PyTorch Ecosystem</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://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/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/ecosystem/contributor-awards-2023">
<span class="dropdown-title">Contributor Awards - 2023</span>
<p>Award winners announced at this year's PyTorch Conference</p>
</a>
</div>
</div>
</li>
<li>
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Edge
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/edge">
<span class="dropdown-title">About PyTorch Edge</span>
<p>Build innovative and privacy-aware AI experiences for edge devices</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/executorch-overview">
<span class="dropdown-title">ExecuTorch</span>
<p>End-to-end solution for enabling on-device inference capabilities across mobile and edge devices</p>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Docs
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/docs/stable/index.html">
<span class="dropdown-title">PyTorch</span>
<p>Explore the documentation for comprehensive guidance on how to use PyTorch</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/pytorch-domains">
<span class="dropdown-title">PyTorch Domains</span>
<p>Read the PyTorch Domains documentation to learn more about domain-specific libraries</p>
</a>
</div>
</div>
</li>
<li>
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Blogs & News
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/blog/">
<span class="dropdown-title">PyTorch Blog</span>
<p>Catch up on the latest technical news and happenings</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/community-blog">
<span class="dropdown-title">Community Blog</span>
<p>Stories from the PyTorch ecosystem</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/videos">
<span class="dropdown-title">Videos</span>
<p>Learn about the latest PyTorch tutorials, new, and more </p>
<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/events">
<span class="dropdown-title">Events</span>
<p>Find events, webinars, and podcasts</p>
</a>
</div>
</li>
<li>
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
About
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/foundation">
<span class="dropdown-title">PyTorch Foundation</span>
<p>Learn more about the PyTorch Foundation</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/governing-board">
<span class="dropdown-title">Governing Board</span>
<p></p>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div class="no-dropdown">
<a href="https://pytorch.org/join" data-cta="join">
Become a Member
</a>
</div>
</li>
<li>
<div class="main-menu-item">
<a href="https://github.com/pytorch/pytorch" class="github-icon">
</a>
</div>
</li>
<!--- TODO: This block adds the search icon to the nav bar. We will enable it later.
<li>
<div class="main-menu-item">
<a href="https://github.com/pytorch/pytorch" class="search-icon">
</a>
</div>
</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'>2.3 ▼</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/extending.func.html">Extending torch.func with autograd.Function</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/fsdp.html">FSDP Notes</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 current"><a class="current reference internal" href="#">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="cpu.html">torch.cpu</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="torch_cuda_memory.html">Understanding CUDA Memory Usage</a></li>
<li class="toctree-l1"><a class="reference internal" href="torch_cuda_memory.html#generating-a-snapshot">Generating a Snapshot</a></li>
<li class="toctree-l1"><a class="reference internal" href="torch_cuda_memory.html#using-the-visualizer">Using the visualizer</a></li>
<li class="toctree-l1"><a class="reference internal" href="torch_cuda_memory.html#snapshot-api-reference">Snapshot API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="mps.html">torch.mps</a></li>
<li class="toctree-l1"><a class="reference internal" href="xpu.html">torch.xpu</a></li>
<li class="toctree-l1"><a class="reference internal" href="meta.html">Meta device</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="export.html">torch.export</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="distributed.tensor.parallel.html">torch.distributed.tensor.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.checkpoint.html">torch.distributed.checkpoint</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="torch.compiler.html">torch.compiler</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="func.html">torch.func</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="fx.experimental.html">torch.fx.experimental</a></li>
<li class="toctree-l1"><a class="reference internal" href="hub.html">torch.hub</a></li>
<li class="toctree-l1"><a class="reference internal" href="jit.html">torch.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="linalg.html">torch.linalg</a></li>
<li class="toctree-l1"><a class="reference internal" href="monitor.html">torch.monitor</a></li>
<li class="toctree-l1"><a class="reference internal" href="signal.html">torch.signal</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="nn.attention.html">torch.nn.attention</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="utils.html">torch.utils</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="deterministic.html">torch.utils.deterministic</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>
<li class="toctree-l1"><a class="reference internal" href="future_mod.html">torch.__future__</a></li>
<li class="toctree-l1"><a class="reference internal" href="logging.html">torch._logging</a></li>
<li class="toctree-l1"><a class="reference internal" href="torch_environment_variables.html">Torch Environment Variables</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="https://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>Automatic differentiation package - torch.autograd</li>
<li class="pytorch-breadcrumbs-aside">
<a href="_sources/autograd.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">
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-T8XT4PS"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<div class="rst-content">
<div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
<article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
<div class="section" id="module-torch.autograd">
<span id="automatic-differentiation-package-torch-autograd"></span><h1>Automatic differentiation package - torch.autograd<a class="headerlink" href="#module-torch.autograd" title="Permalink to this heading">¶</a></h1>
<p><code class="docutils literal notranslate"><span class="pre">torch.autograd</span></code> provides classes and functions implementing automatic
differentiation of arbitrary scalar valued functions. It requires minimal
changes to the existing code - you only need to declare <code class="xref py py-class docutils literal notranslate"><span class="pre">Tensor</span></code> s
for which gradients should be computed with the <code class="docutils literal notranslate"><span class="pre">requires_grad=True</span></code> keyword.
As of now, we only support autograd for floating point <code class="xref py py-class docutils literal notranslate"><span class="pre">Tensor</span></code> types (
half, float, double and bfloat16) and complex <code class="xref py py-class docutils literal notranslate"><span class="pre">Tensor</span></code> types (cfloat, cdouble).</p>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><p id="torch.autograd.backward"/><a class="reference internal" href="generated/torch.autograd.backward.html#torch.autograd.backward" title="torch.autograd.backward"><code class="xref py py-obj docutils literal notranslate"><span class="pre">backward</span></code></a></p></td>
<td><p>Computes the sum of gradients of given tensors with respect to graph leaves.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.autograd.grad"/><a class="reference internal" href="generated/torch.autograd.grad.html#torch.autograd.grad" title="torch.autograd.grad"><code class="xref py py-obj docutils literal notranslate"><span class="pre">grad</span></code></a></p></td>
<td><p>Computes and returns the sum of gradients of outputs with respect to the inputs.</p></td>
</tr>
</tbody>
</table>
<div class="section" id="forward-mode-automatic-differentiation">
<span id="forward-mode-ad"></span><h2>Forward-mode Automatic Differentiation<a class="headerlink" href="#forward-mode-automatic-differentiation" title="Permalink to this heading">¶</a></h2>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>This API is in beta. Even though the function signatures are very unlikely to change, improved
operator coverage is planned before we consider this stable.</p>
</div>
<p>Please see the <a class="reference external" href="https://pytorch.org/tutorials/intermediate/forward_ad_usage.html">forward-mode AD tutorial</a>
for detailed steps on how to use this API.</p>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.forward_ad.dual_level.html#torch.autograd.forward_ad.dual_level" title="torch.autograd.forward_ad.dual_level"><code class="xref py py-obj docutils literal notranslate"><span class="pre">forward_ad.dual_level</span></code></a></p></td>
<td><p>Context-manager for forward AD, where all forward AD computation must occur within the <code class="docutils literal notranslate"><span class="pre">dual_level</span></code> context.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.forward_ad.make_dual.html#torch.autograd.forward_ad.make_dual" title="torch.autograd.forward_ad.make_dual"><code class="xref py py-obj docutils literal notranslate"><span class="pre">forward_ad.make_dual</span></code></a></p></td>
<td><p>Associate a tensor value with its tangent to create a "dual tensor" for forward AD gradient computation.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.forward_ad.unpack_dual.html#torch.autograd.forward_ad.unpack_dual" title="torch.autograd.forward_ad.unpack_dual"><code class="xref py py-obj docutils literal notranslate"><span class="pre">forward_ad.unpack_dual</span></code></a></p></td>
<td><p>Unpack a "dual tensor" to get both its Tensor value and its forward AD gradient.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.forward_ad.enter_dual_level.html#torch.autograd.forward_ad.enter_dual_level" title="torch.autograd.forward_ad.enter_dual_level"><code class="xref py py-obj docutils literal notranslate"><span class="pre">forward_ad.enter_dual_level</span></code></a></p></td>
<td><p>Enter a new forward grad level.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.forward_ad.exit_dual_level.html#torch.autograd.forward_ad.exit_dual_level" title="torch.autograd.forward_ad.exit_dual_level"><code class="xref py py-obj docutils literal notranslate"><span class="pre">forward_ad.exit_dual_level</span></code></a></p></td>
<td><p>Exit a forward grad level.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.forward_ad.UnpackedDualTensor.html#torch.autograd.forward_ad.UnpackedDualTensor" title="torch.autograd.forward_ad.UnpackedDualTensor"><code class="xref py py-obj docutils literal notranslate"><span class="pre">forward_ad.UnpackedDualTensor</span></code></a></p></td>
<td><p>Namedtuple returned by <code class="xref py py-func docutils literal notranslate"><span class="pre">unpack_dual()</span></code> containing the primal and tangent components of the dual tensor.</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="functional-higher-level-api">
<span id="functional-api"></span><h2>Functional higher level API<a class="headerlink" href="#functional-higher-level-api" title="Permalink to this heading">¶</a></h2>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>This API is in beta. Even though the function signatures are very unlikely to change, major
improvements to performances are planned before we consider this stable.</p>
</div>
<p>This section contains the higher level API for the autograd that builds on the basic API above
and allows you to compute jacobians, hessians, etc.</p>
<p>This API works with user-provided functions that take only Tensors as input and return
only Tensors.
If your function takes other arguments that are not Tensors or Tensors that don’t have requires_grad set,
you can use a lambda to capture them.
For example, for a function <code class="docutils literal notranslate"><span class="pre">f</span></code> that takes three inputs, a Tensor for which we want the jacobian, another
tensor that should be considered constant and a boolean flag as <code class="docutils literal notranslate"><span class="pre">f(input,</span> <span class="pre">constant,</span> <span class="pre">flag=flag)</span></code>
you can use it as <code class="docutils literal notranslate"><span class="pre">functional.jacobian(lambda</span> <span class="pre">x:</span> <span class="pre">f(x,</span> <span class="pre">constant,</span> <span class="pre">flag=flag),</span> <span class="pre">input)</span></code>.</p>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.functional.jacobian.html#torch.autograd.functional.jacobian" title="torch.autograd.functional.jacobian"><code class="xref py py-obj docutils literal notranslate"><span class="pre">functional.jacobian</span></code></a></p></td>
<td><p>Compute the Jacobian of a given function.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.functional.hessian.html#torch.autograd.functional.hessian" title="torch.autograd.functional.hessian"><code class="xref py py-obj docutils literal notranslate"><span class="pre">functional.hessian</span></code></a></p></td>
<td><p>Compute the Hessian of a given scalar function.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.functional.vjp.html#torch.autograd.functional.vjp" title="torch.autograd.functional.vjp"><code class="xref py py-obj docutils literal notranslate"><span class="pre">functional.vjp</span></code></a></p></td>
<td><p>Compute the dot product between a vector <code class="docutils literal notranslate"><span class="pre">v</span></code> and the Jacobian of the given function at the point given by the inputs.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.functional.jvp.html#torch.autograd.functional.jvp" title="torch.autograd.functional.jvp"><code class="xref py py-obj docutils literal notranslate"><span class="pre">functional.jvp</span></code></a></p></td>
<td><p>Compute the dot product between the Jacobian of the given function at the point given by the inputs and a vector <code class="docutils literal notranslate"><span class="pre">v</span></code>.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.functional.vhp.html#torch.autograd.functional.vhp" title="torch.autograd.functional.vhp"><code class="xref py py-obj docutils literal notranslate"><span class="pre">functional.vhp</span></code></a></p></td>
<td><p>Compute the dot product between vector <code class="docutils literal notranslate"><span class="pre">v</span></code> and Hessian of a given scalar function at a specified point.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.functional.hvp.html#torch.autograd.functional.hvp" title="torch.autograd.functional.hvp"><code class="xref py py-obj docutils literal notranslate"><span class="pre">functional.hvp</span></code></a></p></td>
<td><p>Compute the dot product between the scalar function's Hessian and a vector <code class="docutils literal notranslate"><span class="pre">v</span></code> at a specified point.</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="locally-disabling-gradient-computation">
<span id="locally-disable-grad"></span><h2>Locally disabling gradient computation<a class="headerlink" href="#locally-disabling-gradient-computation" title="Permalink to this heading">¶</a></h2>
<p>See <a class="reference internal" href="notes/autograd.html#locally-disable-grad-doc"><span class="std std-ref">Locally disabling gradient computation</span></a> for more information on the differences
between no-grad and inference mode as well as other related mechanisms that
may be confused with the two. Also see <a class="reference internal" href="torch.html#torch-rst-local-disable-grad"><span class="std std-ref">Locally disabling gradient computation</span></a>
for a list of functions that can be used to locally disable gradients.</p>
</div>
<div class="section" id="default-gradient-layouts">
<span id="default-grad-layouts"></span><h2>Default gradient layouts<a class="headerlink" href="#default-gradient-layouts" title="Permalink to this heading">¶</a></h2>
<p>When a non-sparse <code class="docutils literal notranslate"><span class="pre">param</span></code> receives a non-sparse gradient during
<a class="reference internal" href="generated/torch.autograd.backward.html#torch.autograd.backward" title="torch.autograd.backward"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.autograd.backward()</span></code></a> or <a class="reference internal" href="generated/torch.Tensor.backward.html#torch.Tensor.backward" title="torch.Tensor.backward"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.Tensor.backward()</span></code></a>
<code class="docutils literal notranslate"><span class="pre">param.grad</span></code> is accumulated as follows.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">param.grad</span></code> is initially <code class="docutils literal notranslate"><span class="pre">None</span></code>:</p>
<ol class="arabic simple">
<li><p>If <code class="docutils literal notranslate"><span class="pre">param</span></code>’s memory is non-overlapping and dense, <code class="docutils literal notranslate"><span class="pre">.grad</span></code> is
created with strides matching <code class="docutils literal notranslate"><span class="pre">param</span></code> (thus matching <code class="docutils literal notranslate"><span class="pre">param</span></code>’s
layout).</p></li>
<li><p>Otherwise, <code class="docutils literal notranslate"><span class="pre">.grad</span></code> is created with rowmajor-contiguous strides.</p></li>
</ol>
<p>If <code class="docutils literal notranslate"><span class="pre">param</span></code> already has a non-sparse <code class="docutils literal notranslate"><span class="pre">.grad</span></code> attribute:</p>
<ol class="arabic simple" start="3">
<li><p>If <code class="docutils literal notranslate"><span class="pre">create_graph=False</span></code>, <code class="docutils literal notranslate"><span class="pre">backward()</span></code> accumulates into <code class="docutils literal notranslate"><span class="pre">.grad</span></code>
in-place, which preserves its strides.</p></li>
<li><p>If <code class="docutils literal notranslate"><span class="pre">create_graph=True</span></code>, <code class="docutils literal notranslate"><span class="pre">backward()</span></code> replaces <code class="docutils literal notranslate"><span class="pre">.grad</span></code> with a
new tensor <code class="docutils literal notranslate"><span class="pre">.grad</span> <span class="pre">+</span> <span class="pre">new</span> <span class="pre">grad</span></code>, which attempts (but does not guarantee)
matching the preexisting <code class="docutils literal notranslate"><span class="pre">.grad</span></code>’s strides.</p></li>
</ol>
<p>The default behavior (letting <code class="docutils literal notranslate"><span class="pre">.grad</span></code>s be <code class="docutils literal notranslate"><span class="pre">None</span></code> before the first
<code class="docutils literal notranslate"><span class="pre">backward()</span></code>, such that their layout is created according to 1 or 2,
and retained over time according to 3 or 4) is recommended for best performance.
Calls to <code class="docutils literal notranslate"><span class="pre">model.zero_grad()</span></code> or <code class="docutils literal notranslate"><span class="pre">optimizer.zero_grad()</span></code> will not affect <code class="docutils literal notranslate"><span class="pre">.grad</span></code>
layouts.</p>
<p>In fact, resetting all <code class="docutils literal notranslate"><span class="pre">.grad</span></code>s to <code class="docutils literal notranslate"><span class="pre">None</span></code> before each
accumulation phase, e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">iterations</span><span class="o">...</span>
<span class="o">...</span>
<span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">parameters</span><span class="p">():</span>
<span class="n">param</span><span class="o">.</span><span class="n">grad</span> <span class="o">=</span> <span class="kc">None</span>
<span class="n">loss</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span>
</pre></div>
</div>
<p>such that they’re recreated according to 1 or 2 every time,
is a valid alternative to <code class="docutils literal notranslate"><span class="pre">model.zero_grad()</span></code> or <code class="docutils literal notranslate"><span class="pre">optimizer.zero_grad()</span></code>
that may improve performance for some networks.</p>
<div class="section" id="manual-gradient-layouts">
<h3>Manual gradient layouts<a class="headerlink" href="#manual-gradient-layouts" title="Permalink to this heading">¶</a></h3>
<p>If you need manual control over <code class="docutils literal notranslate"><span class="pre">.grad</span></code>’s strides,
assign <code class="docutils literal notranslate"><span class="pre">param.grad</span> <span class="pre">=</span></code> a zeroed tensor with desired strides
before the first <code class="docutils literal notranslate"><span class="pre">backward()</span></code>, and never reset it to <code class="docutils literal notranslate"><span class="pre">None</span></code>.
3 guarantees your layout is preserved as long as <code class="docutils literal notranslate"><span class="pre">create_graph=False</span></code>.
4 indicates your layout is <em>likely</em> preserved even if <code class="docutils literal notranslate"><span class="pre">create_graph=True</span></code>.</p>
</div>
</div>
<div class="section" id="in-place-operations-on-tensors">
<h2>In-place operations on Tensors<a class="headerlink" href="#in-place-operations-on-tensors" title="Permalink to this heading">¶</a></h2>
<p>Supporting in-place operations in autograd is a hard matter, and we discourage
their use in most cases. Autograd’s aggressive buffer freeing and reuse makes
it very efficient and there are very few occasions when in-place operations
actually lower memory usage by any significant amount. Unless you’re operating
under heavy memory pressure, you might never need to use them.</p>
<div class="section" id="in-place-correctness-checks">
<h3>In-place correctness checks<a class="headerlink" href="#in-place-correctness-checks" title="Permalink to this heading">¶</a></h3>
<p>All <code class="xref py py-class docutils literal notranslate"><span class="pre">Tensor</span></code> s keep track of in-place operations applied to them, and
if the implementation detects that a tensor was saved for backward in one of
the functions, but it was modified in-place afterwards, an error will be raised
once backward pass is started. This ensures that if you’re using in-place
functions and not seeing any errors, you can be sure that the computed
gradients are correct.</p>
</div>
</div>
<div class="section" id="variable-deprecated">
<h2>Variable (deprecated)<a class="headerlink" href="#variable-deprecated" title="Permalink to this heading">¶</a></h2>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The Variable API has been deprecated: Variables are no longer necessary to
use autograd with tensors. Autograd automatically supports Tensors with
<code class="docutils literal notranslate"><span class="pre">requires_grad</span></code> set to <code class="docutils literal notranslate"><span class="pre">True</span></code>. Below please find a quick guide on what
has changed:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">Variable(tensor)</span></code> and <code class="docutils literal notranslate"><span class="pre">Variable(tensor,</span> <span class="pre">requires_grad)</span></code> still work as expected,
but they return Tensors instead of Variables.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">var.data</span></code> is the same thing as <code class="docutils literal notranslate"><span class="pre">tensor.data</span></code>.</p></li>
<li><p>Methods such as <code class="docutils literal notranslate"><span class="pre">var.backward(),</span> <span class="pre">var.detach(),</span> <span class="pre">var.register_hook()</span></code> now work on tensors
with the same method names.</p></li>
</ul>
<p>In addition, one can now create tensors with <code class="docutils literal notranslate"><span class="pre">requires_grad=True</span></code> using factory
methods such as <a class="reference internal" href="generated/torch.randn.html#torch.randn" title="torch.randn"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.randn()</span></code></a>, <a class="reference internal" href="generated/torch.zeros.html#torch.zeros" title="torch.zeros"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.zeros()</span></code></a>, <a class="reference internal" href="generated/torch.ones.html#torch.ones" title="torch.ones"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.ones()</span></code></a>, and others
like the following:</p>
<p><code class="docutils literal notranslate"><span class="pre">autograd_tensor</span> <span class="pre">=</span> <span class="pre">torch.randn((2,</span> <span class="pre">3,</span> <span class="pre">4),</span> <span class="pre">requires_grad=True)</span></code></p>
</div>
</div>
<div class="section" id="tensor-autograd-functions">
<h2>Tensor autograd functions<a class="headerlink" href="#tensor-autograd-functions" title="Permalink to this heading">¶</a></h2>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><code class="xref py py-obj docutils literal notranslate"><span class="pre">torch.Tensor.grad</span></code></p></td>
<td><p>This attribute is <code class="docutils literal notranslate"><span class="pre">None</span></code> by default and becomes a Tensor the first time a call to <a class="reference internal" href="generated/torch.autograd.backward.html#torch.autograd.backward" title="torch.autograd.backward"><code class="xref py py-func docutils literal notranslate"><span class="pre">backward()</span></code></a> computes gradients for <code class="docutils literal notranslate"><span class="pre">self</span></code>.</p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-obj docutils literal notranslate"><span class="pre">torch.Tensor.requires_grad</span></code></p></td>
<td><p>Is <code class="docutils literal notranslate"><span class="pre">True</span></code> if gradients need to be computed for this Tensor, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-obj docutils literal notranslate"><span class="pre">torch.Tensor.is_leaf</span></code></p></td>
<td><p>All Tensors that have <code class="xref py py-attr docutils literal notranslate"><span class="pre">requires_grad</span></code> which is <code class="docutils literal notranslate"><span class="pre">False</span></code> will be leaf Tensors by convention.</p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-obj docutils literal notranslate"><span class="pre">torch.Tensor.backward</span></code>([gradient, ...])</p></td>
<td><p>Computes the gradient of current tensor wrt graph leaves.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-obj docutils literal notranslate"><span class="pre">torch.Tensor.detach</span></code></p></td>
<td><p>Returns a new Tensor, detached from the current graph.</p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-obj docutils literal notranslate"><span class="pre">torch.Tensor.detach_</span></code></p></td>
<td><p>Detaches the Tensor from the graph that created it, making it a leaf.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-obj docutils literal notranslate"><span class="pre">torch.Tensor.register_hook</span></code>(hook)</p></td>
<td><p>Registers a backward hook.</p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-obj docutils literal notranslate"><span class="pre">torch.Tensor.register_post_accumulate_grad_hook</span></code>(hook)</p></td>
<td><p>Registers a backward hook that runs after grad accumulation.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-obj docutils literal notranslate"><span class="pre">torch.Tensor.retain_grad</span></code>()</p></td>
<td><p>Enables this Tensor to have their <a class="reference internal" href="generated/torch.autograd.grad.html#torch.autograd.grad" title="torch.autograd.grad"><code class="xref py py-attr docutils literal notranslate"><span class="pre">grad</span></code></a> populated during <a class="reference internal" href="generated/torch.autograd.backward.html#torch.autograd.backward" title="torch.autograd.backward"><code class="xref py py-func docutils literal notranslate"><span class="pre">backward()</span></code></a>.</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="function">
<h2><span class="hidden-section">Function</span><a class="headerlink" href="#function" title="Permalink to this heading">¶</a></h2>
<dl class="py class">
<dt class="sig sig-object py" id="torch.autograd.Function">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">torch.autograd.</span></span><span class="sig-name descname"><span class="pre">Function</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/autograd/function.html#Function"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.autograd.Function" title="Permalink to this definition">¶</a></dt>
<dd><p>Base class to create custom <cite>autograd.Function</cite>.</p>
<p>To create a custom <cite>autograd.Function</cite>, subclass this class and implement
the <a class="reference internal" href="generated/torch.autograd.Function.forward.html#torch.autograd.Function.forward" title="torch.autograd.Function.forward"><code class="xref py py-meth docutils literal notranslate"><span class="pre">forward()</span></code></a> and <a class="reference internal" href="generated/torch.autograd.backward.html#torch.autograd.backward" title="torch.autograd.backward"><code class="xref py py-meth docutils literal notranslate"><span class="pre">backward()</span></code></a> static methods. Then, to use your custom
op in the forward pass, call the class method <code class="docutils literal notranslate"><span class="pre">apply</span></code>. Do not call
<a class="reference internal" href="generated/torch.autograd.Function.forward.html#torch.autograd.Function.forward" title="torch.autograd.Function.forward"><code class="xref py py-meth docutils literal notranslate"><span class="pre">forward()</span></code></a> directly.</p>
<p>To ensure correctness and best performance, make sure you are calling the
correct methods on <code class="docutils literal notranslate"><span class="pre">ctx</span></code> and validating your backward function using
<a class="reference internal" href="#module-torch.autograd.gradcheck" title="torch.autograd.gradcheck"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.autograd.gradcheck()</span></code></a>.</p>
<p>See <a class="reference internal" href="notes/extending.html#extending-autograd"><span class="std std-ref">Extending torch.autograd</span></a> for more details on how to use this class.</p>
<p>Examples:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Exp</span><span class="p">(</span><span class="n">Function</span><span class="p">):</span>
<span class="gp">>>> </span> <span class="nd">@staticmethod</span>
<span class="gp">>>> </span> <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
<span class="gp">>>> </span> <span class="n">result</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">exp</span><span class="p">()</span>
<span class="gp">>>> </span> <span class="n">ctx</span><span class="o">.</span><span class="n">save_for_backward</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
<span class="gp">>>> </span> <span class="k">return</span> <span class="n">result</span>
<span class="go">>>></span>
<span class="gp">>>> </span> <span class="nd">@staticmethod</span>
<span class="gp">>>> </span> <span class="k">def</span> <span class="nf">backward</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">grad_output</span><span class="p">):</span>
<span class="gp">>>> </span> <span class="n">result</span><span class="p">,</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">saved_tensors</span>
<span class="gp">>>> </span> <span class="k">return</span> <span class="n">grad_output</span> <span class="o">*</span> <span class="n">result</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="c1"># Use it by calling the apply method:</span>
<span class="gp">>>> </span><span class="n">output</span> <span class="o">=</span> <span class="n">Exp</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.Function.forward.html#torch.autograd.Function.forward" title="torch.autograd.Function.forward"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Function.forward</span></code></a></p></td>
<td><p>Define the forward of the custom autograd Function.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.Function.backward.html#torch.autograd.Function.backward" title="torch.autograd.Function.backward"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Function.backward</span></code></a></p></td>
<td><p>Define a formula for differentiating the operation with backward mode automatic differentiation.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.Function.jvp.html#torch.autograd.Function.jvp" title="torch.autograd.Function.jvp"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Function.jvp</span></code></a></p></td>
<td><p>Define a formula for differentiating the operation with forward mode automatic differentiation.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.Function.vmap.html#torch.autograd.Function.vmap" title="torch.autograd.Function.vmap"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Function.vmap</span></code></a></p></td>
<td><p>Define the behavior for this autograd.Function underneath <a class="reference internal" href="generated/torch.vmap.html#torch.vmap" title="torch.vmap"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.vmap()</span></code></a>.</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="context-method-mixins">
<h2>Context method mixins<a class="headerlink" href="#context-method-mixins" title="Permalink to this heading">¶</a></h2>
<p>When creating a new <a class="reference internal" href="#torch.autograd.Function" title="torch.autograd.Function"><code class="xref py py-class docutils literal notranslate"><span class="pre">Function</span></code></a>, the following methods are available to <cite>ctx</cite>.</p>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.function.FunctionCtx.mark_dirty.html#torch.autograd.function.FunctionCtx.mark_dirty" title="torch.autograd.function.FunctionCtx.mark_dirty"><code class="xref py py-obj docutils literal notranslate"><span class="pre">function.FunctionCtx.mark_dirty</span></code></a></p></td>
<td><p>Mark given tensors as modified in an in-place operation.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.function.FunctionCtx.mark_non_differentiable.html#torch.autograd.function.FunctionCtx.mark_non_differentiable" title="torch.autograd.function.FunctionCtx.mark_non_differentiable"><code class="xref py py-obj docutils literal notranslate"><span class="pre">function.FunctionCtx.mark_non_differentiable</span></code></a></p></td>
<td><p>Mark outputs as non-differentiable.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.function.FunctionCtx.save_for_backward.html#torch.autograd.function.FunctionCtx.save_for_backward" title="torch.autograd.function.FunctionCtx.save_for_backward"><code class="xref py py-obj docutils literal notranslate"><span class="pre">function.FunctionCtx.save_for_backward</span></code></a></p></td>
<td><p>Save given tensors for a future call to <a class="reference internal" href="generated/torch.autograd.Function.backward.html#torch.autograd.Function.backward" title="torch.autograd.Function.backward"><code class="xref py py-func docutils literal notranslate"><span class="pre">backward()</span></code></a>.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.function.FunctionCtx.set_materialize_grads.html#torch.autograd.function.FunctionCtx.set_materialize_grads" title="torch.autograd.function.FunctionCtx.set_materialize_grads"><code class="xref py py-obj docutils literal notranslate"><span class="pre">function.FunctionCtx.set_materialize_grads</span></code></a></p></td>
<td><p>Set whether to materialize grad tensors.</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="custom-function-utilities">
<h2>Custom Function utilities<a class="headerlink" href="#custom-function-utilities" title="Permalink to this heading">¶</a></h2>
<p>Decorator for backward method.</p>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.function.once_differentiable.html#torch.autograd.function.once_differentiable" title="torch.autograd.function.once_differentiable"><code class="xref py py-obj docutils literal notranslate"><span class="pre">function.once_differentiable</span></code></a></p></td>
<td><p></p></td>
</tr>
</tbody>
</table>
<p>Base custom <a class="reference internal" href="#torch.autograd.Function" title="torch.autograd.Function"><code class="xref py py-class docutils literal notranslate"><span class="pre">Function</span></code></a> used to build PyTorch utilities</p>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.function.BackwardCFunction.html#torch.autograd.function.BackwardCFunction" title="torch.autograd.function.BackwardCFunction"><code class="xref py py-obj docutils literal notranslate"><span class="pre">function.BackwardCFunction</span></code></a></p></td>
<td><p>This class is used for internal autograd work.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.function.InplaceFunction.html#torch.autograd.function.InplaceFunction" title="torch.autograd.function.InplaceFunction"><code class="xref py py-obj docutils literal notranslate"><span class="pre">function.InplaceFunction</span></code></a></p></td>
<td><p>This class is here only for backward compatibility reasons.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.function.NestedIOFunction.html#torch.autograd.function.NestedIOFunction" title="torch.autograd.function.NestedIOFunction"><code class="xref py py-obj docutils literal notranslate"><span class="pre">function.NestedIOFunction</span></code></a></p></td>
<td><p>This class is here only for backward compatibility reasons.</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="module-torch.autograd.gradcheck">
<span id="numerical-gradient-checking"></span><span id="grad-check"></span><h2>Numerical gradient checking<a class="headerlink" href="#module-torch.autograd.gradcheck" title="Permalink to this heading">¶</a></h2>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><p id="torch.autograd.gradcheck.gradcheck"/><a class="reference internal" href="generated/torch.autograd.gradcheck.gradcheck.html#torch.autograd.gradcheck.gradcheck" title="torch.autograd.gradcheck.gradcheck"><code class="xref py py-obj docutils literal notranslate"><span class="pre">gradcheck</span></code></a></p></td>
<td><p>Check gradients computed via small finite differences against analytical gradients wrt tensors in <code class="xref py py-attr docutils literal notranslate"><span class="pre">inputs</span></code> that are of floating point or complex type and with <code class="docutils literal notranslate"><span class="pre">requires_grad=True</span></code>.</p></td>
</tr>
<tr class="row-even"><td><p><p id="torch.autograd.gradcheck.gradgradcheck"/><a class="reference internal" href="generated/torch.autograd.gradcheck.gradgradcheck.html#torch.autograd.gradcheck.gradgradcheck" title="torch.autograd.gradcheck.gradgradcheck"><code class="xref py py-obj docutils literal notranslate"><span class="pre">gradgradcheck</span></code></a></p></td>
<td><p>Check gradients of gradients computed via small finite differences against analytical gradients wrt tensors in <code class="xref py py-attr docutils literal notranslate"><span class="pre">inputs</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">grad_outputs</span></code> that are of floating point or complex type and with <code class="docutils literal notranslate"><span class="pre">requires_grad=True</span></code>.</p></td>
</tr>
<tr class="row-odd"><td><p><p id="torch.autograd.gradcheck.GradcheckError"/><a class="reference internal" href="generated/torch.autograd.gradcheck.GradcheckError.html#torch.autograd.gradcheck.GradcheckError" title="torch.autograd.gradcheck.GradcheckError"><code class="xref py py-obj docutils literal notranslate"><span class="pre">GradcheckError</span></code></a></p></td>
<td><p>Error raised by <a class="reference internal" href="generated/torch.autograd.gradcheck.gradcheck.html#torch.autograd.gradcheck.gradcheck" title="torch.autograd.gradcheck.gradcheck"><code class="xref py py-func docutils literal notranslate"><span class="pre">gradcheck()</span></code></a> and <a class="reference internal" href="generated/torch.autograd.gradcheck.gradgradcheck.html#torch.autograd.gradcheck.gradgradcheck" title="torch.autograd.gradcheck.gradgradcheck"><code class="xref py py-func docutils literal notranslate"><span class="pre">gradgradcheck()</span></code></a>.</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="profiler">
<h2>Profiler<a class="headerlink" href="#profiler" title="Permalink to this heading">¶</a></h2>
<p>Autograd includes a profiler that lets you inspect the cost of different
operators inside your model - both on the CPU and GPU. There are three modes
implemented at the moment - CPU-only using <a class="reference internal" href="#torch.autograd.profiler.profile" title="torch.autograd.profiler.profile"><code class="xref py py-class docutils literal notranslate"><span class="pre">profile</span></code></a>.
nvprof based (registers both CPU and GPU activity) using
<a class="reference internal" href="#torch.autograd.profiler.emit_nvtx" title="torch.autograd.profiler.emit_nvtx"><code class="xref py py-class docutils literal notranslate"><span class="pre">emit_nvtx</span></code></a>.
and vtune profiler based using
<a class="reference internal" href="#torch.autograd.profiler.emit_itt" title="torch.autograd.profiler.emit_itt"><code class="xref py py-class docutils literal notranslate"><span class="pre">emit_itt</span></code></a>.</p>
<dl class="py class">
<dt class="sig sig-object py" id="torch.autograd.profiler.profile">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">torch.autograd.profiler.</span></span><span class="sig-name descname"><span class="pre">profile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">enabled</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_cuda</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_device</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">record_shapes</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">with_flops</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">profile_memory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">with_stack</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">with_modules</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_kineto</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_cpu</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_mtia</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">experimental_config</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/autograd/profiler.html#profile"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.autograd.profiler.profile" title="Permalink to this definition">¶</a></dt>
<dd><p>Context manager that manages autograd profiler state and holds a summary of results.</p>
<p>Under the hood it just records events of functions being executed in C++ and
exposes those events to Python. You can wrap any code into it and it will
only report runtime of PyTorch functions.
Note: profiler is thread local and is automatically propagated into the async tasks</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>enabled</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – Setting this to False makes this context manager a no-op.</p></li>
<li><p><strong>use_cuda</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – Enables timing of CUDA events as well using the cudaEvent API.
Adds approximately 4us of overhead to each tensor operation.</p></li>
<li><p><strong>record_shapes</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – If shapes recording is set, information
about input dimensions will be collected. This allows one to see which
dimensions have been used under the hood and further group by them
using prof.key_averages(group_by_input_shape=True). Please note that
shape recording might skew your profiling data. It is recommended to
use separate runs with and without shape recording to validate the timing.
Most likely the skew will be negligible for bottom most events (in a case
of nested function calls). But for higher level functions the total
self cpu time might be artificially increased because of the shape
collection.</p></li>
<li><p><strong>with_flops</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – If with_flops is set, the profiler will estimate
the FLOPs (floating point operations) value using the operator’s input shape.
This allows one to estimate the hardware performance. Currently,
this option only works for the matrix multiplication and 2D convolution operators.</p></li>
<li><p><strong>profile_memory</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – track tensor memory allocation/deallocation.</p></li>
<li><p><strong>with_stack</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – record source information (file and line number) for the ops.</p></li>
<li><p><strong>with_modules</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a>) – record module hierarchy (including function names)
corresponding to the callstack of the op. e.g. If module A’s forward call’s
module B’s forward which contains an aten::add op,
then aten::add’s module hierarchy is A.B
Note that this support exist, at the moment, only for TorchScript models
and not eager mode models.</p></li>
<li><p><strong>use_kineto</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – experimental, enable profiling with Kineto profiler.</p></li>
<li><p><strong>use_cpu</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – profile CPU events; setting to <code class="docutils literal notranslate"><span class="pre">False</span></code> requires
<code class="docutils literal notranslate"><span class="pre">use_kineto=True</span></code> and can be used to lower the overhead for GPU-only profiling.</p></li>
<li><p><strong>experimental_config</strong> (<em>_ExperimentalConfig</em>) – A set of experimental options
used by profiler libraries like Kineto. Note, backward compatibility is not guaranteed.</p></li>
</ul>
</dd>
</dl>
<p class="rubric">Example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">requires_grad</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">with</span> <span class="n">torch</span><span class="o">.</span><span class="n">autograd</span><span class="o">.</span><span class="n">profiler</span><span class="o">.</span><span class="n">profile</span><span class="p">()</span> <span class="k">as</span> <span class="n">prof</span><span class="p">:</span>
<span class="gp">>>> </span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">):</span> <span class="c1"># any normal python code, really!</span>
<span class="gp">>>> </span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span>
<span class="gp">>>> </span> <span class="n">y</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span>
<span class="gp">>>> </span><span class="c1"># NOTE: some columns were removed for brevity</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">prof</span><span class="o">.</span><span class="n">key_averages</span><span class="p">()</span><span class="o">.</span><span class="n">table</span><span class="p">(</span><span class="n">sort_by</span><span class="o">=</span><span class="s2">"self_cpu_time_total"</span><span class="p">))</span>
<span class="go">----------------------------------- --------------- --------------- ---------------</span>
<span class="go">Name Self CPU total CPU time avg Number of Calls</span>
<span class="go">----------------------------------- --------------- --------------- ---------------</span>
<span class="go">mul 32.048ms 32.048ms 200</span>
<span class="go">pow 27.041ms 27.041ms 200</span>
<span class="go">PowBackward0 9.727ms 55.483ms 100</span>
<span class="go">torch::autograd::AccumulateGrad 9.148ms 9.148ms 100</span>
<span class="go">torch::autograd::GraphRoot 691.816us 691.816us 100</span>
<span class="go">----------------------------------- --------------- --------------- ---------------</span>
</pre></div>
</div>
</dd></dl>
<table class="autosummary longtable docutils colwidths-auto align-default">
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.profiler.profile.export_chrome_trace.html#torch.autograd.profiler.profile.export_chrome_trace" title="torch.autograd.profiler.profile.export_chrome_trace"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler.profile.export_chrome_trace</span></code></a></p></td>
<td><p>Export an EventList as a Chrome tracing tools file.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.profiler.profile.key_averages.html#torch.autograd.profiler.profile.key_averages" title="torch.autograd.profiler.profile.key_averages"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler.profile.key_averages</span></code></a></p></td>
<td><p>Averages all function events over their keys.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.profiler.profile.self_cpu_time_total.html#torch.autograd.profiler.profile.self_cpu_time_total" title="torch.autograd.profiler.profile.self_cpu_time_total"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler.profile.self_cpu_time_total</span></code></a></p></td>
<td><p>Returns total time spent on CPU.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.profiler.profile.total_average.html#torch.autograd.profiler.profile.total_average" title="torch.autograd.profiler.profile.total_average"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler.profile.total_average</span></code></a></p></td>
<td><p>Averages all events.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.profiler.parse_nvprof_trace.html#torch.autograd.profiler.parse_nvprof_trace" title="torch.autograd.profiler.parse_nvprof_trace"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler.parse_nvprof_trace</span></code></a></p></td>
<td><p></p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.profiler.EnforceUnique.html#torch.autograd.profiler.EnforceUnique" title="torch.autograd.profiler.EnforceUnique"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler.EnforceUnique</span></code></a></p></td>
<td><p>Raises an error if a key is seen more than once.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.profiler.KinetoStepTracker.html#torch.autograd.profiler.KinetoStepTracker" title="torch.autograd.profiler.KinetoStepTracker"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler.KinetoStepTracker</span></code></a></p></td>
<td><p>Provides an abstraction for incrementing the step count globally.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.profiler.record_function.html#torch.autograd.profiler.record_function" title="torch.autograd.profiler.record_function"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler.record_function</span></code></a></p></td>
<td><p>Context manager/function decorator that adds a label to a code block/function when running autograd profiler.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.profiler_util.Interval.html#torch.autograd.profiler_util.Interval" title="torch.autograd.profiler_util.Interval"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler_util.Interval</span></code></a></p></td>
<td><p></p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.profiler_util.Kernel.html#torch.autograd.profiler_util.Kernel" title="torch.autograd.profiler_util.Kernel"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler_util.Kernel</span></code></a></p></td>
<td><p></p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/torch.autograd.profiler_util.MemRecordsAcc.html#torch.autograd.profiler_util.MemRecordsAcc" title="torch.autograd.profiler_util.MemRecordsAcc"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler_util.MemRecordsAcc</span></code></a></p></td>
<td><p>Acceleration structure for accessing mem_records in interval.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/torch.autograd.profiler_util.StringTable.html#torch.autograd.profiler_util.StringTable" title="torch.autograd.profiler_util.StringTable"><code class="xref py py-obj docutils literal notranslate"><span class="pre">profiler_util.StringTable</span></code></a></p></td>
<td><p></p></td>
</tr>
</tbody>
</table>
<dl class="py class">
<dt class="sig sig-object py" id="torch.autograd.profiler.emit_nvtx">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">torch.autograd.profiler.</span></span><span class="sig-name descname"><span class="pre">emit_nvtx</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">enabled</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">record_shapes</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/autograd/profiler.html#emit_nvtx"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.autograd.profiler.emit_nvtx" title="Permalink to this definition">¶</a></dt>
<dd><p>Context manager that makes every autograd operation emit an NVTX range.</p>
<p>It is useful when running the program under nvprof:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">nvprof</span> <span class="o">--</span><span class="n">profile</span><span class="o">-</span><span class="n">from</span><span class="o">-</span><span class="n">start</span> <span class="n">off</span> <span class="o">-</span><span class="n">o</span> <span class="n">trace_name</span><span class="o">.</span><span class="n">prof</span> <span class="o">--</span> <span class="o"><</span><span class="n">regular</span> <span class="n">command</span> <span class="n">here</span><span class="o">></span>
</pre></div>
</div>
<p>Unfortunately, there’s no way to force nvprof to flush the data it collected
to disk, so for CUDA profiling one has to use this context manager to annotate
nvprof traces and wait for the process to exit before inspecting them.
Then, either NVIDIA Visual Profiler (nvvp) can be used to visualize the timeline, or
<a class="reference internal" href="generated/torch.autograd.profiler.load_nvprof.html#torch.autograd.profiler.load_nvprof" title="torch.autograd.profiler.load_nvprof"><code class="xref py py-func docutils literal notranslate"><span class="pre">torch.autograd.profiler.load_nvprof()</span></code></a> can load the results for inspection
e.g. in Python REPL.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>enabled</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – Setting <code class="docutils literal notranslate"><span class="pre">enabled=False</span></code> makes this context manager a no-op.
Default: <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p></li>
<li><p><strong>record_shapes</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.12)"><em>bool</em></a><em>, </em><em>optional</em>) – If <code class="docutils literal notranslate"><span class="pre">record_shapes=True</span></code>, the nvtx range wrapping
each autograd op will append information about the sizes of Tensor arguments received
by that op, in the following format:
<code class="docutils literal notranslate"><span class="pre">[[arg0.size(0),</span> <span class="pre">arg0.size(1),</span> <span class="pre">...],</span> <span class="pre">[arg1.size(0),</span> <span class="pre">arg1.size(1),</span> <span class="pre">...],</span> <span class="pre">...]</span></code>
Non-tensor arguments will be represented by <code class="docutils literal notranslate"><span class="pre">[]</span></code>.
Arguments will be listed in the order they are received by the backend op.
Please note that this order may not match the order in which those arguments were passed
on the Python side. Also note that shape recording may increase the overhead of nvtx range creation.
Default: <code class="docutils literal notranslate"><span class="pre">False</span></code></p></li>
</ul>
</dd>
</dl>
<p class="rubric">Example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">torch</span><span class="o">.</span><span class="n">cuda</span><span class="o">.</span><span class="n">profiler</span><span class="o">.</span><span class="n">profile</span><span class="p">():</span>
<span class="gp">... </span> <span class="n">model</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c1"># Warmup CUDA memory allocator and profiler</span>
<span class="gp">... </span> <span class="k">with</span> <span class="n">torch</span><span class="o">.</span><span class="n">autograd</span><span class="o">.</span><span class="n">profiler</span><span class="o">.</span><span class="n">emit_nvtx</span><span class="p">():</span>
<span class="gp">... </span> <span class="n">model</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<p><strong>Forward-backward correlation</strong></p>
<p>When viewing a profile created using <a class="reference internal" href="#torch.autograd.profiler.emit_nvtx" title="torch.autograd.profiler.emit_nvtx"><code class="xref py py-class docutils literal notranslate"><span class="pre">emit_nvtx</span></code></a> in the Nvidia Visual Profiler,
correlating each backward-pass op with the corresponding forward-pass op can be difficult.
To ease this task, <a class="reference internal" href="#torch.autograd.profiler.emit_nvtx" title="torch.autograd.profiler.emit_nvtx"><code class="xref py py-class docutils literal notranslate"><span class="pre">emit_nvtx</span></code></a> appends sequence number information to the ranges it
generates.</p>
<p>During the forward pass, each function range is decorated with <code class="docutils literal notranslate"><span class="pre">seq=<N></span></code>. <code class="docutils literal notranslate"><span class="pre">seq</span></code> is a running
counter, incremented each time a new backward Function object is created and stashed for backward.
Thus, the <code class="docutils literal notranslate"><span class="pre">seq=<N></span></code> annotation associated with each forward function range tells you that
if a backward Function object is created by this forward function,
the backward object will receive sequence number N.
During the backward pass, the top-level range wrapping each C++ backward Function’s
<code class="docutils literal notranslate"><span class="pre">apply()</span></code> call is decorated with <code class="docutils literal notranslate"><span class="pre">stashed</span> <span class="pre">seq=<M></span></code>. <code class="docutils literal notranslate"><span class="pre">M</span></code> is the sequence number that
the backward object was created with. By comparing <code class="docutils literal notranslate"><span class="pre">stashed</span> <span class="pre">seq</span></code> numbers in backward with <code class="docutils literal notranslate"><span class="pre">seq</span></code>
numbers in forward, you can track down which forward op created each backward Function.</p>
<p>Any functions executed during the backward pass are also decorated with <code class="docutils literal notranslate"><span class="pre">seq=<N></span></code>. During