vllm.spec_decode.spec_decode_worker
SpecDecodeWorker
¶
Bases: LoRANotSupportedWorkerBase
Worker which implements speculative decoding.
Speculative decoding reduces decoding per-token latency by using a proposal method, such as a small draft model, to speculate ahead of a larger LLM. The probabilities of the speculative tokens are then determined by the larger LLM, after which some verification routine determines which (if any) of the speculative tokens are accepted by the larger LLM.
See https://github.com/vllm-project/vllm/pull/2188 and https://github.com/vllm-project/vllm/pull/3103 for more info.
The current implementation has the following limitations: * Only draft-model proposal is implemented (contributions for more forms are welcome!). * Only top-1 proposal and scoring are implemented. Tree-attention is left as future work. * All sequences in a batch must have the same proposal length, or zero. This can be improved by having per-sequence speculation in the future. * The scoring forward pass is done without an MQA kernel, which is suboptimal especially as the batch size, proposal length, and sequence lengths grow. Contributions to add a MQA scoring are welcome once correctness tests pass. More info here https://docs.google.com/document/d/1T-JaS2T1NRfdP51qzqpyakoCXxSXTtORppiwaj5asxA/edit.
Source code in vllm/spec_decode/spec_decode_worker.py
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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 |
|
_allow_zero_draft_token_step
instance-attribute
¶
_enable_lm_head_weight_load
instance-attribute
¶
_metrics
instance-attribute
¶
_metrics = (
AsyncMetricsCollector(spec_decode_sampler)
if metrics_collector is None
else metrics_collector
)
_request_id_seq_id_mapping
instance-attribute
¶
_seq_with_bonus_token_in_last_step
instance-attribute
¶
_vocab_size
cached
property
¶
_vocab_size: int
Get the vocab size of the model and make sure it's consistent between draft and target workers.
disable_by_batch_size
instance-attribute
¶
disable_by_batch_size = disable_by_batch_size or float(
"inf"
)
__init__
¶
__init__(
proposer_worker: ProposerWorkerBase,
scorer_worker: WorkerBase,
spec_decode_sampler: SpecDecodeBaseSampler,
disable_mqa_scorer: bool = False,
disable_logprobs: bool = False,
disable_log_stats: bool = False,
metrics_collector: Optional[
AsyncMetricsCollector
] = None,
disable_by_batch_size: Optional[int] = None,
allow_zero_draft_token_step: Optional[bool] = True,
enable_lm_head_weight_load: Optional[bool] = False,
num_spec_prefill_steps: int = 1,
)
Create a SpecDecodeWorker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proposer_worker
|
ProposerWorkerBase
|
A worker that can produce speculative tokens for sequences. |
required |
scorer_worker
|
WorkerBase
|
A worker that produces probabilities of speculative tokens according to some base model. Typically a vanilla vLLM Worker. |
required |
spec_decode_sampler
|
SpecDecodeBaseSampler
|
A Torch module used to perform acceptance sampling of the draft tokens in the verification step of speculative decoding. Currently we support two different types of sampler namely RejectionSampler and TypicalAcceptanceSampler. 'spec_decode_sampler' is either an instance of RejectionSampler or TypicalAcceptanceSampler. |
required |
disable_mqa_scorer
|
bool
|
If set to True, disable the MQA scorer and use the BatchExpansionTop1Scorer instead. |
False
|
disable_logprobs
|
bool
|
If set to True, token log probabilities will not be output in both the draft worker and the target worker. If set to False, log probabilities will be output by both. |
False
|
disable_log_stats
|
bool
|
If set to True, disable periodic printing of speculative stage times. |
False
|
disable_by_batch_size
|
Optional[int]
|
If the batch size is larger than this, disable speculative decoding for new incoming requests. |
None
|
metrics_collector
|
Optional[AsyncMetricsCollector]
|
Helper class for collecting metrics; can be set for testing purposes. |
None
|
allow_zero_draft_token_step
|
Optional[bool]
|
whether to allow a step where the draft model generates no draft token; should disallow when the tp of draft model is larger than 1 (TODO: #5814) |
True
|
enable_lm_head_weight_load
|
Optional[bool]
|
whether to load lm_head weight for draft models like eagle. |
False
|
num_spec_prefill_steps
|
int
|
number of speculative prefill steps to run before the speculative decoding starts. This is only used when the draft model is a deepseek_mtp model that requires prefill kv cache separately for each MTP layer. |
1
|
Source code in vllm/spec_decode/spec_decode_worker.py
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 |
|
_configure_model_sampler_for_spec_decode
¶
Configure model sampler to emit GPU tensors. This allows spec decode to keep data on device without transferring to CPU and serializing, which significantly reduces overhead of sampling during verification.
NOTE(cade): This breaks abstraction boundaries pretty badly. The better design is to have the "move to CPU and serialize" sampling decision be done outside of the model/sampler; this way the "last-mile" worker object which interfaces with the scheduler can serialize and incur the performance hit as necessary. This allows us to run the worker several iterations in a row without incurring the "move to CPU and serialize" performance penalty.
Since this requires a large change to vLLM, we defer it to later and temporarily accept this broken abstraction boundary.
NOTE(cade): This will require a special check if the proposer worker does not have a sampler (e.g. ngram speculation).
Source code in vllm/spec_decode/spec_decode_worker.py
_create_dummy_logprob_lists
¶
_create_dummy_logprob_lists(
batch_size: int, num_steps: int, num_top_k: int
) -> Tuple[
List[List[int]],
List[List[float]],
List[List[List[Optional[float]]]],
List[List[List[Optional[int]]]],
]
Creates and returns four dummy lists representing token probabilities and their ranks.
This method initializes and returns
- The ranks of the accepted tokens, shaped (num_steps, batch_size)
- The log probabilities of the accepted tokens, shaped (num_steps, batch_size)
- The log probabilities of the top k tokens, shaped (num_steps, batch_size, num_top_k)
- The token IDs of the top k tokens, shaped (num_steps, batch_size, num_top_k)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size
|
int
|
The size of the batch. |
required |
num_steps
|
int
|
The number of steps in the sequence. |
required |
num_top_k
|
int
|
The number of top-k token log probabilities to |
required |
Returns:
Type | Description |
---|---|
Tuple[List[List[int]], List[List[float]], List[List[List[Optional[float]]]], List[List[List[Optional[int]]]]]
|
A tuple containing four dummy lists as described above. |
Source code in vllm/spec_decode/spec_decode_worker.py
_create_logprob_lists_from_tensors
¶
_create_logprob_lists_from_tensors(
target_logprobs_by_step: Tensor,
accepted_token_ids_by_step: Tensor,
num_top_k: int,
) -> Tuple[
List[List[int]],
List[List[float]],
List[List[List[Optional[float]]]],
List[List[List[Optional[int]]]],
]
Creates and returns four lists representing token probabilities and their ranks.
This method initializes and returns four lists containing
- The ranks of the accepted tokens, shaped (num_steps, batch_size)
- The log probabilities of the accepted tokens, shaped (num_steps, batch_size)
- The log probabilities of the top k tokens, shaped (num_steps, batch_size, num_top_k)
- The token IDs of the top k tokens, shaped (num_steps, batch_size, num_top_k)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_logprobs_by_step
|
Tensor
|
Tensor representing the |
required |
accepted_token_ids_by_step
|
Tensor
|
Tensor representing |
required |
num_top_k
|
int
|
The number of top-k token log probabilities to |
required |
Returns:
Type | Description |
---|---|
Tuple[List[List[int]], List[List[float]], List[List[List[Optional[float]]]], List[List[List[Optional[int]]]]]
|
A tuple containing the lists as described above. |
Source code in vllm/spec_decode/spec_decode_worker.py
_create_output_sampler_list
¶
_create_output_sampler_list(
seq_group_metadata_list: List[SequenceGroupMetadata],
accepted_token_ids: Tensor,
target_logprobs: Tensor,
prompt_logprobs: Optional[Tensor],
k: int,
stage_times: Tuple[float, float, float],
) -> List[SamplerOutput]
Given the accepted token ids, create a list of SamplerOutput.
The output is padded with -1 tokens such that each sequence has the same number of outputs.
Source code in vllm/spec_decode/spec_decode_worker.py
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 |
|
_maybe_disable_speculative_tokens
¶
_maybe_disable_speculative_tokens(
disable_all_speculation: bool,
seq_group_metadata_list: List[SequenceGroupMetadata],
) -> None
Source code in vllm/spec_decode/spec_decode_worker.py
_maybe_log_stage_times
¶
_maybe_log_stage_times(
average_time_per_proposal_tok_ms: float,
scoring_time_ms: float,
verification_time_ms: float,
) -> None
Log the speculative stage times. If stat logging is disabled, do nothing.
Source code in vllm/spec_decode/spec_decode_worker.py
_run_no_spec
¶
_run_no_spec(
execute_model_req: ExecuteModelRequest,
skip_proposer: bool,
) -> List[SamplerOutput]
Run a single generation step without any speculation. The input is sent to the proposer and scorer model so that the KV cache is consistent between the two. When skip_proposer is True, the proposer model is not called, meaning that the kv-cache in proposer for requests is not updated, so they cannot enable spec decode in the rest decoding.
Source code in vllm/spec_decode/spec_decode_worker.py
_run_non_driver_rank
¶
_run_non_driver_rank() -> bool
Run proposer and verifier model in non-driver workers. This is used for both speculation cases (num_lookahead_slots>0) and non-speculation cases (e.g. prefill).
Returns True if there are remaining sequences to process.
Source code in vllm/spec_decode/spec_decode_worker.py
_run_speculative_decoding_step
¶
_run_speculative_decoding_step(
execute_model_req: ExecuteModelRequest,
num_lookahead_slots: int,
) -> List[SamplerOutput]
Execute a single step of speculative decoding.
This invokes the proposer worker to get k speculative tokens for each sequence, then scores each speculative token using the scoring worker.
When enable_chunked_prefill
is set, scorer will batch decodes and
prefills, while proposer will sync its KV-cache by running an extra
forward on prefills.
Returns a list of SamplerOutput, each containing a single token per sequence.
Source code in vllm/spec_decode/spec_decode_worker.py
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 |
|
_serialize_sampler_output_no_logprobs
¶
_serialize_sampler_output_no_logprobs(
execute_model_req: ExecuteModelRequest,
sampler_output: SamplerOutput,
) -> List[SamplerOutput]
Creates and returns a SamplerOutput
with only the token IDs being
serialized to CPU and populated in CompletionSequenceGroupOutput
.
All other parameters in CompletionSequenceGroupOutput
related to log
probabilities are skipped.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
execute_model_req
|
ExecuteModelRequest
|
The model request that |
required |
sampler_output
|
SamplerOutput
|
The output from the sampler with |
required |
Returns:
Name | Type | Description |
---|---|---|
SamplerOutput |
List[SamplerOutput]
|
A new |
List[SamplerOutput]
|
|
|
List[SamplerOutput]
|
populated. |
Source code in vllm/spec_decode/spec_decode_worker.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
|
_should_disable_all_speculation
¶
_should_disable_all_speculation(
execute_model_req: ExecuteModelRequest,
) -> bool
Source code in vllm/spec_decode/spec_decode_worker.py
_track_finished_requests
¶
_track_finished_requests(
execute_model_req: ExecuteModelRequest,
)
Removes the finished requests and their associated sequence ids from internal book keeping data structures.
Source code in vllm/spec_decode/spec_decode_worker.py
_track_sequences_with_bonus_tokens
¶
_track_sequences_with_bonus_tokens(
seq_ids: List[int],
request_ids_seq_ids_mapping: Dict[str, Set[int]],
accepted_token_ids_by_step: List[List[int]],
)
Updates the internal data structures which keep track of sequences which have been assigned bonus tokens in their last forward pass.
Source code in vllm/spec_decode/spec_decode_worker.py
_verify_tokens
¶
_verify_tokens(
seq_group_metadata_list: List[SequenceGroupMetadata],
proposal_scores: SpeculativeScores,
proposals: SpeculativeProposals,
max_proposal_len: int,
) -> Tuple[Tensor, Tensor]
Determine which speculative tokens are accepted using the probabilities of each token according to the proposer and scorer models.
Returns a tuple of Tensors, one for the accepted token ids and one for the logprobs according to the scoring model.
Source code in vllm/spec_decode/spec_decode_worker.py
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 |
|
create_worker
classmethod
¶
create_worker(
scorer_worker: WorkerBase,
draft_worker_kwargs: Dict[str, Any],
disable_mqa_scorer: bool,
disable_by_batch_size: Optional[int],
draft_token_acceptance_method: str,
typical_acceptance_sampler_posterior_threshold: float,
typical_acceptance_sampler_posterior_alpha: float,
disable_logprobs: bool,
disable_log_stats: bool,
num_speculative_tokens: int,
) -> SpecDecodeWorker
Source code in vllm/spec_decode/spec_decode_worker.py
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 |
|
determine_num_available_blocks
¶
Determine the number of cache blocks to use.
This is done by profiling the scorer model (which is typically the larger of the two). Then the total memory which would be used by the scorer cache is divided evenly between the proposer and scorer model KV, such that the number of blocks is equal in both KV caches.
Source code in vllm/spec_decode/spec_decode_worker.py
execute_model
¶
execute_model(
execute_model_req: Optional[ExecuteModelRequest] = None,
) -> List[SamplerOutput]
Perform speculative decoding on the input batch.
Source code in vllm/spec_decode/spec_decode_worker.py
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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
|
get_cache_block_size_bytes
¶
Return the size of a cache block in bytes.
This function is only used to compose workers within a SpecDecodeWorker. We leave composing a SpecDecodeWorker within a SpecDecodeWorker undefined for now, although it could be implemented in the future. See https://arxiv.org/abs/2308.04623.
Source code in vllm/spec_decode/spec_decode_worker.py
init_device
¶
Initialize both scorer and proposer models.
Source code in vllm/spec_decode/spec_decode_worker.py
initialize_cache
¶
Initialize the cache engine of the scorer and proposer workers.
Source code in vllm/spec_decode/spec_decode_worker.py
load_model
¶
start_profile
¶
start_worker_execution_loop
¶
Execute model loop to perform speculative decoding in parallel worker.
create_spec_worker
¶
create_spec_worker(*args, **kwargs) -> SpecDecodeWorker
Helper method that is the entrypoint for Executors which use WorkerWrapper. It constructs a SpecDecodeWorker from the speculative config.
Source code in vllm/spec_decode/spec_decode_worker.py
prepare_prefill_hidden_states
¶
prepare_prefill_hidden_states(
prefill_hidden_states: Tensor,
) -> HiddenStates
Source code in vllm/spec_decode/spec_decode_worker.py
split_num_cache_blocks_evenly
¶
split_num_cache_blocks_evenly(
scorer_cache_block_size_bytes: int,
proposer_cache_block_size_bytes: int,
total_num_gpu_blocks: int,
) -> int
Given total_num_gpu_blocks, the number of GPU blocks that could be allocate to the target model, this function calculates how many blocks should be given to the draft and target model.
Note that usually the block size, in bytes, of each model is different, as it's a function of number of KV/layer, number of heads, and hidden dimension size.
Since the target and draft models allocate the same number of blocks, we simply calculate the number of blocks where if allocated by both models, the total memory usage from KV cache is no larger than the number of blocks allocatable by the target model alone.