def test_mvn_normalizer_across_channels(self):
        graph = build_graph(nodes, [('input', 'mvn_caffe'),
                                    ('mvn_caffe', 'output')],
                            {'mvn_caffe': {
                                'across_channels': 1
                            }},
                            nodes_with_edges_only=True)
        graph.stage = 'front'

        MVNCaffeToMVN().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes, [('input', 'mvn', {
            'out': 0
        }), ('input', 'rank', {
            'out': 0
        }), *connect_front('start_1', '0:range'),
                                        *connect_front('rank', '1:range'),
                                        *connect_front('step', '2:range'),
                                        *connect_front('range', '1:mvn'),
                                        ('mvn', 'output')],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def build_main_graph(self, pre_processing: str):
        def build_body_graph(pre_processing: str):
            nodes = {
                **regular_op('input', {'type': 'Parameter'}),

                **regular_op('mul', {'op': 'Mul', 'type': 'Multiply', 'name': 'my_body_mul'}),
                **regular_op('sub', {'op': 'Sub', 'type': 'Subtract', 'name': 'my_body_sub'}),
                **const('body_mul_const', self.mul_const),
                **const('body_sub_const', self.sub_const),

                **regular_op(self.loop_start_node_name, {'op': 'Identity'}),
                **regular_op(self.loop_end_node_name, {'op': 'Identity'}),

                **regular_op('resize', {'type': 'Interpolate'}),
                **result('result'),
            }
            edges = None
            if pre_processing == 'no':
                edges = [*connect_front('input', self.loop_start_node_name),
                         *connect_front(self.loop_start_node_name, 'resize'),
                         *connect_front('resize', self.loop_end_node_name),
                         *connect_front(self.loop_end_node_name, 'result'),
                         ]
            elif pre_processing == 'trailing':
                edges = [*connect_front('input', self.loop_start_node_name),
                         *connect_front(self.loop_start_node_name, 'resize'),
                         *connect_front('resize', self.loop_end_node_name),
                         *connect_front(self.loop_end_node_name, '0:mul'),
                         *connect_front('body_mul_const', '1:mul'),
                         *connect_front('body_sub_const', '0:sub'),
                         *connect_front('mul', '1:sub'),
                         *connect_front('sub', 'result'),
                         ]
            else:
                edges = [*connect_front('input', '0:mul'),
                         *connect_front('body_mul_const', '1:mul'),
                         *connect_front('body_sub_const', '0:sub'),
                         *connect_front('mul', '1:sub'),
                         *connect_front('sub', self.loop_start_node_name),
                         *connect_front(self.loop_start_node_name, 'resize'),
                         *connect_front('resize', self.loop_end_node_name),
                         *connect_front(self.loop_end_node_name, 'result'),
                         ]
            graph = build_graph(nodes, edges, nodes_with_edges_only=True)
            graph.stage = 'front'
            return graph

        edges = [*connect_front('input', self.start_node_name),
                 *connect_front(self.start_node_name, 'loop'),
                 *connect_front('loop:0', self.end_node_name),
                 *connect_front('loop:1', self.end_node_name2),
                 *connect_front(self.end_node_name, 'result'),
                 ]
        graph = build_graph(self.nodes, edges, {'loop': {'body': build_body_graph(pre_processing)}},
                            nodes_with_edges_only=True)
        graph.stage = 'front'
        return graph
    def test_case_3(self, update_parameter_shape_mock):
        # test for case #3 described in the ObjectDetectionAPIPreprocessor2Replacement
        update_parameter_shape_mock.return_value = None

        edges = [*connect_front('input', self.start_node_name),
                 *connect_front(self.start_node_name, 'resize'),
                 *connect_front('resize', self.end_node_name),
                 *connect_front(self.end_node_name, 'result'),
                 ]
        graph = build_graph(self.nodes, edges)
        graph.stage = 'front'

        ObjectDetectionAPIPreprocessor2Replacement().transform_graph(graph, self.replacement_desc)

        (flag, resp) = compare_graphs(graph, self.build_ref_graph(False), 'result', check_op_attrs=True)
        self.assertTrue(flag, resp)
Exemple #4
0
    def test_attributed_slice_replacer(self, attributed_slice_attrs):
        nodes = {
            **regular_op_with_empty_data('input', {'type': 'Parameter'}),
            **regular_op_with_empty_data('attributed_slice', attributed_slice_attrs),
            **result(),

            # nodes after replacement
            **const('start', np.array([0, 0])),
            **const('end', np.array([1, -1])),
            **const('axis', np.array(np.array([0, 1]))),
            **regular_op_with_empty_data('slice', {
                'op': 'Slice',
                'type': None
            }),
        }

        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                ('input', 'attributed_slice'),
                                ('attributed_slice', 'output'),
                            ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'

        AttributedSliceToSliceReplacer().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes_attrs=nodes,
                                edges=[
                                    ('input', 'slice'),
                                    *connect_front('start', '1:slice'),
                                    *connect_front('end', '2:slice'),
                                    *connect_front('axis', '3:slice'),
                                    ('slice', 'output'),
                                ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Exemple #5
0
    def test_reduce_axis_is_None(self):
        graph = build_graph(nodes, edges, nodes_with_edges_only=True)
        graph.stage = 'front'

        ReduceAxisNormalizer().find_and_replace_pattern(graph)

        ref_nodes = nodes.copy()
        ref_nodes.update({
            **regular_op('rank', {
                'op': 'Rank',
                'type': None
            }),
            **regular_op('range', {
                'op': 'Range',
                'type': 'Range'
            }),
            **regular_op('begin', {
                'type': 'Const',
                'value': int64_array([0])
            }),
            **regular_op('step', {
                'type': 'Const',
                'value': int64_array([1])
            }),
        })
        graph_ref = build_graph(ref_nodes, [
            *edges,
            *connect_front('parameter:0', 'rank'),
            *connect_front('begin:0', '0:range'),
            *connect_front('rank:0', '1:range'),
            *connect_front('step:0', '2:range'),
            *connect_front('range:0', '1:reduce'),
        ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_mvn_normalize(self):
        graph = build_graph(nodes, [('input', 'mvn_onnx'),
                                    ('mvn_onnx', 'output')],
                            nodes_with_edges_only=True)
        graph.stage = 'front'

        MvnOnnxToMvn().find_and_replace_pattern(graph)

        graph_ref = build_graph(
            nodes, [('input', 'mvn'), *connect_front('axes', '1:mvn'),
                    ('mvn', 'output')],
            nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
 def build_ref_graph(self, preprocessing: bool):
     if preprocessing:
         ref_edges = [*connect_front('input', '0:mul'),
                      *connect_front('mul_const', '1:mul'),
                      *connect_front('sub_const', '0:sub'),
                      *connect_front('mul', '1:sub'),
                      *connect_front('sub', 'result'),
                      ]
     else:
         ref_edges = [*connect_front('input', 'result')]
     ref_graph = build_graph(self.nodes, ref_edges, nodes_with_edges_only=True)
     ref_graph.stage = 'front'
     return ref_graph
Exemple #8
0
    def test_reduce_axis_is_const(self):
        graph = build_graph(nodes,
                            edges, {'reduce': {
                                'axis': 1
                            }},
                            nodes_with_edges_only=True)
        graph.stage = 'front'

        graph_ref = build_graph(nodes, [
            *edges,
            *connect_front('axis', '1:reduce'),
        ], {'axis': {
            'value': np.int64(1)
        }},
                                nodes_with_edges_only=True)

        ReduceAxisNormalizer().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Exemple #9
0
    def test_slice_replacer_begin_with_2_inputs(self):
        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                ('input', 'tfslice'),
                                *connect_front('begin:0', '1:tfslice'),
                                *connect_front('begin:0', '0:john_doe'),
                                *connect_front('size:0', '2:tfslice'),
                                *connect_front('tfslice:0', 'output'),
                            ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'

        TFSliceToSliceReplacer().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes_attrs=nodes,
                                edges=[
                                    *connect_front('input:0', 'slice'),
                                    *connect_front('input:0', 'shapeof'),
                                    *connect_front('begin:0', 'slice:1'),
                                    *connect_front('begin:0', 'john_doe:1'),
                                    *connect_front('begin:0', 'end_const:0'),
                                    *connect_front('size:0', 'end_const:1'),
                                    *connect_front('size:0', 'equal:0'),
                                    *connect_front('shapeof:0', 'select:1'),
                                    *connect_front('minus_one:0', 'equal:1'),
                                    *connect_front('equal:0', 'select:0'),
                                    *connect_front('end_const:0', 'cast:0'),
                                    *connect_front('cast:0', 'select:2'),
                                    *connect_front('select:0', 'slice:2'),
                                    *connect_front('slice:0', 'output'),
                                ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Exemple #10
0
    def test_slice_replacer(self):
        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                *connect_front('input:0', 'tfslice'),
                                *connect_front('begin:0', '1:tfslice'),
                                *connect_front('size:0', '2:tfslice'),
                                *connect_front('tfslice:0', 'output'),
                            ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'

        TFSliceToSliceReplacer().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes_attrs=nodes,
                                edges=[
                                    *connect_front('input:0', 'slice'),
                                    *connect_front('begin:0', '1:slice'),
                                    *connect_front('begin:0', '0:end_const'),
                                    *connect_front('size:0', '1:end_const'),
                                    *connect_front('size:0', '0:equal'),
                                    *connect_front('int32_max:0', '1:select'),
                                    *connect_front('minus_one:0', '1:equal'),
                                    *connect_front('equal:0', '0:select'),
                                    *connect_front('end_const:0', '0:cast'),
                                    *connect_front('cast:0', '2:select'),
                                    *connect_front('select:0', '2:slice'),
                                    *connect_front('slice:0', 'output'),
                                ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Exemple #11
0
nodes = {
    **regular_op('parameter', {'type': 'Parameter'}),
    **regular_op('reduce', {
        'op': 'ReduceSum',
        'axis': None
    }),
    **regular_op('axis', {
        'op': 'Const',
        'type': 'Const',
        'value': int64_array([1])
    }),
    **result(),
}

edges = [
    *connect_front('parameter:0', '0:reduce'),
    *connect_front('reduce', 'output'),
]


class ReduceAxisNormalizerTest(unittest.TestCase):
    def test_reduce_axis_is_None(self):
        graph = build_graph(nodes, edges, nodes_with_edges_only=True)
        graph.stage = 'front'

        ReduceAxisNormalizer().find_and_replace_pattern(graph)

        ref_nodes = nodes.copy()
        ref_nodes.update({
            **regular_op('rank', {
                'op': 'Rank',
Exemple #12
0
    def test_tdnnreplacer(self, weights, biases, time_offsets):
        def generate_offsets():
            offset_edges = []
            offset_nodes = {}

            for i, t in enumerate(time_offsets):
                offset_nodes.update(**regular_op('memoryoffset_' +
                                                 str(i), {'type': None}))

                if t != 0:
                    offset_edges.append(
                        ('placeholder', 'memoryoffset_' + str(i), {
                            'out': 0,
                            'in': 0
                        }))
                    offset_edges.append(('memoryoffset_' + str(i), 'concat', {
                        'out': 0,
                        'in': i
                    }))
                else:
                    offset_edges.append(('placeholder', 'concat', {
                        'out': 0,
                        'in': i
                    }))

            return offset_nodes, offset_edges

        offset_nodes, ref_offset_edges = generate_offsets()

        nodes = {
            **offset_nodes,
            **regular_op('placeholder', {'type': 'Parameter'}),
            **regular_op(
                'tdnncomponent', {
                    'op': 'tdnncomponent',
                    'weights': np.array(weights),
                    'biases': np.array(biases),
                    'time_offsets': np.array(time_offsets)
                }),
            **const('weights', np.array(weights)),
            **const('biases', np.array(biases)),
            **regular_op('concat', {
                'type': 'Concat',
                'axis': 1
            }),
            **regular_op('memoryoffset_0', {'type': None}),
            **regular_op('memoryoffset_1', {'type': None}),
            **regular_op('memoryoffset_2', {'type': None}),
            **regular_op('fully_connected', {'type': 'FullyConnected'}),
            **result('result'),
        }

        graph = build_graph(nodes, [
            *connect_front('placeholder', 'tdnncomponent'),
            *connect_front('tdnncomponent', 'result')
        ],
                            nodes_with_edges_only=True)

        graph.stage = 'front'

        ref_graph = build_graph(nodes, [
            *ref_offset_edges, *connect_front('concat', '0:fully_connected'),
            *connect_front('weights', '1:fully_connected'),
            *connect_front('biases', '2:fully_connected'),
            *connect_front('fully_connected', 'result')
        ],
                                nodes_with_edges_only=True)

        TdnnComponentReplacer().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      ref_graph,
                                      'result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)