Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / onnx / lstm_ext_test.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 import unittest
17
18 import numpy as np
19 import onnx
20
21 from extensions.front.onnx.lstm_ext import LSTMFrontExtractor
22 from mo.utils.unittest.extractors import PB
23
24
25 class LSTMExtractorTest(unittest.TestCase):
26     @staticmethod
27     def _create_node(**attrs):
28         pb = onnx.helper.make_node(
29             'LSTM',
30             inputs=['X', 'W', 'R', 'B',],
31             outputs=['Y', 'Y_h', 'Y_c'],
32             hidden_size=128,
33             **attrs,
34         )
35         node = PB({'pb': pb})
36         return node
37
38     base_attrs = {
39         'type': 'RNNSequence',
40         'op': 'LSTM',
41         'batch_dim': 1,
42         'sequence_dim': 0,
43         'blobs_wrb': True,
44         'has_num_directions': True,
45         'num_layers': 1,
46         'format': 'onnx',
47         'multilayers': False,
48         'gate_order': np.array([2, 0, 3, 1]),
49         'direction': 'forward',
50     }
51
52     def test_base_attrs(self):
53         node = self._create_node()
54         LSTMFrontExtractor.extract(node)
55
56         exp_res = self.base_attrs
57
58         for key in exp_res.keys():
59             equal = np.all(np.equal(node[key], exp_res[key], dtype=object))
60             self.assertTrue(equal)
61
62     def test_additional_attributes(self):
63         additional_attrs = {
64             'activation_alpha': [1.0, 0.0, 2.0],
65             'activations': [b'relu', b'tanh', b'sigmoid'],
66             'clip': 10.0,
67         }
68
69         node = self._create_node(**additional_attrs)
70         LSTMFrontExtractor.extract(node)
71
72         exp_res = dict(**self.base_attrs, **additional_attrs)
73         exp_res['activations'] = ['relu', 'tanh', 'sigmoid']
74
75         for key in exp_res.keys():
76             equal = np.all(np.equal(node[key], exp_res[key], dtype=object))
77             self.assertTrue(equal, 'Values for attr {} are not equal'.format(key))