-
Notifications
You must be signed in to change notification settings - Fork 320
/
utils.py
203 lines (175 loc) · 6.71 KB
/
utils.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
192
193
194
195
196
197
198
199
200
201
202
203
# Copyright (c) 2021 PPViT 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.
"""utils for ViT
Contains AverageMeter for monitoring, get_exclude_from_decay_fn for training
and WarmupCosineScheduler for training
"""
import logging
import sys
import os
import math
import numpy as np
import paddle
import paddle.distributed as dist
def get_logger(file_path):
"""Set logging file and format, logs are written in 2 loggers, one local_logger records
the information on its own gpu/process, one master_logger records the overall/average
information over all gpus/processes.
Args:
file_path: str, folder path of the logger files to write
Return:
local_logger: python logger for each process
master_logger: python logger for overall processes (on node 0)
"""
local_rank = dist.get_rank()
filename = os.path.join(file_path, 'log_all.txt')
log_format = "%(asctime)s %(message)s"
logging.basicConfig(filename=filename, level=logging.INFO,
format=log_format, datefmt="%m%d %I:%M:%S %p")
# local_logger for each process/GPU
local_logger = logging.getLogger(f'local_{local_rank}')
filename = os.path.join(file_path, f'log_{local_rank}.txt')
fh = logging.FileHandler(filename)
fh.setFormatter(logging.Formatter(log_format))
local_logger.addHandler(fh)
## console
#sh = logging.StreamHandler(sys.stdout)
#sh.setFormatter(logging.Formatter(log_format))
#local_logger.addHandler(sh)
# master_logger records avg performance
if local_rank == 0:
master_logger = logging.getLogger('master')
# log.txt
filename = os.path.join(file_path, 'log.txt')
fh = logging.FileHandler(filename)
fh.setFormatter(logging.Formatter(log_format))
master_logger.addHandler(fh)
# console
sh = logging.StreamHandler(sys.stdout)
sh.setFormatter(logging.Formatter(log_format))
master_logger.addHandler(sh)
else:
master_logger = None
return local_logger, master_logger
def write_log(local_logger, master_logger, msg_local, msg_master=None, level='info'):
"""Write messages in loggers
Args:
local_logger: python logger, logs information on single gpu
master_logger: python logger, logs information over all gpus
msg_local: str, message to log on local_logger
msg_master: str, message to log on master_logger, if None, use msg_local, default: None
level: str, log level, in ['info', 'warning', 'fatal'], default: 'info'
"""
# write log to local logger
if local_logger:
if level == 'info':
local_logger.info(msg_local)
elif level == 'warning':
local_logger.warning(msg_local)
elif level == 'fatal':
local_logger.fatal(msg_local)
else:
raise ValueError("level must in ['info', 'warning', 'fatal']")
# write log to master logger on node 0
if master_logger and dist.get_rank() == 0:
if msg_master is None:
msg_master = msg_local
if level == 'info':
master_logger.info("MASTER_LOG " + msg_master)
elif level == 'warning':
master_logger.warning("MASTER_LOG " + msg_master)
elif level == 'fatal':
master_logger.fatal("MASTER_LOG " + msg_master)
else:
raise ValueError("level must in ['info', 'warning', 'fatal']")
def all_reduce_mean(x):
"""perform all_reduce on Tensor for gathering results from multi-gpus"""
world_size = dist.get_world_size()
if world_size > 1:
x_reduce = paddle.to_tensor(x)
dist.all_reduce(x_reduce)
x_reduce = x_reduce / world_size
return x_reduce.item()
return x
class AverageMeter():
""" Meter for monitoring losses"""
def __init__(self):
self.avg = 0
self.sum = 0
self.cnt = 0
self.reset()
def reset(self):
"""reset all values to zeros"""
self.avg = 0
self.sum = 0
self.cnt = 0
def update(self, val, n=1):
"""update avg by val and n, where val is the avg of n values"""
self.sum += val * n
self.cnt += n
self.avg = self.sum / self.cnt
def skip_weight_decay_fn(model, skip_list=[], filter_bias_and_bn=True):
""" Set params with no weight decay during the training
For certain params, e.g., positional encoding in ViT, weight decay
may not needed during the learning, this method is used to find
these params.
Args:
model: nn.Layer, model
skip_list: list, a list of params names which need to exclude
from weight decay, default: []
filter_bias_and_bn: bool, set True to exclude bias and bn in model, default: True
Returns:
exclude_from_weight_decay_fn: a function returns True if param
will be excluded from weight decay
"""
if len(skip_list) == 0 and not filter_bias_and_bn:
exclude_from_weight_decay_fn = None
else:
skip_list_all = []
for name, param in model.named_parameters():
if param.stop_gradient:
continue
if len(param.shape) == 1 or name.endswith('.bias') or name in skip_list:
skip_list_all.append(name)
def exclude_fn(param):
for name in skip_list_all:
if param == name:
return False
return True
exclude_from_weight_decay_fn = exclude_fn
return exclude_from_weight_decay_fn
def orthogonal(t, gain=1.):
if t.ndim < 2:
raise ValueError("Only tensors with 2 or more dimensions are supported")
gain = paddle.to_tensor(gain)
rows = t.shape[0]
cols = np.size(t) // rows
#cols = t.numel() // rows
flattened = paddle.normal(0, 1, [rows, cols])
if rows < cols:
flattened = flattened.transpose([1, 0])
# Compute the qr factorization
q, r = np.linalg.qr(flattened.cpu().numpy())
q = paddle.to_tensor(q)
r = paddle.to_tensor(r)
d = paddle.diag(r, 0)
ph = d.sign()
q *= ph
if rows < cols:
q = q.transpose([1, 0])
with paddle.no_grad():
t = q
#t.view_as(q).copy_(q)
t = t.multiply(gain)
return t