Slice#

Slice - 13#

Version

  • name: Slice (GitHub)

  • domain: main

  • since_version: 13

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 13.

Summary

Produces a slice of the input tensor along multiple axes. Similar to numpy: https://numpy.org/doc/stable/user/basics.indexing.html?highlight=slice#slicing-and-striding

Slice uses the starts, ends, axes and steps inputs to select a sub-tensor of its input data tensor.

An effective start[i], end[i], and step[i] must be computed for each i in [0, … r-1] where r = rank(input) as follows:

If axes are omitted, they are set to [0, …, r-1]. If steps are omitted, they are set to [1, …, 1] of length len(starts)

The effective values are initialized as start[i] = 0, end[i] = dims[i] where dims are the dimensions of input and `step[i] = `1.

All negative elements of axes are made non-negatve by adding r to them, where r =rank(input).

All negative values in starts[i] and ends[i] have dims[axes[i]] added to them, where dims are the dimensions of input. Then start[axes[i]] is the adjusted starts[i] is clamped into the range [0, dims[axes[i]]] for positive stepping and [0, dims[axes[i]]-1] for negative stepping.

The clamping for the adjusted ends[i] depends on the sign of steps[i] and must accommodate copying 0 through dims[axes[i]] elements, so for positive stepping end[axes[i]] is clamped to [0, dims[axes[i]]], while for negative stepping it is clamped to [-1, dims[axes[i]]-1].

Finally, step[axes[i]] = steps[i].

For slicing to the end of a dimension with unknown size, it is recommended to pass in INT_MAX when slicing forward and ‘INT_MIN’ when slicing backward.

Example 1:
data = [

[1, 2, 3, 4], [5, 6, 7, 8],

] axes = [0, 1] starts = [1, 0] ends = [2, 3] steps = [1, 2] result = [

[5, 7],

]

Example 2:
data = [

[1, 2, 3, 4], [5, 6, 7, 8],

] starts = [0, 1] ends = [-1, 1000] result = [

[2, 3, 4],

]

Inputs

Between 3 and 5 inputs.

  • data (heterogeneous) - T: Tensor of data to extract slices from.

  • starts (heterogeneous) - Tind: 1-D tensor of starting indices of corresponding axis in axes

  • ends (heterogeneous) - Tind: 1-D tensor of ending indices (exclusive) of corresponding axis in axes

  • axes (optional, heterogeneous) - Tind: 1-D tensor of axes that starts and ends apply to. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data). Behavior is undefined if an axis is repeated.

  • steps (optional, heterogeneous) - Tind: 1-D tensor of slice step of corresponding axis in axes. Negative value means slicing backward. ‘steps’ cannot be 0. Defaults to 1s.

Outputs

  • output (heterogeneous) - T: Sliced data tensor.

Type Constraints

  • T in ( tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output types to all tensor types.

  • Tind in ( tensor(int32), tensor(int64) ): Constrain indices to integer types

Examples

_slice

node = onnx.helper.make_node(
    "Slice",
    inputs=["x", "starts", "ends", "axes", "steps"],
    outputs=["y"],
)

x = np.random.randn(20, 10, 5).astype(np.float32)
y = x[0:3, 0:10]
starts = np.array([0, 0], dtype=np.int64)
ends = np.array([3, 10], dtype=np.int64)
axes = np.array([0, 1], dtype=np.int64)
steps = np.array([1, 1], dtype=np.int64)

expect(
    node, inputs=[x, starts, ends, axes, steps], outputs=[y], name="test_slice"
)

_slice_neg

node = onnx.helper.make_node(
    "Slice",
    inputs=["x", "starts", "ends", "axes", "steps"],
    outputs=["y"],
)

x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([0], dtype=np.int64)
ends = np.array([-1], dtype=np.int64)
axes = np.array([1], dtype=np.int64)
steps = np.array([1], dtype=np.int64)
y = x[:, 0:-1]

expect(
    node,
    inputs=[x, starts, ends, axes, steps],
    outputs=[y],
    name="test_slice_neg",
)

_slice_start_out_of_bounds

node = onnx.helper.make_node(
    "Slice",
    inputs=["x", "starts", "ends", "axes", "steps"],
    outputs=["y"],
)

x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([1000], dtype=np.int64)
ends = np.array([1000], dtype=np.int64)
axes = np.array([1], dtype=np.int64)
steps = np.array([1], dtype=np.int64)
y = x[:, 1000:1000]

expect(
    node,
    inputs=[x, starts, ends, axes, steps],
    outputs=[y],
    name="test_slice_start_out_of_bounds",
)

_slice_end_out_of_bounds

node = onnx.helper.make_node(
    "Slice",
    inputs=["x", "starts", "ends", "axes", "steps"],
    outputs=["y"],
)

x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([1], dtype=np.int64)
ends = np.array([1000], dtype=np.int64)
axes = np.array([1], dtype=np.int64)
steps = np.array([1], dtype=np.int64)
y = x[:, 1:1000]

expect(
    node,
    inputs=[x, starts, ends, axes, steps],
    outputs=[y],
    name="test_slice_end_out_of_bounds",
)

_slice_default_axes

node = onnx.helper.make_node(
    "Slice",
    inputs=["x", "starts", "ends"],
    outputs=["y"],
)

x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([0, 0, 3], dtype=np.int64)
ends = np.array([20, 10, 4], dtype=np.int64)
y = x[:, :, 3:4]

expect(
    node, inputs=[x, starts, ends], outputs=[y], name="test_slice_default_axes"
)

_slice_default_steps

node = onnx.helper.make_node(
    "Slice",
    inputs=["x", "starts", "ends", "axes"],
    outputs=["y"],
)

x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([0, 0, 3], dtype=np.int64)
ends = np.array([20, 10, 4], dtype=np.int64)
axes = np.array([0, 1, 2], dtype=np.int64)
y = x[:, :, 3:4]

expect(
    node,
    inputs=[x, starts, ends, axes],
    outputs=[y],
    name="test_slice_default_steps",
)

_slice_neg_steps

node = onnx.helper.make_node(
    "Slice",
    inputs=["x", "starts", "ends", "axes", "steps"],
    outputs=["y"],
)

x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([20, 10, 4], dtype=np.int64)
ends = np.array([0, 0, 1], dtype=np.int64)
axes = np.array([0, 1, 2], dtype=np.int64)
steps = np.array([-1, -3, -2]).astype(np.int64)
y = x[20:0:-1, 10:0:-3, 4:1:-2]

expect(
    node,
    inputs=[x, starts, ends, axes, steps],
    outputs=[y],
    name="test_slice_neg_steps",
)

_slice_negative_axes

node = onnx.helper.make_node(
    "Slice",
    inputs=["x", "starts", "ends", "axes"],
    outputs=["y"],
)

x = np.random.randn(20, 10, 5).astype(np.float32)
starts = np.array([0, 0, 3], dtype=np.int64)
ends = np.array([20, 10, 4], dtype=np.int64)
axes = np.array([0, -2, -1], dtype=np.int64)
y = x[:, :, 3:4]

expect(
    node,
    inputs=[x, starts, ends, axes],
    outputs=[y],
    name="test_slice_negative_axes",
)

Differences

00Produces a slice of the input tensor along multiple axes. Similar to numpy:Produces a slice of the input tensor along multiple axes. Similar to numpy:
11https://docs.scipy.org/doc/numpy/reference/arrays.indexing.htmlhttps://numpy.org/doc/stable/user/basics.indexing.html?highlight=slice#slicing-and-striding
2
23Slices uses starts, ends, axes and steps inputs to specify the start and endSlice uses the starts, ends, axes and steps inputs to select a sub-tensor
34dimension and step for each axis in the list of axes, it uses this information toof its input data tensor.
5
6An effective start[i], end[i], and step[i] must be computed for each i
7in [0, ... r-1] where r = rank(input) as follows:
8
9If axes are omitted, they are set to [0, ..., r-1].
10If steps are omitted, they are set to [1, ..., 1] of length len(starts)
11
12The effective values are initialized as start[i] = 0, end[i] = dims[i] where
13dims are the dimensions of input and step[i] = 1.
14
15All negative elements of axes are made non-negatve by adding r to them, where
416slice the input data tensor. If a negative value is passed for any of ther =rank(input).
17
18All negative values in starts[i] and ends[i] have dims[axes[i]] added to them,
19where dims are the dimensions of input. Then start[axes[i]] is the adjusted
20starts[i] is clamped into the range [0, dims[axes[i]]] for positive stepping
21and [0, dims[axes[i]]-1] for negative stepping.
22
23The clamping for the adjusted ends[i] depends on the sign of steps[i] and must
24accommodate copying 0 through dims[axes[i]] elements, so for positive stepping
25end[axes[i]] is clamped to [0, dims[axes[i]]], while for negative stepping it
526start or end indices, it represents number of elements before the end of thatis clamped to [-1, dims[axes[i]]-1].
27
628dimension. If the value passed to start or end is larger than the n (theFinally, step[axes[i]] = steps[i].
7number of elements in this dimension), it represents n. For slicing to the
29
830end of a dimension with unknown size, it is recommended to pass in INT_MAXFor slicing to the end of a dimension with unknown size, it is recommended to pass
931when slicing forward and 'INT_MIN' when slicing backward.in INT_MAX when slicing forward and 'INT_MIN' when slicing backward.
10If a negative value is passed for step, it represents slicing backward.
11However step value cannot be 0.
12If axes are omitted, they are set to [0, ..., ndim-1].
13If steps are omitted, they are set to [1, ..., 1] of length len(starts)
32
1433Example 1:Example 1:
1534 data = [ data = [
1635 [1, 2, 3, 4], [1, 2, 3, 4],
1736 [5, 6, 7, 8], [5, 6, 7, 8],
1837 ] ]
1938 axes = [0, 1] axes = [0, 1]
2039 starts = [1, 0] starts = [1, 0]
2140 ends = [2, 3] ends = [2, 3]
2241 steps = [1, 2] steps = [1, 2]
2342 result = [ result = [
2443 [5, 7], [5, 7],
2544 ] ]
2645Example 2:Example 2:
2746 data = [ data = [
2847 [1, 2, 3, 4], [1, 2, 3, 4],
2948 [5, 6, 7, 8], [5, 6, 7, 8],
3049 ] ]
3150 starts = [0, 1] starts = [0, 1]
3251 ends = [-1, 1000] ends = [-1, 1000]
3352 result = [ result = [
3453 [2, 3, 4], [2, 3, 4],
3554 ] ]
3655
3756**Inputs****Inputs**
3857
3958Between 3 and 5 inputs.Between 3 and 5 inputs.
4059
4160* **data** (heterogeneous) - **T**:* **data** (heterogeneous) - **T**:
4261 Tensor of data to extract slices from. Tensor of data to extract slices from.
4362* **starts** (heterogeneous) - **Tind**:* **starts** (heterogeneous) - **Tind**:
4463 1-D tensor of starting indices of corresponding axis in axes 1-D tensor of starting indices of corresponding axis in axes
4564* **ends** (heterogeneous) - **Tind**:* **ends** (heterogeneous) - **Tind**:
4665 1-D tensor of ending indices (exclusive) of corresponding axis in 1-D tensor of ending indices (exclusive) of corresponding axis in
4766 axes axes
4867* **axes** (optional, heterogeneous) - **Tind**:* **axes** (optional, heterogeneous) - **Tind**:
4968 1-D tensor of axes that starts and ends apply to. Negative value 1-D tensor of axes that starts and ends apply to. Negative value
5069 means counting dimensions from the back. Accepted range is [-r, r-1] means counting dimensions from the back. Accepted range is [-r, r-1]
5170 where r = rank(data). where r = rank(data). Behavior is undefined if an axis is repeated.
5271* **steps** (optional, heterogeneous) - **Tind**:* **steps** (optional, heterogeneous) - **Tind**:
5372 1-D tensor of slice step of corresponding axis in axes. Negative 1-D tensor of slice step of corresponding axis in axes. Negative
5473 value means slicing backward. 'steps' cannot be 0. Defaults to 1. value means slicing backward. 'steps' cannot be 0. Defaults to 1s.
5574
5675**Outputs****Outputs**
5776
5877* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
5978 Sliced data tensor. Sliced data tensor.
6079
6180**Type Constraints****Type Constraints**
6281
6382* **T** in (* **T** in (
83 tensor(bfloat16),
6484 tensor(bool), tensor(bool),
6585 tensor(complex128), tensor(complex128),
6686 tensor(complex64), tensor(complex64),
6787 tensor(double), tensor(double),
6888 tensor(float), tensor(float),
6989 tensor(float16), tensor(float16),
7090 tensor(int16), tensor(int16),
7191 tensor(int32), tensor(int32),
7292 tensor(int64), tensor(int64),
7393 tensor(int8), tensor(int8),
7494 tensor(string), tensor(string),
7595 tensor(uint16), tensor(uint16),
7696 tensor(uint32), tensor(uint32),
7797 tensor(uint64), tensor(uint64),
7898 tensor(uint8) tensor(uint8)
7999 ): ):
80100 Constrain input and output types to all tensor types. Constrain input and output types to all tensor types.
81101* **Tind** in (* **Tind** in (
82102 tensor(int32), tensor(int32),
83103 tensor(int64) tensor(int64)
84104 ): ):
85105 Constrain indices to integer types Constrain indices to integer types

Slice - 11#

Version

  • name: Slice (GitHub)

  • domain: main

  • since_version: 11

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 11.

Summary

Produces a slice of the input tensor along multiple axes. Similar to numpy: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html Slices uses starts, ends, axes and steps inputs to specify the start and end dimension and step for each axis in the list of axes, it uses this information to slice the input data tensor. If a negative value is passed for any of the start or end indices, it represents number of elements before the end of that dimension. If the value passed to start or end is larger than the 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 when slicing forward and ‘INT_MIN’ when slicing backward. If a negative value is passed for step, it represents slicing backward. However step value cannot be 0. If axes are omitted, they are set to [0, …, ndim-1]. If steps are omitted, they are set to [1, …, 1] of length len(starts) Example 1:

data = [

[1, 2, 3, 4], [5, 6, 7, 8],

] axes = [0, 1] starts = [1, 0] ends = [2, 3] steps = [1, 2] result = [

[5, 7],

]

Example 2:
data = [

[1, 2, 3, 4], [5, 6, 7, 8],

] starts = [0, 1] ends = [-1, 1000] result = [

[2, 3, 4],

]

Inputs

Between 3 and 5 inputs.

  • data (heterogeneous) - T: Tensor of data to extract slices from.

  • starts (heterogeneous) - Tind: 1-D tensor of starting indices of corresponding axis in axes

  • ends (heterogeneous) - Tind: 1-D tensor of ending indices (exclusive) of corresponding axis in axes

  • axes (optional, heterogeneous) - Tind: 1-D tensor of axes that starts and ends apply to. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data).

  • steps (optional, heterogeneous) - Tind: 1-D tensor of slice step of corresponding axis in axes. Negative value means slicing backward. ‘steps’ cannot be 0. Defaults to 1.

Outputs

  • output (heterogeneous) - T: Sliced data tensor.

Type Constraints

  • T in ( tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output types to all tensor types.

  • Tind in ( tensor(int32), tensor(int64) ): Constrain indices to integer types

Differences

00Produces a slice of the input tensor along multiple axes. Similar to numpy:Produces a slice of the input tensor along multiple axes. Similar to numpy:
11https://docs.scipy.org/doc/numpy/reference/arrays.indexing.htmlhttps://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
22Slices uses starts, ends, axes and steps inputs to specify the start and endSlices uses starts, ends, axes and steps inputs to specify the start and end
33dimension and step for each axis in the list of axes, it uses this information todimension and step for each axis in the list of axes, it uses this information to
44slice the input data tensor. If a negative value is passed for any of theslice the input data tensor. If a negative value is passed for any of the
55start or end indices, it represent number of elements before the end of thatstart or end indices, it represents number of elements before the end of that
66dimension. If the value passed to start or end is larger than the n (thedimension. If the value passed to start or end is larger than the n (the
77number of elements in this dimension), it represents n. For slicing to thenumber of elements in this dimension), it represents n. For slicing to the
88end of a dimension with unknown size, it is recommended to pass in INT_MAX.end of a dimension with unknown size, it is recommended to pass in INT_MAX
9when slicing forward and 'INT_MIN' when slicing backward.
910If a negative value is passed for step, it represents slicing backward.If a negative value is passed for step, it represents slicing backward.
11However step value cannot be 0.
1012If axes are omitted, they are set to [0, ..., ndim-1].If axes are omitted, they are set to [0, ..., ndim-1].
1113If steps are omitted, they are set to [1, ..., 1] of length len(starts)If steps are omitted, they are set to [1, ..., 1] of length len(starts)
1214Example 1:Example 1:
1315 data = [ data = [
1416 [1, 2, 3, 4], [1, 2, 3, 4],
1517 [5, 6, 7, 8], [5, 6, 7, 8],
1618 ] ]
1719 axes = [0, 1] axes = [0, 1]
1820 starts = [1, 0] starts = [1, 0]
1921 ends = [2, 3] ends = [2, 3]
2022 steps = [1, 2] steps = [1, 2]
2123 result = [ result = [
2224 [5, 7], [5, 7],
2325 ] ]
2426Example 2:Example 2:
2527 data = [ data = [
2628 [1, 2, 3, 4], [1, 2, 3, 4],
2729 [5, 6, 7, 8], [5, 6, 7, 8],
2830 ] ]
2931 starts = [0, 1] starts = [0, 1]
3032 ends = [-1, 1000] ends = [-1, 1000]
3133 result = [ result = [
3234 [2, 3, 4], [2, 3, 4],
3335 ] ]
3436
3537**Inputs****Inputs**
3638
3739Between 3 and 5 inputs.Between 3 and 5 inputs.
3840
3941* **data** (heterogeneous) - **T**:* **data** (heterogeneous) - **T**:
4042 Tensor of data to extract slices from. Tensor of data to extract slices from.
4143* **starts** (heterogeneous) - **Tind**:* **starts** (heterogeneous) - **Tind**:
4244 1-D tensor of starting indices of corresponding axis in axes 1-D tensor of starting indices of corresponding axis in axes
4345* **ends** (heterogeneous) - **Tind**:* **ends** (heterogeneous) - **Tind**:
4446 1-D tensor of ending indices (exclusive) of corresponding axis in 1-D tensor of ending indices (exclusive) of corresponding axis in
4547 axes axes
4648* **axes** (optional, heterogeneous) - **Tind**:* **axes** (optional, heterogeneous) - **Tind**:
4749 1-D tensor of axes that starts and ends apply to. 1-D tensor of axes that starts and ends apply to. Negative value
50 means counting dimensions from the back. Accepted range is [-r, r-1]
51 where r = rank(data).
4852* **steps** (optional, heterogeneous) - **Tind**:* **steps** (optional, heterogeneous) - **Tind**:
4953 1-D tensor of slice step of corresponding axis in axes. Default to 1-D tensor of slice step of corresponding axis in axes. Negative
50 1.
54 value means slicing backward. 'steps' cannot be 0. Defaults to 1.
5155
5256**Outputs****Outputs**
5357
5458* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
5559 Sliced data tensor. Sliced data tensor.
5660
5761**Type Constraints****Type Constraints**
5862
5963* **T** in (* **T** in (
6064 tensor(bool), tensor(bool),
6165 tensor(complex128), tensor(complex128),
6266 tensor(complex64), tensor(complex64),
6367 tensor(double), tensor(double),
6468 tensor(float), tensor(float),
6569 tensor(float16), tensor(float16),
6670 tensor(int16), tensor(int16),
6771 tensor(int32), tensor(int32),
6872 tensor(int64), tensor(int64),
6973 tensor(int8), tensor(int8),
7074 tensor(string), tensor(string),
7175 tensor(uint16), tensor(uint16),
7276 tensor(uint32), tensor(uint32),
7377 tensor(uint64), tensor(uint64),
7478 tensor(uint8) tensor(uint8)
7579 ): ):
7680 Constrain input and output types to all tensor types. Constrain input and output types to all tensor types.
7781* **Tind** in (* **Tind** in (
7882 tensor(int32), tensor(int32),
7983 tensor(int64) tensor(int64)
8084 ): ):
8185 Constrain indices to integer types Constrain indices to integer types

Slice - 10#

Version

  • name: Slice (GitHub)

  • domain: main

  • since_version: 10

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 10.

Summary

Produces a slice of the input tensor along multiple axes. Similar to numpy: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html Slices uses starts, ends, axes and steps inputs to specify the start and end dimension and step for each axis in the list of axes, it uses this information to slice the input data tensor. If a negative value is passed for any of the start or end indices, it represent number of elements before the end of that dimension. If the value passed to start or end is larger than the 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. If a negative value is passed for step, it represents slicing backward. If axes are omitted, they are set to [0, …, ndim-1]. If steps are omitted, they are set to [1, …, 1] of length len(starts) Example 1:

data = [

[1, 2, 3, 4], [5, 6, 7, 8],

] axes = [0, 1] starts = [1, 0] ends = [2, 3] steps = [1, 2] result = [

[5, 7],

]

Example 2:
data = [

[1, 2, 3, 4], [5, 6, 7, 8],

] starts = [0, 1] ends = [-1, 1000] result = [

[2, 3, 4],

]

Inputs

Between 3 and 5 inputs.

  • data (heterogeneous) - T: Tensor of data to extract slices from.

  • starts (heterogeneous) - Tind: 1-D tensor of starting indices of corresponding axis in axes

  • ends (heterogeneous) - Tind: 1-D tensor of ending indices (exclusive) of corresponding axis in axes

  • axes (optional, heterogeneous) - Tind: 1-D tensor of axes that starts and ends apply to.

  • steps (optional, heterogeneous) - Tind: 1-D tensor of slice step of corresponding axis in axes. Default to 1.

Outputs

  • output (heterogeneous) - T: Sliced data tensor.

Type Constraints

  • T in ( tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output types to all tensor types.

  • Tind in ( tensor(int32), tensor(int64) ): Constrain indices to integer types

Differences

00Produces a slice of the input tensor along multiple axes. Similar to numpy:Produces a slice of the input tensor along multiple axes. Similar to numpy:
11https://docs.scipy.org/doc/numpy/reference/arrays.indexing.htmlhttps://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
22Slices uses axes, starts and ends attributes to specify the start and endSlices uses starts, ends, axes and steps inputs to specify the start and end
33dimension for each axis in the list of axes, it uses this information todimension and step for each axis in the list of axes, it uses this information to
44slice the input data tensor. If a negative value is passed for any of theslice the input data tensor. If a negative value is passed for any of the
55start or end indices, it represent number of elements before the end of thatstart or end indices, it represent number of elements before the end of that
66dimension. If the value passed to start or end is larger than the n (thedimension. If the value passed to start or end is larger than the n (the
77number of elements in this dimension), it represents n. For slicing to thenumber of elements in this dimension), it represents n. For slicing to the
88end of a dimension with unknown size, it is recommended to pass in INT_MAX.end of a dimension with unknown size, it is recommended to pass in INT_MAX.
9If a negative value is passed for step, it represents slicing backward.
910If axes are omitted, they are set to [0, ..., ndim-1].If axes are omitted, they are set to [0, ..., ndim-1].
11If steps are omitted, they are set to [1, ..., 1] of length len(starts)
1012Example 1:Example 1:
1113 data = [ data = [
1214 [1, 2, 3, 4], [1, 2, 3, 4],
1315 [5, 6, 7, 8], [5, 6, 7, 8],
1416 ] ]
1517 axes = [0, 1] axes = [0, 1]
1618 starts = [1, 0] starts = [1, 0]
1719 ends = [2, 3] ends = [2, 3]
20 steps = [1, 2]
1821 result = [ result = [
1922 [5, 6, 7], [5, 7],
2023 ] ]
2124Example 2:Example 2:
2225 data = [ data = [
2326 [1, 2, 3, 4], [1, 2, 3, 4],
2427 [5, 6, 7, 8], [5, 6, 7, 8],
2528 ] ]
2629 starts = [0, 1] starts = [0, 1]
2730 ends = [-1, 1000] ends = [-1, 1000]
2831 result = [ result = [
2932 [2, 3, 4], [2, 3, 4],
3033 ] ]
3134
32**Attributes**
33
34* **axes**:
3535 Axes that starts and ends apply to. It's optional. If not**Inputs**
36
3637 present, will be treated as [0, 1, ..., len(starts) - 1].Between 3 and 5 inputs.
38
39* **data** (heterogeneous) - **T**:
40 Tensor of data to extract slices from.
41* **starts** (heterogeneous) - **Tind**:
3742* **ends** (required): 1-D tensor of starting indices of corresponding axis in axes
43* **ends** (heterogeneous) - **Tind**:
3844 Ending indices (exclusive) of corresponding axis in axes 1-D tensor of ending indices (exclusive) of corresponding axis in
39* **starts** (required):
40 Starting indices of corresponding axis in axes
41
45 axes
4246**Inputs*** **axes** (optional, heterogeneous) - **Tind**:
43
47 1-D tensor of axes that starts and ends apply to.
4448* **data** (heterogeneous) - **T**:* **steps** (optional, heterogeneous) - **Tind**:
45 Tensor of data to extract slices from.
49 1-D tensor of slice step of corresponding axis in axes. Default to
50 1.
4651
4752**Outputs****Outputs**
4853
4954* **output** (heterogeneous) - **T**:* **output** (heterogeneous) - **T**:
5055 Sliced data tensor. Sliced data tensor.
5156
5257**Type Constraints****Type Constraints**
5358
5459* **T** in (* **T** in (
5560 tensor(bool), tensor(bool),
5661 tensor(complex128), tensor(complex128),
5762 tensor(complex64), tensor(complex64),
5863 tensor(double), tensor(double),
5964 tensor(float), tensor(float),
6065 tensor(float16), tensor(float16),
6166 tensor(int16), tensor(int16),
6267 tensor(int32), tensor(int32),
6368 tensor(int64), tensor(int64),
6469 tensor(int8), tensor(int8),
6570 tensor(string), tensor(string),
6671 tensor(uint16), tensor(uint16),
6772 tensor(uint32), tensor(uint32),
6873 tensor(uint64), tensor(uint64),
6974 tensor(uint8) tensor(uint8)
7075 ): ):
7176 Constrain input and output types to all tensor types. Constrain input and output types to all tensor types.
77* **Tind** in (
78 tensor(int32),
79 tensor(int64)
80 ):
81 Constrain indices to integer types

Slice - 1#

Version

  • name: Slice (GitHub)

  • domain: main

  • since_version: 1

  • function: False

  • support_level: SupportType.COMMON

  • shape inference: True

This version of the operator has been available since version 1.

Summary

Produces a slice of the input tensor along multiple axes. Similar to numpy: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html Slices uses axes, starts and ends attributes to specify the start and end dimension for each axis in the list of axes, it uses this information to slice the input data tensor. If a negative value is passed for any of the start or end indices, it represent number of elements before the end of that dimension. If the value passed to start or end is larger than the 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. If axes are omitted, they are set to [0, …, ndim-1]. Example 1:

data = [

[1, 2, 3, 4], [5, 6, 7, 8],

] axes = [0, 1] starts = [1, 0] ends = [2, 3] result = [

[5, 6, 7],

]

Example 2:
data = [

[1, 2, 3, 4], [5, 6, 7, 8],

] starts = [0, 1] ends = [-1, 1000] result = [

[2, 3, 4],

]

Attributes

  • axes: Axes that starts and ends apply to. It’s optional. If not present, will be treated as [0, 1, …, len(starts) - 1].

  • ends (required): Ending indices (exclusive) of corresponding axis in axes`

  • starts (required): Starting indices of corresponding axis in axes

Inputs

  • data (heterogeneous) - T: Tensor of data to extract slices from.

Outputs

  • output (heterogeneous) - T: Sliced data tensor.

Type Constraints

  • T in ( tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) ): Constrain input and output types to all tensor types.