Skip to content

Collection of return values in Multiprocess functions runner #3235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions torchrec/distributed/test_utils/multi_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,27 @@ def _run_multi_process_test_per_rank(
self.assertEqual(0, p.exitcode)


def _wrapper_func_for_multiprocessing(args): # pyre-ignore[2, 3]
"""Wrapper function that unpacks arguments and calls the original func"""
func, rank, world_size, kwargs = args
kwargs["rank"] = rank
kwargs["world_size"] = world_size
return func(**kwargs)


# pyre-ignore[3]
def run_multi_process_func(
# pyre-ignore[2]
func: Callable[
[int, int, ...], # rank, world_size, ...
None,
Any, # Changed from None to Any to allow return values
],
multiprocessing_method: str = "spawn",
use_deterministic_algorithms: bool = True,
world_size: int = 2,
# pyre-ignore
**kwargs,
) -> None:
) -> List[Any]:
""" """
os.environ["MASTER_ADDR"] = str("localhost")
os.environ["MASTER_PORT"] = str(get_free_port())
Expand All @@ -215,22 +225,16 @@ def run_multi_process_func(
if world_size == 1:
kwargs["world_size"] = 1
kwargs["rank"] = 0
func(**kwargs)
return
result = func(**kwargs)
return [result]

ctx = multiprocessing.get_context(multiprocessing_method)
processes = []
for rank in range(world_size):
kwargs["rank"] = rank
kwargs["world_size"] = world_size
p = ctx.Process(
target=func,
name=f"rank{rank}",
kwargs=kwargs,
)
p.start()
processes.append(p)

for p in processes:
p.join()
if p.exitcode != 0:
print(p)
# Prepare arguments for each process
args_list = [(func, rank, world_size, kwargs.copy()) for rank in range(world_size)]

# Create a pool of worker processes for each rank
with ctx.Pool(processes=world_size) as pool:
results = pool.map(_wrapper_func_for_multiprocessing, args_list)

return results
Loading