Training speed on Single GPU vs Multi-GPUs

username: sungtae
cooked: <p>Dear members,</p>
<p>While I was doing some practices to be familiar with multi GPU training, I found out that there is a difference not only in running time but also in training speed, e.g. number of epochs taken to get the same level of loss/accuracy.</p>
<p>I have trained a custom, yet simple, CNN model on a custom dataset of 6-classes classification problem with a single GPU and two GPUs separately.<br>
Everything is same except</p>
<p>model = nn.DataParallel(model)</p>
<p>in multi GPU training.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/9/9fd3383e799d83e0465099dea41e9cd268173cea.png" data-download-href="https://discuss.pytorch.org/uploads/default/9fd3383e799d83e0465099dea41e9cd268173cea" title="13%20PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/9/9fd3383e799d83e0465099dea41e9cd268173cea_2_690x236.png" alt="13%20PM" data-base62-sha1="mNSs8TTZdo2VIOs94ZlcIyReB2W" width="690" height="236" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/9/9fd3383e799d83e0465099dea41e9cd268173cea_2_690x236.png, https://discuss.pytorch.org/uploads/default/optimized/2X/9/9fd3383e799d83e0465099dea41e9cd268173cea_2_1035x354.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/9/9fd3383e799d83e0465099dea41e9cd268173cea.png 2x" data-dominant-color="F6F6F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">13%20PM</span><span class="informations">1372×470 63.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/8/85f310bd3deec1304c4ef4e1cd92c0867c546b5d.png" data-download-href="https://discuss.pytorch.org/uploads/default/85f310bd3deec1304c4ef4e1cd92c0867c546b5d" title="31%20PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/8/85f310bd3deec1304c4ef4e1cd92c0867c546b5d_2_690x240.png" alt="31%20PM" data-base62-sha1="j6YgF7fE9u6HqH5P9MXGDevkt2t" width="690" height="240" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/8/85f310bd3deec1304c4ef4e1cd92c0867c546b5d_2_690x240.png, https://discuss.pytorch.org/uploads/default/optimized/2X/8/85f310bd3deec1304c4ef4e1cd92c0867c546b5d_2_1035x360.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/8/85f310bd3deec1304c4ef4e1cd92c0867c546b5d.png 2x" data-dominant-color="F5F5F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">31%20PM</span><span class="informations">1356×472 65.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Attached figure contains loss (NLL loss after log-softmax) and accuracy plot over epochs.<br>
ORANGE line is for single GPU and BLUE line is for multi GPU training.<br>
I was intentionally overfitting the training set so that I can check if my draft model was being trained, so you do not need to worry about overfitting shown in the plots.</p>
<p>The point is that it is clear training with multiple GPUs converges quicker than with a single GPU.<br>
I am wondering whether it is expected result since I thought that using DataParallel is just splitting batch into chunks fed into each GPU and losses from backward pass are just collected to be summed/averaged together.</p>
<p>Thanks!</p>

username: enisberk
cooked: <p>I think this is interesting, did you find out reason behind these results ?</p>

username: Felix_Lessange
cooked: <p>WIth two gpus for the same batch size, your batch is divided in two, so the batch size seen by each gpu is two times inferior by the one seen by your single gpu in your first experiment. Since batch norms tensors are specific to a gpu, this means that the batch size seen by batch norm layers is divided by two. This could explain the difference.</p>

username: James_Condon
cooked: <p>Hi <a class="mention" href="/u/sungtae">@sungtae</a>,</p>
<p>Thanks for this. What is your batch size? what size are your images? and how ‘difficult’ is your dataset and your classes?</p>
<p>How many num_workers are you using for the dataloader?</p>
<p>I’m interested as I’m getting unexpected times per epoch for large (medical) images, same batch size, iterating over ranges of num_workers with different GPUs. 1 GPU is a lot quicker per epoch than 2 GPUs???</p>
<p>Cheers.</p>