Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / BlockLSTM.py
1 """
2  Copyright (c) 2018-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 import numpy as np
23
24
25 class BlockLSTM(Op):
26     op = 'BlockLSTM'
27
28     def __init__(self, graph: Graph, attrs: dict):
29         mandatory_props = {
30             'op': __class__.op,
31             'infer': __class__.infer,
32             'type': __class__.op,
33         }
34         super().__init__(graph, mandatory_props, attrs)
35
36     @staticmethod
37     def infer(node: Node):
38         """
39          MO input edges:   |   Description:
40          -------------------------------------------------
41                 0          | x: The sequence input to the LSTM, shape (timelen, batch_size, num_inputs)
42                 1          | w: The weight matrix
43                 2          | b: The bias vector
44                 3          | h_prev: Previous/initial hidden state
45                 4          | cs_prev: Value of the initial cell state
46          """
47         assert len(node.in_nodes()) == 5
48
49         """
50         MO output edges:    |   Description:
51                 0           | cs: Output data / output hidden states concatenated over the whole time sequence
52                 1           | h: Output cell states concatenated over the whole time sequence
53         """
54
55         assert len(node.out_nodes()) in [1, 2]
56
57         mark_input_bins(node)
58         input_shape = node.in_node(0).shape
59
60         assert len(input_shape) == 3
61         out_shape = input_shape
62         node.out_node(0).shape = out_shape
63         if len(node.out_nodes()) > 1:
64             node.out_node(1).shape = out_shape