Skip to content
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

Fix issue 1721 by always initializing process group. #1722

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 27 additions & 17 deletions yolox/core/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,34 @@ def launch(
args (tuple): arguments passed to main_func
"""
world_size = num_machines * num_gpus_per_machine
if world_size <= 0:
raise ValueError('`world_size` should be positive, currently {}'.format(world_size))

# Even if `world_size == 1`, we have to initialize the process group,
# so the user code can use all the `torch.dist`` facilities. This
# makes the code uniform whether there is one or more processes.

if dist_url == "auto":
assert (
num_machines == 1
), "`dist_url=auto` cannot work with distributed training."
port = _find_free_port()
dist_url = f"tcp://127.0.0.1:{port}"

worker_args = (
main_func,
world_size,
num_gpus_per_machine,
machine_rank,
backend,
dist_url,
args,
)

if world_size > 1:
# https://github.com/pytorch/pytorch/pull/14391
# TODO prctl in spawned processes

if dist_url == "auto":
assert (
num_machines == 1
), "dist_url=auto cannot work with distributed training."
port = _find_free_port()
dist_url = f"tcp://127.0.0.1:{port}"

start_method = "spawn"
cache = vars(args[1]).get("cache", False)

Expand All @@ -82,20 +99,13 @@ def launch(
mp.start_processes(
_distributed_worker,
nprocs=num_gpus_per_machine,
args=(
main_func,
world_size,
num_gpus_per_machine,
machine_rank,
backend,
dist_url,
args,
),
args=worker_args,
daemon=False,
start_method=start_method,
)

else:
main_func(*args)
_distributed_worker(0, *worker_args)


def _distributed_worker(
Expand Down