-
Notifications
You must be signed in to change notification settings - Fork 60
/
serialize.py
169 lines (146 loc) · 5.28 KB
/
serialize.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
from attr import Attribute
import numpy as np
from collections import Counter
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
import tvm
from tvm import relay
from tvm.relay import ExprFunctor, ExprMutator, ExprVisitor
from pprint import pprint
from .mod import mod_load, mod_save, ComputeDAG
from .utils import from_pytorch, convert_ir_var
class SerializeVisitor(ExprVisitor):
def __init__(self, params=dict(), meta=None, verbose=False):
super().__init__()
self.names = set()
self.graph = []
self.params = dict()
self.old_params = params
self.MAP = dict()
self.CHILD_COUNT = Counter()
self.verbose = verbose
self.meta = meta
self.output_count_idx = 0
def visit_var(self, var: tvm.relay.expr.Var):
# print(var.name_hint)
name = str(var.name_hint)
if "input" in name or "label" in name:
return
# if name[0].isdigit():
# name = "v" + name
# print(name, var.checked_type.dtype)
if self.old_params is not None and name in self.old_params:
if self.verbose:
print(f"[loaded] {str(var.name_hint)} successfully loaded")
self.params[str(var.name_hint)] = self.old_params[name].numpy()
else:
print(
f"[missing] {str(var.name_hint)} is not found in the params, filling ones {var.checked_type.shape}"
)
shape = convert_ir_var(var.checked_type.shape)
dtype = convert_ir_var(var.checked_type.dtype)
self.params[str(var.name_hint)] = np.ones(shape).astype(dtype)
def visit_call(self, call):
# global MAP
# Recursively parse the AST
self.visit(call.op)
for a in call.args:
if isinstance(a, relay.expr.Call):
self.CHILD_COUNT[a.handle.value] += 1
self.visit(a)
# Perform recording
op_info = dict()
op_count = 0
while f"op@{op_count}" in self.names:
op_count += 1
op_name = f"op@{op_count}"
self.names.add(op_name)
op_type = str(call.op)
op_name = f"{op_type}@{op_count}"
op_info = {
"name": op_name,
"type": op_type,
}
op_attrs = dict()
if call.attrs is not None:
try:
for k in call.attrs.keys():
temp = call.attrs[k]
# print(type(temp))
op_attrs[k] = convert_ir_var(temp)
except AttributeError:
raise NotImplementedError(f"Attrs of {call.op} is not registered")
op_info["attrs"] = op_attrs
op_args = []
arg_shapes = []
# process inputs
for a in call.args:
meta = None
if isinstance(a, relay.expr.Call):
name, shape, dtype = self.MAP[a.handle.value]
var_type = "activation"
elif isinstance(a, relay.expr.Var):
name = a.name_hint
shape = a.type_annotation.shape
dtype = a.type_annotation.dtype
var_type = "parameter"
elif isinstance(a, relay.expr.Constant):
name = f"{op_name}-constant"
shape = a.data.shape
dtype = a.data.dtype
var_type = "constant"
meta = {}
meta["data"] = a.data.numpy().tolist()
else:
print("==" * 40)
print(call.op, a, type(a))
print("==" * 40)
raise NotImplementedError(f"{type(a)} is not supported yet.")
shape = convert_ir_var(shape)
dtype = convert_ir_var(dtype)
# dtype = convert_ir_var(call.checked_type.dtype)
if len(shape) == 0:
# print("yes")
shape = [
1,
]
info = {
"name": name,
"var_type": var_type,
"shape": shape,
"dtype": dtype,
"meta": meta,
}
arg_shapes.append(shape)
op_args.append(info)
op_info["inputs"] = op_args
out_name = f"out_{op_name}"
shape = convert_ir_var(call.checked_type.shape)
dtype = convert_ir_var(call.checked_type.dtype)
if len(call.checked_type.shape) == 0:
shape = [
1,
]
self.MAP[call.handle.value] = out_name, shape, dtype
# print(out_name, call.checked_type.shape, call.checked_type.dtype)
name = out_name
meta = {"children": self.CHILD_COUNT[call.handle.value]}
if self.CHILD_COUNT[call.handle.value] == 0 and self.meta is not None:
# is leaf node:
# print("find lead node", self.output_count_idx)
meta["output_info"] = self.meta["output_info"][self.output_count_idx]
# meta["output_count_idx"] = self.output_count_idx
self.output_count_idx += 1
info = {
"name": name,
"var_type": "activation",
"shape": shape,
"dtype": dtype,
"meta": meta,
}
op_info["outputs"] = [
info,
]
self.graph.append(op_info)