---
title: "ClikaRT::ops::gather"
sidebar_label: "gather"
description: "Axis-wise gather: read input at positions given by index along dim. out[i][j][k] = x[index[i][j][k]][j][k] for dim = 0 (likewise for any other dim; only that axis's coordinate is replaced)."
---

<!-- Generated by tools/doxygen2mdx from Doxygen XML. Do not edit. -->

<span class="api-kind api-kind--function">function</span>

## gather() {#gather}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`gather`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `input`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int64_t` `dim`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `index`<br />
)

</div>

Axis-wise gather: read `input` at positions given by [`index`](./index-fn.md#index) along `dim`.

`out[i][j][k] = x[`[`index`](./index-fn.md#index)`[i][j][k]][j][k]` for `dim = 0` (likewise for any other `dim`; only that axis's coordinate is replaced). The output takes [`index`](./index-fn.md#index)'s shape and `input`'s dtype. Index tensors are `Int32` or `Int64` on every backend; entries must lie in `[0, x`'s `dim` extent`)`.

**Parameters**

- `input`: the source.
- `dim`: the axis the indices address (negative wraps).
- `index`: integer positions, same rank as `input`.

**Throws**

- `ClikaRT::Error`: (`INVALID_ARGUMENT`) when `dim` is out of range or the index dtype is not `Int32`/`Int64` (`code_name()` carries the reason).

**Returns:** a new tensor shaped like [`index`](./index-fn.md#index), dtype of `input`.

```cpp
// one logit per row: [B, V] gathered by [B, 1] ids -> [B, 1]
auto picked = ClikaRT::ops::gather(logits, 1, ids);
```

<div class="api-declared">

Declared in `ClikaRT/compute/ops.h`, line 2492

</div>
