-
Notifications
You must be signed in to change notification settings - Fork 2
/
optimizer.py
191 lines (164 loc) · 5.94 KB
/
optimizer.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# --------------------------------------------------------
# Reversible Column Networks
# Copyright (c) 2022 Megvii Inc.
# Licensed under The Apache License 2.0 [see LICENSE for details]
# Written by Yuxuan Cai
# --------------------------------------------------------
import json
import numpy as np
from torch import optim as optim
import timm.optim.optim_factory as optim_factory
from apex.optimizers import FusedAdam, FusedLAMB
def build_optimizer(config, model):
"""
Build optimizer, set weight decay of normalization to 0 by default.
"""
skip = {}
skip_keywords = {}
if hasattr(model, "no_weight_decay"):
skip = model.no_weight_decay()
if hasattr(model, "no_weight_decay_keywords"):
skip_keywords = model.no_weight_decay_keywords()
if "revcol" in config.MODEL.TYPE:
parameters = param_groups_lrd(
model,
weight_decay=config.TRAIN.WEIGHT_DECAY,
no_weight_decay_list=[],
layer_decay=config.TRAIN.OPTIMIZER.LAYER_DECAY,
)
else:
parameters = set_weight_decay(model, skip, skip_keywords)
opt_lower = config.TRAIN.OPTIMIZER.NAME.lower()
optimizer = None
if opt_lower == "sgd":
optimizer = optim.SGD(
parameters,
momentum=config.TRAIN.OPTIMIZER.MOMENTUM,
nesterov=True,
lr=config.TRAIN.BASE_LR,
weight_decay=config.TRAIN.WEIGHT_DECAY,
)
elif opt_lower == "adamw":
optimizer = optim.AdamW(
parameters,
eps=config.TRAIN.OPTIMIZER.EPS,
betas=config.TRAIN.OPTIMIZER.BETAS,
lr=config.TRAIN.BASE_LR,
)
elif opt_lower == "lamb":
optimizer = optim_factory.Lamb(
parameters,
trust_clip=True,
lr=config.TRAIN.BASE_LR,
eps=config.TRAIN.OPTIMIZER.EPS,
betas=config.TRAIN.OPTIMIZER.BETAS,
# max_grad_norm=10.0,
)
elif opt_lower == "fused_lamb":
optimizer = FusedLAMB(
parameters,
eps=config.TRAIN.OPTIMIZER.EPS,
betas=config.TRAIN.OPTIMIZER.BETAS,
lr=config.TRAIN.BASE_LR,
weight_decay=config.TRAIN.WEIGHT_DECAY,
# max_grad_norm=10.0,
)
return optimizer
def set_weight_decay(model, skip_list=(), skip_keywords=()):
has_decay = []
no_decay = []
for name, param in model.named_parameters():
if not param.requires_grad or name in [
"linear_eval.weight",
"linear_eval.bias",
]:
continue # frozen weights
if (
len(param.shape) == 1
or name.endswith(".bias")
or (name in skip_list)
or check_keywords_in_name(name, skip_keywords)
):
no_decay.append(param)
# print(f"{name} has no weight decay")
else:
has_decay.append(param)
return [{"params": has_decay}, {"params": no_decay, "weight_decay": 0.0}]
def check_keywords_in_name(name, keywords=()):
isin = False
for keyword in keywords:
if keyword in name:
isin = True
return isin
def cal_model_depth(columns, layers):
depth = sum(layers)
dp = np.zeros((depth, columns))
dp[:, 0] = np.linspace(0, depth - 1, depth)
dp[0, :] = np.linspace(0, columns - 1, columns)
for i in range(1, depth):
for j in range(1, columns):
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + 1
dp = dp.astype(int)
return dp
def param_groups_lrd(
model, weight_decay=0.05, no_weight_decay_list=[], layer_decay=0.75
):
"""
Parameter groups for layer-wise lr decay
Following BEiT: https://github.com/microsoft/unilm/blob/master/beit/optim_factory.py#L58
"""
param_group_names = {}
param_groups = {}
dp = cal_model_depth(model.num_subnet, model.layers) + 1
num_layers = dp[-1][-1] + 1
layer_scales = list(layer_decay ** (num_layers - i) for i in range(num_layers + 1))
for n, p in model.named_parameters():
if not p.requires_grad:
continue
# no decay: all 1D parameters and model specific ones
# if p.ndim == 1 or n in no_weight_decay_list or "norm" in n or n.endswith(".bias"): # or re.match('(.*).alpha.$', n):
if p.ndim == 1 or n in no_weight_decay_list: # or re.match('(.*).alpha.$', n):
g_decay = "no_decay"
this_decay = 0.0
else:
g_decay = "decay"
this_decay = weight_decay
layer_id = get_layer_id(n, dp, model.layers)
group_name = "layer_%d_%s" % (layer_id, g_decay)
if group_name not in param_group_names:
this_scale = layer_scales[layer_id]
param_group_names[group_name] = {
"lr_scale": this_scale,
"weight_decay": this_decay,
"params": [],
}
param_groups[group_name] = {
"lr_scale": this_scale,
"weight_decay": this_decay,
"params": [],
}
param_group_names[group_name]["params"].append(n)
param_groups[group_name]["params"].append(p)
# print("parameter groups: \n%s" % json.dumps(param_group_names, indent=2))
return list(param_groups.values())
def get_layer_id(n, dp, layers):
if n.startswith("subnet"):
name_part = n.split(".")
subnet = int(name_part[0][6:])
if name_part[1].startswith("alpha"):
id = dp[0][subnet]
else:
level = int(name_part[1][-1])
if name_part[2].startswith("blocks"):
sub = int(name_part[3])
if sub > layers[level] - 1:
sub = layers[level] - 1
block = sum(layers[:level]) + sub
if name_part[2].startswith("fusion"):
block = sum(layers[:level])
id = dp[block][subnet]
elif n.startswith("stem"):
id = 0
else:
id = dp[-1][-1] + 1
return id