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

persistent_topk_block

def persistent_topk_block(ctx: DeviceContext, in_scores: Pointer[Float32, ImmutAnyOrigin, _safe=False], out_idxs: Pointer[Int32, MutAnyOrigin, _safe=False], N: Int, K: Int, total_seq_len: Int)

Launch block-wide bitonic top-k for total_seq_len score rows.

For N ≀ PERSISTENT_TOPK_MAX_N (= 2048) a single block sorts the whole row. For N > PERSISTENT_TOPK_MAX_N a streaming variant folds _TILE-wide tiles into a running top-_TILE champion; this requires K ≀ PERSISTENT_TOPK_MAX_N (the champion width). Call sites needing K > PERSISTENT_TOPK_MAX_N must use topk_gpu.

Each row of N float32 scores yields the K highest-scoring column indices (as int32) in descending score order in out_idxs.

Args:

  • ​ctx (DeviceContext): Device context.
  • ​in_scores (Pointer[Float32, ImmutAnyOrigin, _safe=False]): Flat score buffer [total_seq_len Γ— N] row-major.
  • ​out_idxs (Pointer[Int32, MutAnyOrigin, _safe=False]): Output buffer [total_seq_len Γ— K] row-major (int32).
  • ​N (Int): Score columns per token.
  • ​K (Int): Top-k count per token (≀ N, and ≀ PERSISTENT_TOPK_MAX_N when N > 2048).
  • ​total_seq_len (Int): Number of rows (one block per row).