feat: spec decode with draft models (#24322)
Signed-off-by: Tomas Ruiz <tomas.ruiz.te@gmail.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
import os
|
||||
from collections.abc import Callable
|
||||
from dataclasses import replace
|
||||
from typing import TYPE_CHECKING, Any, Literal
|
||||
|
||||
import torch
|
||||
@@ -709,3 +710,6 @@ class ParallelConfig:
|
||||
)
|
||||
|
||||
return self
|
||||
|
||||
def replace(self, **kwargs) -> Self:
|
||||
return replace(self, **kwargs)
|
||||
|
||||
@@ -77,6 +77,9 @@ class SpeculativeConfig:
|
||||
draft_tensor_parallel_size: int | None = Field(default=None, ge=1)
|
||||
"""The degree of the tensor parallelism for the draft model. Can only be 1
|
||||
or the same as the target model's tensor parallel size."""
|
||||
tensor_parallel_size: int | None = None
|
||||
"""Users should pass "draft_tensor_parallel_size". This parameter's purpose is to
|
||||
warn users when they mistakenly provide the wrong argument."""
|
||||
|
||||
# Draft model configuration
|
||||
quantization: me_quant.QuantizationMethods | None = None
|
||||
@@ -397,13 +400,11 @@ class SpeculativeConfig:
|
||||
"one layer. Might need some code changes "
|
||||
"to support multiple layers."
|
||||
)
|
||||
elif self.method == "draft_model":
|
||||
pass
|
||||
else:
|
||||
self.method = "draft_model"
|
||||
raise NotImplementedError(
|
||||
"Speculative decoding with draft model is not "
|
||||
"supported yet. Please consider using other "
|
||||
"speculative decoding methods such as ngram, medusa, "
|
||||
"eagle, or mtp."
|
||||
f"Unsupported speculative method: '{self.method}'"
|
||||
)
|
||||
|
||||
# Replace hf_config for EAGLE draft_model
|
||||
@@ -631,6 +632,12 @@ class SpeculativeConfig:
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _verify_args(self) -> Self:
|
||||
if self.tensor_parallel_size is not None:
|
||||
raise ValueError(
|
||||
"'tensor_parallel_size' is not a valid argument in the "
|
||||
"speculative_config. Please pass 'draft_tensor_parallel_size' instead."
|
||||
)
|
||||
|
||||
if self.num_speculative_tokens is None:
|
||||
raise ValueError(
|
||||
"num_speculative_tokens must be provided with "
|
||||
@@ -669,12 +676,32 @@ class SpeculativeConfig:
|
||||
f"Eagle3 is only supported for {eagle3_target_supported} models. " # noqa: E501
|
||||
f"Got {self.target_model_config.hf_text_config.model_type=}"
|
||||
)
|
||||
|
||||
self.verify_equal_vocab_size_if_draft_model()
|
||||
return self
|
||||
|
||||
def verify_equal_vocab_size_if_draft_model(self):
|
||||
if (
|
||||
self.method == "draft_model"
|
||||
and self.target_model_config is not None
|
||||
and self.draft_model_config is not None
|
||||
):
|
||||
target_vocab_size = self.target_model_config.get_vocab_size()
|
||||
draft_vocab_size = self.draft_model_config.get_vocab_size()
|
||||
if target_vocab_size != draft_vocab_size:
|
||||
raise ValueError(
|
||||
f"Target and draft model should have the same vocabulary size. "
|
||||
f"Target model vocab_size={target_vocab_size}. "
|
||||
f"Draft model vocab_size={draft_vocab_size}. "
|
||||
f"Using models with different tokenizers can cause out-of-bounds "
|
||||
f"errors during speculative decoding."
|
||||
)
|
||||
|
||||
def use_eagle(self) -> bool:
|
||||
return self.method in ("eagle", "eagle3", "mtp")
|
||||
|
||||
def uses_draft_model(self) -> bool:
|
||||
return self.method == "draft_model"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
method = self.method
|
||||
model = None if method in ("ngram", "suffix") else self.draft_model_config.model
|
||||
|
||||
+23
-13
@@ -1214,10 +1214,19 @@ class VllmConfig:
|
||||
compilation_config = self.compilation_config
|
||||
computed_compile_ranges_split_points = []
|
||||
|
||||
# The upper bound of the compile ranges is the max_num_batched_tokens
|
||||
max_num_batched_tokens = self.scheduler_config.max_num_batched_tokens
|
||||
if max_num_batched_tokens is not None:
|
||||
computed_compile_ranges_split_points.append(max_num_batched_tokens)
|
||||
# The upper bound of the compile ranges is the max_num_batched_tokens.
|
||||
# For speculative decoding with draft model, the compile range must be extended
|
||||
# by 1 for each sequence.
|
||||
compile_range_end = self.scheduler_config.max_num_batched_tokens
|
||||
if compile_range_end is not None:
|
||||
do_extend: bool = (
|
||||
self.speculative_config is not None
|
||||
and self.speculative_config.uses_draft_model()
|
||||
)
|
||||
if do_extend:
|
||||
compile_range_end += self.scheduler_config.max_num_seqs
|
||||
|
||||
computed_compile_ranges_split_points.append(compile_range_end)
|
||||
|
||||
# Add the compile ranges for flashinfer
|
||||
if compilation_config.pass_config.fuse_allreduce_rms:
|
||||
@@ -1228,10 +1237,7 @@ class VllmConfig:
|
||||
self.model_config.get_hidden_size()
|
||||
* self.model_config.dtype.itemsize
|
||||
)
|
||||
if (
|
||||
max_num_batched_tokens is not None
|
||||
and max_token_num < max_num_batched_tokens
|
||||
):
|
||||
if compile_range_end is not None and max_token_num < compile_range_end:
|
||||
computed_compile_ranges_split_points.append(max_token_num)
|
||||
else:
|
||||
logger.debug(
|
||||
@@ -1243,11 +1249,7 @@ class VllmConfig:
|
||||
for x in compilation_config.compile_ranges_split_points:
|
||||
assert isinstance(x, int)
|
||||
assert x > 0, f"Invalid compile range split point: {x}"
|
||||
if (
|
||||
max_num_batched_tokens is not None
|
||||
and x < max_num_batched_tokens
|
||||
and x > 1
|
||||
):
|
||||
if compile_range_end is not None and x < compile_range_end and x > 1:
|
||||
computed_compile_ranges_split_points.append(x)
|
||||
compilation_config.compile_ranges_split_points = sorted(
|
||||
computed_compile_ranges_split_points
|
||||
@@ -1316,6 +1318,14 @@ class VllmConfig:
|
||||
path = self.compilation_config.debug_dump_path / append_path
|
||||
return path
|
||||
|
||||
def replace(self, **kwargs):
|
||||
"""
|
||||
Replace attributes of the config, and 'recompute' the config.
|
||||
dataclass.replace() calls __init__() and __post_init__(), source:
|
||||
https://docs.python.org/3/library/dataclasses.html#dataclasses.replace
|
||||
"""
|
||||
return replace(self, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
f"model={self.model_config.model!r}, "
|
||||
|
||||
Reference in New Issue
Block a user