forked from pytorch/pytorch.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistributed_deprecated.html
1400 lines (1212 loc) · 85.8 KB
/
distributed_deprecated.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Distributed communication package (deprecated) - torch.distributed.deprecated — PyTorch master documentation</title>
<link rel="canonical" href="https://pytorch.org/docs/stable/distributed_deprecated.html"/>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<!-- <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="stylesheet" href="_static/katex-math.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Legacy package - torch.legacy" href="legacy.html" />
<link rel="prev" title="torch.onnx" href="onnx.html" />
<script src="_static/js/modernizr.min.js"></script>
</head>
<div class="container-fluid header-holder tutorials-header" id="header-holder">
<div class="container">
<div class="header-container">
<a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
<div class="main-menu">
<ul>
<li>
<a href="https://pytorch.org/get-started">Get Started</a>
</li>
<li>
<a href="https://pytorch.org/features">Features</a>
</li>
<li>
<a href="https://pytorch.org/ecosystem">Ecosystem</a>
</li>
<li>
<a href="https://pytorch.org/blog/">Blog</a>
</li>
<li>
<a href="https://pytorch.org/tutorials">Tutorials</a>
</li>
<li class="active">
<a href="https://pytorch.org/docs/stable/index.html">Docs</a>
</li>
<li>
<a href="https://pytorch.org/resources">Resources</a>
</li>
<li>
<a href="https://github.com/pytorch/pytorch">Github</a>
</li>
</ul>
</div>
<a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
</div>
</div>
</div>
<body class="pytorch-body">
<div class="table-of-contents-link-wrapper">
<span>Table of Contents</span>
<a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
</div>
<nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
<div class="pytorch-side-scroll">
<div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<div class="pytorch-left-menu-search">
<div class="version">
<a href='http://pytorch.org/docs/versions.html'>1.0.1 ▼</a>
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search Docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p class="caption"><span class="caption-text">Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="notes/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/cuda.html">CUDA semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/extending.html">Extending PyTorch</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/multiprocessing.html">Multiprocessing best practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/randomness.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/serialization.html">Serialization semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/windows.html">Windows FAQ</a></li>
</ul>
<p class="caption"><span class="caption-text">Community</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="community/contribution_guide.html">PyTorch Contribution Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/governance.html">PyTorch Governance</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/persons_of_interest.html">PyTorch Governance | Persons of Interest</a></li>
</ul>
<p class="caption"><span class="caption-text">Package Reference</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="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="type_info.html">Type Info</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="cuda.html">torch.cuda</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="nn.html">torch.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.html#torch-nn-functional">torch.nn.functional</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.html#torch-nn-init">torch.nn.init</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="autograd.html">torch.autograd</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.html">torch.distributed</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributions.html">torch.distributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="jit.html">torch.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="multiprocessing.html">torch.multiprocessing</a></li>
<li class="toctree-l1"><a class="reference internal" href="bottleneck.html">torch.utils.bottleneck</a></li>
<li class="toctree-l1"><a class="reference internal" href="checkpoint.html">torch.utils.checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="cpp_extension.html">torch.utils.cpp_extension</a></li>
<li class="toctree-l1"><a class="reference internal" href="data.html">torch.utils.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="dlpack.html">torch.utils.dlpack</a></li>
<li class="toctree-l1"><a class="reference internal" href="ffi.html">torch.utils.ffi</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="model_zoo.html">torch.utils.model_zoo</a></li>
<li class="toctree-l1"><a class="reference internal" href="onnx.html">torch.onnx</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">torch.distributed.deprecated</a></li>
<li class="toctree-l1"><a class="reference internal" href="legacy.html">torch.legacy</a></li>
</ul>
<p class="caption"><span class="caption-text">torchvision Reference</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="torchvision/index.html">torchvision</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>Distributed communication package (deprecated) - torch.distributed.deprecated</li>
<li class="pytorch-breadcrumbs-aside">
<a href="_sources/distributed_deprecated.rst.txt" rel="nofollow"><img src="_static/images/view-page-source-icon.svg"></a>
</li>
</ul>
</div>
</div>
<div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
Shortcuts
</div>
</div>
<section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
<div class="pytorch-content-left">
<div class="rst-content">
<div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
<article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
<div class="section" id="distributed-communication-package-deprecated-torch-distributed-deprecated">
<h1>Distributed communication package (deprecated) - torch.distributed.deprecated<a class="headerlink" href="#distributed-communication-package-deprecated-torch-distributed-deprecated" title="Permalink to this headline">¶</a></h1>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">torch.distributed.deprecated is the older version of torch.distributed and
currently deprecated. It will be removed soon. Please use and refer the doc
for torch.distributed, which is the latest distributed communication
package for PyTorch</p>
</div>
<span class="target" id="module-torch.distributed.deprecated"></span><p>torch.distributed.deprecated provides an MPI-like interface for exchanging tensor
data across multi-machine networks. It supports a few different backends
and initialization methods.</p>
<p>Currently torch.distributed.deprecated supports four backends, each with
different capabilities. The table below shows which functions are available
for use with CPU / CUDA tensors.
MPI supports cuda only if the implementation used to build PyTorch supports it.</p>
<table border="1" class="docutils">
<colgroup>
<col width="23%" />
<col width="10%" />
<col width="10%" />
<col width="10%" />
<col width="10%" />
<col width="10%" />
<col width="10%" />
<col width="10%" />
<col width="10%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Backend</th>
<th class="head" colspan="2"><code class="docutils literal"><span class="pre">tcp</span></code></th>
<th class="head" colspan="2"><code class="docutils literal"><span class="pre">gloo</span></code></th>
<th class="head" colspan="2"><code class="docutils literal"><span class="pre">mpi</span></code></th>
<th class="head" colspan="2"><code class="docutils literal"><span class="pre">nccl</span></code></th>
</tr>
<tr class="row-even"><th class="head">Device</th>
<th class="head">CPU</th>
<th class="head">GPU</th>
<th class="head">CPU</th>
<th class="head">GPU</th>
<th class="head">CPU</th>
<th class="head">GPU</th>
<th class="head">CPU</th>
<th class="head">GPU</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-odd"><td>send</td>
<td>✓</td>
<td>✘</td>
<td>✘</td>
<td>✘</td>
<td>✓</td>
<td>?</td>
<td>✘</td>
<td>✘</td>
</tr>
<tr class="row-even"><td>recv</td>
<td>✓</td>
<td>✘</td>
<td>✘</td>
<td>✘</td>
<td>✓</td>
<td>?</td>
<td>✘</td>
<td>✘</td>
</tr>
<tr class="row-odd"><td>broadcast</td>
<td>✓</td>
<td>✘</td>
<td>✓</td>
<td>✓</td>
<td>✓</td>
<td>?</td>
<td>✘</td>
<td>✓</td>
</tr>
<tr class="row-even"><td>all_reduce</td>
<td>✓</td>
<td>✘</td>
<td>✓</td>
<td>✓</td>
<td>✓</td>
<td>?</td>
<td>✘</td>
<td>✓</td>
</tr>
<tr class="row-odd"><td>reduce</td>
<td>✓</td>
<td>✘</td>
<td>✘</td>
<td>✘</td>
<td>✓</td>
<td>?</td>
<td>✘</td>
<td>✓</td>
</tr>
<tr class="row-even"><td>all_gather</td>
<td>✓</td>
<td>✘</td>
<td>✘</td>
<td>✘</td>
<td>✓</td>
<td>?</td>
<td>✘</td>
<td>✓</td>
</tr>
<tr class="row-odd"><td>gather</td>
<td>✓</td>
<td>✘</td>
<td>✘</td>
<td>✘</td>
<td>✓</td>
<td>?</td>
<td>✘</td>
<td>✘</td>
</tr>
<tr class="row-even"><td>scatter</td>
<td>✓</td>
<td>✘</td>
<td>✘</td>
<td>✘</td>
<td>✓</td>
<td>?</td>
<td>✘</td>
<td>✘</td>
</tr>
<tr class="row-odd"><td>barrier</td>
<td>✓</td>
<td>✘</td>
<td>✓</td>
<td>✓</td>
<td>✓</td>
<td>?</td>
<td>✘</td>
<td>✘</td>
</tr>
</tbody>
</table>
<div class="section" id="basics">
<span id="distributed-deprecated-basics"></span><h2>Basics<a class="headerlink" href="#basics" title="Permalink to this headline">¶</a></h2>
<p>The <cite>torch.distributed.deprecated</cite> package provides PyTorch support and communication primitives
for multiprocess parallelism across several computation nodes running on one or more
machines. The class <code class="xref py py-func docutils literal"><span class="pre">torch.nn.parallel.deprecated.DistributedDataParallel()</span></code> builds on this
functionality to provide synchronous distributed training as a wrapper around any
PyTorch model. This differs from the kinds of parallelism provided by
<a class="reference internal" href="multiprocessing.html"><span class="doc">Multiprocessing package - torch.multiprocessing</span></a> and <a class="reference internal" href="nn.html#torch.nn.DataParallel" title="torch.nn.DataParallel"><code class="xref py py-func docutils literal"><span class="pre">torch.nn.DataParallel()</span></code></a> in that it supports
multiple network-connected machines and in that the user must explicitly launch a separate
copy of the main training script for each process.</p>
<p>In the single-machine synchronous case, <cite>torch.distributed.deprecated</cite> or the
<code class="xref py py-func docutils literal"><span class="pre">torch.nn.parallel.deprecated.DistributedDataParallel()</span></code> wrapper may still have advantages over other
approaches to data-parallelism, including <a class="reference internal" href="nn.html#torch.nn.DataParallel" title="torch.nn.DataParallel"><code class="xref py py-func docutils literal"><span class="pre">torch.nn.DataParallel()</span></code></a>:</p>
<ul class="simple">
<li>Each process maintains its own optimizer and performs a complete optimization step with each
iteration. While this may appear redundant, since the gradients have already been gathered
together and averaged across processes and are thus the same for every process, this means
that no parameter broadcast step is needed, reducing time spent transferring tensors between
nodes.</li>
<li>Each process contains an independent Python interpreter, eliminating the extra interpreter
overhead and “GIL-thrashing” that comes from driving several execution threads, model
replicas, or GPUs from a single Python process. This is especially important for models that
make heavy use of the Python runtime, including models with recurrent layers or many small
components.</li>
</ul>
</div>
<div class="section" id="initialization">
<h2>Initialization<a class="headerlink" href="#initialization" title="Permalink to this headline">¶</a></h2>
<p>The package needs to be initialized using the <a class="reference internal" href="#torch.distributed.deprecated.init_process_group" title="torch.distributed.deprecated.init_process_group"><code class="xref py py-func docutils literal"><span class="pre">torch.distributed.deprecated.init_process_group()</span></code></a>
function before calling any other methods. This blocks until all processes have
joined.</p>
<dl class="function">
<dt id="torch.distributed.deprecated.init_process_group">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">init_process_group</code><span class="sig-paren">(</span><em>backend</em>, <em>init_method='env://'</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#init_process_group"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.init_process_group" title="Permalink to this definition">¶</a></dt>
<dd><p>Initializes the distributed package.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>backend</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a>) – Name of the backend to use. Depending on build-time configuration
valid values include: <code class="docutils literal"><span class="pre">tcp</span></code>, <code class="docutils literal"><span class="pre">mpi</span></code>, <code class="docutils literal"><span class="pre">gloo</span></code> and <code class="docutils literal"><span class="pre">nccl</span></code>.</li>
<li><strong>init_method</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a><em>, </em><em>optional</em>) – URL specifying how to initialize the package.</li>
<li><strong>world_size</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a><em>, </em><em>optional</em>) – Number of processes participating in the job.</li>
<li><strong>rank</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a><em>, </em><em>optional</em>) – Rank of the current process.</li>
<li><strong>group_name</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.7)"><em>str</em></a><em>, </em><em>optional</em>) – Group name. See description of init methods.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>To enable <code class="docutils literal"><span class="pre">backend</span> <span class="pre">==</span> <span class="pre">mpi</span></code>, PyTorch needs to built from source on a system that
supports MPI. If you want to use Open MPI with CUDA-aware support, please use
Open MPI major version 2 and above.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This method initializes CUDA context. Therefore, if multiple processes
run on a single machine but use different GPUs, make sure to use
<a class="reference internal" href="cuda.html#torch.cuda.set_device" title="torch.cuda.set_device"><code class="xref py py-func docutils literal"><span class="pre">torch.cuda.set_device()</span></code></a> before this method to avoid unnecessarily
creating context on the first visible device.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.get_rank">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">get_rank</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#get_rank"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.get_rank" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the rank of current process.</p>
<p>Rank is a unique identifier assigned to each process within a distributed
group. They are always consecutive integers ranging from <code class="docutils literal"><span class="pre">0</span></code> to
<code class="docutils literal"><span class="pre">world_size</span> <span class="pre">-</span> <span class="pre">1</span></code> (inclusive).</p>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.get_world_size">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">get_world_size</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#get_world_size"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.get_world_size" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the number of processes in the distributed group.</p>
</dd></dl>
<hr class="docutils" />
<p>Currently three initialization methods are supported:</p>
<div class="section" id="tcp-initialization">
<h3>TCP initialization<a class="headerlink" href="#tcp-initialization" title="Permalink to this headline">¶</a></h3>
<p>There are two ways to initialize using TCP, both requiring a network address
reachable from all processes and a desired <code class="docutils literal"><span class="pre">world_size</span></code>. The first way
requires specifying an address that belongs to the rank 0 process. This
initialization method requires that all processes have manually specified ranks.</p>
<p>Alternatively, the address has to be a valid IP multicast address, in which case
ranks can be assigned automatically. Multicast initialization also supports
a <code class="docutils literal"><span class="pre">group_name</span></code> argument, which allows you to use the same address for multiple
jobs, as long as they use different group names.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch.distributed.deprecated</span> <span class="k">as</span> <span class="nn">dist</span>
<span class="c1"># Use address of one of the machines</span>
<span class="n">dist</span><span class="o">.</span><span class="n">init_process_group</span><span class="p">(</span><span class="n">backend</span><span class="p">,</span> <span class="n">init_method</span><span class="o">=</span><span class="s1">'tcp://10.1.1.20:23456'</span><span class="p">,</span> <span class="n">rank</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">rank</span><span class="p">,</span> <span class="n">world_size</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="c1"># or a multicast address - rank will be assigned automatically if unspecified</span>
<span class="n">dist</span><span class="o">.</span><span class="n">init_process_group</span><span class="p">(</span><span class="n">backend</span><span class="p">,</span> <span class="n">init_method</span><span class="o">=</span><span class="s1">'tcp://[ff15:1e18:5d4c:4cf0:d02d:b659:53ba:b0a7]:23456'</span><span class="p">,</span>
<span class="n">world_size</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="shared-file-system-initialization">
<h3>Shared file-system initialization<a class="headerlink" href="#shared-file-system-initialization" title="Permalink to this headline">¶</a></h3>
<p>Another initialization method makes use of a file system that is shared and
visible from all machines in a group, along with a desired <code class="docutils literal"><span class="pre">world_size</span></code>. The URL should start
with <code class="docutils literal"><span class="pre">file://</span></code> and contain a path to a non-existent file (in an existing
directory) on a shared file system. This initialization method also supports a
<code class="docutils literal"><span class="pre">group_name</span></code> argument, which allows you to use the same shared file path for
multiple jobs, as long as they use different group names.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">This method assumes that the file system supports locking using <code class="docutils literal"><span class="pre">fcntl</span></code> - most
local systems and NFS support it.</p>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch.distributed.deprecated</span> <span class="k">as</span> <span class="nn">dist</span>
<span class="c1"># Rank will be assigned automatically if unspecified</span>
<span class="n">dist</span><span class="o">.</span><span class="n">init_process_group</span><span class="p">(</span><span class="n">backend</span><span class="p">,</span> <span class="n">init_method</span><span class="o">=</span><span class="s1">'file:///mnt/nfs/sharedfile'</span><span class="p">,</span>
<span class="n">world_size</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">group_name</span><span class="o">=</span><span class="n">args</span><span class="o">.</span><span class="n">group</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="environment-variable-initialization">
<h3>Environment variable initialization<a class="headerlink" href="#environment-variable-initialization" title="Permalink to this headline">¶</a></h3>
<p>This method will read the configuration from environment variables, allowing
one to fully customize how the information is obtained. The variables to be set
are:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">MASTER_PORT</span></code> - required; has to be a free port on machine with rank 0</li>
<li><code class="docutils literal"><span class="pre">MASTER_ADDR</span></code> - required (except for rank 0); address of rank 0 node</li>
<li><code class="docutils literal"><span class="pre">WORLD_SIZE</span></code> - required; can be set either here, or in a call to init function</li>
<li><code class="docutils literal"><span class="pre">RANK</span></code> - required; can be set either here, or in a call to init function</li>
</ul>
<p>The machine with rank 0 will be used to set up all connections.</p>
<p>This is the default method, meaning that <code class="docutils literal"><span class="pre">init_method</span></code> does not have to be specified (or
can be <code class="docutils literal"><span class="pre">env://</span></code>).</p>
</div>
</div>
<div class="section" id="groups">
<h2>Groups<a class="headerlink" href="#groups" title="Permalink to this headline">¶</a></h2>
<p>By default collectives operate on the default group (also called the world) and
require all processes to enter the distributed function call. However, some workloads can benefit
from more fine-grained communication. This is where distributed groups come
into play. <a class="reference internal" href="#torch.distributed.deprecated.new_group" title="torch.distributed.deprecated.new_group"><code class="xref py py-func docutils literal"><span class="pre">new_group()</span></code></a> function can be
used to create new groups, with arbitrary subsets of all processes. It returns
an opaque group handle that can be given as a <code class="docutils literal"><span class="pre">group</span></code> argument to all collectives
(collectives are distributed functions to exchange information in certain well-known programming patterns).</p>
<dl class="function">
<dt id="torch.distributed.deprecated.new_group">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">new_group</code><span class="sig-paren">(</span><em>ranks=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#new_group"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.new_group" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a new distributed group.</p>
<p>This function requires that all processes in the main group (i.e., all
processes that are part of the distributed job) enter this function, even
if they are not going to be members of the group. Additionally, groups
should be created in the same order in all processes.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>ranks</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.7)"><em>list</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a><em>]</em>) – List of ranks of group members.</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body">A handle of distributed group that can be given to collective calls.</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
<div class="section" id="point-to-point-communication">
<h2>Point-to-point communication<a class="headerlink" href="#point-to-point-communication" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="torch.distributed.deprecated.send">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">send</code><span class="sig-paren">(</span><em>tensor</em>, <em>dst</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#send"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.send" title="Permalink to this definition">¶</a></dt>
<dd><p>Sends a tensor synchronously.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Tensor to send.</li>
<li><strong>dst</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – Destination rank.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.recv">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">recv</code><span class="sig-paren">(</span><em>tensor</em>, <em>src=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#recv"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.recv" title="Permalink to this definition">¶</a></dt>
<dd><p>Receives a tensor synchronously.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Tensor to fill with received data.</li>
<li><strong>src</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a><em>, </em><em>optional</em>) – Source rank. Will receive from any
process if unspecified.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">Sender rank.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p><a class="reference internal" href="#torch.distributed.deprecated.isend" title="torch.distributed.deprecated.isend"><code class="xref py py-func docutils literal"><span class="pre">isend()</span></code></a> and <a class="reference internal" href="#torch.distributed.deprecated.irecv" title="torch.distributed.deprecated.irecv"><code class="xref py py-func docutils literal"><span class="pre">irecv()</span></code></a>
return distributed request objects when used. In general, the type of this object is unspecified
as they should never be created manually, but they are guaranteed to support two methods:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">is_completed()</span></code> - returns True if the operation has finished</li>
<li><code class="docutils literal"><span class="pre">wait()</span></code> - will block the process until the operation is finished.
<code class="docutils literal"><span class="pre">is_completed()</span></code> is guaranteed to return True once it returns.</li>
</ul>
<p>When using the MPI backend, <a class="reference internal" href="#torch.distributed.deprecated.isend" title="torch.distributed.deprecated.isend"><code class="xref py py-func docutils literal"><span class="pre">isend()</span></code></a> and <a class="reference internal" href="#torch.distributed.deprecated.irecv" title="torch.distributed.deprecated.irecv"><code class="xref py py-func docutils literal"><span class="pre">irecv()</span></code></a>
support non-overtaking, which has some guarantees on supporting message order. For more detail, see
<a class="reference external" href="http://mpi-forum.org/docs/mpi-2.2/mpi22-report/node54.htm#Node54">http://mpi-forum.org/docs/mpi-2.2/mpi22-report/node54.htm#Node54</a></p>
<dl class="function">
<dt id="torch.distributed.deprecated.isend">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">isend</code><span class="sig-paren">(</span><em>tensor</em>, <em>dst</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#isend"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.isend" title="Permalink to this definition">¶</a></dt>
<dd><p>Sends a tensor asynchronously.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Tensor to send.</li>
<li><strong>dst</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – Destination rank.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">A distributed request object.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.irecv">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">irecv</code><span class="sig-paren">(</span><em>tensor</em>, <em>src</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#irecv"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.irecv" title="Permalink to this definition">¶</a></dt>
<dd><p>Receives a tensor asynchronously.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Tensor to fill with received data.</li>
<li><strong>src</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – Source rank.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">A distributed request object.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
<div class="section" id="collective-functions">
<h2>Collective functions<a class="headerlink" href="#collective-functions" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="torch.distributed.deprecated.broadcast">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">broadcast</code><span class="sig-paren">(</span><em>tensor</em>, <em>src</em>, <em>group=<object object></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#broadcast"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.broadcast" title="Permalink to this definition">¶</a></dt>
<dd><p>Broadcasts the tensor to the whole group.</p>
<p><code class="xref py py-attr docutils literal"><span class="pre">tensor</span></code> must have the same number of elements in all processes
participating in the collective.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Data to be sent if <code class="xref py py-attr docutils literal"><span class="pre">src</span></code> is the rank of
current process, and tensor to be used to save received data
otherwise.</li>
<li><strong>src</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – Source rank.</li>
<li><strong>group</strong> (<em>optional</em>) – Group of the collective.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.all_reduce">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">all_reduce</code><span class="sig-paren">(</span><em>tensor</em>, <em>op=<object object></em>, <em>group=<object object></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#all_reduce"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.all_reduce" title="Permalink to this definition">¶</a></dt>
<dd><p>Reduces the tensor data across all machines in such a way that all get
the final result.</p>
<p>After the call <code class="xref py py-attr docutils literal"><span class="pre">tensor</span></code> will be bitwise identical in all processes.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Input and output of the collective. The function
operates in-place.</li>
<li><strong>op</strong> (<em>optional</em>) – One of the values from <code class="docutils literal"><span class="pre">torch.distributed.deprecated.reduce_op</span></code>
enum. Specifies an operation used for element-wise reductions.</li>
<li><strong>group</strong> (<em>optional</em>) – Group of the collective.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.reduce">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">reduce</code><span class="sig-paren">(</span><em>tensor</em>, <em>dst</em>, <em>op=<object object></em>, <em>group=<object object></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#reduce"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.reduce" title="Permalink to this definition">¶</a></dt>
<dd><p>Reduces the tensor data across all machines.</p>
<p>Only the process with rank <code class="xref py py-attr docutils literal"><span class="pre">dst</span></code> is going to receive the final result.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Input and output of the collective. The function
operates in-place.</li>
<li><strong>dst</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – Destination rank</li>
<li><strong>op</strong> (<em>optional</em>) – One of the values from <code class="docutils literal"><span class="pre">torch.distributed.deprecated.reduce_op</span></code>
enum. Specifies an operation used for element-wise reductions.</li>
<li><strong>group</strong> (<em>optional</em>) – Group of the collective.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.all_gather">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">all_gather</code><span class="sig-paren">(</span><em>tensor_list</em>, <em>tensor</em>, <em>group=<object object></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#all_gather"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.all_gather" title="Permalink to this definition">¶</a></dt>
<dd><p>Gathers tensors from the whole group in a list.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor_list</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.7)"><em>list</em></a><em>[</em><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a><em>]</em>) – Output list. It should contain
correctly-sized tensors to be used for output of the collective.</li>
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Tensor to be broadcast from current process.</li>
<li><strong>group</strong> (<em>optional</em>) – Group of the collective.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.gather">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">gather</code><span class="sig-paren">(</span><em>tensor</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#gather"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.gather" title="Permalink to this definition">¶</a></dt>
<dd><p>Gathers a list of tensors in a single process.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Input tensor.</li>
<li><strong>dst</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – Destination rank. Required in all processes except the one that
is receiveing the data.</li>
<li><strong>gather_list</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.7)"><em>list</em></a><em>[</em><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a><em>]</em>) – List of appropriately-sized tensors to
use for received data. Required only in the receiving process.</li>
<li><strong>group</strong> (<em>optional</em>) – Group of the collective.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.scatter">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">scatter</code><span class="sig-paren">(</span><em>tensor</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#scatter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.scatter" title="Permalink to this definition">¶</a></dt>
<dd><p>Scatters a list of tensors to all processes in a group.</p>
<p>Each process will receive exactly one tensor and store its data in the
<code class="xref py py-attr docutils literal"><span class="pre">tensor</span></code> argument.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor</strong> (<a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a>) – Output tensor.</li>
<li><strong>src</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – Source rank. Required in all processes except the one that
is sending the data.</li>
<li><strong>scatter_list</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.7)"><em>list</em></a><em>[</em><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a><em>]</em>) – List of tensors to scatter. Required only
in the process that is sending the data.</li>
<li><strong>group</strong> (<em>optional</em>) – Group of the collective.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.barrier">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">barrier</code><span class="sig-paren">(</span><em>group=<object object></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#barrier"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.barrier" title="Permalink to this definition">¶</a></dt>
<dd><p>Synchronizes all processes.</p>
<p>This collective blocks processes until the whole group enters this function.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>group</strong> (<em>optional</em>) – Group of the collective.</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
<div class="section" id="multi-gpu-collective-functions">
<h2>Multi-GPU collective functions<a class="headerlink" href="#multi-gpu-collective-functions" title="Permalink to this headline">¶</a></h2>
<p>If you have more than one GPU on each node, when using the NCCL backend,
<a class="reference internal" href="#torch.distributed.deprecated.broadcast_multigpu" title="torch.distributed.deprecated.broadcast_multigpu"><code class="xref py py-func docutils literal"><span class="pre">broadcast_multigpu()</span></code></a>
<a class="reference internal" href="#torch.distributed.deprecated.all_reduce_multigpu" title="torch.distributed.deprecated.all_reduce_multigpu"><code class="xref py py-func docutils literal"><span class="pre">all_reduce_multigpu()</span></code></a>
<a class="reference internal" href="#torch.distributed.deprecated.reduce_multigpu" title="torch.distributed.deprecated.reduce_multigpu"><code class="xref py py-func docutils literal"><span class="pre">reduce_multigpu()</span></code></a> and
<a class="reference internal" href="#torch.distributed.deprecated.all_gather_multigpu" title="torch.distributed.deprecated.all_gather_multigpu"><code class="xref py py-func docutils literal"><span class="pre">all_gather_multigpu()</span></code></a> support distributed collective
operations among multiple GPUs within each node. These functions can potentially
improve the overall distributed training performance and be easily used by
passing a list of tensors. Each Tensor in the passed tensor list needs
to be on a separate GPU device of the host where the function is called. Note
that the length of the tensor list needs to be identical among all the
distributed processes. Also note that currently the multi-GPU collective
functions are only supported by the NCCL backend.</p>
<p>For example, if the system we use for distributed training has 2 nodes, each
of which has 8 GPUs. On each of the 16 GPUs, there is a tensor that we would
like to all-reduce. The following code can serve as a reference:</p>
<p>Code running on Node 0</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.distributed.deprecated</span> <span class="k">as</span> <span class="nn">dist</span>
<span class="n">dist</span><span class="o">.</span><span class="n">init_process_group</span><span class="p">(</span><span class="n">backend</span><span class="o">=</span><span class="s2">"nccl"</span><span class="p">,</span>
<span class="n">init_method</span><span class="o">=</span><span class="s2">"file:///distributed_test"</span><span class="p">,</span>
<span class="n">world_size</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
<span class="n">rank</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">tensor_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">dev_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">cuda</span><span class="o">.</span><span class="n">device_count</span><span class="p">()):</span>
<span class="n">tensor_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">FloatTensor</span><span class="p">([</span><span class="mi">1</span><span class="p">])</span><span class="o">.</span><span class="n">cuda</span><span class="p">(</span><span class="n">dev_idx</span><span class="p">))</span>
<span class="n">dist</span><span class="o">.</span><span class="n">all_reduce_multigpu</span><span class="p">(</span><span class="n">tensor_list</span><span class="p">)</span>
</pre></div>
</div>
<p>Code running on Node 1</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.distributed.deprecated</span> <span class="k">as</span> <span class="nn">dist</span>
<span class="n">dist</span><span class="o">.</span><span class="n">init_process_group</span><span class="p">(</span><span class="n">backend</span><span class="o">=</span><span class="s2">"nccl"</span><span class="p">,</span>
<span class="n">init_method</span><span class="o">=</span><span class="s2">"file:///distributed_test"</span><span class="p">,</span>
<span class="n">world_size</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
<span class="n">rank</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">tensor_list</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">dev_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">cuda</span><span class="o">.</span><span class="n">device_count</span><span class="p">()):</span>
<span class="n">tensor_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">FloatTensor</span><span class="p">([</span><span class="mi">1</span><span class="p">])</span><span class="o">.</span><span class="n">cuda</span><span class="p">(</span><span class="n">dev_idx</span><span class="p">))</span>
<span class="n">dist</span><span class="o">.</span><span class="n">all_reduce_multigpu</span><span class="p">(</span><span class="n">tensor_list</span><span class="p">)</span>
</pre></div>
</div>
<p>After the call, all 16 tensors on the two nodes will have the all-reduced value
of 16</p>
<dl class="function">
<dt id="torch.distributed.deprecated.broadcast_multigpu">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">broadcast_multigpu</code><span class="sig-paren">(</span><em>tensor_list</em>, <em>src</em>, <em>group=<object object></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#broadcast_multigpu"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.broadcast_multigpu" title="Permalink to this definition">¶</a></dt>
<dd><p>Broadcasts the tensor to the whole group with multiple GPU tensors
per node.</p>
<p><code class="xref py py-attr docutils literal"><span class="pre">tensor</span></code> must have the same number of elements in all the GPUs from
all processes participating in the collective. each tensor in the list must
be on a different GPU.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Only NCCL backend is currently supported. <code class="xref py py-attr docutils literal"><span class="pre">tensor_list</span></code> should only
contain GPU tensors.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor_list</strong> (<em>List</em><em>[</em><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a><em>]</em>) – Tensors that participate in the collective
operation. if <code class="docutils literal"><span class="pre">src</span></code> is the rank, then the first element of
<code class="docutils literal"><span class="pre">tensor_list</span></code> (<code class="docutils literal"><span class="pre">tensor_list[0]</span></code>) will be broadcasted to all
other tensors (on different GPUs) in the src process and all tensors
in <code class="docutils literal"><span class="pre">tensor_list</span></code> of other non-src processes. You also need to make
sure that <code class="docutils literal"><span class="pre">len(tensor_list)</span></code> is the same for all the distributed
processes calling this function.</li>
<li><strong>src</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – Source rank.</li>
<li><strong>group</strong> (<em>optional</em>) – Group of the collective.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.all_reduce_multigpu">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">all_reduce_multigpu</code><span class="sig-paren">(</span><em>tensor_list</em>, <em>op=<object object></em>, <em>group=<object object></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#all_reduce_multigpu"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.all_reduce_multigpu" title="Permalink to this definition">¶</a></dt>
<dd><p>Reduces the tensor data across all machines in such a way that all get
the final result. This function reduces a number of tensors on every node,
while each tensor resides on a different GPU.
Therefore, the input tensor in the tensor list needs to be GPU tensors.
Also, each tensor in the tensor list needs to reside on a different GPU.</p>
<p>After the call, all tensors in <code class="xref py py-attr docutils literal"><span class="pre">tensor_list</span></code> will be bitwise identical
in all processes.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Only NCCL backend is currently supported. <code class="xref py py-attr docutils literal"><span class="pre">tensor_list</span></code> should only
contain GPU tensors.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor_list</strong> (<em>List</em><em>[</em><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a><em>]</em>) – List of input and output tensors of
the collective. The function operates in-place and requires that
each tensor to be a GPU tensor on different GPUs.
You also need to make sure that <code class="docutils literal"><span class="pre">len(tensor_list)</span></code> is the same for
all the distributed processes calling this function.</li>
<li><strong>op</strong> (<em>optional</em>) – One of the values from <code class="docutils literal"><span class="pre">torch.distributed.deprecated.reduce_op</span></code>
enum. Specifies an operation used for element-wise reductions.</li>
<li><strong>group</strong> (<em>optional</em>) – Group of the collective.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.reduce_multigpu">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">reduce_multigpu</code><span class="sig-paren">(</span><em>tensor_list</em>, <em>dst</em>, <em>op=<object object></em>, <em>group=<object object></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#reduce_multigpu"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.reduce_multigpu" title="Permalink to this definition">¶</a></dt>
<dd><p>Reduces the tensor data on multiple GPUs across all machines. Each tensor
in :attr`tensor_list` should reside on a separate GPU.</p>
<p>Only the GPU of <code class="docutils literal"><span class="pre">tensor_list[0]</span></code> on the process with rank <code class="xref py py-attr docutils literal"><span class="pre">dst</span></code> is
going to receive the final result.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Only NCCL backend is currently supported. <code class="xref py py-attr docutils literal"><span class="pre">tensor_list</span></code> should only
contain GPU tensors.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>tensor_list</strong> (<em>List</em><em>[</em><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a><em>]</em>) – Input and output GPU tensors of the
collective. The function operates in-place.
You also need to make sure that <code class="docutils literal"><span class="pre">len(tensor_list)</span></code> is the same for
all the distributed processes calling this function.</li>
<li><strong>dst</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.7)"><em>int</em></a>) – Destination rank</li>
<li><strong>op</strong> (<em>optional</em>) – One of the values from <code class="docutils literal"><span class="pre">torch.distributed.deprecated.reduce_op</span></code>
enum. Specifies an operation used for element-wise reductions.</li>
<li><strong>group</strong> (<em>optional</em>) – Group of the collective.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="torch.distributed.deprecated.all_gather_multigpu">
<code class="descclassname">torch.distributed.deprecated.</code><code class="descname">all_gather_multigpu</code><span class="sig-paren">(</span><em>output_tensor_lists</em>, <em>input_tensor_list</em>, <em>group=<object object></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/torch/distributed/deprecated.html#all_gather_multigpu"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#torch.distributed.deprecated.all_gather_multigpu" title="Permalink to this definition">¶</a></dt>
<dd><p>Gathers tensors from the whole group in a list.
Each tensor in <code class="xref py py-attr docutils literal"><span class="pre">input_tensor_list</span></code> should reside on a separate GPU.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Only NCCL backend is currently supported. <code class="xref py py-attr docutils literal"><span class="pre">output_tensor_lists</span></code> and
<code class="xref py py-attr docutils literal"><span class="pre">input_tensor_list</span></code> should only contain GPU tensors.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>output_tensor_lists</strong> (<em>List</em><em>[</em><em>List</em><em>[</em><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a><em>]</em><em>]</em>) – Output lists. It should
contain correctly-sized tensors on each GPU to be used for output of
the collective.
e.g. <code class="docutils literal"><span class="pre">output_tensor_lists[i]</span></code> contains the all_gather
result that resides on the GPU of <code class="docutils literal"><span class="pre">input_tensor_list[i]</span></code>.
Note that each element of <code class="docutils literal"><span class="pre">output_tensor_lists[i]</span></code> has the size of
<code class="docutils literal"><span class="pre">world_size</span> <span class="pre">*</span> <span class="pre">len(input_tensor_list)</span></code>, since the function all
gathers the result from every single GPU in the group. To interpret
each element of <code class="docutils literal"><span class="pre">output_tensor_list[i]</span></code>, note that
<code class="docutils literal"><span class="pre">input_tensor_list[j]</span></code> of rank k will be appear in
<code class="docutils literal"><span class="pre">output_tensor_list[i][rank</span> <span class="pre">*</span> <span class="pre">world_size</span> <span class="pre">+</span> <span class="pre">j]</span></code>
Also note that <code class="docutils literal"><span class="pre">len(output_tensor_lists)</span></code>, and the size of each
element in <code class="docutils literal"><span class="pre">output_tensor_lists</span></code> (each element is a list,
therefore <code class="docutils literal"><span class="pre">len(output_tensor_lists[i])</span></code>) need to be the same
for all the distributed processes calling this function.</li>
<li><strong>input_tensor_list</strong> (<em>List</em><em>[</em><a class="reference internal" href="tensors.html#torch.Tensor" title="torch.Tensor"><em>Tensor</em></a><em>]</em>) – List of tensors (on different GPUs) to
be broadcast from current process.
Note that <code class="docutils literal"><span class="pre">len(input_tensor_list)</span></code> needs to be the same for