Slice

Extracts elements from an input tensor into an output tensor.

Attributes

start First coordinate to use.

size Output dimensions.

stride Stride between coordinates.

slice_mode Describes how out-of-bounds coordinates are handled.

axes Describes which axes start, size, and stride apply to.

Let \(y\) denote the components of an output coordinate and \(d\) denote dimensions of the input tensor. Define \(x_i=y_i \cdot \text{stride}_i + \text{start}_i\).

  • STRICT_BOUNDS \(output[y_i]=input[x_i]\) if \(0 \leq x_i < d_i\). Otherwise issue error.
  • WRAP \(output[y_i]=input[mod(x_i, d_i)]\)
  • CLAMP \(output[y_i]=input[z_i] \text{ where } z_i=\begin{cases}0, \text{if } x_i<0 \\ d_i-1, \text{if } x_i \geq d_i \\ x_i, \text{otherwise}\end{cases}\)
  • FILL \(output[y_i]=\begin{cases}input[x_i], \text{if } 0\leq x_i < d_i \\ \text{fill value}, \text{otherwise}\end{cases}\)
  • REFLECT \(output[y_i]=input[z_i] \text{ where } z_i=\begin{cases}2 \cdot d_i-2-c_i, \text{if } c_i \geq d_i \\ c_i, \text{otherwise } \end{cases} \text{ where } c_i=mod(|x_i|, 2 \cdot d_i -2)\)

Alternatively, the start, size, and stride can be specified by inputs to the slice operation.

Inputs

input0: tensor of type T.

input1: optional tensor of type Int32 or Int64` with ``start.

input2: optional tensor of type Int32 or Int64` with ``size.

input3: optional tensor of type Int32 or Int64` with ``stride.

input4: optional tensor of type T (or type immplicitly convertible to T) containing fill value for FILL mode.

axes: optional tensor of type Int32 or Int64` with ``axes.

Outputs

output: tensor of type T.

Data Types

T: bool, int4, int8, int32, int64, float8, float16, float32, bfloat16

  • Note: CLAMP, FILL and REFLECT modes do not support int64 or bfloat16.

Shape Information

input0 has shape \([d_0,...,d_{n-1}]\).

input1 has shape \([n]\).

input2 has shape \([n]\).

input3 has shape \([n]\).

input4 has shape \([]\).

input5 has shape \([m]\).

output is a tensor with shape \([\text{size}_0,...,\text{size}_{n-1}]\).

DLA Support

DLA FP16 is supported.

Examples

Slice
in1 = network.add_input("input1", dtype=trt.float32, shape=(3, 3))
layer = network.add_slice(in1, start=(0, 0), shape=(2, 2), stride=(1, 1))
network.mark_output(layer.get_output(0))

inputs[in1.name] = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])

outputs[layer.get_output(0).name] = layer.get_output(0).shape
expected[layer.get_output(0).name] = np.array([[0, 1], [3, 4]])

Slice Fill
in1 = network.add_input("input1", dtype=trt.float32, shape=(2, 2))
layer = network.add_slice(in1, start=(0, 0), shape=(3, 3), stride=(1, 1))
fill_constant = network.add_input("fill_constant", dtype=trt.float32, shape=())
layer.mode = trt.SampleMode.FILL
layer.set_input(4, fill_constant)
network.mark_output(layer.get_output(0))

inputs[in1.name] = np.zeros(shape=(2, 2))
inputs[fill_constant.name] = np.array([1.0])

outputs[layer.get_output(0).name] = layer.get_output(0).shape
expected[layer.get_output(0).name] = np.array([[0, 0, 1], [0, 0, 1], [1, 1, 1]])

C++ API

For more information about the C++ ISliceLayer operator, refer to the C++ ISliceLayer documentation.

Python API

For more information about the Python ISliceLayer operator, refer to the Python ISliceLayer documentation.