Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MultiNodeBatchNormalization #106

Merged
merged 20 commits into from
Aug 24, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add comments to highlight the differences from original Chainer imple…
…mentation
  • Loading branch information
iwiwi committed Aug 17, 2017
commit 629643e7bdb7824acbf36ff7beeb22c641025ea6
9 changes: 8 additions & 1 deletion chainermn/functions/batch_normalization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# This file is heavily based on Chainer's batch normalization implementation.
# See: chainer/functions/normalization/batch_normalization.py (dbb650)

import chainer
from chainer import cuda
from chainer import function
Expand Down Expand Up @@ -115,6 +118,7 @@ def forward(self, inputs):
if chainer.configuration.config.train:
axis = (0,) + tuple(range(head_ndim, x.ndim))

# ChainerMN diff (1/2) begins
mpi_comm = self.comm.mpi_comm
tmp = xp.empty(gamma.size * 2, dtype=x.dtype)
x.mean(axis=axis, out=tmp[:gamma.size])
Expand All @@ -129,6 +133,7 @@ def forward(self, inputs):
mean = tmp[:gamma.size]
sqmean = tmp[gamma.size:]
var = sqmean - xp.square(mean)
# ChainerMN diff (1/2) ends

var += self.eps
else:
Expand Down Expand Up @@ -194,7 +199,8 @@ def backward(self, inputs, grad_outputs):
# Note: If length of inputs is not 5, we must be in train mode.
assert chainer.configuration.config.train

# It is wrong to multiply m by mpi_comm.size
# ChainerMN diff (2/2) begins
# Note: It is wrong to multiply m by mpi_comm.size
# (instead of multiplying 1/size to gbeta, ggamma)
mpi_comm = self.comm.mpi_comm
tmp = xp.empty(gamma.size * 2, dtype=x.dtype)
Expand All @@ -208,6 +214,7 @@ def backward(self, inputs, grad_outputs):
tmp *= 1.0 / mpi_comm.size
gbeta = tmp[:gamma.size]
ggamma = tmp[gamma.size:]
# ChainerMN diff (2/2) ends

if xp is numpy:
gx = (gamma / self.std)[expander] * (
Expand Down