-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflops_utils_final.py
389 lines (328 loc) · 11 KB
/
flops_utils_final.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import os
import numpy as np
import torch
import torch.nn as nn
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from PIL import Image
import math
from models.utils import *
from quantization.quantize import Quantization
def get_density(w):
n_nonzeros = (w.abs()>0).sum().item()
return n_nonzeros * 1.0 / w.numel()
def get_nonzero_number(w):
n_nonzeros = (w.abs()>0).sum().item()
return n_nonzeros
def adder_tree(base, length):
count = 0
base_i = base
while(length>1):
count_i = length // 2
count += count_i * base_i
length = math.ceil(length/2)
base_i +=1
return count, base_i
def count_conv_linear_adder_tree(module, input, output):
input = input[0]
output = output[0]
if output.dim() == 3:
C, H, W = output.shape
else:
C = output.shape[0]
H = W = 1
####### Params
weight = module.weight.data
w = weight.view(-1)
w_element = w.numel()
w_nonzero = (w.abs()>0).sum().item()
# scale terms for scale and bias in Conv2D_INT/FC_INT (FP16)
scale_param = (2 * C + 1) / 2.
module.total_params = (w_element + w_nonzero * module.bit_width) / 32. + scale_param
####### FLOPs
# multiplication
multiply_bit_width = max(module.bit_width, module.act_bit_width)
k_element = w_element / C
density = get_density(w)
multiply_ops = density * H * W * C * k_element * multiply_bit_width / 32.
# addition
base_bit_width = module.bit_width + module.act_bit_width
if module.input_signed:
base_bit_width -= 1
addition_ops = 0
max_bit_global = 0
for kernel_idx in range(C):
w_k = weight[kernel_idx].view(-1)
n_el = get_nonzero_number(w_k)
addition_bits, max_bit = adder_tree(base_bit_width, n_el)
addition_ops += addition_bits * H * W
assert(max_bit<23)
if max_bit > max_bit_global:
max_bit_global = max_bit
# print(max_bit_global)
addition_ops /= 32.
# scale and bias in FP16 format
scale_bias_ops = C * H * W * 2 * 16. / 32.
module.total_ops += multiply_ops + addition_ops + scale_bias_ops
def count_conv_linear_adder_int16(module, input, output):
input = input[0]
output = output[0]
# print(output.dim())
if output.dim() == 3:
C, H, W = output.shape
else:
C = output.shape[0]
H = W = 1
####### Params
weight = module.weight.data
w = weight.view(-1)
w_element = w.numel()
w_nonzero = (w.abs()>0).sum().item()
# scale terms for scale and bias in Conv2D_INT/FC_INT (FP16)
scale_param = (2 * C + 1) / 2.
module.total_params = (w_element + w_nonzero * module.bit_width) / 32. + scale_param
####### FLOPs
# multiplication
multiply_bit_width = max(module.bit_width, module.act_bit_width)
k_element = w_element / C
density = get_density(w)
multiply_ops = density * H * W * C * k_element * multiply_bit_width / 32.
# addition
base_bit_width = module.bit_width + module.act_bit_width
if module.input_signed:
base_bit_width -= 1
addition_ops = 0
max_n_el = 0
for kernel_idx in range(C):
w_k = weight[kernel_idx].view(-1)
n_el = get_nonzero_number(w_k)
if n_el > max_n_el:
max_n_el = n_el
_, max_bit = adder_tree(base_bit_width, max_n_el)
max_bit = min(max_bit, 16)
print(max_bit)
for kernel_idx in range(C):
w_k = weight[kernel_idx].view(-1)
n_el = get_nonzero_number(w_k)
addition_ops += max_bit * (n_el - 1) * H * W
addition_ops /= 32.
# scale and bias in FP16 format
scale_bias_ops = C * H * W * 2 * 16. / 32.
module.total_ops += multiply_ops + addition_ops + scale_bias_ops
def count_conv_linear_adder_int(module, input, output):
input = input[0]
output = output[0]
# print(output.dim())
if output.dim() == 3:
C, H, W = output.shape
else:
C = output.shape[0]
H = W = 1
####### Params
weight = module.weight.data
w = weight.view(-1)
w_element = w.numel()
w_nonzero = (w.abs()>0).sum().item()
# scale terms for scale and bias in Conv2D_INT/FC_INT (FP16)
scale_param = (2 * C + 1) / 2.
module.total_params = (w_element + w_nonzero * module.bit_width) / 32. + scale_param
####### FLOPs
# multiplication
multiply_bit_width = max(module.bit_width, module.act_bit_width)
k_element = w_element / C
density = get_density(w)
multiply_ops = density * H * W * C * k_element * multiply_bit_width / 32.
# addition
base_bit_width = module.bit_width + module.act_bit_width
if module.input_signed:
base_bit_width -= 1
addition_ops = 0
max_n_el = 0
for kernel_idx in range(C):
w_k = weight[kernel_idx].view(-1)
n_el = get_nonzero_number(w_k)
if n_el > max_n_el:
max_n_el = n_el
_, max_bit = adder_tree(base_bit_width, max_n_el)
for kernel_idx in range(C):
w_k = weight[kernel_idx].view(-1)
n_el = get_nonzero_number(w_k)
addition_ops += max_bit * (n_el - 1) * H * W
addition_ops /= 32.
# scale and bias in FP16 format
scale_bias_ops = C * H * W * 2 * 16. / 32.
module.total_ops += multiply_ops + addition_ops + scale_bias_ops
def count_conv_linear_adder_fp16(module, input, output):
input = input[0]
output = output[0]
# print(output.dim())
if output.dim() == 3:
C, H, W = output.shape
else:
C = output.shape[0]
H = W = 1
####### Params
weight = module.weight.data
w = weight.view(-1)
w_element = w.numel()
w_nonzero = (w.abs()>0).sum().item()
# scale terms for scale and bias in Conv2D_INT/FC_INT (FP16)
scale_param = (2 * C + 1) / 2.
module.total_params = (w_element + w_nonzero * module.bit_width) / 32. + scale_param
####### FLOPs
# multiplication
multiply_bit_width = max(module.bit_width, module.act_bit_width)
k_element = w_element / C
density = get_density(w)
multiply_ops = density * H * W * C * k_element * multiply_bit_width / 32.
addition_ops = density * H * W * C * (k_element - 1) * 16. / 32.
# scale and bias in FP16 format
scale_bias_ops = C * H * W * 2 * 16. / 32.
module.total_ops += multiply_ops + addition_ops + scale_bias_ops
def count_conv_linear_adder_fp32(module, input, output):
input = input[0]
output = output[0]
# print(output.dim())
if output.dim() == 3:
C, H, W = output.shape
else:
C = output.shape[0]
H = W = 1
####### Params
weight = module.weight.data
w = weight.view(-1)
w_element = w.numel()
w_nonzero = (w.abs()>0).sum().item()
# scale terms for scale and bias in Conv2D_INT/FC_INT (FP16)
scale_param = (2 * C + 1) / 2.
module.total_params = (w_element + w_nonzero * module.bit_width) / 32. + scale_param
####### FLOPs
# multiplication
multiply_bit_width = max(module.bit_width, module.act_bit_width)
k_element = w_element / C
density = get_density(w)
multiply_ops = density * H * W * C * k_element * multiply_bit_width / 32.
addition_ops = density * H * W * C * (k_element - 1) * 32. / 32.
# scale and bias in FP16 format
scale_bias_ops = C * H * W * 2 * 16. / 32.
module.total_ops += multiply_ops + addition_ops + scale_bias_ops
def count_quantization(module, input, output):
input = input[0]
output = output[0]
num_elements = input.numel()
alpha = module.scale # min
if alpha == 1.0:
# print(alpha)
# omit .div(alpha), FP32 to UINT
quan_op = 0.0 # num_elements * 3.0 / 2.0
param = 0.0 # FP16
else:
# FP32 to INT, only count .div(alpha)
quan_op = num_elements * 1.0 / 2.0 # num_elements * 4.0 / 2.0
param = 1.0 / 2.0 # FP16
quan_op = 0.0 # num_elements * 3.0 / 2.0
param = 0.0 # FP16
module.total_params = param
module.total_ops += quan_op
def count_relu(module, input, output):
input = input[0]
output = output[0]
num_elements = output.numel()
total_ops = 1 * num_elements / 2.
module.total_ops += total_ops
module.total_params = 0.
def count_avgpool2d(module, input, output):
#count flops for single input
input = input[0]
output = output[0]
N, C, H, W = input.shape
assert(C == output.numel())
add_per_output = H * W - 1
mul_per_out = 1
MAC_per_out = add_per_output + mul_per_out
# total_ops = MAC_per_out * C / 2.
total_ops = MAC_per_out * C
module.total_ops += total_ops
module.total_params = 0
def count_sigmoid(module, input, output):
#count flops for single input
input = input[0]
output = output[0]
num_elements = output.numel()
# total_ops = 3 * num_elements / 2.0
total_ops = 3 * num_elements
module.total_ops += total_ops
module.total_params = 0
def count_swish(module, input, output):
#count flops for single input
input = input[0]
output = output[0]
num_elements = output.numel()
# total_ops = 4 * num_elements / 2.0
# total_ops = 4 * num_elements
total_ops = 4 * num_elements
module.total_ops += total_ops
module.total_params = 0
def count_eltwise(module, input, output):
#count flops for single input
x1 = input[0][0]
x2 = input[1][0]
output = output[0]
total_ops = max(x1.numel(), x2.numel()) / 2.0
module.total_ops += total_ops
module.total_params = 0
__hook_fn_dict_adder_fp32__ = {
nn.Conv2d: count_conv_linear_adder_fp32,
nn.ReLU: count_relu,
nn.Sigmoid: count_sigmoid,
Swish: count_swish,
PointProduct: count_eltwise,
PointAdd: count_eltwise,
AvgPool: count_avgpool2d,
nn.Linear: count_conv_linear_adder_fp32,
Quantization: count_quantization,
}
__hook_fn_dict_adder_fp16__ = {
nn.Conv2d: count_conv_linear_adder_fp16,
nn.ReLU: count_relu,
nn.Sigmoid: count_sigmoid,
Swish: count_swish,
PointProduct: count_eltwise,
PointAdd: count_eltwise,
AvgPool: count_avgpool2d,
nn.Linear: count_conv_linear_adder_fp16,
Quantization: count_quantization,
}
__hook_fn_dict_adder_int__ = {
nn.Conv2d: count_conv_linear_adder_int,
nn.ReLU: count_relu,
nn.Sigmoid: count_sigmoid,
Swish: count_swish,
PointProduct: count_eltwise,
PointAdd: count_eltwise,
AvgPool: count_avgpool2d,
nn.Linear: count_conv_linear_adder_int,
Quantization: count_quantization,
}
__hook_fn_dict_adder_int16__ = {
nn.Conv2d: count_conv_linear_adder_int16,
nn.ReLU: count_relu,
nn.Sigmoid: count_sigmoid,
Swish: count_swish,
PointProduct: count_eltwise,
PointAdd: count_eltwise,
AvgPool: count_avgpool2d,
nn.Linear: count_conv_linear_adder_int16,
Quantization: count_quantization,
}
__hook_fn_dict_adder_tree__ = {
nn.Conv2d: count_conv_linear_adder_tree,
nn.ReLU: count_relu,
nn.Sigmoid: count_sigmoid,
Swish: count_swish,
PointProduct: count_eltwise,
PointAdd: count_eltwise,
AvgPool: count_avgpool2d,
nn.Linear: count_conv_linear_adder_tree,
Quantization: count_quantization,
}