What is the best practice for running distributed adversarial training?

krumo: <p>Hi all,</p>
<p>I’m just a newbie to PyTorch and struggling for PyTorch distributed training. Currently, I’m trying to implement a GAN like training strategy. The training consists of two stages:</p>
<ol>
<li>
<p>Fix task network, train discrinmator, my workflow is as following:<br>
src_data -&gt; T() -&gt;detach()-&gt; D() -&gt; loss(src_pred, src_label)<br>
tgt_data -&gt; T()-&gt;detach()-&gt;D()-&gt;loss(tgt_pred, tgt_label)</p>
</li>
<li>
<p>Fix discrinmator, train task network, my workflow is as following:<br>
src_data-&gt;T()-&gt;supervised_loss<br>
tgt_data-&gt;T()-&gt;D()-&gt;-1*loss(tgt_pred, tgt_label)<br>
The task network T() and discriminator network D() are both wrapped in DDP and they are placed in different process group. The task network is trained with supervised loss with labeled data and finetuned by the adversarial loss with unlabeled data.</p>
</li>
</ol>
<p>For this setting I have 2 questions:</p>
<ol>
<li>Is it the correct way to combine two DDP models? Or do I have to warp them into one single module first and then place them under DDP?</li>
<li>During training process of task network, I have to fix discriminator’s parameters. Now I just set the requires_grad of all parameters in discrinmator as False and turn them back to True after the loss.backward() is called. Is there anything else to be changed? I found DDP doesn’t allow unused parameters now, but it seems okay to use a module which doesn’t require gradients entirely. Do I do it in a correct way?</li>
</ol>
<p>I’ll appreciate if there’s somebody could tell me what’s the best practice of implementing multiple models for adversarial training. Thanks in advance!</p>

mdlockyer: <p>I have opened a discussion here about a similar question regarding two DDP modules in a GAN setting <a href="https://discuss.pytorch.org/t/calling-distributeddataparallel-on-multiple-modules/38055/5" class="inline-onebox">Calling DistributedDataParallel on multiple Modules?</a>. I’m still trying to determine if one process group can suffice, but it seems like the safest course of action is to use separate groups for G and D.</p>
<p>Regarding setting requires_grad to False on D while back propagating G’s loss, I have been meaning to implement that same thing but never got around to it. It seems like the logical approach, as it is just wasting compute time calculating gradients for D when they are going to be discarded.</p>

pietern: <aside class="quote no-group" data-username="mdlockyer" data-post="2" data-topic="43323">
<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/m/3bc359/48.png" class="avatar"> mdlockyer:</div>
<blockquote>
<p>Regarding setting requires_grad to False on D while back propagating G’s loss, I have been meaning to implement that same thing but never got around to it. It seems like the logical approach, as it is just wasting compute time calculating gradients for D when they are going to be discarded.</p>
</blockquote>
</aside>
<p>I think that doing exactly that would make it work with a single process group. Because you no longer race the allreduce calls from the two models. Also, I think you could put the discriminator in eval mode when doing this, which side steps some of the synchronization code paths in <code>DistributedDataParallel</code>.</p>