forked from LMCache/LMCache-Ascend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffload.py
More file actions
131 lines (103 loc) · 3.98 KB
/
Copy pathoffload.py
File metadata and controls
131 lines (103 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# SPDX-License-Identifier: Apache-2.0
# Standard
from dataclasses import asdict
import argparse
import contextlib
import os
import time
# Third Party
from vllm import LLM, SamplingParams
from vllm.config import KVTransferConfig
from vllm.engine.arg_utils import EngineArgs
def setup_environment_variables(use_disk: bool = False):
# LMCache-related environment variables
# LMCache is set to use 256 tokens per chunk
os.environ["LMCACHE_CHUNK_SIZE"] = "256"
if use_disk:
# Disable local CPU backend in LMCache
os.environ["LMCACHE_LOCAL_CPU"] = "False"
# Set the maximum size of the local CPU buffer size to 5GB
os.environ["LMCACHE_MAX_LOCAL_CPU_SIZE"] = "5"
# Enable local disk backend in LMCache
os.environ["LMCACHE_LOCAL_DISK"] = "file://local_disk/"
# Set the maximum size of the local disk size to 10GB
os.environ["LMCACHE_MAX_LOCAL_DISK_SIZE"] = "10"
else:
# Enable local CPU backend in LMCache
os.environ["LMCACHE_LOCAL_CPU"] = "True"
# Set the maximum size of the local CPU size to 5GB
os.environ["LMCACHE_MAX_LOCAL_CPU_SIZE"] = "5"
@contextlib.contextmanager
def build_llm_with_lmcache_ascend(model: str):
ktc = KVTransferConfig(
kv_connector="LMCacheAscendConnectorV1Dynamic",
kv_role="kv_both",
kv_connector_module_path="lmcache_ascend.integration.vllm.lmcache_ascend_connector_v1",
)
# Set NPU memory utilization to 0.8 for an Ascend NPU with 40GB
# memory. Reduce the value if your NPU has less memory.
# Note: LMCache supports chunked prefill (see vLLM#14505, LMCache#392).
llm_args = EngineArgs(
enforce_eager=True,
model=model,
kv_transfer_config=ktc,
max_model_len=8000,
gpu_memory_utilization=0.8,
)
llm = LLM(**asdict(llm_args))
try:
yield llm
finally:
# Clean up lmcache backend
# Third Party
from lmcache.integration.vllm.utils import ENGINE_NAME
from lmcache.v1.cache_engine import LMCacheEngineBuilder
LMCacheEngineBuilder.destroy(ENGINE_NAME)
def print_output(
llm: LLM,
prompt: list[str],
sampling_params: SamplingParams,
req_str: str,
):
# Should be able to see logs like the following:
# `LMCache INFO: Storing KV cache for 6006 out of 6006 tokens for request 0`
# This indicates that the KV cache has been stored in LMCache.
start = time.time()
outputs = llm.generate(prompt, sampling_params)
print("-" * 50)
for output in outputs:
generated_text = output.outputs[0].text
print(f"Generated text: {generated_text!r}")
print(f"Generation took {time.time() - start:.2f} seconds, {req_str} request done.")
print("-" * 50)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--use-disk",
action="store_true",
help="Specify whether to use disk as backend (default: False)",
)
return parser.parse_args()
def main():
args = parse_args()
model = "meta-llama/Llama-3.1-8B-Instruct"
setup_environment_variables(args.use_disk)
with build_llm_with_lmcache_ascend(model) as llm:
# This example script runs two requests with a shared prefix.
# Define the shared prompt and specific prompts
shared_prompt = "Hello, how are you?" * 1000
first_prompt = [
shared_prompt + "Hello, my name is",
]
second_prompt = [
shared_prompt + "Tell me a very long story",
]
sampling_params = SamplingParams(temperature=0, top_p=0.95, max_tokens=10)
# Print the first output
print_output(llm, first_prompt, sampling_params, "first")
time.sleep(1)
# print the second output
print_output(llm, second_prompt, sampling_params, "second")
if __name__ == "__main__":
main()