vllm.v1.sample.ops.topk_topp_sampler
¶
Classes:
-
TopKTopPSampler–Module that performs optional top-k and top-p filtering followed by
Functions:
-
apply_top_k_only–Apply top-k mask to the logits.
-
apply_top_k_top_p_pytorch–Apply top-k and top-p masks to the logits.
-
flashinfer_sample–Sample from the logits using FlashInfer.
-
flashinfer_sampler_supported–Decide whether FlashInfer's top-p/top-k sampler can be used.
-
random_sample–Randomly sample from the probabilities.
-
register_top_k_top_p_warmups–Register every native accelerator sampling kernel used at runtime.
-
xpu_sample–Sample from the logits using the fused XPU top-k/top-p kernel.
-
xpu_sampler_supported–Decide whether the fused XPU top-k/top-p sampler kernel can be used.
TopKTopPSampler
¶
Bases: Module
Module that performs optional top-k and top-p filtering followed by weighted random sampling of logits.
Implementations may update the logits tensor in-place.
Methods:
-
aiter_sample–Sample from logits using aiter ops.
-
forward_cpu–Fused Gumbel-max sampling for CPU.
-
forward_cuda–More optimized implementation for top-k and top-p sampling.
-
forward_hip–Optimized ROCm/aiter path (same structure as forward_cuda).
-
forward_native–PyTorch-native implementation of top-k and top-p sampling.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
aiter_sample(logits, k, p, generators)
¶
Sample from logits using aiter ops.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_cpu(logits, generators, k, p)
¶
Fused Gumbel-max sampling for CPU.
Uses a precomputed Gumbel table + single-pass argmax over logits, skipping softmax and intermediate allocations entirely. Falls back to the native path when fp64 Gumbel noise is requested.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_cuda(logits, generators, k, p)
¶
More optimized implementation for top-k and top-p sampling.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_hip(logits, generators, k, p)
¶
Optimized ROCm/aiter path (same structure as forward_cuda).
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_native(logits, generators, k, p)
¶
PyTorch-native implementation of top-k and top-p sampling.
The logits tensor may be updated in-place.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
_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).
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
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.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
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.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
flashinfer_sample(logits, k, p, generators={})
¶
Sample from the logits using FlashInfer.
Statistically, this function is equivalent to the random_sample function.
However, this function is faster because it avoids sorting the logits tensor
via rejection sampling.
NOTE: The outputs of this function do not necessarily match the outputs of
the random_sample function. It only guarantees that the outputs are
statistically equivalent.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
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.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
random_sample(probs, generators, use_fp64_gumbel=False)
¶
Randomly sample from the probabilities.
We use this function instead of torch.multinomial because torch.multinomial causes CPU-GPU synchronization.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
register_top_k_top_p_warmups()
¶
Register every native accelerator sampling kernel used at runtime.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
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 random_sample, 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.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
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.