Torch.distributed.barrier() hangs in DDP

Manuel_Alejandro_Dia: <p>Hi everyone.</p>
<p>I am currently using DDP (NCCL backend) to train a network on a machine with 8 GPUs.<br>
I do a validation pass after each epoch, but I don’t want to do the same validation step on all 8 GPUs. So in order to only use one GPU for validation I am using <code>torch.distributed.barrier()</code>. But the process seems to hang up once it reaches the barrier statement.</p>
<p>Here is an example of the training loop:</p>
<pre><code class="lang-auto">for epoch in range(opt['epochs']):
    #[1]
    model.train()
    #[2]

    for batch_i, (imgs, targets) in enumerate(dataloader):
        #[3]
        imgs = Variable(imgs.cuda(gpu))
        targets = Variable(targets.cuda(gpu), requires_grad=False)

        loss, outputs = model(imgs, targets)
        loss.backward()

        optimizer.step()
        optimizer.zero_grad()
        
    scheduler.step()
    
    if epoch % opt['evaluation_interval'] == 0 &amp;&amp; gpu==0:
        print("\n---- Evaluating Model ----")

        evaluation_metrics = evaluate(model)

    #[4]
</code></pre>
<p>I have tried to put the barrier statement in four different places (maked in the code as comments) and no matter where I put it, the code hangs once it reaches that point.</p>
<p>For the cases (1,2) the code executes well on the first pass, but after validation it hangs. For the case (3) the code never reaches that point after the validation pass. For the case (4) once the validation is done, it also hangs.</p>
<p>I have also tested running the validation on all GPUs without using the barrier, and it does not hang.</p>
<p>Does anyone have any idea on why this is happenning?</p>
<p>I read this other two posts: <a href="https://discuss.pytorch.org/t/distributed-data-parallel-freezes-without-error-message/8009">1</a>, <a href="https://discuss.pytorch.org/t/multiprocessing-barrier-blocks-all-processes/80345/24">2</a>. But I think that their problem is not similar to mine.</p>
<p>Any help would be very appreciated! Thanks!</p>

rvarm1: <p>Thanks for reporting this issue! If <code>barrier()</code> is indeed not working properly, it seems like this is a bug and it would be great to create an issue over at <a href="http://github.com/pytorch/pytorch/issues/" rel="noopener nofollow ugc">http://github.com/pytorch/pytorch/issues/</a> with an example code snippet that reproduces the issue.</p>
<p>Although I’m a little confused, while the barrier at any of your points should work, I’m not sure how it helps you use only one GPU for validation? A barrier will just block all processes until all processes have entered the barrier.</p>

Manuel_Alejandro_Dia: <aside class="quote no-group" data-username="rvarm1" data-post="2" data-topic="114522">
<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/rvarm1/48/15821_2.png" class="avatar"> rvarm1:</div>
<blockquote>
<p>Although I’m a little confused, while the barrier at any of your points should work, I’m not sure how it helps you use only one GPU for validation? A barrier will just block all processes until all processes have entered the barrier.</p>
</blockquote>
</aside>
<p>I am also confused about this. My thought process is just that it seems like a waste of power to do the same validation step on all GPUs at the same time.</p>
<p>Right now, my validation is coded to be done by a single process (and by consequence a single GPU), so there wouldn’t be any performance gain running it across multiple GPUs.</p>
<p>I will try to reproduce the error and post an issue on Monday, since currently I don’t have access to the machine.</p>
<p>I guess in the end I will have to just run validation on all GPUs.</p>

Manuel_Alejandro_Dia: <p>What important information would be recommended for me to put on the issue <a class="mention" href="/u/rvarm1">@rvarm1</a> ?</p>
<p>Thanks!</p>

rvarm1: <p>It would be best if you could provide a minimal example script in which you call <code>barrier()</code> as you expect to do here and it fails in the issue description. Thank you!</p>

Manuel_Alejandro_Dia: <p><a href="https://github.com/pytorch/pytorch/issues/54059" rel="noopener nofollow ugc">Issue created!</a></p>
<p>I’m sorry it took me a day extra <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"></p>

mrshenli: <p>This is resolved, please see discussion in <a href="https://github.com/pytorch/pytorch/issues/54059" class="inline-onebox" rel="noopener nofollow ugc">Using torch.distributed.barrier() makes the whole code hang · Issue #54059 · pytorch/pytorch · GitHub</a></p>

Manuel_Alejandro_Dia: <p>As <a class="mention" href="/u/rvarm1">@rvarm1</a> suggested in the Github issue, the problem is solved by using the local model when running the validation, not the DDP one.</p>
<p>So instead of using:</p>
<pre><code class="lang-auto">evaluation_metrics = evaluate(model)
</code></pre>
<p>I should use:</p>
<pre><code class="lang-auto">evaluation_metrics = evaluate(model.module)
</code></pre>
<p>Thanks!</p>