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

top_k

def top_k[dtype: DType, out_idx_type: DType, //, largest: Bool = True, target: StringSlice[StaticConstantOrigin] = StringSlice("cpu")](input: TileTensor[dtype, address_space=input.address_space, linear_idx_type=input.linear_idx_type, element_size=input.element_size], max_k: Int, axis: Int, out_vals: TileTensor[dtype, address_space=out_vals.address_space, linear_idx_type=out_vals.linear_idx_type, element_size=out_vals.element_size], out_idxs: TileTensor[out_idx_type, address_space=out_idxs.address_space, linear_idx_type=out_idxs.linear_idx_type, element_size=out_idxs.element_size], sorted: Bool, ctx: DeviceContext, k: Optional[TileTensor[DType.int64, Layout[*?, *?], ImmutAnyOrigin]] = None)

Implementation of the Top K algorithm. Returns the top or bottom K elements and their index along a specified axis.

Parameters:

  • ​dtype (DType): Data type of the input buffer.
  • ​out_idx_type (DType): The data dtype of the output indices (default == DType.int64).
  • ​largest (Bool): Whether to find the maximum (top k) or minimum value (bottom k).
  • ​target (StringSlice[StaticConstantOrigin]): The target to run on.

Args: