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
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 |
|
forward
¶
forward(
metadata: SpecDecodeMetadata,
draft_probs: Optional[Tensor],
target_logits: Tensor,
bonus_token_ids: Tensor,
sampling_metadata: SamplingMetadata,
) -> Tensor
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 |
target_logits
|
Tensor
|
Target model's logits probability distribution.
Shape is [num_tokens, vocab_size]. Here, probabilities from
different requests are flattened into a single tensor because
this is the shape of the output logits.
NOTE: |
required |
bonus_token_ids_tensor
|
Tensor
|
A tensor containing bonus tokens. Shape is [batch_size, 1]. Bonus tokens are added to the end of the sequence if all proposed tokens are accepted. We generate the bonus tokens outside of the rejection sampler with the default sampling strategy. It allows for more flexibility in the sampling process such as top_p, top_k sampling. |
required |
sampling_metadata
|
SamplingMetadata
|
Additional metadata needed for sampling, such as temperature, top-k/top-p parameters, or other relevant information. |
required |
Returns: output_token_ids (torch.Tensor): A tensor containing the final output token IDs.
Source code in vllm/v1/sample/rejection_sampler.py
parse_output
staticmethod
¶
Parse the output of the rejection sampler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_token_ids
|
Tensor
|
The sampled token IDs in shape
[batch_size, max_spec_len + 1]. The rejected tokens are
replaced with |
required |
vocab_size
|
int
|
The size of the vocabulary. |
required |
Returns:
Type | Description |
---|---|
list[list[int]]
|
A list of lists of token IDs. |
Source code in vllm/v1/sample/rejection_sampler.py
compute_probs
¶
compute_probs(
logits: Tensor,
cu_num_draft_tokens: Tensor,
sampling_metadata: SamplingMetadata,
) -> Tensor
Compute probability distribution from logits based on sampling metadata.
This function applies temperature scaling to the logits and converts them to probabilities using softmax. For greedy decoding, it returns the original logits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logits
|
Tensor
|
Input logits tensor to be converted to probabilities. |
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: Probability distribution (softmax of scaled logits) if non-greedy sampling is used, 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 Total number of tokens. |
required | |
num_draft_tokens
|
List[List[int]] Number of draft tokens per request. |
required | |
generators
|
Optional[Dict[int, torch.Generator]]
A dictionary mapping indices in the batch to
|
required | |
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: Optional[Tensor],
target_probs: Tensor,
bonus_token_ids: Tensor,
sampling_metadata: SamplingMetadata,
) -> Tensor
Source code in vllm/v1/sample/rejection_sampler.py
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 |
|
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: Optional[Tensor],
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,
)