d9b7a2243dd740da592476c385dcee8086daf7b8
[platform/upstream/dldt.git] / model-optimizer / mo / front / kaldi / loader / loader_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 io
17 import numpy as np
18 import struct
19 import unittest
20
21 from mo.front.kaldi.loader.loader import load_topology_map, load_components
22 from mo.graph.graph import Graph
23 from mo.utils.unittest.graph import build_graph, compare_graphs
24
25
26 class TestKaldiModelsLoading(unittest.TestCase):
27
28     def test_component_map_loading_sequence(self):
29         test_map = "input-node name=input dim=16 \n" + \
30                    "component-node name=lda component=lda input=input \n" + \
31                    "component-node name=tdnn1.affine component=tdnn1.affine input=lda \n" + \
32                    "component-node name=tdnn1.relu component=tdnn1.relu input=tdnn1.affine \n" + \
33                    "component-node name=tdnn1.batchnorm component=tdnn1.batchnorm input=tdnn1.relu \n\n"
34         graph = Graph(name="test_graph_component_map_loading_sequence")
35
36         test_top_map, input_shape, input_name = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)
37
38         ref_map = {b"lda": ["lda"],
39                    b"tdnn1.affine": ["tdnn1.affine"],
40                    b"tdnn1.relu": ["tdnn1.relu"],
41                    b"tdnn1.batchnorm": ["tdnn1.batchnorm"]}
42         self.assertEqual(test_top_map, ref_map)
43         self.assertListEqual(list(input_shape), [1, 16])
44         self.assertEquals(input_name, "input")
45
46         ref_graph = build_graph({'input': {'shape': np.array([1, 16]), 'kind': 'op', 'op': 'Parameter'},
47                                  'lda': {'kind': 'op'},
48                                  'tdnn1.affine': {'kind': 'op'},
49                                  'tdnn1.relu': {'kind': 'op'},
50                                  'tdnn1.batchnorm': {'kind': 'op'},
51                                  },
52                                 [
53                                     ('input', 'lda'),
54                                     ('lda', 'tdnn1.affine'),
55                                     ('tdnn1.affine', 'tdnn1.relu'),
56                                     ('tdnn1.relu', 'tdnn1.batchnorm'),
57                                 ]
58                                 )
59         (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.batchnorm')
60         self.assertTrue(flag, resp)
61
62     # NOTE: this test is disabled because it's broken and need to be fixed! Merge request 948.
63     # Fail in load_topology_map() in read_node() method - we create edge with node which doesn't exist in graph
64     def test_component_map_loading_swap(self):
65         test_map = "input-node name=input dim=16 \n" + \
66                    "component-node name=lda component=lda input=input \n" + \
67                    "component-node name=tdnn1.batchnorm component=tdnn1.batchnorm input=tdnn1.relu \n" + \
68                    "component-node name=tdnn1.relu component=tdnn1.relu input=tdnn1.affine \n" + \
69                    "component-node name=tdnn1.affine component=tdnn1.affine input=lda \n" + \
70                    "\n"
71         graph = Graph(name="test_graph_component_map_loading_swap")
72
73         test_top_map, input_shape, input_name = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)
74
75         ref_map = {b"lda": ["lda"],
76                    b"tdnn1.affine": ["tdnn1.affine"],
77                    b"tdnn1.relu": ["tdnn1.relu"],
78                    b"tdnn1.batchnorm": ["tdnn1.batchnorm"]}
79         self.assertEqual(test_top_map, ref_map)
80         self.assertListEqual(list(input_shape), [1, 16])
81         self.assertEquals(input_name, "input")
82
83         ref_graph = build_graph({'input': {'shape': np.array([1, 16]), 'kind': 'op', 'op': 'Parameter'},
84                                  'lda': {'kind': 'op'},
85                                  'tdnn1.affine': {'kind': 'op'},
86                                  'tdnn1.relu': {'kind': 'op'},
87                                  'tdnn1.batchnorm': {'kind': 'op'},
88                                  },
89                                 [
90                                     ('input', 'lda'),
91                                     ('lda', 'tdnn1.affine'),
92                                     ('tdnn1.affine', 'tdnn1.relu'),
93                                     ('tdnn1.relu', 'tdnn1.batchnorm'),
94                                 ]
95                                 )
96         (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.batchnorm')
97         self.assertTrue(flag, resp)
98
99     def test_component_map_loading_append(self):
100         test_map = "input-node name=input dim=16 \n" + \
101                    "component-node name=lda component=lda input=input \n" + \
102                    "component-node name=tdnn1.affine component=tdnn1.affine input=Append(input, lda) \n" + \
103                    "component-node name=tdnn1.relu component=tdnn1.relu input=Append(tdnn1.affine, input, lda) \n" + \
104                    "\n"
105         graph = Graph(name="test_graph_component_map_loading_append")
106
107         test_top_map, input_shape, input_name = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)
108
109         ref_map = {b"lda": ["lda"],
110                    b"tdnn1.affine": ["tdnn1.affine"],
111                    b"tdnn1.relu": ["tdnn1.relu"]}
112         self.assertEqual(test_top_map, ref_map)
113         self.assertListEqual(list(input_shape), [1, 16])
114         self.assertEqual(input_name, "input")
115
116         ref_graph = build_graph({'input': {'shape': np.array([1, 16]), 'kind': 'op', 'op': 'Parameter'},
117                                  'lda': {'kind': 'op'},
118                                  'tdnn1.affine': {'kind': 'op'},
119                                  'tdnn1.relu': {'kind': 'op'},
120                                  'append_input_lda': {'kind': 'op', 'op': 'Concat'},
121                                  'append_affine_input_lda': {'kind': 'op', 'op': 'Concat'},
122                                  },
123                                 [
124                                     ('input', 'lda', {'out': 0}),
125                                     ('lda', 'append_input_lda', {'in': 1, 'out': 0}),
126                                     ('input', 'append_input_lda', {'in': 0, 'out': 1}),
127                                     ('append_input_lda', 'tdnn1.affine', {'out': 0}),
128                                     ('input', 'append_affine_input_lda', {'in': 1, 'out': 2}),
129                                     ('lda', 'append_affine_input_lda', {'in': 2, 'out': 1}),
130                                     ('tdnn1.affine', 'append_affine_input_lda', {'in': 0, 'out': 0}),
131                                     ('append_affine_input_lda', 'tdnn1.relu', {'out': 0}),
132                                 ]
133                                 )
134
135         (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.relu')
136         self.assertTrue(flag, resp)
137
138     def test_component_map_loading_offset(self):
139         test_map = "input-node name=input dim=16\n" + \
140                    "component-node name=lda component=lda input=Offset(input, -3)\n" + \
141                    "component-node name=tdnn1.affine component=tdnn1.affine input=Append(Offset(input, -1), Offset(lda, 1))\n" + \
142                    "component-node name=tdnn1.relu component=tdnn1.relu input=tdnn1.affine\n" + \
143                    "\n"
144         graph = Graph(name="test_graph_component_map_loading_offset")
145
146         test_top_map, input_shape, input_name = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)
147
148         ref_map = {b"lda": ["lda"],
149                    b"tdnn1.affine": ["tdnn1.affine"],
150                    b"tdnn1.relu": ["tdnn1.relu"]}
151         self.assertEqual(test_top_map, ref_map)
152         self.assertListEqual(list(input_shape), [1, 16])
153         self.assertEqual(input_name, "input")
154
155         ref_graph = build_graph({'input': {'shape': np.array([1, 16]), 'kind': 'op', 'op': 'Parameter'},
156                                  'lda': {'kind': 'op'},
157                                  'tdnn1.affine': {'kind': 'op'},
158                                  'tdnn1.relu': {'kind': 'op'},
159                                  'append_input_lda': {'kind': 'op', 'op': 'Concat'},
160                                  'offset_in_input_3': {'kind': 'op', 'op': 'memoryoffset', 't': -3, 'pair_name': 'offset_out_input_3'},
161                                  'offset_in_input_1': {'kind': 'op', 'op': 'memoryoffset', 't': -1, 'pair_name': 'offset_out_input_1'},
162                                  'offset_in_lda_1': {'kind': 'op', 'op': 'memoryoffset', 't': -1, 'pair_name': 'offset_out_lda_1'},
163                                  },
164                                 [
165                                     ('input', 'offset_in_input_3', {'out': 0}),
166                                     ('offset_in_input_3', 'lda', {'out': 0}),
167                                     ('lda', 'offset_in_lda_1', {'out': 0}),
168                                     ('input', 'offset_in_input_1', {'out': 1}),
169                                     ('offset_in_lda_1', 'append_input_lda', {'in': 1, 'out': 0}),
170                                     ('offset_in_input_1', 'append_input_lda', {'in': 0, 'out': 0}),
171                                     ('append_input_lda', 'tdnn1.affine', {'out': 0}),
172                                     ('tdnn1.affine', 'tdnn1.relu', {'out': 0}),
173                                 ]
174                                 )
175
176         (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.relu')
177         self.assertTrue(flag, resp)
178
179     def test_load_components(self):
180         test_map = b"<NumComponents> " + struct.pack('B', 4) + struct.pack('I', 3) + \
181                    b"<ComponentName> lda <FixedAffineComponent> <LinearParams> </FixedAffineComponent> " + \
182                    b"<ComponentName> tdnn1.affine <NaturalGradientAffineComponent> <MaxChange>  @?<LearningRate> <LinearParams> </NaturalGradientAffineComponent> " + \
183                    b"<ComponentName> tdnn1.relu <RectifiedLinearComponent> <ValueAvg> FV </RectifiedLinearComponent>"
184
185         graph = build_graph({'input': {'shape': np.array([1, 16]), 'kind': 'op', 'op': 'Parameter'},
186                              'lda': {'kind': 'op', 'op': 'fixedaffinecomponent'},
187                              'tdnn1.affine': {'kind': 'op', 'op': 'fixedaffinecomponent'},
188                              'tdnn1.relu': {'kind': 'op', 'op': 'relu'},
189                              },
190                             [
191                                 ('input', 'lda'),
192                                 ('lda', 'tdnn1.affine'),
193                                 ('tdnn1.affine', 'tdnn1.relu'),
194                             ]
195                             )
196
197         ref_map = {b"lda": ["lda"],
198                    b"tdnn1.affine": ["tdnn1.affine"],
199                    b"tdnn1.relu": ["tdnn1.relu"]}
200
201         load_components(io.BytesIO(test_map), graph, ref_map)
202
203         ref_graph = build_graph({'input': {'shape': np.array([1, 16]), 'kind': 'op', 'op': 'Parameter'},
204                                  'lda': {'kind': 'op', 'op': 'fixedaffinecomponent', 'parameters': '<LinearParams> '},
205                                  'tdnn1.affine': {'kind': 'op', 'op': 'naturalgradientaffinecomponent', 'parameters': "<MaxChange>  @?<LearningRate> ·С8<LinearParams> "},
206                                  'tdnn1.relu': {'kind': 'op', 'op': 'rectifiedlinearcomponent', 'parameters': "<Dim>   <ValueAvg> FV "},
207                                  },
208                                 [
209                                     ('input', 'lda'),
210                                     ('lda', 'tdnn1.affine'),
211                                     ('tdnn1.affine', 'tdnn1.relu'),
212                                 ]
213                                 )
214         (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.relu')
215         self.assertTrue(flag, resp)