Slice

输入张量中提取元素到输出张量中。

属性

start 要使用的第一个坐标。

size 输出维度。

stride 坐标之间的步幅。

slice_mode 描述如何处理越界坐标。

axes 描述 startsizestride 应用于哪些轴。

\(y\) 表示输出坐标的分量,\(d\) 表示输入张量的维度。定义 \(x_i=y_i \cdot \text{stride}_i + \text{start}_i\)

  • STRICT_BOUNDS 如果 \(0 \leq x_i < d_i\),则 \(output[y_i]=input[x_i]\)。否则发出错误。
  • WRAP \(output[y_i]=input[mod(x_i, d_i)]\)
  • CLAMP \(output[y_i]=input[z_i] \text{ 其中 } z_i=\begin{cases}0, \text{如果 } x_i<0 \\ d_i-1, \text{如果 } x_i \geq d_i \\ x_i, \text{否则}\end{cases}\)
  • FILL \(output[y_i]=\begin{cases}input[x_i], \text{如果 } 0\leq x_i < d_i \\ \text{填充值}, \text{否则}\end{cases}\)
  • REFLECT \(output[y_i]=input[z_i] \text{ 其中 } z_i=\begin{cases}2 \cdot d_i-2-c_i, \text{如果 } c_i \geq d_i \\ c_i, \text{否则 } \end{cases} \text{ 其中 } c_i=mod(|x_i|, 2 \cdot d_i -2)\)

或者,startsizestride 可以通过切片操作的输入来指定。

输入

input0T 类型的张量。

input1:可选的 Int32Int64` 类型张量,带有 ``start``

input2:可选的 Int32Int64` 类型张量,带有 ``size``

input3:可选的 Int32Int64` 类型张量,带有 ``stride``

input4:可选的 T 类型张量(或可隐式转换为 T 类型的张量),包含 FILL 模式的填充值。

axes:可选的 Int32Int64` 类型张量,带有 ``axes``

输出

outputT 类型的张量。

数据类型

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

  • 注意:CLAMPFILLREFLECT 模式不支持 int64bfloat16

形状信息

input0 的形状为 \([d_0,...,d_{n-1}]\)

input1 的形状为 \([n]\)

input2 的形状为 \([n]\)

input3 的形状为 \([n]\)

input4 的形状为 \([]\)

input5 的形状为 \([m]\)

output 是一个形状为 \([\text{size}_0,...,\text{size}_{n-1}]\) 的张量。

DLA 支持

支持 DLA FP16。

示例

切片 (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

有关 C++ ISliceLayer 算子的更多信息,请参阅 C++ ISliceLayer 文档

Python API

有关 Python ISliceLayer 算子的更多信息,请参阅 Python ISliceLayer 文档