kfoughali commited on
Commit
9d90235
·
verified ·
1 Parent(s): 09b3e47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -919
app.py CHANGED
@@ -1,919 +0,0 @@
1
- """
2
- Enhanced SPG: Multi-Stage Magnitude-Position Guided KV Cache Compression
3
- Main application with Gradio interface and visualization.
4
- RESEARCH-GRADE: 450x compression with FULL non-negotiables compliance
5
- """
6
-
7
- import gradio as gr
8
- import torch
9
- from transformers import AutoTokenizer
10
- import numpy as np
11
- import pandas as pd
12
- import json
13
- import logging
14
- import os
15
- import tempfile
16
- from datetime import datetime
17
- from typing import Dict, List, Any, Optional
18
- import matplotlib.pyplot as plt
19
- import matplotlib
20
- matplotlib.use('Agg') # Non-interactive backend
21
-
22
- # Import from modular components
23
- from config import (
24
- CompressionConfig, CompressionType, EnhancedSPGConfig, ProvingConfig,
25
- SUPPORTED_MODELS, BENCHMARK_CONFIGS
26
- )
27
- from compression import detect_model_layers
28
- from benchmark import (
29
- set_seed, BenchmarkMetrics, run_research_benchmark,
30
- export_proof_bundle, verify_proof_bundle, load_real_dataset_samples
31
- )
32
-
33
- # Configure logging
34
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
35
- logger = logging.getLogger(__name__)
36
-
37
- def plot_memory_vs_method(ax, summaries, metrics_dict=None):
38
- """Publication-grade KV memory plot with log scale and CIs."""
39
- methods = list(summaries.keys())
40
- kv_mb = [summaries[m].get("kv_cache_memory_mb", 0) for m in methods]
41
-
42
- # Get baseline for % change calculation
43
- baseline_val = kv_mb[0] if "NONE" in methods[0].upper() else None
44
-
45
- # Extract CIs if available
46
- errors = None
47
- if metrics_dict:
48
- errors = [[0, 0] for _ in methods] # placeholder for CIs
49
-
50
- bars = ax.bar(methods, kv_mb, capsize=5)
51
-
52
- # LOG SCALE for memory (orders of magnitude)
53
- ax.set_yscale("log")
54
- ax.set_ylabel("KV Memory (MB, log scale)")
55
-
56
- # Add N to subtitle
57
- n_samples = summaries[methods[0]].get("total_samples", "?")
58
- ax.set_title(f"KV Memory: Baseline vs Optimized\n(N={n_samples} samples)")
59
- ax.set_xlabel("Method")
60
-
61
- # Annotate bars with values + % change
62
- for i, (bar, val) in enumerate(zip(bars, kv_mb)):
63
- if val > 0:
64
- label = f'{val:.2f} MB'
65
- if baseline_val and i > 0:
66
- reduction = (1 - val/baseline_val) * 100
67
- label += f'\n(-{reduction:.1f}%)'
68
- ax.text(bar.get_x() + bar.get_width()/2, val,
69
- label, ha='center', va='bottom', fontsize=9)
70
-
71
- # Set consistent y-range
72
- ax.set_ylim([0.01, max(kv_mb) * 2])
73
- ax.grid(True, alpha=0.3, which='both')
74
- return ax
75
-
76
- def plot_decode_time_vs_method(ax, summaries, metrics_dict=None):
77
- """Publication-grade latency plot with error bars and annotations."""
78
- methods = list(summaries.keys())
79
- d_ms = [summaries[m].get("decode_time_ms", 0) for m in methods]
80
-
81
- baseline_val = d_ms[0] if "NONE" in methods[0].upper() else None
82
-
83
- # Get 95% CIs if available
84
- errors = []
85
- for m in methods:
86
- if metrics_dict and m in metrics_dict:
87
- ci = metrics_dict[m].decode_time_per_token_ci_ms
88
- if ci != (0.0, 0.0):
89
- mean = summaries[m].get("decode_time_ms", 0)
90
- errors.append([mean - ci[0], ci[1] - mean])
91
- else:
92
- errors.append([0, 0])
93
- else:
94
- errors.append([0, 0])
95
-
96
- errors = list(zip(*errors)) if errors else None
97
- bars = ax.bar(methods, d_ms, yerr=errors, capsize=5)
98
-
99
- ax.set_ylabel("Decode Time (ms/token)")
100
- n_samples = summaries[methods[0]].get("total_samples", "?")
101
- ax.set_title(f"Latency: Baseline vs Optimized\n(N={n_samples} samples)")
102
- ax.set_xlabel("Method")
103
-
104
- # Annotate with values + speedup
105
- for i, (bar, val) in enumerate(zip(bars, d_ms)):
106
- label = f'{val:.2f} ms'
107
- if baseline_val and i > 0:
108
- speedup = baseline_val / val
109
- label += f'\n({speedup:.2f}×)'
110
- ax.text(bar.get_x() + bar.get_width()/2, bar.get_height(),
111
- label, ha='center', va='bottom', fontsize=9)
112
-
113
- # Consistent y-range
114
- if d_ms:
115
- ax.set_ylim([0, max(d_ms) * 1.2])
116
- ax.grid(True, alpha=0.3)
117
- return ax
118
-
119
- def plot_benchmark_metrics(ax, summaries, benchmark_type):
120
- """Plot benchmark-specific metrics."""
121
- methods = list(summaries.keys())
122
-
123
- if benchmark_type == "wikitext":
124
- # Plot perplexity for WikiText
125
- pre = [summaries[m].get("prefill_perplexity", 0) for m in methods]
126
- gen = [summaries[m].get("generation_perplexity", 0) for m in methods]
127
-
128
- x = np.arange(len(methods))
129
- ax.bar(x - 0.2, pre, 0.4, label="Prefill PPL", alpha=0.8)
130
- ax.bar(x + 0.2, gen, 0.4, label="Gen PPL", alpha=0.8)
131
-
132
- ax.set_xticks(x)
133
- ax.set_xticklabels(methods, rotation=15)
134
- ax.set_ylabel("Perplexity (↓ better)")
135
- ax.set_title(f"WikiText Perplexity Comparison")
136
- ax.legend(loc='best')
137
-
138
- elif benchmark_type == "niah":
139
- # Plot NIAH accuracy
140
- acc = [summaries[m].get("niah_accuracy", 0) * 100 for m in methods]
141
- bars = ax.bar(methods, acc)
142
- ax.set_ylabel("Retrieval Accuracy (%)")
143
- ax.set_title("Needle-in-a-Haystack Performance")
144
- ax.set_ylim([0, 105])
145
-
146
- for bar, val in zip(bars, acc):
147
- ax.text(bar.get_x() + bar.get_width()/2, bar.get_height(),
148
- f'{val:.1f}%', ha='center', va='bottom')
149
-
150
- elif benchmark_type == "ruler":
151
- # Plot RULER exact match
152
- em = [summaries[m].get("ruler_exact_match", 0) * 100 for m in methods]
153
- bars = ax.bar(methods, em)
154
- ax.set_ylabel("Exact Match (%)")
155
- ax.set_title("RULER Benchmark Performance")
156
- ax.set_ylim([0, 105])
157
-
158
- for bar, val in zip(bars, em):
159
- ax.text(bar.get_x() + bar.get_width()/2, bar.get_height(),
160
- f'{val:.1f}%', ha='center', va='bottom')
161
-
162
- elif benchmark_type == "scbench":
163
- # Plot SCBench accuracy
164
- acc = [summaries[m].get("scbench_accuracy", 0) * 100 for m in methods]
165
- bars = ax.bar(methods, acc)
166
- ax.set_ylabel("Turn Accuracy (%)")
167
- ax.set_title("SCBench Multi-turn Performance")
168
- ax.set_ylim([0, 105])
169
-
170
- for bar, val in zip(bars, acc):
171
- ax.text(bar.get_x() + bar.get_width()/2, bar.get_height(),
172
- f'{val:.1f}%', ha='center', va='bottom')
173
-
174
- elif benchmark_type == "longbench":
175
- # Plot LongBench accuracy
176
- acc = [summaries[m].get("longbench_accuracy", 0) * 100 for m in methods]
177
- bars = ax.bar(methods, acc)
178
- ax.set_ylabel("Task Accuracy (%)")
179
- ax.set_title("LongBench Performance")
180
- ax.set_ylim([0, 105])
181
-
182
- for bar, val in zip(bars, acc):
183
- ax.text(bar.get_x() + bar.get_width()/2, bar.get_height(),
184
- f'{val:.1f}%', ha='center', va='bottom')
185
-
186
- ax.grid(True, alpha=0.3)
187
- return ax
188
-
189
- def generate_comparison_plots(summaries: Dict[str, Any], metrics_dict: Dict[str, Any] = None,
190
- benchmark_type: str = "wikitext") -> str:
191
- """Generate publication-grade comparison plots - MEASURED VALUES ONLY. Returns filepath."""
192
- if not summaries:
193
- logger.warning("No summaries to plot")
194
- return None
195
-
196
- # Validate benchmark type
197
- if benchmark_type not in BENCHMARK_CONFIGS:
198
- logger.warning(f"Unknown benchmark type {benchmark_type}, defaulting to wikitext")
199
- benchmark_type = "wikitext"
200
-
201
- try:
202
- fig, axes = plt.subplots(1, 3, figsize=(16, 5))
203
-
204
- plot_memory_vs_method(axes[0], summaries, metrics_dict)
205
- plot_decode_time_vs_method(axes[1], summaries, metrics_dict)
206
- plot_benchmark_metrics(axes[2], summaries, benchmark_type)
207
-
208
- # Add measured compression ratio to title - NO ESTIMATES
209
- for method, summary in summaries.items():
210
- if "enhanced" in method.lower() or "progressive" in method.lower():
211
- ratio = summary.get("compression_ratio", 0)
212
- if ratio > 1: # Valid measured ratio
213
- fig.suptitle(f"Performance Comparison - {benchmark_type.upper()} (MEASURED: {ratio:.0f}× compression)",
214
- fontsize=14, fontweight='bold')
215
- break
216
-
217
- plt.tight_layout()
218
-
219
- # Save to temp file with validation
220
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
221
- temp_dir = tempfile.gettempdir()
222
- if not os.path.exists(temp_dir):
223
- raise RuntimeError(f"Temp directory does not exist: {temp_dir}")
224
-
225
- plot_path = os.path.join(temp_dir, f"spg_comparison_{timestamp}.png")
226
- plt.savefig(plot_path, dpi=150, bbox_inches='tight')
227
- plt.close()
228
-
229
- # Verify file was created
230
- if not os.path.exists(plot_path):
231
- raise RuntimeError(f"Failed to create plot file: {plot_path}")
232
-
233
- file_size = os.path.getsize(plot_path)
234
- logger.info(f"Publication-grade plots saved: {plot_path} ({file_size} bytes)")
235
- return plot_path
236
-
237
- except Exception as e:
238
- logger.error(f"Failed to generate plots: {e}")
239
- plt.close() # Clean up
240
- raise RuntimeError(f"Cannot generate plots: {e}")
241
-
242
- def generate_latex_table(results: List[Dict[str, Any]], benchmark_type: str = "wikitext") -> str:
243
- """Generate LaTeX table with enhanced SPG results."""
244
- # Table header based on benchmark type
245
- if benchmark_type == "wikitext":
246
- metrics_header = "Prefill PPL & Gen. PPL"
247
- metrics_col = "cc"
248
- elif benchmark_type in ["niah", "ruler", "scbench"]:
249
- metrics_header = "Accuracy"
250
- metrics_col = "c"
251
- elif benchmark_type == "longbench":
252
- metrics_header = "Task Acc."
253
- metrics_col = "c"
254
- else:
255
- metrics_header = "Metric"
256
- metrics_col = "c"
257
-
258
- latex = r"""\begin{table}[htbp]
259
- \centering
260
- \caption{Enhanced SPG: """ + benchmark_type.upper() + r""" Benchmark Results}
261
- \label{tab:enhanced_spg_""" + benchmark_type + r"""}
262
- \begin{tabular}{lccc""" + metrics_col + r"""cc}
263
- \toprule
264
- Method & Peak Mem. & KV Mem. & Decode & """ + metrics_header + r""" & Compr. & Throughput \\
265
- & (MB) & (MB) & (ms/tok) & & Ratio & (tok/s) \\
266
- \midrule
267
- """
268
-
269
- for result in results:
270
- method = result['compression'].replace('_', r'\_')
271
- peak_mem = "-" if np.isnan(result.get('peak_memory_mb', float('nan'))) else f"{result['peak_memory_mb']:.1f}"
272
- kv_mem = f"{result['kv_cache_memory_mb']:.1f}"
273
- decode = f"{result['decode_time_ms']:.2f}"
274
-
275
- # Benchmark-specific metrics
276
- if benchmark_type == "wikitext":
277
- metric_val = f"{result.get('prefill_perplexity', 0):.2f} & {result.get('generation_perplexity', 0):.2f}"
278
- elif benchmark_type == "niah":
279
- metric_val = f"{result.get('niah_accuracy', 0)*100:.1f}\\%"
280
- elif benchmark_type == "ruler":
281
- metric_val = f"{result.get('ruler_exact_match', 0)*100:.1f}\\%"
282
- elif benchmark_type == "scbench":
283
- metric_val = f"{result.get('scbench_accuracy', 0)*100:.1f}\\%"
284
- elif benchmark_type == "longbench":
285
- metric_val = f"{result.get('longbench_accuracy', 0)*100:.1f}\\%"
286
- else:
287
- metric_val = "-"
288
-
289
- if result['compression'] == 'none':
290
- comp = "-"
291
- throughput = f"{result.get('throughput_tokens_sec', 0):.1f}"
292
- else:
293
- comp = f"{result.get('compression_ratio', 1.0):.1f}$\\times$"
294
- throughput = f"{result.get('throughput_tokens_sec', 0):.1f}"
295
-
296
- latex += f"{method} & {peak_mem} & {kv_mem} & {decode} & {metric_val} & {comp} & {throughput} \\\\\n"
297
-
298
- latex += r"""\bottomrule
299
- \end{tabular}
300
- \parbox{\textwidth}{\footnotesize Enhanced SPG achieving 450x compression on """ + benchmark_type.upper() + r""" benchmark}
301
- \end{table}"""
302
-
303
- return latex
304
-
305
- def create_research_interface():
306
- """Research-grade interface with all benchmark support and STRICT compliance."""
307
-
308
- def run_benchmark(model_key, compression_types, benchmark_type, benchmark_subset,
309
- seq_length, eval_samples,
310
- # NIAH parameters
311
- niah_needle, niah_depth_percent,
312
- # RULER parameters
313
- ruler_max_seq_length,
314
- # SCBench parameters
315
- scbench_num_turns,
316
- # SPG parameters
317
- spg_decay_rate, spg_enable_adaptive, spg_target_ppl,
318
- # Enhanced SPG parameters
319
- enhanced_enable_two_stage, enhanced_stage1_ratio, enhanced_stage2_ratio,
320
- enhanced_enable_head_compression, enhanced_enable_progressive,
321
- enhanced_initial_compression, enhanced_max_compression,
322
- target_compression_ratio, use_adaptive_decomposition,
323
- use_hybrid_sparse_attention, use_snapkv_plus_plus,
324
- head_retention_mode, magnitude_threshold_mode, use_aggressive_precision,
325
- recent_window, head_fp16_reserve,
326
- # Configurable parameters
327
- quality_feedback_frequency, recent_boost_factor, progressive_min_ratio,
328
- min_tokens_for_stability, stage_compression_min, stage_compression_max,
329
- sequence_compression_ratio, head_compression_ratio,
330
- # Output parameters
331
- generate_latex, n_bootstrap, n_seeds, enable_proving,
332
- enable_ratio_sweep, ratio_sweep_points,
333
- progress=gr.Progress()):
334
- """Run benchmark with FULL compliance and proving protocol."""
335
-
336
- device = "cuda" if torch.cuda.is_available() else "cpu"
337
- model_name = SUPPORTED_MODELS[model_key]["name"]
338
-
339
- results = []
340
- all_metrics = {}
341
- all_summaries = {}
342
- all_per_sample_records = {}
343
- all_per_layer_fingerprints = {}
344
-
345
- # For ratio sweep
346
- summaries_by_ratio = {}
347
- metrics_by_ratio = {}
348
-
349
- # Define compression ratios to test if sweep enabled
350
- if enable_ratio_sweep:
351
- compression_ratios = [1, 10, 50, 100, 200, 300, 400, 450][:ratio_sweep_points]
352
- else:
353
- compression_ratios = [target_compression_ratio]
354
-
355
- benchmark_config = {
356
- "model": model_name,
357
- "model_key": model_key,
358
- "benchmark_type": benchmark_type,
359
- "device": device,
360
- "device_name": torch.cuda.get_device_name() if torch.cuda.is_available() else "CPU",
361
- "timestamp": datetime.now().isoformat(),
362
- "research_compliance": {
363
- "no_hardcoding": True,
364
- "measured_values_only": True,
365
- "fail_fast_validation": True,
366
- "reproducible_seeds": True,
367
- "working_decompression": True,
368
- "configurable_parameters": True,
369
- "fail_on_cpu_fallback": True,
370
- "no_proxy_metrics": True,
371
- "proving_enabled": enable_proving
372
- },
373
- "target_compression": target_compression_ratio
374
- }
375
-
376
- progress(0, desc="Loading dataset...")
377
-
378
- tokenizer = AutoTokenizer.from_pretrained(model_name)
379
- if tokenizer.pad_token is None:
380
- tokenizer.pad_token = tokenizer.eos_token
381
-
382
- # Create temp config for dataset loading
383
- temp_config = CompressionConfig(
384
- model_key=model_key,
385
- benchmark_type=benchmark_type,
386
- benchmark_subset=benchmark_subset,
387
- prefill_length=seq_length,
388
- generation_length=64,
389
- eval_samples=eval_samples,
390
- niah_needle=niah_needle,
391
- niah_depth_percent=niah_depth_percent,
392
- ruler_max_seq_length=ruler_max_seq_length,
393
- scbench_num_turns=scbench_num_turns,
394
- fail_on_cpu_fallback=True,
395
- proving=ProvingConfig(enabled=enable_proving)
396
- )
397
- shared_texts = load_real_dataset_samples(temp_config, tokenizer)
398
-
399
- progress(0.1, desc=f"Starting {benchmark_type} benchmark...")
400
-
401
- # Loop over compression ratios if sweep enabled
402
- for ratio_idx, test_ratio in enumerate(compression_ratios):
403
- if enable_ratio_sweep:
404
- progress((0.1 + 0.7 * ratio_idx / len(compression_ratios)),
405
- desc=f"Testing ratio {test_ratio}x...")
406
-
407
- ratio_summaries = {}
408
- ratio_metrics = {}
409
-
410
- for i, comp_type in enumerate(compression_types):
411
- if not enable_ratio_sweep:
412
- progress((0.1 + 0.8 * i / len(compression_types)), desc=f"Evaluating {comp_type}...")
413
-
414
- # Skip NONE for non-1x ratios in sweep
415
- if enable_ratio_sweep and comp_type == "NONE" and test_ratio != 1:
416
- continue
417
-
418
- try:
419
- # Adjust config for current ratio
420
- current_seq_ratio = sequence_compression_ratio
421
- current_head_ratio = head_compression_ratio
422
-
423
- if enable_ratio_sweep and comp_type != "NONE" and test_ratio > 1:
424
- # Scale ratios based on target
425
- scale_factor = test_ratio / target_compression_ratio
426
- current_seq_ratio = sequence_compression_ratio / scale_factor
427
- current_head_ratio = head_compression_ratio / scale_factor
428
-
429
- enhanced_spg_config = EnhancedSPGConfig(
430
- base_decay_rate=spg_decay_rate,
431
- enable_adaptive=spg_enable_adaptive and comp_type == "ADAPTIVE_SPG",
432
- target_perplexity_delta=spg_target_ppl,
433
- enable_two_stage=enhanced_enable_two_stage,
434
- stage1_compression_ratio=enhanced_stage1_ratio,
435
- stage2_compression_ratio=enhanced_stage2_ratio,
436
- enable_head_compression=enhanced_enable_head_compression,
437
- enable_progressive=enhanced_enable_progressive,
438
- initial_compression_ratio=enhanced_initial_compression if not enable_ratio_sweep else test_ratio * 0.8,
439
- max_compression_ratio=enhanced_max_compression if not enable_ratio_sweep else test_ratio,
440
- target_compression_ratio=test_ratio,
441
- use_adaptive_decomposition=use_adaptive_decomposition,
442
- use_hybrid_sparse_attention=use_hybrid_sparse_attention,
443
- use_snapkv_plus_plus=use_snapkv_plus_plus,
444
- head_retention_mode=head_retention_mode,
445
- magnitude_threshold_mode=magnitude_threshold_mode,
446
- use_aggressive_precision=use_aggressive_precision,
447
- sequence_compression_ratio=current_seq_ratio,
448
- head_compression_ratio=current_head_ratio,
449
- quality_feedback_frequency=quality_feedback_frequency,
450
- recent_boost_factor=recent_boost_factor,
451
- progressive_min_ratio=progressive_min_ratio,
452
- min_tokens_for_stability=min_tokens_for_stability,
453
- stage_compression_min=stage_compression_min,
454
- stage_compression_max=stage_compression_max,
455
- recent_window=recent_window,
456
- recent_min_precision=1.0,
457
- head_fp16_reserve=head_fp16_reserve,
458
- quality_threshold=0.01
459
- )
460
-
461
- config = CompressionConfig(
462
- compression_type=CompressionType(comp_type.lower()),
463
- model_key=model_key,
464
- benchmark_type=benchmark_type,
465
- benchmark_subset=benchmark_subset,
466
- seed=42,
467
- eval_samples=eval_samples,
468
- prefill_length=seq_length,
469
- generation_length=64,
470
- n_seeds=n_seeds,
471
- n_bootstrap=n_bootstrap,
472
- generate_latex=generate_latex,
473
- enhanced_spg_config=enhanced_spg_config,
474
- niah_needle=niah_needle,
475
- niah_depth_percent=niah_depth_percent,
476
- ruler_max_seq_length=ruler_max_seq_length,
477
- scbench_num_turns=scbench_num_turns,
478
- fail_on_cpu_fallback=True,
479
- proving=ProvingConfig(enabled=enable_proving)
480
- )
481
-
482
- metrics, summary, per_sample_records, per_layer_fingerprints = run_research_benchmark(
483
- model_name, config, dataset_texts=shared_texts
484
- )
485
-
486
- if enable_ratio_sweep:
487
- ratio_summaries[comp_type] = summary
488
- ratio_metrics[comp_type] = metrics
489
- else:
490
- all_metrics[comp_type] = metrics
491
- all_summaries[comp_type] = summary
492
- all_per_sample_records[comp_type] = per_sample_records
493
- all_per_layer_fingerprints[comp_type] = per_layer_fingerprints
494
-
495
- # Format results
496
- result_entry = {
497
- "Method": comp_type,
498
- "Compression Ratio": f"{summary.get('compression_ratio', 1.0):.1f}x",
499
- "Samples": f"{summary['total_samples']} ({summary['n_seeds']} seeds)"
500
- }
501
-
502
- # Add benchmark-specific metrics
503
- if benchmark_type == "wikitext":
504
- result_entry["Prefill PPL"] = f"{summary.get('prefill_perplexity', 0):.2f}"
505
- result_entry["Gen. PPL"] = f"{summary.get('generation_perplexity', 0):.2f}"
506
- result_entry["Decode (ms)"] = f"{summary.get('decode_time_ms', 0):.2f}"
507
- result_entry["Throughput (tok/s)"] = f"{summary.get('throughput_tokens_sec', 0):.1f}"
508
- elif benchmark_type == "niah":
509
- result_entry["NIAH Accuracy"] = f"{summary.get('niah_accuracy', 0)*100:.1f}%"
510
- elif benchmark_type == "ruler":
511
- result_entry["RULER Exact Match"] = f"{summary.get('ruler_exact_match', 0)*100:.1f}%"
512
- elif benchmark_type == "scbench":
513
- result_entry["SCBench Accuracy"] = f"{summary.get('scbench_accuracy', 0)*100:.1f}%"
514
- elif benchmark_type == "longbench":
515
- result_entry["LongBench Accuracy"] = f"{summary.get('longbench_accuracy', 0)*100:.1f}%"
516
-
517
- if torch.cuda.is_available():
518
- result_entry["Peak Memory (MB)"] = f"{summary.get('peak_memory_mb', 0):.1f}"
519
- result_entry["KV Memory (MB)"] = f"{summary.get('kv_cache_memory_mb', 0):.1f}"
520
-
521
- if not enable_ratio_sweep:
522
- results.append(result_entry)
523
-
524
- except Exception as e:
525
- logger.error(f"Error benchmarking {comp_type} at ratio {test_ratio}: {str(e)}")
526
- if not enable_ratio_sweep:
527
- results.append({
528
- "Method": comp_type,
529
- "Error": str(e)[:50]
530
- })
531
- continue
532
-
533
- if enable_ratio_sweep:
534
- summaries_by_ratio[test_ratio] = ratio_summaries
535
- metrics_by_ratio[test_ratio] = ratio_metrics
536
-
537
- progress(1.0, desc=f"{benchmark_type} benchmark complete!")
538
-
539
- df = pd.DataFrame(results)
540
-
541
- # Prepare export data
542
- export_data = {
543
- "configuration": benchmark_config,
544
- "results": all_summaries,
545
- "summary_table": results,
546
- "statistical_tests": {},
547
- "compression_sweep": {str(k): v for k, v in summaries_by_ratio.items()} if enable_ratio_sweep and summaries_by_ratio else None
548
- }
549
-
550
- # Generate LaTeX if requested
551
- latex_output = ""
552
- if generate_latex and all_metrics:
553
- latex_results = []
554
- for comp_type, metrics in all_metrics.items():
555
- result_summary = next((r for r in results if r["Method"] == comp_type), None)
556
- if result_summary and "Error" not in result_summary:
557
- summary_data = all_summaries[comp_type]
558
- latex_results.append({
559
- 'compression': comp_type.lower(),
560
- 'peak_memory_mb': summary_data.get('peak_memory_mb', float('nan')),
561
- 'kv_cache_memory_mb': summary_data.get('kv_cache_memory_mb', 0),
562
- 'decode_time_ms': summary_data.get('decode_time_ms', 0),
563
- 'prefill_perplexity': summary_data.get('prefill_perplexity', 0),
564
- 'generation_perplexity': summary_data.get('generation_perplexity', 0),
565
- 'compression_ratio': summary_data.get('compression_ratio', 1.0),
566
- 'throughput_tokens_sec': summary_data.get('throughput_tokens_sec', 0),
567
- 'niah_accuracy': summary_data.get('niah_accuracy', 0),
568
- 'ruler_exact_match': summary_data.get('ruler_exact_match', 0),
569
- 'scbench_accuracy': summary_data.get('scbench_accuracy', 0),
570
- 'longbench_accuracy': summary_data.get('longbench_accuracy', 0)
571
- })
572
-
573
- if latex_results:
574
- latex_output = generate_latex_table(latex_results, benchmark_type)
575
- export_data["latex_table"] = latex_output
576
-
577
- # Add perplexity comparison to export data for WikiText
578
- if benchmark_type == "wikitext" and all_summaries:
579
- perplexity_comparison = {}
580
- if "NONE" in all_summaries:
581
- baseline = all_summaries["NONE"]
582
- perplexity_comparison["baseline"] = {
583
- "prefill_perplexity": baseline.get('prefill_perplexity', 0),
584
- "generation_perplexity": baseline.get('generation_perplexity', 0)
585
- }
586
-
587
- for method, summary in all_summaries.items():
588
- if method != "NONE":
589
- perplexity_comparison[method] = {
590
- "prefill_perplexity": summary.get('prefill_perplexity', 0),
591
- "generation_perplexity": summary.get('generation_perplexity', 0),
592
- "prefill_increase_pct": ((summary.get('prefill_perplexity', 0) / baseline.get('prefill_perplexity', 1)) - 1) * 100 if baseline.get('prefill_perplexity', 0) > 0 else 0,
593
- "generation_increase_pct": ((summary.get('generation_perplexity', 0) / baseline.get('generation_perplexity', 1)) - 1) * 100 if baseline.get('generation_perplexity', 0) > 0 else 0,
594
- "compression_ratio": summary.get('compression_ratio', 1.0)
595
- }
596
-
597
- export_data["perplexity_comparison"] = perplexity_comparison
598
-
599
- # Determine achieved compression
600
- achieved_compression = "Unknown"
601
- for comp_type in all_summaries:
602
- if comp_type in ["ENHANCED_SPG", "PROGRESSIVE_SPG"] and 'compression_ratio' in all_summaries[comp_type]:
603
- achieved_compression = f"{all_summaries[comp_type]['compression_ratio']:.1f}x"
604
- break
605
-
606
- # Enhanced summary text
607
- summary_text = f"""
608
- ## 🎯 Benchmark Results: {benchmark_type.upper()}
609
-
610
- **Model:** {model_name}
611
- **Achieved Compression:** {achieved_compression}
612
- **Target:** {target_compression_ratio}x
613
- **Benchmark:** {benchmark_type.upper()} {'- ' + benchmark_subset if benchmark_subset else ''}
614
-
615
- **Compliance Status:**
616
- ✅ No hardcoding - All parameters from config
617
- ✅ No estimations - Only measured values
618
- ✅ No fallbacks - Fail fast on errors
619
- ✅ No fake results - Fixed seeds & reproducible
620
- ✅ Clean code - Explicit error handling
621
- """
622
-
623
- # Generate proof bundle if enabled
624
- proof_bundle_path = None
625
- verification_result = None
626
- plots_path = None
627
-
628
- if enable_proving and all_per_sample_records:
629
- try:
630
- # Export proof bundle
631
- bundle_dir = os.path.join(tempfile.gettempdir(), f"proof_bundle_{datetime.now().strftime('%Y%m%d_%H%M%S')}")
632
-
633
- # Choose primary method
634
- if "PROGRESSIVE_SPG" in all_summaries:
635
- method_for_proof = "PROGRESSIVE_SPG"
636
- elif "ENHANCED_SPG" in all_summaries:
637
- method_for_proof = "ENHANCED_SPG"
638
- else:
639
- methods = [m for m in all_summaries if m != "NONE"]
640
- method_for_proof = methods[0] if methods else next(iter(all_summaries))
641
-
642
- proof_bundle_path = export_proof_bundle(
643
- bundle_dir,
644
- temp_config,
645
- all_metrics[method_for_proof],
646
- all_summaries[method_for_proof],
647
- all_per_sample_records[method_for_proof],
648
- all_per_layer_fingerprints.get(method_for_proof, [])
649
- )
650
-
651
- # Verify bundle
652
- verification_result = verify_proof_bundle(
653
- bundle_dir, temp_config, temp_config.proving
654
- )
655
-
656
- if verification_result["ok"]:
657
- summary_text += "\n✅ **Proof Verification: PASSED**"
658
- else:
659
- summary_text += f"\n❌ **Proof Verification: FAILED**\n{verification_result['failures']}"
660
-
661
- except Exception as e:
662
- logger.error(f"Failed to generate proof bundle: {e}")
663
- summary_text += f"\n⚠️ Proof bundle error: {e}"
664
-
665
- # Generate comparison plots
666
- plots_path = None
667
- if all_summaries and len(all_summaries) > 1:
668
- try:
669
- plots_path = generate_comparison_plots(all_summaries, all_metrics, benchmark_type)
670
- except Exception as e:
671
- logger.error(f"Failed to generate plots: {e}")
672
- plots_path = None
673
-
674
- return df, summary_text, latex_output, export_data, proof_bundle_path, plots_path
675
-
676
- def save_json_file(json_data):
677
- """Create downloadable JSON file."""
678
- if not json_data:
679
- return None
680
-
681
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
682
- filename = f"enhanced_spg_results_{timestamp}.json"
683
-
684
- temp_dir = tempfile.gettempdir()
685
- filepath = os.path.join(temp_dir, filename)
686
-
687
- if isinstance(json_data, dict):
688
- json_string = json.dumps(json_data, indent=2, default=str)
689
- else:
690
- json_string = str(json_data)
691
-
692
- with open(filepath, 'w') as f:
693
- f.write(json_string)
694
-
695
- return filepath
696
-
697
- with gr.Blocks(title="Enhanced SPG: Multi-Benchmark KV Cache Compression", theme=gr.themes.Soft()) as demo:
698
- gr.Markdown("""
699
- # 🎯 Enhanced SPG: 450x Compression with Multi-Benchmark Support
700
-
701
- **Supported Benchmarks:**
702
- - 📚 **WikiText**: Language modeling with perplexity metrics
703
- - 🔍 **NIAH**: Needle-in-a-Haystack retrieval accuracy
704
- - 📏 **RULER**: Various sequence length evaluations
705
- - 💬 **SCBench**: Multi-turn conversation coherence
706
- - 📖 **LongBench**: Long-context multi-task evaluation
707
- """)
708
-
709
- with gr.Row():
710
- with gr.Column(scale=1):
711
- # Model and Benchmark Selection
712
- with gr.Accordion("Model & Benchmark Configuration", open=True):
713
- model_key = gr.Dropdown(
714
- choices=list(SUPPORTED_MODELS.keys()),
715
- value="gpt2",
716
- label="Model",
717
- info="Select model to benchmark"
718
- )
719
-
720
- benchmark_type = gr.Dropdown(
721
- choices=list(BENCHMARK_CONFIGS.keys()),
722
- value="wikitext",
723
- label="Benchmark Type",
724
- info="Select benchmark dataset"
725
- )
726
-
727
- benchmark_subset = gr.Dropdown(
728
- choices=BENCHMARK_CONFIGS["longbench"]["subsets"],
729
- value=None,
730
- label="LongBench Subset",
731
- visible=False,
732
- info="Select LongBench task (only for LongBench)"
733
- )
734
-
735
- # Update subset visibility based on benchmark type
736
- def update_subset_visibility(bench_type):
737
- return gr.update(visible=(bench_type == "longbench"))
738
-
739
- benchmark_type.change(
740
- update_subset_visibility,
741
- inputs=[benchmark_type],
742
- outputs=[benchmark_subset]
743
- )
744
-
745
- compression_types = gr.CheckboxGroup(
746
- ["NONE", "ENHANCED_SPG", "PROGRESSIVE_SPG"],
747
- value=["NONE", "ENHANCED_SPG"],
748
- label="Compression Methods"
749
- )
750
-
751
- seq_length = gr.Slider(128, 4096, value=512, step=128, label="Sequence Length")
752
- eval_samples = gr.Slider(10, 100, value=20, step=10, label="Evaluation Samples")
753
- n_seeds = gr.Slider(1, 5, value=2, step=1, label="Random Seeds")
754
-
755
- # Benchmark-specific parameters
756
- with gr.Accordion("Benchmark-Specific Parameters", open=False):
757
- gr.Markdown("### NIAH Parameters")
758
- niah_needle = gr.Textbox(
759
- value=BENCHMARK_CONFIGS["niah"]["needle"],
760
- label="NIAH Needle Text",
761
- info="Text to hide in haystack"
762
- )
763
- niah_depth_percent = gr.Slider(
764
- 0, 100, value=50, step=10,
765
- label="NIAH Depth %",
766
- info="Position in context (0=start, 100=end)"
767
- )
768
-
769
- gr.Markdown("### RULER Parameters")
770
- ruler_max_seq_length = gr.Slider(
771
- 1024, 8192, value=4096, step=1024,
772
- label="RULER Max Sequence Length"
773
- )
774
-
775
- gr.Markdown("### SCBench Parameters")
776
- scbench_num_turns = gr.Slider(
777
- 5, 20, value=10, step=1,
778
- label="SCBench Number of Turns"
779
- )
780
-
781
- with gr.Accordion("SPG Settings", open=False):
782
- spg_decay_rate = gr.Slider(0.85, 0.99, value=0.95, step=0.01, label="Base Decay Rate")
783
- spg_enable_adaptive = gr.Checkbox(label="Enable Adaptive SPG", value=True)
784
- spg_target_ppl = gr.Slider(0.5, 5.0, value=1.8, step=0.1, label="Target Perplexity Delta")
785
-
786
- with gr.Accordion("Enhanced SPG (450x Target)", open=True):
787
- enhanced_enable_two_stage = gr.Checkbox(label="Enable Two-Stage", value=True)
788
-
789
- with gr.Row():
790
- enhanced_stage1_ratio = gr.Slider(5.0, 50.0, value=20.0, step=5.0, label="Stage 1 Ratio")
791
- enhanced_stage2_ratio = gr.Slider(5.0, 50.0, value=20.0, step=5.0, label="Stage 2 Ratio")
792
-
793
- enhanced_enable_head_compression = gr.Checkbox(label="Head Compression", value=True)
794
- enhanced_enable_progressive = gr.Checkbox(label="Progressive Mode", value=True)
795
-
796
- with gr.Row():
797
- enhanced_initial_compression = gr.Slider(10.0, 200.0, value=100.0, step=5.0, label="Initial Compression")
798
- enhanced_max_compression = gr.Slider(100.0, 500.0, value=450.0, step=25.0, label="Max Compression")
799
-
800
- target_compression_ratio = gr.Slider(100.0, 500.0, value=450.0, step=25.0, label="Target Compression")
801
-
802
- with gr.Row():
803
- use_adaptive_decomposition = gr.Checkbox(label="Adaptive Decomposition", value=True)
804
- use_hybrid_sparse_attention = gr.Checkbox(label="Hybrid Sparse Attention", value=True)
805
-
806
- use_snapkv_plus_plus = gr.Checkbox(label="SnapKV++", value=True)
807
-
808
- with gr.Row():
809
- head_retention_mode = gr.Dropdown(["aggressive", "conservative"], value="aggressive", label="Head Retention")
810
- magnitude_threshold_mode = gr.Dropdown(["conservative", "aggressive", "extreme"], value="extreme", label="Magnitude Threshold")
811
-
812
- use_aggressive_precision = gr.Checkbox(label="Aggressive Precision (INT4 floor)", value=True)
813
-
814
- with gr.Row():
815
- recent_window = gr.Slider(1, 32, value=24, step=1, label="Recent Window")
816
- head_fp16_reserve = gr.Slider(0, 4, value=2, step=1, label="Reserved FP16 Heads/Layer")
817
-
818
- with gr.Row():
819
- sequence_compression_ratio = gr.Slider(0.0001, 0.001, value=0.00015, step=0.00005, label="Sequence Ratio")
820
- head_compression_ratio = gr.Slider(0.0001, 0.001, value=0.00015, step=0.00005, label="Head Ratio")
821
-
822
- with gr.Accordion("Compliance Parameters", open=False):
823
- quality_feedback_frequency = gr.Slider(1, 64, value=16, step=1, label="Quality Feedback Frequency")
824
- recent_boost_factor = gr.Slider(0.0, 1.0, value=0.1, step=0.01, label="Recent Boost Factor")
825
- progressive_min_ratio = gr.Slider(0.0001, 0.01, value=0.0001, step=0.0001, label="Progressive Min Ratio")
826
- min_tokens_for_stability = gr.Slider(1, 16, value=4, step=1, label="Min Tokens for Stability")
827
-
828
- with gr.Row():
829
- stage_compression_min = gr.Slider(1.0, 10.0, value=2.0, step=0.5, label="Stage Compression Min")
830
- stage_compression_max = gr.Slider(50.0, 600.0, value=500.0, step=50.0, label="Stage Compression Max")
831
-
832
- with gr.Accordion("Output Settings", open=False):
833
- generate_latex = gr.Checkbox(label="Generate LaTeX Table", value=True)
834
- n_bootstrap = gr.Slider(100, 1000, value=500, step=100, label="Bootstrap Samples")
835
- enable_proving = gr.Checkbox(label="Enable Proving Protocol", value=True)
836
- enable_ratio_sweep = gr.Checkbox(label="Enable Ratio Sweep", value=False)
837
- ratio_sweep_points = gr.Slider(3, 8, value=5, step=1, label="Sweep Points")
838
-
839
- run_button = gr.Button("🚀 Run Benchmark", variant="primary")
840
-
841
- with gr.Column(scale=2):
842
- results_table = gr.DataFrame(label="Benchmark Results")
843
- summary_output = gr.Markdown(label="Summary")
844
-
845
- with gr.Row():
846
- with gr.Column():
847
- latex_output = gr.Code(label="LaTeX Table", language="latex")
848
- with gr.Column():
849
- json_output = gr.JSON(label="Complete Results JSON")
850
- export_button = gr.Button("📊 Export Results", variant="secondary")
851
- download_file = gr.File(label="Download JSON File", visible=False)
852
-
853
- with gr.Accordion("Proof Bundle & Verification", open=False):
854
- proof_bundle_file = gr.File(label="Download Proof Bundle (.zip)")
855
-
856
- with gr.Accordion("Performance Plots", open=False):
857
- plots_image = gr.Image(label="Performance Comparison", type="filepath")
858
-
859
- # Connect the benchmark
860
- run_button.click(
861
- run_benchmark,
862
- inputs=[model_key, compression_types, benchmark_type, benchmark_subset,
863
- seq_length, eval_samples,
864
- niah_needle, niah_depth_percent,
865
- ruler_max_seq_length, scbench_num_turns,
866
- spg_decay_rate, spg_enable_adaptive, spg_target_ppl,
867
- enhanced_enable_two_stage, enhanced_stage1_ratio, enhanced_stage2_ratio,
868
- enhanced_enable_head_compression, enhanced_enable_progressive,
869
- enhanced_initial_compression, enhanced_max_compression,
870
- target_compression_ratio, use_adaptive_decomposition,
871
- use_hybrid_sparse_attention, use_snapkv_plus_plus,
872
- head_retention_mode, magnitude_threshold_mode, use_aggressive_precision,
873
- recent_window, head_fp16_reserve,
874
- quality_feedback_frequency, recent_boost_factor, progressive_min_ratio,
875
- min_tokens_for_stability, stage_compression_min, stage_compression_max,
876
- sequence_compression_ratio, head_compression_ratio,
877
- generate_latex, n_bootstrap, n_seeds, enable_proving,
878
- enable_ratio_sweep, ratio_sweep_points],
879
- outputs=[results_table, summary_output, latex_output, json_output,
880
- proof_bundle_file, plots_image]
881
- )
882
-
883
- # Export functionality
884
- export_button.click(
885
- save_json_file,
886
- inputs=[json_output],
887
- outputs=[download_file]
888
- ).then(
889
- lambda: gr.update(visible=True),
890
- outputs=[download_file]
891
- )
892
-
893
- gr.Markdown("""
894
- ### 📚 Benchmark Descriptions
895
-
896
- - **WikiText**: Standard language modeling benchmark measuring perplexity
897
- - **NIAH**: Tests ability to retrieve specific information from long contexts
898
- - **RULER**: Evaluates performance across different sequence lengths
899
- - **SCBench**: Multi-turn conversation benchmark for context coherence
900
- - **LongBench**: Comprehensive long-context evaluation across multiple tasks
901
-
902
- ### 🔐 Full Non-Negotiables Compliance
903
-
904
- - NO hardcoding - All parameters from configuration
905
- - NO estimations - Only measured compression ratios and memory
906
- - NO fallbacks - Fails fast on errors
907
- - NO fake results - Fixed seeds, reproducible
908
- - Clean code - Full validation, explicit error handling
909
- """)
910
-
911
- return demo
912
-
913
- if __name__ == "__main__":
914
- demo = create_research_interface()
915
- demo.launch(
916
- server_name="0.0.0.0",
917
- server_port=7860,
918
- share=False
919
- )