-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathindex.html
1432 lines (1173 loc) · 90.7 KB
/
index.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>
<html lang="en">
<head>
<!-- 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 -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?">
<title>
PyTorch 2.x | PyTorch
</title>
<meta name="robots" content="index, follow" />
<meta name="description" content="Overview
" />
<meta property="og:image" content="https://pytorch.org/assets/images/featured-img-pytorch-2.png" />
<meta name="twitter:image" content="https://pytorch.org/assets/images/featured-img-pytorch-2.png" />
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="website" />
<meta property="og:title" content="PyTorch 2.x" />
<meta property="og:description" content="Overview
" />
<meta property="og:site_name" content="PyTorch" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="PyTorch 2.x" />
<meta name="twitter:description" content="Overview
" />
<link rel="stylesheet" href="/assets/main.css">
<script src="/assets/vendor/jquery.min.js"></script>
<script src="/assets/vendor/popper.min.js"></script>
<script src="/assets/vendor/bootstrap.min.js"></script>
<script src="/assets/vendor/anchor.min.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
inlineMath: [['$','$']]
}
});
</script>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"></script>
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window,document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '243028289693773');
fbq('track', 'PageView');
</script>
<noscript>
<img height="1" width="1"
src="https://www.facebook.com/tr?id=243028289693773&ev=PageView
&noscript=1"/>
</noscript>
<!-- Twitter universal website tag code -->
<img height="1" width="1" style="display:none;" alt="" src="https://analytics.twitter.com/i/adsct?p_id=Twitter&p_user_id=0&txn_id=o2gi1&events=%5B%5B%22pageview%22%2Cnull%5D%5D&tw_sale_amount=0&tw_order_quantity=0 (https://urldefense.proofpoint.com/v2/url?u=https-3A__analytics.twitter.com_i_adsct-3Fp-5Fid-3DTwitter-26p-5Fuser-5Fid-3D0-26txn-5Fid-3Do2gi1-26events-3D-255B-255B-2522pageview-2522-252Cnull-255D-255D-26tw-5Fsale-5Famount-3D0-26tw-5Forder-5Fquantity-3D0&d=DwMGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=GMr8XYCDyeQQZuD3noL91A&m=dAJyokk16UvYy-vMrGn_JwYiGfp_eEgo25B9iGDCG-A&s=o6i4D0V0088WH2RnzIoqiF-vj45PL-2sTrsxQ0SNO3A&e=)" />
<img height="1" width="1" style="display:none;" alt="" src="//t.co/i/adsct?p_id=Twitter&p_user_id=0&txn_id=o2gi1&events=%5B%5B%22pageview%22%2Cnull%5D%5D&tw_sale_amount=0&tw_order_quantity=0 (https://urldefense.proofpoint.com/v2/url?u=https-3A__linkprotect.cudasvc.com_url-3Fa-3Dhttp-253a-252f-252ft.co-252fi-252fadsct-253fp-5Fid-253dTwitter-2526p-5Fuser-5Fid-253d0-2526txn-5Fid-253do2gi1-2526events-253d-25255B-25255B-252522pageview-252522-25252Cnull-25255D-25255D-2526tw-5Fsale-5Famount-253d0-2526tw-5Forder-5Fquantity-253d0-26c-3DE-2C1-2CC33dLwIhtuEcl5FhdztSnUwsioeej5k-2DWy0RYREBAq51kGji32A2Cw94YU9vQBpY5tPN3AukEw3C-5F-2DlbtndnLoR7-5FA-5FLoH0Rr7zLtP1ykptN-26typo-3D1&d=DwMGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=GMr8XYCDyeQQZuD3noL91A&m=dAJyokk16UvYy-vMrGn_JwYiGfp_eEgo25B9iGDCG-A&s=Abgc3XBkhESv8XBYtLchdDZyISGsK6v_BB6cLMJGyCw&e=)" />
<!-- End Twitter universal website tag code -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css" />
<link href="/feed.xml" type="application/atom+xml" rel="alternate" title="Pythorch Blog Posts" />
</head>
<body class="get-started">
<!-- 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="hello-bar">
<div class="container">
Join us at PyTorch Conference in San Francisco, October 22-23. CFP open now! <a target="_blank" href="https://events.linuxfoundation.org/pytorch-conference/">Learn more</a>.
</div>
</div>
<div class="container-fluid header-holder get-started-header">
<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="dropdownMenuButton" 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="/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>
<a class="nav-dropdown-item" href="/new">
<span class="dropdown-title">New to PyTorch Foundation</span>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div id="dropdownMenuButton" 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://landscape.pytorch.org/" target="_blank">
<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="/join-ecosystem">
<span class="dropdown-title">Join the Ecosystem</span>
</a>
<a class="nav-dropdown-item" href="/#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="/resources">
<span class=dropdown-title>Developer Resources</span>
<p>Find resources and get questions answered</p>
</a>
<a class="nav-dropdown-item" href="/ecosystem/contributor-awards-2024">
<span class="dropdown-title">Contributor Awards - 2024</span>
<p>Award winners announced at this year's PyTorch Conference</p>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div id="dropdownMenuButton" 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="/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="/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>
<a class="nav-dropdown-item" target="_blank" href="https://pytorch.org/executorch/stable/index.html">
<span class="dropdown-title">ExecuTorch Documentation</span>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div id="docsDropdownButton" 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">
<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="/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 class="main-menu-item">
<div id="dropdownMenuButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Blog & News
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="/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="/community-blog">
<span class="dropdown-title">Community Blog</span>
<p>Stories from the PyTorch ecosystem</p>
</a>
<a class="nav-dropdown-item" href="/videos">
<span class="dropdown-title">Videos</span>
<p>Learn about the latest PyTorch tutorials, new, and more </p>
</a>
<a class="nav-dropdown-item" href="/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="/events">
<span class=dropdown-title>Events</span>
<p>Find events, webinars, and podcasts</p>
</a>
<a class="nav-dropdown-item" href="/newsletter">
<span class=dropdown-title>Newsletter</span>
<p>Stay up-to-date with the latest updates</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">
About
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="/foundation">
<span class=dropdown-title>PyTorch Foundation</span>
<p>Learn more about the PyTorch Foundation.</p>
</a>
<a class="nav-dropdown-item" href="/governing-board">
<span class=dropdown-title>Governing Board</span>
</a>
<a class="nav-dropdown-item" href="/credits">
<span class=dropdown-title>Cloud Credit Program</span>
</a>
<a class="nav-dropdown-item" href="/tac">
<span class=dropdown-title>Technical Advisory Council</span>
</a>
<a class="nav-dropdown-item" href="/staff">
<span class=dropdown-title>Staff</span>
</a>
<a class="nav-dropdown-item" href="/contact-us">
<span class=dropdown-title>Contact Us</span>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<a href="/join" data-cta="join">
Become a Member
</a>
</li>
<li class="main-menu-item" id="github-main-menu-link">
<a href="https://github.com/pytorch/pytorch" title="Go to PyTorch GitHub">
<div id="topnav-gh-icon"></div>
</a>
</li>
<li class="navSearchWrapper reactNavSearchWrapper" key="search">
<div class="search-border">
<div id="search-icon"></div>
<input
id="search-input"
type="text"
title="Search"
/>
<div id="close-search">X</div>
</div>
</li>
</ul>
</div>
<script src="/assets/main-menu-dropdown.js"></script>
<a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
</div>
</div>
</div>
<div class="main-background get-started-background"></div>
<div class="jumbotron jumbotron-fluid on-dark-background">
<div class="container">
<h1>Get Started</h1>
<p class="lead">
Select preferences and run the command to install PyTorch locally, or
get started quickly with one of the supported cloud platforms.
</p>
</div>
</div>
<div class="main-content-wrapper">
<div class="main-content">
<div class="container-fluid nav-menu-wrapper">
<div class="container">
<nav class="navbar navbar-expand-lg navbar-light main-content-menu">
<ul class="navbar-nav">
<li class="nav-item nav-select">
<a
class="nav-link get-started-nav-link"
data-id="get-started-locally"
href="/get-started/locally/"
>Start Locally</a
>
</li>
<li class="nav-item nav-select">
<a
class="nav-link get-started-nav-link"
data-id="pytorch"
href="/get-started/pytorch-2.0/"
>PyTorch 2.x</a
>
</li>
<li class="nav-item nav-select">
<a
class="nav-link get-started-nav-link"
data-id="get-started-via-cloud-partners"
href="/get-started/cloud-partners/"
>Start via Cloud Partners</a
>
</li>
<li class="nav-item nav-select">
<a
class="nav-link get-started-nav-link"
data-id="previous-versions"
href="/get-started/previous-versions/"
>Previous PyTorch Versions</a
>
</li>
<li class="nav-item nav-select">
<a
class="nav-link get-started-nav-link"
data-id="mobile"
href="/get-started/executorch/"
>PyTorch for Edge</a
>
</li>
</ul>
</nav>
</div>
</div>
<div class="container">
<div class="row pytorch-2">
<div class="col-md-3"><div class="sticky-top get-started-locally-sidebar">
<p id="get-started-shortcuts-menu">Shortcuts</p>
<ul id="get-started-locally-sidebar-list">
<li>
<a href="#overview">Overview</a>
</li>
<li>
<a href="#pytorch-2x-faster-more-pythonic-and-as-dynamic-as-ever">
PyTorch 2.x: faster, more pythonic and as dynamic as ever
</a>
</li>
<li>
<a href="#testimonials">Testimonials</a>
</li>
<li>
<a href="#motivation">Motivation</a>
</li>
<li>
<a href="#technology-overview">Technology Overview</a>
</li>
<li>
<a href="#user-experience">User Experience</a>
</li>
<li>
<a href="#distributed">Distributed</a>
</li>
<li>
<a href="#developervendor-experience">Developer/Vendor Experience</a>
</li>
<li>
<a href="#final-thoughts">Final Thoughts</a>
</li>
<li>
<a href="#accelerating-hugging-face-and-timm-models-with-pytorch-20">
Accelerating Hugging Face And Timm Models With Pytorch 2.0
</a>
</li>
<li>
<a href="#requirements">Requirements</a>
</li>
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#faqs">FAQs</a>
</li>
<li>
<a href="#ask-the-engineers-20-live-qa-series">Ask The Engineers 2.0 Live Q&A Series</a>
</li>
<li>
<a href="#watch-the-talks-from-pytorch-conference">Watch The Talks From Pytorch Conference</a>
</li>
</ul>
</div>
</div>
<div class="col-md-8 offset-md-1" style="font-family: Verdana;">
<div class="article-wrapper" data-id="">
<article class="pytorch-article"><h2 id="overview">Overview</h2>
<p>Introducing PyTorch 2.0, our first steps toward the next generation 2-series release of PyTorch. Over the last few years we have innovated and iterated from PyTorch 1.0 to the most recent 1.13 and moved to the newly formed PyTorch Foundation, part of the Linux Foundation.</p>
<p>PyTorch’s biggest strength beyond our amazing community is that we continue as a first-class Python integration, imperative style, simplicity of the API and options. PyTorch 2.0 offers the same eager-mode development and user experience, while fundamentally changing and supercharging how PyTorch operates at compiler level under the hood. We are able to provide faster performance and support for Dynamic Shapes and Distributed.</p>
<p>Below you will find all the information you need to better understand what PyTorch 2.0 is, where it’s going and more importantly how to get started today (e.g., tutorial, requirements, models, common FAQs). There is still a lot to learn and develop but we are looking forward to community feedback and contributions to make the 2-series better and thank you all who have made the 1-series so successful.</p>
<h2 id="pytorch-2x-faster-more-pythonic-and-as-dynamic-as-ever">PyTorch 2.x: faster, more pythonic and as dynamic as ever</h2>
<p>Today, we announce <code class="language-plaintext highlighter-rouge">torch.compile</code>, a feature that pushes PyTorch performance to new heights and starts the move for parts of PyTorch from C++ back into Python. We believe that this is a substantial new direction for PyTorch – hence we call it 2.0. <code class="language-plaintext highlighter-rouge">torch.compile</code> is a fully additive (and optional) feature and hence 2.0 is 100% backward compatible by definition.</p>
<p>Underpinning <code class="language-plaintext highlighter-rouge">torch.compile</code> are new technologies – TorchDynamo, AOTAutograd, PrimTorch and TorchInductor.</p>
<ul>
<li>
<p><strong>TorchDynamo</strong> captures PyTorch programs safely using Python Frame Evaluation Hooks and is a significant innovation that was a result of 5 years of our R&D into safe graph capture</p>
</li>
<li>
<p><strong>AOTAutograd</strong> overloads PyTorch’s autograd engine as a tracing autodiff for generating ahead-of-time backward traces.</p>
</li>
<li><strong>PrimTorch</strong> canonicalizes ~2000+ PyTorch operators down to a closed set of ~250 primitive operators that developers can target to build a complete PyTorch backend. This substantially lowers the barrier of writing a PyTorch feature or backend.</li>
<li><strong>TorchInductor</strong> is a deep learning compiler that generates fast code for multiple accelerators and backends. For NVIDIA and AMD GPUs, it uses OpenAI Triton as a key building block.</li>
</ul>
<p>TorchDynamo, AOTAutograd, PrimTorch and TorchInductor are written in Python and support dynamic shapes (i.e. the ability to send in Tensors of different sizes without inducing a recompilation), making them flexible, easily hackable and lowering the barrier of entry for developers and vendors.</p>
<p>To validate these technologies, we used a diverse set of 163 open-source models across various machine learning domains. We built this benchmark carefully to include tasks such as Image Classification, Object Detection, Image Generation, various NLP tasks such as Language Modeling, Q&A, Sequence Classification, Recommender Systems and Reinforcement Learning. We separate the benchmarks into three categories:</p>
<ul style="margin: 1.5rem 0 1.5rem 0;">
<li>46 models from <a href="https://github.com/huggingface/transformers" target="_blank">HuggingFace Transformers</a></li>
<li>61 models from <a href="https://github.com/rwightman/pytorch-image-models" target="_blank">TIMM</a>: a collection of state-of-the-art PyTorch image models by Ross Wightman</li>
<li>56 models from <a href="https://github.com/pytorch/benchmark/" target="_blank">TorchBench</a>: a curated set of popular code-bases from across github</li>
</ul>
<!--
- 46 models from [HuggingFace Transformers](https://github.com/huggingface/transformers)
- 61 models from [TIMM](https://github.com/rwightman/pytorch-image-models): a collection of state-of-the-art PyTorch image models by Ross Wightman
- 56 models from [TorchBench](https://github.com/pytorch/benchmark/): a curated set of popular code-bases from across github -->
<p>We don’t modify these open-source models except to add a <code class="language-plaintext highlighter-rouge">torch.compile</code> call wrapping them.</p>
<p>We then measure speedups and validate accuracy across these models. Since speedups can be dependent on data-type, we measure speedups on both float32 and Automatic Mixed Precision (AMP). We report an uneven weighted average speedup of <em>0.75 * AMP + 0.25 * float32</em> since we find AMP is more common in practice.</p>
<p>Across these 163 open-source models <code class="language-plaintext highlighter-rouge">torch.compile</code> works 93% of time, and the model runs 43% faster in training on an NVIDIA A100 GPU. At Float32 precision, it runs 21% faster on average and at AMP Precision it runs 51% faster on average.</p>
<p><strong>Caveats:</strong> On a desktop-class GPU such as a NVIDIA 3090, we’ve measured that speedups are lower than on server-class GPUs such as A100. As of today, our default backend TorchInductor supports CPUs and NVIDIA Volta and Ampere GPUs. It does not (yet) support other GPUs, xPUs or older NVIDIA GPUs.</p>
<p>
<img src="/assets/images/Caveats.jpg" width="90%" />
<center> <u> Speedups for torch.compile against eager mode on an NVIDIA A100 GPU </u> </center>
</p>
<p><strong>Try it:</strong> <code class="language-plaintext highlighter-rouge">torch.compile</code> is in the early stages of development. Starting today, you can try out <code class="language-plaintext highlighter-rouge">torch.compile</code> in the <code class="language-plaintext highlighter-rouge">nightly</code> binaries. We expect to ship the first stable 2.0 release in early March 2023.</p>
<p>In the roadmap of PyTorch 2.x we hope to push the compiled mode further and further in terms of performance and scalability. Some of this work is in-flight, as we talked about at the Conference today. Some of this work has not started yet. Some of this work is what we hope to see, but don’t have the bandwidth to do ourselves. If you are interested in contributing, come chat with us at the <strong>Ask the Engineers: 2.0 Live Q&A Series</strong> starting this month (details at the end of this post) and/or via Github / Forums.</p>
<p>
<img src="/assets/images/pytorch-2.0-img2.png" width="90%" />
</p>
<h3 id="testimonials">Testimonials</h3>
<p>Here is what some of PyTorch’s users have to say about our new direction:</p>
<p><strong>Sylvain Gugger</strong> the <strong>primary maintainer of HuggingFace transformers</strong>:</p>
<p><em>“With just one line of code to add, PyTorch 2.0 gives a speedup between 1.5x and 2.x in training Transformers models. This is the most exciting thing since mixed precision training was introduced!”</em></p>
<p><strong>Ross Wightman the primary maintainer of TIMM</strong> (one of the largest vision model hubs within the PyTorch ecosystem):</p>
<p><em>“It just works out of the box with majority of TIMM models for inference and train workloads with no code changes”</em></p>
<p><strong>Luca Antiga</strong> the <strong>CTO of Lightning AI</strong> and one of the <strong>primary maintainers of PyTorch Lightning</strong></p>
<p><em>“PyTorch 2.0 embodies the future of deep learning frameworks. The possibility to capture a PyTorch program with effectively no user intervention and get massive on-device speedups and program manipulation out of the box unlocks a whole new dimension for AI developers.”</em></p>
<h2 id="motivation">Motivation</h2>
<p>Our philosophy on PyTorch has always been to keep flexibility and hackability our top priority, and performance as a close second. We strived for:</p>
<ol>
<li>High-Performance eager execution</li>
<li>Pythonic internals</li>
<li>Good abstractions for Distributed, Autodiff, Data loading, Accelerators, etc.</li>
</ol>
<p>Since we launched PyTorch in 2017, hardware accelerators (such as GPUs) have become ~15x faster in compute and about ~2x faster in the speed of memory access. So, to keep eager execution at high-performance, we’ve had to move substantial parts of PyTorch internals into C++. Moving internals into C++ makes them less hackable and increases the barrier of entry for code contributions.</p>
<p>From day one, we knew the performance limits of eager execution. In July 2017, we started our first research project into developing a Compiler for PyTorch. The compiler needed to make a PyTorch program fast, but not at the cost of the PyTorch experience. Our key criteria was to preserve certain kinds of flexibility – support for dynamic shapes and dynamic programs which researchers use in various stages of exploration.</p>
<p>
<img src="/assets/images/pytorch-2.0-img3.gif" width="90%" />
</p>
<h2 id="technology-overview">Technology Overview</h2>
<p>Over the years, we’ve built several compiler projects within PyTorch. Let us break down the compiler into three parts:</p>
<ul>
<li>graph acquisition</li>
<li>graph lowering</li>
<li>graph compilation</li>
</ul>
<p>Graph acquisition was the harder challenge when building a PyTorch compiler.</p>
<p>In the past 5 years, we built <code class="language-plaintext highlighter-rouge">torch.jit.trace</code>, TorchScript, FX tracing, Lazy Tensors. But none of them felt like they gave us everything we wanted. Some were flexible but not fast, some were fast but not flexible and some were neither fast nor flexible. Some had bad user-experience (like being silently wrong). While TorchScript was promising, it needed substantial changes to your code and the code that your code depended on. This need for substantial change in code made it a non-starter for a lot of PyTorch users.</p>
<p>
<img src="/assets/images/pytorch-2.0-img4.jpg" width="90%" />
<center><u>The PyTorch compilation process</u></center>
</p>
<h3 id="torchdynamo-acquiring-graphs-reliably-and-fast">TorchDynamo: Acquiring Graphs reliably and fast</h3>
<p>Earlier this year, we started working on TorchDynamo, an approach that uses a CPython feature introduced in <a href="https://peps.python.org/pep-0523/">PEP-0523</a> called the Frame Evaluation API. We took a data-driven approach to validate its effectiveness on Graph Capture. We used 7,000+ Github projects written in PyTorch as our validation set. While TorchScript and others struggled to even acquire the graph 50% of the time, often with a big overhead, TorchDynamo acquired the graph <a href="https://dev-discuss.pytorch.org/t/torchdynamo-update-8-torchdynamo-passed-correctness-check-on-7k-github-models/663">99% of the time</a>, correctly, safely and with negligible overhead – without needing any changes to the original code. This is when we knew that we finally broke through the barrier that we were struggling with for many years in terms of flexibility and speed.</p>
<h3 id="torchinductor-fast-codegen-using-a-define-by-run-ir">TorchInductor: fast codegen using a define-by-run IR</h3>
<p>For a new compiler backend for PyTorch 2.0, we took inspiration from how our users were writing high performance custom kernels: increasingly using the <a href="https://github.com/openai/triton">Triton</a> language. We also wanted a compiler backend that used similar abstractions to PyTorch eager, and was general purpose enough to support the wide breadth of features in PyTorch. TorchInductor uses a pythonic define-by-run loop level IR to automatically map PyTorch models into generated Triton code on GPUs and C++/OpenMP on CPUs. TorchInductor’s core loop level IR contains only ~50 operators, and it is implemented in Python, making it easily hackable and extensible.</p>
<h3 id="aotautograd-reusing-autograd-for-ahead-of-time-graphs">AOTAutograd: reusing Autograd for ahead-of-time graphs</h3>
<p>For PyTorch 2.0, we knew that we wanted to accelerate training. Thus, it was critical that we not only captured user-level code, but also that we captured backpropagation. Moreover, we knew that we wanted to reuse the existing battle-tested PyTorch autograd system. AOTAutograd leverages PyTorch’s <strong>torch_dispatch</strong> extensibility mechanism to trace through our Autograd engine, allowing us to capture the backwards pass “ahead-of-time”. This allows us to accelerate both our forwards <em>and</em> backwards pass using TorchInductor.</p>
<h3 id="primtorch-stable-primitive-operators">PrimTorch: Stable Primitive operators</h3>
<p>Writing a backend for PyTorch is challenging. PyTorch has 1200+ operators, and 2000+ if you consider various overloads for each operator.</p>
<p>
<img src="/assets/images/pytorch-2.0-img5.png" width="90%" />
<center> <i> <u> A breakdown of the 2000+ PyTorch operators </u></i> </center>
</p>
<p>Hence, writing a backend or a cross-cutting feature becomes a draining endeavor. Within the PrimTorch project, we are working on defining smaller and stable operator sets. PyTorch programs can consistently be lowered to these operator sets. We aim to define two operator sets:</p>
<ul>
<li>Prim ops with about ~250 operators, which are fairly low-level. These are suited for compilers because they are low-level enough that you need to fuse them back together to get good performance.</li>
<li>ATen ops with about ~750 canonical operators and suited for exporting as-is. These are suited for backends that already integrate at the ATen level or backends that won’t have compilation to recover performance from a lower-level operator set like Prim ops.</li>
</ul>
<p>We discuss more about this topic below in the Developer/Vendor Experience section</p>
<h2 id="user-experience">User Experience</h2>
<p>We introduce a simple function <code class="language-plaintext highlighter-rouge">torch.compile</code> that wraps your model and returns a compiled model.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">compiled_model</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="nb">compile</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
</code></pre></div></div>
<p>This <code class="language-plaintext highlighter-rouge">compiled_model</code> holds a reference to your model and compiles the <code class="language-plaintext highlighter-rouge">forward</code> function to a more optimized version. When compiling the model, we give a few knobs to adjust it:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">torch</span><span class="p">.</span><span class="nb">compile</span><span class="p">(</span><span class="n">model</span><span class="p">:</span> <span class="n">Callable</span><span class="p">,</span>
<span class="o">*</span><span class="p">,</span>
<span class="n">mode</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="s">"default"</span><span class="p">,</span>
<span class="n">dynamic</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="bp">False</span><span class="p">,</span>
<span class="n">fullgraph</span><span class="p">:</span><span class="nb">bool</span> <span class="o">=</span> <span class="bp">False</span><span class="p">,</span>
<span class="n">backend</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Callable</span><span class="p">]</span> <span class="o">=</span> <span class="s">"inductor"</span><span class="p">,</span>
<span class="c1"># advanced backend options go here as kwargs
</span> <span class="o">**</span><span class="n">kwargs</span>
<span class="p">)</span> <span class="o">-></span> <span class="n">torch</span><span class="p">.</span><span class="n">_dynamo</span><span class="p">.</span><span class="n">NNOptimizedModule</span>
</code></pre></div></div>
<ul>
<li>
<p><strong>mode</strong> specifies what the compiler should be optimizing while compiling.</p>
<ul>
<li>The default mode is a preset that tries to compile efficiently without taking too long to compile or using extra memory.</li>
<li>Other modes such as <code class="language-plaintext highlighter-rouge">reduce-overhead</code> reduce the framework overhead by a lot more, but cost a small amount of extra memory. <code class="language-plaintext highlighter-rouge">max-autotune</code> compiles for a long time, trying to give you the fastest code it can generate.</li>
</ul>
</li>
<li><strong>dynamic</strong> specifies whether to enable the code path for Dynamic Shapes. Certain compiler optimizations cannot be applied to dynamic shaped programs. Making it explicit whether you want a compiled program with dynamic shapes or with static shapes will help the compiler give you better optimized code.</li>
<li><strong>fullgraph</strong> is similar to Numba’s <code class="language-plaintext highlighter-rouge">nopython</code>. It compiles the entire program into a single graph or gives an error explaining why it could not do so. Most users don’t need to use this mode. If you are very performance conscious, then you try to use it.</li>
<li><strong>backend</strong> specifies which compiler backend to use. By default, TorchInductor is used, but there are a few others available.</li>
</ul>
<p>
<img src="/assets/images/pytorch-2.0-img6.png" width="90%" />
</p>
<p>The compile experience intends to deliver most benefits and the most flexibility in the default mode. Here is a mental model of what you get in each mode.</p>
<p>Now, let us look at a full example of compiling a real model and running it (with random data)</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torchvision.models</span> <span class="k">as</span> <span class="n">models</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">models</span><span class="p">.</span><span class="n">resnet18</span><span class="p">().</span><span class="n">cuda</span><span class="p">()</span>
<span class="n">optimizer</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="n">optim</span><span class="p">.</span><span class="n">SGD</span><span class="p">(</span><span class="n">model</span><span class="p">.</span><span class="n">parameters</span><span class="p">(),</span> <span class="n">lr</span><span class="o">=</span><span class="mf">0.01</span><span class="p">)</span>
<span class="n">compiled_model</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="nb">compile</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">224</span><span class="p">,</span> <span class="mi">224</span><span class="p">).</span><span class="n">cuda</span><span class="p">()</span>
<span class="n">optimizer</span><span class="p">.</span><span class="n">zero_grad</span><span class="p">()</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">compiled_model</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">out</span><span class="p">.</span><span class="nb">sum</span><span class="p">().</span><span class="n">backward</span><span class="p">()</span>
<span class="n">optimizer</span><span class="p">.</span><span class="n">step</span><span class="p">()</span>
</code></pre></div></div>
<p>The first time you run the <code class="language-plaintext highlighter-rouge">compiled_model(x)</code>, it compiles the model. Hence, it takes longer to run. Subsequent runs are fast.</p>
<h3 id="modes">Modes</h3>
<p>The compiler has a few presets that tune the compiled model in different ways.
You might be running a small model that is slow because of framework overhead. Or, you might be running a large model that barely fits into memory. Depending on your need, you might want to use a different mode.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># API NOT FINAL
# default: optimizes for large models, low compile-time
# and no extra memory usage
</span><span class="n">torch</span><span class="p">.</span><span class="nb">compile</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="c1"># reduce-overhead: optimizes to reduce the framework overhead
# and uses some extra memory. Helps speed up small models
</span><span class="n">torch</span><span class="p">.</span><span class="nb">compile</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s">"reduce-overhead"</span><span class="p">)</span>
<span class="c1"># max-autotune: optimizes to produce the fastest model,
# but takes a very long time to compile
</span><span class="n">torch</span><span class="p">.</span><span class="nb">compile</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s">"max-autotune"</span><span class="p">)</span>
</code></pre></div></div>
<h3 id="reading-and-updating-attributes">Reading and updating Attributes</h3>
<p>Accessing model attributes work as they would in eager mode.
You can access or modify attributes of your model (such as <code class="language-plaintext highlighter-rouge">model.conv1.weight</code>) as you generally would. This is completely safe and sound in terms of code correction. TorchDynamo inserts guards into the code to check if its assumptions hold true. If attributes change in certain ways, then TorchDynamo knows to recompile automatically as needed.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># optimized_model works similar to model, feel free to access its attributes and modify them
</span><span class="n">optimized_model</span><span class="p">.</span><span class="n">conv1</span><span class="p">.</span><span class="n">weight</span><span class="p">.</span><span class="n">fill_</span><span class="p">(</span><span class="mf">0.01</span><span class="p">)</span>
<span class="c1"># this change is reflected in model
</span></code></pre></div></div>
<h3 id="hooks">Hooks</h3>
<p>Module and Tensor <a href="https://pytorch.org/docs/stable/notes/modules.html#module-hooks">hooks</a> don’t fully work at the moment, but they will eventually work as we finish development.</p>
<h3 id="serialization">Serialization</h3>
<p>You can serialize the state-dict of the <code class="language-plaintext highlighter-rouge">optimized_model</code> OR the <code class="language-plaintext highlighter-rouge">model</code>. They point to the same parameters and state and hence are equivalent.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">torch</span><span class="p">.</span><span class="n">save</span><span class="p">(</span><span class="n">optimized_model</span><span class="p">.</span><span class="n">state_dict</span><span class="p">(),</span> <span class="s">"foo.pt"</span><span class="p">)</span>
<span class="c1"># both these lines of code do the same thing
</span><span class="n">torch</span><span class="p">.</span><span class="n">save</span><span class="p">(</span><span class="n">model</span><span class="p">.</span><span class="n">state_dict</span><span class="p">(),</span> <span class="s">"foo.pt"</span><span class="p">)</span>
</code></pre></div></div>
<p>You cannot serialize <code class="language-plaintext highlighter-rouge">optimized_model</code> currently. If you wish to save the object directly, save <code class="language-plaintext highlighter-rouge">model</code> instead.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">torch</span><span class="p">.</span><span class="n">save</span><span class="p">(</span><span class="n">optimized_model</span><span class="p">,</span> <span class="s">"foo.pt"</span><span class="p">)</span> <span class="c1"># Error
</span><span class="n">torch</span><span class="p">.</span><span class="n">save</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="s">"foo.pt"</span><span class="p">)</span> <span class="c1"># Works
</span></code></pre></div></div>
<h3 id="inference-and-export">Inference and Export</h3>
<p>For model inference, after generating a compiled model using torch.compile, run some warm-up steps before actual model serving. This helps mitigate latency spikes during initial serving.</p>
<p>In addition, we will be introducing a mode called <code class="language-plaintext highlighter-rouge">torch.export</code> that carefully exports the entire model and the guard infrastructure for environments that need guaranteed and predictable latency. <code class="language-plaintext highlighter-rouge">torch.export</code> would need changes to your program, especially if you have data dependent control-flow.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># API Not Final
</span><span class="n">exported_model</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="n">_dynamo</span><span class="p">.</span><span class="n">export</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="nb">input</span><span class="p">)</span>
<span class="n">torch</span><span class="p">.</span><span class="n">save</span><span class="p">(</span><span class="n">exported_model</span><span class="p">,</span> <span class="s">"foo.pt"</span><span class="p">)</span>
</code></pre></div></div>
<p>This is in early stages of development. Catch the talk on Export Path at the PyTorch Conference for more details. You can also engage on this topic at our “Ask the Engineers: 2.0 Live Q&A Series” starting this month (more details at the end of this post).</p>
<h3 id="debugging-issues">Debugging Issues</h3>
<p>A compiled mode is opaque and hard to debug. You will have questions such as:</p>
<ul>
<li>Why is my program crashing in compiled mode?</li>
<li>Is compiled mode as accurate as eager mode?</li>
<li>Why am I not seeing speedups?</li>
</ul>
<p>If compiled mode produces an error or a crash or diverging results from eager mode (beyond machine precision limits), it is very unlikely that it is your code’s fault. However, understanding what piece of code is the reason for the bug is useful.</p>
<p>To aid in debugging and reproducibility, we have created several tools and logging capabilities out of which one stands out: <strong>The Minifier.</strong></p>
<p>The minifier automatically reduces the issue you are seeing to a small snippet of code. This small snippet of code reproduces the original issue and you can file a github issue with the minified code. This will help the PyTorch team fix the issue easily and quickly.</p>
<p>If you are not seeing the speedups that you expect, then we have the <strong>torch._dynamo.explain</strong> tool that explains which parts of your code induced what we call “graph breaks”. Graph breaks generally hinder the compiler from speeding up the code, and reducing the number of graph breaks likely will speed up your code (up to some limit of diminishing returns).</p>
<p>You can read about these and more in our <a href="https://pytorch.org/docs/stable/torch.compiler_troubleshooting.html">troubleshooting guide</a>.</p>
<h3 id="dynamic-shapes">Dynamic Shapes</h3>
<p>When looking at what was necessary to support the generality of PyTorch code, one key requirement was supporting dynamic shapes, and allowing models to take in tensors of different sizes without inducing recompilation every time the shape changes.</p>
<p>As of today, support for Dynamic Shapes is limited and a rapid work in progress. It will be fully featured by stable release. It is gated behind a <code class="language-plaintext highlighter-rouge">dynamic=True</code> argument, and we have more progress on a feature branch (symbolic-shapes), on which we have successfully run BERT_pytorch in training with full symbolic shapes with TorchInductor. For inference with dynamic shapes, we have more coverage. For example, let’s look at a common setting where dynamic shapes are helpful - text generation with language models.</p>
<p>We can see that even when the shape changes dynamically from 4 all the way to 256, Compiled mode is able to consistently outperform eager by up to 40%. Without support for dynamic shapes, a common workaround is to pad to the nearest power of two. However, as we can see from the charts below, it incurs a significant amount of performance overhead, and also results in significantly longer compilation time. Moreover, padding is sometimes non-trivial to do correctly.</p>
<p>By supporting dynamic shapes in PyTorch 2.0’s Compiled mode, we can get the best of performance <em>and</em> ease of use.</p>
<div style="display:flex; flex-direction: row; padding: 10px;">
<img src="/assets/images/pytorch-2.0-img7.png" width="50%" />
<img src="/assets/images/pytorch-2.0-img8.png" width="50%" />
</div>
<p>The current work is evolving very rapidly and we may temporarily let some models regress as we land fundamental improvements to infrastructure. The latest updates for our progress on dynamic shapes can be found <a href="https://dev-discuss.pytorch.org/t/state-of-symbolic-shapes-branch/777/19">here</a>.</p>
<h2 id="distributed">Distributed</h2>
<p>In summary, torch.distributed’s two main distributed wrappers work well in compiled mode.</p>
<p>Both <code class="language-plaintext highlighter-rouge">DistributedDataParallel</code> (DDP) and <code class="language-plaintext highlighter-rouge">FullyShardedDataParallel</code> (FSDP) work in compiled mode and provide improved performance and memory utilization relative to eager mode, with some caveats and limitations.</p>
<p>
<center> <u>Speedups in AMP Precision</u></center>
<img src="/assets/images/pytorch-2.0-img9.png" width="90%" />
<center><u>Left: speedups for FSDP in Compiled mode over eager mode (AMP precision).<br />
Right: FSDP in Compiled mode takes substantially lesser memory than in eager mode</u></center>
</p>
<div style="display:flex; flex-direction: row; padding:10px;">
<img src="/assets/images/pytorch-2.0-img10.png" width="50%" />
<img src="/assets/images/pytorch-2.0-img11.png" width="50%" />
</div>
<h3 id="distributeddataparallel-ddp">DistributedDataParallel (DDP)</h3>
<p>DDP relies on overlapping AllReduce communications with backwards computation, and grouping smaller per-layer AllReduce operations into ‘buckets’ for greater efficiency. AOTAutograd functions compiled by TorchDynamo prevent communication overlap, when combined naively with DDP, but performance is recovered by compiling separate subgraphs for each ‘bucket’ and allowing communication ops to happen outside and in-between the subgraphs. DDP support in compiled mode also currently requires <code class="language-plaintext highlighter-rouge">static_graph=False</code>. See <a href="https://dev-discuss.pytorch.org/t/torchdynamo-update-9-making-ddp-work-with-torchdynamo/860">this post</a> for more details on the approach and results for DDP + TorchDynamo.</p>
<h3 id="fullyshardeddataparallel-fsdp">FullyShardedDataParallel (FSDP)</h3>
<p>FSDP itself is a “beta” PyTorch feature and has a higher level of system complexity than DDP due to the ability to tune which submodules are wrapped and because there are generally more configuration options. FSDP works with TorchDynamo and TorchInductor for a variety of popular models, if configured with the <code class="language-plaintext highlighter-rouge">use_original_params=True</code> flag. Some compatibility issues with particular models or configurations are expected at this time, but will be actively improved, and particular models can be prioritized if github issues are filed.</p>
<p>Users specify an <code class="language-plaintext highlighter-rouge">auto_wrap_policy</code> argument to indicate which submodules of their model to wrap together in an FSDP instance used for state sharding, or manually wrap submodules in FSDP instances. For example, many transformer models work well when each ‘transformer block’ is wrapped in a separate FSDP instance and thus only the full state of one transformer block needs to be materialized at one time. Dynamo will insert graph breaks at the boundary of each FSDP instance, to allow communication ops in forward (and backward) to happen outside the graphs and in parallel to computation.</p>
<p>If FSDP is used without wrapping submodules in separate instances, it falls back to operating similarly to DDP, but without bucketing. Hence all gradients are reduced in one operation, and there can be no compute/communication overlap even in Eager. This configuration has only been tested with TorchDynamo for functionality but not for performance.</p>
<h2 id="developervendor-experience">Developer/Vendor Experience</h2>
<p>With PyTorch 2.0, we want to simplify the backend (compiler) integration experience. To do this, we have focused on <strong>reducing the number of operators</strong> and <strong>simplifying the semantics</strong> of the operator set necessary to bring up a PyTorch backend.</p>
<p>In graphical form, the PT2 stack looks like:</p>
<p>
<img src="/assets/images/pytorch-2.0-img12.png" width="90%" />
</p>
<p>Starting in the middle of the diagram, AOTAutograd dynamically captures autograd logic in an ahead-of-time fashion, producing a graph of forward and backwards operators in FX graph format.</p>
<p>We provide a set of hardened decompositions (i.e. operator implementations written in terms of other operators) that can be leveraged to <strong>reduce</strong> the number of operators a backend is required to implement. We also <strong>simplify</strong> the semantics of PyTorch operators by selectively rewriting complicated PyTorch logic including mutations and views via a process called <em>functionalization</em>, as well as guaranteeing operator metadata information such as shape propagation formulas. This work is actively in progress; our goal is to provide a <em>primitive</em> and <em>stable</em> set of ~250 operators with simplified semantics, called <em>PrimTorch,</em> that vendors can leverage (i.e. opt-in to) in order to simplify their integrations.<br />
After reducing and simplifying the operator set, backends may choose to integrate at the Dynamo (i.e. the middle layer, immediately after AOTAutograd) or Inductor (the lower layer). We describe some considerations in making this choice below, as well as future work around mixtures of backends.</p>
<p><strong>Dynamo Backend</strong></p>
<p>Vendors with existing compiler stacks may find it easiest to integrate as a TorchDynamo backend, receiving an FX Graph in terms of ATen/Prims IR. Note that for both training and inference, the integration point would be immediately after AOTAutograd, since we currently apply decompositions as part of AOTAutograd, and merely skip the backward-specific steps if targeting inference.</p>
<p><strong>Inductor backend</strong></p>
<p>Vendors can also integrate their backend directly into Inductor. Inductor takes in a graph produced by AOTAutograd that consists of ATen/Prim operations, and further lowers them down to a loop level IR. Today, Inductor provides lowerings to its loop-level IR for pointwise, reduction, scatter/gather and window operations. In addition, Inductor creates fusion groups, does indexing simplification, dimension collapsing, and tunes loop iteration order in order to support efficient code generation. Vendors can then integrate by providing the mapping from the loop level IR to hardware-specific code. Currently, Inductor has two backends: (1) C++ that generates multithreaded CPU code, (2) Triton that generates performant GPU code. These Inductor backends can be used as an inspiration for the alternate backends.</p>
<p><strong>Mixture of Backends Interface (coming soon)</strong></p>
<p>We have built utilities for partitioning an FX graph into subgraphs that contain operators supported by a backend and executing the remainder eagerly. These utilities can be extended to support a “mixture of backends,” configuring which portions of the graphs to run for which backend. However, there is not yet a stable interface or contract for backends to expose their operator support, preferences for patterns of operators, etc. This remains as ongoing work, and we welcome feedback from early adopters.</p>
<h2 id="final-thoughts">Final Thoughts</h2>
<p>We are super excited about the direction that we’ve taken for PyTorch 2.0 and beyond. The road to the final 2.0 release is going to be rough, but come join us on this journey early-on. If you are interested in deep-diving further or contributing to the compiler, please continue reading below which includes more information on how to get started (e.g., tutorials, benchmarks, models, FAQs) and <strong>Ask the Engineers: 2.0 Live Q&A Series</strong> starting this month. Additional resources include:</p>
<ul>
<li><a href="https://pytorch.org/docs/stable/torch.compiler_get_started.html">Getting Started</a></li>
<li><a href="https://pytorch.org/tutorials/">Tutorials</a></li>
<li><a href="https://pytorch.org/docs/stable">Documentation</a></li>
<li><a href="https://dev-discuss.pytorch.org">Developer Discussions</a></li>
</ul>
<script page-id="pytorch" src="/assets/menu-tab-selection.js"></script>
<script src="/assets/quick-start-module.js"></script>
<script src="/assets/show-screencast.js"></script>
<h2 id="accelerating-hugging-face-and-timm-models-with-pytorch-20">Accelerating Hugging Face and TIMM models with PyTorch 2.0</h2>
<p>Author: Mark Saroufim</p>
<p><code class="language-plaintext highlighter-rouge">torch.compile()</code> makes it easy to experiment with different compiler backends to make PyTorch code faster with a single line decorator <code class="language-plaintext highlighter-rouge">torch.compile()</code>. It works either directly over an nn.Module as a drop-in replacement for torch.jit.script() but without requiring you to make any source code changes. We expect this one line code change to provide you with between 30%-2x training time speedups on the vast majority of models that you’re already running.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">opt_module</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="nb">compile</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
</code></pre></div></div>
<p>torch.compile supports arbitrary PyTorch code, control flow, mutation and comes with experimental support for dynamic shapes. We’re so excited about this development that we call it PyTorch 2.0.</p>
<p>What makes this announcement different for us is we’ve already benchmarked some of the most popular open source PyTorch models and gotten substantial speedups ranging from 30% to 2x <a href="https://github.com/pytorch/torchdynamo/issues/681">https://github.com/pytorch/torchdynamo/issues/681</a>.</p>
<p>There are no tricks here, we’ve pip installed popular libraries like <a href="https://github.com/huggingface/transformers">https://github.com/huggingface/transformers</a>, <a href="https://github.com/huggingface/accelerate">https://github.com/huggingface/accelerate</a> and <a href="https://github.com/rwightman/pytorch-image-models">https://github.com/rwightman/pytorch-image-models</a> and then ran torch.compile() on them and that’s it.</p>
<p>It’s rare to get both performance and convenience, but this is why the core team finds PyTorch 2.0 so exciting.</p>
<h2 id="requirements">Requirements</h2>
<p>For GPU (newer generation GPUs will see drastically better performance)</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip3 install numpy --pre torch --force-reinstall --index-url https://download.pytorch.org/whl/nightly/cu117
</code></pre></div></div>
<p>For CPU</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip3 install --pre torch --index-url https://download.pytorch.org/whl/nightly/cpu
</code></pre></div></div>
<p>Optional: Verify Installation</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone https://github.com/pytorch/pytorch
cd tools/dynamo
python verify_dynamo.py
</code></pre></div></div>
<p>Optional: Docker installation</p>
<p>We also provide all the required dependencies in the PyTorch nightly
binaries which you can download with</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>docker pull ghcr.io/pytorch/pytorch-nightly
</code></pre></div></div>
<p>And for ad hoc experiments just make sure that your container has access to all your GPUs</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>docker run --gpus all -it ghcr.io/pytorch/pytorch-nightly:latest /bin/bash
</code></pre></div></div>
<h2 id="getting-started">Getting Started</h2>
<p>Please read Mark Saroufim’s <a href="/blog/Accelerating-Hugging-Face-and-TIMM-models/">full blog post</a> where he walks you through a tutorial and real models for you to try PyTorch 2.0 today.</p>
<p>Our goal with PyTorch was to build a breadth-first compiler that would speed up the vast majority of actual models people run in open source. The Hugging Face Hub ended up being an extremely valuable benchmarking tool for us, ensuring that any optimization we work on actually helps accelerate models people want to run.</p>
<p>The blog tutorial will show you exactly how to replicate those speedups so you can be as excited as to PyTorch 2.0 as we are. So please try out PyTorch 2.0, enjoy the free perf and if you’re not seeing it then please open an issue and we will make sure your model is supported <a href="https://github.com/pytorch/torchdynamo/issues">https://github.com/pytorch/torchdynamo/issues</a></p>
<p>After all, we can’t claim we’re created a breadth-first unless <strong>YOUR</strong> models actually run faster.</p>
<h2 id="faqs">FAQs</h2>
<ol>
<li>
<p><strong>What is PT 2.0?</strong><br />
2.0 is the latest PyTorch version. PyTorch 2.0 offers the same eager-mode development experience, while adding a compiled mode via torch.compile. This compiled mode has the potential to speedup your models during training and inference.</p>
</li>
<li>
<p><strong>Why 2.0 instead of 1.14?</strong><br />
PyTorch 2.0 is what 1.14 would have been. We were releasing substantial new features that we believe change how you meaningfully use PyTorch, so we are calling it 2.0 instead.</p>
</li>
<li>
<p><strong>How do I install 2.0? Any additional requirements?</strong></p>
<p>Install the latest nightlies:</p>
<p>CUDA 11.8<br /></p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> pip3 install numpy --pre torch torchvision torchaudio --force-reinstall --index-url https://download.pytorch.org/whl/nightly/cu118
</code></pre></div> </div>
<p>CUDA 11.7</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> pip3 install numpy --pre torch torchvision torchaudio --force-reinstall --index-url https://download.pytorch.org/whl/nightly/cu117
</code></pre></div> </div>
<p>CPU</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> pip3 install numpy --pre torch torchvision torchaudio --force-reinstall --index-url https://download.pytorch.org/whl/nightly/cpu
</code></pre></div> </div>
</li>
<li>
<p><strong>Is 2.0 code backwards-compatible with 1.X?</strong><br />
Yes, using 2.0 will not require you to modify your PyTorch workflows. A single line of code <code class="language-plaintext highlighter-rouge">model = torch.compile(model)</code> can optimize your model to use the 2.0 stack, and smoothly run with the rest of your PyTorch code. This is completely opt-in, and you are not required to use the new compiler.</p>
</li>
<li>
<p><strong>Is 2.0 enabled by default?</strong><br />
2.0 is the name of the release. torch.compile is the feature released in 2.0, and you need to explicitly use torch.compile.</p>
</li>
<li><strong>How do I migrate my PT1.X code to PT2.0?</strong><br />
Your code should be working as-is without the need for any migrations. If you want to use the new Compiled mode feature introduced in 2.0, then you can start by optimizing your model with one line: <code class="language-plaintext highlighter-rouge">model = torch.compile(model)</code>.<br />
While the speedups are primarily observed during training, you can also use it for inference if your model runs faster than eager mode.
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="kn">import</span> <span class="nn">torch</span>
<span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">dataloader</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="nb">compile</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="k">for</span> <span class="n">batch</span> <span class="ow">in</span> <span class="n">dataloader</span><span class="p">:</span>
<span class="n">run_epoch</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">batch</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">infer</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">torch</span><span class="p">.</span><span class="nb">compile</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="k">return</span> <span class="n">model</span><span class="p">(</span>\<span class="o">*</span>\<span class="o">*</span><span class="nb">input</span><span class="p">)</span>
</code></pre></div> </div>
</li>
<li>
<p><strong>Why should I use PT2.0 instead of PT 1.X?</strong><br />
See answer to Question (2).</p>
</li>
<li><strong>What is my code doing differently when running PyTorch 2.0?</strong><br />
Out of the box, PyTorch 2.0 is the same as PyTorch 1.x, your models run in eager-mode i.e. every line of Python is executed one after the other.<br />
In 2.0, if you wrap your model in <code class="language-plaintext highlighter-rouge">model = torch.compile(model)</code>, your model goes through 3 steps before execution:
<ol>
<li>Graph acquisition: first the model is rewritten as blocks of subgraphs. Subgraphs which can be compiled by TorchDynamo are “flattened” and the other subgraphs (which might contain control-flow code or other unsupported Python constructs) will fall back to Eager-Mode.</li>
<li>Graph lowering: all the PyTorch operations are decomposed into their constituent kernels specific to the chosen backend.</li>
<li>Graph compilation, where the kernels call their corresponding low-level device-specific operations.</li>
</ol>
</li>
<li><strong>What new components does PT2.0 add to PT?</strong>
<ul>
<li><strong>TorchDynamo</strong> generates FX Graphs from Python bytecode. It maintains the eager-mode capabilities using <a href="https://pytorch.org/docs/stable/torch.compiler_guards_overview.html#caching-and-guards-overview">guards</a> to ensure the generated graphs are valid (<a href="https://dev-discuss.pytorch.org/t/torchdynamo-an-experiment-in-dynamic-python-bytecode-transformation/361">read more</a>)</li>
<li><strong>AOTAutograd</strong> to generate the backward graph corresponding to the forward graph captured by TorchDynamo (<a href="https://dev-discuss.pytorch.org/t/torchdynamo-update-6-training-support-with-aotautograd/570">read more</a>).</li>
<li><strong>PrimTorch</strong> to decompose complicated PyTorch operations into simpler and more elementary ops (<a href="https://dev-discuss.pytorch.org/t/tracing-with-primitives-update-2/645">read more</a>).</li>
<li><strong>[Backend]</strong> Backends integrate with TorchDynamo to compile the graph into IR that can run on accelerators. For example, <strong>TorchInductor</strong> compiles the graph to either <strong>Triton</strong> for GPU execution or <strong>OpenMP</strong> for CPU execution (<a href="https://dev-discuss.pytorch.org/t/torchinductor-a-pytorch-native-compiler-with-define-by-run-ir-and-symbolic-shapes/747">read more</a>).</li>
</ul>
</li>
<li>
<p><strong>What compiler backends does 2.0 currently support?</strong><br />
The default and the most complete backend is <a href="https://github.com/pytorch/pytorch/tree/master/torch/_inductor">TorchInductor</a>, but TorchDynamo has a growing list of backends that can be found by calling <code class="language-plaintext highlighter-rouge">torchdynamo.list_backends()</code>.</p>
</li>
<li>
<p><strong>How does distributed training work with 2.0?</strong><br />
DDP and FSDP in Compiled mode can run up to 15% faster than Eager-Mode in FP32 and up to 80% faster in AMP precision. PT2.0 does some extra optimization to ensure DDP’s communication-computation overlap works well with Dynamo’s partial graph creation. Ensure you run DDP with static_graph=False. More details <a href="https://dev-discuss.pytorch.org/t/torchdynamo-update-9-making-ddp-work-with-torchdynamo/860">here</a>.</p>
</li>
<li>
<p><strong>How can I learn more about PT2.0 developments?</strong><br />
The <a href="http://dev-discuss.pytorch.org/">PyTorch Developers forum</a> is the best place to learn about 2.0 components directly from the developers who build them.</p>
</li>
<li>
<p><strong>Help my code is running slower with 2.0’s Compiled Mode!</strong><br />
The most likely reason for performance hits is too many graph breaks. For instance, something innocuous as a print statement in your model’s forward triggers a graph break. We have ways to diagnose these - read more <a href="https://pytorch.org/docs/stable/torch.compiler_faq.html#why-am-i-not-seeing-speedups">here</a>.</p>
</li>
<li><strong>My previously-running code is crashing with 2.0’s Compiled Mode! How do I debug it?</strong><br />
Here are some techniques to triage where your code might be failing, and printing helpful logs: <a href="https://pytorch.org/docs/stable/torch.compiler_faq.html#why-is-my-code-crashing">https://pytorch.org/docs/stable/torch.compiler_faq.html#why-is-my-code-crashing</a>.</li>
</ol>
<h2 id="ask-the-engineers-20-live-qa-series">Ask the Engineers: 2.0 Live Q&A Series</h2>
<p>We will be hosting a series of live Q&A sessions for the community to have deeper questions and dialogue with the experts. Please check back to see the full calendar of topics throughout the year. If you are unable to attend: 1) They will be recorded for future viewing and 2) You can attend our Dev Infra Office Hours every Friday at 10 AM PST @ <a href="https://github.com/pytorch/pytorch/wiki/Dev-Infra-Office-Hours">https://github.com/pytorch/pytorch/wiki/Dev-Infra-Office-Hours</a>.</p>
<p>Please click <a href="https://pytorchconference22.splashthat.com/">here</a> to see dates, times, descriptions and links.</p>
<p>Disclaimer: Please do not share your personal information, last name, company when joining the live sessions and submitting questions.</p>
<table style="min-width: 350px" class="QnATable">
<tr>
<td style="width:50%"><b>TOPIC</b></td>
<td style="width:50%"><b>HOST</b></td>
</tr>
<tr>
<td><b>The new developer experience of using 2.0 (install, setup, clone an example, run with 2.0)</b></td>
<td>Suraj Subramanian<br />
<a href="https://www.linkedin.com/in/surajsubramanian/">LinkedIn</a> |
<a href="https://twitter.com/subramen">Twitter</a>
</td>
</tr>
<tr>
<td><a href="https://www.youtube.com/watch?v=1FSBurHpH_Q&list=PL_lsbAsL_o2CQr8oh5sNWt96yWQphNEzM&index=2"><b>PT2 Profiling and Debugging</b></a></td>
<td>Bert Maher<br />
<a href="https://www.linkedin.com/in/bertrand-maher/">LinkedIn</a> |
<a href="https://twitter.com/tensorbert">Twitter</a>
</td>
</tr>
<tr>
<td><a href="https://community.linuxfoundation.org/j/gayr75zshnded/"><b>A deep dive on TorchInductor and PT 2.0 Backend Integration</b></a></td>
<td>Natalia Gimelshein, Bin Bao and Sherlock Huang<br />
Natalia Gimelshein<br />