slice

Description

Extracts a slice from an input tensor based on begin and end.

See the following example:

  • tensor: [[1,2,3],[4,5,6],[7,8,9]]
  • begin: [0,1]
  • end: [2,2]

The slice API extracts the elements whose indexes are in the range of [0, 2) along the 0th dimension and then extracts the elements whose indexes are in the range of [1, 2) along the 1st dimension. The result is as follows:

slice_tensor = [[2], [5]]

Prototype

slice(tensor, begin, end, stride=None)

Parameters

  • tensor: a tvm.tensor for the data to be sliced.

    The supported data types include float16, float32, int8, uint8, int16, uint16, int32, uint32, int64, uint64, and bool.

  • begin: a list for the dimension index, from which the slicing starts.

    The supported data types are int32 and int64.

  • end: a list for the dimension index, at which the slicing stops.

    The supported data types are int32 and int64.

  • stride: defaults to None. This reserved parameter is not supported in the current version.

Returns

A tvm.tensor for the result tensor after slicing.

Restrictions

  • The lengths of begin and end must be the same as the dimension count of the input tensor shape.
  • The data types of begin and end must be the same, for example, both int32 or int64.
  • The input shape, begin, and end must meet the following conditions:

    0 <= begin[0] < end[0] <= shape[0]

  • This API cannot be used in conjunction with other TBE DSL APIs.

Availability

Atlas Training Series Product

Example

  • tensor:
    tensor = [[1,2,3,4,5],
              [6,7,8,9,10],
              [11,12,13,14,15],
              [16,17,18,19,20]]

    begin:

    begin = [0,1]

    end:

    end = [3,5]
    Call the slice API to extract a slice based on begin and end. A code example is as follows:
    from tbe import tvm
    from tbe import dsl
    tensor = tvm.placeholder((4, 5), dtype=dtype, name="params")
    begin = [0, 1]
    end = [3, 5]
    slice_tensor = dsl.slice(tensor, begin, end)

    The preceding code is used to obtain data slices of a tensor. For the 0th dimension, slice whose indexes are 0, 1, and 2 is obtained. For the 1st dimension, slice whose indexes are 1, 2, 3, and 4 is obtained.

    The output is as follows:

    slice_tensor = [[2,3,4,5],
                     [7,8,9,10],
                     [12,13,14,15]]