-
Notifications
You must be signed in to change notification settings - Fork 16
/
flop_count.py
267 lines (236 loc) · 9.29 KB
/
flop_count.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
# taken from detectron2 with a few modifications
# to include bmm and a few other ops
# https://github.com/facebookresearch/detectron2/blob/master/detectron2/utils/analysis.py
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import logging
import typing
from collections import Counter, defaultdict
import torch
import torch.nn as nn
from functools import partial
from jit_handles import (
addmm_flop_jit,
batchnorm_flop_jit,
conv_flop_jit,
einsum_flop_jit,
matmul_flop_jit,
bmm_flop_jit,
basic_binary_op_flop_jit,
rsqrt_flop_jit,
softmax_flop_jit,
dropout_flop_jit,
)
# A dictionary that maps supported operations to their flop count jit handles.
_SUPPORTED_OPS: typing.Dict[str, typing.Callable] = {
"aten::addmm": addmm_flop_jit,
"aten::_convolution": conv_flop_jit,
"aten::einsum": einsum_flop_jit,
"aten::matmul": matmul_flop_jit,
"aten::batch_norm": batchnorm_flop_jit,
"aten::bmm": bmm_flop_jit,
"aten::add": partial(basic_binary_op_flop_jit, name='aten::add'),
"aten::add_": partial(basic_binary_op_flop_jit, name='aten::add_'),
"aten::mul": partial(basic_binary_op_flop_jit, name='aten::mul'),
"aten::sub": partial(basic_binary_op_flop_jit, name='aten::sub'),
"aten::div": partial(basic_binary_op_flop_jit, name='aten::div'),
"aten::floor_divide": partial(basic_binary_op_flop_jit, name='aten::floor_divide'),
"aten::relu": partial(basic_binary_op_flop_jit, name='aten::relu'),
"aten::relu_": partial(basic_binary_op_flop_jit, name='aten::relu_'),
"aten::rsqrt": rsqrt_flop_jit,
"aten::softmax": softmax_flop_jit,
"aten::dropout": dropout_flop_jit,
}
# A list that contains ignored operations.
_IGNORED_OPS: typing.List[str] = [
"aten::Int",
"aten::__and__",
"aten::arange",
"aten::cat",
"aten::clamp",
"aten::clamp_",
"aten::contiguous",
"aten::copy_",
"aten::detach",
"aten::empty",
"aten::eq",
"aten::expand",
"aten::flatten",
"aten::floor",
"aten::full",
"aten::gt",
"aten::index",
"aten::index_put_",
"aten::max",
"aten::nonzero",
"aten::permute",
"aten::remainder",
"aten::reshape",
"aten::select",
"aten::size",
"aten::slice",
"aten::split_with_sizes",
"aten::squeeze",
"aten::t",
"aten::to",
"aten::transpose",
"aten::unsqueeze",
"aten::view",
"aten::zeros",
"aten::zeros_like",
"prim::Constant",
"prim::Int",
"prim::ListConstruct",
"prim::ListUnpack",
"prim::NumToTensor",
"prim::TupleConstruct",
]
_HAS_ALREADY_SKIPPED = False
def flop_count(
model: nn.Module,
inputs: typing.Tuple[object, ...],
whitelist: typing.Union[typing.List[str], None] = None,
customized_ops: typing.Union[
typing.Dict[str, typing.Callable], None
] = None,
measure_scope=None,
) -> typing.DefaultDict[str, float]:
"""
Given a model and an input to the model, compute the Gflops of the given
model. Note the input should have a batch size of 1.
Args:
model (nn.Module): The model to compute flop counts.
inputs (tuple): Inputs that are passed to `model` to count flops.
Inputs need to be in a tuple.
whitelist (list(str)): Whitelist of operations that will be counted. It
needs to be a subset of _SUPPORTED_OPS. By default, the function
computes flops for all supported operations.
customized_ops (dict(str,Callable)) : A dictionary contains customized
operations and their flop handles. If customized_ops contains an
operation in _SUPPORTED_OPS, then the default handle in
_SUPPORTED_OPS will be overwritten.
Returns:
defaultdict: A dictionary that records the number of gflops for each
operation.
"""
# Copy _SUPPORTED_OPS to flop_count_ops.
# If customized_ops is provided, update _SUPPORTED_OPS.
flop_count_ops = _SUPPORTED_OPS.copy()
if customized_ops:
flop_count_ops.update(customized_ops)
# If whitelist is None, count flops for all suported operations.
if whitelist is None:
whitelist_set = set(flop_count_ops.keys())
else:
whitelist_set = set(whitelist)
# Torch script does not support parallell torch models.
if isinstance(
model,
(nn.parallel.distributed.DistributedDataParallel, nn.DataParallel),
):
model = model.module # pyre-ignore
assert set(whitelist_set).issubset(
flop_count_ops
), "whitelist needs to be a subset of _SUPPORTED_OPS and customized_ops."
assert isinstance(inputs, tuple), "Inputs need to be in a tuple."
# Compatibility with torch.jit.
if hasattr(torch.jit, "get_trace_graph"):
trace, _ = torch.jit.get_trace_graph(model, inputs)
trace_nodes = trace.graph().nodes()
else:
with scope_name_workaround():
trace, _ = torch.jit._get_trace_graph(model, inputs)
# graph=torch.onnx._optimize_trace(trace, torch.onnx.OperatorExportTypes.ONNX)
trace_nodes = trace.nodes()
skipped_ops = Counter()
total_flop_counter = Counter()
if measure_scope is not None:
for node in trace_nodes:
if measure_scope in node.scopeName():
kind = node.kind()
if kind not in whitelist_set:
# If the operation is not in _IGNORED_OPS, count skipped operations.
if kind not in _IGNORED_OPS:
skipped_ops[kind] += 1
continue
handle_count = flop_count_ops.get(kind, None)
if handle_count is None:
continue
inputs, outputs = list(node.inputs()), list(node.outputs())
flops_counter = handle_count(inputs, outputs)
total_flop_counter += flops_counter
else:
for node in trace_nodes:
kind = node.kind()
if kind not in whitelist_set:
# If the operation is not in _IGNORED_OPS, count skipped operations.
if kind not in _IGNORED_OPS:
skipped_ops[kind] += 1
continue
handle_count = flop_count_ops.get(kind, None)
if handle_count is None:
continue
inputs, outputs = list(node.inputs()), list(node.outputs())
flops_counter = handle_count(inputs, outputs)
total_flop_counter += flops_counter
global _HAS_ALREADY_SKIPPED
if len(skipped_ops) > 0 and not _HAS_ALREADY_SKIPPED:
_HAS_ALREADY_SKIPPED = True
for op, freq in skipped_ops.items():
logging.warning("Skipped operation {} {} time(s)".format(op, freq))
# Convert flop count to gigaflops.
final_count = defaultdict(float)
for op in total_flop_counter:
final_count[op] = total_flop_counter[op] / 1e9
return final_count
def print_table(rows, header=['Operation', 'OPS']):
r"""Simple helper function to print a list of lists as a table
:param rows: a :class:`list` of :class:`list` containing the data to be printed. Each entry in the list
represents an individual row
:param input: (optional) a :class:`list` containing the header of the table
"""
if len(rows) == 0:
return
col_max = [max([len(str(val[i])) for val in rows]) + 3 for i in range(len(rows[0]))]
row_format = ''.join(["{:<" + str(length) + "}" for length in col_max])
if len(header) > 0:
print(row_format.format(*header))
print(row_format.format(*['-' * (val - 2) for val in col_max]))
for row in rows:
print(row_format.format(*row))
print(row_format.format(*['-' * (val - 3) for val in col_max]))
# Workaround for scopename in pytorch 1.4 and newer
# see: https://github.com/pytorch/pytorch/issues/33463
class scope_name_workaround(object):
def __init__(self):
self.backup = None
def __enter__(self):
def _tracing_name(self_, tracing_state):
if not tracing_state._traced_module_stack:
return None
module = tracing_state._traced_module_stack[-1]
for name, child in module.named_children():
if child is self_:
return name
return None
def _slow_forward(self_, *input, **kwargs):
tracing_state = torch._C._get_tracing_state()
if not tracing_state or isinstance(self_.forward, torch._C.ScriptMethod):
return self_.forward(*input, **kwargs)
if not hasattr(tracing_state, '_traced_module_stack'):
tracing_state._traced_module_stack = []
name = _tracing_name(self_, tracing_state)
if name:
tracing_state.push_scope('%s[%s]' % (self_._get_name(), name))
else:
tracing_state.push_scope(self_._get_name())
tracing_state._traced_module_stack.append(self_)
try:
result = self_.forward(*input, **kwargs)
finally:
tracing_state.pop_scope()
tracing_state._traced_module_stack.pop()
return result
self.backup = torch.nn.Module._slow_forward
setattr(torch.nn.Module, '_slow_forward', _slow_forward)
def __exit__(self, type, value, tb):
setattr(torch.nn.Module, '_slow_forward', self.backup)