2 Copyright (c) 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.
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
26 class TestKaldiModelsLoading(unittest.TestCase):
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")
36 test_top_map, input_shape, input_name = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)
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")
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'},
54 ('lda', 'tdnn1.affine'),
55 ('tdnn1.affine', 'tdnn1.relu'),
56 ('tdnn1.relu', 'tdnn1.batchnorm'),
59 (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.batchnorm')
60 self.assertTrue(flag, resp)
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" + \
71 graph = Graph(name="test_graph_component_map_loading_swap")
73 test_top_map, input_shape, input_name = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)
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")
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'},
91 ('lda', 'tdnn1.affine'),
92 ('tdnn1.affine', 'tdnn1.relu'),
93 ('tdnn1.relu', 'tdnn1.batchnorm'),
96 (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.batchnorm')
97 self.assertTrue(flag, resp)
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" + \
105 graph = Graph(name="test_graph_component_map_loading_append")
107 test_top_map, input_shape, input_name = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)
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")
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'},
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}),
135 (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.relu')
136 self.assertTrue(flag, resp)
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" + \
144 graph = Graph(name="test_graph_component_map_loading_offset")
146 test_top_map, input_shape, input_name = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)
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")
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'},
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}),
176 (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.relu')
177 self.assertTrue(flag, resp)
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>"
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'},
192 ('lda', 'tdnn1.affine'),
193 ('tdnn1.affine', 'tdnn1.relu'),
197 ref_map = {b"lda": ["lda"],
198 b"tdnn1.affine": ["tdnn1.affine"],
199 b"tdnn1.relu": ["tdnn1.relu"]}
201 load_components(io.BytesIO(test_map), graph, ref_map)
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 "},
210 ('lda', 'tdnn1.affine'),
211 ('tdnn1.affine', 'tdnn1.relu'),
214 (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.relu')
215 self.assertTrue(flag, resp)