-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_kvpress_native.py
More file actions
491 lines (407 loc) · 16.3 KB
/
Copy pathbench_kvpress_native.py
File metadata and controls
491 lines (407 loc) · 16.3 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/env python3
"""
kvpress KV-Cache压缩方法基准测试
=================================
使用transformers原生模型测试kvpress库的各种KV-cache压缩方法:
- SnapKV: 基于attention窗口的稀疏化
- StreamingLLM: Sink + Recent窗口
- ExpectedAttentionPress: 基于期望attention的压缩
- KnormPress: 基于K范数的压缩
- RandomPress: 随机采样 (baseline)
测试维度:
- 不同batch size
- 不同压缩方法
- 吞吐量和延迟对比
"""
from fastcache_paths import ensure_sys_paths, CKPT_DIR, DATASETS_DIR, RESULTS_DIR
ensure_sys_paths()
import os
import sys
import torch
import gc
import time
import json
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass
from datetime import datetime
import warnings
warnings.filterwarnings('ignore')
def clear_gpu():
"""清理GPU内存"""
gc.collect()
torch.cuda.empty_cache()
if torch.cuda.is_available():
torch.cuda.synchronize()
def get_gpu_memory():
"""获取GPU显存使用情况"""
if torch.cuda.is_available():
return torch.cuda.memory_allocated() / 1024**3, torch.cuda.max_memory_allocated() / 1024**3
return 0, 0
@dataclass
class BenchResult:
"""基准测试结果"""
model_name: str
method: str
compression_ratio: float
batch_size: int
input_len: int
output_len: int
# 性能指标
prefill_time_ms: float
compress_time_ms: float
decode_time_ms: float
total_time_ms: float
# 吞吐量
throughput_tok_s: float
prefill_throughput: float
decode_throughput: float
# 内存
memory_peak_gb: float
# 可选
error: Optional[str] = None
class KVPressEngine:
"""
基于transformers + kvpress的推理引擎
kvpress通过forward hook来实现KV-cache压缩,
在每层attention的forward后自动压缩KV-cache。
"""
def __init__(
self,
model_path: str,
device: str = "cuda",
dtype: torch.dtype = torch.float16,
):
self.device = device
self.dtype = dtype
self.model_path = model_path
# 加载模型和tokenizer
print(f"Loading model from {model_path}...")
from transformers import AutoModelForCausalLM, AutoTokenizer
self.tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
self.model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=dtype,
device_map=device,
trust_remote_code=True,
attn_implementation="flash_attention_2" if torch.cuda.is_available() else None,
)
self.model.eval()
# 获取模型配置
self.config = self.model.config
self.num_layers = self.config.num_hidden_layers
self.num_heads = self.config.num_attention_heads
self.num_kv_heads = getattr(self.config, 'num_key_value_heads', self.num_heads)
self.head_dim = self.config.hidden_size // self.num_heads
print(f"Model loaded:")
print(f" - Layers: {self.num_layers}")
print(f" - Heads: {self.num_heads}")
print(f" - KV Heads: {self.num_kv_heads}")
print(f" - Head Dim: {self.head_dim}")
print(f" - Dtype: {dtype}")
def _get_press(self, method: str, compression_ratio: float):
"""获取kvpress压缩器"""
import kvpress
# 压缩比 = 保留的比例,kvpress的compression_ratio是保留比例
# 为SnapKV等需要window的方法使用较小的window_size
press_map = {
'snapkv': lambda: kvpress.SnapKVPress(compression_ratio=compression_ratio, window_size=32),
'streaming_llm': lambda: kvpress.StreamingLLMPress(compression_ratio=compression_ratio),
'expected_attention': lambda: kvpress.ExpectedAttentionPress(compression_ratio=compression_ratio),
'knorm': lambda: kvpress.KnormPress(compression_ratio=compression_ratio),
'random': lambda: kvpress.RandomPress(compression_ratio=compression_ratio),
'observed_attention': lambda: kvpress.ObservedAttentionPress(compression_ratio=compression_ratio),
'tova': lambda: kvpress.TOVAPress(compression_ratio=compression_ratio),
'think': lambda: kvpress.ThinKPress(compression_ratio=compression_ratio),
'simlayer': lambda: kvpress.SimLayerKVPress(compression_ratio=compression_ratio),
}
if method not in press_map:
raise ValueError(f"Unknown compression method: {method}. Available: {list(press_map.keys())}")
return press_map[method]()
@torch.no_grad()
def generate(
self,
prompts: List[str],
max_new_tokens: int = 64,
compress_method: Optional[str] = None,
compression_ratio: float = 0.5,
temperature: float = 0.0,
) -> Tuple[List[str], Dict[str, float]]:
"""
生成文本
Args:
prompts: 输入prompts
max_new_tokens: 最大生成token数
compress_method: 压缩方法名 (None表示不压缩)
compression_ratio: 压缩比例 (0-1, 保留的比例)
temperature: 采样温度
Returns:
(generated_texts, timing_info)
"""
batch_size = len(prompts)
# Tokenize
encodings = self.tokenizer(
prompts,
return_tensors="pt",
padding=True,
truncation=True,
max_length=4096 - max_new_tokens,
)
input_ids = encodings.input_ids.to(self.device)
attention_mask = encodings.attention_mask.to(self.device)
input_len = input_ids.shape[1]
# 使用kvpress压缩
if compress_method is not None:
press = self._get_press(compress_method, compression_ratio)
# kvpress使用context manager来应用压缩
# press(model)会在attention层注册forward hook
torch.cuda.synchronize()
start_time = time.time()
with press(self.model):
outputs = self.model.generate(
input_ids,
attention_mask=attention_mask,
max_new_tokens=max_new_tokens,
do_sample=temperature > 0,
temperature=temperature if temperature > 0 else None,
pad_token_id=self.tokenizer.pad_token_id,
use_cache=True,
)
torch.cuda.synchronize()
total_time = time.time() - start_time
# kvpress在forward hook中压缩,无法精确分离prefill和compress时间
# 但压缩主要发生在prefill阶段
prefill_time = total_time * 0.4 # 估算(包含压缩)
compress_time = total_time * 0.1 # 估算
decode_time = total_time * 0.5 # 估算
else:
# 无压缩
torch.cuda.synchronize()
start_time = time.time()
outputs = self.model.generate(
input_ids,
attention_mask=attention_mask,
max_new_tokens=max_new_tokens,
do_sample=temperature > 0,
temperature=temperature if temperature > 0 else None,
pad_token_id=self.tokenizer.pad_token_id,
use_cache=True,
)
torch.cuda.synchronize()
total_time = time.time() - start_time
prefill_time = total_time * 0.3 # 估算
compress_time = 0
decode_time = total_time * 0.7
# 计算实际生成的token数
generated_tokens = outputs.shape[1] - input_len
total_tokens = input_len * batch_size + generated_tokens * batch_size
# Decode texts
texts = self.tokenizer.batch_decode(
outputs[:, input_len:],
skip_special_tokens=True
)
timing = {
'prefill_time': prefill_time,
'compress_time': compress_time,
'decode_time': decode_time,
'total_time': total_time,
'input_tokens': input_len * batch_size,
'output_tokens': generated_tokens * batch_size,
'total_tokens': total_tokens,
'throughput': total_tokens / total_time,
}
return texts, timing
def run_benchmark(
model_path: str,
batch_sizes: List[int],
input_len: int = 512,
output_len: int = 64,
methods: List[str] = None,
compression_ratio: float = 0.5,
) -> List[BenchResult]:
"""运行基准测试"""
if methods is None:
methods = ['none', 'snapkv', 'streaming_llm', 'knorm']
results = []
# 加载模型
clear_gpu()
engine = KVPressEngine(model_path, dtype=torch.bfloat16)
model_name = os.path.basename(model_path)
# 生成测试prompts
base_prompt = "Please explain the concept of machine learning in great detail, covering its history, applications, and future developments. "
for batch_size in batch_sizes:
prompts = [f"{base_prompt} This is prompt {i}." for i in range(batch_size)]
for method in methods:
print(f"\n{'='*60}")
print(f"Testing: batch_size={batch_size}, method={method}")
print(f"{'='*60}")
try:
# Warmup
if batch_size <= 4:
_ = engine.generate(
prompts[:min(2, batch_size)],
max_new_tokens=8,
compress_method=method if method != 'none' else None,
compression_ratio=compression_ratio,
)
clear_gpu()
# 实际测试
torch.cuda.reset_peak_memory_stats()
texts, timing = engine.generate(
prompts,
max_new_tokens=output_len,
compress_method=method if method != 'none' else None,
compression_ratio=compression_ratio,
)
_, mem_peak = get_gpu_memory()
result = BenchResult(
model_name=model_name,
method=method,
compression_ratio=compression_ratio if method != 'none' else 1.0,
batch_size=batch_size,
input_len=input_len,
output_len=output_len,
prefill_time_ms=timing['prefill_time'] * 1000,
compress_time_ms=timing['compress_time'] * 1000,
decode_time_ms=timing['decode_time'] * 1000,
total_time_ms=timing['total_time'] * 1000,
throughput_tok_s=timing['throughput'],
prefill_throughput=timing['input_tokens'] / timing['prefill_time'] if timing['prefill_time'] > 0 else 0,
decode_throughput=timing['output_tokens'] / timing['decode_time'] if timing['decode_time'] > 0 else 0,
memory_peak_gb=mem_peak,
)
results.append(result)
print(f" Throughput: {result.throughput_tok_s:.1f} tok/s")
print(f" Total time: {result.total_time_ms:.1f} ms")
print(f" Memory peak: {result.memory_peak_gb:.2f} GB")
except Exception as e:
print(f" Error: {e}")
import traceback
traceback.print_exc()
results.append(BenchResult(
model_name=model_name,
method=method,
compression_ratio=compression_ratio if method != 'none' else 1.0,
batch_size=batch_size,
input_len=input_len,
output_len=output_len,
prefill_time_ms=0,
compress_time_ms=0,
decode_time_ms=0,
total_time_ms=0,
throughput_tok_s=0,
prefill_throughput=0,
decode_throughput=0,
memory_peak_gb=0,
error=str(e),
))
clear_gpu()
return results
def print_results_table(results: List[BenchResult]):
"""打印结果表格"""
print("\n" + "="*120)
print(" BENCHMARK RESULTS SUMMARY")
print("="*120)
# 按batch_size和method分组
by_batch = {}
for r in results:
if r.error:
continue
if r.batch_size not in by_batch:
by_batch[r.batch_size] = {}
by_batch[r.batch_size][r.method] = r
# 获取所有方法
all_methods = sorted(set(r.method for r in results if not r.error))
# 打印表头
header = f"{'Batch':>6} |"
for method in all_methods:
header += f" {method:>15} |"
print(header)
print("-"*len(header))
# 打印每行
for batch in sorted(by_batch.keys()):
methods = by_batch[batch]
row = f"{batch:>6} |"
baseline = methods.get('none')
for method in all_methods:
r = methods.get(method)
if r:
tp = r.throughput_tok_s
if method != 'none' and baseline:
speedup = tp / baseline.throughput_tok_s
row += f" {tp:>8.0f} ({speedup:.2f}x) |"
else:
row += f" {tp:>15.0f} |"
else:
row += f" {'N/A':>15} |"
print(row)
def save_results(results: List[BenchResult], filename: str):
"""保存结果"""
data = []
for r in results:
data.append({
'model_name': r.model_name,
'method': r.method,
'compression_ratio': r.compression_ratio,
'batch_size': r.batch_size,
'input_len': r.input_len,
'output_len': r.output_len,
'prefill_time_ms': r.prefill_time_ms,
'compress_time_ms': r.compress_time_ms,
'decode_time_ms': r.decode_time_ms,
'total_time_ms': r.total_time_ms,
'throughput_tok_s': r.throughput_tok_s,
'prefill_throughput': r.prefill_throughput,
'decode_throughput': r.decode_throughput,
'memory_peak_gb': r.memory_peak_gb,
'error': r.error,
})
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
print(f"\nResults saved to {filename}")
def main():
import argparse
parser = argparse.ArgumentParser(description='kvpress KV-Cache Compression Benchmark')
parser.add_argument('--model', default='/data/huggingface/Llama-3.1-8B',
help='Model path')
parser.add_argument('--batch-sizes', type=int, nargs='+',
default=[1, 4, 8, 16],
help='Batch sizes to test')
parser.add_argument('--input-len', type=int, default=512,
help='Input sequence length')
parser.add_argument('--output-len', type=int, default=64,
help='Output sequence length')
parser.add_argument('--methods', type=str, nargs='+',
default=['none', 'snapkv', 'streaming_llm', 'knorm'],
help='Compression methods to test')
parser.add_argument('--compression-ratio', type=float, default=0.8,
help='Compression ratio (fraction of tokens to REMOVE, 0.8 means keep 20%)')
parser.add_argument('--output', type=str, default=None,
help='Output JSON file')
args = parser.parse_args()
print("#" * 80)
print(" kvpress KV-Cache Compression Benchmark")
print("#" * 80)
print(f"\nModel: {args.model}")
print(f"Batch sizes: {args.batch_sizes}")
print(f"Input length: {args.input_len}")
print(f"Output length: {args.output_len}")
print(f"Methods: {args.methods}")
print(f"Compression ratio: {args.compression_ratio}")
results = run_benchmark(
model_path=args.model,
batch_sizes=args.batch_sizes,
input_len=args.input_len,
output_len=args.output_len,
methods=args.methods,
compression_ratio=args.compression_ratio,
)
print_results_table(results)
if args.output:
save_results(results, args.output)
else:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
save_results(results, f"kvpress_bench_{timestamp}.json")
if __name__ == '__main__':
main()