Reasons why Horovod is much faster than DDP

polo5: <p>Using DDP for my code is 3x slower than using Horovod, both for single and multi gpu use cases. I’m trying to understand if this comes from an inherent design difference between the two frameworks, or if I broke something in DDP.</p>
<p><strong>Observations:</strong></p>
<ol>
<li>DDP is more cpu bounded: GPUs spend x2 more time idle waiting for the next batch than with horovod.</li>
<li>DDP spends x10 more time at the beginning of each epoch to set things up (and setting persistent_workers=True doesn’t change this)</li>
<li>DDP re-imports all my files at the beginning of each epoch, doing so n_gpus*n_cpu_workers times.</li>
<li>DDP sometimes leads to ‘leaked semaphore objects’ errors</li>
<li>DDP complains that some parameters weren’t used in producing the loss and so I need to use ‘find_unused_parameters=True’. I get no such error with vanilla single gpu code or with horovod.</li>
<li>During my debugging efforts I also found that using the horovod launching method on a single gpu is faster than vanilla pytorch on a single gpu (no DDP code). This is very strange. In other words, my training epoch is twice as fast if I do this  <code>mpirun -allow-run-as-root -np 1 -H localhost:1 -x MASTER_ADDR=127.0.0.1 -x MASTER_PORT=23457 -x HOROVOD_TIMELINE=/tmp/timeline.json -x OMP_NUM_THREADS=1 -x KMP_AFFINITY='granularity=fine,compact,1,0' -bind-to none python scripts/train_horovod.py</code> than if I do this <code>python scripts/train_singlegpu.py</code>.</li>
</ol>
<p><strong>Set up:</strong><br>
My DDP code follows the <a href="https://pytorch.org/docs/master/notes/ddp.html" rel="noopener nofollow ugc">official guidelines</a> and I’ve used DDP successfully in the past. The main specificity of my codebase is that my dataset class loads the nuscenes-devkit, which takes a lot of system memory. I share this object between the validation and training datasets using a global variable.</p>

polo5: <p>More specifically, this parameter is the cause of mpirun being faster than vanilla pytorch for my code:</p>
<aside class="quote no-group" data-username="polo5" data-post="1" data-topic="189754">
<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/polo5/48/8557_2.png" class="avatar"> polo5:</div>
<blockquote>
<p>-bind-to none</p>
</blockquote>
</aside>

wconstab: <p>it would help to file a github issue and include a reproducible code snippet so we can look into these issues.</p>
<p>In lieu of that, just off the top of my head</p>
<p>(1) seems a bit surprising, but it may depend on the model.  DDP shouldn’t really be doing anything during model forward, but it does schedule allreduce operations during backward.  if you’re profiling, you might be able to shed light on what cpu code is running.</p>
<p>(2) and (3) are also surprising, DDP itself is not really a trainer or a framework.  it shouldn’t have control over the beginning of an epoch, and it definitely shouldn’t be importing your files.  All DDP does is wrap your model code with something that schedules allreduce ops during backwards.<br>
(4) would be good to file a bug report for if you can reproduce reliably<br>
(5) is legitimate: the way DDP works, it needs to know which parameters to wait for gradients, and this can either be accomplished by using all the parameters (faster), or by enabling the mode to detect unused parameters (a bit slower).  Note there is also a ‘static_graph=True’ option that may be worth trying if you can gaurantee your graph is indeed static</p>
<p>(6) again is surprising.  i wonder if you’d find the same is true if you comment out the model training and just time the import of torch and your frameworks.</p>

polo5: <p>Thanks for sharing your thoughts.</p>
<p>Let me reply for the record in case someone else is looking at this in the future.</p>
<p>By “DDP” being slow I wasn’t being very precise, I meant “DDP+DistributedSampler” are slow, and probably the latter is to blame in this context.</p>
<p>I have had the same issue described above in a new codebase, and can confirm that using DPP and DistributedSampler imports my <code>dataset.py</code> files <code>n_workers*n_gpus times</code> there as well. I’m no expert but I reckon this is probably normal behaviour, and horovod may use a different way to do threading that doesn’t induce this behavior.</p>
<p>The difference in my new repo is that setting <code>persistent_workers=True</code> does help for epoch&gt;1, and so this large inefficiency of DDP/DistributedSampler is only a problem on the first epoch, when it takes 3 minutes to even start training.</p>
<p>People whose cpu can’t afford persistent_workers may be stuck at extremely low speeds, especially if the dataset is pretty small and you do many epochs.</p>