def test_slice_infer_constant(self): # Testing constant path case graph = build_graph( nodes_attributes, [('data_1', 'slice'), ('begin', 'slice'), ('size', 'slice'), ('slice', 'data_2')], { 'data_1': { 'shape': np.array([4]), 'value': np.array([1, 3, 224, 224]) }, 'slice': { 'start': np.array([1]), 'end': np.array([2]) }, 'size': { 'value': np.array([1]) }, 'begin': { 'value': np.array([1]) } }) slice_node = Node(graph, 'slice') Slice.infer(slice_node) self.assertTrue( np.array_equal(slice_node.out_node().value, np.array([3]))) self.assertTrue( np.array_equal(slice_node.out_node().shape, np.array([1]))) self.assertTrue( np.array_equal(slice_node['slices'], np.array([slice(1, 2, 1)])))
def test_slice_infer_non_constant(self): # Testing non-constant path case (when value in input is None) # with multiply params graph = build_graph( nodes_attributes, [('data_1', 'slice'), ('begin', 'slice'), ('size', 'slice'), ('slice', 'data_2')], { 'data_1': { 'shape': np.array([4, 5, 6]) }, 'slice': { 'start': np.array([1, 2]), 'end': np.array([4, 3]) }, 'size': { 'value': np.array([3, 1]) }, 'begin': { 'value': np.array([1, 2]) } }) slice_node = Node(graph, 'slice') Slice.infer(slice_node) self.assertTrue(np.array_equal(slice_node.out_node().value, None)) self.assertTrue( np.array_equal(slice_node.out_node().shape, np.array([3, 1, 6]))) self.assertTrue( np.array_equal( slice_node['slices'], np.array([slice(1, 4, 1), slice(2, 3, 1), slice(0, 6, 1)])))
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))
def test_slice_infer_multiply_params(self): # Test case when size[i] == -1 (that means all # remaining elements in dimension i are included in the slice) graph = build_graph( nodes_attributes, [('data_1', 'slice'), ('begin', 'slice'), ('size', 'slice'), ('slice', 'data_2')], { 'data_1': { 'shape': np.array([4, 5, 6]) }, 'slice': { 'start': np.array([1, 2]), 'end': np.array([4, 1]) }, 'size': { 'value': np.array([3, -1]) }, 'begin': { 'value': np.array([1, 2]) } }) slice_node = Node(graph, 'slice') Slice.infer(slice_node) self.assertTrue(np.array_equal(slice_node.out_node().value, None)) self.assertTrue( np.array_equal(slice_node.out_node().shape, np.array([3, 3, 6]))) self.assertTrue( np.array_equal( slice_node['slices'], np.array([slice(1, 4, 1), slice(2, 5, 1), slice(0, 6, 1)])))
def test_slice_onnx_10_opset_case(self): # check for negative end value in the case of ONNX 10 opset input = np.array([[4, 5, 6, 7], [2, 3, 5, 6], [5, 6, 8, 9], [5, 6, 8, 9]]) starts = np.array([0, 1]) ends = np.array([3, -2]) expected_values = np.array([[5], [3], [6]]) graph = build_graph( nodes_attributes, [('data_1', 'slice'), ('starts', 'slice'), ('ends', 'slice'), ('slice', 'data_2')], { 'data_1': { 'value': input, 'shape': input.shape }, 'starts': { 'value': starts, 'shape': starts.shape }, 'ends': { 'value': ends, 'shape': ends.shape }, 'slice': { 'format': 'onnx' } }) slice_node = Node(graph, 'slice') Slice.infer(slice_node) self.assertTrue( np.array_equal(slice_node.out_node().value, expected_values))
def test_1(self): """ Testing case with non-constant path and multiple slicing dimensions :return: """ graph = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'slice'), ('slice', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data'), ('output_data', 'op_output')], { 'placeholder_1_data': { 'shape': np.array([4, 5, 6]) }, 'slice': { 'start': np.array([1, 2, 3]), 'end': np.array([3, 4, 4]), 'axis': None }, }, nodes_with_edges_only=True, ) slice_node = Node(graph, 'slice') Slice.infer(slice_node) pattern = ConvertSlice() pattern.find_and_replace_pattern(graph) graph.clean_up() ss_node = Node(graph, graph.get_node_id_by_name('slice_node')) assert ss_node.type == 'Crop', 'Something wrong with transformed Slice node' graph_ref = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'crop'), ('crop', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data'), ('output_data', 'op_output')], { 'placeholder_1_data': { 'shape': np.array([4, 5, 6]) }, 'crop': { 'axis': np.array([0, 1, 2]), 'offset': np.array([1, 2, 3]), 'dim': np.array([2, 2, 1]) }, }, nodes_with_edges_only=True, ) (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True) self.assertTrue(flag, resp)
def test_no_steps_no_axes(self): input_shape = int64_array([5, 10, 20]) starts_value = int64_array([3, 2, 7]) ends_value = int64_array([5, 8, 15]) steps_value = int64_array([1, 1, 1]) masks_value = np.zeros([len(input_shape)], dtype=np.int64) graph = build_graph(self.nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'slice', {'in': 0}), ('starts', 'starts_data'), ('starts_data', 'slice', {'in': 1}), ('ends', 'ends_data'), ('ends_data', 'slice', {'in': 2}), ('slice', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data'), ('output_data', 'op_output') ], {'placeholder_1_data': {'shape': input_shape}, 'starts': {'shape': starts_value.shape, 'value': starts_value}, 'starts_data': {'shape': starts_value.shape, 'value': starts_value}, 'ends': {'shape': ends_value.shape, 'value': ends_value}, 'ends_data': {'shape': ends_value.shape, 'value': ends_value}, }, nodes_with_edges_only=True ) slice_node = Node(graph, 'slice') Slice.infer(slice_node) pattern = ConvertSlice() pattern.find_and_replace_pattern(graph) ss_node = Node(graph, graph.get_node_id_by_name('slice_node')) assert ss_node.type == 'StridedSlice', 'Something wrong with transformed Slice node' graph_ref = build_graph(self.nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'strided_slice', {'in': 0}), ('starts', 'starts_data'), ('starts_data', 'strided_slice', {'in': 1}), ('ends', 'ends_data'), ('ends_data', 'strided_slice', {'in': 2}), ('strides', 'strides_data'), ('strides_data', 'strided_slice', {'in': 3}), ('strided_slice', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data'), ('output_data', 'op_output') ], {'placeholder_1_data': {'shape': input_shape}, 'strided_slice': {'new_axis_mask': masks_value, 'shrink_axis_mask': masks_value, 'ellipsis_mask': masks_value, 'begin_mask': np.ones([3]), 'end_mask': np.ones([3])}, 'slice_data': {'shape': int64_array([2, 6, 8])} }, nodes_with_edges_only=True ) (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True) self.assertTrue(flag, resp)
def test_2(self): """ Testing case with constant path and one slicing dimension """ graph = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'slice'), ('slice', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data')], { 'placeholder_1_data': { 'shape': np.array([4, 5, 6]) }, 'slice': { 'start': np.array([1]), 'end': np.array([3]), 'axis': None }, 'output_op': { 'is_output': True } }) slice_node = Node(graph, 'slice') Slice.infer(slice_node) pattern = ConvertSlice() pattern.find_and_replace_pattern(graph) graph_ref = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'strided_slice'), ('strided_slice', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data')], { 'placeholder_1_data': { 'shape': np.array([4, 5, 6]) }, 'strided_slice': { 'slices': np.array([slice(1, 3, 1), slice(0, 5, 1), slice(0, 6, 1)]), 'shrink_axis_mask': np.array([False, False, False]) }, 'output_op': { 'is_output': True } }) (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True) self.assertTrue(flag, resp)
def test_1(self): """ Testing case with non-constant path and multiple slicing dimensions :return: """ graph = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'slice'), ('slice', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data')], { 'placeholder_1_data': { 'shape': np.array([4, 5, 6]) }, 'slice': { 'start': np.array([1, 2, 3]), 'end': np.array([3, 4, 4]), 'axis': None }, 'output_op': { 'is_output': True }, }) slice_node = Node(graph, 'slice') Slice.infer(slice_node) pattern = ConvertSlice() pattern.find_and_replace_pattern(graph) graph_ref = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'crop'), ('crop', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data')], { 'placeholder_1_data': { 'shape': np.array([4, 5, 6]) }, 'crop': { 'axis': np.array([0, 1, 2]), 'offset': np.array([1, 2, 3]), }, 'output_op': { 'is_output': True }, 'dim': { 'dim': np.array([2, 2, 1]) }, }) (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True) self.assertTrue(flag, resp)
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))
def test_3(self): """ Testing case with constant path and one slicing dimension """ graph = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_1_data', 'slice'), ('slice', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data'), ('output_data', 'op_output')], { 'placeholder_1_data': { 'shape': np.array([1, 5, 6]) }, 'slice': { 'start': np.array([1]), 'end': np.array([3]), 'axis': np.array([1]) } }, nodes_with_edges_only=True, ) graph.graph['layout'] = 'NHWC' slice_node = Node(graph, 'slice') Slice.infer(slice_node) pattern = ConvertSlice() pattern.find_and_replace_pattern(graph) graph.clean_up() ss_node = Node(graph, graph.get_node_id_by_name('slice_node')) assert ss_node.type == 'StridedSlice', 'Something wrong with transformed Slice node' graph_ref = build_graph( nodes_attributes, [('placeholder_1', 'placeholder_1_data'), ('placeholder_2', 'placeholder_2_data'), ('placeholder_3', 'placeholder_3_data'), ('placeholder_1_data', 'strided_slice'), ('placeholder_2_data', 'strided_slice'), ('placeholder_3_data', 'strided_slice'), ('strided_slice', 'slice_data'), ('slice_data', 'output_op'), ('output_op', 'output_data'), ('output_data', 'op_output')], { 'placeholder_1_data': { 'shape': np.array([1, 5, 6]) }, 'strided_slice': { 'slices': np.array([slice(0, 1, 1), slice(1, 3, 1), slice(0, 6, 1)]), 'shrink_axis_mask': np.array([False, False, False]) }, }, nodes_with_edges_only=True, ) (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True) self.assertTrue(flag, resp)