vllm.v1.sample.ops.topk_topp_sampler ¶
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.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
__init__ ¶
__init__(
logprobs_mode: LogprobsMode = "raw_logprobs",
) -> None
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_cpu ¶
forward_cpu(
logits: Tensor,
generators: dict[int, Generator],
k: Optional[Tensor],
p: Optional[Tensor],
) -> tuple[Tensor, Optional[Tensor]]
PyTorch-native implementation of top-k and top-p sampling for CPU.
The logits tensor may be updated in-place.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_cuda ¶
forward_cuda(
logits: Tensor,
generators: dict[int, Generator],
k: Optional[Tensor],
p: Optional[Tensor],
) -> tuple[Tensor, Optional[Tensor]]
More optimized implementation for top-k and top-p sampling.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_native ¶
forward_native(
logits: Tensor,
generators: dict[int, Generator],
k: Optional[Tensor],
p: Optional[Tensor],
) -> tuple[Tensor, Optional[Tensor]]
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
apply_top_k_only ¶
Apply top-k mask to the logits.
This implementation doesn't involve sorting the entire vocab.
The logits tensor may be updated in-place.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
apply_top_k_top_p ¶
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 ¶
flashinfer_sample(
logits: Tensor,
k: Optional[Tensor],
p: Optional[Tensor],
generators: dict[int, Generator],
) -> Tensor
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.
NOTE: This function includes CPU-GPU synchronization, while random_sample
does not. Call this function at the end of the forward pass to minimize the synchronization overhead.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
random_sample ¶
Randomly sample from the probabilities.
We use this function instead of torch.multinomial because torch.multinomial causes CPU-GPU synchronization.