diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index c28a7a35384f968664b81091175546ec95beedf1..821a974dd7863b697114fb5a9eee8c34b1d1286c 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -121,7 +121,6 @@ __all__ = [ 'gaussian_random_batch_size_like', 'sum', 'slice', - 'strided_slice', 'shape', 'size', 'clip', @@ -7530,227 +7529,6 @@ def slice(input, axes, starts, ends): return out -@deprecated(since='2.0.0', update_to="paddle.strided_slice") -def strided_slice(input, axes, starts, ends, strides): - """ - :alias_main: paddle.strided_slice - :alias: paddle.strided_slice,paddle.tensor.strided_slice,paddle.tensor.manipulation.strided_slice - :old_api: paddle.fluid.layers.strided_slice - - This operator produces a slice of ``input`` along multiple axes. Similar to numpy: - https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html - Slice uses ``axes``, ``starts`` and ``ends`` attributes to specify the start and - end dimension for each axis in the list of axes and Slice uses this information - to slice the input data tensor. If a negative value is passed to - ``starts`` or ``ends`` such as :math:`-i`, it represents the reverse position of the - axis :math:`i-1` th(here 0 is the initial position). The ``strides`` represents steps of - slicing and if the ``strides`` is negative, slice operation is in the opposite direction. - If the value passed to ``starts`` or ``ends`` is greater than n - (the number of elements in this dimension), it represents n. - For slicing to the end of a dimension with unknown size, it is recommended - to pass in INT_MAX. The size of ``axes`` must be equal to ``starts`` , ``ends`` and ``strides``. - Following examples will explain how strided_slice works: - - .. code-block:: text - - Case1: - Given: - data = [ [1, 2, 3, 4], [5, 6, 7, 8], ] - axes = [0, 1] - starts = [1, 0] - ends = [2, 3] - strides = [1, 1] - Then: - result = [ [5, 6, 7], ] - - Case2: - Given: - data = [ [1, 2, 3, 4], [5, 6, 7, 8], ] - axes = [0, 1] - starts = [0, 1] - ends = [2, 0] - strides = [1, -1] - Then: - result = [ [8, 7, 6], ] - - Case3: - Given: - data = [ [1, 2, 3, 4], [5, 6, 7, 8], ] - axes = [0, 1] - starts = [0, 1] - ends = [-1, 1000] - strides = [1, 3] - Then: - result = [ [2], ] - Args: - input (Variable): An N-D ``Tensor`` or ``LoDTensor`` . The data type is ``bool``, ``float32``, ``float64``, ``int32`` or ``int64``. - axes (list|tuple): The data type is ``int32`` . Axes that `starts` and `ends` apply to. - It's optional. If it is not provides, it will be treated as :math:`[0,1,...,len(starts)-1]`. - starts (list|tuple|Variable): The data type is ``int32`` . If ``starts`` is a list or tuple, the elements of - it should be integers or Tensors with shape [1]. If ``starts`` is an Variable, it should be an 1-D Tensor. - It represents starting indices of corresponding axis in ``axes``. - ends (list|tuple|Variable): The data type is ``int32`` . If ``ends`` is a list or tuple, the elements of - it should be integers or Tensors with shape [1]. If ``ends`` is an Variable, it should be an 1-D Tensor . - It represents ending indices of corresponding axis in ``axes``. - strides (list|tuple|Variable): The data type is ``int32`` . If ``strides`` is a list or tuple, the elements of - it should be integers or Tensors with shape [1]. If ``strides`` is an Variable, it should be an 1-D Tensor . - It represents slice step of corresponding axis in ``axes``. - - Returns: - Variable: A ``Tensor`` or ``LoDTensor`` with the same dimension as ``input``. The data type is same as ``input``. - - Raises: - TypeError: The type of ``starts`` must be list, tuple or Variable. - TypeError: The type of ``ends`` must be list, tuple or Variable. - TypeError: The type of ``strides`` must be list, tuple or Variable. - - Examples: - .. code-block:: python - - import paddle.fluid as fluid - import paddle - - paddle.enable_static() - input = fluid.data( - name="input", shape=[3, 4, 5, 6], dtype='float32') - - # example 1: - # attr starts is a list which doesn't contain tensor Variable. - axes = [0, 1, 2] - starts = [-3, 0, 2] - ends = [3, 2, 4] - strides_1 = [1, 1, 1] - strides_2 = [1, 1, 2] - sliced_1 = fluid.layers.strided_slice(input, axes=axes, starts=starts, ends=ends, strides=strides_1) - # sliced_1 is input[:, 0:3:1, 0:2:1, 2:4:1]. - - - # example 2: - # attr starts is a list which contain tensor Variable. - minus_3 = fluid.layers.fill_constant([1], "int32", -3) - sliced_2 = fluid.layers.strided_slice(input, axes=axes, starts=[minus_3, 0, 2], ends=ends, strides=strides_2) - # sliced_2 is input[:, 0:3:1, 0:2:1, 2:4:2]. - """ - if in_dygraph_mode(): - return _C_ops.strided_slice(input, axes, starts, ends, strides) - - helper = LayerHelper('strided_slice', **locals()) - - check_variable_and_dtype( - input, - 'input', - ['bool', 'float32', 'float64', 'int32', 'int64'], - 'strided_slice', - ) - check_type(axes, 'axes', (list, tuple), 'strided_slice') - check_type(starts, 'starts', (list, tuple, Variable), 'strided_slice') - check_type(ends, 'ends', (list, tuple, Variable), 'strided_slice') - check_type(strides, 'strides', (list, tuple, Variable), 'strided_slice') - - def check_list_elements_dtype(list_input, input_name): - if isinstance(list_input, Variable): - check_dtype( - list_input.dtype, input_name, ['int32'], 'strided_slice' - ) - else: - for i, var in enumerate(list_input): - var_name = input_name + '[' + str(i) + ']' - if isinstance(var, Variable): - check_dtype(var.dtype, var_name, ['int32'], 'strided_slice') - - check_list_elements_dtype(axes, 'axes') - check_list_elements_dtype(starts, 'starts') - check_list_elements_dtype(ends, 'ends') - check_list_elements_dtype(strides, 'strides') - - def get_new_list_tensor(old_list): - new_list_tensor = [] - for dim in old_list: - if isinstance(dim, Variable): - dim.stop_gradient = True - new_list_tensor.append(dim) - else: - assert isinstance(dim, int) - temp_out = helper.create_variable_for_type_inference('int32') - fill_constant([1], 'int32', dim, force_cpu=True, out=temp_out) - new_list_tensor.append(temp_out) - return new_list_tensor - - inputs = {'Input': input} - attrs = {'axes': axes} - infer_flags = list(1 for i in range(len(axes))) - - if _non_static_mode(): - inputs = {'Input': input} - attrs = { - 'axes': axes, - 'starts': starts, - 'ends': ends, - 'strides': strides, - 'infer_flags': infer_flags, - } - else: - # starts - if isinstance(starts, Variable): - starts.stop_gradient = True - inputs['StartsTensor'] = starts - elif isinstance(starts, (list, tuple)): - attrs['starts'] = [] - if utils._contain_var(starts): - inputs['StartsTensorList'] = get_new_list_tensor(starts) - for i, dim in enumerate(starts): - if isinstance(dim, Variable): - attrs['starts'].append(-1) - infer_flags[i] = -1 - else: - attrs['starts'].append(dim) - else: - attrs['starts'] = starts - - # ends - if isinstance(ends, Variable): - ends.stop_gradient = True - inputs['EndsTensor'] = ends - elif isinstance(ends, (list, tuple)): - attrs['ends'] = [] - if utils._contain_var(ends): - inputs['EndsTensorList'] = get_new_list_tensor(ends) - for i, dim in enumerate(ends): - if isinstance(dim, Variable): - attrs['ends'].append(-1) - infer_flags[i] = -1 - else: - attrs['ends'].append(dim) - else: - attrs['ends'] = ends - - # strides - if isinstance(strides, Variable): - strides.stop_gradient = True - inputs['StridesTensor'] = strides - elif isinstance(strides, (list, tuple)): - attrs['strides'] = [] - if utils._contain_var(strides): - inputs['StridesTensorList'] = get_new_list_tensor(strides) - for i, dim in enumerate(strides): - if isinstance(dim, Variable): - attrs['strides'].append(-1) - infer_flags[i] = -1 - else: - attrs['strides'].append(dim) - else: - attrs['strides'] = strides - attrs['infer_flags'] = infer_flags - out = helper.create_variable_for_type_inference( - dtype=helper.input_dtype('input') - ) - helper.append_op( - type='strided_slice', inputs=inputs, attrs=attrs, outputs={'Out': out} - ) - - return out - - def shape(input): """ :alias_main: paddle.shape diff --git a/python/paddle/fluid/tests/unittests/ipu/test_strided_slice_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_strided_slice_op_ipu.py index 293144018947463296c4be643d986f771f5e5e5a..62ef7151fd1b9f282c12767f828b013c29f89231 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_strided_slice_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_strided_slice_op_ipu.py @@ -52,7 +52,7 @@ class TestBase(IPUOpTest): x = paddle.static.data( name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32' ) - out = paddle.fluid.layers.strided_slice(x, **self.attrs) + out = paddle.strided_slice(x, **self.attrs) self.fetch_list = [out.name] def run_model(self, exec_mode): @@ -128,9 +128,7 @@ class TestCase3(TestBase): ends = paddle.static.data( name=self.feed_list[2], shape=self.feed_shape[2], dtype='int32' ) - out = paddle.fluid.layers.strided_slice( - x, starts=starts, ends=ends, **self.attrs - ) + out = paddle.strided_slice(x, starts=starts, ends=ends, **self.attrs) self.fetch_list = [out.name] diff --git a/python/paddle/fluid/tests/unittests/npu/test_strided_slice_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_strided_slice_op_npu.py index 6dcd8fcdae2ba37f32e86085ecdbc2413674f900..c4470b101d18435919fcc6b9a3038c7e5df544bb 100755 --- a/python/paddle/fluid/tests/unittests/npu/test_strided_slice_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_strided_slice_op_npu.py @@ -596,28 +596,28 @@ class TestStridedSliceAPI(unittest.TestCase): append_batch_size=False, dtype="float64", ) - out_1 = fluid.layers.strided_slice( + out_1 = paddle.strided_slice( x, axes=[0, 1, 2], starts=[-3, 0, 2], ends=[3, 100, -1], strides=[1, 1, 1], ) - out_2 = fluid.layers.strided_slice( + out_2 = paddle.strided_slice( x, axes=[0, 1, 3], starts=[minus_3, 0, 2], ends=[3, 100, -1], strides=[1, 1, 1], ) - out_3 = fluid.layers.strided_slice( + out_3 = paddle.strided_slice( x, axes=[0, 1, 3], starts=[minus_3, 0, 2], ends=[3, 100, minus_1], strides=[1, 1, 1], ) - out_4 = fluid.layers.strided_slice( + out_4 = paddle.strided_slice( x, axes=[0, 1, 2], starts=starts, ends=ends, strides=strides ) diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 752a089dadf834f9cfdc15202401ecabe8a9dbb1..4a9c5f907a4469d5d33d18caedfd1ef58b1dcb43 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -3846,7 +3846,7 @@ class TestBook(LayerTest): strides = [1, 1, 1] with self.static_graph(): x = layers.data(name="x", shape=[245, 30, 30], dtype="float32") - out = layers.strided_slice( + out = paddle.strided_slice( x, axes=axes, starts=starts, ends=ends, strides=strides ) return out