Ejemplo n.º 1
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))
Ejemplo n.º 2
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)
Ejemplo n.º 3
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))
Ejemplo n.º 4
0
                               }),
 **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':
         np.array([1]),
         'pad':
         np.array([2, 2]),
         'stride':
         np.array([2, 2]),
         'get_weights_permute':
         PermuteAttrs.Permutation(perm=int64_array([3, 2, 0, 1]),