forked from NVIDIA/DeepLearningExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer.py
More file actions
149 lines (129 loc) · 5.26 KB
/
optimizer.py
File metadata and controls
149 lines (129 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
50
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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Copyright (c) 2022 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.
import sys
import logging
import paddle
from paddle import optimizer as optim
_EXCLUDE_FROM_DECAY = ["b_0", "norm"]
class AdamW:
"""
AdamW optimizer.
Args:
args(Namespace): Arguments obtained from ArgumentParser.
learning_rate(float|LRScheduler, optional): The learning rate used to update parameters. Default: 0.001
Can be a float value or a paddle.optimizer.lr.LRScheduler.
"""
def __init__(self, args, learning_rate):
self.learning_rate = learning_rate
self.beta1 = args.beta1
self.beta2 = args.beta2
self.epsilon = args.epsilon
self.weight_decay = args.weight_decay
self.multi_precision = args.amp
def __call__(self):
# not apply weight decay to all bias and layer_norm
def apply_decay_func(name):
return False if any(key in name
for key in _EXCLUDE_FROM_DECAY) else True
# add grad clipping to prevent exploding gradients
clip = paddle.nn.ClipGradByGlobalNorm(clip_norm=1.0)
opt = optim.AdamW(
learning_rate=self.learning_rate,
beta1=self.beta1,
beta2=self.beta2,
epsilon=self.epsilon,
weight_decay=self.weight_decay,
apply_decay_param_fun=apply_decay_func,
grad_clip=clip,
multi_precision=self.multi_precision)
return opt
class Lamb:
"""
Lamb optimizer.
Args:
args(Namespace): Arguments obtained from ArgumentParser.
learning_rate(float|LRScheduler, optional): The learning rate used to update parameters. Default: 0.001
Can be a float value or a paddle.optimizer.lr.LRScheduler.
"""
def __init__(self, args, learning_rate):
self.learning_rate = learning_rate
self.beta1 = args.beta1
self.beta2 = args.beta2
self.epsilon = args.epsilon
self.lamb_weight_decay = args.weight_decay
self.multi_precision = args.amp
def __call__(self):
# not apply weight decay to all bias and layer_norm
def exclude_from_decay_func(param):
return True if any(key in param.name
for key in _EXCLUDE_FROM_DECAY) else False
clip = paddle.nn.ClipGradByGlobalNorm(clip_norm=1.0)
opt = optim.Lamb(
learning_rate=self.learning_rate,
lamb_weight_decay=self.lamb_weight_decay,
beta1=self.beta1,
beta2=self.beta2,
epsilon=self.epsilon,
exclude_from_weight_decay_fn=exclude_from_decay_func,
grad_clip=clip)
opt._multi_precision = True if self.multi_precision else False
return opt
class DistributedFusedLamb:
"""
DistributedFusedLamb optimizer.
Args:
args(Namespace): Arguments obtained from ArgumentParser.
learning_rate(float|LRScheduler, optional): The learning rate used to update parameters. Default: 0.001
Can be a float value or a paddle.optimizer.lr.LRScheduler.
"""
def __init__(self, args, learning_rate):
self.learning_rate = learning_rate
self.beta1 = args.beta1
self.beta2 = args.beta2
self.epsilon = args.epsilon
self.lamb_weight_decay = args.weight_decay
self.gradient_merge_steps = args.gradient_merge_steps
def __call__(self):
# not apply weight decay to all bias and layer_norm
def exclude_from_decay_func(param):
return True if any(key in param.name
for key in _EXCLUDE_FROM_DECAY) else False
clip = paddle.nn.ClipGradByGlobalNorm(clip_norm=1.0)
opt = paddle.incubate.DistributedFusedLamb(
learning_rate=self.learning_rate,
lamb_weight_decay=self.lamb_weight_decay,
beta1=self.beta1,
beta2=self.beta2,
epsilon=self.epsilon,
exclude_from_weight_decay_fn=exclude_from_decay_func,
grad_clip=clip,
clip_after_allreduce=True,
is_grad_scaled_by_nranks=False,
use_master_param_norm=True,
gradient_accumulation_steps=self.gradient_merge_steps,
use_master_acc_grad=True)
return opt
def build_optimizer(args, lr):
"""
Build a raw optimizer with learning rate scheduler.
Args:
args(Namespace): Arguments obtained from ArgumentParser.
lr(paddle.optimizer.lr.LRScheduler): A LRScheduler used for training.
return:
optim(paddle.optimizer): A normal optmizer.
"""
optimizer_mod = sys.modules[__name__]
opt = getattr(optimizer_mod, args.optimizer)(args, learning_rate=lr)()
logging.info("build optimizer %s success..", opt)
return opt