Пример #1
0
    def test_component_map_loading_swap(self):
        test_map = "input-node name=input dim=16 \n" + \
                   "component-node name=lda component=lda input=input \n" + \
                   "component-node name=tdnn1.batchnorm component=tdnn1.batchnorm input=tdnn1.relu \n" + \
                   "component-node name=tdnn1.relu component=tdnn1.relu input=tdnn1.affine \n" + \
                   "component-node name=tdnn1.affine component=tdnn1.affine input=lda \n" + \
                   "\n"
        graph = Graph(name="test_graph_component_map_loading_swap")

        test_top_map = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)

        ref_map = {b"lda": ["lda"],
                   b"tdnn1.affine": ["tdnn1.affine"],
                   b"tdnn1.relu": ["tdnn1.relu"],
                   b"tdnn1.batchnorm": ["tdnn1.batchnorm"]}
        self.assertEqual(test_top_map, ref_map)
        self.assertTrue("input" in graph.nodes())
        self.assertListEqual(list(Node(graph, 'input')['shape']), [1, 16])

        ref_graph = build_graph({'input': {'shape': np.array([1, 16]), 'kind': 'op', 'op': 'Parameter'},
                                 'lda': {'kind': 'op'},
                                 'tdnn1.affine': {'kind': 'op'},
                                 'tdnn1.relu': {'kind': 'op'},
                                 'tdnn1.batchnorm': {'kind': 'op'},
                                 },
                                [
                                    ('input', 'lda'),
                                    ('lda', 'tdnn1.affine'),
                                    ('tdnn1.affine', 'tdnn1.relu'),
                                    ('tdnn1.relu', 'tdnn1.batchnorm'),
                                ]
                                )
        (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.batchnorm')
        self.assertTrue(flag, resp)
Пример #2
0
    def test_component_map_loading_scale(self):
        test_map = "input-node name=input dim=16\n" + \
                   "component-node name=lda component=lda input=Scale(0.1, input)\n" + \
                   "\n"
        graph = Graph(name="test_graph_component_map_loading_scale")

        test_top_map = load_topology_map(io.BytesIO(bytes(test_map, 'ascii')),
                                         graph)

        ref_map = {b"lda": ["lda"]}
        self.assertEqual(test_top_map, ref_map)
        self.assertTrue("input" in graph.nodes())
        self.assertListEqual(list(Node(graph, 'input')['shape']), [1, 16])

        ref_graph = build_graph(
            {
                'input': {
                    'shape': np.array([1, 16]),
                    'kind': 'op',
                    'op': 'Parameter'
                },
                'lda': {
                    'kind': 'op'
                },
                'mul': {
                    'kind': 'op'
                },
                'scale_const': {
                    'kind': 'op',
                    'op': 'Const'
                },
            }, [
                ('input', 'mul', {
                    'in': 0
                }),
                ('scale_const', 'mul', {
                    'in': 1
                }),
                ('mul', 'lda', {
                    'out': 0
                }),
            ])

        (flag, resp) = compare_graphs(graph, ref_graph, 'lda')
        self.assertTrue(flag, resp)
Пример #3
0
    def test_component_map_loading_offset(self):
        test_map = "input-node name=input dim=16\n" + \
                   "component-node name=lda component=lda input=Offset(input, -3)\n" + \
                   "component-node name=tdnn1.affine component=tdnn1.affine input=Append(Offset(input, -1), Offset(lda, 1))\n" + \
                   "component-node name=tdnn1.relu component=tdnn1.relu input=tdnn1.affine\n" + \
                   "\n"
        graph = Graph(name="test_graph_component_map_loading_offset")

        test_top_map= load_topology_map(io.BytesIO(bytes(test_map, 'ascii')), graph)

        ref_map = {b"lda": ["lda"],
                   b"tdnn1.affine": ["tdnn1.affine"],
                   b"tdnn1.relu": ["tdnn1.relu"]}
        self.assertEqual(test_top_map, ref_map)
        self.assertTrue("input" in graph.nodes())
        self.assertListEqual(list(Node(graph, 'input')['shape']), [1, 16])

        ref_graph = build_graph({'input': {'shape': np.array([1, 16]), 'kind': 'op', 'op': 'Parameter'},
                                 'lda': {'kind': 'op'},
                                 'tdnn1.affine': {'kind': 'op'},
                                 'tdnn1.relu': {'kind': 'op'},
                                 'append_input_lda': {'kind': 'op', 'op': 'Concat'},
                                 'offset_in_input_3': {'kind': 'op', 'op': 'memoryoffset', 't': -3, 'pair_name': 'offset_out_input_3'},
                                 'offset_in_input_1': {'kind': 'op', 'op': 'memoryoffset', 't': -1, 'pair_name': 'offset_out_input_1'},
                                 'offset_in_lda_1': {'kind': 'op', 'op': 'memoryoffset', 't': -1, 'pair_name': 'offset_out_lda_1'},
                                 },
                                [
                                    ('input', 'offset_in_input_3', {'out': 0}),
                                    ('offset_in_input_3', 'lda', {'out': 0}),
                                    ('lda', 'offset_in_lda_1', {'out': 0}),
                                    ('input', 'offset_in_input_1', {'out': 1}),
                                    ('offset_in_lda_1', 'append_input_lda', {'in': 1, 'out': 0}),
                                    ('offset_in_input_1', 'append_input_lda', {'in': 0, 'out': 0}),
                                    ('append_input_lda', 'tdnn1.affine', {'out': 0}),
                                    ('tdnn1.affine', 'tdnn1.relu', {'out': 0}),
                                ]
                                )

        (flag, resp) = compare_graphs(graph, ref_graph, 'tdnn1.relu')
        self.assertTrue(flag, resp)