optimization.py 8.6 KB
Newer Older
T
tangjiji 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#    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.

""" text preprocess """

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import paddle.fluid as fluid

def manual_warmup_decay(learning_rate, warmup_steps, num_train_steps, decay_steps=[], lr_decay_ratio=0.1):
T
tangjiji 已提交
23
    """ Applies linear warmup of learning rate from 0 and keep constant."""
T
tangjiji 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    with fluid.default_main_program()._lr_schedule_guard():
        lr = fluid.layers.tensor.create_global_var(
            shape=[1],
            value=0.0,
            dtype='float32',
            persistable=True,
            name="scheduled_learning_rate")

        global_step = fluid.layers.learning_rate_scheduler._decay_step_counter(
        )
        with fluid.layers.control_flow.Switch() as switch:
            with switch.case(global_step < warmup_steps):
                warmup_lr = learning_rate * (global_step / warmup_steps)
                fluid.layers.tensor.assign(warmup_lr, lr)
            for i, step in enumerate(decay_steps):
                with switch.case(global_step < step):
                    decayed_lr = learning_rate * (global_step / global_step) * pow(lr_decay_ratio, i)
                    fluid.layers.tensor.assign(decayed_lr, lr)
            with switch.default():
                constant_lr = learning_rate * (global_step / global_step) * pow(lr_decay_ratio, len(decay_steps))
                fluid.layers.tensor.assign(constant_lr, lr)

        return lr


def linear_warmup_decay(learning_rate, warmup_steps, num_train_steps):
T
tangjiji 已提交
50
    """ Applies linear warmup of learning rate from 0 and decay to 0."""
T
tangjiji 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    with fluid.default_main_program()._lr_schedule_guard():
        lr = fluid.layers.tensor.create_global_var(
            shape=[1],
            value=0.0,
            dtype='float32',
            persistable=True,
            name="scheduled_learning_rate")

        global_step = fluid.layers.learning_rate_scheduler._decay_step_counter(
        )

        with fluid.layers.control_flow.Switch() as switch:
            with switch.case(global_step < warmup_steps):
                warmup_lr = learning_rate * (global_step / warmup_steps)
                fluid.layers.tensor.assign(warmup_lr, lr)
            with switch.default():
                decayed_lr = fluid.layers.learning_rate_scheduler.polynomial_decay(
                    learning_rate=learning_rate,
                    decay_steps=num_train_steps,
                    end_learning_rate=0.0,
                    power=1.0,
                    cycle=False)
                fluid.layers.tensor.assign(decayed_lr, lr)

        return lr

T
tangjiji 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

def layer_decay(param, param_last, learning_rate, decay_rate, text_layers, n_layers):
    """ layer_decay implementation """
    delta = param - param_last
    if "encoder_layer" in param.name and param.name.index("encoder_layer")==0:
        layer = int(param.name.split("_")[2])
        if layer >= text_layers:
            cur_layer = text_layers + (layer - text_layers) * 2 + 1
        else:
            cur_layer = layer
        ratio = decay_rate ** (n_layers - cur_layer)
        print("text_layer_name:", param.name, "\t", "ratio:", ratio)
        param_update = param + (ratio - 1) * delta
    elif "encoder_vlayer" in param.name and param.name.index("encoder_vlayer")==0:
        layer = int(param.name.split("_")[2])
        cur_layer = text_layers + (layer) * 2 + 1
        ratio = decay_rate ** (n_layers - cur_layer)
        param_update = param + (ratio - 1) * delta
        print("image_layer_name:", param.name, "\t", "ratio:", ratio)
    elif "encoder_colayer" in param.name and param.name.index("encoder_colayer")==0:
        layer = int(param.name.split("_")[2])
        cur_layer = text_layers + (layer) * 2
        ratio = decay_rate ** (n_layers - cur_layer)
        param_update = param + (ratio - 1) * delta
        print("co_layer_name:", param.name, "\t", "ratio:", ratio)
    elif "embedding" in param.name:
        ratio = decay_rate ** (n_layers + 1)
        param_update = param + (ratio - 1) * delta
    elif "image_emb" in param.name or "image_loc" in param.name:
        ratio = decay_rate ** (n_layers - text_layers + 1)
        param_update = param + (ratio - 1) * delta
    else:
        param_update = None
    return param_update

T
tangjiji 已提交
112 113 114 115 116 117 118 119 120 121
def optimization(loss,
                 warmup_steps,
                 num_train_steps,
                 learning_rate,
                 train_program,
                 startup_prog,
                 weight_decay,
                 scheduler='linear_warmup_decay',
                 decay_steps=[],
                 lr_decay_dict_file="",
T
tangjiji 已提交
122 123 124 125 126
                 lr_decay_ratio=0.1,
                 layer_decay_rate=0.0,
                 text_init_layers=18,
                 n_layers=30):
    """ optimization implementation """
T
tangjiji 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    if warmup_steps > 0:
        if scheduler == 'noam_decay':
            scheduled_lr = fluid.layers.learning_rate_scheduler \
             .noam_decay(1 / (warmup_steps * (learning_rate ** 2)),
                         warmup_steps)
        elif scheduler == 'linear_warmup_decay':
            scheduled_lr = linear_warmup_decay(learning_rate, warmup_steps,
                                               num_train_steps)
        elif scheduler == 'manual_warmup_decay':
            scheduled_lr = manual_warmup_decay(learning_rate, warmup_steps,
                                               num_train_steps, decay_steps, lr_decay_ratio)
        else:
            raise ValueError("Unkown learning rate scheduler, should be "
                             "'noam_decay' or 'linear_warmup_decay' or 'manual_warmup_decay'")
    else:
        scheduled_lr = fluid.layers.create_global_var(
            name=fluid.unique_name.generate("learning_rate"),
            shape=[1],
            value=learning_rate,
            dtype='float32',
            persistable=True)

    lr_decay_dict = {}
    if lr_decay_dict_file != "":
        with open(lr_decay_dict_file) as f:
            for line in f:
                param, decay_rate = line.strip().split('\t')
                lr_decay_dict[param] = float(decay_rate)

    for param in fluid.default_main_program().block(0).all_parameters():
        if param.name in lr_decay_dict:
            print (param.name, lr_decay_dict[param.name])
            param.optimize_attr['learning_rate'] = lr_decay_dict[param.name]

    optimizer = fluid.optimizer.Adam(learning_rate=scheduled_lr)
    optimizer._learning_rate_map[fluid.default_main_program(
    )] = scheduled_lr


    fluid.clip.set_gradient_clip(
        clip=fluid.clip.GradientClipByGlobalNorm(clip_norm=1.0))

    def exclude_from_weight_decay(name):
T
tangjiji 已提交
170
        """ parameters not use weight decay
T
tangjiji 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        """
        if name.find("layer_norm") > -1:
            return True
        bias_suffix = ["_bias", "_b", ".b_0"]
        for suffix in bias_suffix:
            if name.endswith(suffix):
                return True
        return False

    param_list = dict()

    for param in train_program.global_block().all_parameters():
        param_list[param.name] = param * 1.0
        param_list[param.name].stop_gradient = True

    _, param_grads = optimizer.minimize(loss)

T
tangjiji 已提交
188 189 190 191 192 193 194 195 196 197
    
    if layer_decay_rate > 0:
        for param, grad in param_grads:
            with param.block.program._optimized_guard(
                [param, grad]), fluid.framework.name_scope("layer_decay"):
                param_decay = layer_decay(param, param_list[param.name], scheduled_lr, 
                                  layer_decay_rate, text_init_layers, n_layers)
                if param_decay:
                    fluid.layers.assign(output=param, input=param_decay)
    
T
tangjiji 已提交
198 199 200 201 202 203 204 205 206 207 208
    if weight_decay > 0:
        for param, grad in param_grads:
            if exclude_from_weight_decay(param.name):
                continue
            with param.block.program._optimized_guard(
                [param, grad]), fluid.framework.name_scope("weight_decay"):
                updated_param = param - param_list[
                    param.name] * weight_decay * scheduled_lr * param.optimize_attr['learning_rate']
                fluid.layers.assign(output=param, input=updated_param)

    return scheduled_lr