From 0b27ba5ab85856aaade58f1c6e17f9a0a4a63d9a Mon Sep 17 00:00:00 2001 From: zkh2016 Date: Tue, 14 Jun 2022 12:26:37 +0000 Subject: [PATCH] test SyncBatchNorm --- .../tests/unittests/test_sparse_norm_op.py | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_sparse_norm_op.py b/python/paddle/fluid/tests/unittests/test_sparse_norm_op.py index 8eccefed6ef64..3283d6317a620 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_norm_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_norm_op.py @@ -16,6 +16,7 @@ import unittest import numpy as np import paddle +from paddle.incubate.sparse import nn import paddle.fluid as fluid from paddle.fluid.framework import _test_eager_guard import copy @@ -56,11 +57,10 @@ def test(self): # test backward sparse_y.backward(sparse_y) - assert np.allclose( - dense_x.grad.flatten().numpy(), - sparse_x.grad.values().flatten().numpy(), - atol=1e-5, - rtol=1e-5) + assert np.allclose(dense_x.grad.flatten().numpy(), + sparse_x.grad.values().flatten().numpy(), + atol=1e-5, + rtol=1e-5) fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": False}) def test_error_layout(self): @@ -86,5 +86,22 @@ def test2(self): # [1, 6, 6, 6, 3] +class TestConvertSyncBatchNorm(unittest.TestCase): + + def test_convert(self): + base_model = paddle.nn.Sequential(nn.Conv3D(3, 5, 3), nn.BatchNorm(5), + nn.BatchNorm(5)) + + model = paddle.nn.Sequential( + nn.Conv3D(3, 5, 3), nn.BatchNorm(5), + nn.BatchNorm(5, + weight_attr=fluid.ParamAttr(name='bn.scale'), + bias_attr=fluid.ParamAttr(name='bn.bias'))) + model = nn.SyncBatchNorm.convert_sync_batchnorm(model) + for idx, sublayer in enumerate(base_model.sublayers()): + if isinstance(sublayer, nn.BatchNorm): + self.assertEqual(isinstance(model[idx], nn.SyncBatchNorm), True) + + if __name__ == "__main__": unittest.main()