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
18 19 20 21 22 23 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
__init__ ¶
__init__(
logprobs_mode: LogprobsMode = "raw_logprobs",
) -> None
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
aiter_sample ¶
aiter_sample(
logits: Tensor,
k: Tensor | None,
p: Tensor | None,
generators: dict[int, Generator],
) -> Tensor
Sample from logits using aiter ops.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_cpu ¶
forward_cpu(
logits: Tensor,
generators: dict[int, Generator],
k: Tensor | None,
p: Tensor | None,
) -> tuple[Tensor, Tensor | None]
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: Tensor | None,
p: Tensor | None,
) -> tuple[Tensor, Tensor | None]
More optimized implementation for top-k and top-p sampling.
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_hip ¶
forward_hip(
logits: Tensor,
generators: dict[int, Generator],
k: Tensor | None,
p: Tensor | None,
) -> tuple[Tensor, Tensor | None]
Optimized ROCm/aiter path (same structure as forward_cuda).
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
forward_native ¶
forward_native(
logits: Tensor,
generators: dict[int, Generator],
k: Tensor | None,
p: Tensor | None,
) -> tuple[Tensor, Tensor | None]
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
_to_tensor_scalar_tuple ¶
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
compiled_random_sample ¶
Source code in vllm/v1/sample/ops/topk_topp_sampler.py
flashinfer_sample ¶
flashinfer_sample(
logits: Tensor,
k: Tensor | None,
p: Tensor | None,
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.