__init__.py 6.31 KB
Newer Older
mohammad's avatar
mohammad committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# coding=utf-8
# Copyright (c) 2020, NVIDIA CORPORATION.  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.

from apex.optimizers import FusedAdam as Adam
17
from apex.optimizers import FusedSGD as SGD
mohammad's avatar
mohammad committed
18

mohammad's avatar
mohammad committed
19
from megatron import get_args
20
from megatron.model import LayerNorm
mohammad's avatar
mohammad committed
21

22
# >>>
23
# from .distributed_fused_adam import DistributedFusedAdam
Lawrence McAfee's avatar
Lawrence McAfee committed
24
from lutil import pax, tp
25
# <<<
mohammad's avatar
mohammad committed
26
from .grad_scaler import ConstantGradScaler, DynamicGradScaler
27
# >>>
28
29
from .optimizer import Float16OptimizerWithFloat16Params, FP32Optimizer
from .distrib_optimizer import DistributedOptimizer
30
# <<<
mohammad's avatar
mohammad committed
31

32
33
34
35
36
37
38
39
def get_param_groups(modules,
                     no_weight_decay_cond,
                     scale_lr_cond,
                     lr_mult):
    """creates param groups based on weight decay condition (regularized vs non regularized)
       and learning rate scale condition (args.lr vs lr_mult * args.lr)
       scale_lr_cond is used during finetuning where head of the network requires a scaled
       version of the base learning rate. 
mohammad's avatar
mohammad committed
40
    """
41
42
43
44
    wd_no_scale_lr = []
    wd_scale_lr = []
    no_wd_no_scale_lr = []
    no_wd_scale_lr = []
45
    for module in modules:
46
47
48
49
50
51
        for name, param in module.named_parameters():
            if not param.requires_grad:
                continue

            if no_weight_decay_cond is not None:
                no_wd = no_weight_decay_cond(name, param)
52
            else:
Vijay Korthikanti's avatar
Vijay Korthikanti committed
53
                # do not regularize biases nor Norm parameters
54
                no_wd = name.endswith(".bias") or len(param.shape) == 1
mohammad's avatar
mohammad committed
55

56
57
58
59
            if scale_lr_cond is not None:
                scale_lr = scale_lr_cond(name, param)
            else:
                scale_lr = False
mohammad's avatar
mohammad committed
60

61
62
63
64
65
66
67
68
            if not no_wd and not scale_lr:
                wd_no_scale_lr.append(param)
            elif not no_wd and scale_lr:
                wd_scale_lr.append(param)
            elif no_wd and not scale_lr:
                no_wd_no_scale_lr.append(param)
            else:
                no_wd_scale_lr.append(param)
mohammad's avatar
mohammad committed
69

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    param_groups = []
    if len(wd_no_scale_lr):
        param_groups.append({'params': wd_no_scale_lr, 'wd_mult': 1.0, 'lr_mult': 1.0})
    if len(wd_scale_lr):
        param_groups.append({'params': wd_scale_lr, 'wd_mult': 1.0, 'lr_mult': lr_mult})
    if len(no_wd_no_scale_lr):
        param_groups.append({'params': no_wd_no_scale_lr, 'wd_mult': 0.0, 'lr_mult': 1.0})
    if len(no_wd_scale_lr):
        param_groups.append({'params': no_wd_scale_lr, 'wd_mult': 0.0, 'lr_mult': lr_mult})

    return param_groups

def get_megatron_optimizer(model,
                           no_weight_decay_cond=None,
                           scale_lr_cond=None,
                           lr_mult=1.0):
mohammad's avatar
mohammad committed
86
87
88
    args = get_args()

    # Base optimizer.
89
90
91
92
93
    param_groups = get_param_groups(model,
                                    no_weight_decay_cond,
                                    scale_lr_cond,
                                    lr_mult)

Lawrence McAfee's avatar
Lawrence McAfee committed
94
    # >>>
Lawrence McAfee's avatar
Lawrence McAfee committed
95
    # params = [ p for m in model for p in m.parameters() ]
Lawrence McAfee's avatar
Lawrence McAfee committed
96
    # pax(0, {
Lawrence McAfee's avatar
Lawrence McAfee committed
97
    #     "params" : [ (p.tensor_model_parallel, tp(p)) for p in params ],
Lawrence McAfee's avatar
Lawrence McAfee committed
98
99
100
    # })
    # <<<

101
    # >>>
102
103
104
    # if args.use_distributed_optimizer:
    #     optimizer = DistributedFusedAdam(param_groups)
    # elif args.optimizer == 'adam':
105
    # <<<
106
    if args.optimizer == 'adam':
107
108
109
110
111
        optimizer = Adam(param_groups,
                         lr=args.lr,
                         weight_decay=args.weight_decay,
                         betas=(args.adam_beta1, args.adam_beta2),
                         eps=args.adam_eps)
Vijay Korthikanti's avatar
Vijay Korthikanti committed
112
    elif args.optimizer == 'sgd':
113
114
115
116
        optimizer = SGD(param_groups,
                        lr=args.lr,
                        weight_decay=args.weight_decay,
                        momentum=args.sgd_momentum)
Vijay Korthikanti's avatar
Vijay Korthikanti committed
117
118
    else:
        raise Exception('{} optimizer is not supported.'.format(
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
119
            args.optimizer))
mohammad's avatar
mohammad committed
120

Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    # Determine whether the params have main-grad field.
    params_have_main_grad = False
    if args.DDP_impl == 'local':
        params_have_main_grad = True

    if args.fp16 or args.bf16:

        # Grad scaler:
        #    if loss-scale is provided, instantiate the constant scaler.
        #    if we are using fp16 and loss-scale is not present, use a
        #       dynamic scaler.
        #    otherwise we are running in bf16 with no loss-scale so
        #       leave it as None.
        grad_scaler = None
mohammad's avatar
mohammad committed
135
136
137
138
139
        # Constant loss scale.
        if args.loss_scale:
            grad_scaler = ConstantGradScaler(args.loss_scale)
        # Dynamic loss scale.
        else:
Mohammad Shoeybi's avatar
Mohammad Shoeybi committed
140
141
142
143
144
145
146
147
148
            if args.fp16:
                grad_scaler = DynamicGradScaler(
                    initial_scale=args.initial_loss_scale,
                    min_scale=args.min_loss_scale,
                    growth_factor=2.0,
                    backoff_factor=0.5,
                    growth_interval=args.loss_scale_window,
                    hysteresis=args.hysteresis)

mohammad's avatar
mohammad committed
149
        # Megatron optimizer.
150
151
152
153
        # >>>
        opt_ty = Float16DistributedOptimizer \
            if args.use_distributed_optimizer \
            else Float16OptimizerWithFloat16Params
Lawrence McAfee's avatar
Lawrence McAfee committed
154
        opt = opt_ty(optimizer,
155
156
157
158
159
                      args.clip_grad,
                      args.log_num_zeros_in_grad,
                      params_have_main_grad,
                      args.use_contiguous_buffers_in_local_ddp,
                      args.bf16,
160
161
                      grad_scaler,
                      model)
Lawrence McAfee's avatar
Lawrence McAfee committed
162
163
164
165
166
        # >>>
        # opt.debug_main_param_sum(0, "after init")
        # opt.debug_main_grad_sum(0, "after init")
        # <<<
        return opt
167
        # <<<
mohammad's avatar
mohammad committed
168
169

    # FP32.
170
171
172
173
174
175
176
177
178
    # >>>
    opt_ty = Float32DistributedOptimizer \
        if args.use_distributed_optimizer \
           else Float32Optimizer
    return opt_ty(optimizer, args.clip_grad,
                  args.log_num_zeros_in_grad,
                  params_have_main_grad,
                  args.use_contiguous_buffers_in_local_ddp)
    # <<<