class nbla::Gather

template<typename T>
class Gather : public nbla::BaseFunction<int, int>

Gather from the input data according to the index.

Given the input data \(X\) of \((D_{0}, \ldots, D_{N-1})\) shape and the indices \(IDX\) of \((I_{0}, \ldots, I_{M-1})\) shape, in case of `batch_dims = 0`, the gather outputs

\[\begin{split}\begin{eqnarray*} && Y[d_{0}, \ldots, d_{axis - 1}, i_{0}, \ldots, i_{M-1}, d_{axis + 1}, \ldots, d_{N-1}] = \\ && X[d_{0}, \ldots, d_{axis - 1}, IDX[i_{0}, \ldots, i_{M-1}], d_{axis + 1}, \ldots, d_{N-1}]. \end{eqnarray*}\end{split}\]

Generally, the gather ouptuts

\[\begin{split}\begin{eqnarray*} && Y[d_{0}, \ldots, d_{axis - 1}, i_{B}, \ldots, i_{M-1}, d_{axis + 1}, \ldots, d_{N-1}] = \\ && X[d_{0}, \ldots, d_{axis - 1}, IDX[i_{0}, \ldots, i_{B - 1}, i_{B} \ldots, i_{M-1}], d_{axis + 1}, \ldots d_{N-1}]. \end{eqnarray*}\end{split}\]

where \(B\) = `batch_dims`.

`x.shape[:batch_dims]` must be equal to `indices.shape[:batch_dims]`.

Output shape is `x.shape[:axis] + indices.shape[batch_dims:] + x.shape[axis + 1]`.

Inputs:

  • Data from which to gather.

  • Index with which to gather.

Outputs:

  • Gathered output.

Template Parameters:

T – Data type for computation.

Param axis:

Axis in the data to gather from. `axis` must be greater than or equal to `batch_dims`.

Param batch_dims:

The number of batch dimensions.

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.