vllm.v1.sample.rejection_sampler ¶
RejectionSampler ¶
Bases: Module
The implementation strictly follows the algorithm described in https://arxiv.org/abs/2211.17192. However, we want to clarify the terminology used in the implementation: accepted tokens: tokens that are accepted based on the relationship between the "raw" draft and target probabilities. recovered tokens: tokens that are sampled based on the adjusted probability distribution, which is derived from both the draft and target probabilities. bonus tokens: If all proposed tokens are accepted, the bonus token is added to the end of the sequence. The bonus token is only sampled from the target probabilities. We pass in the bonus tokens instead of sampling them in the rejection sampler to allow for more flexibility in the sampling process. For example, we can use top_p, top_k sampling for bonus tokens, while spec decode does not support these sampling strategies. output tokens: Tokens are finally generated with the rejection sampler. output tokens = accepted tokens + recovered tokens + bonus tokens
Source code in vllm/v1/sample/rejection_sampler.py
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 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 | |
is_processed_logprobs_mode instance-attribute ¶
_combine_outputs_with_spec_tokens staticmethod ¶
_combine_outputs_with_spec_tokens(
output_token_ids: list[list[int]],
spec_token_ids: list[list[int]] | None = None,
) -> list[list[int]]
Source code in vllm/v1/sample/rejection_sampler.py
_get_logprobs_tensors ¶
_get_logprobs_tensors(
max_num_logprobs: int,
metadata: SpecDecodeMetadata,
logits: Tensor,
target_logits: Tensor,
bonus_logits: Tensor,
sampled_token_ids: Tensor,
) -> LogprobsTensors
Source code in vllm/v1/sample/rejection_sampler.py
apply_logits_processors ¶
apply_logits_processors(
logits: Tensor,
sampling_metadata: SamplingMetadata,
metadata: SpecDecodeMetadata,
) -> Tensor
Source code in vllm/v1/sample/rejection_sampler.py
apply_penalties staticmethod ¶
apply_penalties(
logits: Tensor,
sampling_metadata: SamplingMetadata,
metadata: SpecDecodeMetadata,
repeat_indices: Tensor,
output_token_ids: list[list[int]],
) -> Tensor
Source code in vllm/v1/sample/rejection_sampler.py
forward ¶
forward(
metadata: SpecDecodeMetadata,
draft_probs: Tensor | None,
logits: Tensor,
sampling_metadata: SamplingMetadata,
) -> SamplerOutput
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata | SpecDecodeMetadata | Metadata for spec decoding. | required |
draft_probs | Optional[Tensor] | Probability distribution for the draft tokens. Shape is [num_tokens, vocab_size]. Can be None if probabilities are not provided, which is the case for ngram spec decode. | required |
logits | Tensor | Target model's logits probability distribution. Shape is [num_tokens + batch_size, vocab_size]. Here, probabilities from different requests are flattened into a single tensor because this is the shape of the output logits. NOTE: | required |
sampling_metadata | SamplingMetadata | Additional metadata needed for sampling, such as temperature, top-k/top-p parameters, or other relevant information. | required |
Returns: SamplerOutput: Contains the final output token IDs and their logprobs if requested.
Source code in vllm/v1/sample/rejection_sampler.py
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 | |
parse_output staticmethod ¶
Parse the output of the rejection sampler. Args: output_token_ids: The sampled token IDs in shape [batch_size, max_spec_len + 1]. The rejected tokens are replaced with PLACEHOLDER_TOKEN_ID by the rejection sampler and will be filtered out in this function. vocab_size: The size of the vocabulary. Returns: A list of lists of token IDs.
Source code in vllm/v1/sample/rejection_sampler.py
apply_sampling_constraints ¶
apply_sampling_constraints(
logits: Tensor,
cu_num_draft_tokens: Tensor,
sampling_metadata: SamplingMetadata,
) -> Tensor
Process logits based on sampling metadata.
This function applies temperature scaling to the logits, as well as top-k and top-p. For greedy decoding, it returns the original logits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Input logits tensor to be processed. | required |
cu_num_draft_tokens | Tensor | Cumulative number of draft tokens. | required |
sampling_metadata | SamplingMetadata | Metadata containing sampling parameters such as temperature and whether greedy sampling is used. | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Processed logits if non-greedy sampling is used, |
Tensor | otherwise returns the original logits. |
Source code in vllm/v1/sample/rejection_sampler.py
expand_batch_to_tokens ¶
expand_batch_to_tokens(
x: Tensor,
cu_num_tokens: Tensor,
num_tokens: int,
replace_from: int = 0,
replace_to: int = 0,
) -> Tensor
Expand [batch_size] tensor to [num_tokens] tensor based on the number of tokens per batch in cu_num_tokens.
For example, if x = [a, b, c] and cu_num_tokens = [2, 5, 6], then num_tokens = 6, and expanded_x = [a, a, b, b, b, c].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Tensor | [batch_size] tensor to expand. | required |
cu_num_tokens | Tensor | [batch_size] tensor containing the cumulative number of tokens per batch. Each element represents the total number of tokens up to and including that batch. | required |
num_tokens | int | Total number of tokens. | required |
replace_from | int | int = 0 Value to be replaced if it is found in x. | 0 |
replace_to | int | int = 0 Value to replace with when replace_from is found. | 0 |
Returns: expanded_x: [num_tokens] tensor.
Source code in vllm/v1/sample/rejection_sampler.py
expand_kernel ¶
expand_kernel(
output_ptr,
input_ptr,
cu_num_tokens_ptr,
replace_from,
replace_to,
MAX_NUM_TOKENS: constexpr,
)
Source code in vllm/v1/sample/rejection_sampler.py
generate_uniform_probs ¶
generate_uniform_probs(
num_tokens: int,
num_draft_tokens: list[int],
generators: dict[int, Generator],
device: device,
) -> Tensor
Generates a batch of uniform random samples, with optional seeding if available.
This method creates a tensor of shape (num_tokens, ) filled with uniform random values in the range [0, 1). If generators is provided, the requests with their own seeds will use the provided torch.Generator for reproducibility. The samples for the other requests will be generated without a seed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_tokens | int | int Total number of tokens. | required |
num_draft_tokens | list[int] | List[List[int]] Number of draft tokens per request. | required |
generators | dict[int, Generator] | Optional[Dict[int, torch.Generator]] A dictionary mapping indices in the batch to | required |
device | device | torch.device The device on which to allocate the tensor. | required |
Returns: uniform_rand: torch.Tensor A tensor of shape (num_tokens, ) containing uniform random values in the range [0, 1).
Source code in vllm/v1/sample/rejection_sampler.py
rejection_greedy_sample_kernel ¶
rejection_greedy_sample_kernel(
output_token_ids_ptr,
cu_num_draft_tokens_ptr,
draft_token_ids_ptr,
target_argmax_ptr,
bonus_token_ids_ptr,
is_greedy_ptr,
max_spec_len,
)
Source code in vllm/v1/sample/rejection_sampler.py
rejection_random_sample_kernel ¶
rejection_random_sample_kernel(
output_token_ids_ptr,
cu_num_draft_tokens_ptr,
draft_token_ids_ptr,
draft_probs_ptr,
target_probs_ptr,
bonus_token_ids_ptr,
recovered_token_ids_ptr,
uniform_probs_ptr,
is_greedy_ptr,
max_spec_len,
vocab_size,
NO_DRAFT_PROBS: constexpr,
)
Source code in vllm/v1/sample/rejection_sampler.py
rejection_sample ¶
rejection_sample(
draft_token_ids: Tensor,
num_draft_tokens: list[int],
max_spec_len: int,
cu_num_draft_tokens: Tensor,
draft_probs: Tensor | None,
target_probs: Tensor,
bonus_token_ids: Tensor,
sampling_metadata: SamplingMetadata,
) -> Tensor
Source code in vllm/v1/sample/rejection_sampler.py
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 | |
sample_recovered_tokens ¶
sample_recovered_tokens(
max_spec_len: int,
num_draft_tokens: list[int],
cu_num_draft_tokens: Tensor,
draft_token_ids: Tensor,
draft_probs: Tensor | None,
target_probs: Tensor,
sampling_metadata: SamplingMetadata,
device: device,
) -> Tensor
Source code in vllm/v1/sample/rejection_sampler.py
sample_recovered_tokens_kernel ¶
sample_recovered_tokens_kernel(
output_token_ids_ptr,
cu_num_draft_tokens_ptr,
draft_token_ids_ptr,
draft_probs_ptr,
target_probs_ptr,
q_ptr,
vocab_size,
PADDED_VOCAB_SIZE: constexpr,
NO_DRAFT_PROBS: constexpr,
)