2 Copyright (c) 2018-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.
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
25 class RNNFrontExtractor(FrontExtractorOp):
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]
40 'batch_dim': layout.index('N'),
41 'sequence_dim': layout.index('T'),
43 'hidden_size': state_size,
44 'has_num_directions': bidirectional,
45 'direction': 'bidirectional' if bidirectional else 'forward',
46 'num_layers': num_layers,
48 'multilayers': num_layers != 1,
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)
61 node_attrs['gate_order'] = [1, 0, 2]
62 node_attrs['linear_before_reset'] = 1
63 GRU.update_node_stat(node, node_attrs)
65 node_attrs['gate_order'] = [1, 0, 2, 3]
66 LSTM.update_node_stat(node, node_attrs)
69 "Operation RNN with mode '{}' not supported." +
72 return __class__.enabled