| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import os |
| 4 | from urllib.request import urlopen |
| 5 | |
| 6 | from vllm import LLM, SamplingParams |
| 7 | |
| 8 | os.environ["VLLM_ALLOW_LONG_MAX_MODEL_LEN"] = "1" |
| 9 | |
| 10 | |
| 11 | def load_prompt() -> str: |
| 12 | # Test cases with various lengths can be found at: |
| 13 | # |
| 14 | # https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-1M/test-data/64k.txt |
| 15 | # https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-1M/test-data/200k.txt |
| 16 | # https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-1M/test-data/600k.txt |
| 17 | # https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-1M/test-data/1m.txt |
| 18 | |
| 19 | with urlopen( |
| 20 | "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2.5-1M/test-data/600k.txt", |
| 21 | timeout=5, |
| 22 | ) as response: |
| 23 | prompt = response.read().decode("utf-8") |
| 24 | return prompt |
| 25 | |
| 26 | |
| 27 | # Processing the prompt. |
| 28 | def process_requests(llm: LLM, prompts: list[str]) -> None: |
| 29 | # Create a sampling params object. |
| 30 | sampling_params = SamplingParams( |
| 31 | temperature=0.7, |
| 32 | top_p=0.8, |
| 33 | top_k=20, |
| 34 | repetition_penalty=1.05, |
| 35 | detokenize=True, |
| 36 | max_tokens=256, |
| 37 | ) |
| 38 | # Generate texts from the prompts. |
| 39 | outputs = llm.generate(prompts, sampling_params) |
| 40 | # Print the outputs. |
| 41 | for output in outputs: |
| 42 | prompt_token_ids = output.prompt_token_ids |
| 43 | generated_text = output.outputs[0].text |
| 44 | print( |
| 45 | f"Prompt length: {len(prompt_token_ids)}, " |
| 46 | f"Generated text: {generated_text!r}" |
| 47 | ) |
| 48 | |
| 49 | |
| 50 | # Create an LLM. |
| 51 | def initialize_engine() -> LLM: |
| 52 | llm = LLM( |
| 53 | model="Qwen/Qwen2.5-7B-Instruct-1M", |
| 54 | max_model_len=1048576, |
| 55 | tensor_parallel_size=4, |
| 56 | enforce_eager=True, |
| 57 | enable_chunked_prefill=True, |
| 58 | max_num_batched_tokens=131072, |
| 59 | ) |
| 60 | return llm |
| 61 | |
| 62 | |
| 63 | def main(): |
| 64 | llm = initialize_engine() |
| 65 | prompt = load_prompt() |
| 66 | process_requests(llm, [prompt]) |
| 67 | |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | main() |