Should we split batch_size according to ngpu_per_node when DistributedDataparallel

hhxx: <p>Assume we have two nodes: node-A and node-B, each has 4gpus(i.e. ngpu_per_node=4). We set args.batch_size = 256 on each node, means that we want each node process 256 images in each forward.</p>
<p>(1) If we use DistributedDataparallel with 1gpu-per-process mode, shall we manually divide the batchsize by ngpu_per_node in torch.utils.data.DataLoader : <strong>torch.utils.data.DataLoader(batch_size = args.batch_size / 4)</strong>(the way used in <a href="https://github.com/pytorch/examples/blob/5df464c46cf321ed1cc3df1e670358d7f5ae1887/imagenet/main.py#L42" rel="nofollow noopener">pytorch-imagenet-official-example</a>). In my original opinion, I think <strong>DistributedSampler</strong> can handle such thing, because we have passed world_size and rank to DistributedSampler. . If I was wrong, please point it out, thanks!</p>
<p>(2) If dividing the batchsize by ngpu_per_node is a correct way, I wonder what will happen if we do not that.</p>
<ul>
<li>
<p>Does it means in each node, 4*batch_size images are processed per forward-process?</p>
</li>
<li>
<p>Will 4*len(dataset)  images are processed in one epoch, or the forward frequency are four times less than usual(i.e. the total number images proceeded per epoch keep same)?</p>
</li>
</ul>

mrshenli: <aside class="quote no-group" data-username="hhxx" data-post="1" data-topic="72769">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hhxx/48/21482_2.png" class="avatar"> hhxx:</div>
<blockquote>
<p>(2) If dividing the batchsize by ngpu_per_node is a correct way, I wonder what will happen if we do not that.</p>
<ul>
<li>Does it means in each node, 4*batch_size images are processed per forward-process?</li>
<li>Will 4*len(dataset) images are processed in one epoch, or the forward frequency are four times less than usual(i.e. the total number images proceeded per epoch keep same)?</li>
</ul>
</blockquote>
</aside>
<p>You are correct. Each DataLoader instance pairs with a DDP instances. If you do not divide the batch-size=256 by 4, then each DDP instance will process 256 images. As your env has 8-GPUs in total, there will be 8 DDP instances. So one iteration will process 256 * 8 images in total.</p>
<p>However, DDP does divide the gradients by the world_size by default <a href="https://github.com/pytorch/pytorch/blob/f326045b3757236aabe367dfca1894be14ce31ef/torch/csrc/distributed/c10d/reducer.cpp#L360" rel="noopener nofollow ugc">code</a>. So, when configuring learning rate, you only need to consider the batch_size for a single DDP instance.</p>

hhxx: <p>Another question is if we do not divide batch-size by 8, the total images processed in <strong>one epoch</strong> will be the same as usual or eight times?</p>
<p>As for learning rate, if we have 8-gpus in total, there wiil be 8 DDP instances. If the batch-size in each DDP distances is <strong>64</strong> (has been divides manually), then one iteration will process <strong>64×4=256</strong> images per node. Taking all gpu into account (2 nodes, 4gpus per node), then one iteration will process <strong>64×8=512</strong> images.  Assuming in one-gpu-one-node scenario, we set <strong>1×lr</strong> when batch-size=64, <strong>4×lr</strong> when batch-size=256 and <strong>8×lr</strong> when batch-size=512(a universal strategy that  increase learning rate with batch-size linearly). Let us back to DDP scenario (2 node, 4gpus per node), what learning rate shall we use？ 1×lr or 4×lr or 8×lr？</p>

mrshenli: <aside class="quote no-group" data-username="hhxx" data-post="3" data-topic="72769">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hhxx/48/21482_2.png" class="avatar"> hhxx:</div>
<blockquote>
<p>Another question is if we do not divide batch-size by 8, the total images processed in <strong>one epoch</strong> will be the same as usual or eight times?</p>
</blockquote>
</aside>
<p>The total number of images processed will be 8 times, because each DDP instance/process will process batch_size images.</p>
<blockquote>
<p>Let us back to DDP scenario (2 node, 4gpus per node), what learning rate shall we use？ 1×lr or 4×lr or 8×lr？</p>
</blockquote>
<p>It should be 1x lr. Because DDP calculates the average instead of sum of all local gradients. Let’s use some number to explain this. Assuming every image leads to a <code>torch.ones_like(param)</code> gradient for each parameter.</p>
<ul>
<li>For local training without DDP, if you set batch_size = 64, the gradient for each parameter will then be <code>torch.ones_like(param) * 64</code>.</li>
<li>For 8-process DDP training, if you set batch_size = 64, the local gradient for each parameter will also be <code>torch.ones_like(param) * 64</code>. Then DDP use collective communication to calculate the sum of gradients across all DDP instances which will be <code>torch.ones_like(param) * 64 * 8</code>, and then DDP divides that value by 8. So the final gradient in <code>param.grad</code> field will still be  <code>torch.ones_like(param) * 64</code> (the code actually first divide and then do globally sum). So, when set lr, you only need to consider local batch_size.</li>
</ul>

hhxx: <p>According to the discuss in <a href="https://discuss.pytorch.org/t/is-average-the-correct-way-for-the-gradient-in-distributeddataparallel-with-multi-nodes/34260/3">Is average the correct way for the gradient in DistributedDataParallel</a>, I think we should  set 8×lr. I will state my reason under 1 node, 8gpus, local-batch=64(images processed by one gpu each iteration) scenario:<br>
(1) Let us consider a batch images (batch-size=512), in DataParallel scenario, a complete forward-backforwad pipeline is:</p>
<ol>
<li>
<p>the input data are split to 8 slices (each contains 64 images), each slice is feed to net to compute output</p>
</li>
<li>
<p>outputs are concated  in master gpu (usually gpu 0) to form a [512, C] outputs</p>
</li>
<li>
<p>compute the loss with groundtruth(same dimension: [512, C]) :   <code>loss = \frac{1}{512} \sum_{i=1}^512 mse(output[i], groundtruth[i])</code>( use mse loss as illustration)</p>
</li>
<li>
<p>use loss.backward to compute gradients.</p>
</li>
</ol>
<p>So the finally [512, C] outputs are the same as computed on one gpu. So the learning rate here shall be set as 8×lr to keep same as 512 batchsize in one-gpu-one-node scenarior.</p>
<p>(2) Secondly, when DistributedDataparallel is used, the pipeline is</p>
<ol>
<li>
<p>the input data are also split to 8 slices</p>
</li>
<li>
<p>outputs are computed in each gpu to form a [64, C] outputs</p>
</li>
<li>
<p>In each gpu, compute the loss  <code>loss = \frac{1}{64} \sum_{i=1}^64 mse(output[i], groundtruth[i])</code> and compute gradients <code>grad_k (k is the gpu number, k=0,1,...,7)</code>:  (this is different with Dataparallel, which need collect all outputs in master gpu)</p>
</li>
<li>
<p>Average the gradients between all gpus: <code>avg_grad =\frac{1}{8} \sum_{k=1}^8 grad_k</code></p>
</li>
</ol>
<p>By this way, the averaged gradients are also same as the gradients computed on one-gpu-one-node scenario. So I think learning rate here need to be set as 8×lr to keep same as 512 batchsize on one-gpu-one-node scenario.</p>
<p>The main difference between you and me is that when local batch is set as 64, I think local gradients will be averaged over local samples, resulting in <code>torch.ones_like(param)*64/64</code>, but you think the local gradients will be summed over local samples, resulting in <code>torch.ones_like(param) * 64</code>. I think local gradients will be averaged mainly because the loss function in pytroch, like mse(), will compute the average loss over all input samples, so the gradients computed from such loss also should be averaged over all input samples.</p>
<p>I do not know if I understand DistributedDataparallel in a right way. Please let me know if there has any wrong.</p>

mrshenli: <p>I agree with all your analysis on the magnitude of the gradients, and I agree that it depends on the loss function. But even with MSE loss fn, it can lead to different conclusions:</p>
<ol>
<li>If the fw-bw has processed 8X data, we should set lr to 8X, meaning that the model should take a larger step if it has processed more data as the gradient is more accurate. (IIUC, this is what you advocate for)</li>
<li>If the gradient is of the same magnitude, we should use 1X lr, especially when approaching convergence. Otherwise, if we use 8X lr, it is more likely to overshoot and hurt converged model accuracy.</li>
</ol>
<p>After reading your analysis, I realized that, with MSE loss fn, the discussion is mostly irrelevant to DDP. The question would then be, if I increase batch size by k, how should I adjust the learning rate, which is an open question. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

hhxx: <p>Is it correct that when local batch-size is 64 (i.e. <code>torch.utils.data.DataLoader(batch_size=64) and torch.utils.data.distributed.DistributedSampler() is used</code>), and there are N processes totally in ddp (N processes distirbute in one node or more than one node), the forward-backward process is similar to the forward-backward process in 1-gpu-1-node using 64×N batch-size inputs?</p>

sakh251: <p>For SGD this <a href="https://arxiv.org/abs/1706.02677" rel="noopener nofollow ugc">https://arxiv.org/abs/1706.02677</a> paper suggest When the minibatch size is multiplied by k, multiply the learning rate by k.</p>

amitdedhia: <p>For my better understanding, <a class="mention" href="/u/mrshenli">@mrshenli</a> - can you please answer?</p>
<p>Suppose we have 32*8 images in the dataset, and the batch size is 32. We want to train the model for 1 epoch only. Now consider following 3 scenarios. Note that we use same LR and optimizer function in all three cases below.</p>
<p>(1) Single Node - Single GPU: In this case, one epoch will require 8 steps to execute i.e. in each step, 32 images will be processed. The gradient will be calculated and applied 8 times. The model parameters will get updated 8 times.</p>
<p>(2) Single Node - Multiple GPU using DataParallel: Suppose we use 8 GPUs. In this case, one epoch will still require 8 steps to execute i.e. in each step, 32 images will be processed (albeit each GPU will process only 4 images and then the gradients will be summed over). The gradient will be calculated and applied 8 times. The model parameters will get updated 8 times.</p>
<p>(3) Single Node - Multiple GPU using DistributedDataParallel: Suppose we use 8 GPUs. In this case, one epoch will require just 1 step to execute as in each step 32*8 images will be processed. However, this also means that the gradient will be calculated and applied only 1 time. And consequently, the model parameters will get updated just 1 time. So, in order to get similar results as previous two points (<span class="hashtag">#1</span> and <span class="hashtag">#2</span> above), we will have to execute 8 epochs instead of 1, as gradients are averaged in the backward function and hence effective weight updates are almost similar in scenario 3 and those in scenarios 1 and 2 above.</p>
<p>Is this understanding correct?</p>

ekurtic: <p>I think that points 1) and 2) are correct. And if you want to get the same behaviour in the DDP experiment (3), you shouldn’t do 8 epochs with batch size 32.<br>
You just have to pass <code>batch_size/num_GPUs</code> as your real batch size, i.e.: <code>32/8=4</code>. Then all 8 GPUs will process 4 images, i.e. <code>4 images * 8 GPUs = 32 images</code> per epoch (just like the (1) and (2)).</p>

amitdedhia: <p><a class="mention" href="/u/ekurtic">@ekurtic</a> Thanks for quick response.</p>
<p>I am trying to find how is the gain in training time (without affecting accuracy as much) by using DistributedDataParallel. As per you, if I use batch size of 4, then yes the training will be faster. However in that case I will be using LR of batch size 32 for that of 4. Is this okay? And if I divide LR by 8, then I am effectively slowing down the training, and thus not gaining on training time. Am I missing something here?</p>

ekurtic: <p>From my understanding of how DDP works:<br>
There are a few gains that result in better performance (faster training) with DDP. One of them is that in a DDP experiment, your model and optimizer are “replicated” on each GPU (before training starts; notice this happens only once). After that, each GPU will optimize your model independently and exchange gradients with other GPUs to ensure that optimization steps are the same at each GPU.<br>
On the other side, DataParallel has to scatter input data from the main GPU, replicate your model on each GPU, do a forward pass, gather back all outputs to the main GPU to compute loss, then send back the results to each GPU so they can compute gradients of the model, and in the end, collect all those gradients on the main GPU to compute the optimization step. And all of this happens for each batch of input data. As you can see, in contrast to the DDP, there is a lot of communication, copying, synchronizing and so on.</p>
<p>Regarding the LR question:<br>
If you use <code>batch_size/num_GPUs = 32/8 = 4</code> as your batch size in DDP, then you don’t have to change the LR. It should be the same as the one in DataParallel with <code>batch_size = 32</code>, because the effective batch size that your model is working with is the same: <code>32</code>. It’s just handled in a different way with DDP.</p>

amitdedhia: <p><a class="mention" href="/u/ekurtic">@ekurtic</a> I agree with your description of advantages of DDP in terms of communication, copy, sync etc. <img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=9" title=":+1:" class="emoji" alt=":+1:"></p>
<p>On the LR part, however, I am not sure. As per the discussion in this thread and few others, each gpu will individually process 4 images, loss will be calculated and then during backward function the gradients will be calculated individually first. Then DDP calculates the average (instead of sum) of all local gradients. This means that the gradient magnitude (the average value) is effectively in the near-range of that for batch size of 4, and not 32. So I am not sure whether LR of batchsize 4 should be applied or that of 32.</p>

ekurtic: <p>Loosely speaking this is why I think we don’t need to update LR if we ensure that the effective batch sizes are the same:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/5/c58767560f9257da6301905750ed55d9c6218194.png" data-download-href="https://discuss.pytorch.org/uploads/default/c58767560f9257da6301905750ed55d9c6218194" title="Screenshot 2021-07-16 at 07.45.05"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c58767560f9257da6301905750ed55d9c6218194_2_465x500.png" alt="Screenshot 2021-07-16 at 07.45.05" data-base62-sha1="sbqa5k82uiTBp0Qi5CzN94brmFS" width="465" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c58767560f9257da6301905750ed55d9c6218194_2_465x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c58767560f9257da6301905750ed55d9c6218194_2_697x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c58767560f9257da6301905750ed55d9c6218194_2_930x1000.png 2x" data-dominant-color="F4F4F4"><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">Screenshot 2021-07-16 at 07.45.05</span><span class="informations">1096×1178 101 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>

amitdedhia: <p>Thank you. This is very good explanation.</p>

junb: <p>Thanks for this explanation, I wonder about the following sentence in the context of DDP:</p>
<blockquote>
<p>We would have to multiply loss value with the number of GPUs only if the loss function had “reduction=sum” and not “reduction=mean” (to cancel out the DDP gradient averaging)</p>
</blockquote>
<p>If <code>reduction=sum</code> is used for computing the loss, it shouldn’t matter whether:</p>
<ul>
<li>the loss is multiply with the number of GPUs prior calling <code>backward()</code>,</li>
<li>the gradient is multiply with the number of GPUs after calling <code>backward()</code>.</li>
</ul>
<p>Do you agree?</p>

ekurtic: <p>Yes, I think I agree with that. As long as your “main loss” is a sum of some terms (either via reduction=sum or it’s just composed out of a few different losses), you would have to multiply it somewhere to cancel out the <code>1/num_GPUs</code> averaging that comes with DDP. From what I have seen so far, the most common thing is to just multiply loss prior to calling the <code>backward()</code> (your first suggestion). However, multiplying after should be fine too but you have to be careful and multiply before the <code>optimizer.step()</code> is called. I guess this second approach can then be achieved in two different ways: either by multiplying gradients (which could be slow) or by multiplying your learning_rate so that the final weight update has a proper scale.</p>

junb: <aside class="quote no-group" data-username="ekurtic" data-post="18" data-topic="72769">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/e9c0ed/48.png" class="avatar"> ekurtic:</div>
<blockquote>
<p>However, multiplying after should be fine too but you have to be careful and multiply before the <code>optimizer.step()</code> is called. I guess this second approach can then be achieved in two different ways: either by multiplying gradients (which could be slow) or by multiplying your learning_rate so that the final weight update has a proper scale.</p>
</blockquote>
</aside>
<p>Good call, I will have to remember that! Thanks for your quick answer.</p>

yuanze1024: <p>Thank you for your explanation.<br>
If I want to maximize the performance of the GPU, I must increase the batch size per GPU. In this situation, how can I change the learning rate to get similar performance to training on a single GPU? In my opinion, If the reduction is SUM, I will need to multiply the learning rate by N to compensate the difference.</p>

Maria_Djeblahi: <p>did you get any clear conclusion about that think</p>
<ol>
<li>if we have already a small batch size = 16 / 32 , is it necessery to devide it by the number of GPU .</li>
<li>for the LR  , when to multiplly it by the number of GPU<br>
i suppose we use one process per GPU</li>
</ol>