-
-
Notifications
You must be signed in to change notification settings - Fork 10k
[Speculative Decoding] Add speculators
config support
#21345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a5a7cb2
add speculators support
dsikka 2748f01
fix imports
dsikka e223832
update
dsikka 2fe7dd2
updatE
dsikka 7c718ca
make it a lot simpler
dsikka d1e490c
clean-up + simplify spec config
dsikka d46cc42
update
dsikka f305419
use instance
dsikka bbc0a56
update
dsikka 9469974
update
dsikka b1577d2
update
dsikka f9c0a8b
update return types; add smoke test
dsikka b4165bb
Merge branch 'main' into speculators_config
mgoin 802ba3e
Merge branch 'main' into speculators_config
dsikka cb7f2ca
format post rebase
dsikka f246f1c
Merge branch 'main' into speculators_config
dsikka 7f627bb
format
dsikka 2865242
Merge branch 'main' into speculators_config
dsikka 65a93f1
foramt
dsikka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
import pytest | ||
import torch | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"model_path", | ||
[("nm-testing/SpeculatorLlama3-1-8B-Eagle3-converted-0717"), | ||
("nm-testing/SpeculatorLlama3-1-8B-Eagle3-converted-0717-quantized")]) | ||
def test_llama(vllm_runner, example_prompts, model_path): | ||
with vllm_runner(model_path, dtype=torch.bfloat16) as vllm_model: | ||
vllm_outputs = vllm_model.generate_greedy(example_prompts, | ||
max_tokens=20) | ||
print(vllm_outputs) | ||
assert vllm_outputs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
||
SUPPORTED_SPECULATORS_TYPES = {} | ||
|
||
|
||
def register_speculator(name): | ||
|
||
def decorator(fn): | ||
SUPPORTED_SPECULATORS_TYPES[name] = fn | ||
return fn | ||
|
||
return decorator | ||
dsikka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@register_speculator("eagle3") | ||
def update_eagle3(config_dict: dict, vllm_config: dict) -> None: | ||
dsikka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Apply Eagle-3 specific configuration transformations. | ||
|
||
Eagle-3 specific fields: | ||
- draft_vocab_size: Size of the draft model's vocabulary | ||
- target_hidden_size: Hidden size of the target model | ||
- norm_before_residual: Whether to apply norm before residual connection | ||
""" | ||
|
||
vllm_config["draft_vocab_size"] = config_dict.get("draft_vocab_size") | ||
if config_dict.get("target_hidden_size") is not None: | ||
vllm_config["target_hidden_size"] = config_dict["target_hidden_size"] | ||
vllm_config["norm_before_residual"] = config_dict.get( | ||
"norm_before_residual", True) | ||
vllm_config["architectures"] = ["Eagle3LlamaForCausalLM"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
dsikka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
import os | ||
from typing import Any, Union | ||
|
||
from transformers import PretrainedConfig | ||
|
||
from vllm.transformers_utils.configs.speculators.algos import ( | ||
SUPPORTED_SPECULATORS_TYPES) | ||
|
||
__all__ = ["SpeculatorsConfig"] | ||
|
||
|
||
class SpeculatorsConfig(PretrainedConfig): | ||
model_type = "speculators" | ||
|
||
@classmethod | ||
def from_pretrained( | ||
cls, | ||
pretrained_model_name_or_path: Union[str, os.PathLike], | ||
**kwargs, | ||
) -> "SpeculatorsConfig": | ||
"""Load speculators Eagle config and convert to vLLM format.""" | ||
config_dict, _ = cls.get_config_dict(pretrained_model_name_or_path, | ||
**kwargs) | ||
|
||
speculators_model_type = config_dict.get("speculators_model_type") | ||
if speculators_model_type not in SUPPORTED_SPECULATORS_TYPES: | ||
raise ValueError( | ||
f"Expected one of: {SUPPORTED_SPECULATORS_TYPES}. " | ||
"Please ensure you're loading a speculators-format model.") | ||
|
||
# validate fields | ||
# TODO: @dsikka - use speculators pydantic model to validate | ||
cls.validate_speculators_config(config_dict=config_dict) | ||
# Convert from speculators config -> format that can be ingested by vLLM | ||
vllm_config = cls.convert_speculators_to_vllm(config_dict=config_dict) | ||
# Apply anything specific to the supported algorithm | ||
algo_updater = SUPPORTED_SPECULATORS_TYPES[speculators_model_type] | ||
algo_updater(config_dict=config_dict, vllm_config=vllm_config) | ||
return cls(**vllm_config) | ||
|
||
@classmethod | ||
def validate_speculators_config(cls, config_dict: dict[str, Any]) -> None: | ||
try: | ||
spec_config = config_dict["speculators_config"] | ||
methods = spec_config["proposal_methods"] | ||
first_method = methods[0] | ||
_ = first_method["speculative_tokens"] | ||
_ = spec_config["verifier"]["name_or_path"] | ||
_ = config_dict["speculators_model_type"] | ||
except (KeyError, IndexError, TypeError) as e: | ||
raise ValueError("Invalid speculators config structure") from e | ||
|
||
if "transformer_layer_config" not in config_dict: | ||
raise ValueError("Must provide transformer_layer_config") | ||
dsikka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if not isinstance(config_dict["transformer_layer_config"], dict): | ||
raise TypeError( | ||
"'transformer_layer_config' must be a dictionary if provided") | ||
|
||
@classmethod | ||
def convert_speculators_to_vllm( | ||
cls, config_dict: dict[str, Any]) -> dict[str, Any]: | ||
""" | ||
Convert speculators config format to vLLM format. | ||
|
||
This method handles the translation of field names and structure | ||
between speculators and vLLM formats. | ||
|
||
Returns: | ||
Dictionary with vLLM-compatible configuration | ||
""" | ||
# Currently we only support one proposal method | ||
spec_config = config_dict["speculators_config"] | ||
first_method = spec_config.get("proposal_methods")[0] | ||
num_lookahead_tokens = first_method.get("speculative_tokens") | ||
|
||
if num_lookahead_tokens is None: | ||
raise ValueError( | ||
"Missing 'speculative_tokens' in proposal method. " | ||
f"Got: {first_method}") | ||
|
||
# Build base vLLM config | ||
vllm_config = { | ||
"method": config_dict.get("speculators_model_type"), | ||
"num_lookahead_tokens": num_lookahead_tokens, | ||
"target_model": spec_config.get("verifier")["name_or_path"] | ||
} | ||
vllm_config.update(config_dict["transformer_layer_config"]) | ||
return vllm_config |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.