meta_optimizer_base.py 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2020 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 paddle.fluid.optimizer import Optimizer
16

17 18
__all__ = []

19 20

class MetaOptimizerBase(Optimizer):
21

22
    def __init__(self, optimizer):
23 24 25 26 27
        self.inner_opt = optimizer
        self._learning_rate = self.inner_opt._learning_rate
        self._learning_rate_map = self.inner_opt._learning_rate_map
        self.meta_optimizers_white_list = []
        self.meta_optimizers_black_list = []
28 29 30 31 32 33 34 35

    def _set_basic_info(self, loss, role_maker, user_defined_optimizer,
                        user_defined_strategy):
        self.loss = loss
        self.role_maker = role_maker
        self.user_defined_optimizer = user_defined_optimizer
        self.user_defined_strategy = user_defined_strategy

36
    def _update_inner_optimizer(self, optimizer):
37 38 39 40 41 42 43 44 45 46 47
        self.inner_opt = optimizer

    def _can_apply(self):
        return False

    def _is_graph_out(self):
        return False

    def _can_update(self, optimizer):
        if str(optimizer.__class__.__name__) in self.meta_optimizers_white_list:
            return True
48
        return False
49

D
Dong Daxiang 已提交
50
    def _disable_strategy(self, dist_strategy):
51 52 53
        raise NotImplementedError(
            "you should implement disable strategy in {}".format(
                type(self).__name__))
D
Dong Daxiang 已提交
54

55
    def _enable_strategy(self, dist_strategy, context=None):
56 57 58
        raise NotImplementedError(
            "you should implement enable strategy in {}".format(
                type(self).__name__))
59

60 61 62 63 64 65 66 67 68 69 70 71 72
    def apply_gradients(self, params_grads):
        return self.inner_opt.apply_gradients(params_grads=params_grads)

    def backward(self,
                 loss,
                 startup_program=None,
                 parameter_list=None,
                 no_grad_set=None,
                 callbacks=None):
        return self.inner_opt.backward(loss, startup_program, parameter_list,
                                       no_grad_set, callbacks)

    def apply_optimize(self, loss, startup_program, params_grads):
73 74 75
        return self.inner_opt.apply_optimize(loss,
                                             startup_program=startup_program,
                                             params_grads=params_grads)
76

77 78 79 80 81
    def minimize_impl(self,
                      loss,
                      startup_program=None,
                      parameter_list=None,
                      no_grad_set=None):
82 83 84 85
        params_grads = self.backward(loss,
                                     startup_program=startup_program,
                                     parameter_list=parameter_list,
                                     no_grad_set=no_grad_set)
86

87 88 89
        optimize_ops = self.apply_optimize(loss,
                                           startup_program=startup_program,
                                           params_grads=params_grads)
90 91

        return optimize_ops, params_grads
92 93 94 95 96 97

    def minimize(self,
                 loss,
                 startup_program=None,
                 parameter_list=None,
                 no_grad_set=None):
98 99 100
        optimize_ops, params_grads = self.minimize_impl(loss, startup_program,
                                                        parameter_list,
                                                        no_grad_set)
101
        return optimize_ops, params_grads