-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathkvcache.py
357 lines (292 loc) · 13.4 KB
/
kvcache.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import torch
import argparse
import os
import cag.dataset as cagds
import cag.similarity as cagsim
from time import time
from transformers import BitsAndBytesConfig, AutoTokenizer, AutoModelForCausalLM
from transformers.cache_utils import DynamicCache
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
from dotenv import load_dotenv
load_dotenv()
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
raise ValueError("HF_TOKEN not found")
"""Hugging Face Llama model"""
global model_name, model, tokenizer
global rand_seed
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
logger.info(f"Using device: {device}")
"""KV Cache test"""
# Allowlist the DynamicCache class
torch.serialization.add_safe_globals([DynamicCache])
torch.serialization.add_safe_globals([set])
def generate(
model,
input_ids: torch.Tensor,
past_key_values,
max_new_tokens: int = 300
) -> torch.Tensor:
"""
Generate text with greedy decoding.
Args:
model: HuggingFace model with automatic device mapping
input_ids: Input token ids
past_key_values: KV Cache for knowledge
max_new_tokens: Maximum new tokens to generate
"""
embed_device = model.model.embed_tokens.weight.device
origin_ids = input_ids
input_ids = input_ids.to(embed_device)
output_ids = input_ids.clone()
next_token = input_ids
with torch.no_grad():
for _ in range(max_new_tokens):
outputs = model(
input_ids=next_token,
past_key_values=past_key_values,
use_cache=True
)
next_token_logits = outputs.logits[:, -1, :]
next_token = next_token_logits.argmax(dim=-1).unsqueeze(-1)
next_token = next_token.to(embed_device)
past_key_values = outputs.past_key_values
output_ids = torch.cat([output_ids, next_token], dim=1)
if next_token.item() in model.config.eos_token_id:
break
return output_ids[:, origin_ids.shape[-1]:]
def preprocess_knowledge(
model,
tokenizer,
prompt: str,
) -> DynamicCache:
"""
Prepare knowledge kv cache for CAG.
Args:
model: HuggingFace model with automatic device mapping
tokenizer: HuggingFace tokenizer
prompt: The knowledge to preprocess, which is basically a prompt
Returns:
DynamicCache: KV Cache
"""
embed_device = model.model.embed_tokens.weight.device
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(embed_device)
past_key_values = DynamicCache()
with torch.no_grad():
outputs = model(
input_ids=input_ids,
past_key_values=past_key_values,
use_cache=True,
output_attentions=False,
output_hidden_states=False
)
return outputs.past_key_values
def write_kv_cache(kv: DynamicCache, path: str):
os.makedirs(os.path.dirname(path), exist_ok=True)
"""
Write the KV Cache to a file.
"""
torch.save(kv, path)
def clean_up(kv: DynamicCache, origin_len: int):
"""
Truncate the KV Cache to the original length.
"""
for i in range(len(kv.key_cache)):
kv.key_cache[i] = kv.key_cache[i][:, :, :origin_len, :]
kv.value_cache[i] = kv.value_cache[i][:, :, :origin_len, :]
def read_kv_cache(path: str) -> DynamicCache | None:
"""
Read the KV Cache from a file. If the cache file is invalid or empty, return None.
"""
if os.path.exists(path) and os.path.getsize(path) > 0:
kv = torch.load(path, weights_only=True)
return kv
else:
# Regenerate cache if it doesn't exist or is too small
return None
def prepare_kvcache(documents, filepath: str = "./data_cache/cache_knowledges.pt", answer_instruction: str | None = None):
# Prepare the knowledges kvcache
if answer_instruction is None:
answer_instruction = "Answer the question with a super short answer."
knowledges = f"""
<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>
You are an assistant for giving short answers based on given context.<|eot_id|>
<|start_header_id|>user<|end_header_id|>
Context information is bellow.
------------------------------------------------
{documents}
------------------------------------------------
{answer_instruction}
Question:
"""
# Get the knowledge cache
t1 = time()
kv = preprocess_knowledge(model, tokenizer, knowledges)
print("kvlen: ", kv.key_cache[0].shape[-2])
write_kv_cache(kv, filepath)
t2 = time()
logger.info(f"KV cache prepared in {t2 - t1:.2f} seconds.")
return kv, t2 - t1
def kvcache_test(args: argparse.Namespace):
answer_instruction = "Answer the question with a super short answer."
text_list, dataset = cagds.get(args.dataset, max_knowledge=args.maxKnowledge, max_paragraph=args.maxParagraph, max_questions=args.maxQuestion)
kvcache_path = "./data_cache/cache_knowledges.pt"
knowledges = '\n\n\n\n\n\n'.join(text_list)
knowledge_cache, prepare_time = prepare_kvcache(knowledges, filepath=kvcache_path, answer_instruction=answer_instruction)
kv_len = knowledge_cache.key_cache[0].shape[-2]
print(f"KVcache prepared in {prepare_time} seconds")
with open(args.output, "a") as f:
f.write(f"KVcache prepared in {prepare_time} seconds\n")
results = {
"cache_time": [],
"generate_time": [],
"similarity": [],
"prompts": [],
"responses": []
}
dataset = list(dataset) # Convert the dataset to a list
max_questions = min(len(dataset), args.maxQuestion) if args.maxQuestion is not None else len(dataset)
# Retrieve the knowledge from the vector database
for id, (question, ground_truth) in enumerate(dataset[:max_questions]):
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
# Read the knowledge cache from the cache file
cache_t1 = time()
# if args.kvcache == "file":
# knowledge_cache = read_kv_cache(kvcache_path)
# Not a good idea to use this method, as it will consume a lot of memory
# if args.kvcache == "variable":
# knowledge_cache = documents_cache
cache_t2 = time()
# Generate Response for the question
knowledges = '\n\n\n'.join(text_list)
if args.usePrompt:
prompt = f"""
<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>
You are an assistant for giving short answers based on given context.<|eot_id|>
<|start_header_id|>user<|end_header_id|>
Context information is bellow.
------------------------------------------------
{knowledges}
------------------------------------------------
{answer_instruction}
Question:
{question}<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
"""
generate_t1 = time()
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
output = generate(model, input_ids, DynamicCache())
generated_text = tokenizer.decode(output[0], skip_special_tokens=True, temperature=None)
generate_t2 = time()
else:
prompt = f"""
{question}<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
"""
generate_t1 = time()
clean_up(knowledge_cache, kv_len)
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
output = generate(model, input_ids, knowledge_cache)
generated_text = tokenizer.decode(output[0], skip_special_tokens=True, temperature=None)
generate_t2 = time()
# print("D: ", knowledges)
print("Q: ", question)
print("A: ", generated_text)
# Evaluate bert-score similarity
similarity = cagsim.bert(generated_text, ground_truth)
print(f"[{id}]: Semantic Similarity: {round(similarity, 5)},",
f"cache time: {cache_t2 - cache_t1},",
f"generate time: {generate_t2 - generate_t1}")
with open(args.output, "a") as f:
f.write(f"[{id}]: Semantic Similarity: {round(similarity, 5)},\t cache time: {cache_t2 - cache_t1},\t generate time: {generate_t2 - generate_t1}\n")
results["prompts"].append(question)
results["responses"].append(generated_text)
results["cache_time"].append(cache_t2 - cache_t1)
results["generate_time"].append(generate_t2 - generate_t1)
results["similarity"].append(similarity)
with open(args.output, "a") as f:
f.write(f"[{id}]: [Cumulative]: "
+ f"Semantic Similarity: {round(sum(results['similarity']) / (len(results['similarity'])) , 5)},"
+ f"\t cache time: {sum(results['cache_time']) / (len(results['cache_time'])) },"
+ f"\t generate time: {sum(results['generate_time']) / (len(results['generate_time'])) }\n")
avg_similarity = sum(results["similarity"]) / len(results["similarity"])
avg_cache_time = sum(results["cache_time"]) / len(results["cache_time"])
avg_generate_time = sum(results["generate_time"]) / len(results["generate_time"])
print()
print(f"Prepare time: {prepare_time}")
print(f"Average Semantic Similarity: {avg_similarity}")
print(f"cache time: {avg_cache_time},\t generate time: {avg_generate_time}")
print()
with open(args.output, "a") as f:
f.write("\n")
f.write(f"Result for {args.output}\n")
f.write(f"Prepare time: {prepare_time}\n")
f.write(f"Average Semantic Similarity: {avg_similarity}\n")
f.write(f"cache time: {avg_cache_time},\t generate time: {avg_generate_time}\n")
# Define quantization configuration
bnb_config = BitsAndBytesConfig(
load_in_4bit=True, # Load model in 4-bit precision
bnb_4bit_quant_type="nf4", # Normalize float 4 quantization
bnb_4bit_compute_dtype=torch.float16, # Compute dtype for 4-bit base matrices
bnb_4bit_use_double_quant=True # Use nested quantization
)
def load_quantized_model(model_name, hf_token=None):
tokenizer = AutoTokenizer.from_pretrained(
model_name,
token=hf_token
)
# Load model with quantization
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=bnb_config,
device_map="auto", # Automatically choose best device
trust_remote_code=True, # Required for some models
token=hf_token
)
return tokenizer, model
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run RAG test with specified parameters.")
# parser.add_argument('--method', choices=['rag', 'kvcache'], required=True, help='Method to use (rag or kvcache)')
# parser.add_argument('--kvcache', choices=['file', 'variable'], required=True, help='Method to use (from_file or from_var)')
parser.add_argument('--modelname', required=False, default="meta-llama/Llama-3.2-1B-Instruct", type=str, help='Model name to use')
parser.add_argument('--quantized', required=False, default=False, type=bool, help='Quantized model')
parser.add_argument('--kvcache', choices=['file'], required=True, help='Method to use (from_file or from_var)')
parser.add_argument('--similarity', choices=['bertscore'], required=True, help='Similarity metric to use (bertscore)')
parser.add_argument('--output', required=True, type=str, help='Output file to save the results')
parser.add_argument('--maxQuestion', required=False, default=None, type=int, help='Maximum number of questions to test')
parser.add_argument('--maxKnowledge', required=False, default=None, type=int, help='Maximum number of knowledge items to use')
parser.add_argument('--maxParagraph', required=False, default=None, type=int, help='Maximum number of paragraph to use')
parser.add_argument('--usePrompt', default=False, action="store_true", help='Do not use cache')
parser.add_argument('--dataset', required=True, help='Dataset to use (kis, kis_sample, squad-dev, squad-train)',
choices=['kis', 'kis_sample',
'squad-dev', 'squad-train',
'hotpotqa-dev', 'hotpotqa-train', 'hotpotqa-test'])
parser.add_argument('--randomSeed', required=False, default=None, type=int, help='Random seed to use')
# 48 Articles, each article average 40~50 paragraph, each average 5~10 questions
args = parser.parse_args()
print("maxKnowledge", args.maxKnowledge, "maxParagraph", args.maxParagraph, "maxQuestion", args.maxQuestion, "randomeSeed", args.randomSeed)
model_name = args.modelname
rand_seed = args.randomSeed if args.randomSeed is not None else None
if args.quantized:
tokenizer, model = load_quantized_model(model_name=model_name, hf_token=HF_TOKEN)
else:
tokenizer = AutoTokenizer.from_pretrained(model_name, token=HF_TOKEN)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto",
token=HF_TOKEN
)
def unique_path(path, i=0):
if os.path.exists(path):
# path = path.split("_")[:-1] if i != 0 else path
return unique_path(path + "_" + str(i), i + 1)
return path
if os.path.exists(args.output):
args.output = unique_path(args.output)
kvcache_test(args)