Skip to content

vllm.v1.sample.ops.topk_topp_sampler

Functions:

_flashinfer_jit_unsupported_reason(capability)

Return why FlashInfer JIT codegen cannot target the current GPU, or None if it can.

FlashInfer swallows arch-detection errors when building its compilation context (e.g. SM 12.x with a CUDA toolkit older than 12.9), leaving an empty target-arch set that makes every JIT spec fail with a misleading "requires sm75 or higher" error at first use — killing the engine during startup profiling (https://github.com/vllm-project/vllm/issues/42393).

apply_top_k_only(logits, k)

Apply top-k mask to the logits.

This implementation doesn't involve sorting the entire vocab. Note however that it involves a GPU->CPU sync which can be detrimental for async scheduling performance.

The logits tensor may be updated in-place.

apply_top_k_top_p(logits, k, p, has_topp_only_rows=True)

Apply top-k and/or top-p masking to logits.

has_topp_only_rows must be True if any row has p < 1 and k >= logits.shape[-1] (no active top-k).

apply_top_k_top_p_cpu(logits, k, p)

CPU-optimized top-k + top-p. Uses C++ ternary-search kernels.

apply_top_k_top_p_pytorch(logits, k, p, allow_cpu_sync=False)

Apply top-k and top-p masks to the logits.

If a top-p is used, this function will sort the logits tensor, which can be slow for large batches.

The logits tensor may be updated in-place.

flashinfer_sample(logits, k, p, generators={})

Sample from the logits using FlashInfer.

Statistically, this function is equivalent to top-k/top-p filtering followed by weighted random sampling. However, this function is faster because it avoids sorting the logits tensor via rejection sampling.

flashinfer_sampler_supported()

Decide whether FlashInfer's top-p/top-k sampler can be used.

Returns False (with appropriate logging) when VLLM_USE_FLASHINFER_SAMPLER is 0, when the platform isn't CUDA, when the GPU's compute capability is unsupported, when the GPU has 16 or fewer SMs, or when FlashInfer cannot JIT-compile for the current GPU/CUDA toolchain. Raises RuntimeError if the user explicitly opted in via the env var but FlashInfer is unavailable.

Assumes flashinfer is installed, as guaranteed by requirements/cuda.txt; otherwise importing the FlashInfer backend below raises ImportError.

Note: callers must additionally ensure logprobs_mode doesn't require post-top-k/top-p logits/logprobs for any request whose logprobs will be returned in this step, since FlashInfer doesn't expose those.

register_top_k_top_p_warmups()

Register every native accelerator sampling kernel used at runtime.

xpu_sample(logits, k, p, logprobs_mode='raw_logprobs')

Sample from the logits using the fused XPU top-k/top-p kernel.

Statistically equivalent to top-k/top-p filtering followed by weighted random sampling, but avoids sorting the vocab and never materializes the probability tensor. The noise comes from the device's default generator, so per-request generators aren't supported.

Returns the sampled token ids and, for processed logprobs modes, the post-top-k/top-p logits (or logprobs); otherwise None.

xpu_sampler_supported()

Decide whether the fused XPU top-k/top-p sampler kernel can be used.

Returns False (with appropriate logging) when the platform isn't XPU, when VLLM_XPU_USE_SAMPLER_KERNEL is 0.

Note: callers must additionally ensure no request needs a per-request seed or greedy sampling, since the kernel draws from the device's default generator and always samples randomly.