def test_broadcast(self, data, target_shape, axes_mapping=None, mode='numpy', ref_out=None, test_raising=False):
        if ref_out is not None:
            input = valued_const_with_data('data', int64_array(data))
        else:
            input = shaped_data('data', int64_array(data))

        nodes = {
            **input,
            **valued_const_with_data('target_shape', int64_array(target_shape)),
            **regular_op_with_empty_data('broadcast', {'op': 'Broadcast', 'mode': mode}),
        }

        edges = [('data', 'broadcast'),
                 ('target_shape', 'broadcast'),
                 ('broadcast', 'broadcast_d')]

        if axes_mapping is not None:
            nodes.update(**valued_const_with_data('axes_mapping', int64_array(axes_mapping)))
            edges.append(('axes_mapping', 'broadcast'))
        graph = build_graph(nodes, edges)

        broadcast_node = Node(graph, 'broadcast')
        if test_raising:
            self.assertRaises(AssertionError, Broadcast.infer, broadcast_node)
            return

        Broadcast.infer(broadcast_node)
        if ref_out is not None:
            self.assertTrue(np.array_equal(broadcast_node.out_node().value, np.array(ref_out)))
        else:
            self.assertTrue(np.array_equal(broadcast_node.out_node().shape, np.array(target_shape)))
Exemple #2
0
    def test_broadcast_dynamic(self, data, target_shape_shape, mode='numpy', ref_out_shape=None, test_raising=False):
        nodes = {
            **shaped_data('data', int64_array(data)),
            **shaped_data('target_shape', int64_array(target_shape_shape)),
            **regular_op_with_empty_data('broadcast', {'op': 'Broadcast', 'mode': mode}),
        }

        edges = [('data', 'broadcast'),
                 ('target_shape', 'broadcast'),
                 ('broadcast', 'broadcast_d')]

        graph = build_graph(nodes, edges)

        broadcast_node = Node(graph, 'broadcast')
        if test_raising:
            self.assertRaises(AssertionError, Broadcast.infer, broadcast_node)
            return

        Broadcast.infer(broadcast_node)
        self.assertTrue(np.array_equal(broadcast_node.out_node().shape, ref_out_shape))
Exemple #3
0
    def test_slice_infer(self, inp_value, inp_shape, starts, ends, axes, steps,
                         expected_value, expected_shape):
        if inp_value is None:
            input_node = shaped_data('data_1', int64_array(inp_shape))
        else:
            input_node = valued_data('data_1', int64_array(inp_value))
        if inp_value is not None and inp_shape is not None:
            assert np.array_equal(np.array(inp_value).shape, inp_shape)

        def convert_args(val, name=''):
            if val is not None:
                return valued_const_with_data(name, int64_array(val))
            else:
                return shaped_const_with_data(name, [0])  #fake shape

        starts = convert_args(starts, 'starts')
        ends = convert_args(ends, 'ends')
        axes = convert_args(axes, 'axes')
        steps = convert_args(steps, 'steps')
        if expected_shape is not None:
            expected_shape = shape_array(expected_shape)

        nodes = {
            **input_node,
            **regular_op_with_empty_data('slice', {'op': 'Slice'}),
            **starts,
            **ends,
            **axes,
            **steps,
        }

        graph = build_graph(
            nodes,
            [('data_1', 'slice'), *connect('starts', '1:slice'),
             *connect('ends', '2:slice'), *connect('axes', '3:slice'),
             *connect('steps', '4:slice'), *connect('slice', 'slice_d')])

        graph.stage = 'middle'
        slice_node = Node(graph, 'slice')

        Slice.infer(slice_node)
        if expected_value is not None:
            self.assertTrue(
                strict_compare_tensors(slice_node.out_node().value,
                                       expected_value))
        self.assertTrue(
            strict_compare_tensors(slice_node.out_node().shape,
                                   expected_shape))
Exemple #4
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
Exemple #5
0
    def test_slice_infer_negative(self,
                                  inp_value,
                                  starts,
                                  ends,
                                  axes,
                                  steps,
                                  expected,
                                  inp_shape=None):
        if inp_value is None:
            input_node = shaped_data('data_1', int64_array(inp_shape))
        else:
            input_node = valued_data('data_1', int64_array(inp_value))

        def convert_args(val, name=''):
            if val is not None:
                return valued_const_with_data(name, int64_array(val))
            else:
                return shaped_const_with_data(name, [0])  #fake shape

        starts = convert_args(starts, 'starts')
        ends = convert_args(ends, 'ends')
        axes = convert_args(axes, 'axes')
        steps = convert_args(steps, 'steps')

        nodes = {
            **input_node,
            **regular_op_with_empty_data('slice', {'op': 'Slice'}),
            **starts,
            **ends,
            **axes,
            **steps
        }

        graph = build_graph(
            nodes,
            [('data_1', 'slice'), *connect('starts', '1:slice'),
             *connect('ends', '2:slice'), *connect('axes', '3:slice'),
             *connect('steps', '4:slice'), *connect('slice', 'slice_d')])

        graph.stage = 'middle'
        slice_node = Node(graph, 'slice')
        self.assertRaises(Error, Slice.infer, slice_node)
Exemple #6
0
    def test_slice_infer(self,
                         inp_value,
                         starts,
                         ends,
                         axes,
                         steps,
                         expected,
                         inp_shape=None):
        if inp_value is None:
            input_node = shaped_data('data_1', int64_array(inp_shape))
        else:
            input_node = valued_data('data_1', int64_array(inp_value))

        nodes = {
            **input_node,
            **regular_op_with_empty_data('slice', {'op': 'Slice'}),
            **valued_const_with_data('starts', int64_array(starts)),
            **valued_const_with_data('ends', int64_array(ends)),
            **valued_const_with_data('axes', int64_array(axes)),
            **valued_const_with_data('steps', int64_array(steps)),
        }

        graph = build_graph(
            nodes,
            [('data_1', 'slice'), *connect('starts', '1:slice'),
             *connect('ends', '2:slice'), *connect('axes', '3:slice'),
             *connect('steps', '4:slice'), *connect('slice', 'slice_d')])

        graph.stage = 'middle'
        slice_node = Node(graph, 'slice')

        Slice.infer(slice_node)
        if inp_value is not None:
            self.assertTrue(
                np.array_equal(slice_node.out_node().value, expected))
        else:
            self.assertTrue(
                np.array_equal(slice_node.out_node().shape, expected))
Exemple #7
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
Exemple #8
0
from openvino.tools.mo.utils.ir_engine.compare_graphs import compare_graphs
from unit_tests.utils.graph import build_graph, result, connect, regular_op_with_shaped_data, regular_op, shaped_data, \
    valued_const_with_data, shaped_const_with_data, valued_data

nodes = {
    **regular_op_with_shaped_data('placeholder1', [1, 16, 10, 10], {
                                      'type': 'Parameter'
                                  }),
    **valued_const_with_data('split_1_axis', int64_array(1), {
                                 'type': 'Const'
                             }),
    **regular_op('split_1', {
        'type': 'Split',
        'can_be_fused': True
    }),
    **shaped_data('split_1_data1', [1, 4, 10, 10]),
    **shaped_data('split_1_data2', [1, 4, 10, 10]),
    **shaped_data('split_1_data3', [1, 4, 10, 10]),
    **shaped_data('split_1_data4', [1, 4, 10, 10]),
    **shaped_const_with_data('split_2_in_const_weights',
                             int64_array([3, 3, 4, 16]), {'type': 'Const'}),
    **regular_op('split_2', {'type': 'Split'}),
    **valued_data('split_2_data1', np.zeros([3, 3, 4, 4])),
    **valued_data('split_2_data2', np.zeros([3, 3, 4, 4])),
    **valued_data('split_2_data3', np.zeros([3, 3, 4, 4])),
    **valued_data('split_2_data4', np.zeros([3, 3, 4, 4])),
    **regular_op_with_shaped_data(
        'conv2d_1', [1, 4, 8, 8], {
            'type':
            'Convolution',
            'channel_dims':