# Gather#

## Gather - 13#

Version

• name: Gather (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

Given data tensor of rank r >= 1, and indices tensor of rank q, gather entries of the axis dimension of data (by default outer-most one as axis=0) indexed by indices, and concatenates them in an output tensor of rank q + (r - 1).

axis = 0 :

Let k = indices[i_{0}, …, i_{q-1}] Then output[i_{0}, …, i_{q-1}, j_{0}, …, j_{r-2}] = input[k , j_{0}, …, j_{r-2}]

```data = [
[1.0, 1.2],
[2.3, 3.4],
[4.5, 5.7],
]
indices = [
[0, 1],
[1, 2],
]
output = [
[
[1.0, 1.2],
[2.3, 3.4],
],
[
[2.3, 3.4],
[4.5, 5.7],
],
]
```

axis = 1 :

Let k = indices[i_{0}, …, i_{q-1}] Then output[j_{0}, i_{0}, …, i_{q-1}, j_{1}, …, j_{r-2}] = input[j_{0}, k, j_{1}, …, j_{r-2}]

```data = [
[1.0, 1.2, 1.9],
[2.3, 3.4, 3.9],
[4.5, 5.7, 5.9],
]
indices = [
[0, 2],
]
axis = 1,
output = [
[[1.0, 1.9]],
[[2.3, 3.9]],
[[4.5, 5.9]],
]
```

Attributes

• axis: Which axis to gather on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data). Default value is `0`.

Inputs

• data (heterogeneous) - T: Tensor of rank r >= 1.

• indices (heterogeneous) - Tind: Tensor of int32/int64 indices, of any rank q. All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds.

Outputs

• output (heterogeneous) - T: Tensor of rank q + (r - 1).

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 any tensor type.

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

Examples

_gather_0

```node = onnx.helper.make_node(
"Gather",
inputs=["data", "indices"],
outputs=["y"],
axis=0,
)
data = np.random.randn(5, 4, 3, 2).astype(np.float32)
indices = np.array([0, 1, 3])
y = np.take(data, indices, axis=0)

expect(
node,
inputs=[data, indices.astype(np.int64)],
outputs=[y],
name="test_gather_0",
)
```

_gather_1

```node = onnx.helper.make_node(
"Gather",
inputs=["data", "indices"],
outputs=["y"],
axis=1,
)
data = np.random.randn(5, 4, 3, 2).astype(np.float32)
indices = np.array([0, 1, 3])
y = np.take(data, indices, axis=1)

expect(
node,
inputs=[data, indices.astype(np.int64)],
outputs=[y],
name="test_gather_1",
)
```

_gather_2d_indices

```node = onnx.helper.make_node(
"Gather",
inputs=["data", "indices"],
outputs=["y"],
axis=1,
)
data = np.random.randn(3, 3).astype(np.float32)
indices = np.array([[0, 2]])
y = np.take(data, indices, axis=1)

expect(
node,
inputs=[data, indices.astype(np.int64)],
outputs=[y],
name="test_gather_2d_indices",
)
```

_gather_negative_indices

```node = onnx.helper.make_node(
"Gather",
inputs=["data", "indices"],
outputs=["y"],
axis=0,
)
data = np.arange(10).astype(np.float32)
indices = np.array([0, -9, -10])
y = np.take(data, indices, axis=0)

# print(y)
# [0. 1. 0.]

expect(
node,
inputs=[data, indices.astype(np.int64)],
outputs=[y],
name="test_gather_negative_indices",
)
```

Differences

 `0` `0` `Given data tensor of rank r >= 1, and indices tensor of rank q, gather` `Given data tensor of rank r >= 1, and indices tensor of rank q, gather` `1` `1` `entries of the axis dimension of data (by default outer-most one as axis=0) indexed by indices, and concatenates` `entries of the axis dimension of data (by default outer-most one as axis=0) indexed by indices, and concatenates` `2` `2` `them in an output tensor of rank q + (r - 1).` `them in an output tensor of rank q + (r - 1).` `3` `3` `4` `4` `axis = 0 :` `axis = 0 :` `5` `5` `6` `6` `Let` `Let` `7` `7` `k = indices[i_{0}, ..., i_{q-1}]` `k = indices[i_{0}, ..., i_{q-1}]` `8` `8` `Then` `Then` `9` `9` `output[i_{0}, ..., i_{q-1}, j_{0}, ..., j_{r-2}] = input[k , j_{0}, ..., j_{r-2}]` `output[i_{0}, ..., i_{q-1}, j_{0}, ..., j_{r-2}] = input[k , j_{0}, ..., j_{r-2}]` `10` `10` `11` `11` `::` `::` `12` `12` `13` `13` ` data = [` ` data = [` `14` `14` ` [1.0, 1.2],` ` [1.0, 1.2],` `15` `15` ` [2.3, 3.4],` ` [2.3, 3.4],` `16` `16` ` [4.5, 5.7],` ` [4.5, 5.7],` `17` `17` ` ]` ` ]` `18` `18` ` indices = [` ` indices = [` `19` `19` ` [0, 1],` ` [0, 1],` `20` `20` ` [1, 2],` ` [1, 2],` `21` `21` ` ]` ` ]` `22` `22` ` output = [` ` output = [` `23` `23` ` [` ` [` `24` `24` ` [1.0, 1.2],` ` [1.0, 1.2],` `25` `25` ` [2.3, 3.4],` ` [2.3, 3.4],` `26` `26` ` ],` ` ],` `27` `27` ` [` ` [` `28` `28` ` [2.3, 3.4],` ` [2.3, 3.4],` `29` `29` ` [4.5, 5.7],` ` [4.5, 5.7],` `30` `30` ` ],` ` ],` `31` `31` ` ]` ` ]` `32` `32` `33` `33` `axis = 1 :` `axis = 1 :` `34` `34` `35` `35` `Let` `Let` `36` `36` `k = indices[i_{0}, ..., i_{q-1}]` `k = indices[i_{0}, ..., i_{q-1}]` `37` `37` `Then` `Then` `38` `38` `output[i_{0}, ..., i_{q-1}, j_{0}, ..., j_{r-2}] = input[j_{0}, k, j_{1}, ..., j_{r-2}]` `output[j_{0}, i_{0}, ..., i_{q-1}, j_{1}, ..., j_{r-2}] = input[j_{0}, k, j_{1}, ..., j_{r-2}]` `39` `39` `40` `40` `::` `::` `41` `41` `42` `42` ` data = [` ` data = [` `43` `43` ` [1.0, 1.2, 1.9],` ` [1.0, 1.2, 1.9],` `44` `44` ` [2.3, 3.4, 3.9],` ` [2.3, 3.4, 3.9],` `45` `45` ` [4.5, 5.7, 5.9],` ` [4.5, 5.7, 5.9],` `46` `46` ` ]` ` ]` `47` `47` ` indices = [` ` indices = [` `48` `48` ` [0, 2],` ` [0, 2],` `49` `49` ` ]` ` ]` `50` `50` ` axis = 1,` ` axis = 1,` `51` `51` ` output = [` ` output = [` `52` ` [` `53` `52` ` [1.0, 1.9],` ` [[1.0, 1.9]],` `54` `53` ` [2.3, 3.9],` ` [[2.3, 3.9]],` `55` `54` ` [4.5, 5.9],` ` [[4.5, 5.9]],` `56` ` ],` `57` `55` ` ]` ` ]` `58` `56` `59` `57` `**Attributes**` `**Attributes**` `60` `58` `61` `59` `* **axis**:` `* **axis**:` `62` `60` ` Which axis to gather on. Negative value means counting dimensions` ` Which axis to gather on. Negative value means counting dimensions` `63` `61` ` from the back. Accepted range is [-r, r-1] where r = rank(data). Default value is 0.` ` from the back. Accepted range is [-r, r-1] where r = rank(data). Default value is 0.` `64` `62` `65` `63` `**Inputs**` `**Inputs**` `66` `64` `67` `65` `* **data** (heterogeneous) - **T**:` `* **data** (heterogeneous) - **T**:` `68` `66` ` Tensor of rank r >= 1.` ` Tensor of rank r >= 1.` `69` `67` `* **indices** (heterogeneous) - **Tind**:` `* **indices** (heterogeneous) - **Tind**:` `70` `68` ` Tensor of int32/int64 indices, of any rank q. All index values are` ` Tensor of int32/int64 indices, of any rank q. All index values are` `71` `69` ` expected to be within bounds [-s, s-1] along axis of size s. It is` ` expected to be within bounds [-s, s-1] along axis of size s. It is` `72` `70` ` an error if any of the index values are out of bounds.` ` an error if any of the index values are out of bounds.` `73` `71` `74` `72` `**Outputs**` `**Outputs**` `75` `73` `76` `74` `* **output** (heterogeneous) - **T**:` `* **output** (heterogeneous) - **T**:` `77` `75` ` Tensor of rank q + (r - 1).` ` Tensor of rank q + (r - 1).` `78` `76` `79` `77` `**Type Constraints**` `**Type Constraints**` `80` `78` `81` `79` `* **T** in (` `* **T** in (` `80` ` tensor(bfloat16),` `82` `81` ` tensor(bool),` ` tensor(bool),` `83` `82` ` tensor(complex128),` ` tensor(complex128),` `84` `83` ` tensor(complex64),` ` tensor(complex64),` `85` `84` ` tensor(double),` ` tensor(double),` `86` `85` ` tensor(float),` ` tensor(float),` `87` `86` ` tensor(float16),` ` tensor(float16),` `88` `87` ` tensor(int16),` ` tensor(int16),` `89` `88` ` tensor(int32),` ` tensor(int32),` `90` `89` ` tensor(int64),` ` tensor(int64),` `91` `90` ` tensor(int8),` ` tensor(int8),` `92` `91` ` tensor(string),` ` tensor(string),` `93` `92` ` tensor(uint16),` ` tensor(uint16),` `94` `93` ` tensor(uint32),` ` tensor(uint32),` `95` `94` ` tensor(uint64),` ` tensor(uint64),` `96` `95` ` tensor(uint8)` ` tensor(uint8)` `97` `96` ` ):` ` ):` `98` `97` ` Constrain input and output types to any tensor type.` ` Constrain input and output types to any tensor type.` `99` `98` `* **Tind** in (` `* **Tind** in (` `100` `99` ` tensor(int32),` ` tensor(int32),` `101` `100` ` tensor(int64)` ` tensor(int64)` `102` `101` ` ):` ` ):` `103` `102` ` Constrain indices to integer types` ` Constrain indices to integer types`

## Gather - 11#

Version

• name: Gather (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

Given data tensor of rank r >= 1, and indices tensor of rank q, gather entries of the axis dimension of data (by default outer-most one as axis=0) indexed by indices, and concatenates them in an output tensor of rank q + (r - 1).

axis = 0 :

Let k = indices[i_{0}, …, i_{q-1}] Then output[i_{0}, …, i_{q-1}, j_{0}, …, j_{r-2}] = input[k , j_{0}, …, j_{r-2}]

```data = [
[1.0, 1.2],
[2.3, 3.4],
[4.5, 5.7],
]
indices = [
[0, 1],
[1, 2],
]
output = [
[
[1.0, 1.2],
[2.3, 3.4],
],
[
[2.3, 3.4],
[4.5, 5.7],
],
]
```

axis = 1 :

Let k = indices[i_{0}, …, i_{q-1}] Then output[i_{0}, …, i_{q-1}, j_{0}, …, j_{r-2}] = input[j_{0}, k, j_{1}, …, j_{r-2}]

```data = [
[1.0, 1.2, 1.9],
[2.3, 3.4, 3.9],
[4.5, 5.7, 5.9],
]
indices = [
[0, 2],
]
axis = 1,
output = [
[
[1.0, 1.9],
[2.3, 3.9],
[4.5, 5.9],
],
]
```

Attributes

• axis: Which axis to gather on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data). Default value is `0`.

Inputs

• data (heterogeneous) - T: Tensor of rank r >= 1.

• indices (heterogeneous) - Tind: Tensor of int32/int64 indices, of any rank q. All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds.

Outputs

• output (heterogeneous) - T: Tensor of rank q + (r - 1).

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 any tensor type.

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

Differences

 `0` `0` `Given data tensor of rank r >= 1, and indices tensor of rank q, gather` `Given data tensor of rank r >= 1, and indices tensor of rank q, gather` `1` `1` `entries of the axis dimension of data (by default outer-most one as axis=0) indexed by indices, and concatenates` `entries of the axis dimension of data (by default outer-most one as axis=0) indexed by indices, and concatenates` `2` `2` `them in an output tensor of rank q + (r - 1).` `them in an output tensor of rank q + (r - 1).` `3` `4` `axis = 0 :` `5` `6` `Let` `3` `7` `Example 1:` `k = indices[i_{0}, ..., i_{q-1}]` `8` `Then` `9` `output[i_{0}, ..., i_{q-1}, j_{0}, ..., j_{r-2}] = input[k , j_{0}, ..., j_{r-2}]` `10` `4` `11` `::` `::` `5` `12` `6` `13` ` data = [` ` data = [` `7` `14` ` [1.0, 1.2],` ` [1.0, 1.2],` `8` `15` ` [2.3, 3.4],` ` [2.3, 3.4],` `9` `16` ` [4.5, 5.7],` ` [4.5, 5.7],` `10` `17` ` ]` ` ]` `11` `18` ` indices = [` ` indices = [` `12` `19` ` [0, 1],` ` [0, 1],` `13` `20` ` [1, 2],` ` [1, 2],` `14` `21` ` ]` ` ]` `15` `22` ` output = [` ` output = [` `16` `23` ` [` ` [` `17` `24` ` [1.0, 1.2],` ` [1.0, 1.2],` `18` `25` ` [2.3, 3.4],` ` [2.3, 3.4],` `19` `26` ` ],` ` ],` `20` `27` ` [` ` [` `21` `28` ` [2.3, 3.4],` ` [2.3, 3.4],` `29` ` [4.5, 5.7],` `30` ` ],` `31` ` ]` `32` `33` `axis = 1 :` `34` `35` `Let` `22` `36` ` [4.5, 5.7],` `k = indices[i_{0}, ..., i_{q-1}]` `23` ` ],` `24` ` ]` `25` `37` `Then` `26` `38` `Example 2:` `output[i_{0}, ..., i_{q-1}, j_{0}, ..., j_{r-2}] = input[j_{0}, k, j_{1}, ..., j_{r-2}]` `39` `27` `40` `::` `::` `28` `41` `29` `42` ` data = [` ` data = [` `30` `43` ` [1.0, 1.2, 1.9],` ` [1.0, 1.2, 1.9],` `31` `44` ` [2.3, 3.4, 3.9],` ` [2.3, 3.4, 3.9],` `32` `45` ` [4.5, 5.7, 5.9],` ` [4.5, 5.7, 5.9],` `33` `46` ` ]` ` ]` `34` `47` ` indices = [` ` indices = [` `35` `48` ` [0, 2],` ` [0, 2],` `36` `49` ` ]` ` ]` `37` `50` ` axis = 1,` ` axis = 1,` `38` `51` ` output = [` ` output = [` `39` `52` ` [` ` [` `40` `53` ` [1.0, 1.9],` ` [1.0, 1.9],` `41` `54` ` [2.3, 3.9],` ` [2.3, 3.9],` `42` `55` ` [4.5, 5.9],` ` [4.5, 5.9],` `43` `56` ` ],` ` ],` `44` `57` ` ]` ` ]` `45` `58` `46` `59` `**Attributes**` `**Attributes**` `47` `60` `48` `61` `* **axis**:` `* **axis**:` `49` `62` ` Which axis to gather on. Negative value means counting dimensions` ` Which axis to gather on. Negative value means counting dimensions` `50` `63` ` from the back. Accepted range is [-r, r-1] Default value is 0.` ` from the back. Accepted range is [-r, r-1] where r = rank(data). Default value is 0.` `51` `64` `52` `65` `**Inputs**` `**Inputs**` `53` `66` `54` `67` `* **data** (heterogeneous) - **T**:` `* **data** (heterogeneous) - **T**:` `55` `68` ` Tensor of rank r >= 1.` ` Tensor of rank r >= 1.` `56` `69` `* **indices** (heterogeneous) - **Tind**:` `* **indices** (heterogeneous) - **Tind**:` `57` `70` ` Tensor of int32/int64 indices, of any rank q. All index values are` ` Tensor of int32/int64 indices, of any rank q. All index values are` `58` `71` ` expected to be within bounds. It is an error if any of the index` ` expected to be within bounds [-s, s-1] along axis of size s. It is` `59` `72` ` values are out of bounds.` ` an error if any of the index values are out of bounds.` `60` `73` `61` `74` `**Outputs**` `**Outputs**` `62` `75` `63` `76` `* **output** (heterogeneous) - **T**:` `* **output** (heterogeneous) - **T**:` `64` `77` ` Tensor of rank q + (r - 1).` ` Tensor of rank q + (r - 1).` `65` `78` `66` `79` `**Type Constraints**` `**Type Constraints**` `67` `80` `68` `81` `* **T** in (` `* **T** in (` `69` `82` ` tensor(bool),` ` tensor(bool),` `70` `83` ` tensor(complex128),` ` tensor(complex128),` `71` `84` ` tensor(complex64),` ` tensor(complex64),` `72` `85` ` tensor(double),` ` tensor(double),` `73` `86` ` tensor(float),` ` tensor(float),` `74` `87` ` tensor(float16),` ` tensor(float16),` `75` `88` ` tensor(int16),` ` tensor(int16),` `76` `89` ` tensor(int32),` ` tensor(int32),` `77` `90` ` tensor(int64),` ` tensor(int64),` `78` `91` ` tensor(int8),` ` tensor(int8),` `79` `92` ` tensor(string),` ` tensor(string),` `80` `93` ` tensor(uint16),` ` tensor(uint16),` `81` `94` ` tensor(uint32),` ` tensor(uint32),` `82` `95` ` tensor(uint64),` ` tensor(uint64),` `83` `96` ` tensor(uint8)` ` tensor(uint8)` `84` `97` ` ):` ` ):` `85` `98` ` Constrain input and output types to any tensor type.` ` Constrain input and output types to any tensor type.` `86` `99` `* **Tind** in (` `* **Tind** in (` `87` `100` ` tensor(int32),` ` tensor(int32),` `88` `101` ` tensor(int64)` ` tensor(int64)` `89` `102` ` ):` ` ):` `90` `103` ` Constrain indices to integer types` ` Constrain indices to integer types`

## Gather - 1#

Version

• name: Gather (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

Given data tensor of rank r >= 1, and indices tensor of rank q, gather entries of the axis dimension of data (by default outer-most one as axis=0) indexed by indices, and concatenates them in an output tensor of rank q + (r - 1). Example 1:

```data = [
[1.0, 1.2],
[2.3, 3.4],
[4.5, 5.7],
]
indices = [
[0, 1],
[1, 2],
]
output = [
[
[1.0, 1.2],
[2.3, 3.4],
],
[
[2.3, 3.4],
[4.5, 5.7],
],
]
```

Example 2:

```data = [
[1.0, 1.2, 1.9],
[2.3, 3.4, 3.9],
[4.5, 5.7, 5.9],
]
indices = [
[0, 2],
]
axis = 1,
output = [
[
[1.0, 1.9],
[2.3, 3.9],
[4.5, 5.9],
],
]
```

Attributes

• axis: Which axis to gather on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] Default value is `0`.

Inputs

• data (heterogeneous) - T: Tensor of rank r >= 1.

• indices (heterogeneous) - Tind: Tensor of int32/int64 indices, of any rank q. All index values are expected to be within bounds. It is an error if any of the index values are out of bounds.

Outputs

• output (heterogeneous) - T: Tensor of rank q + (r - 1).

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 any tensor type.

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