Beispiel #1
0
    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 test_case_3(self, update_parameter_shape_mock):
        # test for case #3 described in the ObjectDetectionAPIPreprocessor2Replacement
        update_parameter_shape_mock.return_value = (None, 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'
        graph.graph['cmd_params'] = Namespace(
            tensorflow_object_detection_api_pipeline_config=__file__)

        with unittest.mock.patch(
                'builtins.open',
                unittest.mock.mock_open(read_data=file_content)):
            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)
Beispiel #3
0
    def test_set_ports_chain(self):
        nodes = {
            **regular_op('op1', {}),
            **regular_op('op2', {}),
            **regular_op('op3', {}),
        }

        graph = build_graph(nodes, [('op1', 'op2', {
            'fw_tensor_debug_info': {}
        }), ('op2', 'op3', {
            'fw_tensor_debug_info': {}
        })],
                            nodes_with_edges_only=True)

        graph.stage = 'front'

        ref_graph = build_graph(nodes, [
            *connect_front('op1:0', '0:op2'), *connect_front('op2:0', '0:op3')
        ],
                                nodes_with_edges_only=True)

        SetPortsPattern().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      ref_graph,
                                      'op3',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #4
0
 def test_caffe_argmax_to_topk(self):
     graph = build_graph(
         nodes_attrs=nodes_attributes,
         edges=[*connect('input', 'argmax'), *connect('argmax', 'result')],
         update_attributes={'argmax': {
             'out_max_val': 1
         }},
         nodes_with_edges_only=True)
     ArgOpsToTopK().find_and_replace_pattern(graph)
     ref_graph = build_graph(
         nodes_attrs=nodes_attributes,
         edges=[
             *connect('input', '0:topk'), *connect('topk_scalar', '1:topk'),
             *connect_front('topk:0', 'topk_out_0_data'),
             *connect_front('topk:1', 'topk_out_1_data'),
             *connect_front('topk_out_0_data', '1:concat'),
             *connect_front('topk_out_1_data', '0:concat'),
             *connect('concat', 'result')
         ],
         update_attributes={
             'topk': {
                 'axis': 0,
                 'mode': 'max',
                 'remove_values_output': True
             },
         },
         nodes_with_edges_only=True)
     (flag, resp) = compare_graphs(graph,
                                   ref_graph,
                                   'input',
                                   check_op_attrs=True)
     self.assertTrue(flag, resp)
Beispiel #5
0
    def test_set_ports_split2(self):
        nodes = {
            **regular_op('op1', {}),
            **regular_op('split', {'op': 'Split'}),
            **regular_op('op2', {}),
            **regular_op('op3', {}),
            **regular_op('op4', {}),
        }

        graph = build_graph(nodes, [
            ('op1', 'split', {'fw_tensor_debug_info': {}}),
            ('split', 'op2', {'fw_tensor_debug_info': {}, 'out_port': 0}),
            ('split', 'op4', {'fw_tensor_debug_info': {}, 'out_port': 4}),
            ('split', 'op3', {'fw_tensor_debug_info': {}, 'out_port': 6})
        ], nodes_with_edges_only=True)

        graph.stage = 'front'
        graph.nodes()['split']['out_ports_count'] = 3

        ref_graph = build_graph(nodes, [
            *connect_front('op1:0', '0:split'),
            *connect_front('split:0', '0:op2'),
            *connect_front('split:1', '0:op4'),
            *connect_front('split:2', '0:op3')
        ], nodes_with_edges_only=True)

        SetPortsPattern().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph, ref_graph, 'op4', check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #6
0
 def test_tf_argmin_to_topk(self):
     graph = build_graph(nodes_attrs=nodes_attributes,
                         edges=[
                             *connect('input', '0:argmin'),
                             *connect('axis_const', '1:argmin'),
                             *connect('argmin', 'result')
                         ],
                         nodes_with_edges_only=True)
     ArgOpsToTopK().find_and_replace_pattern(graph)
     ref_graph = build_graph(
         nodes_attrs=nodes_attributes,
         edges=[
             *connect('input', '0:topk'), *connect('topk_scalar', '1:topk'),
             *connect_front('topk:1', 'topk_out_1_data'),
             *connect_front('topk_out_1_data', 'result')
         ],
         update_attributes={
             'topk': {
                 'axis': int64_array([1]),
                 'mode': 'min',
                 'remove_values_output': True
             },
         },
         nodes_with_edges_only=True)
     (flag, resp) = compare_graphs(graph,
                                   ref_graph,
                                   'input',
                                   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'),
            }
            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
Beispiel #8
0
 def build_loop_graph(body_graph):
     # create fake Loop operation
     nodes = {
         **regular_op('input', {'op': 'Parameter'}),
         **regular_op('loop', {'op': 'Loop', 'body': body_graph, 'sub_graphs': ['body']}),
         **result('result'),
     }
     edges = [*connect_front('input', '0:loop'),
              *connect_front('loop:0', 'result'),
              ]
     graph = build_graph(nodes, edges)
     graph.stage = 'front'
     return graph
Beispiel #9
0
    def test_case_2(self, update_parameter_shape_mock):
        # test for case #2 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, '0:mul'),
            *connect_front('mul_const', '1:mul'),
            *connect_front('sub_const', '0:sub'),
            *connect_front('mul', '1:sub'),
            *connect_front('sub', '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(True),
                                      'result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_case_1_no_pad_to_max_dim(self, update_parameter_shape_mock):
        # test for case #1 described in the ObjectDetectionAPIPreprocessor2Replacement
        # sub/mul should be kept even though they are applied before prep-processing and pad_to_max_dimension is False
        update_parameter_shape_mock.return_value = (None, None)
        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', 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'
        graph.graph['cmd_params'] = Namespace(
            tensorflow_object_detection_api_pipeline_config=__file__)

        updated_pipeline_config_content = file_content.replace(
            'pad_to_max_dimension: true', 'pad_to_max_dimension: false')
        with unittest.mock.patch(
                'builtins.open',
                unittest.mock.mock_open(
                    read_data=updated_pipeline_config_content)):
            ObjectDetectionAPIPreprocessor2Replacement().transform_graph(
                graph, self.replacement_desc)

        (flag, resp) = compare_graphs(graph,
                                      self.build_ref_graph(True),
                                      'result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #11
0
    def build_conv_graph():
        nodes = {
            **const('weights', np.random.randn(1, 1, 1, 1)),
            **regular_op('input', {'op': 'Parameter'}),
            **regular_op('conv', {'op': 'Conv2D', 'layout': 'NHWC'}),
            **result('result'),
        }
        edges = [*connect_front('input', '0:conv'),
                 *connect_front('weights', '1:conv'),
                 *connect_front('conv:0', 'result'),
                 ]
        graph = build_graph(nodes, edges)

        graph.stage = 'front'
        return graph
    def test_replace_to_reverse_channel(self):
        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                *connect_front('input:0', '0:unpack'),
                                *connect_front('unpack:0', '2:pack'),
                                *connect_front('unpack:1', '1:pack'),
                                *connect_front('unpack:2', '0:pack'),
                                *connect_front('pack:0', '0:output'),
                            ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'

        UnpackPackReverseInputChannels().find_and_replace_pattern(graph)

        graph_ref = build_graph(
            nodes_attrs=nodes,
            edges=[
                *connect_front('input:0', '0:reverseChannels'),
                *connect_front('reverseChannels:0', '0:output'),
            ],
            nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #13
0
    def test_timeheightconvolution_1offset(self):
        graph = build_graph(self.nodes, [
            *connect_front('placeholder', '0:timeheightconv'),
            *connect_front('weights', '1:timeheightconv'),
            *connect_front('biases', '2:timeheightconv'),
            *connect_front('timeheightconv', 'placeholder_out')
        ],
                            nodes_with_edges_only=True)

        graph.stage = 'front'

        conv = graph.nodes['timeheightconv']
        conv['height_subsample'] = 1
        conv['height_in'] = 80
        conv['height_out'] = 80
        conv['in_channels'] = 1
        conv['out_channels'] = 12
        conv['offsets'] = int64_array([[-1, -1], [-1, 0], [-1, 1]])
        conv['time_offsets'] = [-1]
        graph.nodes['weights']['value'] = np.zeros([36])

        ref_graph = build_graph(self.nodes, [
            *connect_front('placeholder', 'memoryoffset_0'),
            *connect_front('memoryoffset_0', '0:concat'),
            *connect_front('concat', '0:conv'),
            *connect_front('weights', '1:conv'),
            *connect_front('biases', '2:conv'),
            *connect_front('conv', 'placeholder_out')
        ],
                                nodes_with_edges_only=True)
        ref_graph.nodes['weights']['value'] = np.zeros([36])
        new_conv = ref_graph.nodes['conv']
        new_conv['pad'] = int64_array([[0, 0], [0, 0], [0, 0], [1, 1]])
        new_conv['dilation'] = int64_array([1, 1, 1, 1])
        new_conv['kernel'] = int64_array([12, 1, 1, 3])
        new_conv['stride'] = int64_array([1, 1, 1, 1])

        ReplaceTimeHeightConvolutionPattern().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      ref_graph,
                                      'placeholder_out',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #14
0
 def build_parameter_result_graph():
     nodes = {
         **regular_op('input', {'op': 'Parameter'}),
         **result('result'),
     }
     edges = [*connect_front('input', '0:result'),
              ]
     graph = build_graph(nodes, edges)
     graph.stage = 'front'
     return graph
Beispiel #15
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)
    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)
Beispiel #18
0
    def create_fake_quantize_net(self, il, ih, num_bits, narrow_range, nudged_il, nudged_ih, expected_step, ir_version, use_new_frontend):
        # original tf model
        import tensorflow as tf
        tf.compat.v1.reset_default_graph()
        with tf.compat.v1.Session() as sess:
            data = tf.compat.v1.placeholder(tf.float32, [11], 'parameter')
            input_min = tf.constant(il, name='input_min')
            input_max = tf.constant(ih, name='input_max')
            tf.quantization.fake_quant_with_min_max_vars(data, input_min, input_max, num_bits, narrow_range, 'fq')

            tf.compat.v1.global_variables_initializer()
            tf_net = sess.graph_def

        # reference graph to compare with IR
        ref_net = None
        if check_ir_version(10, None, ir_version) and not use_new_frontend:
            levels = 2 ** num_bits - int(narrow_range)

            # data (shape, value) -> const (shape, vale) -> data (shape, no value)
            const_for_layer_tests = lambda name, value: {
                **{name + '_dd': {'kind': 'data', 'value': value, 'shape': value.shape}},
                **{name: {'kind': 'op', 'type': 'Const'}},
                **shaped_data(name + '_d', int64_array(value.shape))}

            connect_const_for_layer_tests = lambda first_tensor_name, second_tensor_name: [
                *connect_front(first_tensor_name + '_dd', first_tensor_name),
                *connect(first_tensor_name, second_tensor_name)]

            nodes = {
                **regular_op_with_shaped_data('parameter', [11], {'type': 'Parameter'}),
                **const_for_layer_tests('il', np.array([nudged_il], dtype=np.float32)),
                **const_for_layer_tests('ih', np.array([nudged_ih], dtype=np.float32)),
                **const_for_layer_tests('ol', np.array([nudged_il], dtype=np.float32)),
                **const_for_layer_tests('oh', np.array([nudged_ih], dtype=np.float32)),
                **regular_op_with_shaped_data('fq', [11], {'type': 'FakeQuantize', 'levels': levels}),
                **regular_op('result', {'type': 'Result'}),
            }
            edges = [
                *connect('parameter', '0:fq'),
                *connect_const_for_layer_tests('il', '1:fq'),
                *connect_const_for_layer_tests('ih', '2:fq'),
                *connect_const_for_layer_tests('ol', '3:fq'),
                *connect_const_for_layer_tests('oh', '4:fq'),
                *connect('fq', 'result'),
            ]
            ref_net = build_graph(nodes, edges)

        return tf_net, ref_net
 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
    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)
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',
    def test_simple_pooling(self):
        graph = build_graph(self.nodes, [
            *connect_front('input', 'splice'),
            *connect_front('splice', 'pool'), *connect_front('pool', 'out_op')
        ],
                            nodes_with_edges_only=True)
        graph.stage = 'front'
        AddReshapeTransposeAroundConvPool.find_and_replace_pattern(graph)

        ref_graph = build_graph(self.ref_nodes, [
            *connect_front('input', 'splice'),
            *connect_front('splice', '0:reshape_in'),
            *connect_front('splice', 'shapeof'),
            *connect_front('shapeof:0', '0:gather_batch'),
            *connect_front('ind', '1:gather_batch'),
            *connect_front('axis', '2:gather_batch'),
            *connect_front('shapeof:0', '0:gather_h'),
            *connect_front('ind_h', '1:gather_h'),
            *connect_front('axis', '2:gather_h'),
            *connect_front('gather_h', '0:div'), *connect_front('th', '1:div'),
            *connect_front('gather_batch', '0:concat'),
            *connect_front('t', '1:concat'), *connect_front('h', '3:concat'),
            *connect_front('div', '2:concat'),
            *connect_front('concat', '1:reshape_in'),
            *connect_front('reshape_in', '0:transpose_in'),
            *connect_front('transpose_in_order', "1:transpose_in"),
            *connect_front('transpose_in', 'pool'),
            *connect_front('pool', '0:transpose_out'),
            *connect_front('transpose_out_order', '1:transpose_out'),
            *connect_front('transpose_out', '0:reshape_out'),
            *connect_front('reshape_out_shape', '1:reshape_out'),
            *connect_front('reshape_out', 'out_op')
        ])

        (flag, resp) = compare_graphs(graph,
                                      ref_graph,
                                      'out_op',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Beispiel #23
0
    def test_1(self):
        graph = build_graph(nodes_attrs={
            **shaped_parameter('input', int64_array([1, 3, 15, 15])),
            **regular_op_with_empty_data('div_sqrt_dim', {
                'op': '_contrib_div_sqrt_dim'
            }),
            **result('result')
        },
                            edges=[
                                *connect('input', 'div_sqrt_dim'),
                                *connect('div_sqrt_dim', 'result')
                            ])

        ref_graph = build_graph(
            nodes_attrs={
                **shaped_parameter('input', int64_array([1, 3, 15, 15])),
                **regular_op_with_empty_data('div_sqrt_shape_of', {
                    'op': 'ShapeOf',
                    'type': 'ShapeOf'
                }),
                **shaped_const_with_data('gather_axis', None),
                **shaped_const_with_data('gather_indices', None),
                **regular_op_with_empty_data('gather', {
                    'op': 'Gather',
                    'type': 'Gather'
                }),
                **regular_op_with_empty_data('power', {
                    'op': 'AttributedPower',
                    'power': 0.5,
                    'type': 'Power'
                }),
                **regular_op_with_empty_data('cast', {
                    'op': 'Cast',
                    'type': 'Convert',
                    'dst_type': np.float32
                }),
                **regular_op_with_empty_data('z_convert_like', {
                    'op': 'ConvertLike',
                    'type': 'ConvertLike'
                }),
                **regular_op_with_empty_data('div', {
                    'op': 'Div',
                    'type': 'Divide'
                }),
                **result('result')
            },
            edges=[
                *connect('input', '0:div'),
                *connect_data('input', 'div_sqrt_shape_of'),
                *connect('div_sqrt_shape_of', '0:gather'),
                *connect('gather_axis', '1:gather'),
                *connect('gather_indices', '2:gather'),
                *connect('gather', 'cast'), *connect('cast', 'power'),
                *connect('power', '0:z_convert_like'),
                *connect_front('input_d', '1:z_convert_like'),
                *connect('z_convert_like', '1:div'), *connect('div', 'result')
            ],
        )
        DivSqrtDim().find_and_replace_pattern(graph)
        flag, resp = compare_graphs(graph,
                                    ref_graph,
                                    'result',
                                    'result',
                                    check_op_attrs=True)
        self.assertTrue(flag, resp)
    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)
Beispiel #25
0
    def create_tf_random_uniform_net(self, global_seed, op_seed, x_shape,
                                     min_val, max_val, input_type, precision,
                                     ir_version, use_new_frontend):
        tf.compat.v1.reset_default_graph()

        # Create the graph and model
        with tf.compat.v1.Session() as sess:
            tf_x_shape = x_shape.copy()

            tf_x_shape = permute_nchw_to_nhwc(tf_x_shape, use_new_frontend)

            x = tf.compat.v1.placeholder(input_type, tf_x_shape, 'Input')
            if global_seed is not None:
                tf.compat.v1.random.set_random_seed(global_seed)
            random_uniform = tf.random.uniform(x_shape,
                                               seed=op_seed,
                                               dtype=input_type,
                                               minval=min_val,
                                               maxval=max_val) + x

            tf.compat.v1.global_variables_initializer()
            tf_net = sess.graph_def

        ref_net = None
        if check_ir_version(10, None, ir_version) and not use_new_frontend:

            const_for_layer_tests = lambda name, value, shape, shape1: {
                **{
                    name + '_dd': {
                        'kind': 'data',
                        'value': value,
                        'shape': shape1
                    }
                },
                **{
                    name: {
                        'kind': 'op',
                        'type': 'Const'
                    }
                },
                **shaped_data(name + '_d', shape)
            }

            connect_const_for_layer_tests = lambda first_tensor_name, second_tensor_name: [
                *connect_front(first_tensor_name + '_dd', first_tensor_name),
                *connect(first_tensor_name, second_tensor_name)
            ]

            nodes_attributes = {
                **regular_op_with_shaped_data('input', x_shape, {
                    'type': 'Parameter'
                }),
                **const_for_layer_tests('shape', x_shape,
                                        int64_array([len(x_shape)]),
                                        int64_array([len(x_shape)])),
                **const_for_layer_tests('min_val', min_val, int64_array([]),
                                        int64_array([1])),
                **const_for_layer_tests('max_val', max_val, int64_array([]),
                                        int64_array([1])),
                **regular_op_with_shaped_data('random_uniform', x_shape, {
                    'type': 'RandomUniform'
                }),
                **regular_op_with_shaped_data('convert', x_shape, {
                    'type': 'Convert'
                }),
                **regular_op_with_shaped_data('add', x_shape, {'type': 'Add'}),
                **regular_op_with_shaped_data('result', x_shape, {
                    'type': 'Result'
                }),
            }

            if precision == 'FP16' and input_type == tf.float32:
                ref_net = build_graph(nodes_attributes, [
                    *connect_const_for_layer_tests('shape',
                                                   '0:random_uniform'),
                    *connect_const_for_layer_tests('min_val',
                                                   '1:random_uniform'),
                    *connect_const_for_layer_tests('max_val',
                                                   '2:random_uniform'),
                    *connect('random_uniform', 'convert'),
                    *connect('convert', '0:add'), *connect('input', '1:add'),
                    *connect('add', 'result')
                ])
            else:
                ref_net = build_graph(nodes_attributes, [
                    *connect_const_for_layer_tests('shape',
                                                   '0:random_uniform'),
                    *connect_const_for_layer_tests('min_val',
                                                   '1:random_uniform'),
                    *connect_const_for_layer_tests('max_val',
                                                   '2:random_uniform'),
                    *connect('random_uniform', '0:add'),
                    *connect('input', '1:add'), *connect('add', 'result')
                ])

        return tf_net, ref_net
    def test_slice_replacer(self):
        graph = build_graph(nodes_attrs=nodes,
                            edges=[
                                *connect_front('input:0', '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('input:0', 'shapeof'),
                                    *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('shapeof: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)
Beispiel #27
0
    def test_timeheightconvolution_out_channels(self):
        graph = build_graph(self.nodes, [
            *connect_front('placeholder', '0:timeheightconv'),
            *connect_front('weights', '1:timeheightconv'),
            *connect_front('biases', '2:timeheightconv'),
            *connect_front('timeheightconv', 'placeholder_out')
        ],
                            nodes_with_edges_only=True)

        graph.stage = 'front'
        conv = graph.nodes['timeheightconv']
        conv['height_subsample'] = 1
        conv['height_in'] = 80
        conv['height_out'] = 74
        conv['in_channels'] = 3
        conv['out_channels'] = 4
        conv['offsets'] = int64_array([[-1, 0], [-1, 3], [-1, 6], [1, 0],
                                       [1, 3], [1, 6]])
        conv['time_offsets'] = int64_array([-1])
        graph.nodes['weights']['value'] = np.array([
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
            20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
            37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
            54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
            71, 72
        ])

        ref_graph = build_graph(self.nodes, [
            *connect_front('placeholder', 'memoryoffset_0'),
            *connect_front('placeholder', 'memoryoffset_2'),
            *connect_front('memoryoffset_0', '0:concat'),
            *connect_front('memoryoffset_2', '1:concat'),
            *connect_front('concat', '0:conv'),
            *connect_front('weights', '1:conv'),
            *connect_front('biases', '2:conv'),
            *connect_front('conv', 'placeholder_out')
        ],
                                nodes_with_edges_only=True)
        ref_graph.nodes['weights']['value'] = np.array([
            1, 4, 7, 10, 13, 16, 2, 5, 8, 11, 14, 17, 3, 6, 9, 12, 15, 18, 19,
            22, 25, 28, 31, 34, 20, 23, 26, 29, 32, 35, 21, 24, 27, 30, 33, 36,
            37, 40, 43, 46, 49, 52, 38, 41, 44, 47, 50, 53, 39, 42, 45, 48, 51,
            54, 55, 58, 61, 64, 67, 70, 56, 59, 62, 65, 68, 71, 57, 60, 63, 66,
            69, 72
        ])
        new_conv = ref_graph.nodes['conv']
        new_conv['output'] = 4
        new_conv['pad'] = int64_array([[0, 0], [0, 0], [0, 0], [0, 0]])
        new_conv['dilation'] = int64_array([1, 1, 2, 3])
        new_conv['kernel'] = int64_array([4, 3, 2, 3])
        new_conv['stride'] = int64_array([1, 1, 1, 1])

        ReplaceTimeHeightConvolutionPattern().find_and_replace_pattern(graph)

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