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 ofinput,output, andreduce_init. - βgather_axis (
Int): Axis ofinputto 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:
- βoutput (
TileTensor[dtype, Storage=output.Storage, address_space=output.address_space, linear_idx_type=output.linear_idx_type]): Output tensor holding the reduced embeddings, shape [batch, embedding_dim]. - βinput (
TileTensor[dtype, Storage=input.Storage, address_space=input.address_space, linear_idx_type=input.linear_idx_type]): Source embedding table, shape [num_embeddings, embedding_dim]. - βindices (
TileTensor[DType.int32, Storage=indices.Storage, address_space=indices.address_space, linear_idx_type=indices.linear_idx_type]): Multi-hot indices, shape [batch, multi_hot],int32. - βreduce_init (
Scalar[dtype]): Initial accumulator value for the reduction. - βctx (
Optional[DeviceContext]): Optional device context for parallel execution (defaults to None).
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!