# How to overlap backward and communication?

**URL:** <https://discuss.pytorch.org/t/how-to-overlap-backward-and-communication/30732>\
**Category:** autograd\
**Created:** [November 28, 2018, 3:34am UTC](https://discuss.pytorch.org/t/how-to-overlap-backward-and-communication/30732 "2018-11-28T03:34:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stgzr](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stgzr/32/7549_2.png) [@stgzr](https://discuss.pytorch.org/u/stgzr)\
**Post date:** [November 28, 2018, 3:34am UTC](https://discuss.pytorch.org/t/how-to-overlap-backward-and-communication/30732/1 "2018-11-28T03:34:30Z")

</div>

I am trying to train a CNN distributedly. As the data-parallel nodes increases, the communication time gets longer. So I think overlap backward() and all\_reduce() is a good way to reduce communication time. But I wonder how could I do this. Is there an easy way to do so?  
My PyTorch version is 0.4.0.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [November 28, 2018, 10:23am UTC](https://discuss.pytorch.org/t/how-to-overlap-backward-and-communication/30732/2 "2018-11-28T10:23:53Z")

</div>

Hi,

The version of DP in master should already start doing the all\_reduce of each parameter as soon as it’s gradient is computed. Effectively overlapping the rest of the backward and the communication.

---

<div class="post-metadata">

**Author:** ![stgzr](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stgzr/32/7549_2.png) [@stgzr](https://discuss.pytorch.org/u/stgzr)\
**Post date:** [November 29, 2018, 3:30am UTC](https://discuss.pytorch.org/t/how-to-overlap-backward-and-communication/30732/3 "2018-11-29T03:30:07Z")

</div>

Thank you!  
BTW, does the version 1.0rc1 aleady have the effective overlapping features?  
Could I just use the code below instead of DP module to do an overlapping operation?

> loss.backward()  
> dist.all\_reduce(flatten\_grads)

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [November 29, 2018, 10:39am UTC](https://discuss.pytorch.org/t/how-to-overlap-backward-and-communication/30732/4 "2018-11-29T10:39:35Z")

</div>

Yes the nightly build will have that.  
The code in the new DP is better than that as the `all_reduce` will start before the end of the `backward()` call.

---

<div class="post-metadata">

**Author:** ![stgzr](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stgzr/32/7549_2.png) [@stgzr](https://discuss.pytorch.org/u/stgzr)\
**Post date:** [January 4, 2019, 2:15am UTC](https://discuss.pytorch.org/t/how-to-overlap-backward-and-communication/30732/5 "2019-01-04T02:15:03Z")

</div>

Thanks. I tried several methods but I cannot get any improvements in PyTorch v1.0. So could you describe more details or code examples so that I can use this features?
