main
py 71 lines 2.1 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """This file demonstrates the usage of text generation with an LLM model,
4 comparing the performance with and without speculative decoding.
5
6 Note that this example is out of date and not supported in vLLM v1.
7 """
8
9 import gc
10 import time
11
12 from vllm import LLM, SamplingParams
13
14
15 def time_generation(
16 llm: LLM, prompts: list[str], sampling_params: SamplingParams, title: str
17 ):
18 # Generate texts from the prompts. The output is a list of RequestOutput
19 # objects that contain the prompt, generated text, and other information.
20 # Warmup first
21 llm.generate(prompts, sampling_params)
22 llm.generate(prompts, sampling_params)
23 start = time.time()
24 outputs = llm.generate(prompts, sampling_params)
25 end = time.time()
26 print("-" * 50)
27 print(title)
28 print("time: ", (end - start) / sum(len(o.outputs[0].token_ids) for o in outputs))
29 # Print the outputs.
30 for output in outputs:
31 generated_text = output.outputs[0].text
32 print(f"text: {generated_text!r}")
33 print("-" * 50)
34
35
36 def main():
37 template = (
38 "Below is an instruction that describes a task. Write a response "
39 "that appropriately completes the request.\n\n### Instruction:\n{}"
40 "\n\n### Response:\n"
41 )
42
43 # Sample prompts.
44 prompts = [
45 "Write about the president of the United States.",
46 ]
47 prompts = [template.format(prompt) for prompt in prompts]
48 # Create a sampling params object.
49 sampling_params = SamplingParams(temperature=0.0, max_tokens=200)
50
51 # Create an LLM without spec decoding
52 llm = LLM(model="meta-llama/Llama-2-13b-chat-hf")
53
54 time_generation(llm, prompts, sampling_params, "Without speculation")
55
56 del llm
57 gc.collect()
58
59 # Create an LLM with spec decoding
60 llm = LLM(
61 model="meta-llama/Llama-2-13b-chat-hf",
62 speculative_config={
63 "model": "ibm-ai-platform/llama-13b-accelerator",
64 },
65 )
66
67 time_generation(llm, prompts, sampling_params, "With speculation")
68
69
70 if __name__ == "__main__":
71 main()