forked from vllm-project/vllm
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New Feature][Habana main] spec decode PR2 - Medusa, MLP, Eagle (#461)
Spec Decoder PR2 - enable Medusa, MLP This PR is add on to #375 => Do not merge until PR375 merged
- Loading branch information
Showing
11 changed files
with
313 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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,68 @@ | ||
import gc | ||
import time | ||
from typing import List | ||
|
||
from vllm import LLM, SamplingParams | ||
|
||
|
||
def time_generation(llm: LLM, prompts: List[str], | ||
sampling_params: SamplingParams): | ||
# Generate texts from the prompts. The output is a list of RequestOutput | ||
# objects that contain the prompt, generated text, and other information. | ||
# Warmup first | ||
llm.generate(prompts, sampling_params) | ||
llm.generate(prompts, sampling_params) | ||
start = time.time() | ||
outputs = llm.generate(prompts, sampling_params) | ||
end = time.time() | ||
latency_per_token = (end - start) / sum( | ||
[len(o.outputs[0].token_ids) for o in outputs]) | ||
# Print the outputs. | ||
ret = [] | ||
for output in outputs: | ||
generated_text = output.outputs[0].text | ||
ret.append(generated_text) | ||
return ret, latency_per_token | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
prompts = [ | ||
"The future of AI is", | ||
] | ||
sampling_params = SamplingParams(temperature=0.8, | ||
top_p=0.95, | ||
max_tokens=20) | ||
|
||
# Create an LLM without spec decoding | ||
print("==============Without speculation==================") | ||
llm = LLM(model="JackFram/llama-68m") | ||
|
||
ret_non_spec, latency_per_token_non_spec = time_generation( | ||
llm, prompts, sampling_params) | ||
|
||
del llm | ||
gc.collect() | ||
|
||
# Create an LLM with spec decoding | ||
print("==============With speculation=====================") | ||
llm = LLM( | ||
model="JackFram/llama-68m", | ||
speculative_model="abhigoyal/vllm-eagle-llama-68m-random", | ||
num_speculative_tokens=5, | ||
# These are currently required for MLPSpeculator decoding | ||
use_v2_block_manager=True, | ||
) | ||
|
||
ret_spec, latency_per_token_spec = time_generation(llm, prompts, | ||
sampling_params) | ||
|
||
del llm | ||
gc.collect() | ||
print("================= Summary =====================") | ||
print("input is ", prompts, "\n") | ||
print("Non Spec Decode - latency_per_token is ", | ||
latency_per_token_non_spec) | ||
print("Generated Text is :", ret_non_spec, "\n") | ||
print("Spec Decode - latency_per_token is ", latency_per_token_spec) | ||
print("Generated Text is :", ret_spec) |
This file contains 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,67 @@ | ||
import gc | ||
import time | ||
from typing import List | ||
|
||
from vllm import LLM, SamplingParams | ||
|
||
|
||
def time_generation(llm: LLM, prompts: List[str], | ||
sampling_params: SamplingParams): | ||
# Generate texts from the prompts. The output is a list of RequestOutput | ||
# objects that contain the prompt, generated text, and other information. | ||
# Warmup first | ||
llm.generate(prompts, sampling_params) | ||
llm.generate(prompts, sampling_params) | ||
start = time.time() | ||
outputs = llm.generate(prompts, sampling_params) | ||
end = time.time() | ||
latency_per_token = (end - start) / sum( | ||
[len(o.outputs[0].token_ids) for o in outputs]) | ||
# Print the outputs. | ||
ret = [] | ||
for output in outputs: | ||
generated_text = output.outputs[0].text | ||
ret.append(generated_text) | ||
return ret, latency_per_token | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
prompts = [ | ||
"The future of AI is", | ||
] | ||
sampling_params = SamplingParams(temperature=0.8, | ||
top_p=0.95, | ||
max_tokens=20) | ||
|
||
# Create an LLM without spec decoding | ||
print("==============Without speculation==================") | ||
llm = LLM(model="JackFram/llama-68m") | ||
|
||
ret_non_spec, latency_per_token_non_spec = time_generation( | ||
llm, prompts, sampling_params) | ||
|
||
del llm | ||
gc.collect() | ||
|
||
# Create an LLM with spec decoding | ||
print("==============With speculation=====================") | ||
llm = LLM( | ||
model="JackFram/llama-68m", | ||
speculative_model="abhigoyal/vllm-medusa-llama-68m-random", | ||
num_speculative_tokens=5, | ||
use_v2_block_manager=True, | ||
) | ||
|
||
ret_spec, latency_per_token_spec = time_generation(llm, prompts, | ||
sampling_params) | ||
|
||
del llm | ||
gc.collect() | ||
print("================= Summary =====================") | ||
print("input is ", prompts, "\n") | ||
print("Non Spec Decode - latency_per_token is ", | ||
latency_per_token_non_spec) | ||
print("Generated Text is :", ret_non_spec, "\n") | ||
print("Spec Decode - latency_per_token is ", latency_per_token_spec) | ||
print("Generated Text is :", ret_spec) |
This file contains 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 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,62 @@ | ||
from typing import List, Optional | ||
|
||
import torch | ||
|
||
from vllm.logger import init_logger | ||
from vllm.model_executor.layers.sampler import SamplerOutput | ||
from vllm.sequence import IntermediateTensors | ||
from vllm.worker.hpu_model_runner import HPUModelRunner as ModelRunnerBaseCls | ||
from vllm.worker.hpu_model_runner import ModelInputForHPUWithSamplingMetadata | ||
|
||
logger = init_logger(__name__) | ||
|
||
# A flag to enable debug prints for the updated input tensors | ||
# before each step. | ||
debug_advance_input = False | ||
# A flag to allow GPU advance step for draft model runner. | ||
# Set to False for debugging. | ||
allow_gpu_advance_step = True | ||
|
||
|
||
class HPUTP1DraftModelRunner(ModelRunnerBaseCls): | ||
"""Specialized model runner for speculative decoding draft model. | ||
Since the draft model always execute k forward passes consecutively to | ||
generate k speculative tokens in a single speculative decoding step, | ||
we could get rid of most CPU-GPU synchronization and data transfer | ||
overheads by keeping model input and output tensors on GPU all the time. | ||
TODOs: | ||
1. Support TP > 1 (this requires some designs because we do not expect | ||
any broadcasting inside execute_model). | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
if kwargs.get("return_hidden_states"): | ||
raise ValueError( | ||
"return_hidden_states is not supported for TP1DraftModelRunner." | ||
) | ||
|
||
super().__init__(*args, **kwargs) | ||
|
||
self.indices_of_seq_with_bonus_tokens = None | ||
|
||
@torch.inference_mode() | ||
def execute_model( | ||
self, | ||
model_input: ModelInputForHPUWithSamplingMetadata, | ||
kv_caches: List[torch.Tensor], | ||
previous_hidden_states: Optional[torch.Tensor] = None, | ||
intermediate_tensors: Optional[IntermediateTensors] = None, | ||
num_steps: int = 1, | ||
) -> Optional[List[SamplerOutput]]: | ||
if previous_hidden_states is not None: | ||
_, block_size = model_input.input_tokens.shape | ||
previous_hidden_states = previous_hidden_states.expand( | ||
block_size, -1).unsqueeze(0) | ||
return super().execute_model( | ||
model_input=model_input, | ||
kv_caches=kv_caches, | ||
previous_hidden_states=previous_hidden_states, | ||
intermediate_tensors=intermediate_tensors, | ||
num_steps=num_steps, | ||
) |
This file contains 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 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 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 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
Oops, something went wrong.