|
1 | 1 | # SPDX-License-Identifier: Apache-2.0 |
2 | 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
3 | | -""" |
4 | | -This file demonstrates the example usage of cpu offloading |
5 | | -with LMCache in Sonar v1 or v0. |
6 | | -
|
7 | | -Usage: |
8 | | -
|
9 | | - Specify Sonar version |
10 | | -
|
11 | | - -v v0 : Use LMCacheConnector |
12 | | - model = mistralai/Mistral-7B-Instruct-v0.2 |
13 | | - (Includes enable_chunked_prefill = True) |
14 | | -
|
15 | | - -v v1 : Use LMCacheConnectorV1 (default) |
16 | | - model = meta-llama/Meta-Llama-3.1-8B-Instruct |
17 | | - (Without enable_chunked_prefill) |
| 3 | +"""Offload KV cache to CPU with LMCache and reuse a shared prompt prefix. |
18 | 4 |
|
19 | | -Note that `lmcache` is needed to run this example. |
20 | | -Requirements: |
21 | | -https://docs.lmcache.ai/getting_started/installation.html#prerequisites |
22 | | -Learn more about LMCache environment setup, please refer to: |
23 | | -https://docs.lmcache.ai/getting_started/installation.html |
| 5 | +Install dependencies with tools/install_kv_connectors.sh, then run with |
| 6 | +PYTHONHASHSEED=0. |
24 | 7 | """ |
25 | 8 |
|
26 | 9 | import argparse |
27 | 10 | import contextlib |
28 | 11 | import os |
29 | 12 | import time |
30 | | -from dataclasses import asdict |
31 | | - |
32 | | -from lmcache.v1.cache_engine import LMCacheEngineBuilder |
33 | 13 |
|
34 | 14 | from sonar import LLM, SamplingParams |
35 | 15 | from sonar.config import KVTransferConfig |
36 | | -from sonar.distributed.kv_transfer.kv_connector.v1.lmcache_integration.sonar_v1_adapter import ENGINE_NAME |
37 | | -from sonar.engine.arg_utils import EngineArgs |
38 | 16 |
|
39 | 17 |
|
40 | | -def setup_environment_variables(sonar_version: str): |
41 | | - # LMCache-related environment variables |
42 | | - # Use experimental features in LMCache |
43 | | - os.environ["LMCACHE_USE_EXPERIMENTAL"] = "True" |
44 | | - # LMCache is set to use 256 tokens per chunk |
| 18 | +def setup_environment_variables(): |
45 | 19 | os.environ["LMCACHE_CHUNK_SIZE"] = "256" |
46 | | - # Enable local CPU backend in LMCache |
47 | 20 | os.environ["LMCACHE_LOCAL_CPU"] = "True" |
48 | | - # Set local CPU memory limit to 5.0 GB |
49 | 21 | os.environ["LMCACHE_MAX_LOCAL_CPU_SIZE"] = "5.0" |
50 | | - if sonar_version == "v0": |
51 | | - os.environ["SONAR_USE_V1"] = "0" |
52 | 22 |
|
53 | 23 |
|
54 | 24 | @contextlib.contextmanager |
55 | | -def build_llm_with_lmcache(lmcache_connector: str, model: str, sonar_version: str): |
56 | | - ktc = KVTransferConfig( |
57 | | - kv_connector=lmcache_connector, |
58 | | - kv_role="kv_both", |
| 25 | +def build_llm_with_lmcache(model: str): |
| 26 | + llm = LLM( |
| 27 | + model=model, |
| 28 | + kv_transfer_config=KVTransferConfig(kv_connector="LMCacheConnectorV1", kv_role="kv_both"), |
| 29 | + max_model_len=8000, |
| 30 | + gpu_memory_utilization=0.8, |
| 31 | + # Attribute reuse to LMCache rather than Sonar's local prefix cache. |
| 32 | + enable_prefix_caching=False, |
59 | 33 | ) |
60 | | - # Set GPU memory utilization to 0.8 for an A40 GPU with 40GB |
61 | | - # memory. Reduce the value if your GPU has less memory. |
62 | | - # Note: LMCache supports chunked prefill (see Sonar#14505, LMCache#392). |
63 | | - if sonar_version == "v0": |
64 | | - llm_args = EngineArgs( |
65 | | - model=model, |
66 | | - kv_transfer_config=ktc, |
67 | | - max_model_len=8000, |
68 | | - gpu_memory_utilization=0.8, |
69 | | - enable_chunked_prefill=True, # Only in v0 |
70 | | - ) |
71 | | - else: |
72 | | - llm_args = EngineArgs( |
73 | | - model=model, |
74 | | - kv_transfer_config=ktc, |
75 | | - max_model_len=8000, |
76 | | - gpu_memory_utilization=0.8, |
77 | | - ) |
78 | | - |
79 | | - llm = LLM(**asdict(llm_args)) |
80 | 34 | try: |
81 | 35 | yield llm |
82 | 36 | finally: |
83 | | - # Clean up lmcache backend |
84 | | - LMCacheEngineBuilder.destroy(ENGINE_NAME) |
| 37 | + llm.llm_engine.engine_core.shutdown() |
85 | 38 |
|
86 | 39 |
|
87 | | -def print_output( |
88 | | - llm: LLM, |
89 | | - prompt: list[str], |
90 | | - sampling_params: SamplingParams, |
91 | | - req_str: str, |
92 | | -): |
93 | | - # Should be able to see logs like the following: |
94 | | - # `LMCache INFO: Storing KV cache for 6006 out of 6006 tokens for request 0` |
95 | | - # This indicates that the KV cache has been stored in LMCache. |
| 40 | +def print_output(llm: LLM, prompt: list[str], sampling_params: SamplingParams, req_str: str): |
96 | 41 | start = time.time() |
97 | 42 | outputs = llm.generate(prompt, sampling_params) |
98 | | - print("-" * 50) |
99 | 43 | for output in outputs: |
100 | | - generated_text = output.outputs[0].text |
101 | | - print(f"Generated text: {generated_text!r}") |
| 44 | + print(f"Generated text: {output.outputs[0].text!r}") |
| 45 | + print(f"Cached prompt tokens: {output.num_cached_tokens}") |
102 | 46 | print(f"Generation took {time.time() - start:.2f} seconds, {req_str} request done.") |
103 | | - print("-" * 50) |
104 | | - |
105 | | - |
106 | | -def parse_args(): |
107 | | - parser = argparse.ArgumentParser() |
108 | | - parser.add_argument( |
109 | | - "-v", |
110 | | - "--version", |
111 | | - choices=["v0", "v1"], |
112 | | - default="v1", |
113 | | - help="Specify Sonar version (default: v1)", |
114 | | - ) |
115 | | - return parser.parse_args() |
116 | 47 |
|
117 | 48 |
|
118 | 49 | def main(): |
119 | | - args = parse_args() |
120 | | - |
121 | | - if args.version == "v0": |
122 | | - lmcache_connector = "LMCacheConnector" |
123 | | - model = "mistralai/Mistral-7B-Instruct-v0.2" |
124 | | - else: |
125 | | - lmcache_connector = "LMCacheConnectorV1" |
126 | | - model = "meta-llama/Meta-Llama-3.1-8B-Instruct" |
127 | | - |
128 | | - setup_environment_variables(args.version) |
| 50 | + parser = argparse.ArgumentParser() |
| 51 | + parser.add_argument("--model", default="Qwen/Qwen3-0.6B") |
| 52 | + args = parser.parse_args() |
| 53 | + setup_environment_variables() |
129 | 54 |
|
130 | | - with build_llm_with_lmcache(lmcache_connector, model, args.version) as llm: |
131 | | - # This example script runs two requests with a shared prefix. |
132 | | - # Define the shared prompt and specific prompts |
| 55 | + with build_llm_with_lmcache(args.model) as llm: |
133 | 56 | shared_prompt = "Hello, how are you?" * 1000 |
134 | | - first_prompt = [ |
135 | | - shared_prompt + "Hello, my name is", |
136 | | - ] |
137 | | - second_prompt = [ |
138 | | - shared_prompt + "Tell me a very long story", |
139 | | - ] |
140 | | - |
141 | | - sampling_params = SamplingParams(temperature=0, top_p=0.95, max_tokens=10) |
142 | | - |
143 | | - # Print the first output |
144 | | - print_output(llm, first_prompt, sampling_params, "first") |
145 | | - |
146 | | - time.sleep(1) |
147 | | - |
148 | | - # print the second output |
149 | | - print_output(llm, second_prompt, sampling_params, "second") |
| 57 | + sampling_params = SamplingParams(temperature=0, max_tokens=10) |
| 58 | + print_output(llm, [shared_prompt + "Hello, my name is"], sampling_params, "first") |
| 59 | + print_output(llm, [shared_prompt + "Tell me a very long story"], sampling_params, "second") |
150 | 60 |
|
151 | 61 |
|
152 | 62 | if __name__ == "__main__": |
|
0 commit comments