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

fill_invalid_topk_kernel

def fill_invalid_topk_kernel[IROLayoutType: TensorLayout, iro_origin: ImmOrigin, cache_lengths_layout: TensorLayout, use_causal_mask: Bool](output_indices: Pointer[Int32, MutAnyOrigin, _safe=False], topk_indices: Pointer[Int32, MutAnyOrigin, _safe=False], input_row_offsets: TileTensor[DType.uint32, IROLayoutType, iro_origin], cache_lengths: TileTensor[DType.uint32, cache_lengths_layout, ImmutAnyOrigin], total_seq_len: Int, top_k: Int, effective_k: Int)

Scatter the compact top-k indices into the top_k-strided output, with invalid positions set to -1.

topk_gpu wrote valid indices to positions [0, effective_k) of the compact topk_indices buffer (row stride effective_k). This kernel copies them into output_indices (row stride top_k), setting positions that should be -1:

  • Positions [effective_k, top_k): no computed value.
  • Positions where k_idx >= num_keys for that token.
  • Positions where the index VALUE >= num_keys (topk selected an invalid key).

Output shape: [total_seq_len, top_k].

With causal masking, each token can only see keys up to its position: num_keys = cache_len + local_seq_idx + 1 Without causal masking, each token can see all keys in the batch: num_keys = cache_len + seq_len

Parameters:

  • ​IROLayoutType (TensorLayout): Layout of the input_row_offsets tensor.
  • ​iro_origin (ImmOrigin): Origin of the input_row_offsets tensor.
  • ​cache_lengths_layout (TensorLayout): Layout of the cache_lengths tensor.
  • ​use_causal_mask (Bool): Whether each token is restricted to keys up to its own position.

Args:

Was this page helpful?