Mojo function
topk_gpu
topk_gpu[dtype: DType, out_idx_type: DType, //, sampling: Bool = True, largest: Bool = True, _force_old_impl: Bool = False, KLayoutType: TensorLayout = Layout[RuntimeInt[DType.int64], ComptimeInt[1]], TemperatureLayoutType: TensorLayout = Layout[RuntimeInt[DType.int64], ComptimeInt[1]], TopPLayoutType: TensorLayout = Layout[RuntimeInt[DType.int64], ComptimeInt[1]], SeedLayoutType: TensorLayout = Layout[RuntimeInt[DType.int64], ComptimeInt[1]]](ctx: DeviceContext, max_k: Int, input: TileTensor[dtype, input.LayoutType, input.origin, address_space=input.address_space, linear_idx_type=input.linear_idx_type, element_size=input.element_size], out_vals: TileTensor[dtype, out_vals.LayoutType, out_vals.origin, 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, out_idxs.LayoutType, out_idxs.origin, address_space=out_idxs.address_space, linear_idx_type=out_idxs.linear_idx_type, element_size=out_idxs.element_size], block_size: Optional[Int] = None, num_blocks_per_input: Optional[Int] = None, k: Optional[TileTensor[DType.int64, KLayoutType, ImmutAnyOrigin]] = None, temperature: Optional[TileTensor[DType.float32, TemperatureLayoutType, ImmutAnyOrigin]] = None, top_p: Optional[TileTensor[DType.float32, TopPLayoutType, ImmutAnyOrigin]] = None, seed: Optional[TileTensor[DType.uint64, SeedLayoutType, ImmutAnyOrigin]] = None)
Generalized implementation of the Top K algorithm with/without sampling. Returns the sampled index from the innermost dimension of the input tensor for each row/subvolume or the top K values and indices across the tensor.
Parameters:
- dtype (
DType): DType - The data dtype of the input tensor. - out_idx_type (
DType): DType - The data dtype of the output indices (default == DType.int). - sampling (
Bool): Bool - Whether to return token samples from topK dist (default is True). - largest (
Bool): Bool - Whether to find the maximum or minimum value. - _force_old_impl (
Bool): Bool - Whether to force use the old implementation. - KLayoutType (
TensorLayout): Layout type of the k buffer. - TemperatureLayoutType (
TensorLayout): Layout type of the temperature buffer. - TopPLayoutType (
TensorLayout): Layout type of the top_p buffer. - SeedLayoutType (
TensorLayout): Layout type of the seed buffer.
Args:
- ctx (
DeviceContext): DeviceContext The context for GPU execution. - max_k (
Int): Int Largest number of top elements to keep for each batch element. - input (
TileTensor): TileTensor[dtype] Input tensor as a device TileTensor. - out_vals (
TileTensor): TileTensor[dtype] Output buffer on device for the K largest values. - out_idxs (
TileTensor): TileTensor[DType.int] Output buffer on device for the indices of the K largest values, or sampled token indices. Last dimension is 1 if sampling is True, otherwise K. - block_size (
Optional): Int The number of threads per block (default is 256 from TRT and empirical testing). - num_blocks_per_input (
Optional): Optional[Int] Number of blocks per input (default computed from input size and block size). This is the equivalent of "BLOCKS_PER_BEAM" in TRT-LLM kernel allowing for much larger batch sizes through packing several elements per thread in the first stage. - k (
Optional): Optional TileTensor[DType.int64] Device buffer of top elements to keep for each batch element. - temperature (
Optional): The temperature based scaling. - top_p (
Optional): Only use the tokens whose cumulative probability exceeds this threshold. - seed (
Optional): The seed to use for the random number generator.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!