-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.py
More file actions
194 lines (178 loc) · 6.94 KB
/
Copy pathtranslate.py
File metadata and controls
194 lines (178 loc) · 6.94 KB
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
from typing import Any, Dict, List, Optional, Tuple, Union
import numpy as np
from onnx import AttributeProto, FunctionProto, GraphProto, ModelProto, NodeProto
from onnx.numpy_helper import to_array
from .emitter import EventType, Emitter
class Translater:
"""
Translates an ONNX graph into a code following the light API.
"""
def __init__(
self,
proto: Union[ModelProto, FunctionProto, GraphProto],
emitter: Optional[Emitter] = None,
):
self.proto_ = proto
self.emitter = emitter or Emitter()
def __repr__(self) -> str:
return f"{self.__class__.__name__}(<{type(self.proto_)})"
def export(self, as_str, single_line: bool = False) -> Union[str, List[str]]:
"""
Exports into a code.
:param as_str: as a single string or by rows
:param single_line: tries to compress the output into a single line
:return: list of instructions
"""
rows = []
if isinstance(self.proto_, ModelProto):
opsets = {d.domain: d.version for d in self.proto_.opset_import}
rows.extend(self.emitter(EventType.START, opsets=opsets))
inputs = self.proto_.graph.input
outputs = self.proto_.graph.output
nodes = self.proto_.graph.node
initializers = self.proto_.graph.initializer
sparse_initializers = self.proto_.graph.sparse_initializer
elif isinstance(self.proto_, (FunctionProto, GraphProto)):
inputs = self.proto_.input
outputs = self.proto_.output
nodes = self.proto_.node
if isinstance(self.proto_, GraphProto):
initializers = self.proto_.initializer
sparse_initializers = self.proto_.sparse_initializer
else:
initializers = []
sparse_initializers = []
else:
raise ValueError(f"Unexpected type {type(self.proto_)} for proto.")
if sparse_initializers:
raise NotImplementedError("Sparse initializer not supported yet.")
rows.extend(
self.emitter(
EventType.BEGIN_FUNCTION
if isinstance(self.proto_, FunctionProto)
else EventType.BEGIN_GRAPH
)
)
for i in initializers:
rows.extend(
self.emitter(
EventType.INITIALIZER, name=i.name, init=i, value=to_array(i)
)
)
for i in inputs:
if isinstance(i, str):
rows.extend(self.emitter(EventType.INPUT, name=i))
else:
rows.extend(
self.emitter(
EventType.INPUT,
name=i.name,
elem_type=i.type.tensor_type.elem_type,
shape=tuple(
d.dim_value or d.dim_param
for d in i.type.tensor_type.shape.dim
),
)
)
for node in nodes:
atts = self.extract_attributes(node)
rows.extend(
self.emitter(
EventType.NODE,
op_type=node.op_type,
inputs=node.input,
outputs=node.output,
domain=node.domain,
atts=atts,
)
)
for o in outputs:
if isinstance(o, str):
rows.extend(self.emitter(EventType.INPUT, name=o))
else:
rows.extend(
self.emitter(
EventType.OUTPUT,
name=o.name,
elem_type=o.type.tensor_type.elem_type,
shape=tuple(
d.dim_value or d.dim_param
for d in o.type.tensor_type.shape.dim
),
)
)
if isinstance(self.proto_, (GraphProto, FunctionProto)):
name = self.proto_.name
else:
name = self.proto_.graph.name
rows.extend(
self.emitter(
EventType.END_FUNCTION
if isinstance(self.proto_, FunctionProto)
else EventType.END_GRAPH,
name=name,
)
)
if isinstance(self.proto_, ModelProto) and len(self.proto_.functions) > 0:
raise NotImplementedError("Local functions are not yet implemented.")
rows.extend(self.emitter(EventType.TO_ONNX))
if as_str:
return self.emitter.join(rows, single_line=single_line)
return rows
def extract_attributes(
self, node: NodeProto
) -> Dict[str, Tuple[AttributeProto, Any]]:
"""
Extracts all atributes of a node.
:param node: node proto
:return: dictionary
"""
atts: Dict[str, Tuple[AttributeProto, Any]] = {}
for att in node.attribute:
if hasattr(att, "ref_attr_name") and att.ref_attr_name:
atts[att.name] = (att, None)
continue
if att.type == AttributeProto.INT:
atts[att.name] = (att, att.i)
continue
if att.type == AttributeProto.FLOAT:
atts[att.name] = (att, att.f)
continue
if att.type == AttributeProto.INTS:
atts[att.name] = (att, np.array(att.ints))
continue
if att.type == AttributeProto.FLOATS:
atts[att.name] = (att, np.array(att.floats, dtype=np.float32))
continue
if (
att.type == AttributeProto.GRAPH
and hasattr(att, "g")
and att.g is not None
):
atts[att.name] = (att, None)
continue
if att.type == AttributeProto.SPARSE_TENSORS:
atts[att.name] = (att, to_array(att.sparse_tensor))
continue
if att.type == AttributeProto.TENSOR:
atts[att.name] = (att, to_array(att.t))
continue
if att.type == AttributeProto.TENSORS:
atts[att.name] = (att, [to_array(t) for t in att.tensors])
continue
if att.type == AttributeProto.SPARSE_TENSORS:
atts[att.name] = (att, [to_array(t) for t in att.sparse_tensors])
continue
if att.type == AttributeProto.STRING:
atts[att.name] = (att, att.s.decode("utf-8"))
continue
if att.type == AttributeProto.STRINGS:
atts[att.name] = (
att,
np.array([s.decode("utf-8") for s in att.strings]),
)
continue
raise ValueError(
f"Attribute {att.name!r} with type {att.type} cannot be extracted yet."
)
return atts