| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from vllm import LLM, SamplingParams |
| 5 | from vllm.config import KVTransferConfig |
| 6 | |
| 7 | |
| 8 | def read_prompts(): |
| 9 | context = "Hi " * 1000 |
| 10 | context2 = "Hey " * 500 |
| 11 | return [ |
| 12 | context + "Hello, my name is", |
| 13 | context + "The capital of France is", |
| 14 | context2 + "Your name is", |
| 15 | context2 + "The capital of China is", |
| 16 | ] |
| 17 | |
| 18 | |
| 19 | def main(): |
| 20 | prompts = read_prompts() |
| 21 | |
| 22 | sampling_params = SamplingParams(temperature=0, top_p=0.95, max_tokens=1) |
| 23 | |
| 24 | llm = LLM( |
| 25 | model="meta-llama/Llama-3.2-1B-Instruct", |
| 26 | enforce_eager=True, |
| 27 | gpu_memory_utilization=0.8, |
| 28 | kv_transfer_config=KVTransferConfig( |
| 29 | kv_connector="ExampleConnector", |
| 30 | kv_role="kv_both", |
| 31 | kv_connector_extra_config={"shared_storage_path": "local_storage"}, |
| 32 | ), |
| 33 | ) # , max_model_len=2048, max_num_batched_tokens=2048) |
| 34 | |
| 35 | # 1ST generation (prefill instance) |
| 36 | outputs = llm.generate( |
| 37 | prompts, |
| 38 | sampling_params, |
| 39 | ) |
| 40 | |
| 41 | new_prompts = [] |
| 42 | print("-" * 30) |
| 43 | for output in outputs: |
| 44 | prompt = output.prompt |
| 45 | generated_text = output.outputs[0].text |
| 46 | new_prompts.append(prompt + generated_text) |
| 47 | print(f"Prompt: {prompt!r}\nGenerated text: {generated_text!r}") |
| 48 | print("-" * 30) |
| 49 | |
| 50 | # Write new_prompts to output.txt |
| 51 | with open("output.txt", "w") as f: |
| 52 | for prompt in new_prompts: |
| 53 | f.write(prompt + "\n") |
| 54 | print(f"Saved {len(new_prompts)} prompts to output.txt") |
| 55 | |
| 56 | |
| 57 | if __name__ == "__main__": |
| 58 | main() |