Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / lstm_cell.py
1 """
2  Copyright (c) 2017-2019 Intel Corporation
3
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
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 """
16
17 import networkx as nx
18
19 from mo.front.common.partial_infer.utils import mark_input_bins
20 from mo.graph.graph import Node, Graph
21 from mo.ops.op import Op
22 from mo.utils.error import Error
23
24
25 class LSTMCell(Op):
26     ''' A single LSTM cell (without a loop).
27
28         3 inputs:
29             - [0, required] input data (2D),
30             - [1, required] initial hidden state (2D),
31             - [2, required] initial cell state (2D),
32         
33         2 blobs:
34             - [3, required] LSTM FC weights
35             - [4, required] LSTM FC biases
36         
37         2 outputs:
38             - [required] output data / resulting hidden state (2D)
39             - [required] resulting cell state (2D)
40     '''
41     op = 'LSTMCell'
42
43     def __init__(self, graph: Graph, attrs: dict):
44         mandatory_props = {
45             'type': __class__.op,
46             'op': __class__.op,
47             'infer': __class__.infer,
48             'in_ports_count': 5,
49             'out_ports_count': 2,
50         }
51         super().__init__(graph, mandatory_props, attrs)
52
53     def supported_attrs(self):
54         return [
55             'hidden_size',  # number of the elements in hidden cell size
56             'activations',
57             'activation_alpha',
58             'activation_beta',
59             'clip',
60         ]
61
62     def backend_attrs(self):
63         return [
64             'hidden_size',  # number of the elements in hidden cell size
65             ('activations', lambda node: ','.join(node.activations) if node.activations is not None else None),
66             'activation_alpha',
67             'activation_beta',
68             'clip',
69         ]
70
71     @staticmethod
72     def infer(node: Node):
73         if node.has_and_set('extra_inputs'):
74             assert len(node.in_nodes()) == 8
75         else:
76             assert len(node.in_nodes()) == 5
77         assert len(node.out_nodes()) in [1, 2]
78
79         hidden_shape = node.in_node(1).shape.copy()
80         cell_shape = node.in_node(2).shape.copy()
81
82         mark_input_bins(node, start_port=3)
83         node.out_node(0).shape = hidden_shape
84         if len(node.out_nodes()) == 2:
85             node.out_node(1).shape = cell_shape
86
87         hidden_size = hidden_shape[1]
88
89         if node.has_valid('hidden_size'):
90             if node.hidden_size != hidden_size:
91                 raise Error("Input shape {} for hidden size doesn't match pre-defined hidden_size in node {}".format(
92                     node.in_node(1).shape, node.soft_get('name')))
93         else:
94             node['hidden_size'] = hidden_size
95
96         assert cell_shape[1] == hidden_size