Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / mxnet / RNN_ext.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 from extensions.ops.GRU import GRU
17 from extensions.ops.LSTM import LSTM
18 from extensions.ops.RNN import RNN
19 from mo.front.extractor import FrontExtractorOp
20 from mo.front.mxnet.extractors.utils import get_mxnet_layer_attrs
21 from mo.utils.error import Error
22 from mo.utils.utils import refer_to_faq_msg
23
24
25 class RNNFrontExtractor(FrontExtractorOp):
26     op = 'RNN'
27     enabled = True
28
29     @staticmethod
30     def extract(node):
31         attrs = get_mxnet_layer_attrs(node.symbol_dict)
32         mode = attrs.str('mode', None)
33         state_size = attrs.int('state_size', None)
34         bidirectional = attrs.bool('bidirectional', False)
35         num_layers = attrs.int('num_layers', 1)
36         layout = attrs.str('layout', 'TNC')  # in MXNet RNN by default take data in
37                                              # format [seq_len, batch_size, inp_size]
38
39         node_attrs = {
40             'batch_dim': layout.index('N'),
41             'sequence_dim': layout.index('T'),
42             'blobs_wrb': False,
43             'hidden_size': state_size,
44             'has_num_directions': bidirectional,
45             'direction': 'bidirectional' if bidirectional else 'forward',
46             'num_layers': num_layers,
47             'format': 'mxnet',
48             'multilayers': num_layers != 1,
49             'gate_order':  None,
50         }
51
52         if mode == 'rnn_tanh':
53             node_attrs['gate_order'] = [0]
54             node_attrs['activations'] = ['tanh']
55             RNN.update_node_stat(node, node_attrs)
56         elif mode == 'rnn_relu':
57             node_attrs['gate_order'] = [0]
58             node_attrs['activations'] = ['relu']
59             RNN.update_node_stat(node, node_attrs)
60         elif mode == 'gru':
61             node_attrs['gate_order'] = [1, 0, 2]
62             node_attrs['linear_before_reset'] = 1
63             GRU.update_node_stat(node, node_attrs)
64         elif mode == 'lstm':
65             node_attrs['gate_order'] = [1, 0, 2, 3]
66             LSTM.update_node_stat(node, node_attrs)
67         else:
68             raise Error(
69                 "Operation RNN with mode '{}' not supported." +
70                 refer_to_faq_msg(86),
71                 mode)
72         return __class__.enabled