Skip to main content

Mojo function

topk_sampling_from_prob

topk_sampling_from_prob[dtype: DType, out_idx_type: DType, block_size: Int = 1024, TopKArrLayoutType: TensorLayout = Layout[*?, *?], IndicesLayoutType: TensorLayout = Layout[*?, *?]](ctx: DeviceContext, probs: TileTensor[dtype, address_space=probs.address_space, linear_idx_type=probs.linear_idx_type, element_size=probs.element_size], output: TileTensor[out_idx_type, address_space=output.address_space, linear_idx_type=output.linear_idx_type, element_size=output.element_size], top_k_val: Int, deterministic: Bool = False, rng_seed: UInt64 = UInt64(0), rng_offset: UInt64 = UInt64(0), indices: Optional[TileTensor[out_idx_type, IndicesLayoutType, MutExternalOrigin]] = None, top_k_arr: Optional[TileTensor[out_idx_type, TopKArrLayoutType, MutExternalOrigin]] = None)

Top-K sampling from probability distribution.

Performs stochastic sampling from a probability distribution, considering only the top-k most probable tokens. Uses rejection sampling with ternary search to efficiently find appropriate samples.

Args:

Raises:

Error: If tensor ranks or shapes are invalid.