Skip to content

Commit

Permalink
Support TF2 Keras ConvLSTM2D operation
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Kazantsev <[email protected]>
  • Loading branch information
rkazants committed Feb 5, 2021
1 parent 54c889f commit e03e90c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def find_and_replace_pattern(self, graph: Graph):
if 'permutation' in edge_attrs:
skip_permutation = True
for out_node in node.out_nodes():
edge_attrs = node.graph.get_edge_data(node.id, out_node.id)[0]
edge_attrs = list(node.graph.get_edge_data(node.id, out_node.id).values())[0]
if 'permutation' in edge_attrs:
skip_permutation = True

Expand Down
2 changes: 1 addition & 1 deletion model-optimizer/extensions/middle/ApplyPermutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def merge_nodes_permutations(graph: Graph):

# Get all permutations from out edges
for out_node in node.out_nodes():
edge_attrs = node.graph.get_edge_data(node.id, out_node.id)[0]
edge_attrs = list(node.graph.get_edge_data(node.id, out_node.id).values())[0]
if 'permutation' in edge_attrs:
permutations.append(edge_attrs['permutation'])

Expand Down
47 changes: 47 additions & 0 deletions model-optimizer/extensions/middle/MoveConstToLoopBody.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Copyright (C) 2017-2021 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.
"""

import numpy as np

from extensions.ops.loop import Loop
from mo.middle.replacement import MiddleReplacementPattern
from mo.graph.graph import Graph, Node
from mo.ops.const import Const


class MoveConstToLoopBody(MiddleReplacementPattern):
"""
"""
enabled = True

def run_after(self):
from extensions.middle.PartialInfer import PartialInfer
return [PartialInfer]

def run_before(self):
from extensions.middle.ApplyPermutations import ApplyPermutation
return [ApplyPermutation]

def find_and_replace_pattern(self, graph: Graph):
cleanup_called_once = False
# walk through all Loop nodes and find Const inputs
for loop_node in graph.get_op_nodes(op='Loop'):
if not cleanup_called_once:
graph.clean_up()
cleanup_called_once = True
Loop.pull_constant_inputs_into_body(loop_node)
Loop.normalize_input_output_ports(loop_node)
Loop.infer(loop_node)
2 changes: 1 addition & 1 deletion model-optimizer/extensions/middle/fusings.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def find_and_replace_pattern(self, graph: Graph):
for_graph_and_each_sub_graph_recursively(graph, fuse_linear_ops)
for_graph_and_each_sub_graph_recursively(graph, lambda G: G.clean_up())

normalize_eltwise_inputs(graph)
for_graph_and_each_sub_graph_recursively(graph, normalize_eltwise_inputs)
for_graph_and_each_sub_graph_recursively(graph, lambda G: G.clean_up())

MarkNodesToFuseUpToFakeQuantize().find_and_replace_pattern(graph)
Expand Down
5 changes: 2 additions & 3 deletions model-optimizer/extensions/ops/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ def add_back_edge(loop_node: Node, internal_parameter: Node, internal_result: No
@staticmethod
def pull_constant_inputs_into_body(loop_node: Node):
for port_idx, in_port in reversed(loop_node.in_ports().items()):
# TODO add a check that the input does not correspond to execution_condition
if not in_port.disconnected() and in_port.get_source().node.soft_get('type') == 'Const':
if port_idx > 1 and not in_port.disconnected() and in_port.get_source().node.soft_get('type') == 'Const':
original_const_node = in_port.get_source().node
new_const_node = Const(loop_node.body, original_const_node.attrs()).create_node()

Expand Down Expand Up @@ -463,7 +462,7 @@ def remove_unused_ops_from_port_map(loop_node: Node, port_map: dict, port_map_at
port_to_remove = port_map[record_id_to_remove]['external_port_id']
if port_to_remove != -1:
if dir == 'in':
if port_to_remove not in [0, 1]: # input port 0 and 1 are mandatory for the Loop node
if port_to_remove not in [0, 1] and port_to_remove in loop_node.in_ports().keys(): # input port 0 and 1 are mandatory for the Loop node
loop_node.delete_input_port(port_to_remove)
elif dir == 'out' and port_to_remove in loop_node.out_ports():
loop_node.delete_output_port(port_to_remove)
Expand Down
2 changes: 1 addition & 1 deletion model-optimizer/mo/ops/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def create_permute_attrs(node, attrs=None):
@staticmethod
def set_permutation(node1, node2, permutation, override=False):
# This function creates permutation on edge between node1->node2
edge_attrs = node1.graph.get_edge_data(node1.id, node2.id)[0]
edge_attrs = list(node1.graph.get_edge_data(node1.id, node2.id).values())[0]
if 'permutation' not in edge_attrs or override:
nx.set_edge_attributes(G=node1.graph, values={(node1.id, node2.id, 0): permutation}, name='permutation')
else:
Expand Down

0 comments on commit e03e90c

Please sign in to comment.