Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / LSTM.py
1 """
2  Copyright (c) 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 from extensions.ops.RNN import rnn_infer
17 from mo.graph.graph import Node, Graph
18 from mo.ops.op import Op
19
20
21 class LSTM(Op):
22     op = 'LSTM'
23
24     def __init__(self, graph: Graph, attrs: dict):
25         mandatory_props = {
26             'type': 'RNNSequence',  # should be never emitted to IR; for debugging purposes
27             'op': __class__.op,
28             'blobs_wrb': False,  # input blobs have three separate components W, R and B like in ONNX/LSTM
29             'has_num_directions': False,  # if True, output shape has 4 dimensions; 3D otherwise
30             'direction': 'forward',
31             'infer': __class__.infer,
32             'multiplier': 4,
33             'gate_order': None,
34             'normalized': False,
35             'multilayers': False,
36             'format': None,  # format type of input blobs for different frameworks (onnx, tf, mxnet),
37
38             'activation_alpha': None,
39             'activation_beta': None,
40             'activations': None,
41             'clip': None,
42             'input_forget': None,
43             'in_ports_count': 7,
44             'out_ports_count': 3,
45         }
46         super().__init__(graph, mandatory_props, attrs)
47
48     @staticmethod
49     def supported_attrs():
50         return [
51             'hidden_size',  # number of the elements in hidden cell size
52             'direction',  # one of 'forward', 'reverse', or 'bidirectional'
53             'axis',
54
55             'activation_alpha',
56             'activation_beta',
57             'activations',
58             'clip',
59             # 'input_forget',  # Not supported yet
60         ]
61
62     def backend_attrs(self):
63         return [
64             'hidden_size',  # number of the elements in hidden cell size
65             'direction',  # one of 'forward', 'reverse', or 'bidirectional'
66             'axis',
67
68             'activation_alpha',
69             'activation_beta',
70             ('activations', lambda node: ','.join(node.activations) if node.activations is not None else None),
71             'clip',
72             # 'input_forget',  # Not supported yet
73         ]
74
75     @staticmethod
76     def infer(node: Node):
77         # there are limitations coming from ONNX LSTM definition and normalization rules
78         assert len(node.in_nodes()) >= 3  # X, W and R
79         assert len(node.in_nodes()) <= 7
80         assert len(node.out_nodes()) <= 3
81
82         rnn_infer(node, [1, 2])