-
-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathdistributed.html
1500 lines (1257 loc) · 120 KB
/
distributed.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="author" content="PyTorch-Ignite Contributors">
<meta name="creator" content="PyTorch-Ignite Contributors">
<meta name="publisher" content="PyTorch-Ignite Contributors">
<meta name="generator" content="Sphinx 5.3.0">
<meta name="description" content="High-level library to help with training and evaluating neural networks in PyTorch flexibly and transparently.">
<meta name="keywords" content="pytorch, pytorch ignite, ignite, engine, events, handlers, metrics">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#ee4c2c">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@pytorch_ignite">
<meta name="twitter:creator" content="@pytorch_ignite">
<meta name="twitter:description" content="High-level library to help with training and evaluating neural networks in PyTorch flexibly and transparently.">
<meta name="twitter:title" content="ignite.distributed — PyTorch-Ignite v0.5.1 Documentation">
<meta name="twitter:image" content="https://raw.githubusercontent.com/pytorch/ignite/master/assets/logo/ignite_logo.png">
<meta name="twitter:image:alt" content="PyTorch-Ignite logo">
<meta property="og:title" content="ignite.distributed — PyTorch-Ignite v0.5.1 Documentation">
<meta property="og:type" content="website">
<meta property="og:url" content="https://pytorch.org/ignite/">
<meta property="og:site_name" content="PyTorch-Ignite">
<meta property="og:image" content="https://raw.githubusercontent.com/pytorch/ignite/master/assets/logo/ignite_logo.png">
<meta property="og:image:alt" content="PyTorch-Ignite logo">
<meta property="og:description" content="High-level library to help with training and evaluating neural networks in PyTorch flexibly and transparently.">
<link rel="preconnect" href="https://BH4D9OD16A-dsn.algolia.net" crossorigin />
<title>ignite.distributed — PyTorch-Ignite v0.5.1 Documentation</title>
<link rel="shortcut icon" type="image/svg+xml" href="_static/ignite_logomark.svg"/>
<link rel="canonical" href="https://pytorch.org/ignite/distributed.html"/>
<link rel="preload" href="_static/css/theme.css" as="style" />
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<!-- <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> -->
<link rel="preload" href="_static/pygments.css" as="style" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="preload" href="_static/css/theme.css" as="style" />
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="preload" href="_static/copybutton.css" as="style" />
<link rel="stylesheet" href="_static/copybutton.css" type="text/css" />
<link rel="preload" href="_static/togglebutton.css" as="style" />
<link rel="stylesheet" href="_static/togglebutton.css" type="text/css" />
<link rel="preload" href="_static/banner.css" as="style" />
<link rel="stylesheet" href="_static/banner.css" type="text/css" />
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" as="style" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="preload" href="_static/katex-math.css" as="style" />
<link rel="stylesheet" href="_static/katex-math.css" type="text/css" />
<link rel="preload" href="_static/sphinx-design.5ea377869091fd0449014c60fc090103.min.css" as="style" />
<link rel="stylesheet" href="_static/sphinx-design.5ea377869091fd0449014c60fc090103.min.css" type="text/css" />
<link rel="preload" href="_static/css/ignite_theme.css" as="style" />
<link rel="stylesheet" href="_static/css/ignite_theme.css" type="text/css" />
<link rel="preload" href="https://cdn.jsdelivr.net/npm/@docsearch/css@3" as="style" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@docsearch/css@3" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="DistributedProxySampler" href="generated/ignite.distributed.auto.DistributedProxySampler.html" />
<link rel="prev" title="WaveHedgesDistance" href="generated/ignite.metrics.regression.WaveHedgesDistance.html" />
<!-- katex links / this needs to be loaded before fonts.html -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" crossorigin="anonymous">
<script async src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" crossorigin="anonymous"></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">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-Regular.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">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-5FELLLFHCP"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-5FELLLFHCP');
</script>
</head>
<div class="container-fluid header-holder tutorials-header" id="header-holder">
<div class="container">
<a class="header-logo" href="/ignite" aria-label="PyTorch-Ignite"></a>
<div class="header-container">
<div class="main-menu">
<ul>
<li>
<a href="https://pytorch-ignite.ai/tutorials/beginner/01-getting-started/">Quickstart</a>
</li>
<li>
<a href="https://pytorch-ignite.ai/concepts/">Concepts</a>
</li>
<!-- <li>
<a href="https://pytorch-ignite.ai/tutorials/beginner/01-getting-started/#complete-code">Examples</a>
</li> -->
<li>
<a href="https://pytorch-ignite.ai/how-to-guides/">FAQ</a>
</li>
<li>
<a href="https://github.com/pytorch/ignite" target="_blank" rel="noopener noreferrer">GitHub</a>
</li>
<li>
<a href="https://pytorch-ignite.ai/about/community/">About us</a>
</li>
<li>
<a href="https://pytorch-ignite.ai">⊳ pytorch-ignite.ai</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>
<div class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></div>
</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">
v0.5.1
</div>
<div id="docsearch"></div>
</div>
<p class="caption" role="heading"><span class="caption-text">Package Reference</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="engine.html">ignite.engine</a></li>
<li class="toctree-l1"><a class="reference internal" href="handlers.html">ignite.handlers</a></li>
<li class="toctree-l1"><a class="reference internal" href="metrics.html">ignite.metrics</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">ignite.distributed</a></li>
<li class="toctree-l1"><a class="reference internal" href="exceptions.html">ignite.exceptions</a></li>
<li class="toctree-l1"><a class="reference internal" href="utils.html">ignite.utils</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Contrib Package Reference</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="contrib/engines.html">ignite.contrib.engines</a></li>
<li class="toctree-l1"><a class="reference internal" href="contrib/metrics.html">ignite.contrib.metrics</a></li>
<li class="toctree-l1"><a class="reference internal" href="contrib/handlers.html">ignite.contrib.handlers</a></li>
</ul>
</div>
</div>
<div class="rst-versions" data-toggle="rst-versions" role="note" aria-label="versions">
<span class="rst-current-version" data-toggle="rst-current-version">
<span class="fa fa-book"> Other Versions</span>
v: v0.5.1
<span class="fa fa-caret-down"></span>
</span>
<div class="rst-other-versions">
<dl>
<dt>Tags</dt>
<dd><a href="v0.3.0/index.html">v0.3.0</a></dd>
<dd><a href="v0.4.0.post1/distributed.html">v0.4.0.post1</a></dd>
<dd><a href="v0.4.1/distributed.html">v0.4.1</a></dd>
<dd><a href="v0.4.10/distributed.html">v0.4.10</a></dd>
<dd><a href="v0.4.11/distributed.html">v0.4.11</a></dd>
<dd><a href="v0.4.12/distributed.html">v0.4.12</a></dd>
<dd><a href="v0.4.13/distributed.html">v0.4.13</a></dd>
<dd><a href="v0.4.2/distributed.html">v0.4.2</a></dd>
<dd><a href="v0.4.3/distributed.html">v0.4.3</a></dd>
<dd><a href="v0.4.4.post1/distributed.html">v0.4.4.post1</a></dd>
<dd><a href="v0.4.5/distributed.html">v0.4.5</a></dd>
<dd><a href="v0.4.6/distributed.html">v0.4.6</a></dd>
<dd><a href="v0.4.7/distributed.html">v0.4.7</a></dd>
<dd><a href="v0.4.8/distributed.html">v0.4.8</a></dd>
<dd><a href="v0.4.9/distributed.html">v0.4.9</a></dd>
<dd><a href="v0.4rc.0.post1/distributed.html">v0.4rc.0.post1</a></dd>
<dd><a href="v0.5.0.post2/distributed.html">v0.5.0.post2</a></dd>
<dd><a href="v0.5.1/distributed.html">v0.5.1</a></dd>
</dl>
<dl>
<dt>Branches</dt>
<dd><a href="master/distributed.html">master</a></dd>
</dl>
</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>ignite.distributed</li>
<li class="pytorch-breadcrumbs-aside">
<a href="_sources/distributed.rst.txt" rel="nofollow"><img src="_static/images/view-page-source-icon.svg"></a>
</li>
</ul>
</div>
</div>
<div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
Shortcuts
</div>
</div>
<section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
<div class="pytorch-content-left">
<div class="rst-content">
<div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
<article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
<section id="ignite-distributed">
<h1>ignite.distributed<a class="headerlink" href="#ignite-distributed" title="Permalink to this heading">#</a></h1>
<p>Helper module to use distributed settings for multiple backends:</p>
<ul class="simple">
<li><p>backends from native torch distributed configuration: “nccl”, “gloo”, “mpi”</p></li>
<li><p>XLA on TPUs via <a class="reference external" href="https://github.com/pytorch/xla">pytorch/xla</a></p></li>
<li><p>using <a class="reference external" href="https://horovod.readthedocs.io/en/stable/">Horovod framework</a> as a backend</p></li>
</ul>
<section id="distributed-launcher-and-auto-helpers">
<h2>Distributed launcher and <cite>auto</cite> helpers<a class="headerlink" href="#distributed-launcher-and-auto-helpers" title="Permalink to this heading">#</a></h2>
<p>We provide a context manager to simplify the code of distributed configuration setup for all above supported backends.
In addition, methods like <a class="reference internal" href="generated/ignite.distributed.auto.auto_model.html#ignite.distributed.auto.auto_model" title="ignite.distributed.auto.auto_model"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auto_model()</span></code></a>, <a class="reference internal" href="generated/ignite.distributed.auto.auto_optim.html#ignite.distributed.auto.auto_optim" title="ignite.distributed.auto.auto_optim"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auto_optim()</span></code></a> and
<a class="reference internal" href="generated/ignite.distributed.auto.auto_dataloader.html#ignite.distributed.auto.auto_dataloader" title="ignite.distributed.auto.auto_dataloader"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auto_dataloader()</span></code></a> helps to adapt in a transparent way provided model, optimizer and data
loaders to existing configuration:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># main.py</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">ignite.distributed</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">idist</span>
<span class="k">def</span><span class="w"> </span><span class="nf">training</span><span class="p">(</span><span class="n">local_rank</span><span class="p">,</span> <span class="n">config</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">idist</span><span class="o">.</span><span class="n">get_rank</span><span class="p">(),</span> <span class="s2">": run with config:"</span><span class="p">,</span> <span class="n">config</span><span class="p">,</span> <span class="s2">"- backend="</span><span class="p">,</span> <span class="n">idist</span><span class="o">.</span><span class="n">backend</span><span class="p">())</span>
<span class="n">train_loader</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">auto_dataloader</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">num_workers</span><span class="o">=</span><span class="mi">12</span><span class="p">,</span> <span class="n">shuffle</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="c1"># batch size, num_workers and sampler are automatically adapted to existing configuration</span>
<span class="c1"># ...</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">resnet50</span><span class="p">()</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">auto_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
<span class="c1"># model is DDP or DP or just itself according to existing configuration</span>
<span class="c1"># ...</span>
<span class="n">optimizer</span> <span class="o">=</span> <span class="n">optim</span><span class="o">.</span><span class="n">SGD</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">parameters</span><span class="p">(),</span> <span class="n">lr</span><span class="o">=</span><span class="mf">0.01</span><span class="p">)</span>
<span class="n">optimizer</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">auto_optim</span><span class="p">(</span><span class="n">optimizer</span><span class="p">)</span>
<span class="c1"># optimizer is itself, except XLA configuration and overrides `step()` method.</span>
<span class="c1"># User can safely call `optimizer.step()` (behind `xm.optimizer_step(optimizier)` is performed)</span>
<span class="n">backend</span> <span class="o">=</span> <span class="s2">"nccl"</span> <span class="c1"># torch native distributed configuration on multiple GPUs</span>
<span class="c1"># backend = "xla-tpu" # XLA TPUs distributed configuration</span>
<span class="c1"># backend = None # no distributed configuration</span>
<span class="c1">#</span>
<span class="c1"># dist_configs = {'nproc_per_node': 4} # Use specified distributed configuration if launch as python main.py</span>
<span class="c1"># dist_configs["start_method"] = "fork" # Add start_method as "fork" if using Jupyter Notebook</span>
<span class="k">with</span> <span class="n">idist</span><span class="o">.</span><span class="n">Parallel</span><span class="p">(</span><span class="n">backend</span><span class="o">=</span><span class="n">backend</span><span class="p">,</span> <span class="o">**</span><span class="n">dist_configs</span><span class="p">)</span> <span class="k">as</span> <span class="n">parallel</span><span class="p">:</span>
<span class="n">parallel</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">training</span><span class="p">,</span> <span class="n">config</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>Above code may be executed with <a class="reference external" href="https://pytorch.org/docs/stable/distributed.html#launch-utility">torch.distributed.launch</a> tool or by python and specifying distributed configuration
in the code. For more details, please, see <a class="reference internal" href="generated/ignite.distributed.launcher.Parallel.html#ignite.distributed.launcher.Parallel" title="ignite.distributed.launcher.Parallel"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parallel</span></code></a>,
<a class="reference internal" href="generated/ignite.distributed.auto.auto_model.html#ignite.distributed.auto.auto_model" title="ignite.distributed.auto.auto_model"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auto_model()</span></code></a>, <a class="reference internal" href="generated/ignite.distributed.auto.auto_optim.html#ignite.distributed.auto.auto_optim" title="ignite.distributed.auto.auto_optim"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auto_optim()</span></code></a> and
<a class="reference internal" href="generated/ignite.distributed.auto.auto_dataloader.html#ignite.distributed.auto.auto_dataloader" title="ignite.distributed.auto.auto_dataloader"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auto_dataloader()</span></code></a>.</p>
<p>Complete example of CIFAR10 training can be found
<a class="reference external" href="https://github.com/pytorch/ignite/tree/master/examples/cifar10">here</a>.</p>
</section>
<section id="ignite-distributed-auto">
<h2>ignite.distributed.auto<a class="headerlink" href="#ignite-distributed-auto" title="Permalink to this heading">#</a></h2>
<table class="autosummary longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/ignite.distributed.auto.DistributedProxySampler.html#ignite.distributed.auto.DistributedProxySampler" title="ignite.distributed.auto.DistributedProxySampler"><code class="xref py py-obj docutils literal notranslate"><span class="pre">DistributedProxySampler</span></code></a></p></td>
<td><p>Distributed sampler proxy to adapt user's sampler for distributed data parallelism configuration.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/ignite.distributed.auto.auto_dataloader.html#ignite.distributed.auto.auto_dataloader" title="ignite.distributed.auto.auto_dataloader"><code class="xref py py-obj docutils literal notranslate"><span class="pre">auto_dataloader</span></code></a></p></td>
<td><p>Helper method to create a dataloader adapted for non-distributed and distributed configurations (supporting all available backends from <a class="reference internal" href="#ignite.distributed.utils.available_backends" title="ignite.distributed.utils.available_backends"><code class="xref py py-meth docutils literal notranslate"><span class="pre">available_backends()</span></code></a>).</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/ignite.distributed.auto.auto_model.html#ignite.distributed.auto.auto_model" title="ignite.distributed.auto.auto_model"><code class="xref py py-obj docutils literal notranslate"><span class="pre">auto_model</span></code></a></p></td>
<td><p>Helper method to adapt provided model for non-distributed and distributed configurations (supporting all available backends from <a class="reference internal" href="#ignite.distributed.utils.available_backends" title="ignite.distributed.utils.available_backends"><code class="xref py py-meth docutils literal notranslate"><span class="pre">available_backends()</span></code></a>).</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/ignite.distributed.auto.auto_optim.html#ignite.distributed.auto.auto_optim" title="ignite.distributed.auto.auto_optim"><code class="xref py py-obj docutils literal notranslate"><span class="pre">auto_optim</span></code></a></p></td>
<td><p>Helper method to adapt optimizer for non-distributed and distributed configurations (supporting all available backends from <a class="reference internal" href="#ignite.distributed.utils.available_backends" title="ignite.distributed.utils.available_backends"><code class="xref py py-meth docutils literal notranslate"><span class="pre">available_backends()</span></code></a>).</p></td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>In distributed configuration, methods <a class="reference internal" href="generated/ignite.distributed.auto.auto_model.html#ignite.distributed.auto.auto_model" title="ignite.distributed.auto.auto_model"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auto_model()</span></code></a>, <a class="reference internal" href="generated/ignite.distributed.auto.auto_optim.html#ignite.distributed.auto.auto_optim" title="ignite.distributed.auto.auto_optim"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auto_optim()</span></code></a>
and <a class="reference internal" href="generated/ignite.distributed.auto.auto_dataloader.html#ignite.distributed.auto.auto_dataloader" title="ignite.distributed.auto.auto_dataloader"><code class="xref py py-meth docutils literal notranslate"><span class="pre">auto_dataloader()</span></code></a> will have effect only when distributed group is initialized.</p>
</div>
</section>
<section id="ignite-distributed-launcher">
<h2>ignite.distributed.launcher<a class="headerlink" href="#ignite-distributed-launcher" title="Permalink to this heading">#</a></h2>
<table class="autosummary longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/ignite.distributed.launcher.Parallel.html#ignite.distributed.launcher.Parallel" title="ignite.distributed.launcher.Parallel"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Parallel</span></code></a></p></td>
<td><p>Distributed launcher context manager to simplify distributed configuration setup for multiple backends:</p></td>
</tr>
</tbody>
</table>
</section>
<section id="ignite-distributed-utils">
<h2>ignite.distributed.utils<a class="headerlink" href="#ignite-distributed-utils" title="Permalink to this heading">#</a></h2>
<p>This module wraps common methods to fetch information about distributed configuration, initialize/finalize process
group or spawn multiple processes.</p>
<table class="autosummary longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.backend" title="ignite.distributed.utils.backend"><code class="xref py py-obj docutils literal notranslate"><span class="pre">backend</span></code></a></p></td>
<td><p>Returns computation model's backend.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.broadcast" title="ignite.distributed.utils.broadcast"><code class="xref py py-obj docutils literal notranslate"><span class="pre">broadcast</span></code></a></p></td>
<td><p>Helper method to perform broadcast operation.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.device" title="ignite.distributed.utils.device"><code class="xref py py-obj docutils literal notranslate"><span class="pre">device</span></code></a></p></td>
<td><p>Returns current device according to current distributed configuration.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.available_backends" title="ignite.distributed.utils.available_backends"><code class="xref py py-obj docutils literal notranslate"><span class="pre">available_backends</span></code></a></p></td>
<td><p>Returns available backends.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.model_name" title="ignite.distributed.utils.model_name"><code class="xref py py-obj docutils literal notranslate"><span class="pre">model_name</span></code></a></p></td>
<td><p>Returns distributed configuration name (given by ignite)</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.get_world_size" title="ignite.distributed.utils.get_world_size"><code class="xref py py-obj docutils literal notranslate"><span class="pre">get_world_size</span></code></a></p></td>
<td><p>Returns world size of current distributed configuration.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.get_rank" title="ignite.distributed.utils.get_rank"><code class="xref py py-obj docutils literal notranslate"><span class="pre">get_rank</span></code></a></p></td>
<td><p>Returns process rank within current distributed configuration.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.get_local_rank" title="ignite.distributed.utils.get_local_rank"><code class="xref py py-obj docutils literal notranslate"><span class="pre">get_local_rank</span></code></a></p></td>
<td><p>Returns local process rank within current distributed configuration.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.get_nproc_per_node" title="ignite.distributed.utils.get_nproc_per_node"><code class="xref py py-obj docutils literal notranslate"><span class="pre">get_nproc_per_node</span></code></a></p></td>
<td><p>Returns number of processes (or tasks) per node within current distributed configuration.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.get_node_rank" title="ignite.distributed.utils.get_node_rank"><code class="xref py py-obj docutils literal notranslate"><span class="pre">get_node_rank</span></code></a></p></td>
<td><p>Returns node rank within current distributed configuration.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.get_nnodes" title="ignite.distributed.utils.get_nnodes"><code class="xref py py-obj docutils literal notranslate"><span class="pre">get_nnodes</span></code></a></p></td>
<td><p>Returns number of nodes within current distributed configuration.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.spawn" title="ignite.distributed.utils.spawn"><code class="xref py py-obj docutils literal notranslate"><span class="pre">spawn</span></code></a></p></td>
<td><p>Spawns <code class="docutils literal notranslate"><span class="pre">nproc_per_node</span></code> processes that run <code class="docutils literal notranslate"><span class="pre">fn</span></code> with <code class="docutils literal notranslate"><span class="pre">args</span></code>/<code class="docutils literal notranslate"><span class="pre">kwargs_dict</span></code> and initialize distributed configuration defined by <code class="docutils literal notranslate"><span class="pre">backend</span></code>.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.initialize" title="ignite.distributed.utils.initialize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">initialize</span></code></a></p></td>
<td><p>Initializes distributed configuration according to provided <code class="docutils literal notranslate"><span class="pre">backend</span></code></p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.finalize" title="ignite.distributed.utils.finalize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">finalize</span></code></a></p></td>
<td><p>Finalizes distributed configuration.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.show_config" title="ignite.distributed.utils.show_config"><code class="xref py py-obj docutils literal notranslate"><span class="pre">show_config</span></code></a></p></td>
<td><p>Helper method to display distributed configuration via <code class="docutils literal notranslate"><span class="pre">logging</span></code>.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.set_local_rank" title="ignite.distributed.utils.set_local_rank"><code class="xref py py-obj docutils literal notranslate"><span class="pre">set_local_rank</span></code></a></p></td>
<td><p>Method to hint the local rank in case if torch native distributed context is created by user without using <a class="reference internal" href="#ignite.distributed.utils.initialize" title="ignite.distributed.utils.initialize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">initialize()</span></code></a> or <a class="reference internal" href="#ignite.distributed.utils.spawn" title="ignite.distributed.utils.spawn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">spawn()</span></code></a>.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.all_reduce" title="ignite.distributed.utils.all_reduce"><code class="xref py py-obj docutils literal notranslate"><span class="pre">all_reduce</span></code></a></p></td>
<td><p>Helper method to perform all reduce operation.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.all_gather" title="ignite.distributed.utils.all_gather"><code class="xref py py-obj docutils literal notranslate"><span class="pre">all_gather</span></code></a></p></td>
<td><p>Helper method to perform all gather operation.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.barrier" title="ignite.distributed.utils.barrier"><code class="xref py py-obj docutils literal notranslate"><span class="pre">barrier</span></code></a></p></td>
<td><p>Helper method to synchronize all processes.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.hostname" title="ignite.distributed.utils.hostname"><code class="xref py py-obj docutils literal notranslate"><span class="pre">hostname</span></code></a></p></td>
<td><p>Returns host name for current process within current distributed configuration.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.sync" title="ignite.distributed.utils.sync"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sync</span></code></a></p></td>
<td><p>Helper method to force this module to synchronize with current distributed context.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.one_rank_only" title="ignite.distributed.utils.one_rank_only"><code class="xref py py-obj docutils literal notranslate"><span class="pre">one_rank_only</span></code></a></p></td>
<td><p>Decorator to filter handlers wrt a rank number</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#ignite.distributed.utils.new_group" title="ignite.distributed.utils.new_group"><code class="xref py py-obj docutils literal notranslate"><span class="pre">new_group</span></code></a></p></td>
<td><p>Helper method to make group for each backend from ranks.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#ignite.distributed.utils.one_rank_first" title="ignite.distributed.utils.one_rank_first"><code class="xref py py-obj docutils literal notranslate"><span class="pre">one_rank_first</span></code></a></p></td>
<td><p>Context manager that ensures a specific rank runs first before others in a distributed environment.</p></td>
</tr>
</tbody>
</table>
<span class="target" id="module-ignite.distributed.utils"></span><dl class="py attribute">
<dt class="sig sig-object py" id="ignite.distributed.utils.has_native_dist_support">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">has_native_dist_support</span></span><a class="headerlink" href="#ignite.distributed.utils.has_native_dist_support" title="Permalink to this definition">#</a></dt>
<dd><p>True if <cite>torch.distributed</cite> is available</p>
</dd></dl>
<dl class="py attribute">
<dt class="sig sig-object py" id="ignite.distributed.utils.has_xla_support">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">has_xla_support</span></span><a class="headerlink" href="#ignite.distributed.utils.has_xla_support" title="Permalink to this definition">#</a></dt>
<dd><p>True if <cite>torch_xla</cite> package is found</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.all_gather">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">all_gather</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">group</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/ignite/distributed/utils.html#all_gather"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.all_gather" title="Permalink to this definition">#</a></dt>
<dd><p>Helper method to perform all gather operation.</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 external" href="https://docs.python.org/3/library/typing.html#typing.Union" title="(in Python v3.13)"><em>Union</em></a><em>[</em><a class="reference external" href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor" title="(in PyTorch v2.6)"><em>Tensor</em></a><em>, </em><a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.13)"><em>float</em></a><em>, </em><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><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.13)"><em>Any</em></a><em>]</em>) – tensor or number or str to collect across participating processes. If tensor, it should have the
same shape across processes.</p></li>
<li><p><strong>group</strong> (<a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.13)"><em>Optional</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Union" title="(in Python v3.13)"><em>Union</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.13)"><em>Any</em></a><em>, </em><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.13)"><em>List</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><em>int</em></a><em>]</em><em>]</em><em>]</em>) – list of integer or the process group for each backend. If None, the default process group will be used.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>If input is a tensor, returns a torch.Tensor of shape <code class="docutils literal notranslate"><span class="pre">(world_size</span> <span class="pre">*</span> <span class="pre">tensor.shape[0],</span> <span class="pre">tensor.shape[1],</span> <span class="pre">...)</span></code>.
If input is a number, a torch.Tensor of shape <code class="docutils literal notranslate"><span class="pre">(world_size,</span> <span class="pre">)</span></code> is returned and finally a list of strings
is returned if input is a string. If current process does not belong to <cite>group</cite>, the very <code class="docutils literal notranslate"><span class="pre">tensor</span></code> is
returned.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Union" title="(in Python v3.13)"><em>Union</em></a>[<a class="reference external" href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor" title="(in PyTorch v2.6)"><em>Tensor</em></a>, <a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.13)">float</a>, <a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.13)"><em>List</em></a>[<a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.13)">float</a>], <a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.13)"><em>List</em></a>[<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)">str</a>], <a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.13)"><em>List</em></a>[<a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.13)"><em>Any</em></a>]]</p>
</dd>
</dl>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.4.11: </span>added <code class="docutils literal notranslate"><span class="pre">group</span></code></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.all_reduce">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">all_reduce</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">op</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'SUM'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">group</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/ignite/distributed/utils.html#all_reduce"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.all_reduce" title="Permalink to this definition">#</a></dt>
<dd><p>Helper method to perform all reduce operation.</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 external" href="https://docs.python.org/3/library/typing.html#typing.Union" title="(in Python v3.13)"><em>Union</em></a><em>[</em><a class="reference external" href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor" title="(in PyTorch v2.6)"><em>Tensor</em></a><em>, </em><a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.13)"><em>float</em></a><em>]</em>) – tensor or number to collect across participating processes.</p></li>
<li><p><strong>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>) – reduction operation, “SUM” by default. Possible values: “SUM”, “PRODUCT”, “MIN”, “MAX”, “AND”, “OR”.
Horovod backend supports only “SUM”, “AVERAGE”, “ADASUM”, “MIN”, “MAX”, “PRODUCT”.</p></li>
<li><p><strong>group</strong> (<a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.13)"><em>Optional</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Union" title="(in Python v3.13)"><em>Union</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.13)"><em>Any</em></a><em>, </em><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.13)"><em>List</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><em>int</em></a><em>]</em><em>]</em><em>]</em>) – list of integer or the process group for each backend. If None, the default process group will be used.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>torch.Tensor or number</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Union" title="(in Python v3.13)"><em>Union</em></a>[<a class="reference external" href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor" title="(in PyTorch v2.6)"><em>Tensor</em></a>, <a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.13)">float</a>]</p>
</dd>
</dl>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.4.11: </span>added <code class="docutils literal notranslate"><span class="pre">group</span></code></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.available_backends">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">available_backends</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#available_backends"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.available_backends" title="Permalink to this definition">#</a></dt>
<dd><p>Returns available backends.</p>
<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/typing.html#typing.Tuple" title="(in Python v3.13)"><em>Tuple</em></a>[<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)">str</a>, …]</p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.backend">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">backend</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#backend"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.backend" title="Permalink to this definition">#</a></dt>
<dd><p>Returns computation model’s backend.</p>
<ul class="simple">
<li><p><cite>None</cite> for no distributed configuration</p></li>
<li><p>“nccl” or “gloo” or “mpi” for native torch distributed configuration</p></li>
<li><p>“xla-tpu” for XLA distributed configuration</p></li>
<li><p>“horovod” for Horovod distributed framework</p></li>
</ul>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>str or None</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.13)"><em>Optional</em></a>[<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)">str</a>]</p>
</dd>
</dl>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.4.2: </span>Added Horovod distributed framework.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.barrier">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">barrier</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#barrier"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.barrier" title="Permalink to this definition">#</a></dt>
<dd><p>Helper method to synchronize all processes.</p>
<dl class="field-list simple">
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>None</p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.broadcast">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">broadcast</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">src</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">safe_mode</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/ignite/distributed/utils.html#broadcast"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.broadcast" title="Permalink to this definition">#</a></dt>
<dd><p>Helper method to perform broadcast operation.</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 external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.13)"><em>Optional</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Union" title="(in Python v3.13)"><em>Union</em></a><em>[</em><a class="reference external" href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor" title="(in PyTorch v2.6)"><em>Tensor</em></a><em>, </em><a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.13)"><em>float</em></a><em>, </em><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>]</em>) – tensor or number or str to broadcast to participating processes.
Make sure to respect data type of torch tensor input for all processes, otherwise execution will crash.
Can use None for non-source data with <code class="docutils literal notranslate"><span class="pre">safe_mode=True</span></code>.</p></li>
<li><p><strong>src</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><em>int</em></a>) – source rank. Default, 0.</p></li>
<li><p><strong>safe_mode</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a>) – if True, non source input data can be <code class="docutils literal notranslate"><span class="pre">None</span></code> or anything (will be discarded), otherwise data type
of the input <code class="docutils literal notranslate"><span class="pre">tensor</span></code> should be respected for all processes. Please, keep in mind, this mode is working
only for dense tensors as source input if a tensor is provided. It also leads to some additional
collectives before the broadcast, making it slower than without using this mode. Default, False.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>torch.Tensor or string or number</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Union" title="(in Python v3.13)"><em>Union</em></a>[<a class="reference external" href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor" title="(in PyTorch v2.6)"><em>Tensor</em></a>, <a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.13)">float</a>, <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)">str</a>]</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">if</span> <span class="n">idist</span><span class="o">.</span><span class="n">get_rank</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">t1</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">idist</span><span class="o">.</span><span class="n">device</span><span class="p">())</span>
<span class="n">s1</span> <span class="o">=</span> <span class="s2">"abc"</span>
<span class="n">x</span> <span class="o">=</span> <span class="mf">12.3456</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">idist</span><span class="o">.</span><span class="n">device</span><span class="p">())</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">t1</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">idist</span><span class="o">.</span><span class="n">device</span><span class="p">())</span>
<span class="n">s1</span> <span class="o">=</span> <span class="s2">""</span>
<span class="n">x</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="c1"># Broadcast tensor t1 from rank 0 to all processes</span>
<span class="n">t1</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">broadcast</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span> <span class="n">src</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">)</span>
<span class="c1"># Broadcast string s1 from rank 0 to all processes</span>
<span class="n">s1</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">broadcast</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="n">src</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="c1"># >>> s1 = "abc"</span>
<span class="c1"># Broadcast float number x from rank 0 to all processes</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">broadcast</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">src</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="c1"># >>> x = 12.3456</span>
<span class="c1"># Broadcast any of those types from rank 0,</span>
<span class="c1"># but other ranks do not define the placeholder</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">broadcast</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">src</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">safe_mode</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">New in version 0.4.2.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.4.5: </span>added <code class="docutils literal notranslate"><span class="pre">safe_mode</span></code></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.device">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">device</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#device"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.device" title="Permalink to this definition">#</a></dt>
<dd><p>Returns current device according to current distributed configuration.</p>
<ul class="simple">
<li><p><cite>torch.device(“cpu”)</cite> if no distributed configuration or torch native gloo distributed configuration</p></li>
<li><p><cite>torch.device(“cuda:local_rank”)</cite> if torch native nccl or horovod distributed configuration</p></li>
<li><p><cite>torch.device(“xla:index”)</cite> if XLA distributed configuration</p></li>
</ul>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>torch.device</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p><a class="reference external" href="https://pytorch.org/docs/stable/tensor_attributes.html#torch.device" title="(in PyTorch v2.6)"><em>device</em></a></p>
</dd>
</dl>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.4.2: </span>Added Horovod distributed framework.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.finalize">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">finalize</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#finalize"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.finalize" title="Permalink to this definition">#</a></dt>
<dd><p>Finalizes distributed configuration. For example, in case of native pytorch distributed configuration,
it calls <code class="docutils literal notranslate"><span class="pre">dist.destroy_process_group()</span></code>.</p>
<dl class="field-list simple">
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>None</p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.get_local_rank">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">get_local_rank</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#get_local_rank"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.get_local_rank" title="Permalink to this definition">#</a></dt>
<dd><p>Returns local process rank within current distributed configuration.
Returns 0 if no distributed configuration.</p>
<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#int" title="(in Python v3.13)">int</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.get_nnodes">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">get_nnodes</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#get_nnodes"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.get_nnodes" title="Permalink to this definition">#</a></dt>
<dd><p>Returns number of nodes within current distributed configuration.
Returns 1 if no distributed configuration.</p>
<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#int" title="(in Python v3.13)">int</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.get_node_rank">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">get_node_rank</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#get_node_rank"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.get_node_rank" title="Permalink to this definition">#</a></dt>
<dd><p>Returns node rank within current distributed configuration.
Returns 0 if no distributed configuration.</p>
<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#int" title="(in Python v3.13)">int</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.get_nproc_per_node">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">get_nproc_per_node</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#get_nproc_per_node"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.get_nproc_per_node" title="Permalink to this definition">#</a></dt>
<dd><p>Returns number of processes (or tasks) per node within current distributed configuration.
Returns 1 if no distributed configuration.</p>
<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#int" title="(in Python v3.13)">int</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.get_rank">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">get_rank</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#get_rank"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.get_rank" title="Permalink to this definition">#</a></dt>
<dd><p>Returns process rank within current distributed configuration. Returns 0 if no distributed configuration.</p>
<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#int" title="(in Python v3.13)">int</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.get_world_size">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">get_world_size</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#get_world_size"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.get_world_size" title="Permalink to this definition">#</a></dt>
<dd><p>Returns world size of current distributed configuration. Returns 1 if no distributed configuration.</p>
<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#int" title="(in Python v3.13)">int</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.hostname">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">hostname</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#hostname"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.hostname" title="Permalink to this definition">#</a></dt>
<dd><p>Returns host name for current process within current distributed configuration.</p>
<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/stdtypes.html#str" title="(in Python v3.13)">str</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.initialize">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">initialize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">backend</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#initialize"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.initialize" title="Permalink to this definition">#</a></dt>
<dd><p>Initializes distributed configuration according to provided <code class="docutils literal notranslate"><span class="pre">backend</span></code></p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>backend</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><em>str</em></a>) – backend: <cite>nccl</cite>, <cite>gloo</cite>, <cite>xla-tpu</cite>, <cite>horovod</cite>.</p></li>
<li><p><strong>kwargs</strong> (<a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.13)"><em>Any</em></a>) – <p>acceptable kwargs according to provided backend:</p>
<ul>
<li><div class="line-block">
<div class="line">”nccl” or “gloo” : <code class="docutils literal notranslate"><span class="pre">timeout(=timedelta(minutes=30))</span></code>, <code class="docutils literal notranslate"><span class="pre">init_method(=None)</span></code>,</div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">rank(=None)</span></code>, <code class="docutils literal notranslate"><span class="pre">world_size(=None)</span></code>.</div>
<div class="line">By default, <code class="docutils literal notranslate"><span class="pre">init_method</span></code> will be “env://”. See more info about parameters: <a class="reference external" href="https://pytorch.org/docs/stable/distributed.html#torch.distributed.init_process_group">torch_init</a>.</div>
</div>
</li>
<li><div class="line-block">
<div class="line">”horovod” : comm(=None), more info: <a class="reference external" href="https://horovod.readthedocs.io/en/latest/api.html#module-horovod.torch">hvd_init</a>.</div>
</div>
</li>
</ul>
</p></li>
</ul>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>None</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Launch single node multi-GPU training with <code class="docutils literal notranslate"><span class="pre">torchrun</span></code> utility.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># >>> torchrun --nproc_per_node=4 main.py</span>
<span class="c1"># main.py</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">ignite.distributed</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">idist</span>
<span class="k">def</span><span class="w"> </span><span class="nf">train_fn</span><span class="p">(</span><span class="n">local_rank</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">):</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">torch.distributed</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">dist</span>
<span class="k">assert</span> <span class="n">dist</span><span class="o">.</span><span class="n">is_available</span><span class="p">()</span> <span class="ow">and</span> <span class="n">dist</span><span class="o">.</span><span class="n">is_initialized</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">dist</span><span class="o">.</span><span class="n">get_world_size</span><span class="p">()</span> <span class="o">==</span> <span class="mi">4</span>
<span class="n">device</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">device</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">device</span> <span class="o">==</span> <span class="n">torch</span><span class="o">.</span><span class="n">device</span><span class="p">(</span><span class="sa">f</span><span class="s2">"cuda:</span><span class="si">{</span><span class="n">local_rank</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="n">backend</span> <span class="o">=</span> <span class="s2">"nccl"</span> <span class="c1"># or "gloo" or "horovod" or "xla-tpu"</span>
<span class="n">idist</span><span class="o">.</span><span class="n">initialize</span><span class="p">(</span><span class="n">backend</span><span class="p">)</span>
<span class="c1"># or for torch native distributed on Windows:</span>
<span class="c1"># idist.initialize("nccl", init_method="file://tmp/shared")</span>
<span class="n">local_rank</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">get_local_rank</span><span class="p">()</span>
<span class="n">train_fn</span><span class="p">(</span><span class="n">local_rank</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>
<span class="n">idist</span><span class="o">.</span><span class="n">finalize</span><span class="p">()</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.4.2: </span><code class="docutils literal notranslate"><span class="pre">backend</span></code> now accepts <cite>horovod</cite> distributed framework.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.4.5: </span><code class="docutils literal notranslate"><span class="pre">kwargs</span></code> now accepts <code class="docutils literal notranslate"><span class="pre">init_method</span></code>, <code class="docutils literal notranslate"><span class="pre">rank</span></code>, <code class="docutils literal notranslate"><span class="pre">world_size</span></code> for PyTorch native distributed backend.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.model_name">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">model_name</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#model_name"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.model_name" title="Permalink to this definition">#</a></dt>
<dd><p>Returns distributed configuration name (given by ignite)</p>
<ul class="simple">
<li><p><cite>serial</cite> for no distributed configuration</p></li>
<li><p><cite>native-dist</cite> for native torch distributed configuration</p></li>
<li><p><cite>xla-dist</cite> for XLA distributed configuration</p></li>
<li><p><cite>horovod-dist</cite> for Horovod distributed framework</p></li>
</ul>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 0.4.2: </span><cite>horovod-dist</cite> will be returned for Horovod distributed framework.</p>
</div>
<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/stdtypes.html#str" title="(in Python v3.13)">str</a></p>
</dd>
</dl>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.new_group">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">new_group</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ranks</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#new_group"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.new_group" title="Permalink to this definition">#</a></dt>
<dd><p>Helper method to make group for each backend from ranks.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>ranks</strong> (<a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.13)"><em>List</em></a><em>[</em><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><em>int</em></a><em>]</em>) – subset of ranks to be grouped.</p></li>
<li><p><strong>kwargs</strong> (<a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.13)"><em>Any</em></a>) – <p>acceptable kwargs according to provided backend:</p>
<ul>
<li><div class="line-block">
<div class="line">”nccl” or “gloo” : <code class="docutils literal notranslate"><span class="pre">backend</span> <span class="pre">(=None)</span></code>, <code class="docutils literal notranslate"><span class="pre">pg_options</span> <span class="pre">(=None)</span></code>.</div>
</div>
</li>
</ul>
</p></li>
</ul>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.13)"><em>Any</em></a></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Launch single node multi-GPU training with <code class="docutils literal notranslate"><span class="pre">torchrun</span></code> utility.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">ignite.distributed</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">idist</span>
<span class="n">ranks</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">group</span> <span class="o">=</span> <span class="n">idist</span><span class="o">.</span><span class="n">new_group</span><span class="p">(</span><span class="n">ranks</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">New in version 0.4.11.</span></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.one_rank_first">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">one_rank_first</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">rank</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">local</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/ignite/distributed/utils.html#one_rank_first"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.one_rank_first" title="Permalink to this definition">#</a></dt>
<dd><p>Context manager that ensures a specific rank runs first before others in a distributed
environment.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>rank</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><em>int</em></a>) – rank of the process that should execute the code
block inside the context manager first. Default, 0.</p></li>
<li><p><strong>local</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a>) – flag to specify local rank or global rank.
If True <code class="docutils literal notranslate"><span class="pre">rank</span></code> argument will define a local rank to run first.
Default, False</p></li>
</ul>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.13)"><em>Any</em></a></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">download_dataset</span><span class="p">():</span>
<span class="o">...</span>
<span class="k">with</span> <span class="n">idist</span><span class="o">.</span><span class="n">one_rank_first</span><span class="p">():</span>
<span class="n">ds</span> <span class="o">=</span> <span class="n">download_dataset</span><span class="p">()</span>
<span class="n">dp</span> <span class="o">=</span> <span class="n">ds</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">New in version 0.4.13.</span></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.one_rank_only">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">one_rank_only</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">rank</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">with_barrier</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/ignite/distributed/utils.html#one_rank_only"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.one_rank_only" title="Permalink to this definition">#</a></dt>
<dd><p>Decorator to filter handlers wrt a rank number</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>rank</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><em>int</em></a>) – rank number of the handler (default: 0).</p></li>
<li><p><strong>with_barrier</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a>) – synchronisation with a barrier (default: False).</p></li>
</ul>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Callable" title="(in Python v3.13)"><em>Callable</em></a></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">engine</span> <span class="o">=</span> <span class="o">...</span>
<span class="nd">@engine</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="nd">@one_rank_only</span><span class="p">()</span> <span class="c1"># means @one_rank_only(rank=0)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">some_handler</span><span class="p">(</span><span class="n">_</span><span class="p">):</span>
<span class="o">...</span>
<span class="nd">@engine</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="nd">@one_rank_only</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="k">def</span><span class="w"> </span><span class="nf">some_handler</span><span class="p">(</span><span class="n">_</span><span class="p">):</span>
<span class="o">...</span>
</pre></div>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="ignite.distributed.utils.set_local_rank">
<span class="sig-prename descclassname"><span class="pre">ignite.distributed.utils.</span></span><span class="sig-name descname"><span class="pre">set_local_rank</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">index</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/ignite/distributed/utils.html#set_local_rank"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#ignite.distributed.utils.set_local_rank" title="Permalink to this definition">#</a></dt>
<dd><p>Method to hint the local rank in case if torch native distributed context is created by user
without using <a class="reference internal" href="#ignite.distributed.utils.initialize" title="ignite.distributed.utils.initialize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">initialize()</span></code></a> or <a class="reference internal" href="#ignite.distributed.utils.spawn" title="ignite.distributed.utils.spawn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">spawn()</span></code></a>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>index</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><em>int</em></a>) – local rank or current process index</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>None</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>User set up torch native distributed process group</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">ignite.distributed</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">idist</span>
<span class="k">def</span><span class="w"> </span><span class="nf">run</span><span class="p">(</span><span class="n">local_rank</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">idist</span><span class="o">.</span><span class="n">set_local_rank</span><span class="p">(</span><span class="n">local_rank</span><span class="p">)</span>
<span class="c1"># ...</span>
<span class="n">dist</span><span class="o">.</span><span class="n">init_process_group</span><span class="p">(</span><span class="o">**</span><span class="n">dist_info</span><span class="p">)</span>
<span class="c1"># ...</span>
</pre></div>
</div>
</dd></dl>