# Python processes and GPU usage during distributed training

**URL:** <https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735>\
**Category:** Uncategorized\
**Created:** [March 2, 2020, 4:05pm UTC](https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735 "2020-03-02T16:05:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![742617000027](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/742617000027/32/21226_2.png) [@742617000027](https://discuss.pytorch.org/u/742617000027)\
**Post date:** [March 2, 2020, 4:05pm UTC](https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735/1 "2020-03-02T16:05:04Z")

</div>

Hey all!

I noticed that after the first epoch of training a custom DDP model (1 node, 2 GPUs), two new GPU memory usage entries pop up in `nvidia-smi`, one for each GPU. A memory usage of around 800 MB is reported for these. Curiously, they have the same PID as the ‘main’ process running on the opposite GPU (see the color-coded screenshot below). Is this expected behavior? It’s the only case of training a model with data parallelism where I noticed it.  
I’ve tried using several monitoring tools besides `nvidia-smi`, e.g. `GPUtil.showUtilization()`, `torch.cuda.memory_allocated()`, [printing tensors in use](https://docs.fast.ai/dev/gpu.html#pytorch-tensor-memory-tracking), but none of these are descriptive enough for me to pinpoint what exactly is going on.

Any help, suggestions or input would be greatly appreciated!

 ![torch_colored](https://discuss.pytorch.org/uploads/default/original/3X/c/7/c7bc6647f39b103c5b83ce6e0853affc2737f013.png)

---

<div class="post-metadata">

**Author:** ![mrshenli](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mrshenli/32/12220_2.png) [@mrshenli](https://discuss.pytorch.org/u/mrshenli)\
**Post date:** [March 24, 2020, 1:43am UTC](https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735/2 "2020-03-24T01:43:37Z")

</div>

> after the first epoch of training a custom DDP model (1 node, 2 GPUs), two new GPU memory usage entries pop up in `nvidia-smi`

You mean those two entries was not there during the 1 epoch, and only appears after that? What was executed after the first epoch? How did you initialize DDP? Did you specify `device_ids` or set `CUDA_VISIBLE_DEVICES` for the two DDP instances? Looks like both processes are operating on two GPUs. They should have exclusively run on a different GPU.

---

<div class="post-metadata">

**Author:** ![742617000027](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/742617000027/32/21226_2.png) [@742617000027](https://discuss.pytorch.org/u/742617000027)\
**Post date:** [March 24, 2020, 12:39pm UTC](https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735/3 "2020-03-24T12:39:33Z")

</div>

> You mean those two entries was not there during the 1 epoch, and only appears after that?

Yes, that’s correct.

> What was executed after the first epoch?

After each epoch of training, evaluation is run on GPU 0.

> How did you initialize DDP?

With `torch.distributed.launch --nproc_per_node=2` and the following:

```auto
torch.cuda.set_device(args.local_rank)
torch.distributed.init_process_group(backend='nccl', init_method='env://')
model.cuda(args.local_rank)
model = torch.nn.parallel.DistributedDataParallel(model, 
                                                   device_ids=[args.local_rank],
                                                   output_device=args.local_rank,
                                                   find_unused_parameters=True)

```

(`args.local_rank` is set automatically by `torch.distributed.launch`)

Appreciate the response!

---

<div class="post-metadata">

**Author:** ![mrshenli](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mrshenli/32/12220_2.png) [@mrshenli](https://discuss.pytorch.org/u/mrshenli)\
**Post date:** [March 24, 2020, 2:25pm UTC](https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735/4 "2020-03-24T14:25:36Z")

</div>

> [@742617000027](#):
>
> After each epoch of training, evaluation is run on GPU 0.

Does this occur on all processes or just rank 0?

---

<div class="post-metadata">

**Author:** ![742617000027](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/742617000027/32/21226_2.png) [@742617000027](https://discuss.pytorch.org/u/742617000027)\
**Post date:** [March 24, 2020, 2:46pm UTC](https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735/5 "2020-03-24T14:46:44Z")

</div>

Evaluation is gated by an `if` clause:

```auto
if args.local_rank == 0:
    evaluate(params)

```

Is that what you were asking?

---

<div class="post-metadata">

**Author:** ![mrshenli](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mrshenli/32/12220_2.png) [@mrshenli](https://discuss.pytorch.org/u/mrshenli)\
**Post date:** [March 24, 2020, 2:53pm UTC](https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735/6 "2020-03-24T14:53:51Z")

</div>

> [@742617000027](#):
>
> Is that what you were asking?

Yes.

```python
model = torch.distributed.nn.parallel.DistributedDataParallel(model)

```

The line above might be the reason. IIUC, `torch.cuda.set_device(args.local_rank)` only sets the default device, but DDP process can still see two GPUS. By default DDP would try to replicate models on all visible GPUs.

Could you please try use the following to construct DDP?

```auto
    model = torch.nn.parallel.DistributedDataParallel(model,
                                                      device_ids=[arg.local_rank],
                                                      output_device=arg.local_rank)

```

---

<div class="post-metadata">

**Author:** ![742617000027](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/742617000027/32/21226_2.png) [@742617000027](https://discuss.pytorch.org/u/742617000027)\
**Post date:** [March 24, 2020, 3:03pm UTC](https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735/7 "2020-03-24T15:03:11Z")

</div>

I might have made a mistake when copying the code from the script to post here 😅 In the code that produces the behavior mentioned in the OP, the model is wrapped actually exactly like you propsed, with the addition of the `find_unused_parameters` argument:

```auto
model = torch.nn.parallel.DistributedDataParallel(model, 
                                                  device_ids=[args.local_rank],
                                                  output_device=args.local_rank,
                                                  find_unused_parameters=True)

```

(Edited the previous post accordingly.)

---

<div class="post-metadata">

**Author:** ![mrshenli](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mrshenli/32/12220_2.png) [@mrshenli](https://discuss.pytorch.org/u/mrshenli)\
**Post date:** [March 24, 2020, 3:12pm UTC](https://discuss.pytorch.org/t/python-processes-and-gpu-usage-during-distributed-training/71735/8 "2020-03-24T15:12:09Z")

</div>

Thanks for the clarification.

That’s weird. I don’t know why DDP (or sth else) would then try to access a different GPU. Does it work if you set the `CUDA_VISIBLE_DEVICES` variable to prevent the process from seeing another GPU?
