-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_summary.py
72 lines (49 loc) · 1.59 KB
/
op_summary.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
"""
Utility functions for counting the number of operators
and BYOC overloads in modules.
"""
import tvm
from tvm import relay
from tvm.relay.expr_functor import ExprVisitor
def is_overload(func):
if func.attrs is None:
return False
return "Compiler" in func.attrs
def get_count_expr(counter_class, expr):
counter = counter_class()
counter.visit(expr)
return counter.count
def get_count_mod(counter_class, mod):
total_count = 0
for gv in mod.get_global_vars():
total_count += get_count_expr(counter_class, mod[gv])
return total_count
class Counter(ExprVisitor):
def __init__(self):
super().__init__()
self.count = 0
def eligible(self, expr):
raise NotImplementedError()
def increment(self, expr):
return 1
def visit(self, expr):
if self.eligible(expr):
self.count += self.increment(expr)
super().visit(expr)
class OpCounter(Counter):
def eligible(self, expr):
return isinstance(expr, tvm.ir.op.Op)
class OverloadCounter(Counter):
def eligible(self, expr):
return isinstance(expr, relay.Function) and is_overload(expr)
class OpInOverloadCounter(Counter):
def eligible(self, expr):
return isinstance(expr, relay.Function) and is_overload(expr)
def increment(self, expr):
return get_count_expr(OpCounter, expr)
def count_all_ops(mod):
return get_count_mod(OpCounter, mod)
def count_all_overloads(mod):
return get_count_mod(OverloadCounter, mod)
def count_all_ops_in_overloads(mod):
return get_count_mod(OpInOverloadCounter, mod)