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

Fixes #2407 #2981

Merged
merged 7 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
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
18 changes: 12 additions & 6 deletions pytorch_lightning/accelerators/ddp_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ def spawn_ddp_children(self, model):
# use the same python interpreter and actually running
command = [sys.executable] + command

# since this script sets the visible devices we replace the gpus flag with a number
num_gpus = torch.cuda.device_count()

if '--gpus' in command:
gpu_flag_idx = command.index('--gpus')
command[gpu_flag_idx + 1] = f'{num_gpus}'
# the visible devices tell us how many GPUs we want to use.
# when the trainer script was called the device has already been scoped by the time
# code reaches this point. so, to call the scripts, we need to leave cuda visible devices alone
# but forward the GPUs selected via environment variables
gpu_ids = os.environ.get('CUDA_VISIBLE_DEVICES', '')
Borda marked this conversation as resolved.
Show resolved Hide resolved
if len(gpu_ids) == 1:
gpu_ids = f'{gpu_ids},'

num_gpus = max(1, len(gpu_ids.split(',')))

# set the flag for ddp scripts
os.environ['PL_TRAINER_GPUS'] = gpu_ids

os.environ['WORLD_SIZE'] = f'{num_gpus * self.trainer.num_nodes}'

Expand Down
3 changes: 3 additions & 0 deletions pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ def __init__(
self.accumulate_grad_batches = accumulate_grad_batches
self.configure_accumulated_gradients(accumulate_grad_batches)

# override with environment flag
gpus = os.environ.get('PL_TRAINER_GPUS', gpus)

# for gpus allow int, string and gpu list
if auto_select_gpus and isinstance(gpus, int):
self.gpus = pick_multiple_gpus(gpus)
Expand Down