-
Notifications
You must be signed in to change notification settings - Fork 1
/
count_ops.py
243 lines (208 loc) · 8.4 KB
/
count_ops.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
import sys
import tvm
import os
import pathlib
from tvm import relay
import tvm.relay.testing
from op_summary import count_all_ops, count_all_overloads, count_all_ops_in_overloads
from e2e.resmlp.trial import import_into_relay
from e2e.resmlp.trial import init_net
from tvm.relay.testing.exact_matcher import deduplicate_vars, check_compiler_call
from tvm.relay.testing import annotate_exact_matches
def linear_body(data, weight, bias):
return relay.nn.bias_add(relay.nn.dense(data, weight), bias)
def linear_layer_definition():
input_var = relay.Var("a")
weight_var = relay.Var("b")
bias_var = relay.Var("c")
return relay.Function([input_var, weight_var, bias_var],
linear_body(input_var, weight_var, bias_var))
TEST_DIR = os.path.dirname(os.path.abspath(__file__))
ENET_DIR = os.path.join(TEST_DIR, "models/efficientnet/EfficientNet")
PARAMS_FILE = os.path.join(
ENET_DIR, "0.3358-imagenet-efficientnet-b0-47-best.params")
def callback(expr):
assert isinstance(expr, relay.Call)
assert expr.op.name == "nn.conv2d"
# print(expr)
# print('\n\n\n')
if "groups" not in expr.attrs.keys():
return True
return expr.attrs.groups == 1
def flexasr_pattern(mod):
linear_pattern = linear_layer_definition().body
main_func = mod["main"]
match_bias_add_dense = annotate_exact_matches(
main_func, linear_pattern, "ilaflex", "ilaflex.linear")
return match_bias_add_dense
def hlscnn_pattern(mod):
x = relay.Var("x")
y = relay.Var("y")
main_func = mod["main"]
conv2d = relay.Function([x, y], relay.nn.conv2d(x, y))
match_conv2d = annotate_exact_matches(
main_func, conv2d.body, "", "", callback=callback)
return match_conv2d
def vta_pattern(mod):
main_func = mod["main"]
x = relay.Var("x")
y = relay.Var("y")
dense = relay.Function([x, y], relay.nn.dense(x, y))
match_dense = annotate_exact_matches(main_func, dense.body, "", "")
bias_add = relay.Function([x, y], relay.nn.bias_add(x, y))
match_bias_add = annotate_exact_matches(match_dense, bias_add.body, "", "")
return match_bias_add
def efficientnet2():
print("EFFICIENTNET")
# FlexASR
with open("./models/efficientnet/efficientnet.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
print("total:", count_all_ops(mod))
mod["main"] = flexasr_pattern(mod)
print(count_all_overloads(mod))
# HLSCNN
with open("./models/efficientnet/efficientnet.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
x = relay.Var("x")
y = relay.Var("y")
main_func = mod["main"]
conv2d = relay.Function([x, y], relay.nn.conv2d(x, y))
match_conv2d = annotate_exact_matches(
main_func, conv2d.body, "", "", callback=callback)
mod["main"] = match_conv2d
print(count_all_overloads(mod))
with open("./models/efficientnet/efficientnet.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
# vta
main_func = mod["main"]
x = relay.Var("x")
y = relay.Var("y")
dense = relay.Function([x, y], relay.nn.dense(x, y))
match_dense = annotate_exact_matches(main_func, dense.body, "", "")
bias_add = relay.Function([x, y], relay.nn.bias_add(x, y))
match_bias = annotate_exact_matches(match_dense, bias_add.body, "", "")
mod["main"] = match_bias
print(count_all_overloads(mod))
def mobilenetv2():
print("MOBILENET V2")
with open("./models/mobilenetv2/mobilenet.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
mod = relay.transform.SimplifyInference()(mod)
print("total:", count_all_ops(mod))
# FlexASR
linear_pattern = linear_layer_definition().body
main_func = mod["main"]
mod["main"] = annotate_exact_matches(
main_func, linear_pattern, "ilaflex", "ilaflex.linear")
print(count_all_overloads(mod))
# HLSCNN
with open("./models/mobilenetv2/mobilenet.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
mod = relay.transform.SimplifyInference()(mod)
x = relay.Var("x")
y = relay.Var("y")
main_func = mod["main"]
conv2d = relay.Function([x, y], relay.nn.conv2d(x, y))
match_conv2d = annotate_exact_matches(
main_func, conv2d.body, "", "", callback=callback)
mod["main"] = match_conv2d
print(count_all_overloads(mod))
# VTA
with open("./models/mobilenetv2/mobilenet.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
mod = relay.transform.SimplifyInference()(mod)
main_func = mod["main"]
x = relay.Var("x")
y = relay.Var("y")
dense = relay.Function([x, y], relay.nn.dense(x, y))
match_dense = annotate_exact_matches(main_func, dense.body, "", "")
bias_add = relay.Function([x, y], relay.nn.bias_add(x, y))
match_bias = annotate_exact_matches(match_dense, bias_add.body, "", "")
mod["main"] = match_bias
print(count_all_overloads(mod))
def resmlp():
print("RESMLP")
# This is a hack to make the ResMLP model load correctly.
sys.path.append(os.path.join(os.path.dirname(__file__), 'e2e', 'resmlp'))
net = init_net("./e2e/resmlp/cifar_net.pth")
# with open("./models/res_mlp/resmlp.relay", "r") as fp:
# mod = tvm.parser.fromtext(fp.read()
mod, params = import_into_relay(net)
print("total:", count_all_ops(mod))
mod["main"] = flexasr_pattern(mod)
print(count_all_overloads(mod))
mod, params = import_into_relay(net)
mod["main"] = hlscnn_pattern(mod)
print(count_all_overloads(mod))
mod, params = import_into_relay(net)
mod["main"] = vta_pattern(mod)
print(count_all_overloads(mod))
def resnet20():
print("RESNET")
with open("./models/resnet20/resnet20.relay", "r") as fp:
glob = fp.read()
mod = tvm.parser.fromtext(glob)
mod = relay.transform.SimplifyInference()(mod)
print("total:", count_all_ops(mod))
mod["main"] = flexasr_pattern(mod)
print(count_all_ops_in_overloads(mod))
with open("./models/resnet20/resnet20.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
mod["main"] = hlscnn_pattern(mod)
print(count_all_ops_in_overloads(mod))
with open("./models/resnet20/resnet20.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
mod = relay.transform.SimplifyInference()(mod)
mod["main"] = vta_pattern(mod)
print(count_all_ops_in_overloads(mod))
def transformer():
print("TRANSFORMER")
with open("./models/transformer/transformer.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
print("total:", count_all_ops(mod))
mod["main"] = flexasr_pattern(mod)
print(count_all_overloads(mod))
with open("./models/transformer/transformer.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
mod["main"] = hlscnn_pattern(mod)
print(count_all_overloads(mod))
with open("./models/transformer/transformer.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
mod["main"] = vta_pattern(mod)
print(count_all_overloads(mod))
def lstm2():
print("LSTM")
with open("./models/lstm/lstm_model.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
print("total:", count_all_ops(mod))
with open("./models/lstm/lstm_pattern.relay", "r") as fp:
lmod = tvm.parser.fromtext(fp.read())
main_func = mod["main"]
match_lstm = annotate_exact_matches(main_func, lmod["main"].body, "", "")
lmod["main"] = match_lstm
print(count_all_overloads(lmod))
with open("./models/lstm/lstm_model.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
mod["main"] = hlscnn_pattern(mod)
print(count_all_overloads(mod))
with open("./models/lstm/lstm_model.relay", "r") as fp:
mod = tvm.parser.fromtext(fp.read())
mod["main"] = vta_pattern(mod)
print(count_all_overloads(mod))
def resnet50_from_different_frameworks():
for framework in ['tf', 'pytorch', 'onnx']:
print(f"RESNET50 from {framework}")
with open(pathlib.Path(__file__).parent.resolve() / "diffing_models_from_different_frameworks" / f"resnet50_simplifyinference_from_{framework}.relay", "r") as fp:
glob = fp.read()
mod = tvm.parser.fromtext(glob)
print("total:", count_all_ops(mod))
for pattern in [flexasr_pattern, hlscnn_pattern, vta_pattern]:
mod = tvm.IRModule({'main': pattern(mod)})
print(count_all_ops_in_overloads(mod))
transformer()
efficientnet2()
lstm2()
mobilenetv2()
resmlp()
resnet20()
resnet50_from_different_frameworks()