forked from speechbrain/speechbrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributed.py
More file actions
208 lines (177 loc) · 6.35 KB
/
Copy pathdistributed.py
File metadata and controls
208 lines (177 loc) · 6.35 KB
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
"""Guard for running certain operations on main process only
Authors:
* Abdel Heba 2020
* Aku Rouhe 2020
* Peter Plantinga 2023
"""
import datetime
import os
from functools import wraps
import torch
MAIN_PROC_ONLY = 0
def run_on_main(
func,
args=None,
kwargs=None,
post_func=None,
post_args=None,
post_kwargs=None,
run_post_on_main=False,
):
"""Runs a function with DPP (multi-gpu) support.
The main function is only run on the main process.
A post_function can be specified, to be on non-main processes after the main
func completes. This way whatever the main func produces can be loaded on
the other processes.
Arguments
---------
func : callable
Function to run on the main process.
args : list, None
Positional args to pass to func.
kwargs : dict, None
Keyword args to pass to func.
post_func : callable, None
Function to run after func has finished on main. By default only run on
non-main processes.
post_args : list, None
Positional args to pass to post_func.
post_kwargs : dict, None
Keyword args to pass to post_func.
run_post_on_main : bool
Whether to run post_func on main process as well. (default: False)
"""
# Handle the mutable data types' default args:
if args is None:
args = []
if kwargs is None:
kwargs = {}
if post_args is None:
post_args = []
if post_kwargs is None:
post_kwargs = {}
main_process_only(func)(*args, **kwargs)
ddp_barrier()
if post_func is not None:
if run_post_on_main:
# Just run on every process without any barrier.
post_func(*post_args, **post_kwargs)
else:
# Do the opposite of `run_on_main`
if not if_main_process():
post_func(*post_args, **post_kwargs)
ddp_barrier()
def if_main_process():
"""Checks if the current process is the main local process and authorized to run
I/O commands. In DDP mode, the main local process is the one with LOCAL_RANK == 0.
In standard mode, the process will not have `LOCAL_RANK` Unix var and will be
authorized to run the I/O commands.
"""
if "LOCAL_RANK" in os.environ:
if os.environ["LOCAL_RANK"] == "":
return False
else:
if int(os.environ["LOCAL_RANK"]) == 0:
return True
return False
return True
def main_process_only(function):
"""Function decorator to ensure the function runs only on the main process.
This is useful for things like saving to the filesystem or logging
to a web address where you only want it to happen on a single process.
"""
@wraps(function)
def main_proc_wrapped_func(*args, **kwargs):
"""This decorated function runs only if this is the main process."""
global MAIN_PROC_ONLY
MAIN_PROC_ONLY += 1
if if_main_process():
result = function(*args, **kwargs)
else:
result = None
MAIN_PROC_ONLY -= 1
return result
return main_proc_wrapped_func
def ddp_barrier():
"""In DDP mode, this function will synchronize all processes.
torch.distributed.barrier() will block processes until the whole
group enters this function.
"""
# Check if we're in a single-threaded section, skip barrier
if MAIN_PROC_ONLY >= 1:
return
elif torch.distributed.is_initialized():
torch.distributed.barrier()
def ddp_broadcast(communication_object, src=0):
"""In DDP mode, this function will broadcast an object to all
processes.
Arguments
---------
communication_object: Any
The object to be communicated to all processes. Must be picklable.
See docs for ``torch.distributed.broadcast_object_list()``
src: int
The rank which holds the object to be communicated.
Returns
-------
The communication_object passed on rank src.
"""
if MAIN_PROC_ONLY >= 1 or not torch.distributed.is_initialized():
return communication_object
# Wrapping object in a list is required for preventing
# a copy of the object, maintaining a pointer instead
communication_list = [communication_object]
torch.distributed.broadcast_object_list(communication_list, src=src)
return communication_list[0]
def ddp_init_group(run_opts):
"""This function will initialize the ddp group if
distributed_launch bool is given in the python command line.
The ddp group will use distributed_backend arg for setting the
DDP communication protocol. `RANK` Unix variable will be used for
registering the subprocess to the ddp group.
Arguments
---------
run_opts: list
A list of arguments to parse, most often from `sys.argv[1:]`.
Returns
-------
None
"""
rank = os.environ.get("RANK")
local_rank = os.environ.get("LOCAL_RANK")
if local_rank is None or rank is None:
return
local_rank = int(local_rank)
if not run_opts["distributed_backend"] == "gloo":
if local_rank + 1 > torch.cuda.device_count():
raise ValueError(
"Killing process " + str() + "\n" "Not enough GPUs available!"
)
rank = int(rank)
if run_opts["distributed_backend"] == "nccl":
if not torch.distributed.is_nccl_available():
raise ValueError("NCCL is not supported in your machine.")
elif run_opts["distributed_backend"] == "gloo":
if not torch.distributed.is_gloo_available():
raise ValueError("GLOO is not supported in your machine.")
elif run_opts["distributed_backend"] == "mpi":
if not torch.distributed.is_mpi_available():
raise ValueError("MPI is not supported in your machine.")
else:
raise ValueError(
run_opts["distributed_backend"]
+ " communication protocol doesn't exist."
)
# rank arg is used to set the right rank of the current process for ddp.
# if you have 2 servers with 2 gpu:
# server1:
# GPU0: local_rank=device=0, rank=0
# GPU1: local_rank=device=1, rank=1
# server2:
# GPU0: local_rank=device=0, rank=2
# GPU1: local_rank=device=1, rank=3
torch.distributed.init_process_group(
backend=run_opts["distributed_backend"],
rank=rank,
timeout=datetime.timedelta(seconds=7200),
)