-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdistributed.tensor.html
1900 lines (1685 loc) · 178 KB
/
distributed.tensor.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta name="robots" content="noindex">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>torch.distributed.tensor — PyTorch 2.5 documentation</title>
<link rel="canonical" href="https://pytorch.org/docs/stable/distributed.tensor.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="Generic Join Context Manager" href="distributed.algorithms.join.html" />
<link rel="prev" title="Distributed communication package - torch.distributed" href="distributed.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.5 ▼</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">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/custom_operators.html">PyTorch Custom Operators Landing Page</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/get_start_xpu.html">Getting Started on Intel GPU</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/gradcheck.html">Gradcheck mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/hip.html">HIP (ROCm) semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/large_scale_deployments.html">Features for large-scale deployments</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/modules.html">Modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/mps.html">MPS backend</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/multiprocessing.html">Multiprocessing best practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/numerical_accuracy.html">Numerical accuracy</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/randomness.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/serialization.html">Serialization semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/windows.html">Windows FAQ</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Language Bindings</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="cpp_index.html">C++</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/javadoc/">Javadoc</a></li>
<li class="toctree-l1"><a class="reference internal" href="deploy.html">torch::deploy</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="torch.html">torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.html">torch.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.functional.html">torch.nn.functional</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensors.html">torch.Tensor</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_attributes.html">Tensor Attributes</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_view.html">Tensor Views</a></li>
<li class="toctree-l1"><a class="reference internal" href="amp.html">torch.amp</a></li>
<li class="toctree-l1"><a class="reference internal" href="autograd.html">torch.autograd</a></li>
<li class="toctree-l1"><a class="reference internal" href="library.html">torch.library</a></li>
<li class="toctree-l1"><a class="reference internal" href="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="mtia.html">torch.mtia</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 current"><a class="current reference internal" href="#">torch.distributed.tensor</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.tensor.parallel.html">torch.distributed.tensor.parallel</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.pipelining.html">torch.distributed.pipelining</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="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="size.html">torch.Size</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="module_tracker.html">torch.utils.module_tracker</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>torch.distributed.tensor</li>
<li class="pytorch-breadcrumbs-aside">
<a href="_sources/distributed.tensor.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="torch-distributed-tensor">
<h1>torch.distributed.tensor<a class="headerlink" href="#torch-distributed-tensor" title="Permalink to this heading">¶</a></h1>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">torch.distributed.tensor</span></code> is currently in alpha state and under
development, we are committing backward compatibility for the most APIs listed
in the doc, but there might be API changes if necessary.</p>
</div>
<div class="section" id="pytorch-dtensor-distributed-tensor">
<h2>PyTorch DTensor (Distributed Tensor)<a class="headerlink" href="#pytorch-dtensor-distributed-tensor" title="Permalink to this heading">¶</a></h2>
<p>PyTorch DTensor offers simple and flexible tensor sharding primitives that transparently handles distributed
logic, including sharded storage, operator computation and collective communications across devices/hosts.
<code class="docutils literal notranslate"><span class="pre">DTensor</span></code> could be used to build different paralleism solutions and support sharded state_dict representation
when working with multi-dimensional sharding.</p>
<p>Please see examples from the PyTorch native parallelism solutions that are built on top of <code class="docutils literal notranslate"><span class="pre">DTensor</span></code>:</p>
<ul class="simple">
<li><p><a class="reference external" href="https://pytorch.org/docs/main/distributed.tensor.parallel.html">Tensor Parallel</a></p></li>
<li><p><a class="reference external" href="https://github.com/pytorch/torchtitan/blob/main/docs/fsdp.md">FSDP2</a></p></li>
</ul>
<span class="target" id="module-torch.distributed.tensor"></span><p><a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> follows the SPMD (single program, multiple data) programming model to empower users to
write distributed program as if it’s a <strong>single-device program with the same convergence property</strong>. It
provides a uniform tensor sharding layout (DTensor Layout) through specifying the <code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code>
and <code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code>:</p>
<ul class="simple">
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code> represents the device topology and the communicators of the cluster using
an n-dimensional array.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code> describes the sharding layout of the logical tensor on the <code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code>.
DTensor supports three types of placements: <code class="xref py py-class docutils literal notranslate"><span class="pre">Shard</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">Replicate</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">Partial</span></code>.</p></li>
</ul>
<div class="section" id="dtensor-class-apis">
<h3>DTensor Class APIs<a class="headerlink" href="#dtensor-class-apis" title="Permalink to this heading">¶</a></h3>
<p><a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> is a <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code> subclass. This means once a <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> is created, it could be
used in very similar way to <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code>, including running different types of PyTorch operators as if
running them in a single device, allowing proper distributed computation for PyTorch operators.</p>
<p>In addition to existing <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code> methods, it also offers a set of additional methods to interact with
<code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code>, <code class="docutils literal notranslate"><span class="pre">redistribute</span></code> the DTensor Layout to a new DTensor, get the full tensor content
on all devices, etc.</p>
<dl class="py class">
<dt class="sig sig-object py" id="torch.distributed.tensor.DTensor">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">torch.distributed.tensor.</span></span><span class="sig-name descname"><span class="pre">DTensor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">local_tensor</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">spec</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">requires_grad</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#torch.distributed.tensor.DTensor" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">DTensor</span></code> (Distributed Tensor) is a subclass of <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code> that provides single-device like
abstraction to program with multi-device <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code>. It describes the distributed tensor sharding
layout (DTensor Layout) through the <code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code> and following types of <code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code>:</p>
<ul class="simple">
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Shard</span></code>: Tensor sharded on the tensor dimension <code class="docutils literal notranslate"><span class="pre">dim</span></code> on the devices of the <code class="docutils literal notranslate"><span class="pre">DeviceMesh</span></code> dimension</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Replicate</span></code>: Tensor replicated on the devices of the <code class="docutils literal notranslate"><span class="pre">DeviceMesh</span></code> dimension</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Partial</span></code>: Tensor is pending reduction on the devices of the <code class="docutils literal notranslate"><span class="pre">DeviceMesh</span></code> dimension</p></li>
</ul>
<p>When calling PyTorch operators, <code class="docutils literal notranslate"><span class="pre">DTensor</span></code> overrides the PyTorch operators to perform sharded computation and issue
communications whenever necessary. Along with the operator computation, <code class="docutils literal notranslate"><span class="pre">DTensor</span></code> will transform or propagate the
placements (DTensor Layout) properly (based on the operator semantic itself) and generate new <code class="docutils literal notranslate"><span class="pre">DTensor</span></code> outputs.</p>
<p>To ensure numerical correctness of the <code class="docutils literal notranslate"><span class="pre">DTensor</span></code> sharded computation when calling PyTorch operators, <code class="docutils literal notranslate"><span class="pre">DTensor</span></code>
requires every Tensor argument of the operator be DTensor.</p>
<dl class="field-list simple">
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor">DTensor</a></p>
</dd>
</dl>
<dl class="py property">
<dt class="sig sig-object py" id="torch.distributed.tensor.DTensor.device_mesh">
<em class="property"><span class="pre">property</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">device_mesh</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference internal" href="distributed.html#torch.distributed.device_mesh.DeviceMesh" title="torch.distributed.device_mesh.DeviceMesh"><span class="pre">DeviceMesh</span></a></em><a class="headerlink" href="#torch.distributed.tensor.DTensor.device_mesh" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code> attribute that associates with this DTensor object.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">device_mesh</span></code> is a read-only property, it can not be set.</p>
</div>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="torch.distributed.tensor.DTensor.from_local">
<em class="property"><span class="pre">static</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">from_local</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">local_tensor</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">device_mesh</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">placements</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">run_check</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">shape</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">stride</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/distributed/tensor/_api.html#DTensor.from_local"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.DTensor.from_local" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> from a local torch.Tensor on each rank
according to the <code class="docutils literal notranslate"><span class="pre">device_mesh</span></code> and <code class="docutils literal notranslate"><span class="pre">placements</span></code> specified.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>local_tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>torch.Tensor</em></a>) – local torch.Tensor on each rank.</p></li>
<li><p><strong>device_mesh</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code>, optional) – DeviceMesh to place the
tensor, if not specified, must be called under a DeviceMesh
context manager, default: None</p></li>
<li><p><strong>placements</strong> (List[<code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code>], optional) – the placements that
describes how to place the local torch.Tensor on DeviceMesh, must
have the same number of elements as <code class="docutils literal notranslate"><span class="pre">device_mesh.ndim</span></code>.</p></li>
</ul>
</dd>
<dt class="field-even">Keyword Arguments</dt>
<dd class="field-even"><ul class="simple">
<li><p><strong>run_check</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a><em>, </em><em>optional</em>) – at a cost of extra communications, perform
sanity check across ranks to check each local tensor’s meta information
to ensure correctness. If have <code class="xref py py-class docutils literal notranslate"><span class="pre">Replicate</span></code> in <code class="docutils literal notranslate"><span class="pre">placements</span></code>, the
data on first rank of the device mesh dimension will be broadcasted
to other ranks. default: False</p></li>
<li><p><strong>shape</strong> (<a class="reference internal" href="size.html#torch.Size" title="torch.Size"><em>torch.Size</em></a><em>, </em><em>optional</em>) – A List of int which specifies the size of
DTensor which build on top of <cite>local_tensor</cite>. Note this needs to be
provided if the shape of <code class="docutils literal notranslate"><span class="pre">local_tensor</span></code> are different across the ranks.
If not provided, <code class="docutils literal notranslate"><span class="pre">shape</span></code> will be computed assuming the given distributed
tensor is evenly sharded across ranks. default: None</p></li>
<li><p><strong>stride</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#tuple" title="(in Python v3.13)"><em>tuple</em></a><em>, </em><em>optional</em>) – A List of int which specifies the stride of DTensor.
If not provided, <code class="docutils literal notranslate"><span class="pre">stride</span></code> will be computed assuming the given distributed
tensor is evenly sharded across ranks. default: None</p></li>
</ul>
</dd>
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>A <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> object</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p><a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><em>DTensor</em></a></p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>When <code class="docutils literal notranslate"><span class="pre">run_check=False</span></code>, it is the user’s responsibility to ensure the
local tensor passed in is correct across ranks (i.e. the tensor is sharded for
the <code class="docutils literal notranslate"><span class="pre">Shard(dim)</span></code> placement or replicated for the <code class="docutils literal notranslate"><span class="pre">Replicate()</span></code> placement).
If not, the behavior of the created DTensor is undefined.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">from_local</span></code> is differentiable, the <cite>requires_grad</cite> of the created
<cite>DTensor</cite> object will depend on if <cite>local_tensor</cite> requires_grad or not.</p>
</div>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="torch.distributed.tensor.DTensor.full_tensor">
<span class="sig-name descname"><span class="pre">full_tensor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">grad_placements</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/distributed/tensor/_api.html#DTensor.full_tensor"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.DTensor.full_tensor" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the full tensor of this DTensor. It will perform necessary collectives
to gather the local tensors from other ranks in its DeviceMesh and concatenate
them together. It’s a syntatic sugar of the following code:</p>
<p><code class="docutils literal notranslate"><span class="pre">dtensor.redistribute(placements=[Replicate()]</span> <span class="pre">*</span> <span class="pre">mesh.ndim).to_local()</span></code></p>
<dl class="field-list simple">
<dt class="field-odd">Keyword Arguments</dt>
<dd class="field-odd"><p><strong>grad_placements</strong> (List[<code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code>], optional) – the placements describes
the future layout of any gradient layout of the full Tensor returned from this
function.
<cite>full_tensor</cite> converts DTensor to a full torch.Tensor and the returned torch.tensor
might not be used as the original replicated DTensor layout later in the code. This
argument is the hint that user can give to autograd in case the gradient
layout of the returned tensor does not match the original replicated DTensor layout.
If not specified, we will assume the gradient layout of the full tensor be replicated.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>A <a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">torch.Tensor</span></code></a> object that represents the full tensor of this DTensor.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a></p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">full_tensor</span></code> is differentiable.</p>
</div>
</dd></dl>
<dl class="py property">
<dt class="sig sig-object py" id="torch.distributed.tensor.DTensor.placements">
<em class="property"><span class="pre">property</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">placements</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Tuple" title="(in Python v3.13)"><span class="pre">Tuple</span></a><span class="p"><span class="pre">[</span></span><a class="reference internal" href="#torch.distributed.tensor.placement_types.Placement" title="torch.distributed.tensor.placement_types.Placement"><span class="pre">Placement</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="p"><span class="pre">...</span></span><span class="p"><span class="pre">]</span></span></em><a class="headerlink" href="#torch.distributed.tensor.DTensor.placements" title="Permalink to this definition">¶</a></dt>
<dd><p>The placements attribute of this DTensor that describes the layout of this
DTensor on the its DeviceMesh.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">placements</span></code> is a read-only property, it can not be set.</p>
</div>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="torch.distributed.tensor.DTensor.redistribute">
<span class="sig-name descname"><span class="pre">redistribute</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">device_mesh</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">placements</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">async_op</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/distributed/tensor/_api.html#DTensor.redistribute"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.DTensor.redistribute" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">redistribute</span></code> performs necessary collective operations that redistribute the current
DTensor from its current placements to a new placements, or from is current DeviceMesh
to a new DeviceMesh. i.e. we can turn a Sharded DTensor to a Replicated DTensor by
specifying a Replicate placement for each dimension of the DeviceMesh.</p>
<p>When redistributing from current to the new placements on one device mesh dimension, we
will perform the following operations including communication collective or local operation:</p>
<ol class="arabic simple">
<li><p><code class="docutils literal notranslate"><span class="pre">Shard(dim)</span></code> -> <code class="docutils literal notranslate"><span class="pre">Replicate()</span></code>: <code class="docutils literal notranslate"><span class="pre">all_gather</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Shard(src_dim)</span></code> -> <code class="docutils literal notranslate"><span class="pre">Shard(dst_dim)</span></code>: <code class="docutils literal notranslate"><span class="pre">all_to_all</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Replicate()</span></code> -> <code class="docutils literal notranslate"><span class="pre">Shard(dim)</span></code>: local chunking (i.e. <code class="docutils literal notranslate"><span class="pre">torch.chunk</span></code>)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Partial()</span></code> -> <code class="docutils literal notranslate"><span class="pre">Replicate()</span></code>: <code class="docutils literal notranslate"><span class="pre">all_reduce</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Partial()</span></code> -> <code class="docutils literal notranslate"><span class="pre">Shard(dim)</span></code>: <code class="docutils literal notranslate"><span class="pre">reduce_scatter</span></code></p></li>
</ol>
<p><code class="docutils literal notranslate"><span class="pre">redistribute</span></code> would correctly figure out the necessary redistribute steps for DTensors
that are created either on 1-D or N-D DeviceMesh.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>device_mesh</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code>, optional) – DeviceMesh to place the
DTensor. If not specified, it would use the current DTensor’s DeviceMesh.
default: None</p></li>
<li><p><strong>placements</strong> (List[<code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code>], optional) – the new placements that
describes how to place the DTensor into the DeviceMesh, must
have the same number of elements as <code class="docutils literal notranslate"><span class="pre">device_mesh.ndim</span></code>.
default: replicate on all mesh dimensions</p></li>
</ul>
</dd>
<dt class="field-even">Keyword Arguments</dt>
<dd class="field-even"><p><strong>async_op</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a><em>, </em><em>optional</em>) – whether to perform the DTensor redistribute operation
asynchronously or not. Default: False</p>
</dd>
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>A <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> object</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p><a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><em>DTensor</em></a></p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">redistribute</span></code> is differentiable, which means user do not need to worry about
the backward formula of the redistribute operation.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">redistribute</span></code> currently only supports redistributing DTensor on the same DeviceMesh,
Please file an issue if you need to redistribute DTensor to different DeviceMesh.</p>
</div>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="torch.distributed.tensor.DTensor.to_local">
<span class="sig-name descname"><span class="pre">to_local</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">grad_placements</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/distributed/tensor/_api.html#DTensor.to_local"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.DTensor.to_local" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the local tensor of this DTensor on its current rank. For sharding it returns
a local shard of the logical tensor view, for replication it returns the replica on
its current rank.</p>
<dl class="field-list simple">
<dt class="field-odd">Keyword Arguments</dt>
<dd class="field-odd"><p><strong>grad_placements</strong> (List[<code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code>], optional) – the placements describes
the future layout of any gradient layout of the Tensor returned from this
function.
<cite>to_local</cite> converts DTensor to local tensor and the returned local tensor
might not be used as the original DTensor layout later in the code. This
argument is the hint that user can give to autograd in case the gradient
layout of the returned tensor does not match the original DTensor layout.
If not specified, we will assume the gradient layout remains the same
as the original DTensor and use that for gradient computation.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>A <a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">torch.Tensor</span></code></a> or <code class="docutils literal notranslate"><span class="pre">AsyncCollectiveTensor</span></code> object. it represents the
local tensor on its current rank. When an <code class="docutils literal notranslate"><span class="pre">AsyncCollectiveTensor</span></code> object is returned,
it means the local tensor is not ready yet (i.e. communication is not finished). In this
case, user needs to call <code class="docutils literal notranslate"><span class="pre">wait</span></code> to wait the local tensor to be ready.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a></p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><code class="docutils literal notranslate"><span class="pre">to_local</span></code> is differentiable, the <code class="docutils literal notranslate"><span class="pre">requires_grad</span></code> of the local tensor returned
will depend on if the <cite>DTensor</cite> requires_grad or not.</p>
</div>
</dd></dl>
</dd></dl>
</div>
<div class="section" id="devicemesh-as-the-distributed-communicator">
<h3>DeviceMesh as the distributed communicator<a class="headerlink" href="#devicemesh-as-the-distributed-communicator" title="Permalink to this heading">¶</a></h3>
<p><a class="reference internal" href="distributed.html#torch.distributed.device_mesh.DeviceMesh" title="torch.distributed.device_mesh.DeviceMesh"><code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code></a> was built from DTensor as the abstraction to describe cluster’s device topology and represent
multi-dimensional communicators (on top of <code class="docutils literal notranslate"><span class="pre">ProcessGroup</span></code>). To see the details of how to create/use a DeviceMesh,
please refer to the <a class="reference external" href="https://pytorch.org/tutorials/recipes/distributed_device_mesh.html">DeviceMesh recipe</a>.</p>
</div>
<div class="section" id="module-torch.distributed.tensor.placement_types">
<span id="dtensor-placement-types"></span><h3>DTensor Placement Types<a class="headerlink" href="#module-torch.distributed.tensor.placement_types" title="Permalink to this heading">¶</a></h3>
<p>DTensor supports the following types of <a class="reference internal" href="#torch.distributed.tensor.placement_types.Placement" title="torch.distributed.tensor.placement_types.Placement"><code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code></a> on each <code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code> dimension:</p>
<dl class="py class">
<dt class="sig sig-object py" id="torch.distributed.tensor.placement_types.Shard">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">torch.distributed.tensor.placement_types.</span></span><span class="sig-name descname"><span class="pre">Shard</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dim</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/tensor/placement_types.html#Shard"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.placement_types.Shard" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">Shard(dim)</span></code> placement describes the DTensor sharding on tensor dimension
<code class="docutils literal notranslate"><span class="pre">dim</span></code> over a corresponding <code class="docutils literal notranslate"><span class="pre">DeviceMesh</span></code> dimension, where each rank on the
DeviceMesh dimension only holds a shard/piece of the global Tensor. The
<code class="docutils literal notranslate"><span class="pre">Shard(dim)</span></code> placement follows the <code class="docutils literal notranslate"><span class="pre">torch.chunk(dim)</span></code> semantic, where the
last few shards on the DeviceMesh dimension might be empty when the tensor dimension
is not evenly divisble on the DeviceMesh dimension. The <code class="docutils literal notranslate"><span class="pre">Shard</span></code> placement can be
used by all DTensor APIs (i.e. distribute_tensor, from_local, etc.)</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>dim</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><em>int</em></a>) – The tensor dimension that describes the DTensor is sharded over its
corresponding DeviceMesh dimension.</p>
</dd>
</dl>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>sharding on a tensor dimension where the tensor dimension size is not
evenly divisible on a DeviceMesh dimension is currently experimental and subject to change.</p>
</div>
<dl class="py attribute">
<dt class="sig sig-object py" id="torch.distributed.tensor.placement_types.Shard.dim">
<span class="sig-name descname"><span class="pre">dim</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><span class="pre">int</span></a></em><a class="headerlink" href="#torch.distributed.tensor.placement_types.Shard.dim" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
</dd></dl>
<dl class="py class">
<dt class="sig sig-object py" id="torch.distributed.tensor.placement_types.Replicate">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">torch.distributed.tensor.placement_types.</span></span><span class="sig-name descname"><span class="pre">Replicate</span></span><a class="reference internal" href="_modules/torch/distributed/tensor/placement_types.html#Replicate"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.placement_types.Replicate" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">Replicate()</span></code> placement describes the DTensor replicating on a corresponding
<code class="docutils literal notranslate"><span class="pre">DeviceMesh</span></code> dimension, where each rank on the DeviceMesh dimension holds a
replica of the global Tensor. The <code class="docutils literal notranslate"><span class="pre">Replicate</span></code> placement can be used by all
DTensor APIs (i.e. <code class="docutils literal notranslate"><span class="pre">distribute_tensor</span></code>, <code class="docutils literal notranslate"><span class="pre">DTensor.from_local</span></code>, etc.)</p>
<dl class="field-list simple">
</dl>
</dd></dl>
<dl class="py class">
<dt class="sig sig-object py" id="torch.distributed.tensor.placement_types.Partial">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">torch.distributed.tensor.placement_types.</span></span><span class="sig-name descname"><span class="pre">Partial</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">reduce_op</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'sum'</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/tensor/placement_types.html#Partial"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.placement_types.Partial" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">Partial(reduce_op)</span></code> placement describes the DTensor that is pending
reduction on a specified <code class="docutils literal notranslate"><span class="pre">DeviceMesh</span></code> dimension, where each rank on the
DeviceMesh dimension holds the partial value of the global Tensor. User can
redistribute the <code class="docutils literal notranslate"><span class="pre">Partial</span></code> DTensor to a <code class="docutils literal notranslate"><span class="pre">Replicate</span></code> or <code class="docutils literal notranslate"><span class="pre">Shard(dim)</span></code>
placement on the specified <code class="docutils literal notranslate"><span class="pre">DeviceMesh</span></code> dimension using <code class="docutils literal notranslate"><span class="pre">redistribute</span></code>,
which would trigger necessary communication operations under the hood (i.e.
<code class="docutils literal notranslate"><span class="pre">allreduce</span></code>, <code class="docutils literal notranslate"><span class="pre">reduce_scatter</span></code>).</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>reduce_op</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><em>str</em></a><em>, </em><em>optional</em>) – The reduction op to be used for the partial DTensor
to produce Replicated/Sharded DTensor. Only element-wise reduction operations
are supported, including: “sum”, “avg”, “product”, “max”, “min”, default: “sum”.</p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <code class="docutils literal notranslate"><span class="pre">Partial</span></code> placement can be generated as a result of the DTensor operators,
and can only be used by the <code class="docutils literal notranslate"><span class="pre">DTensor.from_local</span></code> API.</p>
</div>
<dl class="py attribute">
<dt class="sig sig-object py" id="torch.distributed.tensor.placement_types.Partial.reduce_op">
<span class="sig-name descname"><span class="pre">reduce_op</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><span class="pre">str</span></a></em><em class="property"><span class="w"> </span><span class="p"><span class="pre">=</span></span><span class="w"> </span><span class="pre">'sum'</span></em><a class="headerlink" href="#torch.distributed.tensor.placement_types.Partial.reduce_op" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
</dd></dl>
<dl class="py class">
<dt class="sig sig-object py" id="torch.distributed.tensor.placement_types.Placement">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">torch.distributed.tensor.placement_types.</span></span><span class="sig-name descname"><span class="pre">Placement</span></span><a class="reference internal" href="_modules/torch/distributed/tensor/placement_types.html#Placement"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.placement_types.Placement" title="Permalink to this definition">¶</a></dt>
<dd><p>The base class for the Placement type, where it describes how a DTensor is placed onto the
<code class="docutils literal notranslate"><span class="pre">DeviceMesh</span></code>. <code class="docutils literal notranslate"><span class="pre">Placement</span></code> and <code class="docutils literal notranslate"><span class="pre">DeviceMesh</span></code> together could describe the DTensor Layout.
It is the base class of the three main DTensor Placement types: <code class="docutils literal notranslate"><span class="pre">Shard</span></code>, <code class="docutils literal notranslate"><span class="pre">Replicate</span></code>,
and <code class="docutils literal notranslate"><span class="pre">Partial</span></code>.</p>
<p>This class is not meant to be used directly, mainly served as a typing stub.</p>
<dl class="py method">
<dt class="sig sig-object py" id="torch.distributed.tensor.placement_types.Placement.is_partial">
<span class="sig-name descname"><span class="pre">is_partial</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/tensor/placement_types.html#Placement.is_partial"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.placement_types.Placement.is_partial" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)">bool</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="torch.distributed.tensor.placement_types.Placement.is_replicate">
<span class="sig-name descname"><span class="pre">is_replicate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/tensor/placement_types.html#Placement.is_replicate"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.placement_types.Placement.is_replicate" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)">bool</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py method">
<dt class="sig sig-object py" id="torch.distributed.tensor.placement_types.Placement.is_shard">
<span class="sig-name descname"><span class="pre">is_shard</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dim</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/distributed/tensor/placement_types.html#Placement.is_shard"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#torch.distributed.tensor.placement_types.Placement.is_shard" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)">bool</a></p>
</dd>
</dl>
</dd></dl>
</dd></dl>
</div>
</div>
<div class="section" id="different-ways-to-create-a-dtensor">
<h2>Different ways to create a DTensor<a class="headerlink" href="#different-ways-to-create-a-dtensor" title="Permalink to this heading">¶</a></h2>
<dl class="simple">
<dt>There’re three ways to construct a <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a>:</dt><dd><ul class="simple">
<li><p><a class="reference internal" href="#torch.distributed.tensor.distribute_tensor" title="torch.distributed.tensor.distribute_tensor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">distribute_tensor()</span></code></a> creates a <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> from a logical or “global” <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code> on
each rank. This could be used to shard the leaf <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code> s (i.e. model parameters/buffers
and inputs).</p></li>
<li><p><a class="reference internal" href="#torch.distributed.tensor.DTensor.from_local" title="torch.distributed.tensor.DTensor.from_local"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DTensor.from_local()</span></code></a> creates a <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> from a local <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code> on each rank, which can
be used to create <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> from a non-leaf <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code> s (i.e. intermediate activation
tensors during forward/backward).</p></li>
<li><p>DTensor provides dedicated tensor factory functions (e.g. <a class="reference internal" href="#torch.distributed.tensor.empty" title="torch.distributed.tensor.empty"><code class="xref py py-meth docutils literal notranslate"><span class="pre">empty()</span></code></a>, <a class="reference internal" href="#torch.distributed.tensor.ones" title="torch.distributed.tensor.ones"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ones()</span></code></a>, <a class="reference internal" href="#torch.distributed.tensor.randn" title="torch.distributed.tensor.randn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">randn()</span></code></a>, etc.)
to allow different <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> creations by directly specifying the <code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code> and
<code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code>. Compare to <a class="reference internal" href="#torch.distributed.tensor.distribute_tensor" title="torch.distributed.tensor.distribute_tensor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">distribute_tensor()</span></code></a>, this could directly materializing the sharded memory
on device, instead of performing sharding after initializing the logical Tensor memory.</p></li>
</ul>
</dd>
</dl>
<div class="section" id="create-dtensor-from-a-logical-torch-tensor">
<h3>Create DTensor from a logical torch.Tensor<a class="headerlink" href="#create-dtensor-from-a-logical-torch-tensor" title="Permalink to this heading">¶</a></h3>
<p>The SPMD (single program, multiple data) programming model in <code class="docutils literal notranslate"><span class="pre">torch.distributed</span></code> launches multiple processes
(i.e. via <code class="docutils literal notranslate"><span class="pre">torchrun</span></code>) to execute the same program, this means that the model inside the program would be
initialized on different processes first (i.e. the model might be initialized on CPU, or meta device, or directly
on GPU if enough memory).</p>
<p><code class="docutils literal notranslate"><span class="pre">DTensor</span></code> offers a <a class="reference internal" href="#torch.distributed.tensor.distribute_tensor" title="torch.distributed.tensor.distribute_tensor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">distribute_tensor()</span></code></a> API that could shard the model weights or Tensors to <code class="docutils literal notranslate"><span class="pre">DTensor</span></code> s,
where it would create a DTensor from the “logical” Tensor on each process. This would empower the created
<code class="docutils literal notranslate"><span class="pre">DTensor</span></code> s to comply with the single device semantic, which is critical for <strong>numerical correctness</strong>.</p>
<dl class="py function">
<dt class="sig sig-object py" id="torch.distributed.tensor.distribute_tensor">
<span class="sig-prename descclassname"><span class="pre">torch.distributed.tensor.</span></span><span class="sig-name descname"><span class="pre">distribute_tensor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tensor</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">device_mesh</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">placements</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="headerlink" href="#torch.distributed.tensor.distribute_tensor" title="Permalink to this definition">¶</a></dt>
<dd><p>Distribute a leaf <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code> (i.e. nn.Parameter/buffers) to the <code class="docutils literal notranslate"><span class="pre">device_mesh</span></code> according
to the <code class="docutils literal notranslate"><span class="pre">placements</span></code> specified. The rank of <code class="docutils literal notranslate"><span class="pre">device_mesh</span></code> and <code class="docutils literal notranslate"><span class="pre">placements</span></code> must be the
same. The <code class="docutils literal notranslate"><span class="pre">tensor</span></code> to distribute is the logical or “global” tensor, and the API would use
the <code class="docutils literal notranslate"><span class="pre">tensor</span></code> from first rank of the DeviceMesh dimension as the source of truth to perserve
the single-device semantic. If you want to construct a DTensor in the middle of the Autograd
computation, please use <a class="reference internal" href="#torch.distributed.tensor.DTensor.from_local" title="torch.distributed.tensor.DTensor.from_local"><code class="xref py py-meth docutils literal notranslate"><span class="pre">DTensor.from_local()</span></code></a> instead.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>torch.Tensor</em></a>) – torch.Tensor to be distributed. Note that if you
want to shard a tensor on a dimension that is not evenly divisible by
the number of devices in that mesh dimension, we use <code class="docutils literal notranslate"><span class="pre">torch.chunk</span></code>
semantic to shard the tensor and scatter the shards. The uneven sharding
behavior is experimental and subject to change.</p></li>
<li><p><strong>device_mesh</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code>, optional) – DeviceMesh to distribute the
tensor, if not specified, must be called under a DeviceMesh context
manager, default: None</p></li>
<li><p><strong>placements</strong> (List[<code class="xref py py-class docutils literal notranslate"><span class="pre">Placement</span></code>], optional) – the placements that
describes how to place the tensor on DeviceMesh, must have the same
number of elements as <code class="docutils literal notranslate"><span class="pre">device_mesh.ndim</span></code>. If not specified, we will
by default replicate the tensor across the <code class="docutils literal notranslate"><span class="pre">device_mesh</span></code> from the
first rank of each dimension of the <cite>device_mesh</cite>.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>A <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a> or <code class="docutils literal notranslate"><span class="pre">XLAShardedTensor</span></code> object.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><em>DTensor</em></a></p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>When initialize the DeviceMesh with the <code class="docutils literal notranslate"><span class="pre">xla</span></code> device_type, <code class="docutils literal notranslate"><span class="pre">distribute_tensor</span></code>
return <cite>XLAShardedTensor</cite> instead. see <a class="reference external" href="https://github.com/pytorch/pytorch/issues/92909">this issue</a>
for more details. The XLA integration is experimental and subject to change.</p>
</div>
</dd></dl>
<p>Along with <a class="reference internal" href="#torch.distributed.tensor.distribute_tensor" title="torch.distributed.tensor.distribute_tensor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">distribute_tensor()</span></code></a>, DTensor also offers a <a class="reference internal" href="#torch.distributed.tensor.distribute_module" title="torch.distributed.tensor.distribute_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">distribute_module()</span></code></a> API to allow easier
sharding on the <code class="xref py py-class docutils literal notranslate"><span class="pre">nn.Module</span></code> level</p>
<dl class="py function">
<dt class="sig sig-object py" id="torch.distributed.tensor.distribute_module">
<span class="sig-prename descclassname"><span class="pre">torch.distributed.tensor.</span></span><span class="sig-name descname"><span class="pre">distribute_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">module</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">device_mesh</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">partition_fn</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">input_fn</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">output_fn</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="headerlink" href="#torch.distributed.tensor.distribute_module" title="Permalink to this definition">¶</a></dt>
<dd><p>This function expose three functions to control the parameters/inputs/outputs of the module:</p>
<p>1. To perform sharding on the module before runtime execution by specifying the
<code class="docutils literal notranslate"><span class="pre">partition_fn</span></code> (i.e. allow user to convert Module parameters to <a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a>
parameters according to the <cite>partition_fn</cite> specified).
2. To control the inputs or outputs of the module during runtime execution by
specifying the <code class="docutils literal notranslate"><span class="pre">input_fn</span></code> and <code class="docutils literal notranslate"><span class="pre">output_fn</span></code>. (i.e. convert the input to
<a class="reference internal" href="#torch.distributed.tensor.DTensor" title="torch.distributed.tensor.DTensor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DTensor</span></code></a>, convert the output back to <code class="docutils literal notranslate"><span class="pre">torch.Tensor</span></code>)</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>module</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">nn.Module</span></code>) – user module to be partitioned.</p></li>
<li><p><strong>device_mesh</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">DeviceMesh</span></code>) – the device mesh to place the module.</p></li>
<li><p><strong>partition_fn</strong> (<em>Callable</em>) – the function to partition parameters (i.e. shard certain
parameters across the <code class="docutils literal notranslate"><span class="pre">device_mesh</span></code>). If <code class="docutils literal notranslate"><span class="pre">partition_fn</span></code> is not specified,
by default we replicate all module parameters of <code class="docutils literal notranslate"><span class="pre">module</span></code> across the mesh.</p></li>
<li><p><strong>input_fn</strong> (<em>Callable</em>) – specify the input distribution, i.e. could control how the
input of the module is sharded. <code class="docutils literal notranslate"><span class="pre">input_fn</span></code> will be installed as a module
<code class="docutils literal notranslate"><span class="pre">forward_pre_hook</span></code> (pre forward hook).</p></li>
<li><p><strong>output_fn</strong> (<em>Callable</em>) – specify the output distribution, i.e. could control how the
output is sharded, or convert it back to torch.Tensor. <code class="docutils literal notranslate"><span class="pre">output_fn</span></code> will be
installed as a module <code class="docutils literal notranslate"><span class="pre">forward_hook</span></code> (post forward hook).</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>A module that contains parameters/buffers that are all <code class="docutils literal notranslate"><span class="pre">DTensor</span></code> s.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><p id="torch.nn.Module"/><a class="reference internal" href="generated/torch.nn.Module.html#torch.nn.Module" title="torch.nn.modules.module.Module"><em>Module</em></a></p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>When initialize the DeviceMesh with the <code class="docutils literal notranslate"><span class="pre">xla</span></code> device_type, <code class="docutils literal notranslate"><span class="pre">distribute_module</span></code>
return nn.Module with PyTorch/XLA SPMD annotated parameters. See
<a class="reference external" href="https://github.com/pytorch/pytorch/issues/92909">this issue</a>
for more details. The XLA integration is experimental and subject to change.</p>
</div>
</dd></dl>