GatherND#
Domain:
ai.onnxSince version: 29
Given data tensor of rank r >= 1, indices tensor of rank q >= 1, and batch_dims integer b, this operator gathers
slices of data into an output tensor of rank q + r - indices_shape[-1] - 1 - b.
indices is an q-dimensional integer tensor, best thought of as a (q-1)-dimensional tensor of index-tuples into data,
where each element defines a slice of data
batch_dims (denoted as b) is an integer indicating the number of batch dimensions, i.e the leading b number of dimensions of
data tensor and indices are representing the batches, and the gather starts from the b+1 dimension.
Some salient points about the inputs’ rank and shape:
r >= 1 and q >= 1 are to be honored. There is no dependency condition to be met between ranks
randqThe first
bdimensions of the shape ofindicestensor anddatatensor must be equal.b r-b` => error condition
If
indices_shape[-1] == r-b, since the rank ofindicesisq,indicescan be thought of asN(q-b-1)-dimensional tensors containing 1-D tensors of dimensionr-b, whereNis an integer equals to the product of 1 and all the elements in the batch dimensions of the indices_shape. Let us think of each suchr-branked tensor asindices_slice. Each *scalar value* corresponding todata[0:b-1,indices_slice]is filled into the corresponding location of the(q-b-1)-dimensional tensor to form theoutputtensor (Example 1 below)If
indices_shape[-1] < r-b, since the rank ofindicesisq,indicescan be thought of asN(q-b-1)-dimensional tensor containing 1-D tensors of dimension< r-b. Let us think of each such tensors asindices_slice. Each *tensor slice* corresponding todata[0:b-1, indices_slice , :]is filled into the corresponding location of the(q-b-1)-dimensional tensor to form theoutputtensor (Examples 2, 3, 4 and 5 below)
This operator is the inverse of ScatterND.
Example 1
batch_dims = 0
data = [[0,1],[2,3]] # data_shape = [2, 2]
indices = [[0,0],[1,1]] # indices_shape = [2, 2]
output = [0,3] # output_shape = [2]
Example 2
batch_dims = 0
data = [[0,1],[2,3]] # data_shape = [2, 2]
indices = [[1],[0]] # indices_shape = [2, 1]
output = [[2,3],[0,1]] # output_shape = [2, 2]
Example 3
batch_dims = 0
data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]
indices = [[0,1],[1,0]] # indices_shape = [2, 2]
output = [[2,3],[4,5]] # output_shape = [2, 2]
Example 4
batch_dims = 0
data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]
indices = [[[0,1]],[[1,0]]] # indices_shape = [2, 1, 2]
output = [[[2,3]],[[4,5]]] # output_shape = [2, 1, 2]
Example 5
batch_dims = 1
data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]
indices = [[1],[0]] # indices_shape = [2, 1]
output = [[2,3],[4,5]] # output_shape = [2, 2]
Opset 29 adds support for int32 indices; int64 indices remain supported.
Inputs
data (T): Tensor of rank r >= 1.
indices (Tind): Tensor of rank q >= 1. Each index must be in [-s, s-1], where s is the size of the corresponding data axis after the batch dimensions. An out-of-bounds index is an error.
Outputs
output (T): Tensor of rank q + r - indices_shape[-1] - 1 - batch_dims.
Attributes
batch_dims (int): The number of leading batch dimensions shared by data and indices. Index tuples address data axes starting at axis batch_dims.
Type Constraints
T: Constrain input and output types to any tensor type. Allowed types: 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).
Tind: Constrain indices to int32 or int64 tensors. Allowed types: tensor(int32), tensor(int64).
Examples#
test_cc_gathernd_selected_kv_int32_indices
Node:
GatherND(data, indices) -> (output)
Attributes:
batch_dims = 1
Inputs:
data: shape=(2, 4, 3), dtype=float32
[[[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.],
[ 9., 10., 11.]],
[[12., 13., 14.],
[15., 16., 17.],
[18., 19., 20.],
[21., 22., 23.]]]
indices: shape=(2, 1, 2, 1), dtype=int32
[[[[ 0],
[ 2]]],
[[[ 1],
[-1]]]]
Outputs:
output: shape=(2, 1, 2, 3), dtype=float32
[[[[ 0., 1., 2.],
[ 6., 7., 8.]]],
[[[15., 16., 17.],
[21., 22., 23.]]]]
Differences with previous version (13)#
SchemaDiff: GatherND (domain 'ai.onnx')
old version: 13
new version: 29
breaking: yes
Breaking reasons:
input ‘indices’ (changed): type_str changed ‘tensor(int64)’ -> ‘Tind’
Inputs:
[BREAKING] changed ‘indices’: type_str changed ‘tensor(int64)’ -> ‘Tind’
Type constraints:
added ‘Tind’: added types: [‘tensor(int32)’, ‘tensor(int64)’]
Documentation:
line similarity: 0.99 (+2/-0 lines)
--- GatherND v13
+++ GatherND v29
@@ -83,3 +83,5 @@
indices = [[1],[0]] # indices_shape = [2, 1]
output = [[2,3],[4,5]] # output_shape = [2, 2]
```
+
+Opset 29 adds support for int32 indices; int64 indices remain supported.