-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun_epicache.py
More file actions
214 lines (174 loc) · 9.04 KB
/
Copy pathrun_epicache.py
File metadata and controls
214 lines (174 loc) · 9.04 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
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
# For licensing see accompanying LICENSE file.
# Copyright (C) 2025 Apple Inc. All Rights Reserved.
import json
import os
from collections import defaultdict
from tqdm import tqdm
import torch
import numpy as np
from args import args
from data import load_dataset_all
from model import LongConvQAModel
from utils import f1_score
from utils.cluster import ClusterManager
from utils.func import TimeStamp
if __name__ == "__main__":
# ========================================================= #
# Load Model
# ========================================================= #
model = LongConvQAModel(args.model, dtype=args.dtype, evict_level=args.level, scoring_method=args.scoring_method)
# Initialize ClusterManager
cluster_manager = ClusterManager(
embedding_type=args.embedding_type,
n_clusters=args.n_cluster,
conv_window=args.conv_window,
medoid_number=args.n_medoid,
verbose=args.verbose
)
if args.scoring_method == "clustering":
# ========================================================= #
# Offline Clustering (Run Once)
# ========================================================= #
print(">>> Running Offline Clustering...")
if args.data == "longmemeval":
data_path = f"data/longmemeval/custom_lme_{args.target_length}_50.json"
with open(data_path, 'r') as f:
dataset = json.load(f)
dataset = dataset['conversations']
else:
data_path = "./data/locomo/locomo10.json" if args.data == "locomo" else "./data/realtalk/realtalk10.json"
with open(data_path, 'r') as f:
dataset = json.load(f)
for conv_idx in tqdm(range(len(dataset)), desc=f"Conversation Clustering...", disable=not args.verbose, leave=False):
conversation = dataset[conv_idx]
# ============================================================== #
# Embed and cluster conversation windows
# ============================================================== #
if args.data == "longmemeval":
conversation_data = conversation
conversation_windows = cluster_manager.extract_conversation_windows_longmemeval(conversation_data)
else:
conversation_data = conversation['conversation'] if args.data == "locomo" else conversation
conversation_windows = cluster_manager.extract_conversation_windows(conversation_data)
embedded_windows = cluster_manager.embed_conversations(conversation_windows, model=model)
clustering_results = cluster_manager.cluster_conversations(embedded_windows)
# Save results for this conversation
conversation_result = {
'conv_idx': conv_idx,
'embedded_windows': embedded_windows,
'clustering_results': clustering_results,
'n_windows': len(embedded_windows)
}
cluster_manager.all_conversation_results.append(conversation_result)
for cluster_id in range(cluster_manager.n_clusters):
cluster_info = clustering_results['cluster_results'][cluster_id]
# ========================================================= #
# Online Evaluation (LLM Inference)
# ========================================================= #
dataset, _ = load_dataset_all(args.data, model.tokenizer, target_length=args.target_length) # list of data
# Store results for each question
all_results = {}
type_scores = defaultdict(list)
total_scores = []
print(">>> Running Online Evaluation...")
for data_idx, data in enumerate(dataset):
# ========================================================= #
# Prefilling (Build Episodic KV Cache)
# ========================================================= #
kvs = []
for cluster_idx in tqdm(range(cluster_manager.n_clusters), desc=f"Prefilling KV Cache for episodes for conv {data_idx}", disable=not args.verbose, leave=False):
# Get clustering results for this conversation
clustering_results = cluster_manager.all_conversation_results[data_idx]['clustering_results']
# Create cluster prompt
combined_text = cluster_manager.make_cluster_prompt(clustering_results['cluster_results'][cluster_idx]['windows'])
# Prefill
model.cluster_ids = model.encode(combined_text)
ctx_ids = model.encode(data['context'])
kv = model.prefill_memory_constrained(ctx_ids, prefill_chunk_size=args.prefill_chunk_size, \
score_path=args.score_path, kv_budget=args.kv_budget, power=args.power)
if args.verbose and cluster_idx == 0 and data_idx == 0:
kv_budget = kv._budget()
print(f">>> Avg Budget: {kv_budget.mean().item()} | Max Budget: {kv_budget.max().item()} | Min Budget: {kv_budget.min().item()} | Memory Usage: {kv._mem()} GB")
# Move kv cache to CPU for memory efficiency
kv.to_cpu()
kvs.append(kv)
model._init_kv()
torch.cuda.empty_cache()
# ========================================================= #
# Decoding Answer (Query-Episode Matching based)
# ========================================================= #
clustering_results = cluster_manager.all_conversation_results[data_idx]['clustering_results']
centroids = [clustering_results['cluster_results'][i]['centroid'] for i in range(cluster_manager.n_clusters)]
for question_idx in tqdm(
range(len(data['question'])),
desc=f"Question answering for episodes for conv {data_idx}",
disable=not args.verbose,
leave=False
):
question = data['question'][question_idx]
answer = data['answers'][question_idx]
question_type = data['task_types'][question_idx]
# 1. Query Embedding
question_embedding = cluster_manager.embed_question(question, model=model)
# 2. Cluster matching
similarities = []
for centroid in centroids:
similarity = float(np.dot(question_embedding, centroid))
similarities.append(similarity)
best_cluster_idx = similarities.index(max(similarities))
# 3. KV retrieval
selected_kv = kvs[best_cluster_idx]
selected_kv.to_gpu(model.device)
if args.data == "realtalk" and args.model == "Qwen/Qwen2.5-3B-Instruct":
question = "\n".join([q for i, q in enumerate(question.split("\n")) if i != 1])
# 4. Generation
pred, num_generated_tokens = model.generate(model.apply_template(question), kv=selected_kv)
score = f1_score(pred, answer)
# Store result for this question
result_key = f"conv_{data_idx}_cluster_{best_cluster_idx}_q_{question_idx}"
all_results[result_key] = {
'conv_idx': data_idx,
'cluster_idx': best_cluster_idx,
'question_idx': question_idx,
'question': question,
'prediction': pred,
'ground_truth': answer,
'f1_score': score,
'type': question_type,
'similarity_to_cluster': max(similarities)
}
# Track scores for type-wise and overall averages
type_scores[question_type].append(score)
total_scores.append(score)
# Calculate average scores
type_averages = {}
for type_name, scores in type_scores.items():
type_averages[type_name] = {
'average_f1': sum(scores) / len(scores),
'count': len(scores)
}
overall_average = sum(total_scores) / len(total_scores) if total_scores else 0
# Combine all results
final_results = {
'individual_results': all_results,
'type_averages': type_averages,
'overall_average': {
'average_f1': overall_average,
'total_count': len(total_scores)
}
}
# Save to JSON file
output_file_name = f"cluster_{args.model.split('/')[-1]}_{args.data}.json"
output_dir = f"results/{args.exp_name}"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_file = os.path.join(output_dir, output_file_name)
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(final_results, f, indent=2, ensure_ascii=False)
print(f"\n=== Evaluation Results ===")
print(f"Overall Average F1: {overall_average:.4f} ({len(total_scores)} questions)")
print(f"Type-wise averages:")
for type_name, stats in type_averages.items():
print(f" {type_name}: {stats['average_f1']:.4f} ({stats['count']} questions)")
print(f"Results saved to: {output_file}")
print("Finished.")