2 Copyright (c) 2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 from extensions.ops.reverse_sequence import ReverseSequence
19 from mo.graph.graph import Graph
20 from mo.middle.replacement import MiddleReplacementPattern
21 from mo.ops.const import Const
24 class ReverseToReverseSequence(MiddleReplacementPattern):
28 from extensions.middle.PartialInfer import PartialInfer
32 from extensions.middle.reverse_tensor_iterator import ReverseTensorIteratorLSTM
33 return [ReverseTensorIteratorLSTM]
38 ('reverse', dict(kind='op', op='Reverse'))
43 def replace_pattern(self, graph: Graph, match: dict):
44 reverse = match['reverse']
45 input_data_shape = reverse.in_node(0).shape
47 assert reverse.in_port(1).disconnected()
49 # 1. For ReverseSequence 1-port input is seq_lengths => create this input node
50 seq_lengths = np.ones(input_data_shape[0]) * input_data_shape[reverse['axis']]
51 const = Const(graph, dict(value=seq_lengths)).create_node()
53 # 2. Create new ReverseSequence node and reconnect all inputs/outputs to it
54 reverse_sequence = ReverseSequence(graph, {'name': reverse.name + '/ReverseSequence/',
55 'seq_axis': reverse['axis']}).create_node()
57 reverse.in_port(0).get_connection().set_destination(reverse_sequence.in_port(0))
58 const.out_port(0).connect(reverse_sequence.in_port(1))
59 reverse.out_port(0).get_connection().set_source(reverse_sequence.out_port(0))
61 # 3. Delete old Reverse node
62 graph.remove_node(reverse.id)