main
py 130 lines 4.1 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """This file demonstrates the example usage of remote KV cache sharing
4 with LMCache.
5 We will launch 2 vllm instances, and launch an additional LMCache server.
6 KV cache is transferred in the following manner:
7 (1) vLLM instance 1 -> LMCache server (KV cache store).
8 (2) LMCache server -> vLLM instance 2 (KV cache reuse/retrieve).
9
10 Note that lmcache needs to be installed to run this example.
11 Learn more about LMCache in https://github.com/LMCache/LMCache.
12 """
13
14 import os
15 import subprocess
16 import time
17 from multiprocessing import Event, Process
18
19 from lmcache.integration.vllm.utils import ENGINE_NAME
20 from lmcache.v1.cache_engine import LMCacheEngineBuilder
21
22 from vllm import LLM, SamplingParams
23 from vllm.config import KVTransferConfig
24
25 # LMCache-related environment variables
26 # The port to start LMCache server
27 port = 8100
28 # LMCache is set to use 256 tokens per chunk
29 os.environ["LMCACHE_CHUNK_SIZE"] = "256"
30 # Disable local CPU backend in LMCache
31 os.environ["LMCACHE_LOCAL_CPU"] = "False"
32 # Set local CPU memory buffer limit to 5.0 GB
33 os.environ["LMCACHE_MAX_LOCAL_CPU_SIZE"] = "5.0"
34 # Set the remote URL for LMCache server
35 os.environ["LMCACHE_REMOTE_URL"] = f"lm://localhost:{port}"
36 # Set the serializer/deserializer between vllm and LMCache server
37 # `naive` indicates using raw bytes of the tensor without any compression
38 os.environ["LMCACHE_REMOTE_SERDE"] = "naive"
39
40 prompts = [
41 "Hello, how are you?" * 1000,
42 ]
43
44
45 def run_store(store_done, prompts):
46 # We use GPU 0 for KV cache store process.
47 os.environ["CUDA_VISIBLE_DEVICES"] = "0"
48
49 sampling_params = SamplingParams(temperature=0, top_p=0.95, max_tokens=10)
50
51 ktc = KVTransferConfig(kv_connector="LMCacheConnectorV1", kv_role="kv_both")
52 # Set GPU memory utilization to 0.8 for an A40 GPU with 40GB
53 # memory. Reduce the value if your GPU has less memory.
54 llm = LLM(
55 model="mistralai/Mistral-7B-Instruct-v0.2",
56 kv_transfer_config=ktc,
57 max_model_len=8000,
58 gpu_memory_utilization=0.8,
59 enforce_eager=True,
60 )
61
62 outputs = llm.generate(prompts, sampling_params)
63 for output in outputs:
64 generated_text = output.outputs[0].text
65 print(f"Generated text: {generated_text!r}")
66 print("KV cache store is finished.")
67 store_done.set()
68
69 # Clean up lmcache backend
70 LMCacheEngineBuilder.destroy(ENGINE_NAME)
71
72
73 def run_retrieve(store_done, prompts, timeout=1):
74 # We use GPU 1 for KV cache retrieve process.
75 os.environ["CUDA_VISIBLE_DEVICES"] = "1"
76
77 sampling_params = SamplingParams(temperature=0, top_p=0.95, max_tokens=10)
78
79 ktc = KVTransferConfig(kv_connector="LMCacheConnectorV1", kv_role="kv_both")
80 # Set GPU memory utilization to 0.8 for an A40 GPU with 40GB
81 # of memory. Reduce the value if your GPU has less memory.
82 llm = LLM(
83 model="mistralai/Mistral-7B-Instruct-v0.2",
84 kv_transfer_config=ktc,
85 max_model_len=8000,
86 gpu_memory_utilization=0.8,
87 enforce_eager=True,
88 )
89
90 print("Waiting for KV cache store to finish...")
91 store_done.wait()
92 time.sleep(timeout)
93
94 outputs = llm.generate(prompts, sampling_params)
95 for output in outputs:
96 generated_text = output.outputs[0].text
97 print(f"Generated text: {generated_text!r}")
98
99 # Clean up lmcache backend
100 LMCacheEngineBuilder.destroy(ENGINE_NAME)
101
102
103 def run_lmcache_server(port):
104 server_proc = subprocess.Popen(
105 ["python", "-m", "lmcache.v1.server", "localhost", str(port)]
106 )
107 return server_proc
108
109
110 def main():
111 store_done = Event()
112 store_process = Process(target=run_store, args=(store_done, prompts))
113 retrieve_process = Process(target=run_retrieve, args=(store_done, prompts))
114 lmcache_server_process = run_lmcache_server(port)
115
116 # Start KV cache store process
117 store_process.start()
118
119 # Start KV cache retrieve process
120 retrieve_process.start()
121
122 # Clean up the processes
123 store_process.join()
124 retrieve_process.terminate()
125 lmcache_server_process.terminate()
126 lmcache_server_process.wait()
127
128
129 if __name__ == "__main__":
130 main()