-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile.py
222 lines (195 loc) · 7.09 KB
/
profile.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import numpy as np
import time
import torch
import torch.nn as nn
model_profiling_hooks = []
model_profiling_speed_hooks = []
name_space = 95
params_space = 15
macs_space = 15
seconds_space = 15
num_forwards = 10
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
self.start = None
self.end = None
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
self.time = self.end - self.start
if self.verbose:
print('Elapsed time: %f ms.' % self.time)
def get_params(self):
"""get number of params in module"""
return np.sum(
[np.prod(list(w.size())) for w in self.parameters()])
def run_forward(self, input):
with Timer() as t:
for _ in range(num_forwards):
self.forward(*input)
torch.cuda.synchronize()
return int(t.time * 1e9 / num_forwards)
def conv_module_name_filter(name):
"""filter module name to have a short view"""
filters = {
'kernel_size': 'k',
'stride': 's',
'padding': 'pad',
'bias': 'b',
'groups': 'g',
}
for k in filters:
name = name.replace(k, filters[k])
return name
def module_profiling(self, input, output, verbose):
try:
if isinstance(input[0], torch.Tensor):
ins = input[0].size()
else:
ins = input[0][0].size()
except:
raise ValueError('invalid input: {}'.format(input))
if isinstance(output, list):
outs = output[-1].size()
else:
outs = output.size()
# NOTE: There are some difference between type and isinstance, thus please
# be careful.
t = type(self)
if isinstance(self, nn.Conv2d):
self.n_macs = (ins[1] * outs[1] *
self.kernel_size[0] * self.kernel_size[1] *
outs[2] * outs[3] // self.groups) * outs[0]
self.n_params = get_params(self)
self.n_seconds = run_forward(self, input)
self.name = conv_module_name_filter(self.__repr__())
elif isinstance(self, nn.ConvTranspose2d):
self.n_macs = (ins[1] * outs[1] *
self.kernel_size[0] * self.kernel_size[1] *
outs[2] * outs[3] // self.groups) * outs[0]
self.n_params = get_params(self)
self.n_seconds = run_forward(self, input)
self.name = conv_module_name_filter(self.__repr__())
elif isinstance(self, nn.Linear):
self.n_macs = ins[1] * outs[1] * outs[0]
self.n_params = get_params(self)
self.n_seconds = run_forward(self, input)
self.name = self.__repr__()
elif isinstance(self, nn.AvgPool2d):
# NOTE: this function is correct only when stride == kernel size
self.n_macs = ins[1] * ins[2] * ins[3] * ins[0]
self.n_params = 0
self.n_seconds = run_forward(self, input)
self.name = self.__repr__()
elif isinstance(self, nn.AdaptiveAvgPool2d):
# NOTE: this function is correct only when stride == kernel size
self.n_macs = ins[1] * ins[2] * ins[3] * ins[0]
self.n_params = 0
self.n_seconds = run_forward(self, input)
self.name = self.__repr__()
else:
# This works only in depth-first travel of modules.
self.n_macs = 0
self.n_params = 0
self.n_seconds = 0
num_children = 0
self.name = self.__repr__()[:10]
for m in self.children():
if isinstance(m, list) or isinstance(m, nn.Sequential) or isinstance(m, nn.ModuleList):
for i in m:
self.n_macs += getattr(i, 'n_macs', 0)
self.n_params += getattr(i, 'n_params', 0)
self.n_seconds += getattr(i, 'n_seconds', 0)
num_children += 1
else:
self.n_macs += getattr(m, 'n_macs', 0)
self.n_params += getattr(m, 'n_params', 0)
self.n_seconds += getattr(m, 'n_seconds', 0)
num_children += 1
ignore_zeros_t = [
nn.BatchNorm2d, nn.Dropout2d, nn.Dropout, nn.Sequential,
nn.ReLU6, nn.ReLU, nn.MaxPool2d,
nn.modules.padding.ZeroPad2d, nn.modules.activation.Sigmoid,
]
if (not getattr(self, 'ignore_model_profiling', False) and
self.n_macs == 0 and
t not in ignore_zeros_t):
if verbose:
print(
'WARNING: leaf module {} has zero n_macs.'.format(type(self)))
return
if getattr(self, 'ignore_model_profiling', False) or t in ignore_zeros_t:
return
if verbose:
print(
self.name.ljust(name_space, ' ') +
'{:,}'.format(self.n_params).rjust(params_space, ' ') +
'{:,}'.format(self.n_macs).rjust(macs_space, ' ') +
'{:,}'.format(self.n_seconds).rjust(seconds_space, ' '))
return
def add_profiling_hooks(m, verbose):
global model_profiling_hooks
model_profiling_hooks.append(
m.register_forward_hook(lambda m, input, output: module_profiling(
m, input, output, verbose=verbose)))
def remove_profiling_hooks():
global model_profiling_hooks
for h in model_profiling_hooks:
h.remove()
model_profiling_hooks = []
def remove_profiling_value(m):
m.n_macs = 0
m.n_params = 0
m.n_seconds = 0
m.name = None
for child in m.children():
remove_profiling_value(child)
def model_profiling(model, height, width, batch=1, channel=3, use_cuda=True,
verbose=True):
""" Pytorch model profiling with input image size
(batch, channel, height, width).
The function exams the number of multiply-accumulates (n_macs).
Args:
model: pytorch model
height: int
width: int
batch: int
channel: int
use_cuda: bool
Returns:
macs: int
params: int
"""
model.eval()
data = torch.rand(batch, channel, height, width)
device = torch.device("cuda" if use_cuda else "cpu")
model = model.to(device)
data = data.to(device)
model.apply(lambda m: add_profiling_hooks(m, verbose=verbose))
if verbose:
print(
'Item'.ljust(name_space, ' ') +
'params'.rjust(macs_space, ' ') +
'macs'.rjust(macs_space, ' ') +
'nanosecs'.rjust(seconds_space, ' '))
if verbose:
print(''.center(
name_space + params_space + macs_space + seconds_space, '-'))
t = torch.zeros((1), dtype=torch.long, device=device)
model(data, t)
if verbose:
print(''.center(
name_space + params_space + macs_space + seconds_space, '-'))
print(
'Total'.ljust(name_space, ' ') +
'{:,}'.format(model.n_params).rjust(params_space, ' ') +
'{:,}'.format(model.n_macs).rjust(macs_space, ' ') +
'{:,}'.format(model.n_seconds).rjust(seconds_space, ' '))
remove_profiling_hooks()
macs = model.n_macs
param = model.n_params
model.apply(lambda m: remove_profiling_value(m))
return macs, param