Skip to content

Commit 2eb47df

Browse files
rogiervdRogier van Dalen
andauthored
Make run_on_main and main_process_only return the result to all proce… (#2943)
Co-authored-by: Rogier van Dalen <r.vandalen@samsung.com>
1 parent 900f02c commit 2eb47df

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

speechbrain/utils/distributed.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ def run_on_main(
104104
Keyword args to pass to post_func.
105105
run_post_on_main : bool
106106
Whether to run post_func on main process as well. (default: False)
107+
108+
Returns
109+
-------
110+
On all processes: the value that func returned, when it ran on the main
111+
process.
107112
"""
108113
# Handle the mutable data types' default args:
109114
if args is None:
@@ -115,7 +120,7 @@ def run_on_main(
115120
if post_kwargs is None:
116121
post_kwargs = {}
117122

118-
main_process_only(func)(*args, **kwargs)
123+
result = main_process_only(func)(*args, **kwargs)
119124
ddp_barrier()
120125

121126
if post_func is not None:
@@ -128,6 +133,8 @@ def run_on_main(
128133
post_func(*post_args, **post_kwargs)
129134
ddp_barrier()
130135

136+
return result
137+
131138

132139
def is_distributed_initialized() -> bool:
133140
r"Returns whether the current system is distributed."
@@ -171,16 +178,19 @@ def main_process_only(function):
171178
r"""Function decorator to ensure the function runs only on the main process.
172179
This is useful for things like saving to the filesystem or logging
173180
to a web address where you only want it to happen on a single process.
181+
The function will return the result computed on the main process to all
182+
processes.
174183
"""
175184

176185
@wraps(function)
177186
def main_proc_wrapped_func(*args, **kwargs):
178187
"""This decorated function runs only if this is the main process."""
179188
with MainProcessContext():
180189
if if_main_process():
181-
return function(*args, **kwargs)
190+
result = function(*args, **kwargs)
182191
else:
183-
return None
192+
result = None
193+
return ddp_broadcast(result)
184194

185195
return main_proc_wrapped_func
186196

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
Test for distributed.py.
3+
"""
4+
5+
import operator
6+
7+
import torch
8+
9+
from speechbrain.utils import distributed
10+
11+
12+
def mock_initialise_process_group(rank: int, world_size: int, tmpdir):
13+
"""
14+
Pretend to run on under "torchrun".
15+
"""
16+
import os
17+
18+
os.environ["RANK"] = str(rank)
19+
os.environ["LOCAL_RANK"] = str(rank)
20+
sync_file = f"file://{tmpdir}/sync"
21+
torch.distributed.init_process_group(
22+
"gloo", rank=rank, world_size=world_size, init_method=sync_file
23+
)
24+
25+
26+
# Test @main_process_only a single call with a return value.
27+
28+
29+
@distributed.main_process_only
30+
def return_rank_345(rank, world_size):
31+
# This should run on the main process only.
32+
assert rank == 0
33+
return 345 + rank
34+
35+
36+
def return_ranks_345_in_mock_process(rank, world_size, tmpdir):
37+
mock_initialise_process_group(rank, world_size, tmpdir)
38+
result = return_rank_345(rank, world_size)
39+
assert result == 345
40+
41+
42+
def test_main_process_only(tmpdir):
43+
world_size = 2
44+
torch.multiprocessing.spawn(
45+
return_ranks_345_in_mock_process,
46+
(world_size, tmpdir),
47+
world_size,
48+
join=True,
49+
)
50+
51+
52+
# Test @main_process_only a recursive call.
53+
54+
55+
@distributed.main_process_only
56+
def fibonacci(n):
57+
if n == 0 or n == 1:
58+
return 1
59+
return fibonacci(n - 2) + fibonacci(n - 1)
60+
61+
62+
def check_fibonacci(rank, world_size, tmpdir):
63+
mock_initialise_process_group(rank, world_size, tmpdir)
64+
assert fibonacci(0) == 1
65+
assert fibonacci(1) == 1
66+
assert fibonacci(2) == 2
67+
assert fibonacci(3) == 3
68+
assert fibonacci(4) == 5
69+
70+
71+
def test_main_process_only_nested(tmpdir):
72+
world_size = 2
73+
torch.multiprocessing.spawn(
74+
check_fibonacci, (world_size, tmpdir), world_size, join=True
75+
)
76+
77+
78+
# Test run_on_main.
79+
80+
81+
def check_add_in_mock_process(rank, world_size, tmpdir, i, j):
82+
mock_initialise_process_group(rank, world_size, tmpdir)
83+
assert distributed.run_on_main(operator.add, args=(i, j)) == i + j
84+
85+
86+
def test_run_on_main(tmpdir):
87+
world_size = 2
88+
torch.multiprocessing.spawn(
89+
check_add_in_mock_process,
90+
(world_size, tmpdir, 23, 54),
91+
world_size,
92+
join=True,
93+
)

0 commit comments

Comments
 (0)