IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /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. /get-started.md).

Mojo function

gather_elementwise_fn_wrapper

def gather_elementwise_fn_wrapper[input_rank: Int, indices_rank: Int, output_rank: Int, dtype: DType, indices_type: DType, InputFnType: def[width: Int, element_alignment: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable, IndicesFnType: def[width: Int](Coord[**?]) -> SIMD[indices_type, width] & RegisterPassable & ImplicitlyCopyable, OutputFnType: def[width: SIMDLength, element_alignment: Int](Coord[**?], SIMD[dtype, width]) -> None & RegisterPassable & ImplicitlyCopyable, PrefetchFnType: def(Coord[**?], Coord[**?]) -> None & RegisterPassable & ImplicitlyCopyable, *, simd_width: Int, has_prefetch: Bool, target: CompilationTarget, element_alignment: Int = Int(1)](input_fn: InputFnType, indices_fn: IndicesFnType, output_fn: OutputFnType, prefetch_fn: PrefetchFnType, axis: Axis, input_shape: DynamicCoord[.int64, input_rank], indices_shape: DynamicCoord[.int64, indices_rank], output_shape: DynamicCoord[.int64, output_rank], coords: Coord[*coords.element_types], error_index_ptr: OptionalReg[Pointer[Int, MutAnyOrigin]] = None)

Performs a single elementwise gather step for one output coordinate.

Loads the gather index from indices at the coordinate derived from coords, normalizes it against the input axis size, reads the corresponding value from input, and stores it into output at the original coordinate. On CPU, out-of-bounds indices are recorded through error_index_ptr for later reporting; on GPU, a debug assert traps instead.

Parameters:

  • ​input_rank (Int): Rank of the input tensor.
  • ​indices_rank (Int): Rank of the indices tensor.
  • ​output_rank (Int): Rank of the output tensor.
  • ​dtype (DType): Element type of the input and output values.
  • ​indices_type (DType): Element type of the indices buffer.
  • ​InputFnType (def[width: Int, element_alignment: Int](Coord[**?]) -> SIMD[dtype, width] & RegisterPassable & ImplicitlyCopyable): Function type that loads a SIMD vector from the input tensor at given coordinates.
  • ​IndicesFnType (def[width: Int](Coord[**?]) -> SIMD[indices_type, width] & RegisterPassable & ImplicitlyCopyable): Function type that loads a SIMD vector of indices from the indices buffer at given coordinates.
  • ​OutputFnType (def[width: SIMDLength, element_alignment: Int](Coord[**?], SIMD[dtype, width]) -> None & RegisterPassable & ImplicitlyCopyable): Function type that stores a SIMD vector into the output tensor at given coordinates.
  • ​PrefetchFnType (def(Coord[**?], Coord[**?]) -> None & RegisterPassable & ImplicitlyCopyable): Function type of the software index-prefetch callback.
  • ​simd_width (Int): SIMD vector width for the elementwise load and store. When gathering the input's last axis on CPU the width applies to the indices and the output instead, and the data loads are scalar.
  • ​has_prefetch (Bool): Whether to call prefetch_fn before each load (not done on the vectorized last-axis path).
  • ​target (CompilationTarget): Compilation target the gather runs on.
  • ​element_alignment (Int): Alignment factor for element loads and stores (defaults to 1).

Args:

  • ​input_fn (InputFnType): Callback that loads values from the input tensor.
  • ​indices_fn (IndicesFnType): Callback that loads indices from the indices tensor.
  • ​output_fn (OutputFnType): Callback that stores values into the output tensor.
  • ​prefetch_fn (PrefetchFnType): Software index-prefetch callback; ignored unless has_prefetch.
  • ​axis (Axis): Validated axis along which to gather from the input tensor.
  • ​input_shape (DynamicCoord[.int64, input_rank]): Shape of the input tensor.
  • ​indices_shape (DynamicCoord[.int64, indices_rank]): Shape of the indices tensor.
  • ​output_shape (DynamicCoord[.int64, output_rank]): Shape of the output tensor.
  • ​coords (Coord[*coords.element_types]): Output coordinate being processed in this gather step.
  • ​error_index_ptr (OptionalReg[Pointer[Int, MutAnyOrigin]]): Optional pointer to record an out-of-bounds index for CPU error reporting (defaults to None).

Was this page helpful?