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
topk_topp_sampling_from_prob
def topk_topp_sampling_from_prob[dtype: DType, out_idx_type: DType, block_size: Int = Int(1024), from_logits: Bool = False, TopKArrLayoutType: TensorLayout = Layout[*?, *?], IndicesLayoutType: TensorLayout = Layout[*?, *?], TopPArrLayoutType: TensorLayout = Layout[*?, *?], SeedLayoutType: TensorLayout = Layout[*?, *?], TemperatureLayoutType: TensorLayout = Layout[*?, *?], MinPLayoutType: TensorLayout = Layout[*?, *?]](ctx: DeviceContext, probs: TileTensor[dtype, Storage=probs.Storage, address_space=probs.address_space, linear_idx_type=probs.linear_idx_type], output: TileTensor[out_idx_type, Storage=output.Storage, address_space=output.address_space, linear_idx_type=output.linear_idx_type], top_k_val: Int, top_p_val: Float32 = 1, deterministic: Bool = False, rng_seed: Optional[TileTensor[DType.uint64, SeedLayoutType, ImmutAnyOrigin]] = None, rng_offset: UInt64 = UInt64(0), indices: Optional[TileTensor[out_idx_type, IndicesLayoutType, ImmutAnyOrigin]] = None, top_k_arr: Optional[TileTensor[out_idx_type, TopKArrLayoutType, ImmutAnyOrigin]] = None, top_p_arr: Optional[TileTensor[DType.float32, TopPArrLayoutType, ImmutAnyOrigin]] = None, temperature: Optional[TileTensor[DType.float32, TemperatureLayoutType, ImmutAnyOrigin]] = None, min_p: Optional[TileTensor[DType.float32, MinPLayoutType, ImmutAnyOrigin]] = None)
Joint top-k + top-p sampling from probability distribution.
Performs stochastic sampling considering only tokens that satisfy both the top-k count constraint AND the top-p nucleus constraint. When top_p_val is 1.0 (default) this behaves identically to topk_sampling_from_prob.
When from_logits is True, probs contains raw logits: softmax with
per-row temperature scaling and the optional min-p mask are fused into
the sampling kernel, avoiding the [batch_size, d] probability round-trip
through global memory and the separate softmax / mask kernel launches.
Parameters:
- βdtype (
DType): Element type of theprobstensor. - βout_idx_type (
DType): Index type used for the sampled output indices. - βblock_size (
Int): Number of threads per block (defaults to 1024). - βfrom_logits (
Bool): If True,probsholds raw logits and softmax with per-row temperature scaling and min-p masking is fused into the kernel (defaults to False). - βTopKArrLayoutType (
TensorLayout): Memory layout of the optionaltop_k_arrtensor. - βIndicesLayoutType (
TensorLayout): Memory layout of the optionalindicestensor. - βTopPArrLayoutType (
TensorLayout): Memory layout of the optionaltop_p_arrtensor. - βSeedLayoutType (
TensorLayout): Memory layout of the optionalrng_seedtensor. - βTemperatureLayoutType (
TensorLayout): Memory layout of the optionaltemperaturetensor. - βMinPLayoutType (
TensorLayout): Memory layout of the optionalmin_ptensor.
Args:
- βctx (
DeviceContext): Device context for kernel execution. - βprobs (
TileTensor[dtype, Storage=probs.Storage, address_space=probs.address_space, linear_idx_type=probs.linear_idx_type]): Input probability distribution [batch_size, d], or raw logits whenfrom_logitsis True. - βoutput (
TileTensor[out_idx_type, Storage=output.Storage, address_space=output.address_space, linear_idx_type=output.linear_idx_type]): Output sampled indices [batch_size]. - βtop_k_val (
Int): Default top-k value (number of top tokens to consider). - βtop_p_val (
Float32): Default top-p value (nucleus probability threshold). - βdeterministic (
Bool): Whether to use deterministic sampling. - βrng_seed (
Optional[TileTensor[DType.uint64, SeedLayoutType, ImmutAnyOrigin]]): Optional per-row seed tensor [batch_size], indexed by the request's logical row (seeindices). If None, defaults to 0. - βrng_offset (
UInt64): Random offset for Random number generator. - βindices (
Optional[TileTensor[out_idx_type, IndicesLayoutType, ImmutAnyOrigin]]): Optional row indices for batch indexing [batch_size]. - βtop_k_arr (
Optional[TileTensor[out_idx_type, TopKArrLayoutType, ImmutAnyOrigin]]): Optional per-row top-k values [batch_size]. - βtop_p_arr (
Optional[TileTensor[DType.float32, TopPArrLayoutType, ImmutAnyOrigin]]): Optional per-row top-p values [batch_size]. - βtemperature (
Optional[TileTensor[DType.float32, TemperatureLayoutType, ImmutAnyOrigin]]): Optional per-row temperature values [batch_size]. Only used whenfrom_logitsis True; defaults to 1.0 per row. - βmin_p (
Optional[TileTensor[DType.float32, MinPLayoutType, ImmutAnyOrigin]]): Optional per-row min-p thresholds [batch_size]. Only used whenfrom_logitsis True.
Raises:
Error: If tensor ranks or shapes are invalid.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!