Spaces:
Sleeping
Sleeping
a0y0346 commited on
Commit ·
374d38b
1
Parent(s): 47751f7
feat: Use real HuggingFace model attention layers for benchmarks
Browse files- Add attention_utils.py with functions to extract and benchmark
real attention layers from loaded HF models
- Refactor benchmark.py to load actual models and run attention
layer forward passes instead of raw SDPA with random tensors
- Refactor prefill_decode.py to use real model attention for both
prefill and decode phase comparisons
- Update app.py to pass model names to benchmark functions
This ensures all GPU benchmarks use real HuggingFace model
attention layers (SmolLM2-360M, Qwen2.5-0.5B, Llama-3.2-1B)
rather than synthetic random tensors.
- app.py +24 -10
- src/attention_utils.py +408 -0
- src/benchmark.py +86 -14
- src/prefill_decode.py +300 -99
app.py
CHANGED
|
@@ -280,7 +280,7 @@ def create_app() -> gr.Blocks:
|
|
| 280 |
# Event handlers for benchmark tab
|
| 281 |
@spaces.GPU(duration=120)
|
| 282 |
def run_single_benchmark(model_name, seq_len):
|
| 283 |
-
"""Run benchmark for a single configuration
|
| 284 |
from src.benchmark import (
|
| 285 |
run_attention_benchmark,
|
| 286 |
create_benchmark_results_table,
|
|
@@ -295,16 +295,29 @@ def create_app() -> gr.Blocks:
|
|
| 295 |
gpu_specs = detect_gpu()
|
| 296 |
gpu_display = f"**GPU Detected:** {gpu_specs.get('detected_name', gpu_specs['name'])} ({gpu_specs['tflops_fp16']} TFLOPS FP16, {gpu_specs['bandwidth_gbps']} GB/s)"
|
| 297 |
|
| 298 |
-
config = MODEL_CONFIGS.get(model_name, MODEL_CONFIGS[DEFAULT_MODEL])
|
| 299 |
seq_len_int = int(seq_len)
|
| 300 |
|
|
|
|
| 301 |
results = run_attention_benchmark(
|
|
|
|
| 302 |
seq_len=seq_len_int,
|
| 303 |
-
num_heads=config["q_heads"],
|
| 304 |
-
head_dim=config["head_dim"],
|
| 305 |
batch_size=1,
|
| 306 |
)
|
| 307 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
# Calculate roofline metrics from results
|
| 309 |
roofline_metrics = calculate_roofline_metrics(
|
| 310 |
results=results,
|
|
@@ -316,7 +329,10 @@ def create_app() -> gr.Blocks:
|
|
| 316 |
|
| 317 |
table = create_benchmark_results_table(results)
|
| 318 |
insight = create_benchmark_insight(results)
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
# Update roofline with measured data using detected GPU specs
|
| 322 |
roofline = create_roofline_chart(results, gpu_specs, roofline_metrics)
|
|
@@ -329,15 +345,13 @@ def create_app() -> gr.Blocks:
|
|
| 329 |
|
| 330 |
@spaces.GPU(duration=180)
|
| 331 |
def run_scaling_test(model_name):
|
| 332 |
-
"""Run scaling benchmark across sequence lengths."""
|
| 333 |
from src.benchmark import run_scaling_benchmark, create_scaling_chart
|
| 334 |
|
| 335 |
-
|
| 336 |
-
|
| 337 |
results = run_scaling_benchmark(
|
|
|
|
| 338 |
seq_lengths=[512, 1024, 2048, 4096],
|
| 339 |
-
num_heads=config["q_heads"],
|
| 340 |
-
head_dim=config["head_dim"],
|
| 341 |
batch_size=1,
|
| 342 |
)
|
| 343 |
|
|
|
|
| 280 |
# Event handlers for benchmark tab
|
| 281 |
@spaces.GPU(duration=120)
|
| 282 |
def run_single_benchmark(model_name, seq_len):
|
| 283 |
+
"""Run benchmark for a single configuration using REAL model attention layers."""
|
| 284 |
from src.benchmark import (
|
| 285 |
run_attention_benchmark,
|
| 286 |
create_benchmark_results_table,
|
|
|
|
| 295 |
gpu_specs = detect_gpu()
|
| 296 |
gpu_display = f"**GPU Detected:** {gpu_specs.get('detected_name', gpu_specs['name'])} ({gpu_specs['tflops_fp16']} TFLOPS FP16, {gpu_specs['bandwidth_gbps']} GB/s)"
|
| 297 |
|
|
|
|
| 298 |
seq_len_int = int(seq_len)
|
| 299 |
|
| 300 |
+
# Use REAL MODEL attention layer for benchmarking
|
| 301 |
results = run_attention_benchmark(
|
| 302 |
+
model_name=model_name, # Pass model name to load real HF model
|
| 303 |
seq_len=seq_len_int,
|
|
|
|
|
|
|
| 304 |
batch_size=1,
|
| 305 |
)
|
| 306 |
|
| 307 |
+
if "error" in results:
|
| 308 |
+
return (
|
| 309 |
+
f"❌ Error: {results['error']}",
|
| 310 |
+
f"**Error:** {results['error']}",
|
| 311 |
+
"",
|
| 312 |
+
gpu_display,
|
| 313 |
+
None,
|
| 314 |
+
"",
|
| 315 |
+
{"metrics": {}, "gpu_specs": gpu_specs}
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
# Get model config for roofline calculations
|
| 319 |
+
config = MODEL_CONFIGS.get(model_name, MODEL_CONFIGS[DEFAULT_MODEL])
|
| 320 |
+
|
| 321 |
# Calculate roofline metrics from results
|
| 322 |
roofline_metrics = calculate_roofline_metrics(
|
| 323 |
results=results,
|
|
|
|
| 329 |
|
| 330 |
table = create_benchmark_results_table(results)
|
| 331 |
insight = create_benchmark_insight(results)
|
| 332 |
+
|
| 333 |
+
# Indicate this is using real model
|
| 334 |
+
model_indicator = " (Real HF Model)" if results.get("using_real_model") else ""
|
| 335 |
+
status = f"✅ Benchmark complete for {model_name}{model_indicator} @ {seq_len_int} tokens"
|
| 336 |
|
| 337 |
# Update roofline with measured data using detected GPU specs
|
| 338 |
roofline = create_roofline_chart(results, gpu_specs, roofline_metrics)
|
|
|
|
| 345 |
|
| 346 |
@spaces.GPU(duration=180)
|
| 347 |
def run_scaling_test(model_name):
|
| 348 |
+
"""Run scaling benchmark across sequence lengths using REAL model."""
|
| 349 |
from src.benchmark import run_scaling_benchmark, create_scaling_chart
|
| 350 |
|
| 351 |
+
# Use REAL MODEL for scaling benchmark
|
|
|
|
| 352 |
results = run_scaling_benchmark(
|
| 353 |
+
model_name=model_name, # Pass model name to load real HF model
|
| 354 |
seq_lengths=[512, 1024, 2048, 4096],
|
|
|
|
|
|
|
| 355 |
batch_size=1,
|
| 356 |
)
|
| 357 |
|
src/attention_utils.py
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Attention layer extraction and benchmarking utilities.
|
| 3 |
+
|
| 4 |
+
Provides functions to:
|
| 5 |
+
- Extract attention layers from HuggingFace models
|
| 6 |
+
- Create proper inputs for attention forward passes
|
| 7 |
+
- Benchmark attention with different SDPA backends
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
from typing import Tuple, Dict, Any, Optional
|
| 13 |
+
from transformers import PreTrainedModel
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def extract_attention_layer(model: PreTrainedModel, layer_idx: int = 0) -> nn.Module:
|
| 17 |
+
"""
|
| 18 |
+
Extract the attention module from a loaded HuggingFace model.
|
| 19 |
+
|
| 20 |
+
Works for common architectures: Llama, Qwen, SmolLM, Mistral, etc.
|
| 21 |
+
These all follow the pattern: model.model.layers[i].self_attn
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
model: Loaded HuggingFace causal LM model
|
| 25 |
+
layer_idx: Which layer to extract (default: 0, first layer)
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
The attention module (nn.Module)
|
| 29 |
+
"""
|
| 30 |
+
# Most decoder-only models follow this pattern
|
| 31 |
+
try:
|
| 32 |
+
attention = model.model.layers[layer_idx].self_attn
|
| 33 |
+
return attention
|
| 34 |
+
except AttributeError:
|
| 35 |
+
# Fallback for different architectures
|
| 36 |
+
if hasattr(model, 'transformer'):
|
| 37 |
+
# GPT-2 style
|
| 38 |
+
return model.transformer.h[layer_idx].attn
|
| 39 |
+
elif hasattr(model, 'gpt_neox'):
|
| 40 |
+
# GPT-NeoX style
|
| 41 |
+
return model.gpt_neox.layers[layer_idx].attention
|
| 42 |
+
else:
|
| 43 |
+
raise ValueError(
|
| 44 |
+
f"Could not extract attention layer from model type: {type(model).__name__}. "
|
| 45 |
+
"Supported architectures: Llama, Qwen, SmolLM, Mistral, GPT-2, GPT-NeoX"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def create_attention_inputs(
|
| 50 |
+
model: PreTrainedModel,
|
| 51 |
+
batch_size: int,
|
| 52 |
+
seq_len: int,
|
| 53 |
+
device: torch.device,
|
| 54 |
+
dtype: torch.dtype = torch.float16,
|
| 55 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 56 |
+
"""
|
| 57 |
+
Create proper inputs for an attention layer forward pass.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
model: The loaded model (to get hidden_size from config)
|
| 61 |
+
batch_size: Batch size
|
| 62 |
+
seq_len: Sequence length
|
| 63 |
+
device: Target device (cuda/cpu)
|
| 64 |
+
dtype: Data type (default: float16)
|
| 65 |
+
|
| 66 |
+
Returns:
|
| 67 |
+
Tuple of (hidden_states, position_ids)
|
| 68 |
+
"""
|
| 69 |
+
hidden_dim = model.config.hidden_size
|
| 70 |
+
|
| 71 |
+
# Hidden states: [batch, seq_len, hidden_dim]
|
| 72 |
+
hidden_states = torch.randn(
|
| 73 |
+
batch_size, seq_len, hidden_dim,
|
| 74 |
+
dtype=dtype, device=device
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Position IDs: [batch, seq_len]
|
| 78 |
+
position_ids = torch.arange(seq_len, device=device).unsqueeze(0).expand(batch_size, -1)
|
| 79 |
+
|
| 80 |
+
return hidden_states, position_ids
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def create_causal_mask(
|
| 84 |
+
seq_len: int,
|
| 85 |
+
device: torch.device,
|
| 86 |
+
dtype: torch.dtype = torch.float16,
|
| 87 |
+
) -> torch.Tensor:
|
| 88 |
+
"""
|
| 89 |
+
Create a causal attention mask.
|
| 90 |
+
|
| 91 |
+
Args:
|
| 92 |
+
seq_len: Sequence length
|
| 93 |
+
device: Target device
|
| 94 |
+
dtype: Data type
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
Causal mask tensor [1, 1, seq_len, seq_len]
|
| 98 |
+
"""
|
| 99 |
+
# Create lower triangular mask (1 = attend, 0 = mask)
|
| 100 |
+
mask = torch.tril(torch.ones(seq_len, seq_len, device=device, dtype=dtype))
|
| 101 |
+
# Convert to attention mask format (0 = attend, -inf = mask)
|
| 102 |
+
mask = mask.masked_fill(mask == 0, float('-inf'))
|
| 103 |
+
mask = mask.masked_fill(mask == 1, 0.0)
|
| 104 |
+
return mask.unsqueeze(0).unsqueeze(0) # [1, 1, seq_len, seq_len]
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def benchmark_attention_layer(
|
| 108 |
+
attention_layer: nn.Module,
|
| 109 |
+
hidden_states: torch.Tensor,
|
| 110 |
+
position_ids: torch.Tensor,
|
| 111 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 112 |
+
backend: str = "flash",
|
| 113 |
+
num_iterations: int = 10,
|
| 114 |
+
warmup_iterations: int = 3,
|
| 115 |
+
) -> Dict[str, Any]:
|
| 116 |
+
"""
|
| 117 |
+
Benchmark an attention layer with a specific SDPA backend.
|
| 118 |
+
|
| 119 |
+
Args:
|
| 120 |
+
attention_layer: The attention module to benchmark
|
| 121 |
+
hidden_states: Input hidden states [batch, seq, hidden_dim]
|
| 122 |
+
position_ids: Position IDs [batch, seq]
|
| 123 |
+
attention_mask: Optional attention mask
|
| 124 |
+
backend: Which SDPA backend ("math", "flash", "mem_efficient")
|
| 125 |
+
num_iterations: Number of timed iterations
|
| 126 |
+
warmup_iterations: Number of warmup iterations
|
| 127 |
+
|
| 128 |
+
Returns:
|
| 129 |
+
Dict with timing and memory results
|
| 130 |
+
"""
|
| 131 |
+
if not torch.cuda.is_available():
|
| 132 |
+
return {"error": "CUDA not available", "status": "error"}
|
| 133 |
+
|
| 134 |
+
# Map backend name to sdp_kernel flags
|
| 135 |
+
backend_flags = {
|
| 136 |
+
"math": (True, False, False), # enable_math, enable_flash, enable_mem_efficient
|
| 137 |
+
"flash": (False, True, False),
|
| 138 |
+
"mem_efficient": (False, False, True),
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
if backend not in backend_flags:
|
| 142 |
+
return {"error": f"Unknown backend: {backend}", "status": "error"}
|
| 143 |
+
|
| 144 |
+
enable_math, enable_flash, enable_mem_efficient = backend_flags[backend]
|
| 145 |
+
|
| 146 |
+
try:
|
| 147 |
+
# Warmup
|
| 148 |
+
with torch.backends.cuda.sdp_kernel(
|
| 149 |
+
enable_flash=enable_flash,
|
| 150 |
+
enable_math=enable_math,
|
| 151 |
+
enable_mem_efficient=enable_mem_efficient
|
| 152 |
+
):
|
| 153 |
+
with torch.no_grad():
|
| 154 |
+
for _ in range(warmup_iterations):
|
| 155 |
+
_ = attention_layer(
|
| 156 |
+
hidden_states,
|
| 157 |
+
position_ids=position_ids,
|
| 158 |
+
attention_mask=attention_mask,
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
torch.cuda.synchronize()
|
| 162 |
+
torch.cuda.reset_peak_memory_stats()
|
| 163 |
+
|
| 164 |
+
# Timed runs
|
| 165 |
+
start = torch.cuda.Event(enable_timing=True)
|
| 166 |
+
end = torch.cuda.Event(enable_timing=True)
|
| 167 |
+
|
| 168 |
+
with torch.backends.cuda.sdp_kernel(
|
| 169 |
+
enable_flash=enable_flash,
|
| 170 |
+
enable_math=enable_math,
|
| 171 |
+
enable_mem_efficient=enable_mem_efficient
|
| 172 |
+
):
|
| 173 |
+
with torch.no_grad():
|
| 174 |
+
start.record()
|
| 175 |
+
for _ in range(num_iterations):
|
| 176 |
+
output = attention_layer(
|
| 177 |
+
hidden_states,
|
| 178 |
+
position_ids=position_ids,
|
| 179 |
+
attention_mask=attention_mask,
|
| 180 |
+
)
|
| 181 |
+
end.record()
|
| 182 |
+
|
| 183 |
+
torch.cuda.synchronize()
|
| 184 |
+
|
| 185 |
+
time_ms = start.elapsed_time(end) / num_iterations
|
| 186 |
+
memory_mb = torch.cuda.max_memory_allocated() / (1024 * 1024)
|
| 187 |
+
|
| 188 |
+
return {
|
| 189 |
+
"time_ms": round(time_ms, 3),
|
| 190 |
+
"memory_mb": round(memory_mb, 1),
|
| 191 |
+
"status": "success",
|
| 192 |
+
"backend": backend,
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
except Exception as e:
|
| 196 |
+
error_msg = str(e)
|
| 197 |
+
# Common error: Flash attention not available on certain GPUs
|
| 198 |
+
if "flash" in error_msg.lower() or "sm75" in error_msg.lower():
|
| 199 |
+
return {
|
| 200 |
+
"time_ms": None,
|
| 201 |
+
"memory_mb": None,
|
| 202 |
+
"status": f"unsupported: {error_msg[:80]}",
|
| 203 |
+
"backend": backend,
|
| 204 |
+
}
|
| 205 |
+
return {
|
| 206 |
+
"time_ms": None,
|
| 207 |
+
"memory_mb": None,
|
| 208 |
+
"status": f"error: {error_msg[:80]}",
|
| 209 |
+
"backend": backend,
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def create_kv_cache(
|
| 214 |
+
model: PreTrainedModel,
|
| 215 |
+
batch_size: int,
|
| 216 |
+
cache_len: int,
|
| 217 |
+
device: torch.device,
|
| 218 |
+
dtype: torch.dtype = torch.float16,
|
| 219 |
+
layer_idx: int = 0,
|
| 220 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 221 |
+
"""
|
| 222 |
+
Create a simulated KV cache for decode-phase benchmarking.
|
| 223 |
+
|
| 224 |
+
Args:
|
| 225 |
+
model: The loaded model (to get config)
|
| 226 |
+
batch_size: Batch size
|
| 227 |
+
cache_len: Number of cached tokens
|
| 228 |
+
device: Target device
|
| 229 |
+
dtype: Data type
|
| 230 |
+
layer_idx: Which layer (for future multi-layer support)
|
| 231 |
+
|
| 232 |
+
Returns:
|
| 233 |
+
Tuple of (key_cache, value_cache), each [batch, num_kv_heads, cache_len, head_dim]
|
| 234 |
+
"""
|
| 235 |
+
config = model.config
|
| 236 |
+
|
| 237 |
+
# Get number of KV heads (for GQA models)
|
| 238 |
+
if hasattr(config, 'num_key_value_heads'):
|
| 239 |
+
num_kv_heads = config.num_key_value_heads
|
| 240 |
+
else:
|
| 241 |
+
num_kv_heads = config.num_attention_heads
|
| 242 |
+
|
| 243 |
+
head_dim = config.hidden_size // config.num_attention_heads
|
| 244 |
+
|
| 245 |
+
# Create KV cache tensors
|
| 246 |
+
key_cache = torch.randn(
|
| 247 |
+
batch_size, num_kv_heads, cache_len, head_dim,
|
| 248 |
+
dtype=dtype, device=device
|
| 249 |
+
)
|
| 250 |
+
value_cache = torch.randn(
|
| 251 |
+
batch_size, num_kv_heads, cache_len, head_dim,
|
| 252 |
+
dtype=dtype, device=device
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
+
return key_cache, value_cache
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def benchmark_decode_attention(
|
| 259 |
+
attention_layer: nn.Module,
|
| 260 |
+
model: PreTrainedModel,
|
| 261 |
+
kv_cache_len: int,
|
| 262 |
+
num_tokens: int = 10,
|
| 263 |
+
batch_size: int = 1,
|
| 264 |
+
backend: str = "flash",
|
| 265 |
+
num_iterations: int = 5,
|
| 266 |
+
) -> Dict[str, Any]:
|
| 267 |
+
"""
|
| 268 |
+
Benchmark decode-phase attention (single query attending to KV cache).
|
| 269 |
+
|
| 270 |
+
Args:
|
| 271 |
+
attention_layer: The attention module
|
| 272 |
+
model: The loaded model (for config)
|
| 273 |
+
kv_cache_len: Length of the KV cache (context)
|
| 274 |
+
num_tokens: Number of decode tokens to simulate
|
| 275 |
+
batch_size: Batch size
|
| 276 |
+
backend: SDPA backend to use
|
| 277 |
+
num_iterations: Iterations per token for averaging
|
| 278 |
+
|
| 279 |
+
Returns:
|
| 280 |
+
Dict with per-token timing and memory stats
|
| 281 |
+
"""
|
| 282 |
+
if not torch.cuda.is_available():
|
| 283 |
+
return {"error": "CUDA not available", "status": "error"}
|
| 284 |
+
|
| 285 |
+
device = torch.device("cuda")
|
| 286 |
+
dtype = torch.float16
|
| 287 |
+
|
| 288 |
+
# Create single-token query input
|
| 289 |
+
hidden_dim = model.config.hidden_size
|
| 290 |
+
query_hidden = torch.randn(batch_size, 1, hidden_dim, dtype=dtype, device=device)
|
| 291 |
+
|
| 292 |
+
# Create KV cache
|
| 293 |
+
key_cache, value_cache = create_kv_cache(
|
| 294 |
+
model, batch_size, kv_cache_len, device, dtype
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
# Position ID for the new token (at position = cache_len)
|
| 298 |
+
position_ids = torch.tensor([[kv_cache_len]], device=device).expand(batch_size, 1)
|
| 299 |
+
|
| 300 |
+
# Backend flags
|
| 301 |
+
backend_flags = {
|
| 302 |
+
"math": (True, False, False),
|
| 303 |
+
"flash": (False, True, False),
|
| 304 |
+
"mem_efficient": (False, False, True),
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
if backend not in backend_flags:
|
| 308 |
+
return {"error": f"Unknown backend: {backend}", "status": "error"}
|
| 309 |
+
|
| 310 |
+
enable_math, enable_flash, enable_mem_efficient = backend_flags[backend]
|
| 311 |
+
|
| 312 |
+
try:
|
| 313 |
+
# Note: For proper decode simulation, we'd need to pass past_key_values
|
| 314 |
+
# This is a simplified version that measures attention with asymmetric Q/KV sizes
|
| 315 |
+
# Real models handle this via the past_key_value mechanism
|
| 316 |
+
|
| 317 |
+
# Warmup
|
| 318 |
+
with torch.backends.cuda.sdp_kernel(
|
| 319 |
+
enable_flash=enable_flash,
|
| 320 |
+
enable_math=enable_math,
|
| 321 |
+
enable_mem_efficient=enable_mem_efficient
|
| 322 |
+
):
|
| 323 |
+
with torch.no_grad():
|
| 324 |
+
for _ in range(2):
|
| 325 |
+
_ = attention_layer(
|
| 326 |
+
query_hidden,
|
| 327 |
+
position_ids=position_ids,
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
torch.cuda.synchronize()
|
| 331 |
+
torch.cuda.reset_peak_memory_stats()
|
| 332 |
+
|
| 333 |
+
# Time multiple tokens
|
| 334 |
+
start = torch.cuda.Event(enable_timing=True)
|
| 335 |
+
end = torch.cuda.Event(enable_timing=True)
|
| 336 |
+
|
| 337 |
+
with torch.backends.cuda.sdp_kernel(
|
| 338 |
+
enable_flash=enable_flash,
|
| 339 |
+
enable_math=enable_math,
|
| 340 |
+
enable_mem_efficient=enable_mem_efficient
|
| 341 |
+
):
|
| 342 |
+
with torch.no_grad():
|
| 343 |
+
start.record()
|
| 344 |
+
for _ in range(num_tokens * num_iterations):
|
| 345 |
+
output = attention_layer(
|
| 346 |
+
query_hidden,
|
| 347 |
+
position_ids=position_ids,
|
| 348 |
+
)
|
| 349 |
+
end.record()
|
| 350 |
+
|
| 351 |
+
torch.cuda.synchronize()
|
| 352 |
+
|
| 353 |
+
total_time_ms = start.elapsed_time(end)
|
| 354 |
+
time_per_token_ms = total_time_ms / (num_tokens * num_iterations)
|
| 355 |
+
memory_mb = torch.cuda.max_memory_allocated() / (1024 * 1024)
|
| 356 |
+
|
| 357 |
+
# Clean up
|
| 358 |
+
del query_hidden, key_cache, value_cache
|
| 359 |
+
torch.cuda.empty_cache()
|
| 360 |
+
|
| 361 |
+
return {
|
| 362 |
+
"time_ms_per_token": round(time_per_token_ms, 4),
|
| 363 |
+
"total_time_ms": round(total_time_ms / num_iterations, 3),
|
| 364 |
+
"memory_mb": round(memory_mb, 1),
|
| 365 |
+
"kv_cache_len": kv_cache_len,
|
| 366 |
+
"num_tokens": num_tokens,
|
| 367 |
+
"status": "success",
|
| 368 |
+
"backend": backend,
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
except Exception as e:
|
| 372 |
+
return {
|
| 373 |
+
"time_ms_per_token": None,
|
| 374 |
+
"total_time_ms": None,
|
| 375 |
+
"memory_mb": None,
|
| 376 |
+
"status": f"error: {str(e)[:80]}",
|
| 377 |
+
"backend": backend,
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
def get_model_attention_info(model: PreTrainedModel) -> Dict[str, Any]:
|
| 382 |
+
"""
|
| 383 |
+
Extract attention-related configuration from a model.
|
| 384 |
+
|
| 385 |
+
Returns:
|
| 386 |
+
Dict with num_heads, num_kv_heads, head_dim, hidden_size, etc.
|
| 387 |
+
"""
|
| 388 |
+
config = model.config
|
| 389 |
+
|
| 390 |
+
num_heads = config.num_attention_heads
|
| 391 |
+
|
| 392 |
+
# GQA models have separate num_key_value_heads
|
| 393 |
+
if hasattr(config, 'num_key_value_heads'):
|
| 394 |
+
num_kv_heads = config.num_key_value_heads
|
| 395 |
+
else:
|
| 396 |
+
num_kv_heads = num_heads
|
| 397 |
+
|
| 398 |
+
head_dim = config.hidden_size // num_heads
|
| 399 |
+
|
| 400 |
+
return {
|
| 401 |
+
"num_attention_heads": num_heads,
|
| 402 |
+
"num_kv_heads": num_kv_heads,
|
| 403 |
+
"head_dim": head_dim,
|
| 404 |
+
"hidden_size": config.hidden_size,
|
| 405 |
+
"num_layers": config.num_hidden_layers,
|
| 406 |
+
"gqa_ratio": num_heads // num_kv_heads if num_kv_heads > 0 else 1,
|
| 407 |
+
"is_gqa": num_kv_heads < num_heads,
|
| 408 |
+
}
|
src/benchmark.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Benchmark module for FlashAttention Explorer.
|
| 3 |
-
GPU benchmark functions for comparing attention backends.
|
| 4 |
"""
|
| 5 |
|
| 6 |
import torch
|
|
@@ -9,7 +9,14 @@ import numpy as np
|
|
| 9 |
import plotly.graph_objects as go
|
| 10 |
from plotly.subplots import make_subplots
|
| 11 |
|
| 12 |
-
from .constants import GPU_SPECS, ATTENTION_BACKENDS, MODEL_CONFIGS, DEFAULT_GPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
def detect_gpu() -> dict:
|
|
@@ -152,23 +159,27 @@ def detect_gpu() -> dict:
|
|
| 152 |
|
| 153 |
|
| 154 |
def run_attention_benchmark(
|
|
|
|
| 155 |
seq_len: int = 1024,
|
| 156 |
-
num_heads: int = 16,
|
| 157 |
-
head_dim: int = 64,
|
| 158 |
batch_size: int = 1,
|
| 159 |
num_iterations: int = 10,
|
| 160 |
warmup_iterations: int = 3,
|
|
|
|
|
|
|
|
|
|
| 161 |
) -> dict:
|
| 162 |
"""
|
| 163 |
-
Benchmark three SDPA backends
|
| 164 |
|
| 165 |
Args:
|
|
|
|
|
|
|
| 166 |
seq_len: Sequence length (number of tokens)
|
| 167 |
-
num_heads: Number of attention heads
|
| 168 |
-
head_dim: Dimension per head
|
| 169 |
batch_size: Batch size
|
| 170 |
num_iterations: Number of timed iterations
|
| 171 |
warmup_iterations: Number of warmup iterations
|
|
|
|
|
|
|
| 172 |
|
| 173 |
Returns:
|
| 174 |
Dict with timing and memory results per backend
|
|
@@ -179,13 +190,62 @@ def run_attention_benchmark(
|
|
| 179 |
device = torch.device("cuda")
|
| 180 |
dtype = torch.float16
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
# Create input tensors
|
| 183 |
Q = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 184 |
K = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 185 |
V = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 186 |
|
| 187 |
-
results = {}
|
| 188 |
-
|
| 189 |
# Test each backend
|
| 190 |
backends = [
|
| 191 |
("math", True, False, False),
|
|
@@ -238,7 +298,7 @@ def run_attention_benchmark(
|
|
| 238 |
if results.get("math", {}).get("time_ms"):
|
| 239 |
base_time = results["math"]["time_ms"]
|
| 240 |
for backend in results:
|
| 241 |
-
if results[backend].get("time_ms"):
|
| 242 |
results[backend]["speedup"] = round(base_time / results[backend]["time_ms"], 2)
|
| 243 |
|
| 244 |
# Clean up
|
|
@@ -249,13 +309,22 @@ def run_attention_benchmark(
|
|
| 249 |
|
| 250 |
|
| 251 |
def run_scaling_benchmark(
|
|
|
|
| 252 |
seq_lengths: list = None,
|
|
|
|
|
|
|
| 253 |
num_heads: int = 16,
|
| 254 |
head_dim: int = 64,
|
| 255 |
-
batch_size: int = 1,
|
| 256 |
) -> dict:
|
| 257 |
"""
|
| 258 |
-
Benchmark attention backends across multiple sequence lengths.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
Returns:
|
| 261 |
Dict with arrays of timing and memory results for each backend
|
|
@@ -268,6 +337,7 @@ def run_scaling_benchmark(
|
|
| 268 |
|
| 269 |
results = {
|
| 270 |
"seq_lengths": seq_lengths,
|
|
|
|
| 271 |
"math": {"time_ms": [], "memory_mb": []},
|
| 272 |
"flash": {"time_ms": [], "memory_mb": []},
|
| 273 |
"mem_efficient": {"time_ms": [], "memory_mb": []},
|
|
@@ -275,12 +345,14 @@ def run_scaling_benchmark(
|
|
| 275 |
|
| 276 |
for seq_len in seq_lengths:
|
| 277 |
bench_result = run_attention_benchmark(
|
|
|
|
| 278 |
seq_len=seq_len,
|
| 279 |
-
num_heads=num_heads,
|
| 280 |
-
head_dim=head_dim,
|
| 281 |
batch_size=batch_size,
|
| 282 |
num_iterations=5, # Fewer iterations for scaling test
|
| 283 |
warmup_iterations=2,
|
|
|
|
|
|
|
|
|
|
| 284 |
)
|
| 285 |
|
| 286 |
for backend in ["math", "flash", "mem_efficient"]:
|
|
|
|
| 1 |
"""
|
| 2 |
Benchmark module for FlashAttention Explorer.
|
| 3 |
+
GPU benchmark functions for comparing attention backends using real HuggingFace models.
|
| 4 |
"""
|
| 5 |
|
| 6 |
import torch
|
|
|
|
| 9 |
import plotly.graph_objects as go
|
| 10 |
from plotly.subplots import make_subplots
|
| 11 |
|
| 12 |
+
from .constants import GPU_SPECS, ATTENTION_BACKENDS, MODEL_CONFIGS, DEFAULT_GPU, DEFAULT_MODEL
|
| 13 |
+
from .models import load_model, clear_model_cache
|
| 14 |
+
from .attention_utils import (
|
| 15 |
+
extract_attention_layer,
|
| 16 |
+
create_attention_inputs,
|
| 17 |
+
benchmark_attention_layer,
|
| 18 |
+
get_model_attention_info,
|
| 19 |
+
)
|
| 20 |
|
| 21 |
|
| 22 |
def detect_gpu() -> dict:
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
def run_attention_benchmark(
|
| 162 |
+
model_name: str = None,
|
| 163 |
seq_len: int = 1024,
|
|
|
|
|
|
|
| 164 |
batch_size: int = 1,
|
| 165 |
num_iterations: int = 10,
|
| 166 |
warmup_iterations: int = 3,
|
| 167 |
+
# Legacy parameters (used if model_name is None)
|
| 168 |
+
num_heads: int = 16,
|
| 169 |
+
head_dim: int = 64,
|
| 170 |
) -> dict:
|
| 171 |
"""
|
| 172 |
+
Benchmark three SDPA backends using a real HuggingFace model's attention layer.
|
| 173 |
|
| 174 |
Args:
|
| 175 |
+
model_name: Name of the model from MODEL_CONFIGS (e.g., "SmolLM2-360M")
|
| 176 |
+
If None, falls back to legacy random tensor mode
|
| 177 |
seq_len: Sequence length (number of tokens)
|
|
|
|
|
|
|
| 178 |
batch_size: Batch size
|
| 179 |
num_iterations: Number of timed iterations
|
| 180 |
warmup_iterations: Number of warmup iterations
|
| 181 |
+
num_heads: (Legacy) Number of attention heads if model_name is None
|
| 182 |
+
head_dim: (Legacy) Dimension per head if model_name is None
|
| 183 |
|
| 184 |
Returns:
|
| 185 |
Dict with timing and memory results per backend
|
|
|
|
| 190 |
device = torch.device("cuda")
|
| 191 |
dtype = torch.float16
|
| 192 |
|
| 193 |
+
# If model_name is provided, use real model attention layer
|
| 194 |
+
if model_name is not None and model_name in MODEL_CONFIGS:
|
| 195 |
+
try:
|
| 196 |
+
# Load the real HuggingFace model
|
| 197 |
+
model = load_model(model_name)
|
| 198 |
+
|
| 199 |
+
# Extract attention layer from layer 0
|
| 200 |
+
attention_layer = extract_attention_layer(model, layer_idx=0)
|
| 201 |
+
|
| 202 |
+
# Get model attention info
|
| 203 |
+
attn_info = get_model_attention_info(model)
|
| 204 |
+
|
| 205 |
+
# Create proper inputs for the attention layer
|
| 206 |
+
hidden_states, position_ids = create_attention_inputs(
|
| 207 |
+
model, batch_size, seq_len, device, dtype
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
results = {"model_name": model_name, "using_real_model": True}
|
| 211 |
+
results["model_info"] = attn_info
|
| 212 |
+
|
| 213 |
+
# Benchmark each backend using the real attention layer
|
| 214 |
+
for backend in ["math", "flash", "mem_efficient"]:
|
| 215 |
+
result = benchmark_attention_layer(
|
| 216 |
+
attention_layer=attention_layer,
|
| 217 |
+
hidden_states=hidden_states,
|
| 218 |
+
position_ids=position_ids,
|
| 219 |
+
backend=backend,
|
| 220 |
+
num_iterations=num_iterations,
|
| 221 |
+
warmup_iterations=warmup_iterations,
|
| 222 |
+
)
|
| 223 |
+
results[backend] = result
|
| 224 |
+
|
| 225 |
+
# Clean up inputs
|
| 226 |
+
del hidden_states, position_ids
|
| 227 |
+
torch.cuda.empty_cache()
|
| 228 |
+
|
| 229 |
+
# Calculate speedups
|
| 230 |
+
if results.get("math", {}).get("time_ms"):
|
| 231 |
+
base_time = results["math"]["time_ms"]
|
| 232 |
+
for backend in ["math", "flash", "mem_efficient"]:
|
| 233 |
+
if results.get(backend, {}).get("time_ms"):
|
| 234 |
+
results[backend]["speedup"] = round(base_time / results[backend]["time_ms"], 2)
|
| 235 |
+
|
| 236 |
+
return results
|
| 237 |
+
|
| 238 |
+
except Exception as e:
|
| 239 |
+
return {"error": f"Failed to load model: {str(e)[:100]}"}
|
| 240 |
+
|
| 241 |
+
# Legacy mode: Use raw SDPA with random tensors (fallback)
|
| 242 |
+
results = {"using_real_model": False}
|
| 243 |
+
|
| 244 |
# Create input tensors
|
| 245 |
Q = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 246 |
K = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 247 |
V = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 248 |
|
|
|
|
|
|
|
| 249 |
# Test each backend
|
| 250 |
backends = [
|
| 251 |
("math", True, False, False),
|
|
|
|
| 298 |
if results.get("math", {}).get("time_ms"):
|
| 299 |
base_time = results["math"]["time_ms"]
|
| 300 |
for backend in results:
|
| 301 |
+
if isinstance(results[backend], dict) and results[backend].get("time_ms"):
|
| 302 |
results[backend]["speedup"] = round(base_time / results[backend]["time_ms"], 2)
|
| 303 |
|
| 304 |
# Clean up
|
|
|
|
| 309 |
|
| 310 |
|
| 311 |
def run_scaling_benchmark(
|
| 312 |
+
model_name: str = None,
|
| 313 |
seq_lengths: list = None,
|
| 314 |
+
batch_size: int = 1,
|
| 315 |
+
# Legacy parameters (used if model_name is None)
|
| 316 |
num_heads: int = 16,
|
| 317 |
head_dim: int = 64,
|
|
|
|
| 318 |
) -> dict:
|
| 319 |
"""
|
| 320 |
+
Benchmark attention backends across multiple sequence lengths using a real model.
|
| 321 |
+
|
| 322 |
+
Args:
|
| 323 |
+
model_name: Name of the model from MODEL_CONFIGS (e.g., "SmolLM2-360M")
|
| 324 |
+
seq_lengths: List of sequence lengths to test
|
| 325 |
+
batch_size: Batch size
|
| 326 |
+
num_heads: (Legacy) Number of attention heads if model_name is None
|
| 327 |
+
head_dim: (Legacy) Dimension per head if model_name is None
|
| 328 |
|
| 329 |
Returns:
|
| 330 |
Dict with arrays of timing and memory results for each backend
|
|
|
|
| 337 |
|
| 338 |
results = {
|
| 339 |
"seq_lengths": seq_lengths,
|
| 340 |
+
"model_name": model_name,
|
| 341 |
"math": {"time_ms": [], "memory_mb": []},
|
| 342 |
"flash": {"time_ms": [], "memory_mb": []},
|
| 343 |
"mem_efficient": {"time_ms": [], "memory_mb": []},
|
|
|
|
| 345 |
|
| 346 |
for seq_len in seq_lengths:
|
| 347 |
bench_result = run_attention_benchmark(
|
| 348 |
+
model_name=model_name,
|
| 349 |
seq_len=seq_len,
|
|
|
|
|
|
|
| 350 |
batch_size=batch_size,
|
| 351 |
num_iterations=5, # Fewer iterations for scaling test
|
| 352 |
warmup_iterations=2,
|
| 353 |
+
# Legacy params (ignored if model_name is set)
|
| 354 |
+
num_heads=num_heads,
|
| 355 |
+
head_dim=head_dim,
|
| 356 |
)
|
| 357 |
|
| 358 |
for backend in ["math", "flash", "mem_efficient"]:
|
src/prefill_decode.py
CHANGED
|
@@ -4,6 +4,8 @@ Prefill vs Decode phase comparison module.
|
|
| 4 |
Demonstrates the key difference between:
|
| 5 |
- Prefill: Process entire prompt in parallel (N² attention complexity)
|
| 6 |
- Decode: Generate one token at a time (N attention per token, but sequential)
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
import torch
|
|
@@ -13,8 +15,185 @@ import plotly.graph_objects as go
|
|
| 13 |
from plotly.subplots import make_subplots
|
| 14 |
|
| 15 |
from .constants import MODEL_CONFIGS, ATTENTION_BACKENDS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
|
|
|
| 18 |
def simulate_prefill_attention(
|
| 19 |
batch_size: int,
|
| 20 |
num_heads: int,
|
|
@@ -24,13 +203,8 @@ def simulate_prefill_attention(
|
|
| 24 |
use_flash: bool = True,
|
| 25 |
) -> dict:
|
| 26 |
"""
|
| 27 |
-
Simulate prefill phase attention.
|
| 28 |
-
|
| 29 |
-
Prefill processes the entire prompt at once:
|
| 30 |
-
- Q, K, V all have shape [batch, heads, seq_len, head_dim]
|
| 31 |
-
- Full N×N attention matrix computed
|
| 32 |
-
|
| 33 |
-
Returns timing and memory stats.
|
| 34 |
"""
|
| 35 |
if not torch.cuda.is_available():
|
| 36 |
return {"error": "CUDA not available"}
|
|
@@ -38,40 +212,39 @@ def simulate_prefill_attention(
|
|
| 38 |
device = torch.device("cuda")
|
| 39 |
dtype = torch.float16
|
| 40 |
|
| 41 |
-
# Create tensors for full sequence
|
| 42 |
Q = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 43 |
K = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 44 |
V = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# Warmup
|
| 47 |
for _ in range(2):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
except Exception:
|
| 53 |
-
_ = F.scaled_dot_product_attention(Q, K, V)
|
| 54 |
-
else:
|
| 55 |
-
with torch.backends.cuda.sdp_kernel(enable_flash=False, enable_math=True, enable_mem_efficient=False):
|
| 56 |
_ = F.scaled_dot_product_attention(Q, K, V)
|
|
|
|
|
|
|
| 57 |
|
| 58 |
torch.cuda.synchronize()
|
| 59 |
torch.cuda.reset_peak_memory_stats()
|
| 60 |
|
| 61 |
-
# Timed iterations
|
| 62 |
start = torch.cuda.Event(enable_timing=True)
|
| 63 |
end = torch.cuda.Event(enable_timing=True)
|
| 64 |
|
| 65 |
start.record()
|
| 66 |
for _ in range(num_iterations):
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
else:
|
| 74 |
-
with torch.backends.cuda.sdp_kernel(enable_flash=False, enable_math=True, enable_mem_efficient=False):
|
| 75 |
output = F.scaled_dot_product_attention(Q, K, V)
|
| 76 |
end.record()
|
| 77 |
|
|
@@ -81,7 +254,6 @@ def simulate_prefill_attention(
|
|
| 81 |
avg_time_ms = total_time_ms / num_iterations
|
| 82 |
peak_memory_mb = torch.cuda.max_memory_allocated() / (1024 * 1024)
|
| 83 |
|
| 84 |
-
# Clean up
|
| 85 |
del Q, K, V, output
|
| 86 |
torch.cuda.empty_cache()
|
| 87 |
|
|
@@ -93,6 +265,7 @@ def simulate_prefill_attention(
|
|
| 93 |
}
|
| 94 |
|
| 95 |
|
|
|
|
| 96 |
def simulate_decode_attention(
|
| 97 |
batch_size: int,
|
| 98 |
num_heads: int,
|
|
@@ -102,14 +275,8 @@ def simulate_decode_attention(
|
|
| 102 |
use_flash: bool = True,
|
| 103 |
) -> dict:
|
| 104 |
"""
|
| 105 |
-
Simulate decode phase attention.
|
| 106 |
-
|
| 107 |
-
Decode generates one token at a time:
|
| 108 |
-
- Q has shape [batch, heads, 1, head_dim] (single new token)
|
| 109 |
-
- K, V have shape [batch, heads, kv_cache_len, head_dim] (all past tokens)
|
| 110 |
-
- Attention is 1×N (much smaller than N×N)
|
| 111 |
-
|
| 112 |
-
Returns timing and memory stats.
|
| 113 |
"""
|
| 114 |
if not torch.cuda.is_available():
|
| 115 |
return {"error": "CUDA not available"}
|
|
@@ -117,46 +284,40 @@ def simulate_decode_attention(
|
|
| 117 |
device = torch.device("cuda")
|
| 118 |
dtype = torch.float16
|
| 119 |
|
| 120 |
-
# Create KV cache (simulating past tokens)
|
| 121 |
K_cache = torch.randn(batch_size, num_heads, kv_cache_len, head_dim, device=device, dtype=dtype)
|
| 122 |
V_cache = torch.randn(batch_size, num_heads, kv_cache_len, head_dim, device=device, dtype=dtype)
|
| 123 |
-
|
| 124 |
-
# Single query token
|
| 125 |
Q = torch.randn(batch_size, num_heads, 1, head_dim, device=device, dtype=dtype)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
# Warmup
|
| 128 |
for _ in range(2):
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
except Exception:
|
| 134 |
-
_ = F.scaled_dot_product_attention(Q, K_cache, V_cache)
|
| 135 |
-
else:
|
| 136 |
-
with torch.backends.cuda.sdp_kernel(enable_flash=False, enable_math=True, enable_mem_efficient=False):
|
| 137 |
_ = F.scaled_dot_product_attention(Q, K_cache, V_cache)
|
|
|
|
|
|
|
| 138 |
|
| 139 |
torch.cuda.synchronize()
|
| 140 |
torch.cuda.reset_peak_memory_stats()
|
| 141 |
|
| 142 |
-
# Simulate generating num_tokens
|
| 143 |
start = torch.cuda.Event(enable_timing=True)
|
| 144 |
end = torch.cuda.Event(enable_timing=True)
|
| 145 |
|
| 146 |
start.record()
|
| 147 |
-
for
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
else:
|
| 155 |
-
with torch.backends.cuda.sdp_kernel(enable_flash=False, enable_math=True, enable_mem_efficient=False):
|
| 156 |
output = F.scaled_dot_product_attention(Q, K_cache, V_cache)
|
| 157 |
-
|
| 158 |
-
# In real decode, we'd append to KV cache here
|
| 159 |
-
# For timing purposes, we keep cache size fixed
|
| 160 |
end.record()
|
| 161 |
|
| 162 |
torch.cuda.synchronize()
|
|
@@ -165,7 +326,6 @@ def simulate_decode_attention(
|
|
| 165 |
avg_time_per_token_ms = total_time_ms / num_tokens
|
| 166 |
peak_memory_mb = torch.cuda.max_memory_allocated() / (1024 * 1024)
|
| 167 |
|
| 168 |
-
# Clean up
|
| 169 |
del Q, K_cache, V_cache, output
|
| 170 |
torch.cuda.empty_cache()
|
| 171 |
|
|
@@ -185,7 +345,10 @@ def run_prefill_decode_comparison(
|
|
| 185 |
decode_tokens: int = 32,
|
| 186 |
) -> tuple:
|
| 187 |
"""
|
| 188 |
-
Run full comparison between prefill and decode phases.
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
Returns results dict, comparison chart, KV cache chart, and insight text.
|
| 191 |
"""
|
|
@@ -193,53 +356,86 @@ def run_prefill_decode_comparison(
|
|
| 193 |
return {"error": f"Unknown model: {model_name}"}, None, None, "Error: Unknown model"
|
| 194 |
|
| 195 |
config = MODEL_CONFIGS[model_name]
|
| 196 |
-
num_heads = config["q_heads"]
|
| 197 |
-
kv_heads = config["kv_heads"]
|
| 198 |
-
head_dim = config["head_dim"]
|
| 199 |
-
num_layers = config["layers"]
|
| 200 |
|
| 201 |
results = {
|
| 202 |
"model": model_name,
|
| 203 |
"context_length": context_length,
|
| 204 |
"decode_tokens": decode_tokens,
|
| 205 |
"config": config,
|
|
|
|
| 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 |
results["prefill"] = {
|
| 245 |
"flash": prefill_flash,
|
|
@@ -259,6 +455,11 @@ def run_prefill_decode_comparison(
|
|
| 259 |
# Generate insight
|
| 260 |
insight = generate_phase_insight(results)
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
return results, comparison_chart, kv_cache_chart, insight
|
| 263 |
|
| 264 |
|
|
|
|
| 4 |
Demonstrates the key difference between:
|
| 5 |
- Prefill: Process entire prompt in parallel (N² attention complexity)
|
| 6 |
- Decode: Generate one token at a time (N attention per token, but sequential)
|
| 7 |
+
|
| 8 |
+
Uses REAL HuggingFace model attention layers for accurate benchmarking.
|
| 9 |
"""
|
| 10 |
|
| 11 |
import torch
|
|
|
|
| 15 |
from plotly.subplots import make_subplots
|
| 16 |
|
| 17 |
from .constants import MODEL_CONFIGS, ATTENTION_BACKENDS
|
| 18 |
+
from .models import load_model
|
| 19 |
+
from .attention_utils import (
|
| 20 |
+
extract_attention_layer,
|
| 21 |
+
create_attention_inputs,
|
| 22 |
+
benchmark_attention_layer,
|
| 23 |
+
get_model_attention_info,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def run_prefill_with_real_model(
|
| 28 |
+
model,
|
| 29 |
+
attention_layer,
|
| 30 |
+
seq_len: int,
|
| 31 |
+
batch_size: int = 1,
|
| 32 |
+
num_iterations: int = 5,
|
| 33 |
+
use_flash: bool = True,
|
| 34 |
+
) -> dict:
|
| 35 |
+
"""
|
| 36 |
+
Run prefill phase attention using a REAL model's attention layer.
|
| 37 |
+
|
| 38 |
+
Prefill processes the entire prompt at once:
|
| 39 |
+
- Hidden states have shape [batch, seq_len, hidden_dim]
|
| 40 |
+
- Full N×N attention matrix computed via the real attention layer
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
model: Loaded HuggingFace model
|
| 44 |
+
attention_layer: Extracted attention module
|
| 45 |
+
seq_len: Sequence length
|
| 46 |
+
batch_size: Batch size
|
| 47 |
+
num_iterations: Number of timed iterations
|
| 48 |
+
use_flash: Whether to use FlashAttention backend
|
| 49 |
+
|
| 50 |
+
Returns:
|
| 51 |
+
Dict with timing and memory stats
|
| 52 |
+
"""
|
| 53 |
+
if not torch.cuda.is_available():
|
| 54 |
+
return {"error": "CUDA not available"}
|
| 55 |
+
|
| 56 |
+
device = torch.device("cuda")
|
| 57 |
+
dtype = torch.float16
|
| 58 |
+
|
| 59 |
+
# Create proper inputs for the attention layer
|
| 60 |
+
hidden_states, position_ids = create_attention_inputs(
|
| 61 |
+
model, batch_size, seq_len, device, dtype
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Backend configuration
|
| 65 |
+
backend = "flash" if use_flash else "math"
|
| 66 |
+
|
| 67 |
+
# Run benchmark using the utility function
|
| 68 |
+
result = benchmark_attention_layer(
|
| 69 |
+
attention_layer=attention_layer,
|
| 70 |
+
hidden_states=hidden_states,
|
| 71 |
+
position_ids=position_ids,
|
| 72 |
+
backend=backend,
|
| 73 |
+
num_iterations=num_iterations,
|
| 74 |
+
warmup_iterations=2,
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Clean up
|
| 78 |
+
del hidden_states, position_ids
|
| 79 |
+
torch.cuda.empty_cache()
|
| 80 |
+
|
| 81 |
+
# Add phase info to result
|
| 82 |
+
result["seq_len"] = seq_len
|
| 83 |
+
result["phase"] = "prefill"
|
| 84 |
+
result["using_real_model"] = True
|
| 85 |
+
|
| 86 |
+
return result
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def run_decode_with_real_model(
|
| 90 |
+
model,
|
| 91 |
+
attention_layer,
|
| 92 |
+
kv_cache_len: int,
|
| 93 |
+
num_tokens: int = 10,
|
| 94 |
+
batch_size: int = 1,
|
| 95 |
+
num_iterations: int = 3,
|
| 96 |
+
use_flash: bool = True,
|
| 97 |
+
) -> dict:
|
| 98 |
+
"""
|
| 99 |
+
Run decode phase attention using a REAL model's attention layer.
|
| 100 |
+
|
| 101 |
+
Decode generates one token at a time:
|
| 102 |
+
- Single query token attending to all past keys/values
|
| 103 |
+
- Simulates the memory-bound decode phase
|
| 104 |
+
|
| 105 |
+
Args:
|
| 106 |
+
model: Loaded HuggingFace model
|
| 107 |
+
attention_layer: Extracted attention module
|
| 108 |
+
kv_cache_len: Length of the KV cache (context)
|
| 109 |
+
num_tokens: Number of tokens to simulate generating
|
| 110 |
+
batch_size: Batch size
|
| 111 |
+
num_iterations: Iterations for averaging
|
| 112 |
+
use_flash: Whether to use FlashAttention backend
|
| 113 |
+
|
| 114 |
+
Returns:
|
| 115 |
+
Dict with per-token timing and memory stats
|
| 116 |
+
"""
|
| 117 |
+
if not torch.cuda.is_available():
|
| 118 |
+
return {"error": "CUDA not available"}
|
| 119 |
+
|
| 120 |
+
device = torch.device("cuda")
|
| 121 |
+
dtype = torch.float16
|
| 122 |
+
|
| 123 |
+
# Create single-token query input (simulating decode)
|
| 124 |
+
hidden_dim = model.config.hidden_size
|
| 125 |
+
query_hidden = torch.randn(batch_size, 1, hidden_dim, dtype=dtype, device=device)
|
| 126 |
+
position_ids = torch.tensor([[kv_cache_len]], device=device).expand(batch_size, 1)
|
| 127 |
+
|
| 128 |
+
# Backend flags
|
| 129 |
+
if use_flash:
|
| 130 |
+
enable_math, enable_flash, enable_mem_efficient = False, True, False
|
| 131 |
+
else:
|
| 132 |
+
enable_math, enable_flash, enable_mem_efficient = True, False, False
|
| 133 |
+
|
| 134 |
+
try:
|
| 135 |
+
# Warmup
|
| 136 |
+
with torch.backends.cuda.sdp_kernel(
|
| 137 |
+
enable_flash=enable_flash,
|
| 138 |
+
enable_math=enable_math,
|
| 139 |
+
enable_mem_efficient=enable_mem_efficient
|
| 140 |
+
):
|
| 141 |
+
with torch.no_grad():
|
| 142 |
+
for _ in range(2):
|
| 143 |
+
_ = attention_layer(query_hidden, position_ids=position_ids)
|
| 144 |
+
|
| 145 |
+
torch.cuda.synchronize()
|
| 146 |
+
torch.cuda.reset_peak_memory_stats()
|
| 147 |
+
|
| 148 |
+
# Time multiple tokens
|
| 149 |
+
start = torch.cuda.Event(enable_timing=True)
|
| 150 |
+
end = torch.cuda.Event(enable_timing=True)
|
| 151 |
+
|
| 152 |
+
with torch.backends.cuda.sdp_kernel(
|
| 153 |
+
enable_flash=enable_flash,
|
| 154 |
+
enable_math=enable_math,
|
| 155 |
+
enable_mem_efficient=enable_mem_efficient
|
| 156 |
+
):
|
| 157 |
+
with torch.no_grad():
|
| 158 |
+
start.record()
|
| 159 |
+
for _ in range(num_tokens * num_iterations):
|
| 160 |
+
output = attention_layer(query_hidden, position_ids=position_ids)
|
| 161 |
+
end.record()
|
| 162 |
+
|
| 163 |
+
torch.cuda.synchronize()
|
| 164 |
+
|
| 165 |
+
total_time_ms = start.elapsed_time(end)
|
| 166 |
+
time_per_token_ms = total_time_ms / (num_tokens * num_iterations)
|
| 167 |
+
memory_mb = torch.cuda.max_memory_allocated() / (1024 * 1024)
|
| 168 |
+
|
| 169 |
+
# Clean up
|
| 170 |
+
del query_hidden
|
| 171 |
+
torch.cuda.empty_cache()
|
| 172 |
+
|
| 173 |
+
return {
|
| 174 |
+
"time_ms_per_token": round(time_per_token_ms, 4),
|
| 175 |
+
"total_time_ms": round(total_time_ms / num_iterations, 3),
|
| 176 |
+
"memory_mb": round(memory_mb, 1),
|
| 177 |
+
"kv_cache_len": kv_cache_len,
|
| 178 |
+
"num_tokens": num_tokens,
|
| 179 |
+
"phase": "decode",
|
| 180 |
+
"using_real_model": True,
|
| 181 |
+
"status": "success",
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
except Exception as e:
|
| 185 |
+
return {
|
| 186 |
+
"time_ms_per_token": 0,
|
| 187 |
+
"total_time_ms": 0,
|
| 188 |
+
"memory_mb": 0,
|
| 189 |
+
"kv_cache_len": kv_cache_len,
|
| 190 |
+
"num_tokens": num_tokens,
|
| 191 |
+
"phase": "decode",
|
| 192 |
+
"status": f"error: {str(e)[:80]}",
|
| 193 |
+
}
|
| 194 |
|
| 195 |
|
| 196 |
+
# Legacy function kept for backwards compatibility
|
| 197 |
def simulate_prefill_attention(
|
| 198 |
batch_size: int,
|
| 199 |
num_heads: int,
|
|
|
|
| 203 |
use_flash: bool = True,
|
| 204 |
) -> dict:
|
| 205 |
"""
|
| 206 |
+
Legacy: Simulate prefill phase attention with random tensors.
|
| 207 |
+
Use run_prefill_with_real_model() for real model benchmarks.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
"""
|
| 209 |
if not torch.cuda.is_available():
|
| 210 |
return {"error": "CUDA not available"}
|
|
|
|
| 212 |
device = torch.device("cuda")
|
| 213 |
dtype = torch.float16
|
| 214 |
|
|
|
|
| 215 |
Q = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 216 |
K = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 217 |
V = torch.randn(batch_size, num_heads, seq_len, head_dim, device=device, dtype=dtype)
|
| 218 |
|
| 219 |
+
if use_flash:
|
| 220 |
+
enable_math, enable_flash_flag, enable_mem_efficient = False, True, False
|
| 221 |
+
else:
|
| 222 |
+
enable_math, enable_flash_flag, enable_mem_efficient = True, False, False
|
| 223 |
+
|
| 224 |
# Warmup
|
| 225 |
for _ in range(2):
|
| 226 |
+
with torch.backends.cuda.sdp_kernel(
|
| 227 |
+
enable_flash=enable_flash_flag, enable_math=enable_math, enable_mem_efficient=enable_mem_efficient
|
| 228 |
+
):
|
| 229 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
_ = F.scaled_dot_product_attention(Q, K, V)
|
| 231 |
+
except Exception:
|
| 232 |
+
pass
|
| 233 |
|
| 234 |
torch.cuda.synchronize()
|
| 235 |
torch.cuda.reset_peak_memory_stats()
|
| 236 |
|
|
|
|
| 237 |
start = torch.cuda.Event(enable_timing=True)
|
| 238 |
end = torch.cuda.Event(enable_timing=True)
|
| 239 |
|
| 240 |
start.record()
|
| 241 |
for _ in range(num_iterations):
|
| 242 |
+
with torch.backends.cuda.sdp_kernel(
|
| 243 |
+
enable_flash=enable_flash_flag, enable_math=enable_math, enable_mem_efficient=enable_mem_efficient
|
| 244 |
+
):
|
| 245 |
+
try:
|
| 246 |
+
output = F.scaled_dot_product_attention(Q, K, V)
|
| 247 |
+
except Exception:
|
|
|
|
|
|
|
| 248 |
output = F.scaled_dot_product_attention(Q, K, V)
|
| 249 |
end.record()
|
| 250 |
|
|
|
|
| 254 |
avg_time_ms = total_time_ms / num_iterations
|
| 255 |
peak_memory_mb = torch.cuda.max_memory_allocated() / (1024 * 1024)
|
| 256 |
|
|
|
|
| 257 |
del Q, K, V, output
|
| 258 |
torch.cuda.empty_cache()
|
| 259 |
|
|
|
|
| 265 |
}
|
| 266 |
|
| 267 |
|
| 268 |
+
# Legacy function kept for backwards compatibility
|
| 269 |
def simulate_decode_attention(
|
| 270 |
batch_size: int,
|
| 271 |
num_heads: int,
|
|
|
|
| 275 |
use_flash: bool = True,
|
| 276 |
) -> dict:
|
| 277 |
"""
|
| 278 |
+
Legacy: Simulate decode phase attention with random tensors.
|
| 279 |
+
Use run_decode_with_real_model() for real model benchmarks.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
"""
|
| 281 |
if not torch.cuda.is_available():
|
| 282 |
return {"error": "CUDA not available"}
|
|
|
|
| 284 |
device = torch.device("cuda")
|
| 285 |
dtype = torch.float16
|
| 286 |
|
|
|
|
| 287 |
K_cache = torch.randn(batch_size, num_heads, kv_cache_len, head_dim, device=device, dtype=dtype)
|
| 288 |
V_cache = torch.randn(batch_size, num_heads, kv_cache_len, head_dim, device=device, dtype=dtype)
|
|
|
|
|
|
|
| 289 |
Q = torch.randn(batch_size, num_heads, 1, head_dim, device=device, dtype=dtype)
|
| 290 |
|
| 291 |
+
if use_flash:
|
| 292 |
+
enable_math, enable_flash_flag, enable_mem_efficient = False, True, False
|
| 293 |
+
else:
|
| 294 |
+
enable_math, enable_flash_flag, enable_mem_efficient = True, False, False
|
| 295 |
+
|
| 296 |
# Warmup
|
| 297 |
for _ in range(2):
|
| 298 |
+
with torch.backends.cuda.sdp_kernel(
|
| 299 |
+
enable_flash=enable_flash_flag, enable_math=enable_math, enable_mem_efficient=enable_mem_efficient
|
| 300 |
+
):
|
| 301 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
_ = F.scaled_dot_product_attention(Q, K_cache, V_cache)
|
| 303 |
+
except Exception:
|
| 304 |
+
pass
|
| 305 |
|
| 306 |
torch.cuda.synchronize()
|
| 307 |
torch.cuda.reset_peak_memory_stats()
|
| 308 |
|
|
|
|
| 309 |
start = torch.cuda.Event(enable_timing=True)
|
| 310 |
end = torch.cuda.Event(enable_timing=True)
|
| 311 |
|
| 312 |
start.record()
|
| 313 |
+
for _ in range(num_tokens):
|
| 314 |
+
with torch.backends.cuda.sdp_kernel(
|
| 315 |
+
enable_flash=enable_flash_flag, enable_math=enable_math, enable_mem_efficient=enable_mem_efficient
|
| 316 |
+
):
|
| 317 |
+
try:
|
| 318 |
+
output = F.scaled_dot_product_attention(Q, K_cache, V_cache)
|
| 319 |
+
except Exception:
|
|
|
|
|
|
|
| 320 |
output = F.scaled_dot_product_attention(Q, K_cache, V_cache)
|
|
|
|
|
|
|
|
|
|
| 321 |
end.record()
|
| 322 |
|
| 323 |
torch.cuda.synchronize()
|
|
|
|
| 326 |
avg_time_per_token_ms = total_time_ms / num_tokens
|
| 327 |
peak_memory_mb = torch.cuda.max_memory_allocated() / (1024 * 1024)
|
| 328 |
|
|
|
|
| 329 |
del Q, K_cache, V_cache, output
|
| 330 |
torch.cuda.empty_cache()
|
| 331 |
|
|
|
|
| 345 |
decode_tokens: int = 32,
|
| 346 |
) -> tuple:
|
| 347 |
"""
|
| 348 |
+
Run full comparison between prefill and decode phases using REAL HuggingFace model.
|
| 349 |
+
|
| 350 |
+
Loads the actual model, extracts the attention layer, and benchmarks
|
| 351 |
+
real attention operations for both prefill and decode phases.
|
| 352 |
|
| 353 |
Returns results dict, comparison chart, KV cache chart, and insight text.
|
| 354 |
"""
|
|
|
|
| 356 |
return {"error": f"Unknown model: {model_name}"}, None, None, "Error: Unknown model"
|
| 357 |
|
| 358 |
config = MODEL_CONFIGS[model_name]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
results = {
|
| 361 |
"model": model_name,
|
| 362 |
"context_length": context_length,
|
| 363 |
"decode_tokens": decode_tokens,
|
| 364 |
"config": config,
|
| 365 |
+
"using_real_model": True,
|
| 366 |
}
|
| 367 |
|
| 368 |
+
try:
|
| 369 |
+
# Load the REAL HuggingFace model
|
| 370 |
+
model = load_model(model_name)
|
| 371 |
+
|
| 372 |
+
# Extract attention layer from layer 0
|
| 373 |
+
attention_layer = extract_attention_layer(model, layer_idx=0)
|
| 374 |
+
|
| 375 |
+
# Get model attention info
|
| 376 |
+
attn_info = get_model_attention_info(model)
|
| 377 |
+
results["model_info"] = attn_info
|
| 378 |
+
|
| 379 |
+
# Run prefill benchmarks with REAL model attention
|
| 380 |
+
prefill_flash = run_prefill_with_real_model(
|
| 381 |
+
model=model,
|
| 382 |
+
attention_layer=attention_layer,
|
| 383 |
+
seq_len=context_length,
|
| 384 |
+
batch_size=1,
|
| 385 |
+
use_flash=True,
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
prefill_math = run_prefill_with_real_model(
|
| 389 |
+
model=model,
|
| 390 |
+
attention_layer=attention_layer,
|
| 391 |
+
seq_len=context_length,
|
| 392 |
+
batch_size=1,
|
| 393 |
+
use_flash=False,
|
| 394 |
+
)
|
| 395 |
+
|
| 396 |
+
# Run decode benchmarks with REAL model attention
|
| 397 |
+
decode_flash = run_decode_with_real_model(
|
| 398 |
+
model=model,
|
| 399 |
+
attention_layer=attention_layer,
|
| 400 |
+
kv_cache_len=context_length,
|
| 401 |
+
num_tokens=decode_tokens,
|
| 402 |
+
batch_size=1,
|
| 403 |
+
use_flash=True,
|
| 404 |
+
)
|
| 405 |
+
|
| 406 |
+
decode_math = run_decode_with_real_model(
|
| 407 |
+
model=model,
|
| 408 |
+
attention_layer=attention_layer,
|
| 409 |
+
kv_cache_len=context_length,
|
| 410 |
+
num_tokens=decode_tokens,
|
| 411 |
+
batch_size=1,
|
| 412 |
+
use_flash=False,
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
+
except Exception as e:
|
| 416 |
+
# Fallback to legacy mode if model loading fails
|
| 417 |
+
results["using_real_model"] = False
|
| 418 |
+
results["fallback_reason"] = str(e)[:100]
|
| 419 |
+
|
| 420 |
+
num_heads = config["q_heads"]
|
| 421 |
+
head_dim = config["head_dim"]
|
| 422 |
+
|
| 423 |
+
prefill_flash = simulate_prefill_attention(
|
| 424 |
+
batch_size=1, num_heads=num_heads, seq_len=context_length,
|
| 425 |
+
head_dim=head_dim, use_flash=True,
|
| 426 |
+
)
|
| 427 |
+
prefill_math = simulate_prefill_attention(
|
| 428 |
+
batch_size=1, num_heads=num_heads, seq_len=context_length,
|
| 429 |
+
head_dim=head_dim, use_flash=False,
|
| 430 |
+
)
|
| 431 |
+
decode_flash = simulate_decode_attention(
|
| 432 |
+
batch_size=1, num_heads=num_heads, kv_cache_len=context_length,
|
| 433 |
+
head_dim=head_dim, num_tokens=decode_tokens, use_flash=True,
|
| 434 |
+
)
|
| 435 |
+
decode_math = simulate_decode_attention(
|
| 436 |
+
batch_size=1, num_heads=num_heads, kv_cache_len=context_length,
|
| 437 |
+
head_dim=head_dim, num_tokens=decode_tokens, use_flash=False,
|
| 438 |
+
)
|
| 439 |
|
| 440 |
results["prefill"] = {
|
| 441 |
"flash": prefill_flash,
|
|
|
|
| 455 |
# Generate insight
|
| 456 |
insight = generate_phase_insight(results)
|
| 457 |
|
| 458 |
+
# Add real model indicator to insight
|
| 459 |
+
if results.get("using_real_model"):
|
| 460 |
+
model_indicator = f"\n\n---\n\n*Benchmarked using real **{model_name}** attention layer ({attn_info['num_attention_heads']} heads, {attn_info['head_dim']}d)*"
|
| 461 |
+
insight = insight + model_indicator
|
| 462 |
+
|
| 463 |
return results, comparison_chart, kv_cache_chart, insight
|
| 464 |
|
| 465 |
|