vllm.spec_decode.batch_expansion
BatchExpansionTop1Scorer
¶
Bases: SpeculativeScorer
Implements a speculative scorer that uses batch expansion to get probabilities of speculative tokens according to the scoring model.
Batch expansion converts a list of sequences and multiple query positions to a new batch of sequences, each with a single query position. This allows for MQA-like scoring in speculative decoding without requiring an MQA kernel.
It is strictly less efficient than MQA scoring.
It only supports scoring the top1 proposal tokens of the proposer, instead of topk/tree.
Source code in vllm/spec_decode/batch_expansion.py
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 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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
_contract_batch
¶
_contract_batch(
contracted_seq_group_metadata_list: List[
SequenceGroupMetadata
],
target_sampler_output: SamplerOutput,
proposals: SpeculativeProposals,
num_scoring_tokens: int,
non_spec_indices: List[int],
spec_indices: List[int],
k: int,
) -> SpeculativeScores
Contract the expanded batch back into its original size. This maps the scores of speculative tokens back to their original sequences.
contracted_bs is the original batch size, and the batch size that the target_sampler_output will be contracted to.
Source code in vllm/spec_decode/batch_expansion.py
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 |
|
_contract_batch_all_spec
¶
_contract_batch_all_spec(
target_sampler_output: SamplerOutput,
proposals: SpeculativeProposals,
) -> SpeculativeScores
Contract the expanded batch back into its original size. This maps the scores of speculative tokens back to their original sequences.
It assumes all sequences in the batch were previously expanded.
Source code in vllm/spec_decode/batch_expansion.py
_contract_non_speculative
¶
_contract_non_speculative(
scores: SpeculativeScores,
seq_group_metadata_list: List[SequenceGroupMetadata],
non_spec_indices: List[int],
non_spec_outputs: SpeculativeScores,
has_prompt_log: bool,
) -> SpeculativeScores
Augment input scores
with non-speculative requests outputs.
This includes decode requests with speculation turned off, as well
as prefill requests when enable_chunked_prefill
is set.
For the latter, prefills are further separated into terminal and
non-terminal chunks (from which no token is sampled).
Source code in vllm/spec_decode/batch_expansion.py
_create_scoring_model_input
¶
_create_scoring_model_input(
seq_group_metadata_list: List[SequenceGroupMetadata],
proposal_token_ids: List[List[TokenId]],
target_seq_ids_iter: Iterator[TargetSeqId],
) -> List[SequenceGroupMetadata]
Given the original input sequences and proposed tokens from the draft model, create a list of target sequences that can be used for scoring.
target_seq_ids_iter provides sequence ids for the expanded batch, fulfilling the requirement that no seq id in the expanded batch is equal to the seq id in the original batch.
Source code in vllm/spec_decode/batch_expansion.py
_create_single_target_seq_group_metadata
staticmethod
¶
_create_single_target_seq_group_metadata(
seq_group_metadata: SequenceGroupMetadata,
seq_id: SeqId,
target_seq_id: TargetSeqId,
token_ids: List[TokenId],
sampling_params: SamplingParams,
) -> SequenceGroupMetadata
Create a single target SequenceGroupMetadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq_group_metadata
|
SequenceGroupMetadata
|
The metadata for the input sequence. |
required |
seq_id
|
SeqId
|
The input sequence ID. |
required |
target_seq_id
|
TargetSeqId
|
The corresponding target sequence ID. |
required |
token_ids
|
List[TokenId]
|
The list of token ids that are to be appended to the input sequence. |
required |
Source code in vllm/spec_decode/batch_expansion.py
_create_target_seq_group_metadata
¶
_create_target_seq_group_metadata(
input_seq_group_metadata: SequenceGroupMetadata,
proposal_token_ids: List[List[TokenId]],
batch_index: int,
target_seq_ids_iter: Iterator[TargetSeqId],
) -> List[SequenceGroupMetadata]
Given an input sequence group metadata and a list of draft tokens, create a list of target SequenceGroupMetadata, one for each token id that needs to be scored.
Naive speculative decoding requires K target model scores, one for each draft model token. However one can add a bonus token such that if each token is accepted, then a final token may be sampled from the model. This function creates K+1 target SequenceGroupMetadata to take advantage of the bonus token.
Source code in vllm/spec_decode/batch_expansion.py
_create_target_seq_id_iterator
staticmethod
¶
_create_target_seq_id_iterator(
seq_ids: List[SeqId],
) -> Iterator[TargetSeqId]
Create an iterator for creating target sequence ids. Target sequence ids are distinct from sequence ids because we create a distinct target sequence id for each proposal token to be scored.
This implementation increments a counter starting at 1 + max of all provided input sequence ids.
Source code in vllm/spec_decode/batch_expansion.py
_expand_batch
¶
_expand_batch(
seq_group_metadata_list: List[SequenceGroupMetadata],
proposal_token_ids_list: List[List[TokenId]],
proposal_lens_list: List[int],
) -> Tuple[
List[int], List[int], List[SequenceGroupMetadata], int
]
Given the input sequences and potentially multiple corresponding proposal tokens, create a new batch where each sequence has a single query token.
Source code in vllm/spec_decode/batch_expansion.py
_get_token_ids_to_score
staticmethod
¶
Given an int tensor of proposal token ids, return a list of token ids that should be scored.
Returns k+1 output lists. The additional one is used for generating the bonus token.
Example
Input: [0, 1, 2, 3] (k=4) Output: (k+1 lists) [] [0] [0, 1] [0, 1, 2] [0, 1, 2, 3]
Source code in vllm/spec_decode/batch_expansion.py
_split_scoring_output
staticmethod
¶
_split_scoring_output(
sampler_output: SamplerOutput, num_scoring_tokens: int
) -> Tuple[
Tensor,
Tensor,
Tensor,
Optional[Tensor],
Tensor,
Tensor,
Tensor,
Optional[Tensor],
]
Split the target model output into speculative and non-speculative output.
Source code in vllm/spec_decode/batch_expansion.py
score_proposals
¶
score_proposals(
execute_model_req: ExecuteModelRequest,
proposals: SpeculativeProposals,
) -> SpeculativeScores
Score the proposed tokens via the scorer model.
This converts each input sequence to a set of k+1 target sequences. The target sequences have the unique continuations to be scored and a unique sequence ID that is different from all input sequence ids.
If a speculative sequence length would exceed the max model length, then no speculation is produced for that sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
execute_model_req
|
ExecuteModelRequest
|
The execution request. |
required |
proposals
|
SpeculativeProposals
|
The speculative proposals to score. |
required |
Returns: SpeculativeScores: The scores of each speculative token, along with which sequences were ignored during scoring.