ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onnx_legalizer._ModelTransformerHelper Class Reference

Public Member Functions

 __init__ (self, model)
 
 make_tensor_with_base_name (self, base_name)
 
 make_node (self, opcode, inputs, outputs, *p_args, **k_args)
 
 make_split (self, input, split_sizes, axis)
 
 make_concat (self, inputs, axis)
 
 make_squeeze (self, input, axes)
 
 make_unsqueeze (self, input, axes)
 
 make_gemm (self, A, B, C, trans_a=False, trans_b=False)
 
 make_add (self, a, b)
 
 make_mul (self, a, b)
 
 make_clip (self, input, min, max)
 
 make_act (self, input, act_name)
 
 make_constant_tensor (self, tensor_data, base_name)
 
 mark_for_deletion (self, node)
 
 get_insert_id (self)
 
 set_insert_id (self, insert_id)
 
 delete_marked_nodes (self)
 

Protected Attributes

 _model
 
 _nodes_to_delete
 
 _insert_id
 
 _base_name_idx
 

Detailed Description

Helper for onnx model transformation

This helper is used for convenient operation replacement in onnx model

Attributes:
    _model (onnx.onnx_ml_pb2.ModelProto): target model that should be changed
    _nodes_to_delete (list of onnx.onnx_ml_pb2.NodeProto): list of replaced operations
    _insert_id (int): position to insert created operations (should be in topologically sorted)
    _base_name_idx (dict from str to int): maps tensor "base" name to
        largest existing serial num. For example model has tensors "t_1", "t_2", "t_4",
        in that case _base_name_idx["t_"] == 4.
        This attribute is used for unique tensor name generation.

Definition at line 75 of file onnx_legalizer.py.

Constructor & Destructor Documentation

◆ __init__()

onnx_legalizer._ModelTransformerHelper.__init__ (   self,
  model 
)

Definition at line 89 of file onnx_legalizer.py.

89 def __init__(self, model):
90 self._model = model
91 self._nodes_to_delete = []
92 self._insert_id = 0
93 # each tensor has name containing base name and unique number. for example:
94 # "abc_123": "abs_" - base name, "123" - unique number
95 # if no number in name, consider it is equal to "0"
96
97 # mapping from base names to largest given number
98 self._base_name_idx = {}
99 # gather name information for existing tensors
100 for node in model.graph.node:
101 for t in list(node.input) + list(node.output):
102 base_name, number = _parse_tensor_name(t)
103 if base_name in self._base_name_idx:
104 self._base_name_idx[base_name] = max(self._base_name_idx[base_name],
105 number)
106 else:
107 self._base_name_idx[base_name] = number
108

Member Function Documentation

◆ delete_marked_nodes()

◆ get_insert_id()

onnx_legalizer._ModelTransformerHelper.get_insert_id (   self)

Definition at line 287 of file onnx_legalizer.py.

287 def get_insert_id(self):
288 return self._insert_id
289

References onnx_legalizer._ModelTransformerHelper._insert_id.

◆ make_act()

onnx_legalizer._ModelTransformerHelper.make_act (   self,
  input,
  act_name 
)
Create activation function operation and insert it in graph.

Args:
    input (str): input tensor name
    act_name (str): name of activation function, one of ['Relu', 'Tanh', 'Sigmoid']

Returns:
    str: output tensor name

Definition at line 256 of file onnx_legalizer.py.

256 def make_act(self, input, act_name):
257 """Create activation function operation and insert it in graph.
258
259 Args:
260 input (str): input tensor name
261 act_name (str): name of activation function, one of ['Relu', 'Tanh', 'Sigmoid']
262
263 Returns:
264 str: output tensor name
265 """
266 assert (act_name in ['Relu', 'Tanh', 'Sigmoid'])
267 return self.make_node(act_name, [input], 1)[0]
268

References onnx_legalizer._ModelTransformerHelper.make_node().

◆ make_add()

onnx_legalizer._ModelTransformerHelper.make_add (   self,
  a,
  b 
)
Creates Add operation and insert it in graph.

Args:
    a (str): name of left operand tensor
    b (str): name of right operand tensor

Returns:
    str: output tensor name

Definition at line 219 of file onnx_legalizer.py.

219 def make_add(self, a, b):
220 """Creates Add operation and insert it in graph.
221
222 Args:
223 a (str): name of left operand tensor
224 b (str): name of right operand tensor
225
226 Returns:
227 str: output tensor name
228 """
229 return self.make_node('Add', [a, b], 1)[0]
230

References onnx_legalizer._ModelTransformerHelper.make_node().

◆ make_clip()

onnx_legalizer._ModelTransformerHelper.make_clip (   self,
  input,
  min,
  max 
)
Create Clip operation and insert it in graph.

Args:
    input (str): input tensor name
    min (int/float): lower clip bound
    max (int/float ): upper clip bound

Returns:
    str: output tensor name

Definition at line 243 of file onnx_legalizer.py.

243 def make_clip(self, input, min, max):
244 """Create Clip operation and insert it in graph.
245
246 Args:
247 input (str): input tensor name
248 min (int/float): lower clip bound
249 max (int/float ): upper clip bound
250
251 Returns:
252 str: output tensor name
253 """
254 return self.make_node('Clip', [input], 1, min=min, max=max)[0]
255

References onnx_legalizer._ModelTransformerHelper.make_node().

◆ make_concat()

onnx_legalizer._ModelTransformerHelper.make_concat (   self,
  inputs,
  axis 
)
Create Concat operation and insert it in graph.

Args:
    inputs (list of str): list of tensors names to concat
    axis (int): axis number to concat

Returns:
    str: output tensor name

Definition at line 163 of file onnx_legalizer.py.

163 def make_concat(self, inputs, axis):
164 """Create Concat operation and insert it in graph.
165
166 Args:
167 inputs (list of str): list of tensors names to concat
168 axis (int): axis number to concat
169
170 Returns:
171 str: output tensor name
172 """
173 return self.make_node('Concat', inputs, 1, axis=axis)[0]
174

References onnx_legalizer._ModelTransformerHelper.make_node().

◆ make_constant_tensor()

onnx_legalizer._ModelTransformerHelper.make_constant_tensor (   self,
  tensor_data,
  base_name 
)
Creates onnx constant tensor

Args:
    tensor_data (numpy.ndarray): tensor data
    base_name (str): prefix of constant tensor name

Returns:
    str: name of created constant tensor

Definition at line 269 of file onnx_legalizer.py.

269 def make_constant_tensor(self, tensor_data, base_name):
270 """Creates onnx constant tensor
271
272 Args:
273 tensor_data (numpy.ndarray): tensor data
274 base_name (str): prefix of constant tensor name
275
276 Returns:
277 str: name of created constant tensor
278 """
279 tensor = onnx.numpy_helper.from_array(tensor_data)
280 tensor.name = self.make_tensor_with_base_name(base_name)
281 self._model.graph.initializer.append(tensor)
282 return tensor.name
283

References SingleOperatorTest.SingleOperatorTest._model, ScalarOperandDecl._model, TensorOperandDecl._model, WeightDecl._model, OperationDecl._model, luci::CircleReader._model, mir_onnx::ONNXImporterImpl._model, mir_tflite::TfliteImporter._model, GenericBackend._model, onnx_legalizer._ModelTransformerHelper._model, DotBuilder.DotBuilder._model, luci_interpreter::CircleReader._model, onert_micro::core::reader::OMCircleReader._model, onert::compiler::Compiler._model, onert::exporter::CircleExporter._model, onert::compiler::train::TrainingCompiler._model, onert::loader::BaseLoader< LoaderDomain >._model, and onnx_legalizer._ModelTransformerHelper.make_tensor_with_base_name().

◆ make_gemm()

onnx_legalizer._ModelTransformerHelper.make_gemm (   self,
  A,
  B,
  C,
  trans_a = False,
  trans_b = False 
)
Create Gemm operation and insert it in graph.

Result tensor contains A*B + C

Args:
    A (str): name of tensor A
    B (str): name of tensor B
    C (str): name of tensor C
    transA (bool): if True, transpose tensor A before multiplication
    transB (bool): if True, transpose tensor B before multiplication

Returns:
    str: output tensor name

Definition at line 199 of file onnx_legalizer.py.

199 def make_gemm(self, A, B, C, trans_a=False, trans_b=False):
200 """Create Gemm operation and insert it in graph.
201
202 Result tensor contains A*B + C
203
204 Args:
205 A (str): name of tensor A
206 B (str): name of tensor B
207 C (str): name of tensor C
208 transA (bool): if True, transpose tensor A before multiplication
209 transB (bool): if True, transpose tensor B before multiplication
210
211 Returns:
212 str: output tensor name
213 """
214 return self.make_node('Gemm', [A, B, C],
215 1,
216 transA=bool(trans_a),
217 transB=bool(trans_b))[0]
218

References onnx_legalizer._ModelTransformerHelper.make_node().

◆ make_mul()

onnx_legalizer._ModelTransformerHelper.make_mul (   self,
  a,
  b 
)
Creates Mul operation and insert it in graph.

Args:
    a (str): name of left operand tensor
    b (str): name of right operand tensor

Returns:
    str: output tensor name

Definition at line 231 of file onnx_legalizer.py.

231 def make_mul(self, a, b):
232 """Creates Mul operation and insert it in graph.
233
234 Args:
235 a (str): name of left operand tensor
236 b (str): name of right operand tensor
237
238 Returns:
239 str: output tensor name
240 """
241 return self.make_node('Mul', [a, b], 1)[0]
242

References onnx_legalizer._ModelTransformerHelper.make_node().

◆ make_node()

onnx_legalizer._ModelTransformerHelper.make_node (   self,
  opcode,
  inputs,
  outputs,
p_args,
**  k_args 
)
Create arbitrary node and insert it in graph.

Args:
    opcode (str): opcode name of desired operation
    inputs (list of str): names of input tensors
    outputs (list of str or int): names of existing tensors to use as output tensors for operation or
        number of tensors that should be created
    p_args: additional arguments for onnx make_node helper
    k_args: attributes for onnx node

Returns:
    list of str: list of output tensor names

Definition at line 125 of file onnx_legalizer.py.

125 def make_node(self, opcode, inputs, outputs, *p_args, **k_args):
126 """Create arbitrary node and insert it in graph.
127
128 Args:
129 opcode (str): opcode name of desired operation
130 inputs (list of str): names of input tensors
131 outputs (list of str or int): names of existing tensors to use as output tensors for operation or
132 number of tensors that should be created
133 p_args: additional arguments for onnx make_node helper
134 k_args: attributes for onnx node
135
136 Returns:
137 list of str: list of output tensor names
138 """
139 if type(outputs) == int:
140 outputs = [self.make_tensor_with_base_name('') for i in range(outputs)]
141 assert (type(outputs) == list)
142 node = onnx.helper.make_node(opcode, inputs, outputs, *p_args, **k_args)
143 self._model.graph.node.insert(self._insert_id, node)
144 self._insert_id += 1
145 return outputs
146

References onnx_legalizer._ModelTransformerHelper._insert_id, SingleOperatorTest.SingleOperatorTest._model, ScalarOperandDecl._model, TensorOperandDecl._model, WeightDecl._model, OperationDecl._model, luci::CircleReader._model, mir_onnx::ONNXImporterImpl._model, mir_tflite::TfliteImporter._model, GenericBackend._model, onnx_legalizer._ModelTransformerHelper._model, DotBuilder.DotBuilder._model, luci_interpreter::CircleReader._model, onert_micro::core::reader::OMCircleReader._model, onert::compiler::Compiler._model, onert::exporter::CircleExporter._model, onert::compiler::train::TrainingCompiler._model, onert::loader::BaseLoader< LoaderDomain >._model, and onnx_legalizer._ModelTransformerHelper.make_tensor_with_base_name().

Referenced by onnx_legalizer._ModelTransformerHelper.make_act(), onnx_legalizer._ModelTransformerHelper.make_add(), onnx_legalizer._ModelTransformerHelper.make_clip(), onnx_legalizer._ModelTransformerHelper.make_concat(), onnx_legalizer._ModelTransformerHelper.make_gemm(), onnx_legalizer._ModelTransformerHelper.make_mul(), onnx_legalizer._ModelTransformerHelper.make_split(), onnx_legalizer._ModelTransformerHelper.make_squeeze(), and onnx_legalizer._ModelTransformerHelper.make_unsqueeze().

◆ make_split()

onnx_legalizer._ModelTransformerHelper.make_split (   self,
  input,
  split_sizes,
  axis 
)
Create Split operation and insert it in graph.

Args:
    input (str): name of input tensor
    split_sizes (list of int): list of split sizes
    axis (int): number of axis to split

Returns:
    list: list of output tensor names

Definition at line 147 of file onnx_legalizer.py.

147 def make_split(self, input, split_sizes, axis):
148 """Create Split operation and insert it in graph.
149
150 Args:
151 input (str): name of input tensor
152 split_sizes (list of int): list of split sizes
153 axis (int): number of axis to split
154
155 Returns:
156 list: list of output tensor names
157 """
158 return self.make_node('Split', [input],
159 len(split_sizes),
160 axis=axis,
161 split=split_sizes)
162

References onnx_legalizer._ModelTransformerHelper.make_node().

◆ make_squeeze()

onnx_legalizer._ModelTransformerHelper.make_squeeze (   self,
  input,
  axes 
)
Create Squeeze operation and insert it in graph.

Args:
    input (str): name of input tensor
    axes (list of int): list of dimension containing ones to remove

Returns:
    str: output tensor name

Definition at line 175 of file onnx_legalizer.py.

175 def make_squeeze(self, input, axes):
176 """Create Squeeze operation and insert it in graph.
177
178 Args:
179 input (str): name of input tensor
180 axes (list of int): list of dimension containing ones to remove
181
182 Returns:
183 str: output tensor name
184 """
185 return self.make_node('Squeeze', [input], 1, axes=axes)[0]
186

References onnx_legalizer._ModelTransformerHelper.make_node().

◆ make_tensor_with_base_name()

onnx_legalizer._ModelTransformerHelper.make_tensor_with_base_name (   self,
  base_name 
)
 Create unique name for given base_name

Args:
    base_name (str): base tensor name

Returns:
    str : unique tensor name that starts with base_name

Definition at line 109 of file onnx_legalizer.py.

109 def make_tensor_with_base_name(self, base_name):
110 """ Create unique name for given base_name
111
112 Args:
113 base_name (str): base tensor name
114
115 Returns:
116 str : unique tensor name that starts with base_name
117 """
118 if base_name in self._base_name_idx:
119 self._base_name_idx[base_name] += 1
120 return base_name + str(self._base_name_idx[base_name])
121 else:
122 self._base_name_idx[base_name] = 0
123 return base_name + '0'
124

References onnx_legalizer._ModelTransformerHelper._base_name_idx.

Referenced by onnx_legalizer._ModelTransformerHelper.make_constant_tensor(), and onnx_legalizer._ModelTransformerHelper.make_node().

◆ make_unsqueeze()

onnx_legalizer._ModelTransformerHelper.make_unsqueeze (   self,
  input,
  axes 
)
Create Unsqueeze operation and insert it in graph.

Args:
    input (str): name of input tensor
    axes (list of int): list of dimension to insert ones

Returns:
    str: output tensor name

Definition at line 187 of file onnx_legalizer.py.

187 def make_unsqueeze(self, input, axes):
188 """Create Unsqueeze operation and insert it in graph.
189
190 Args:
191 input (str): name of input tensor
192 axes (list of int): list of dimension to insert ones
193
194 Returns:
195 str: output tensor name
196 """
197 return self.make_node('Unsqueeze', [input], 1, axes=axes)[0]
198

References onnx_legalizer._ModelTransformerHelper.make_node().

◆ mark_for_deletion()

onnx_legalizer._ModelTransformerHelper.mark_for_deletion (   self,
  node 
)

Definition at line 284 of file onnx_legalizer.py.

284 def mark_for_deletion(self, node):
285 self._nodes_to_delete += [node]
286

References onnx_legalizer._ModelTransformerHelper._nodes_to_delete.

◆ set_insert_id()

onnx_legalizer._ModelTransformerHelper.set_insert_id (   self,
  insert_id 
)

Definition at line 290 of file onnx_legalizer.py.

290 def set_insert_id(self, insert_id):
291 self._insert_id = insert_id
292

References onnx_legalizer._ModelTransformerHelper._insert_id.

Field Documentation

◆ _base_name_idx

onnx_legalizer._ModelTransformerHelper._base_name_idx
protected

◆ _insert_id

◆ _model

◆ _nodes_to_delete

onnx_legalizer._ModelTransformerHelper._nodes_to_delete
protected

The documentation for this class was generated from the following file: