test_fuse_optimizer_pass.py 4.0 KB
Newer Older
C
chengduo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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.
14
from simple_nets import simple_fc_net, fc_with_batchnorm, init_data
C
chengduo 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
from parallel_executor_test_base import TestParallelExecutorBase
import paddle.fluid as fluid
import paddle.fluid.core as core
import unittest
import os


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

    def _compare_fused_optimizer_ops(self,
                                     model,
                                     use_cuda,
                                     optimizer=fluid.optimizer.Adam):
        if use_cuda and not core.is_compiled_with_cuda():
            return
33
        img, label = init_data()
C
chengduo 已提交
34
        feed_dict = {"image": img, "label": label}
C
chengduo 已提交
35 36
        not_fuse_op_first_loss, not_fuse_op_last_loss = self.check_network_convergence(
            model,
C
chengduo 已提交
37
            feed_dict=feed_dict,
C
chengduo 已提交
38 39 40 41 42 43
            use_cuda=use_cuda,
            fuse_all_optimizer_ops=False,
            memory_opt=False,  # avoid the gradient's name changed in Python side.
            optimizer=optimizer)
        fuse_op_first_loss, fuse_op_last_loss = self.check_network_convergence(
            model,
C
chengduo 已提交
44
            feed_dict=feed_dict,
C
chengduo 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
            use_cuda=use_cuda,
            fuse_all_optimizer_ops=True,
            memory_opt=False,  # avoid the gradient's name changed in Python side.
            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_fused_optimizer_ops(simple_fc_net, True)
        self._compare_fused_optimizer_ops(simple_fc_net, False)

    def test_batchnorm_fc_with_fuse_op(self):
        self._compare_fused_optimizer_ops(fc_with_batchnorm, True)
61
        self._compare_fused_optimizer_ops(fc_with_batchnorm, False)
C
chengduo 已提交
62 63 64


class TestFuseSGDOps(TestFuseAdamOps):
C
chengduo 已提交
65
    def sgd_optimizer(self, learning_rate=1e-3):
C
chengduo 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        return fluid.optimizer.SGD(learning_rate=learning_rate)

    def test_simple_fc_with_fuse_op(self):
        self._compare_fused_optimizer_ops(
            simple_fc_net, True, optimizer=self.sgd_optimizer)
        self._compare_fused_optimizer_ops(
            simple_fc_net, False, optimizer=self.sgd_optimizer)

    def test_batchnorm_fc_with_fuse_op(self):
        self._compare_fused_optimizer_ops(
            fc_with_batchnorm, True, optimizer=self.sgd_optimizer)
        self._compare_fused_optimizer_ops(
            fc_with_batchnorm, False, optimizer=self.sgd_optimizer)


C
chengduo 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
class TestFuseMomentumOps(TestFuseAdamOps):
    def momentum_optimizer(self, learning_rate=1e-3):
        return fluid.optimizer.Momentum(
            learning_rate=learning_rate, momentum=0.1)

    def test_simple_fc_with_fuse_op(self):
        self._compare_fused_optimizer_ops(
            simple_fc_net, True, optimizer=self.momentum_optimizer)
        self._compare_fused_optimizer_ops(
            simple_fc_net, False, optimizer=self.momentum_optimizer)

    def test_batchnorm_fc_with_fuse_op(self):
        self._compare_fused_optimizer_ops(
            fc_with_batchnorm, True, optimizer=self.momentum_optimizer)
        self._compare_fused_optimizer_ops(
            fc_with_batchnorm, False, optimizer=self.momentum_optimizer)


C
chengduo 已提交
99 100
if __name__ == '__main__':
    unittest.main()