Skip to content

Commit

Permalink
Added support for the Loop operation in the MO IR Reader (#2971)
Browse files Browse the repository at this point in the history
* Added support for the Loop operation in the MO IR Reader
  • Loading branch information
lazarevevgeny authored Nov 5, 2020
1 parent be406fd commit 67e0393
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions model-optimizer/automation/package_BOM.txt
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ mo/utils/ir_reader/extenders/ExtractImagePatches_extender.py
mo/utils/ir_reader/extenders/fakequantize_extender.py
mo/utils/ir_reader/extenders/GRUCell_extender.py
mo/utils/ir_reader/extenders/interpolate_extender.py
mo/utils/ir_reader/extenders/loop_extender.py
mo/utils/ir_reader/extenders/LSTMCell_extender.py
mo/utils/ir_reader/extenders/non_max_suppression_extender.py
mo/utils/ir_reader/extenders/non_zero_extender.py
Expand Down
62 changes: 62 additions & 0 deletions model-optimizer/mo/utils/ir_reader/extenders/loop_extender.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Copyright (C) 2018-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from mo.utils.graph import Node
from mo.utils.ir_reader.extender import Extender
from mo.utils.ir_reader.layer_to_class import copy_graph_with_ops


class LoopExtender(Extender):
op = 'Loop'

@staticmethod
def extend(op: Node):
def normalize_port_map(port_map: dict):
for port in port_map:
for elem in ['axis', 'stride', 'part_size', 'start', 'end', 'purpose']:
if port.get(elem) is None:
port[elem] = None

assert op.has('body'), 'There is no "body" attribute in the Loop op {}.'.format(op.name)

# Now op.body is an IREngine, we need to replace it with IREngine.graph
op.body.graph.graph['cmd_params'] = op.graph.graph['cmd_params']
op.body.graph.graph['ir_version'] = op.graph.graph['ir_version']
op.body.graph.name = op.name + '/body'

for node in op.body.graph.get_op_nodes():
node['internal_layer_id'] = int(node.id)

op.body = copy_graph_with_ops(op.body.graph)

normalize_port_map(op.input_port_map)
normalize_port_map(op.output_port_map)

# the 'external_port_id' uses end-to-end numbering of ports, but at this moment it is separate for input and
# output ports so we need to decrease the output por_id with a number of input ports
for record in op.output_port_map:
if record['external_port_id'] != -1:
record['external_port_id'] -= len(op.in_ports())

for edge in op.back_edges:
edge['from_layer'] = edge['from-layer']
edge['to_layer'] = edge['to-layer']

edge['to_port'] = 0
edge['from_port'] = 0

del(edge['from-layer'])
del(edge['to-layer'])
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""

from mo.front.common.partial_infer.utils import int64_array
from mo.middle.passes.convert_data_type import destination_type_to_np_data_type
from mo.utils.graph import Node
from mo.utils.ir_reader.extender import Extender
Expand All @@ -26,4 +26,7 @@ class Parameter_extender(Extender):
def extend(op: Node):
assert op.has_valid('element_type'), 'Parameter node {} has missed element_type attr!'.format(op.name)
op['data_type'] = destination_type_to_np_data_type(op.element_type)
Extender.attr_to_list(op, 'shape')
if op.shape == '':
op.shape = int64_array([])
else:
Extender.attr_to_list(op, 'shape')

0 comments on commit 67e0393

Please sign in to comment.