-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprofile.py
More file actions
234 lines (181 loc) · 7.66 KB
/
profile.py
File metadata and controls
234 lines (181 loc) · 7.66 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from distutils.version import LooseVersion
from thop.vision.basic_hooks import *
from thop.rnn_hooks import *
from basic_hooks import *
from transformers.modeling_dsbert import *
import torch
import torch.nn as nn
import pdb
# logger = logging.getLogger(__name__)
# logger.setLevel(logging.INFO)
def prRed(skk): print("\033[91m{}\033[00m".format(skk))
def prGreen(skk): print("\033[92m{}\033[00m".format(skk))
def prYellow(skk): print("\033[93m{}\033[00m".format(skk))
if LooseVersion(torch.__version__) < LooseVersion("1.0.0"):
logging.warning(
"You are using an old version PyTorch {version}, which THOP is not going to support in the future.".format(
version=torch.__version__))
default_dtype = torch.float64
register_hooks = {
nn.ZeroPad2d: zero_ops, # padding does not involve any multiplication.
nn.Conv1d: count_convNd,
nn.Conv2d: count_convNd,
nn.Conv3d: count_convNd,
nn.ConvTranspose1d: count_convNd,
nn.ConvTranspose2d: count_convNd,
nn.ConvTranspose3d: count_convNd,
nn.BatchNorm1d: count_bn,
nn.BatchNorm2d: count_bn,
nn.BatchNorm3d: count_bn,
nn.ReLU: zero_ops,
nn.ReLU6: zero_ops,
nn.LeakyReLU: count_relu,
nn.MaxPool1d: zero_ops,
nn.MaxPool2d: zero_ops,
nn.MaxPool3d: zero_ops,
nn.AdaptiveMaxPool1d: zero_ops,
nn.AdaptiveMaxPool2d: zero_ops,
nn.AdaptiveMaxPool3d: zero_ops,
nn.AvgPool1d: count_avgpool,
nn.AvgPool2d: count_avgpool,
nn.AvgPool3d: count_avgpool,
nn.AdaptiveAvgPool1d: count_adap_avgpool,
nn.AdaptiveAvgPool2d: count_adap_avgpool,
nn.AdaptiveAvgPool3d: count_adap_avgpool,
nn.Linear: count_linear,
DynaLinear: count_dynalinear,
# BertSelfAttention: count_selfattention,
nn.Dropout: zero_ops,
nn.Upsample: count_upsample,
nn.UpsamplingBilinear2d: count_upsample,
nn.UpsamplingNearest2d: count_upsample,
nn.RNNCell: count_rnn_cell,
nn.GRUCell: count_gru_cell,
nn.LSTMCell: count_lstm_cell,
nn.RNN: count_rnn,
nn.GRU: count_gru,
nn.LSTM: count_lstm,
}
if LooseVersion(torch.__version__) >= LooseVersion("1.1.0"):
register_hooks.update({
nn.SyncBatchNorm: count_bn
})
def profile_origin(model, inputs, custom_ops=None, verbose=True):
handler_collection = []
types_collection = set()
if custom_ops is None:
custom_ops = {}
def add_hooks(m):
if len(list(m.children())) > 0:
return
if hasattr(m, "total_ops") or hasattr(m, "total_params"):
logging.warning("Either .total_ops or .total_params is already defined in %s. "
"Be careful, it might change your code's behavior." % str(m))
m.register_buffer('total_ops', torch.zeros(1, dtype=default_dtype))
m.register_buffer('total_params', torch.zeros(1, dtype=default_dtype))
for p in m.parameters():
m.total_params += torch.DoubleTensor([p.numel()])
m_type = type(m)
fn = None
if m_type in custom_ops: # if defined both op maps, use custom_ops to overwrite.
fn = custom_ops[m_type]
# if m_type not in types_collection and verbose:
# print("[INFO] Customize rule %s() %s." % (fn.__qualname__, m_type))
elif m_type in register_hooks:
fn = register_hooks[m_type]
# if m_type not in types_collection and verbose:
# print("[INFO] Register %s() for %s." % (fn.__qualname__, m_type))
# else:
# if m_type not in types_collection and verbose:
# prRed("[WARN] Cannot find rule for %s. Treat it as zero Macs and zero Params." % m_type)
if fn is not None:
handler = m.register_forward_hook(fn)
handler_collection.append(handler)
types_collection.add(m_type)
training = model.training
model.eval()
model.apply(add_hooks)
with torch.no_grad():
model(*inputs)
total_ops = 0
total_params = 0
for m in model.modules():
if len(list(m.children())) > 0: # skip for non-leaf module
continue
total_ops += m.total_ops
total_params += m.total_params
total_ops = total_ops.item()
total_params = total_params.item()
# reset model to original status
model.train(training)
for handler in handler_collection:
handler.remove()
# remove temporal buffers
for n, m in model.named_modules():
if len(list(m.children())) > 0:
continue
if "total_ops" in m._buffers:
m._buffers.pop("total_ops")
if "total_params" in m._buffers:
m._buffers.pop("total_params")
return total_ops, total_params
def profile(model: nn.Module, inputs, custom_ops=None, verbose=True, train=True):
#UPDATE
if not train:
register_hooks.update({
DynaLinear: count_dynalinear_inference
})
handler_collection = {}
types_collection = set()
if custom_ops is None:
custom_ops = {}
def add_hooks(m: nn.Module):
m.register_buffer('total_ops', torch.zeros(1, dtype=torch.float64))
m.register_buffer('total_params', torch.zeros(1, dtype=torch.float64))
# for p in m.parameters():
# m.total_params += torch.DoubleTensor([p.numel()])
m_type = type(m)
fn = None
if m_type in custom_ops: # if defined both op maps, use custom_ops to overwrite.
fn = custom_ops[m_type]
# if m_type not in types_collection and verbose:
# print("[INFO] Customize rule %s() %s." % (fn.__qualname__, m_type))
elif m_type in register_hooks:
fn = register_hooks[m_type]
# if m_type not in types_collection and verbose:
# print("[INFO] Register %s() for %s." % (fn.__qualname__, m_type))
# else:
# if m_type not in types_collection and verbose:
# prRed("[WARN] Cannot find rule for %s. Treat it as zero Macs and zero Params." % m_type)
if fn is not None:
handler_collection[m] = (m.register_forward_hook(fn), m.register_forward_hook(count_parameters))
types_collection.add(m_type)
prev_training_status = model.training
model.eval()
model.apply(add_hooks)
with torch.no_grad():
model(*inputs)
def dfs_count(module: nn.Module, prefix="\t") -> (int, int):
total_ops, total_params = 0, 0
for m in module.children():
# if not hasattr(m, "total_ops") and not hasattr(m, "total_params"): # and len(list(m.children())) > 0:
# m_ops, m_params = dfs_count(m, prefix=prefix + "\t")
# else:
# m_ops, m_params = m.total_ops, m.total_params
if m in handler_collection and not isinstance(m, (nn.Sequential, nn.ModuleList)):
m_ops, m_params = m.total_ops.item(), m.total_params.item()
else:
m_ops, m_params = dfs_count(m, prefix=prefix + "\t")
total_ops += m_ops
total_params += m_params
# print(prefix, module._get_name(), (total_ops.item(), total_params.item()))
return total_ops, total_params
total_ops, total_params = dfs_count(model)
# reset model to original status
model.train(prev_training_status)
for m, (op_handler, params_handler) in handler_collection.items():
op_handler.remove()
params_handler.remove()
m._buffers.pop("total_ops")
m._buffers.pop("total_params")
return total_ops, total_params