2 Copyright (c) 2017-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.
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
26 ''' A single LSTM cell (without a loop).
29 - [0, required] input data (2D),
30 - [1, required] initial hidden state (2D),
31 - [2, required] initial cell state (2D),
34 - [3, required] LSTM FC weights
35 - [4, required] LSTM FC biases
38 - [required] output data / resulting hidden state (2D)
39 - [required] resulting cell state (2D)
43 def __init__(self, graph: Graph, attrs: dict):
47 'infer': __class__.infer,
51 super().__init__(graph, mandatory_props, attrs)
53 def supported_attrs(self):
55 'hidden_size', # number of the elements in hidden cell size
62 def backend_attrs(self):
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),
72 def infer(node: Node):
73 if node.has_and_set('extra_inputs'):
74 assert len(node.in_nodes()) == 8
76 assert len(node.in_nodes()) == 5
77 assert len(node.out_nodes()) in [1, 2]
79 hidden_shape = node.in_node(1).shape.copy()
80 cell_shape = node.in_node(2).shape.copy()
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
87 hidden_size = hidden_shape[1]
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')))
94 node['hidden_size'] = hidden_size
96 assert cell_shape[1] == hidden_size