| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import argparse |
| 4 | |
| 5 | from vllm import LLM, SamplingParams |
| 6 | from vllm.config import KVTransferConfig |
| 7 | |
| 8 | |
| 9 | def read_prompts(): |
| 10 | """Read prompts from prefill_output.txt.""" |
| 11 | prompts = [] |
| 12 | try: |
| 13 | with open("prefill_output.txt") as f: |
| 14 | for line in f: |
| 15 | prompts.append(line.strip()) |
| 16 | print(f"Loaded {len(prompts)} prompts from prefill_output.txt") |
| 17 | return prompts |
| 18 | except FileNotFoundError: |
| 19 | print("Error: prefill_output.txt file not found") |
| 20 | exit(-1) |
| 21 | |
| 22 | |
| 23 | def main(): |
| 24 | prompts = read_prompts() |
| 25 | sampling_params = SamplingParams(temperature=0, top_p=0.95, max_tokens=10) |
| 26 | |
| 27 | parser = argparse.ArgumentParser() |
| 28 | parser.add_argument( |
| 29 | "--simulate-failure", action="store_true", help="Simulate KV load failure." |
| 30 | ) |
| 31 | parser.add_argument( |
| 32 | "--async-load", action="store_true", help="Simulate async KV load" |
| 33 | ) |
| 34 | args = parser.parse_args() |
| 35 | |
| 36 | if args.simulate_failure: |
| 37 | ktc = KVTransferConfig( |
| 38 | kv_connector="LoadRecoveryExampleConnector", |
| 39 | kv_role="kv_both", |
| 40 | kv_connector_extra_config={ |
| 41 | "shared_storage_path": "local_storage", |
| 42 | "async_load": args.async_load, |
| 43 | }, |
| 44 | kv_connector_module_path="load_recovery_example_connector", |
| 45 | kv_load_failure_policy="recompute", |
| 46 | ) |
| 47 | out_file = ( |
| 48 | "async_decode_recovered_output.txt" |
| 49 | if args.async_load |
| 50 | else "sync_decode_recovered_output.txt" |
| 51 | ) |
| 52 | else: |
| 53 | ktc = KVTransferConfig( |
| 54 | kv_connector="ExampleConnector", |
| 55 | kv_role="kv_both", |
| 56 | kv_connector_extra_config={ |
| 57 | "shared_storage_path": "local_storage", |
| 58 | }, |
| 59 | ) |
| 60 | out_file = "decode_output.txt" |
| 61 | |
| 62 | llm = LLM( |
| 63 | model="meta-llama/Llama-3.2-1B-Instruct", |
| 64 | enforce_eager=True, |
| 65 | gpu_memory_utilization=0.8, |
| 66 | max_num_batched_tokens=64, |
| 67 | max_num_seqs=16, |
| 68 | kv_transfer_config=ktc, |
| 69 | ) |
| 70 | |
| 71 | outputs = llm.generate(prompts, sampling_params) |
| 72 | |
| 73 | sep_str = "-" * 30 |
| 74 | with open(out_file, "w", encoding="utf-8") as f: |
| 75 | for output in outputs: |
| 76 | prompt = output.prompt |
| 77 | generated_text = output.outputs[0].text |
| 78 | out_str = f"Prompt: {prompt!r}\nGenerated text: {generated_text!r}" |
| 79 | print(out_str) |
| 80 | print(sep_str) |
| 81 | f.write(out_str) |
| 82 | f.write(sep_str) |
| 83 | |
| 84 | |
| 85 | if __name__ == "__main__": |
| 86 | main() |