# About Synchronize Batch Norm across Multi-GPU Implementation

**URL:** <https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129>\
**Category:** Uncategorized\
**Created:** [July 23, 2017, 1:40pm UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129 "2017-07-23T13:40:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ycszen](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ycszen/32/1401_2.png) [@ycszen](https://discuss.pytorch.org/u/ycszen)\
**Post date:** [July 23, 2017, 1:40pm UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/1 "2017-07-23T13:40:50Z")

</div>

I want to implement synchronize batch norm across multi-GPU. How can I do it? I think I should synchronize its mean and variance both forward and backward pass, so can I use the `register_hook` ? Can someone give me some advise? Thank you.

---

<div class="post-metadata">

**Author:** ![ajbrock](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ajbrock/32/139_2.png) [@ajbrock](https://discuss.pytorch.org/u/ajbrock)\
**Post date:** [July 23, 2017, 1:45pm UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/2 "2017-07-23T13:45:01Z")

</div>

I’m not sure on the implementation, but for what it’s worth [it’s actually recommended NOT](https://arxiv.org/abs/1706.02677) to try and synchronize batchnorm for multi-node training. Not to deter you from exploring it, but unless you’re specifically investigating it I’d say it’s not the best place to devote effort for performance improvements.

---

<div class="post-metadata">

**Author:** ![uapatira](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/uapatira/32/670_2.png) [@uapatira](https://discuss.pytorch.org/u/uapatira)\
**Post date:** [July 23, 2017, 10:21pm UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/3 "2017-07-23T22:21:54Z")

</div>

Hmmm that’s weird. Isn’t that the exact opposite of DeepLab v3 and PSPNet, where their _secret-sauce_ is essentially the fine-tuning of BN parameters on the VOC dataset across multiple GPUs?

---

<div class="post-metadata">

**Author:** ![ycszen](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ycszen/32/1401_2.png) [@ycszen](https://discuss.pytorch.org/u/ycszen)\
**Post date:** [July 24, 2017, 4:29am UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/4 "2017-07-24T04:29:06Z")

</div>

Yes, the PSPNet use this BN. I am re-implementing the network and train process. So I have to implement this BN.

---

<div class="post-metadata">

**Author:** ![HANG\_ZHANG](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hang_zhang/32/645_2.png) [@HANG\_ZHANG](https://discuss.pytorch.org/u/HANG_ZHANG)\
**Post date:** [August 9, 2017, 9:32pm UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/5 "2017-08-09T21:32:02Z")

</div>

A brief description of implementing synchronize BN:

> **[Implementing Synchronized Multi-GPU Batch Normalization, Do It Exactly Right](http://hangzh.com/SynchronizeBN/)**
>
> Hang Zhang, Rutgers University, Computer Vision – :white\_check\_mark: Please checkout the new post.

---

<div class="post-metadata">

**Author:** ![ycszen](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ycszen/32/1401_2.png) [@ycszen](https://discuss.pytorch.org/u/ycszen)\
**Post date:** [August 10, 2017, 3:39am UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/6 "2017-08-10T03:39:39Z")

</div>

Thank you. I just think about how to communicate across GPUs.

---

<div class="post-metadata">

**Author:** ![HANG\_ZHANG](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hang_zhang/32/645_2.png) [@HANG\_ZHANG](https://discuss.pytorch.org/u/HANG_ZHANG)\
**Post date:** [August 10, 2017, 6:16pm UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/7 "2017-08-10T18:16:23Z")

</div>

Basically, you need to customize the DataParallel and enable the cross GPU communication for each layer. Good luck.

---

<div class="post-metadata">

**Author:** ![HANG\_ZHANG](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hang_zhang/32/645_2.png) [@HANG\_ZHANG](https://discuss.pytorch.org/u/HANG_ZHANG)\
**Post date:** [November 9, 2017, 2:38pm UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/8 "2017-11-09T14:38:44Z")

</div>

Just to update, I have released the Synchronized Mulit-GPU Batch Normalization [http://hangzh.com/PyTorch-Encoding/syncbn.html](http://hangzh.com/PyTorch-Encoding/syncbn.html)

---

<div class="post-metadata">

**Author:** ![meetshah1995](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/meetshah1995/32/433_2.png) [@meetshah1995](https://discuss.pytorch.org/u/meetshah1995)\
**Post date:** [December 23, 2017, 7:28am UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/9 "2017-12-23T07:28:56Z")

</div>

Any updates on a Synchronized Batch Norm in Pytorch?

ChainerMN has implemented one [here](http://chainermn.readthedocs.io/en/latest/reference/index.html#chainermn.links.MultiNodeBatchNormalization)

---

<div class="post-metadata">

**Author:** ![zhanghang1989](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zhanghang1989/32/2214_2.png) [@zhanghang1989](https://discuss.pytorch.org/u/zhanghang1989)\
**Post date:** [April 13, 2018, 6:49am UTC](https://discuss.pytorch.org/t/about-synchronize-batch-norm-across-multi-gpu-implementation/5129/10 "2018-04-13T06:49:19Z")

</div>

Please checkout the PyTorch compatible Synchronized Cross-GPU [encoding.nn.BatchNorm2d](http://hangzh.com/PyTorch-Encoding/syncbn.html) and the [example](https://github.com/zhanghang1989/PyTorch-SyncBatchNorm).
