def test_not_useless_pad_non_constant_input(self):
        nodes = {
            **regular_op_with_shaped_data('placeholder', [10, 20, 3], {
                                              'type': 'Parameter'
                                          }),
            **regular_op_with_shaped_data('shape_of_1', [3], {
                                              'type': 'ShapeOf'
                                          }),
            **regular_op_with_shaped_data('sub', [3], {
                                              'type': 'Subtract',
                                              'op': 'Sub'
                                          }),
            **valued_const_with_data('desired_output_size',
                                     int64_array([10, 20, 3])),
            **regular_op_with_shaped_data('pad', [10, 20, 3], {
                                              'type': 'Pad',
                                              'op': 'Pad'
                                          }),
            **valued_const_with_data('fill_value', np.array(1)),
            **result('result'),
        }
        edges = [
            *connect('placeholder', '0:pad'),
            *connect('placeholder', 'shape_of_1'),
            *connect('shape_of_1', '0:sub'),
            *connect('desired_output_size', '1:sub'),
            *connect('sub', '1:pad'),
            *connect_data('sub', '2:pad'),
            *connect('fill_value', '3:pad'),
            *connect('pad', 'result'),
        ]
        graph = build_graph(nodes, edges)
        RemoveUselessPad().find_and_replace_pattern(graph)
        ref_graph = build_graph(nodes, edges)

        (flag, resp) = compare_graphs(graph, ref_graph, 'result')
        self.assertTrue(flag, resp)
Пример #2
0
    def test_mean_values_explicit_and_scale_values_explicit(self):
        graph_ref = build_graph(nodes, [
            *connect('parameter', '0:add_mean'),
            *connect('mean', '1:add_mean'),
            *connect('add_mean', '0:mul_scale'),
            *connect('scale', '1:mul_scale'),
            *connect('mul_scale', 'result'),
        ])

        argv = Namespace(mean_scale_values=[[np.array([1., 2., 3.]), np.array([1., 2., 3.])]])
        graph = build_graph(nodes, [*connect('parameter', 'result')],
                            nodes_with_edges_only=True, cli=argv)
        self.set_graph_attrs(graph, ['parameter'])
        self.set_graph_attrs(graph_ref, ['parameter'])
        graph.graph['layout'] = 'NCHW'

        AddMeanScaleValues().find_and_replace_pattern(graph)
        (flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
        self.assertTrue(flag, resp)
        self.check_graph_attrs(graph, graph_ref, ['parameter'])
Пример #3
0
    def test_div_test_2(self):
        # Test with two same inputs from one placeholder
        graph = build_graph(nodes, [
            *connect('placeholder_1:0', '0:div'),
            *connect_data('placeholder_1:0', '1:div'),
            *connect('div', 'output'),
        ], nodes_with_edges_only=True)
        Div().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes, [
            *connect('placeholder_1:0', '0:mul'),
            *connect_data('placeholder_1:0', '0:reciprocal'),
            *connect('minus_one', '1:reciprocal'),
            *connect('reciprocal', '1:mul'),
            *connect('mul', 'output'),
        ], nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True)
        self.assertTrue(flag, resp)
        self.assertTrue(graph.node[graph.get_nodes_with_attributes(type='Multiply')[0]]['name'] == 'my_div')
Пример #4
0
    def test_sub_test_2(self):
        # Test with two same inputs from one placeholder
        graph = build_graph(nodes, [
            *connect('placeholder_1:0', '0:sub'),
            *connect_data('placeholder_1:0', '1:sub'),
            *connect('sub', 'output'),
        ], nodes_with_edges_only=True)
        Sub().find_and_replace_pattern(graph)

        graph_ref = build_graph(nodes, [
            *connect('placeholder_1:0', '0:add'),
            *connect_data('placeholder_1:0', '0:negate'),
            *connect('minus_one', '1:negate'),
            *connect('negate', '1:add'),
            *connect('add', 'output'),
        ], nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True)
        self.assertTrue(flag, resp)
        self.assertTrue(graph.node[graph.get_nodes_with_attributes(type='Add')[0]]['name'] == 'my_sub')
    def test_negative_fq_unacceptable_levels(self, levels):
        nodes = nodes_dict(np.float32, None, levels)

        graph = build_graph(nodes, [
            *connect('weights:0', '0:FQ'),
            *connect('il:0', '1:FQ'),
            *connect('ih:0', '2:FQ'),
            *connect('ol:0', '3:FQ'),
            *connect('oh:0', '4:FQ'),
            *connect('FQ:0', 'output'),
        ],
                            nodes_with_edges_only=True)
        graph_ref = graph.copy()
        CompressQuantizeWeights().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #6
0
    def test_no_min_input(self):
        nodes = {
            **regular_op_with_shaped_data('placeholder', [1, 3, 20, 20], {'type': 'Parameter'}),
            **regular_op_with_shaped_data('a_clamp', [1, 3, 20, 20], {'type': None, 'op': 'Clamp'}),
            **regular_op_with_shaped_data('minimum', [1, 3, 20, 20], {'type': 'Minimum', 'op': 'Minimum'}),
            **valued_const_with_data('max', np.array(3.5)),
            **result('result'),
        }
        edges = [*connect('placeholder', '0:a_clamp'),
                 *connect('max', '2:a_clamp'),
                 *connect('a_clamp', 'result'),
                 ]
        graph = build_graph(nodes, edges)
        ClampNormalizer().find_and_replace_pattern(graph)
        ref_graph = build_graph(nodes, [*connect('placeholder', '0:minimum'),
                                        *connect('max', '1:minimum'),
                                        *connect('minimum', 'result')
                                        ])

        (flag, resp) = compare_graphs(graph, ref_graph, 'result')
        self.assertTrue(flag, resp)
Пример #7
0
    def test_useless_pad_constant_input(self):
        nodes = {
            **regular_op_with_shaped_data('placeholder', [1, 10, 20, 3], {'type': 'Parameter'}),
            **regular_op_with_shaped_data('pad', [1, 10, 20, 3], {'type': 'Pad', 'op': 'Pad'}),
            **valued_const_with_data('pads_begin', int64_array([0, 0, 0, 0])),
            **valued_const_with_data('pads_end', int64_array([0, 0, 0, 0])),
            **valued_const_with_data('fill_value', np.array(1)),
            **result('result'),
        }
        edges = [*connect('placeholder', '0:pad'),
                 *connect('pads_begin', '1:pad'),
                 *connect('pads_end', '2:pad'),
                 *connect('fill_value', '3:pad'),
                 *connect('pad', 'result'),
                 ]
        graph = build_graph(nodes, edges)
        RemoveUselessPad().find_and_replace_pattern(graph)
        ref_graph = build_graph(nodes, [*connect('placeholder', 'result')])

        (flag, resp) = compare_graphs(graph, ref_graph, 'result')
        self.assertTrue(flag, resp)
Пример #8
0
    def test_mean_values_explicit_and_optimized(self):
        graph_ref = build_graph(nodes, [
            *connect('parameter', '0:add_mean'),
            *connect('mean', '1:add_mean'),
            *connect('add_mean', 'result'),
            *connect('parameter_2', 'result_2'),
        ])

        argv = Namespace(
            mean_scale_values={
                'parameter': {
                    'mean': np.array([1., 2., 3.])
                },
                'parameter_2': {
                    'mean': np.array([0., 0., 0.])
                }
            })
        graph = build_graph(nodes, [
            *connect('parameter', 'result'),
            *connect('parameter_2', 'result_2')
        ],
                            nodes_with_edges_only=True,
                            cli=argv)
        graph.graph['layout'] = 'NCHW'

        AddMeanScaleValues().find_and_replace_pattern(graph)
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'result_2',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
    def test_data_type(self, model_dtype, original, transformed=None):
        if transformed is None:
            transformed = original
        nodes = nodes_dict(original, transformed)

        graph = build_graph(nodes, [
            *connect('weights:0', '0:FQ'),
            *connect('il:0', '1:FQ'),
            *connect('ih:0', '2:FQ'),
            *connect('ol:0', '3:FQ'),
            *connect('oh:0', '4:FQ'),
            *connect('FQ:0', 'output'),
        ],
                            nodes_with_edges_only=True,
                            cli=Namespace(data_type=model_dtype,
                                          static_shape=True))

        CompressQuantizeWeights().find_and_replace_pattern(graph)
        graph.clean_up()

        graph_ref = build_graph(nodes, [
            *connect('int_weights:0', '0:cast'),
            *connect('cast:0', '0:sub'),
            *connect('zp:0', '1:sub'),
            *connect('sub:0', '0:mul'),
            *connect('scale:0', '1:mul'),
            *connect('mul:0', 'output'),
        ],
                                nodes_with_edges_only=True)
        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #10
0
    def test_interpolate_concat_reshape_graph_comparison(self):
        graph = build_graph(nodes, [
            *connect('placeholder', '0:interpolate'),
            *connect('out_shape', '1:interpolate'),
            *connect('interpolate', '0:concat'),
            *connect('placeholder_1', '1:concat'),
            *connect('concat', 'output'),
        ], nodes_with_edges_only=True)

        InterpolateConcat().find_and_replace_pattern(graph)
        graph.clean_up()
        graph_ref = build_graph(nodes, [
            *connect('placeholder', '0:interpolate'),
            *connect('placeholder_1', 'shape'),
            *connect('shape', '0:gather'),
            *connect('indices', '1:gather'),
            *connect('axis', '2:gather'),
            *connect('gather', '1:interpolate'),
            *connect('interpolate', '0:concat'),
            *connect_data('placeholder_1', '1:concat'),
            *connect('concat', 'output'),
        ], nodes_with_edges_only=True)
        (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #11
0
        'op': 'Parameter'
    }),
    **regular_op_with_shaped_data('mul', shape, {
        'type': 'Multiply',
        'name': 'mul'
    }),
    **regular_op_with_shaped_data('max', shape, {
        'type': 'Maximum',
        'name': 'final_max'
    }),
    **valued_const_with_data('const', float_array([0.5])),
    **result('result')
}

edges = [
    *connect('input:0', '0:mul'),
    *connect('const', '1:mul'),
    *connect_data('input', '0:max'),
    *connect('mul:0', '1:max'),
    *connect('max:0', 'result'),
]

ref_nodes = {
    **regular_op_with_shaped_data('input', shape, {
        'type': 'Parameter',
        'op': 'Parameter'
    }),
    **regular_op_with_shaped_data('leaky_relu', shape, {
        'type': 'LeakyReLU',
        'name': 'max_final',
        'negative_slope': None
Пример #12
0
 def test_rank_decomposer_assertion(self):
     graph = build_graph(nodes_attrs=nodes(None), edges=[
         *connect('input', 'rank'),
         *connect('rank', 'output'),
     ], nodes_with_edges_only=True)
     self.assertRaises(AssertionError, RankDecomposer().find_and_replace_pattern, graph)
Пример #13
0
 def test_interpolate_reshape_graph_comparison(self):
     graph = build_graph(nodes, [
         *connect('placeholder', '0:interpolate'),
         *connect('out_shape', '1:interpolate'),
         *connect('interpolate', 'output'),
     ],
                         nodes_with_edges_only=True)
     InterpolateReshapeWA().find_and_replace_pattern(graph)
     graph.graph['cmd_params'] = Namespace(keep_shape_ops=True)
     graph.clean_up()
     graph_ref = build_graph(nodes, [
         *connect('placeholder', '0:interpolate'),
         *connect_data('placeholder', 'shape'),
         *connect('shape', '0:gather'),
         *connect('indices', '1:gather'),
         *connect('axis', '2:gather'),
         *connect('gather', '0:mul'),
         *connect('multiplier', '1:mul'),
         *connect('mul', '1:interpolate'),
         *connect('interpolate', 'output'),
     ],
                             nodes_with_edges_only=True)
     (flag, resp) = compare_graphs(graph,
                                   graph_ref,
                                   'output',
                                   check_op_attrs=True)
     self.assertTrue(flag, resp)
    def test_interpolate_concat_negate(self):
        graph = build_graph(nodes, [
            *connect('placeholder', '0:interpolate'),
            *connect('out_shape', '1:interpolate'),
            *connect('interpolate', 'identity_00'),
            *connect('interpolate', 'identity_01'),
            *connect('identity_00', 'output'),
            *connect('identity_01', 'output_1'),
        ], nodes_with_edges_only=True)

        InterpolateWithConcat().find_and_replace_pattern(graph)
        graph.clean_up()
        graph_ref = build_graph(nodes, [
            *connect('placeholder', '0:interpolate'),
            *connect('out_shape', '1:interpolate'),
            *connect('interpolate', 'identity_00'),
            *connect('interpolate', 'identity_01'),
            *connect('identity_00', 'output'),
            *connect('identity_01', 'output_1'),
        ], nodes_with_edges_only=True)
        (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #15
0
 def test_convert_slice_to_strided_slice(self, input_shape, start, end,
                                         axes, steps, ss_begin_parts: tuple,
                                         ss_end_parts: tuple, ss_steps,
                                         ss_begin_mask, ss_end_mask):
     graph = build_graph(
         nodes_attrs={
             **regular_op_with_shaped_data('input', input_shape, {
                 'type': 'Parameter'
             }),
             **valued_const_with_data('start', start),
             **valued_const_with_data('end', end),
             **valued_const_with_data('axes', axes),
             **valued_const_with_data('steps', steps),
             **regular_op_with_empty_data('slice', {
                 'type': None,
                 'op': 'Slice'
             }),
             **result('result')
         },
         edges=[
             *connect('input', 'slice'), *connect('start', '1:slice'),
             *connect('end', '2:slice'), *connect('axes', '3:slice'),
             *connect('steps', '4:slice'), *connect('slice', 'result')
         ])
     ref_graph = build_graph(nodes_attrs={
         **regular_op_with_shaped_data('input', input_shape, {
             'type': 'Parameter'
         }),
         **valued_const_with_data('start', start),
         **valued_const_with_data('begin_first_part', ss_begin_parts[0]),
         **valued_const_with_data('begin_last_part', ss_begin_parts[1]),
         **regular_op_with_empty_data('convert_start', {
             'op': 'Cast',
             'type': 'Convert',
             'dst_type': np.int64
         }),
         **regular_op_with_empty_data('ss_begin', {
             'type': 'Concat',
             'op': 'Concat',
             'axis': 0
         }),
         **valued_const_with_data('end', end),
         **valued_const_with_data('end_first_part', ss_end_parts[0]),
         **valued_const_with_data('end_last_part', ss_end_parts[1]),
         **regular_op_with_empty_data('convert_end', {
             'op': 'Cast',
             'type': 'Convert',
             'dst_type': np.int64
         }),
         **regular_op_with_empty_data('ss_end', {
             'type': 'Concat',
             'op': 'Concat',
             'axis': 0
         }),
         **const('ss_steps', ss_steps),
         **empty_data('ss_steps_d'),
         **regular_op_with_empty_data(
             'ss', {
                 'op': 'StridedSlice',
                 'type': 'StridedSlice',
                 'begin_mask': ss_begin_mask,
                 'end_mask': ss_end_mask,
                 'new_axis_mask': np.zeros(len(input_shape), dtype=np.int64),
                 'shrink_axis_mask': np.zeros(len(input_shape),
                                              dtype=np.int64),
                 'ellipsis_mask': np.zeros(len(input_shape), dtype=np.int64)
             }),
         **result('result')
     },
                             edges=[
                                 *connect('input', 'ss'),
                                 *connect('begin_first_part', 'ss_begin'),
                                 *connect('start', 'convert_start'),
                                 *connect('convert_start', '1:ss_begin'),
                                 *connect('begin_last_part', '2:ss_begin'),
                                 *connect('ss_begin', '1:ss'),
                                 *connect('end_first_part', 'ss_end'),
                                 *connect('end', 'convert_end'),
                                 *connect('convert_end', '1:ss_end'),
                                 *connect('end_last_part', '2:ss_end'),
                                 *connect('ss_end', '2:ss'),
                                 *connect('ss_steps', '3:ss'),
                                 *connect('ss', 'result')
                             ])
     ConvertSlice().find_and_replace_pattern(graph)
     (flag, resp) = compare_graphs(graph,
                                   ref_graph,
                                   'result',
                                   check_op_attrs=True)
     self.assertTrue(flag, resp)
Пример #16
0
 def test_convert_slice_to_strided_slice_without_axes_and_steps(self):
     graph = build_graph(nodes_attrs={
         **regular_op_with_shaped_data('input', int64_array([2, 5, 10]), {
                                           'type': 'Parameter'
                                       }),
         **valued_const_with_data('start', np.array([0, 0, 0])),
         **valued_const_with_data('end', np.array([1, 3, 5])),
         **regular_op_with_empty_data('slice', {
             'type': None,
             'op': 'Slice'
         }),
         **result('result')
     },
                         edges=[
                             *connect('input', 'slice'),
                             *connect('start', '1:slice'),
                             *connect('end', '2:slice'),
                             *connect('slice', 'result')
                         ])
     ref_graph = build_graph(nodes_attrs={
         **regular_op_with_shaped_data('input', int64_array([2, 5, 10]), {
                                           'type': 'Parameter'
                                       }),
         **valued_const_with_data('start', np.array([0, 0, 0])),
         **valued_const_with_data('begin_first_part', int64_array([])),
         **valued_const_with_data('begin_last_part', int64_array([])),
         **regular_op_with_empty_data('convert_start', {
             'op': 'Cast',
             'type': 'Convert',
             'dst_type': np.int64
         }),
         **regular_op_with_empty_data('ss_begin', {
             'type': 'Concat',
             'op': 'Concat',
             'axis': 0
         }),
         **valued_const_with_data('end', np.array([1, 3, 5])),
         **valued_const_with_data('end_first_part', int64_array([])),
         **valued_const_with_data('end_last_part', int64_array([])),
         **regular_op_with_empty_data('convert_end', {
             'op': 'Cast',
             'type': 'Convert',
             'dst_type': np.int64
         }),
         **regular_op_with_empty_data('ss_end', {
             'type': 'Concat',
             'op': 'Concat',
             'axis': 0
         }),
         **const('ss_steps', int64_array([1, 1, 1])),
         **empty_data('ss_steps_d'),
         **regular_op_with_empty_data(
             'ss', {
                 'op': 'StridedSlice',
                 'type': 'StridedSlice',
                 'begin_mask': int64_array([1, 1, 1]),
                 'end_mask': int64_array([1, 1, 1]),
                 'new_axis_mask': np.zeros(3, dtype=np.int64),
                 'shrink_axis_mask': np.zeros(3, dtype=np.int64),
                 'ellipsis_mask': np.zeros(3, dtype=np.int64)
             }),
         **result('result')
     },
                             edges=[
                                 *connect('input', 'ss'),
                                 *connect('begin_first_part', 'ss_begin'),
                                 *connect('start', 'convert_start'),
                                 *connect('convert_start', '1:ss_begin'),
                                 *connect('begin_last_part', '2:ss_begin'),
                                 *connect('ss_begin', '1:ss'),
                                 *connect('end_first_part', 'ss_end'),
                                 *connect('end', 'convert_end'),
                                 *connect('convert_end', '1:ss_end'),
                                 *connect('end_last_part', '2:ss_end'),
                                 *connect('ss_end', '2:ss'),
                                 *connect('ss_steps', '3:ss'),
                                 *connect('ss', 'result')
                             ])
     ConvertSlice().find_and_replace_pattern(graph)
     (flag, resp) = compare_graphs(graph,
                                   ref_graph,
                                   'result',
                                   check_op_attrs=True)
     self.assertTrue(flag, resp)
    def test_quantize(self):
        original_type = np.float32
        nodes = nodes_dict(original_type)

        graph = build_graph(nodes, [
            *connect('weights:0', '0:FQ'),
            *connect('il:0', '1:FQ'),
            *connect('ih:0', '2:FQ'),
            *connect('ol:0', '3:FQ'),
            *connect('oh:0', '4:FQ'),
            *connect('FQ:0', 'output'),
        ],
                            nodes_with_edges_only=True)

        error_message = 'Unexpected number of FakeQuantize nodes {} CompressQuantizeWeights.quantize_data call `{}`'
        fq_nodes = graph.get_op_nodes(type='FakeQuantize')
        self.assertEqual(len(fq_nodes), 1,
                         error_message.format('before', len(fq_nodes)))
        fake_quantize = fq_nodes[0]

        CompressQuantizeWeights.quantize_data(fake_quantize, original_type,
                                              np.int8, "signed")
        graph.clean_up()

        fq_nodes = graph.get_op_nodes(type='FakeQuantize')
        self.assertEqual(len(fq_nodes), 1,
                         error_message.format('after', len(fq_nodes)))
        self.assertEqual(
            fq_nodes[0].in_port(0).get_source().node.soft_get('type'), 'Const')
        self.assertEqual(fq_nodes[0].in_port(0).get_source().node.data_type,
                         np.int8)

        graph_ref = build_graph(nodes, [
            *connect('int_weights:0', '0:FQ'),
            *connect('il:0', '1:FQ'),
            *connect('ih:0', '2:FQ'),
            *connect('ol:0', '3:FQ'),
            *connect('oh:0', '4:FQ'),
            *connect('FQ:0', 'output'),
        ],
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #18
0
    def get_graphs(input_shape, reshape_0_pattern, order, reshape_1_pattern,
                   block_size):
        nodes = {
            **regular_op_with_shaped_data(
                'input', input_shape, {
                    'type': 'Parameter',
                    'shape': int64_array(input_shape),
                    'infer': Parameter.infer
                }),
            **valued_const_with_data('reshape_0_pattern',
                                     int64_array(reshape_0_pattern)),
            **regular_op_with_empty_data('reshape_0', {
                'type': 'Reshape',
                'infer': Reshape.infer
            }),
            **valued_const_with_data('order', int64_array(order)),
            **regular_op_with_empty_data('transpose', {
                'type': 'Transpose',
                'infer': Transpose.infer
            }),
            **valued_const_with_data('reshape_1_pattern',
                                     int64_array(reshape_1_pattern)),
            **regular_op_with_empty_data('reshape_1', {
                'type': 'Reshape',
                'infer': Reshape.infer,
                'name': 'final_reshape'
            }),
            **result(),
        }
        edges = [
            *connect('input', '0:reshape_0'),
            *connect('reshape_0_pattern', '1:reshape_0'),
            *connect('reshape_0', '0:transpose'),
            *connect('order', '1:transpose'),
            *connect('transpose', '0:reshape_1'),
            *connect('reshape_1_pattern', '1:reshape_1'),
            *connect('reshape_1', 'output'),
        ]
        graph = build_graph(nodes,
                            edges,
                            nodes_with_edges_only=True,
                            cli=Namespace())
        for node in graph.get_op_nodes():
            node['op'] = node['type']
        graph.clean_up()

        ref_nodes = {
            **regular_op_with_shaped_data(
                'input', input_shape, {
                    'type': 'Parameter',
                    'shape': int64_array(input_shape),
                    'infer': Parameter.infer
                }),
            **regular_op_with_empty_data(
                'depth_to_space', {
                    'type': 'DepthToSpace',
                    'infer': DepthToSpaceOp.infer,
                    'name': 'final_reshape',
                    'block_size': block_size
                }),
            **result()
        }
        ref_edges = [
            *connect('input', 'depth_to_space'),
            *connect('depth_to_space', 'output')
        ]
        graph_ref = build_graph(ref_nodes,
                                ref_edges,
                                nodes_with_edges_only=True)
        for node in graph_ref.get_op_nodes():
            node['op'] = node['type']
        graph_ref.clean_up()
        graph.graph['layout'] = 'NCHW'
        graph_ref.graph['layout'] = 'NCHW'

        return graph, graph_ref
Пример #19
0
    def test_broadcast_with_range_positive_test(self):
        graph = build_graph(
            {
                **regular_op_with_shaped_data('shape', [2], {
                                                  'type': 'Parameter'
                                              }),
                **valued_const_with_data('value',
                                         np.arange(0, 384).reshape((1, 384))),
                **regular_op_with_empty_data('bc', {'type': 'Broadcast'}),
                **result(),
            }, [
                *connect('value', '0:bc'),
                *connect('shape', '1:bc'),
                *connect('bc', 'output'),
            ],
            nodes_with_edges_only=True)
        ExpandRangeConstant().find_and_replace_pattern(graph)

        graph_ref = build_graph(
            {
                **regular_op_with_shaped_data('shape', [2], {
                                                  'type': 'Parameter'
                                              }),

                # start
                **valued_const_with_data('start', np.array(0)),
                # limit
                **valued_const_with_data('minus_one', np.array(-1)),
                **valued_const_with_data('zero', np.array(0)),
                **regular_op_with_empty_data('range_dim', {'type': 'Gather'}),
                # delta
                **valued_const_with_data('delta', np.array(1)),
                **regular_op_with_empty_data('range', {'type': 'Range'}),

                # keep dims
                **valued_const_with_data('axes', np.array([0])),
                **regular_op_with_empty_data('keep_shape', {
                    'type': 'Unsqueeze'
                }),
                **regular_op_with_empty_data('bc', {'type': 'Broadcast'}),
                **result(),
            },
            [
                *connect('start', '0:range'),
                *connect('shape', '0:range_dim'),
                *connect('minus_one', '1:range_dim'),
                *connect('zero', '2:range_dim'),
                *connect('range_dim', '1:range'),
                *connect('delta', '2:range'),
                *connect('range', '0:keep_shape'),
                *connect('axes', '1:keep_shape'),
                *connect('keep_shape', '0:bc'),
                *connect_data('shape', '1:bc'),
                *connect('bc', 'output'),
            ],
            nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #20
0
    def get_graphs(input_shape, reshape_0_pattern, order, reshape_1_pattern,
                   group):
        nodes = {
            **regular_op_with_shaped_data(
                'input', input_shape, {
                    'type': 'Parameter',
                    'shape': int64_array(input_shape),
                    'infer': Parameter.infer
                }),
            **valued_const_with_data('reshape_0_pattern',
                                     int64_array(reshape_0_pattern)),
            **regular_op_with_empty_data('reshape_0', {
                'type': 'Reshape',
                'infer': Reshape.infer
            }),
            **valued_const_with_data('order', int64_array(order)),
            **regular_op_with_empty_data('transpose', {
                'type': 'Transpose',
                'infer': Transpose.infer
            }),
            **valued_const_with_data('reshape_1_pattern',
                                     int64_array(reshape_1_pattern)),
            **regular_op_with_empty_data('reshape_1', {
                'type': 'Reshape',
                'infer': Reshape.infer,
                'name': 'final_reshape'
            }),
            **result(),
        }
        edges = [
            *connect('input', '0:reshape_0'),
            *connect('reshape_0_pattern', '1:reshape_0'),
            *connect('reshape_0', '0:transpose'),
            *connect('order', '1:transpose'),
            *connect('transpose', '0:reshape_1'),
            *connect('reshape_1_pattern', '1:reshape_1'),
            *connect('reshape_1', 'output'),
        ]
        graph = build_graph(nodes, edges, nodes_with_edges_only=True)
        for node in graph.get_op_nodes():
            node['op'] = node['type']
        graph.clean_up()

        ref_nodes = {
            **regular_op_with_shaped_data(
                'input', input_shape, {
                    'type': 'Parameter',
                    'shape': int64_array(input_shape),
                    'infer': Parameter.infer
                }),
            **regular_op_with_empty_data(
                'shuffle_channel', {
                    'type': 'ShuffleChannels',
                    'infer': ShuffleChannels.infer,
                    'name': 'final_reshape',
                    'group': group
                }),
            **result()
        }
        ref_edges = [
            *connect('input', 'shuffle_channel'),
            *connect('shuffle_channel', 'output')
        ]
        graph_ref = build_graph(ref_nodes,
                                ref_edges,
                                nodes_with_edges_only=True)
        for node in graph_ref.get_op_nodes():
            node['op'] = node['type']
        graph_ref.clean_up()

        return graph, graph_ref
    **regular_op_with_empty_data('placeholder_1', {'type': 'Parameter'}),
    **regular_op_with_empty_data('placeholder_2', {'type': 'Parameter'}),
    **regular_op_with_empty_data('placeholder_3', {'type': 'Parameter'}),
    **regular_op_with_empty_data('node', {
        'op': 'ScatterElementsUpdate',
        'is_scatter': True
    }),
    **regular_op_with_empty_data('axis', {
        'type': 'Const',
        'value': None
    }),
    **result(),
}

edges = [
    *connect('placeholder_1', '0:node'),
    *connect('placeholder_2', '1:node'),
    *connect('placeholder_3', '2:node'),
    *connect('node', 'output'),
]


class TestDiv(unittest.TestCase):
    def test_ScatterElementsUpdate_has_axis_and_3_inputs(self):
        graph = build_graph(nodes,
                            edges, {'node': {
                                'axis': 1
                            }},
                            nodes_with_edges_only=True)
        ScatterNormalizer().find_and_replace_pattern(graph)
Пример #22
0
        'op': 'Const',
        'type': 'Const'
    }),
    **regular_op_with_empty_data(
        'ss', {
            'op': 'StridedSlice',
            'type': 'StridedSlice',
            'new_axis_mask': np.zeros(4, dtype=np.int64),
            'shrink_axis_mask': np.zeros(4, dtype=np.int64),
            'ellipsis_mask': np.zeros(4, dtype=np.int64)
        }),
    **result('result')
}

pattern_graph = [
    *connect('input:0', '0:slice'), *connect('starts:0', '1:slice'),
    *connect('ends:0', '2:slice'), *connect('axes:0', '3:slice'),
    *connect('steps:0', '4:slice'), *connect('slice:0', '0:result')
]

pattern_ref_graph = [
    *connect('input:0', '0:ss'),
    *connect('starts:0', '0:ss_begin_clamp'),
    *connect('ss_begin_clamp:0', '0:ss_begin_cast'),
    *connect('ss_begin_clamp_min:0', '1:ss_begin_clamp'),
    *connect('ss_begin_clamp_max:0', '2:ss_begin_clamp'),
    *connect('ss_begin_concat:0', '1:ss'),
    *connect('ends:0', '0:ss_end_clamp'),
    *connect('ss_end_clamp:0', '0:ss_end_cast'),
    *connect('ss_end_clamp_min:0', '1:ss_end_clamp'),
    *connect('ss_end_clamp_max:0', '2:ss_end_clamp'),
def graph_template(weights_initial_shape,
                   new_reshape_shape,
                   limits_initial_shape,
                   limits_new_shape=None):
    limits_new_shape = limits_initial_shape if limits_new_shape is None else limits_new_shape

    core_connections = [
        *connect('input:0', '0:convolution'),
        *connect('convolution:0', '0:output'),
    ]

    core_nodes = lambda weights_shape, limit_shape, reshape_shape: {
        **regular_op_with_shaped_data('input', None, {
            'type': 'Parameter',
            'op': 'Parameter'
        }),
        **valued_const_with_data('weights', np.ones(weights_shape)),
        **const_with_data('dim', int64_array(reshape_shape)),
        **regular_op_with_shaped_data('reshape', reshape_shape, {
            'type': 'Reshape',
            'infer': Reshape.infer,
            'op': 'Reshape'
        }),
        **valued_const_with_data('il', np.ones(limit_shape)),
        **valued_const_with_data('ih', np.ones(limit_shape)),
        **valued_const_with_data('ol', np.ones(limit_shape)),
        **valued_const_with_data('oh', np.ones(limit_shape)),
        **regular_op_with_shaped_data(
            'FQ', weights_shape, {
                'type': 'FakeQuantize',
                'infer': FakeQuantize.infer,
                'stop_value_propagation': True,
                'levels': 2,
                'op': 'FakeQuantize'
            }),
        **regular_op_with_shaped_data('convolution', None, {
            'type': 'Convolution',
            'op': 'Convolution'
        }),
        **result(),
    }

    nodes_before = core_nodes(weights_initial_shape, limits_initial_shape,
                              new_reshape_shape)
    edges_before = [
        *connect('weights:0', '0:FQ'),
        *connect('il:0', '1:FQ'),
        *connect('ih:0', '2:FQ'),
        *connect('ol:0', '3:FQ'),
        *connect('oh:0', '4:FQ'),
        *connect('FQ:0', '0:reshape'),
        *connect('dim:0', '1:reshape'),
        *connect('reshape:0', '1:convolution'),
        *core_connections,
    ]
    graph = build_graph(nodes_attrs=nodes_before,
                        edges=edges_before,
                        nodes_with_edges_only=True)

    nodes_after = core_nodes(new_reshape_shape, limits_new_shape, [])
    edges_after = [
        *connect('weights:0', '0:FQ'),
        *connect('il:0', '1:FQ'),
        *connect('ih:0', '2:FQ'),
        *connect('ol:0', '3:FQ'),
        *connect('oh:0', '4:FQ'),
        *connect('FQ:0', '1:convolution'),
        *core_connections,
    ]
    graph_ref = build_graph(nodes_attrs=nodes_after,
                            edges=edges_after,
                            nodes_with_edges_only=True)
    return graph, graph_ref
Пример #24
0
    def test_convert_slice_to_strided_slice_without_axes_and_steps(self):
        graph = build_graph(nodes_attrs=nodes_attributes,
                            edges=[
                                *connect('input:0', '0:slice'),
                                *connect('starts:0', '1:slice'),
                                *connect('ends:0', '2:slice'),
                                *connect('slice:0', '0:result')
                            ],
                            update_attributes={
                                'starts': {
                                    'value': int64_array([0, 0, 0, 0]),
                                    'shape': [4]
                                },
                                'ends': {
                                    'value': int64_array([1, 2, 150, 150]),
                                    'shape': [4]
                                },
                            },
                            nodes_with_edges_only=True)

        ref_graph = build_graph(
            nodes_attrs=nodes_attributes,
            edges=pattern_ref_graph + [
                *connect('ss_begin_cast:0', '0:ss_begin_gather_0'),
                *connect('ss_begin_gather_0:0', '0:ss_begin_concat'),
                *connect_data('ss_begin_cast:0', '0:ss_begin_gather_1'),
                *connect('ss_begin_gather_1:0', '1:ss_begin_concat'),
                *connect_data('ss_begin_cast:0', '0:ss_begin_gather_2'),
                *connect('ss_begin_gather_2:0', '2:ss_begin_concat'),
                *connect_data('ss_begin_cast:0', '0:ss_begin_gather_3'),
                *connect('ss_begin_gather_3:0', '3:ss_begin_concat'),
                *connect('ss_end_cast:0', '0:ss_end_gather_0'),
                *connect('ss_end_gather_0:0', '0:ss_end_concat'),
                *connect_data('ss_end_cast:0', '0:ss_end_gather_1'),
                *connect('ss_end_gather_1:0', '1:ss_end_concat'),
                *connect_data('ss_end_cast:0', '0:ss_end_gather_2'),
                *connect('ss_end_gather_2:0', '2:ss_end_concat'),
                *connect_data('ss_end_cast:0', '0:ss_end_gather_3'),
                *connect('ss_end_gather_3:0', '3:ss_end_concat'),
            ],
            update_attributes={
                'starts': {
                    'value': int64_array([0, 0, 0, 0]),
                    'shape': [4]
                },
                'ends': {
                    'value': int64_array([1, 2, 150, 150]),
                    'shape': [4]
                },
                'ss_strides': {
                    'value': int64_array([1, 1, 1, 1]),
                    'shape': [4]
                },
                'ss': {
                    'begin_mask': int64_array([1, 1, 1, 1]),
                    'end_mask': int64_array([1, 1, 1, 1])
                }
            })
        ConvertSlice().find_and_replace_pattern(graph)
        (flag, resp) = compare_graphs(graph,
                                      ref_graph,
                                      'result',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #25
0
            'value': data,
            'shape': int64_array(data.shape)
        },
        **const_with_data('depth', int64_array(depth)),
        **const_with_data('on_value', float_array(on_value)),
        **const_with_data('off_value', float_array(off_value)),
        **regular_op_with_shaped_data('one_hot', None, {
            'type': 'OneHot',
            'axis': axis,
            'Op': 'OneHot'
        })
    }


edges = [
    *connect('indices:0', 'one_hot:0'), *connect('depth:0', 'one_hot:1'),
    *connect('on_value:0', 'one_hot:2'), *connect('off_value:0', 'one_hot:3'),
    ('one_hot', 'one_hot_d')
]


@generator
class TestOneHotInfer(unittest.TestCase):
    @generate(*[
        # 0d input
        (1, [0, 1, 0, 0]),
        # 1d input
        ([1, 2], [[0, 1, 0, 0], [0, 0, 1, 0]]),
        # 2D input
        ([[1, 2], [3, 4]], [[[0, 1, 0, 0], [0, 0, 1, 0]],
                            [[0, 0, 0, 1], [0, 0, 0, 0]]]),
    def test_dequantize(self):
        original_type = np.float32
        nodes = nodes_dict(original_type, np.int8)

        graph = build_graph(nodes, [
            *connect('weights:0', '0:cast'),
            *connect('cast:0', '0:FQ'),
            *connect('il:0', '1:FQ'),
            *connect('ih:0', '2:FQ'),
            *connect('ol:0', '3:FQ'),
            *connect('oh:0', '4:FQ'),
            *connect('FQ:0', 'output'),
        ],
                            nodes_with_edges_only=True)

        error_message = 'Unexpected number of {} nodes {} CompressQuantizeWeights.dequantize_data call `{}`'
        fq_nodes = graph.get_op_nodes(type='FakeQuantize')
        cast_nodes = graph.get_op_nodes(name='cast')
        self.assertEqual(
            len(fq_nodes), 1,
            error_message.format('FakeQuantize', 'before', len(fq_nodes)))
        self.assertEqual(
            len(cast_nodes), 1,
            error_message.format('Convert', 'before', len(cast_nodes)))
        cast_nodes[0]['need_shape_inference'] = True

        CompressQuantizeWeights.dequantize_data(fq_nodes[0], original_type,
                                                np.int8)
        graph.clean_up()

        fq_nodes = graph.get_op_nodes(type='FakeQuantize')
        self.assertEqual(
            len(fq_nodes), 0,
            error_message.format('FakeQuantize', 'after', len(fq_nodes)))

        graph_ref = build_graph(nodes, [
            *connect('int_weights:0', '0:cast'),
            *connect('cast:0', '0:sub'),
            *connect('zp:0', '1:sub'),
            *connect('sub:0', '0:mul'),
            *connect('scale:0', '1:mul'),
            *connect('mul:0', 'output'),
        ], {'cast': {
            'dst_type': original_type
        }},
                                nodes_with_edges_only=True)

        (flag, resp) = compare_graphs(graph,
                                      graph_ref,
                                      'output',
                                      check_op_attrs=True)
        self.assertTrue(flag, resp)
Пример #27
0
    def test_multi(self):
        nodes = {
            **regular_op_with_empty_data('input', {'type': 'Parameter'}),
            **regular_op_with_empty_data('some_op', {
                'type': 'SomeOp',
                'name': 'some_op_name'
            }),
            **empty_data('some_op_d2'),
            **regular_op_with_empty_data(
                'fake_output1', {
                    'type': None,
                    'kind': 'op',
                    'op': 'FakeOutput',
                    'name': 'my_output_name1'
                }),
            **regular_op_with_empty_data(
                'fake_output2', {
                    'type': None,
                    'kind': 'op',
                    'op': 'FakeOutput',
                    'name': 'my_output_name2'
                }),
            **valued_const_with_data('const1', int64_array(0)),
            **valued_const_with_data('const2', int64_array(0)),
            **regular_op_with_empty_data('add1', {
                'type': None,
                'kind': 'op',
                'op': 'Add',
                'name': 'my_output_name1'
            }),
            **regular_op_with_empty_data('add2', {
                'type': None,
                'kind': 'op',
                'op': 'Add',
                'name': 'my_output_name2'
            }),
            **result('result1'),
            **result('result2'),
        }
        edges = [
            *connect('input', 'some_op'),
            *connect('some_op', 'fake_output1'),
            ('some_op', 'some_op_d2'),
            ('some_op_d2', 'fake_output2'),
            *connect('fake_output1', 'result1'),
            *connect('fake_output2', 'result2'),
        ]
        graph = build_graph(nodes, edges)

        edges_ref = [
            *connect('input', 'some_op'),
            *connect('some_op', '0:add1'),
            *connect('const1', '1:add1'),
            ('some_op', 'some_op_d2'),
            ('some_op_d2', 'add2', {
                'in': 0
            }),
            *connect('const2', '1:add2'),
            *connect('add1', 'result1'),
            *connect('add2', 'result2'),
        ]

        graph_ref = build_graph(nodes, edges_ref)

        FakeOutputResolver().find_and_replace_pattern(graph)

        (flag, resp) = compare_graphs(graph, graph_ref, 'result1')
        self.assertTrue(flag, resp)
Пример #28
0
        'type': 'Parameter',
        'op': 'Parameter'
    }),
    **valued_const_with_data('gamma', float_array([0.5])),
    **valued_const_with_data('beta', float_array([0.5])),
    **regular_op_with_shaped_data('group_norm', shape, {
        'op': 'GroupNorm',
        'name': 'group_norm',
        'num_groups': 3,
        'eps': 1e-9
    }),
    **result('result')
}

edges = [
    *connect('input:0', '0:group_norm'),
    *connect('gamma', '1:group_norm'),
    *connect('beta', '2:group_norm'),
    *connect('group_norm:0', 'result'),
]

ref_nodes = {
    **regular_op_with_shaped_data('input', shape, {
        'type': 'Parameter',
        'op': 'Parameter'
    }),
    **regular_op_with_shaped_data('shape1', int64_array([4]), {
                                      'op': 'ShapeOf'
                                  }),
    **regular_op_with_shaped_data('shape2', int64_array([4]), {
                                      'op': 'ShapeOf'