meta_optimizer_base.py 3.4 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
    def __init__(self, optimizer):
22 23 24 25 26
        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 = []
27

28 29 30
    def _set_basic_info(
        self, loss, role_maker, user_defined_optimizer, user_defined_strategy
    ):
31 32 33 34 35
        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
        raise NotImplementedError(
            "you should implement disable strategy in {}".format(
53 54 55
                type(self).__name__
            )
        )
D
Dong Daxiang 已提交
56

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

64 65 66
    def apply_gradients(self, params_grads):
        return self.inner_opt.apply_gradients(params_grads=params_grads)

67 68 69 70 71 72 73 74 75 76 77
    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
        )
78 79

    def apply_optimize(self, loss, startup_program, params_grads):
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        return self.inner_opt.apply_optimize(
            loss, startup_program=startup_program, params_grads=params_grads
        )

    def minimize_impl(
        self, loss, startup_program=None, parameter_list=None, no_grad_set=None
    ):
        params_grads = self.backward(
            loss,
            startup_program=startup_program,
            parameter_list=parameter_list,
            no_grad_set=no_grad_set,
        )

        optimize_ops = self.apply_optimize(
            loss, startup_program=startup_program, params_grads=params_grads
        )
97 98

        return optimize_ops, params_grads
99

100 101 102 103 104 105
    def minimize(
        self, loss, startup_program=None, parameter_list=None, no_grad_set=None
    ):
        optimize_ops, params_grads = self.minimize_impl(
            loss, startup_program, parameter_list, no_grad_set
        )
106
        return optimize_ops, params_grads