IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
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 the probs tensor.
  • ​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, probs holds 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 optional top_k_arr tensor.
  • ​IndicesLayoutType (TensorLayout): Memory layout of the optional indices tensor.
  • ​TopPArrLayoutType (TensorLayout): Memory layout of the optional top_p_arr tensor.
  • ​SeedLayoutType (TensorLayout): Memory layout of the optional rng_seed tensor.
  • ​TemperatureLayoutType (TensorLayout): Memory layout of the optional temperature tensor.
  • ​MinPLayoutType (TensorLayout): Memory layout of the optional min_p tensor.

Args:

Raises:

Error: If tensor ranks or shapes are invalid.