From 7fe5f9ccad399fb9548eab816f9c0eb486706e1b Mon Sep 17 00:00:00 2001 From: Zhang Ting Date: Fri, 6 Nov 2020 19:14:15 +0800 Subject: [PATCH] Fix unittest random failure (#28363) * fix random failure * use two input(x) * fix model --- .../unittests/test_fuse_bn_add_act_pass.py | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_fuse_bn_add_act_pass.py b/python/paddle/fluid/tests/unittests/test_fuse_bn_add_act_pass.py index 316c40971aa..f4cb53b31c5 100644 --- a/python/paddle/fluid/tests/unittests/test_fuse_bn_add_act_pass.py +++ b/python/paddle/fluid/tests/unittests/test_fuse_bn_add_act_pass.py @@ -122,24 +122,23 @@ class TestFusedBnAddActAPI(unittest.TestCase): param_attr=self.conv_param_attr1, bias_attr=False, data_format='NHWC') + bn1 = fluid.layers.batch_norm( + input=conv1_1, + param_attr=self.bn_param_attr1, + bias_attr=self.bn_bias_attr1, + act=None, + data_layout='NHWC') conv1_2 = fluid.layers.conv2d( - input=x, - filter_size=3, + input=conv1_1, + filter_size=1, num_filters=32, stride=1, - padding=1, act=None, param_attr=self.conv_param_attr2, bias_attr=False, data_format='NHWC') - bn1 = fluid.layers.batch_norm( - input=conv1_1, - param_attr=self.bn_param_attr1, - bias_attr=self.bn_bias_attr1, - act=None, - data_layout='NHWC') bn2 = fluid.layers.batch_norm( - input=conv1_2, + input=conv1_1, param_attr=self.bn_param_attr2, bias_attr=self.bn_bias_attr2, act=None, @@ -157,7 +156,7 @@ class TestFusedBnAddActAPI(unittest.TestCase): sgd, use_dynamic_loss_scaling=True, init_loss_scaling=128.0) sgd.minimize(loss) - return x, y, loss + return loss def check(self, place, use_cuda): paddle.seed(1) @@ -168,24 +167,27 @@ class TestFusedBnAddActAPI(unittest.TestCase): # build_fused_program: turn on fuse_bn_add_act_ops main_program = fluid.Program() startup_program = fluid.Program() - x, y, loss = self.build_origin_program(main_program, startup_program, - use_cuda) - feeder = fluid.DataFeeder(feed_list=[x, y], place=place) + loss = self.build_origin_program(main_program, startup_program, + use_cuda) build_strategy_fused = fluid.BuildStrategy() build_strategy_fused.fuse_bn_add_act_ops = True binary_fused = fluid.CompiledProgram(main_program).with_data_parallel( loss_name=loss.name, build_strategy=build_strategy_fused) - train_reader = paddle.batch( - paddle.dataset.mnist.train(), batch_size=batch_size) exe = fluid.Executor(place) loss_vals_fused = [] + x_data = [] + y_data = [] scope = fluid.Scope() with fluid.scope_guard(scope): exe.run(startup_program) for _ in range(iters): - data = next(train_reader()) + x = np.random.random((batch_size, 1, 28, 28)).astype("float32") + y = np.random.random((batch_size, 1)).astype("int64") + x_data.append(x) + y_data.append(y) loss_v = exe.run(binary_fused, - feed=feeder.feed(data), + feed={"x": x, + "y": y}, fetch_list=[loss]) loss_vals_fused.append(loss_v[0][0]) @@ -193,17 +195,15 @@ class TestFusedBnAddActAPI(unittest.TestCase): build_strategy = fluid.BuildStrategy() build_strategy.fuse_bn_add_act_ops = False binary = fluid.CompiledProgram(main_program).with_data_parallel( - loss_name=loss.name, build_strategy=build_strategy) - train_reader = paddle.batch( - paddle.dataset.mnist.train(), batch_size=batch_size) + loss_name=loss.name, build_strategy=build_strategy_fused) loss_vals = [] scope = fluid.Scope() with fluid.scope_guard(scope): exe.run(startup_program) - for _ in range(iters): - data = next(train_reader()) + for i in range(iters): loss_v = exe.run(binary, - feed=feeder.feed(data), + feed={"x": x_data[i], + "y": y_data[i]}, fetch_list=[loss]) loss_vals.append(loss_v[0][0]) @@ -222,16 +222,16 @@ class TestFusedBnAddActAPI(unittest.TestCase): place = fluid.CUDAPlace(0) x, y, loss = self.build_fused_program( main_program, startup_program, use_cuda=True) - feeder = fluid.DataFeeder(feed_list=[x, y], place=place) - train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=16) exe = fluid.Executor(place) scope = fluid.Scope() with fluid.scope_guard(scope): exe.run(startup_program) for _ in range(5): - data = next(train_reader()) + x = np.random.random((4, 1, 28, 28)).astype("float32") + y = np.random.random((4, 1)).astype("int64") loss_v = exe.run(main_program, - feed=feeder.feed(data), + feed={"x": x, + "y": y}, fetch_list=[loss]) -- GitLab