Slice

Extracts elements from an input tensor into an output tensor.

Attributes

start The slicing start offset.

shape The output dimensions.

stride The slicing stride.

slice_mode The mode of handling out-of-bound coordinates:

\(\small\text{glossary: }s-\text{stride}, o-\text{offset}, d-\text{dimension size}, f-\text{constant value to fill}, x_i=b_i \cdot s_i + o_i\)

  • DEFAULT \(output[b_i]=input[x_i]\)
  • WRAP \(output[b_i]=input[mod(x_i, d_i)]\)
  • CLAMP \(output[b_i]=input[x_i] \text{ where } x_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[b_i]=\begin{cases}A[x_i], \text{if } 0\leq x_i < d_i \\ f, \text{otherwise}\end{cases}\)
  • REFLECT \(output[b_i]=input[z_i] \text{ where } z_i=\begin{cases}2 \cdot d_i-2-c_i, \text{if } c_i>d_i \\ c_i, \text{otherwise } \end{cases}, c_i=mod(|x_i|, 2 \cdot d_i -2)\)

Inputs

input: tensor of type T.

Outputs

output: tensor of type T.

Data Types

T: bool, int8, int32, float16, float32

Shape Information

input is a tensor with a shape of \([a_0,...,a_n]\).

output is a tensor with a shape of \([s_0,...,s_n]\) that is defined by the shape attribute.

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.SliceMode.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.