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 function
TopKTopPSamplingFromProbKernel
def TopKTopPSamplingFromProbKernel[ProbsLayoutType: TensorLayout, probs_origin: ImmOrigin, OutputLayoutType: TensorLayout, output_origin: MutOrigin, block_size: Int, vec_size: Int, dtype: DType, out_idx_type: DType, deterministic: Bool, from_logits: Bool = False, emit_dist: Bool = False, dist_dtype: DType = DType.float32, ProbsStorageType: TensorStorage = PointerStorage, OutputStorageType: TensorStorage = PointerStorage](probs: TileTensor[dtype, ProbsLayoutType, probs_origin, Storage=ProbsStorageType], output: TileTensor[out_idx_type, OutputLayoutType, output_origin, Storage=OutputStorageType], out_dist: Optional[Pointer[Scalar[dist_dtype], MutAnyOrigin]], indices: Optional[Pointer[Scalar[out_idx_type], ImmutAnyOrigin]], top_k_arr: Optional[Pointer[Scalar[out_idx_type], ImmutAnyOrigin]], top_k_val: Int32, top_p_arr: Optional[Pointer[Float32, ImmutAnyOrigin]], top_p_val: Float32, d: Int32, rng_seed: Optional[Pointer[UInt64, ImmutAnyOrigin]], rng_offset: UInt64, temperature: Optional[Pointer[Float32, ImmutAnyOrigin]], min_p: Optional[Pointer[Float32, ImmutAnyOrigin]])
Kernel for joint top-k + top-p sampling from probability distribution.
Identical to TopKSamplingFromProbKernel but additionally enforces a nucleus (top-p) constraint: a token is accepted only when both the count of tokens above the pivot is less than k AND the cumulative probability of those tokens is less than p.
When top_p_val = 1.0 and top_p_arr is null, this degrades to top-k-only with zero overhead since sum < 1.0 is always true.
When from_logits is True, probs contains raw logits and softmax with
per-row temperature scaling is fused into the kernel: every load is
transformed to exp((logit - row_max) / temp), the unnormalized softmax
value with the row maximum shifted to exactly 1.0. The pivot search over
[0, 1] is unchanged; the total unnormalized mass z replaces the
normalized distribution's implicit total of 1.0 in the initial CDF budget
and scales the top-p threshold. The optional min-p mask is applied inline
(in this domain the max "probability" is 1.0, so the mask threshold is
simply min_p), matching apply_min_p_mask_kernel semantics in the
normalized domain.
When emit_dist is set, the kernel also writes the masked renormalized
distribution it drew from to out_dist. Speculative decoding builds its
rejection residual from that distribution, and reads the sampled token's
own probability back out of it. Requires from_logits, and a non-Apple
GPU because the cutoff search uses block collectives.
Parameters:
- ProbsLayoutType (
TensorLayout): Memory layout of the inputprobstile. - probs_origin (
ImmOrigin): Origin tag for the immutable inputprobstile. - OutputLayoutType (
TensorLayout): Memory layout of the outputoutputtile. - output_origin (
MutOrigin): Origin tag for the mutable outputoutputtile. - block_size (
Int): Number of threads per block. - vec_size (
Int): Number of elements each thread loads per vectorized access. - dtype (
DType): Element type of theprobstensor. - out_idx_type (
DType): Index type used for the sampled output indices. - deterministic (
Bool): If True, use deterministic sampling. - 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). - emit_dist (
Bool): If True, also write the masked distribution toout_dist(defaults to False). - dist_dtype (
DType): Element type ofout_dist. - ProbsStorageType (
TensorStorage): Storage type of the inputprobstile. - OutputStorageType (
TensorStorage): Storage type of the outputoutputtile.
Args:
- probs (
TileTensor[dtype, ProbsLayoutType, probs_origin, Storage=ProbsStorageType]): Input probability distribution [batch_size, _d]. - output (
TileTensor[out_idx_type, OutputLayoutType, output_origin, Storage=OutputStorageType]): Output sampled indices [batch_size]. - out_dist (
Optional[Pointer[Scalar[dist_dtype], MutAnyOrigin]]): Output masked distribution [batch_size, _d]; required whenemit_distis set. - indices (
Optional[Pointer[Scalar[out_idx_type], ImmutAnyOrigin]]): Optional row indices for batch indexing [batch_size]. - top_k_arr (
Optional[Pointer[Scalar[out_idx_type], ImmutAnyOrigin]]): Optional per-row top_k values [batch_size]. - top_k_val (
Int32): Default top_k value if top_k_arr is null. - top_p_arr (
Optional[Pointer[Float32, ImmutAnyOrigin]]): Optional per-row top_p values [batch_size]. - top_p_val (
Float32): Default top_p value if top_p_arr is null. - d (
Int32): Vocabulary size. - rng_seed (
Optional[Pointer[UInt64, ImmutAnyOrigin]]): Optional per-row seed array [batch_size], indexed by row_idx. If null, defaults to 0. - rng_offset (
UInt64): Random offset for Random number generator. - temperature (
Optional[Pointer[Float32, ImmutAnyOrigin]]): Optional per-row temperature [batch_size]. Only used whenfrom_logitsis True; defaults to 1.0 per row. - min_p (
Optional[Pointer[Float32, ImmutAnyOrigin]]): Optional per-row min-p thresholds [batch_size]. Only used whenfrom_logitsis True.