2 Copyright (c) 2018 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.
19 from copy import deepcopy
21 from extensions.ops.lstm_sequence import LSTMSequence
22 from mo.utils.error import Error
23 from mo.middle.replacement import MiddleReplacementPattern
24 from mo.ops.concat import Concat
25 from mo.ops.op import Op
26 from mo.ops.split import Split
27 from mo.graph.graph import Node
30 class DecomposeBiLSTM(MiddleReplacementPattern):
31 ''' Decomposes bidirectional LSTMSequence to forward and reverse LSTM ops.
33 To extract forward and reverse parts from initial blobs, the helper
34 functions used that should be already built-in into the operation attributes.
36 Both initial state are split to two part, two parts of the results are concatenated.
37 Axis of split/concat is completelly defined by ONNX/LSTM specification.
45 ('lstm', dict(kind='op', op='LSTMSequence', format='onnx', direction='bidirectional')),
46 ('input', dict(kind='data')),
47 ('W', dict(kind='data')),
48 ('R', dict(kind='data')),
51 ('input', 'lstm', {'in': 0}),
52 ('W', 'lstm', {'bin': 'W'}),
53 ('R', 'lstm', {'bin': 'R'}),
58 def replace_pattern(self, graph: nx.MultiDiGraph, match: dict):
59 bilstm = match['lstm']
60 new_init_hiddens = self.split_data(bilstm.in_node(5))
61 new_init_cells = self.split_data(bilstm.in_node(6))
62 assert bilstm.has_valid('blob_bidirectional_split'), \
63 'Node {} doesnt\'t have blob_bidirectional_split attribute defined.'.format(bilstm.soft_get('name'))
64 splitted_W = bilstm.blob_bidirectional_split(bilstm.in_node(1))
65 splitted_R = bilstm.blob_bidirectional_split(bilstm.in_node(2))
66 splitted_B = bilstm.blob_bidirectional_split(bilstm.in_node(3)) if 3 in bilstm.in_nodes() else (None, None)
68 outputs = self.split_bilstm(
77 self.concat(bilstm, outputs[0], outputs[1], bilstm.out_nodes())
79 def split_data(self, data: Node):
80 """ Split data node into two part along 0 axis """
81 assert len(data.shape) == 3
82 assert data.shape[0] == 2
84 output_data = [Op._create_data_node(data.graph, name=data.name + '/SplittedBiLSTM/{}'.format(['forward', 'reverse'][i])) for i in [0, 1]]
85 split_op = Split(data.graph, dict(name=data.name + '/DecomposedBiLSTM_0', axis=0, num_split=2))
86 return split_op.create_node_with_data([data], data_nodes=output_data)
89 def split_bilstm(self,
96 """ Split one bilstm node into 2 one-directional lstm nodes.
98 All input data nodes should be already prepared; they are
99 have 2 in the major dimension.
101 assert len(bilstm.out_nodes()) == 3
104 direction = ['forward', 'reverse'][i]
105 op = LSTMSequence(bilstm.graph, {
106 'hidden_size': bilstm.hidden_size,
107 'direction': direction,
108 'batch_dim': bilstm.batch_dim,
109 'sequence_dim': bilstm.sequence_dim,
110 'blobs_wrb': bilstm.blobs_wrb,
111 'has_num_directions': bilstm.has_num_directions,
112 'format': bilstm.format,
113 'name': bilstm.name + '/Split/' + direction,
116 output_data = Op._create_data_node(
118 name=bilstm.out_node(0).name + '/Split/' + str(i),
119 attrs = {'shape': bilstm.out_node(0).shape.copy()}
122 assert output_data.shape[1] == 2
123 output_data.shape[1] = 1
125 output_hidden = Op._create_data_node(
127 name=bilstm.out_node(1).name + '/Split/' + str(i),
128 attrs = {'shape': bilstm.out_node(1).shape.copy()}
131 assert output_hidden.shape[0] == 2
132 output_hidden.shape[0] = 1
134 output_cell = Op._create_data_node(
136 name=bilstm.out_node(2).name + '/Split/' + str(i),
137 attrs = {'shape': bilstm.out_node(2).shape.copy()}
140 assert output_cell.shape[0] == 2
141 output_cell.shape[0] = 1
144 op.create_node_with_data(
164 def concat(self, bilstm, forward_outputs, reverse_outputs, final_outputs):
165 """ Concatenates two set of outputs from BiLSTM """
168 Concat(bilstm.graph, {
169 'name': bilstm.name + '/FinalConcat/Data',
172 Concat(bilstm.graph, {
173 'name': bilstm.name + '/FinalConcat/HiddenState',
176 Concat(bilstm.graph, {
177 'name': bilstm.name + '/FinalConcat/CellState',
182 bilstm.graph.remove_node(bilstm.id)
184 for i in final_outputs:
185 concat_ops[i].create_node_with_data(
186 [forward_outputs[i], reverse_outputs[i]],
187 data_nodes=[final_outputs[i]]