-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjit_handles.py
262 lines (232 loc) · 9.27 KB
/
jit_handles.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
# taken from detectron2 / fvcore with a few modifications
# https://github.com/facebookresearch/detectron2/blob/master/detectron2/utils/analysis.py
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import typing
from collections import Counter, OrderedDict
import numpy as np
from numpy import prod
from itertools import zip_longest
def get_shape(val: object) -> typing.List[int]:
"""
Get the shapes from a jit value object.
Args:
val (torch._C.Value): jit value object.
Returns:
list(int): return a list of ints.
"""
if val.isCompleteTensor(): # pyre-ignore
r = val.type().sizes() # pyre-ignore
if not r:
r = [1]
return r
elif val.type().kind() in ("IntType", "FloatType"):
return [1]
else:
raise ValueError()
def addmm_flop_jit(
inputs: typing.List[object], outputs: typing.List[object]
) -> typing.Counter[str]:
"""
This method counts the flops for fully connected layers with torch script.
Args:
inputs (list(torch._C.Value)): The input shape in the form of a list of
jit object.
outputs (list(torch._C.Value)): The output shape in the form of a list
of jit object.
Returns:
Counter: A Counter dictionary that records the number of flops for each
operation.
"""
# Count flop for nn.Linear
# inputs is a list of length 3.
input_shapes = [get_shape(v) for v in inputs[1:3]]
# input_shapes[0]: [batch size, input feature dimension]
# input_shapes[1]: [batch size, output feature dimension]
assert len(input_shapes[0]) == 2
assert len(input_shapes[1]) == 2
batch_size, input_dim = input_shapes[0]
output_dim = input_shapes[1][1]
flop = batch_size * input_dim * output_dim
flop_counter = Counter({"addmm": flop})
return flop_counter
def bmm_flop_jit(inputs, outputs):
# Count flop for nn.Linear
# inputs is a list of length 3.
input_shapes = [get_shape(v) for v in inputs]
# input_shapes[0]: [batch size, input feature dimension]
# input_shapes[1]: [batch size, output feature dimension]
assert len(input_shapes[0]) == 3
assert len(input_shapes[1]) == 3
T, batch_size, input_dim = input_shapes[0]
output_dim = input_shapes[1][2]
flop = T * batch_size * input_dim * output_dim
flop_counter = Counter({"bmm": flop})
return flop_counter
def basic_binary_op_flop_jit(inputs, outputs, name):
input_shapes = [get_shape(v) for v in inputs]
# for broadcasting
input_shapes = [s[::-1] for s in input_shapes]
max_shape = np.array(list(zip_longest(*input_shapes, fillvalue=1))).max(1)
flop = prod(max_shape)
flop_counter = Counter({name: flop})
return flop_counter
def rsqrt_flop_jit(inputs, outputs):
input_shapes = [get_shape(v) for v in inputs]
flop = prod(input_shapes[0]) * 2
flop_counter = Counter({"rsqrt": flop})
return flop_counter
def dropout_flop_jit(inputs, outputs):
input_shapes = [get_shape(v) for v in inputs[:1]]
flop = prod(input_shapes[0])
flop_counter = Counter({"dropout": flop})
return flop_counter
def softmax_flop_jit(inputs, outputs):
# from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/profiler/internal/flops_registry.py
input_shapes = [get_shape(v) for v in inputs[:1]]
flop = prod(input_shapes[0]) * 5
flop_counter = Counter({'softmax': flop})
return flop_counter
def _reduction_op_flop_jit(inputs, outputs, reduce_flops=1, finalize_flops=0):
input_shapes = [get_shape(v) for v in inputs]
output_shapes = [get_shape(v) for v in outputs]
in_elements = prod(input_shapes[0])
out_elements = prod(output_shapes[0])
num_flops = (in_elements * reduce_flops
+ out_elements * (finalize_flops - reduce_flops))
return num_flops
def conv_flop_count(
x_shape: typing.List[int],
w_shape: typing.List[int],
out_shape: typing.List[int],
) -> typing.Counter[str]:
"""
This method counts the flops for convolution. Note only multiplication is
counted. Computation for addition and bias is ignored.
Args:
x_shape (list(int)): The input shape before convolution.
w_shape (list(int)): The filter shape.
out_shape (list(int)): The output shape after convolution.
Returns:
Counter: A Counter dictionary that records the number of flops for each
operation.
"""
batch_size, Cin_dim, Cout_dim = x_shape[0], w_shape[1], out_shape[1]
out_size = prod(out_shape[2:])
kernel_size = prod(w_shape[2:])
flop = batch_size * out_size * Cout_dim * Cin_dim * kernel_size
flop_counter = Counter({"conv": flop})
return flop_counter
def conv_flop_jit(
inputs: typing.List[object], outputs: typing.List[object]
) -> typing.Counter[str]:
"""
This method counts the flops for convolution using torch script.
Args:
inputs (list(torch._C.Value)): The input shape in the form of a list of
jit object before convolution.
outputs (list(torch._C.Value)): The output shape in the form of a list
of jit object after convolution.
Returns:
Counter: A Counter dictionary that records the number of flops for each
operation.
"""
# Inputs of Convolution should be a list of length 12. They represent:
# 0) input tensor, 1) convolution filter, 2) bias, 3) stride, 4) padding,
# 5) dilation, 6) transposed, 7) out_pad, 8) groups, 9) benchmark_cudnn,
# 10) deterministic_cudnn and 11) user_enabled_cudnn.
assert len(inputs) == 12
x, w = inputs[:2]
x_shape, w_shape, out_shape = (
get_shape(x),
get_shape(w),
get_shape(outputs[0]),
)
return conv_flop_count(x_shape, w_shape, out_shape)
def einsum_flop_jit(
inputs: typing.List[object], outputs: typing.List[object]
) -> typing.Counter[str]:
"""
This method counts the flops for the einsum operation. We currently support
two einsum operations: "nct,ncp->ntp" and "ntg,ncg->nct".
Args:
inputs (list(torch._C.Value)): The input shape in the form of a list of
jit object before einsum.
outputs (list(torch._C.Value)): The output shape in the form of a list
of jit object after einsum.
Returns:
Counter: A Counter dictionary that records the number of flops for each
operation.
"""
# Inputs of einsum should be a list of length 2.
# Inputs[0] stores the equation used for einsum.
# Inputs[1] stores the list of input shapes.
assert len(inputs) == 2
equation = inputs[0].toIValue() # pyre-ignore
# Get rid of white space in the equation string.
equation = equation.replace(" ", "")
# Re-map equation so that same equation with different alphabet
# representations will look the same.
letter_order = OrderedDict((k, 0) for k in equation if k.isalpha()).keys()
mapping = {ord(x): 97 + i for i, x in enumerate(letter_order)}
equation = equation.translate(mapping)
input_shapes_jit = inputs[1].node().inputs() # pyre-ignore
input_shapes = [get_shape(v) for v in input_shapes_jit]
if equation == "abc,abd->acd":
n, c, t = input_shapes[0]
p = input_shapes[-1][-1]
flop = n * c * t * p
flop_counter = Counter({"einsum": flop})
return flop_counter
elif equation == "abc,adc->adb":
n, t, g = input_shapes[0]
c = input_shapes[-1][1]
flop = n * t * g * c
flop_counter = Counter({"einsum": flop})
return flop_counter
else:
raise NotImplementedError("Unsupported einsum operation.")
def matmul_flop_jit(
inputs: typing.List[object], outputs: typing.List[object]
) -> typing.Counter[str]:
"""
This method counts the flops for matmul.
Args:
inputs (list(torch._C.Value)): The input shape in the form of a list of
jit object before matmul.
outputs (list(torch._C.Value)): The output shape in the form of a list
of jit object after matmul.
Returns:
Counter: A Counter dictionary that records the number of flops for each
operation.
"""
# Inputs should be a list of length 2.
# Inputs contains the shapes of two matrices.
input_shapes = [get_shape(v) for v in inputs]
assert len(input_shapes) == 2
assert len(input_shapes[1]) == 2
assert input_shapes[0][-1] == input_shapes[1][0]
batch_dim = input_shapes[0][0]
m1_dim, m2_dim = input_shapes[1]
flop = m1_dim * m2_dim * batch_dim
flop_counter = Counter({"matmul": flop})
return flop_counter
def batchnorm_flop_jit(
inputs: typing.List[object], outputs: typing.List[object]
) -> typing.Counter[str]:
"""
This method counts the flops for batch norm.
Args:
inputs (list(torch._C.Value)): The input shape in the form of a list of
jit object before batch norm.
outputs (list(torch._C.Value)): The output shape in the form of a list
of jit object after batch norm.
Returns:
Counter: A Counter dictionary that records the number of flops for each
operation.
"""
# Inputs[0] contains the shape of the input.
input_shape = get_shape(inputs[0])
assert 2 <= len(input_shape) <= 5
flop = prod(input_shape) * 4
flop_counter = Counter({"batchnorm": flop})
return flop_counter