meta_optimizer_base.py 3.5 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 31
    def _set_auxiliary_var(self, key, val):
        super()._set_auxiliary_var(key, val)
        self.inner_opt._set_auxiliary_var(key, val)

32 33 34
    def _set_basic_info(
        self, loss, role_maker, user_defined_optimizer, user_defined_strategy
    ):
35 36 37 38 39
        self.loss = loss
        self.role_maker = role_maker
        self.user_defined_optimizer = user_defined_optimizer
        self.user_defined_strategy = user_defined_strategy

40
    def _update_inner_optimizer(self, optimizer):
41 42 43 44 45 46 47 48 49 50 51
        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
52
        return False
53

D
Dong Daxiang 已提交
54
    def _disable_strategy(self, dist_strategy):
55 56
        raise NotImplementedError(
            "you should implement disable strategy in {}".format(
57 58 59
                type(self).__name__
            )
        )
D
Dong Daxiang 已提交
60

61
    def _enable_strategy(self, dist_strategy, context=None):
62 63
        raise NotImplementedError(
            "you should implement enable strategy in {}".format(
64 65 66
                type(self).__name__
            )
        )
67

68 69 70
    def apply_gradients(self, params_grads):
        return self.inner_opt.apply_gradients(params_grads=params_grads)

71 72 73 74 75 76 77 78 79 80 81
    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
        )
82 83

    def apply_optimize(self, loss, startup_program, params_grads):
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        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
        )
101 102

        return optimize_ops, params_grads
103

104 105 106 107 108 109
    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
        )
110
        return optimize_ops, params_grads