test_fuse_elewise_add_act_pass.py 3.0 KB
Newer Older
C
chengduo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
from simple_nets import simple_fc_net, fc_with_batchnorm, init_data
C
chengduo 已提交
16 17 18 19 20 21 22 23 24 25 26 27
from parallel_executor_test_base import TestParallelExecutorBase
import paddle.fluid as fluid
import paddle.fluid.core as core
import unittest
import os


class TestMNIST(TestParallelExecutorBase):
    @classmethod
    def setUpClass(cls):
        os.environ['CPU_NUM'] = str(4)

28
    def _compare_fuse_elewise_add_act_ops(self, model, use_cuda):
C
chengduo 已提交
29 30
        if use_cuda and not core.is_compiled_with_cuda():
            return
31
        img, label = init_data()
C
chengduo 已提交
32 33 34 35 36 37 38

        def _optimizer(learning_rate=1e-6):
            optimizer = fluid.optimizer.SGD(
                learning_rate=learning_rate,
                regularization=fluid.regularizer.L2Decay(1e-6))
            return optimizer

39 40
        # NOTE(dzh):
        # need to make it compatible with elewise fuse act
41 42 43
        # FIXME (liuwei12)
        # the new memory optimize strategy will crash this unittest
        # add enable_inplace=False here to force pass the unittest
C
chengduo 已提交
44 45 46 47 48 49
        not_fuse_op_first_loss, not_fuse_op_last_loss = self.check_network_convergence(
            model,
            feed_dict={"image": img,
                       "label": label},
            use_cuda=use_cuda,
            fuse_elewise_add_act_ops=False,
50
            use_ir_memory_optimize=False,
51
            enable_inplace=False,
C
chengduo 已提交
52 53 54 55 56 57 58
            optimizer=_optimizer)
        fuse_op_first_loss, fuse_op_last_loss = self.check_network_convergence(
            model,
            feed_dict={"image": img,
                       "label": label},
            use_cuda=use_cuda,
            fuse_elewise_add_act_ops=True,
59
            use_ir_memory_optimize=False,
60
            enable_inplace=False,
C
chengduo 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
            optimizer=_optimizer)

        for loss in zip(not_fuse_op_first_loss, fuse_op_first_loss):
            self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)
        for loss in zip(not_fuse_op_last_loss, fuse_op_last_loss):
            self.assertAlmostEquals(loss[0], loss[1], delta=1e-6)

    def test_simple_fc_with_fuse_op(self):
        self._compare_fuse_elewise_add_act_ops(simple_fc_net, True)
        self._compare_fuse_elewise_add_act_ops(simple_fc_net, False)

    def test_batchnorm_fc_with_fuse_op(self):
        self._compare_fuse_elewise_add_act_ops(fc_with_batchnorm, True)
        self._compare_fuse_elewise_add_act_ops(fc_with_batchnorm, False)


if __name__ == '__main__':
    unittest.main()