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

[2.0API] fix weight_norm support negative dim and unittest in convert_syncbn #27108

Merged
merged 2 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions python/paddle/fluid/tests/unittests/test_dygraph_weight_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ def test_check_output(self):
before_weight = linear.weight.numpy()
if self.dim == None:
self.dim = -1

if self.dim != -1:
self.dim = (self.dim + len(before_weight)) % len(before_weight)
wn = weight_norm(linear, dim=self.dim)
outputs = []
for name, data in self.data.items():
Expand Down Expand Up @@ -158,6 +161,13 @@ def init_test_case(self):
self.dim = 3


class TestDygraphWeightNormCase4(TestDygraphWeightNorm):
def init_test_case(self):
self.batch_size = 3
self.data_desc = (['x', [2, 3, 3]], )
self.dim = -3


class TestDygraphRemoveWeightNorm(unittest.TestCase):
def setUp(self):
self.init_test_case()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,15 @@ def test_convert(self):
return

with program_guard(Program(), Program()):
compare_model = paddle.nn.Sequential(
paddle.nn.Conv2d(3, 5, 3), paddle.nn.BatchNorm2d(5))
model = paddle.nn.Sequential(
paddle.nn.Conv2d(3, 5, 3), paddle.nn.BatchNorm2d(5))
sync_model = paddle.nn.SyncBatchNorm.convert_sync_batchnorm(model)
for idx, sublayer in enumerate(model.sublayers()):
model = paddle.nn.SyncBatchNorm.convert_sync_batchnorm(model)
for idx, sublayer in enumerate(compare_model.sublayers()):
if isinstance(sublayer, paddle.nn.BatchNorm2d):
self.assertEqual(
isinstance(sync_model[idx], paddle.nn.SyncBatchNorm),
True)
isinstance(model[idx], paddle.nn.SyncBatchNorm), True)


if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/nn/layer/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,10 +1130,10 @@ def convert_sync_batchnorm(cls, layer):
"""
layer_output = layer
if isinstance(layer, _BatchNormBase):
layer_output = SyncBatchNorm(layer._num_features, layer._epsilon,
layer._momentum, layer._weight_attr,
layer._bias_attr, layer._data_format,
layer._name)
layer_output = SyncBatchNorm(
layer._num_features, layer._momentum, layer._epsilon,
layer._weight_attr, layer._bias_attr, layer._data_format,
layer._track_running_stats, layer._name)

if layer._weight_attr != False and layer._bias_attr != False:
with no_grad():
Expand Down
8 changes: 8 additions & 0 deletions python/paddle/nn/utils/weight_norm_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ def apply(layer, name, dim):
if dim is None:
dim = -1

# support dim is negative numeber, (dim = -1) == (dim = None)
weight_dim = len(layer._parameters[name].shape)
assert (
dim < weight_dim and dim >= -1 * weight_dim
), "dim must set between [-R, R), R means the dimension of weight."
if dim != -1:
dim = (dim + weight_dim) % weight_dim

fn = WeightNorm(name, dim)

w = getattr(layer, name)
Expand Down