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 struct
TopKPerRow
struct TopKPerRow
Registers the reduction.top_k.per_row graph op with the graph compiler.
mo.top_k over the last axis of a [rows, n] input, with a count per
row: row r keeps the first k[r] picks mo.top_k with max_k makes
for it, in descending order, and pads the rest of its max_k slots with
the dead value and index -1. Equal values may come out in any order,
and which of them fill the last slots is unspecified. The input must
not contain NaN. On GPU, float32 and bfloat16 rows with
max_k <= 2048 run the block-wide top-k in topk_per_row, which
selects all picks without one serial reduction per pick.
Implemented traits
Methods
execute
static def execute[max_k: Int](values: T, indices: T, input: T, k: T, ctx: DeviceContext)
Executes the reduction.top_k.per_row graph op.
Parameters:
- max_k (
Int): Output width, the largest count any row may ask for.
Args:
- values (
T):[rows, max_k]selected values. - indices (
T):[rows, max_k]their column indices. - input (
T):[rows, n]values to select from. - k (
T):[rows]picks per row, each in[0, max_k]. - ctx (
DeviceContext): Device context used to enqueue the kernel.
Raises:
Error: If the operation parameters are invalid.