IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

Mojo function

gather_reduce

def gather_reduce[dtype: DType, gather_axis: Int, reduce_axis: Int, simd_width: Int, reduce_fn: def[dtype: DType, width: SIMDLength](SIMD[dtype, width], SIMD[dtype, width]) thin -> SIMD[dtype, width]](output: TileTensor[dtype, Storage=output.Storage, address_space=output.address_space, linear_idx_type=output.linear_idx_type], input: TileTensor[dtype, Storage=input.Storage, address_space=input.address_space, linear_idx_type=input.linear_idx_type], indices: TileTensor[DType.int32, Storage=indices.Storage, address_space=indices.address_space, linear_idx_type=indices.linear_idx_type], reduce_init: Scalar[dtype], ctx: Optional[DeviceContext] = None)

Computes output[i, j, k] = input[indices[i, j], k] and simultaneously reduces the output across axis 1 to produce output[i, k].

The motivating use-case for this is multi-hot embeddings in recommender models. This provides similar functionality to Torch's EmbeddingBag layer. In that context, i is the batch dimension, j is the multi-hot dimension, and k is the embedding dimension.

Parameters:

  • ​dtype (DType): Element type of input, output, and reduce_init.
  • ​gather_axis (Int): Axis of input to gather along (must be 0).
  • ​reduce_axis (Int): Axis of the gathered result to reduce across (must be 1).
  • ​simd_width (Int): SIMD vector width used for tiling the embedding dimension.
  • ​reduce_fn (def[dtype: DType, width: SIMDLength](SIMD[dtype, width], SIMD[dtype, width]) thin -> SIMD[dtype, width]): Binary reduction function combining accumulated and gathered values into the output.

Args: