-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheck_gen.py
More file actions
285 lines (243 loc) Β· 10.5 KB
/
check_gen.py
File metadata and controls
285 lines (243 loc) Β· 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# import debugpy; debugpy.connect(('0.0.0.0', 5678))
import torch
import time
import tracemalloc
from pathlib import Path
from tokenizers import Tokenizer
from omegaconf import OmegaConf
from contextlib import contextmanager
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.columns import Columns
from rich.text import Text
import gc
from cs336_basics.model import TransformerLM
from cs336_basics.generate import generate, install_kv_cache
from cs336_basics.config import TrainConfig
console = Console()
@contextmanager
def measure_performance():
"""Context manager for measuring performance metrics"""
# Clear GPU memory cache
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.reset_peak_memory_stats()
gc.collect()
gpu_memory_start = torch.cuda.memory_allocated()
else:
gpu_memory_start = 0
# Start CPU memory tracking
tracemalloc.start()
# Record start time
start_time = time.time()
# Create results dictionary to store metrics
results = {}
try:
yield results
finally:
# Record end time
end_time = time.time()
execution_time = end_time - start_time
# Get CPU peak memory
current_cpu, peak_cpu = tracemalloc.get_traced_memory()
tracemalloc.stop()
# Get GPU peak memory
peak_gpu_memory = 0
if torch.cuda.is_available():
gpu_memory_max = torch.cuda.max_memory_allocated()
peak_gpu_memory = gpu_memory_max - gpu_memory_start
# Store results in the yielded dictionary
results.update({
'execution_time': execution_time,
'peak_cpu_memory_mb': peak_cpu / 1024 / 1024,
'gpu_memory_start_mb': gpu_memory_start / 1024 / 1024 if torch.cuda.is_available() else 0,
'gpu_memory_max_mb': gpu_memory_max / 1024 / 1024 if torch.cuda.is_available() else 0,
'peak_gpu_memory_mb': peak_gpu_memory / 1024 / 1024 if torch.cuda.is_available() else 0,
})
def create_metrics_table(title, metrics, max_tokens, batch_size=1):
"""Create performance metrics table"""
table = Table(title=title, show_header=True, header_style="bold magenta")
table.add_column("Metric", style="cyan", no_wrap=True)
table.add_column("Value", style="green")
table.add_column("Unit", style="yellow")
table.add_row("Execution Time", f"{metrics['execution_time']:.3f}", "seconds")
table.add_row("Tokens/Second", f"{(max_tokens * batch_size)/metrics['execution_time']:.2f}", "tokens/s")
table.add_row("Peak CPU Memory", f"{metrics['peak_cpu_memory_mb']:.2f}", "MB")
table.add_row("GPU Memory Start", f"{metrics['gpu_memory_start_mb']:.2f}", "MB")
table.add_row("GPU Memory Max", f"{metrics['gpu_memory_max_mb']:.2f}", "MB")
table.add_row("Peak GPU Memory", f"{metrics['peak_gpu_memory_mb']:.2f}", "MB")
return table
def create_comparison_table(metrics_std, metrics_cached, max_tokens, batch_size=1):
"""Create comparison table"""
table = Table(title="π Performance Comparison", show_header=True, header_style="bold blue")
table.add_column("Metric", style="cyan", no_wrap=True)
table.add_column("Standard", style="red")
table.add_column("KV Cache", style="green")
table.add_column("Improvement", style="yellow")
# Time comparison
speedup = metrics_std['execution_time'] / metrics_cached['execution_time']
time_reduction = ((metrics_std['execution_time'] - metrics_cached['execution_time']) / metrics_std['execution_time']) * 100
table.add_row(
"Execution Time",
f"{metrics_std['execution_time']:.3f}s",
f"{metrics_cached['execution_time']:.3f}s",
f"{speedup:.2f}x ({time_reduction:.1f}% faster)"
)
# tokens/s comparison
std_tps = (max_tokens * batch_size) / metrics_std['execution_time']
cached_tps = (max_tokens * batch_size) / metrics_cached['execution_time']
table.add_row(
"Tokens/Second",
f"{std_tps:.2f}",
f"{cached_tps:.2f}",
f"{cached_tps/std_tps:.2f}x"
)
# GPU memory comparison
mem_diff = metrics_cached['peak_gpu_memory_mb'] - metrics_std['peak_gpu_memory_mb']
mem_percent = (mem_diff / metrics_std['peak_gpu_memory_mb']) * 100 if metrics_std['peak_gpu_memory_mb'] > 0 else 0
table.add_row(
"Peak GPU Memory",
f"{metrics_std['peak_gpu_memory_mb']:.2f}MB",
f"{metrics_cached['peak_gpu_memory_mb']:.2f}MB",
f"{'+' if mem_diff > 0 else ''}{mem_diff:.2f}MB ({mem_percent:+.1f}%)"
)
return table
# Load model
console.print(Panel.fit("π Loading Model", style="bold blue"))
# Load model configuration
# the best model of openwebtext
# ckpt_path = "outputs/multiruns/2025-10-01_18-19-21/2/ckpt_4999.pt"
# the best model of tinystories
ckpt_path = "outputs/runs/2025-10-02_05-14-38/ckpt_19999.pt"
cfg_path = Path(ckpt_path).parent / ".hydra" / "config.yaml"
cfg: TrainConfig = OmegaConf.load(cfg_path)
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task("Loading model...", total=None)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = TransformerLM(**cfg.model).to(device)
if cfg.training.is_compile:
model = torch.compile(model)
checkpoint = torch.load(ckpt_path, map_location=device)
model.load_state_dict(checkpoint['model_state_dict'])
model = model._orig_mod
progress.update(task, description="Model loaded successfully!")
total_params = sum(p.numel() for p in model.parameters())
model.eval()
tokenizer = Tokenizer.from_file(cfg.data.tokenizer_path)
# Test parameters
batch_size = 128
max_new_tokens = 1000
temperature = 0.6
top_p = 0.95
# Create context with batch_size
context = torch.zeros((batch_size, 1), dtype=torch.long, device=device)
install_kv_cache(model, batch_size=batch_size, total_len=cfg.model.context_length + max_new_tokens)
# Display model and test information
model_info = Table(title="π Model Information", show_header=False)
model_info.add_column("Property", style="cyan")
model_info.add_column("Value", style="green")
model_info.add_row("Checkpoint Path", ckpt_path)
model_info.add_row("Total Parameters", f"{total_params/1e6:.2f}M")
model_info.add_row("Device", device)
model_info.add_row("Context Length", str(cfg.model.context_length))
para_info = Table(title="βοΈ Test Parameters", show_header=False)
para_info.add_column("Parameter", style="cyan")
para_info.add_column("Value", style="green")
para_info.add_row("Batch Size", str(batch_size))
para_info.add_row("Max New Tokens", str(max_new_tokens))
para_info.add_row("Temperature", str(temperature))
para_info.add_row("Top P", str(top_p)),
info_tables = Columns([
# Model Information Table
Table.grid(model_info, padding=(0, 2)),
# Test Parameters Table
Table.grid(para_info, padding=(0, 2))
])
# Display information tables side by side
model_info = Table(title="π Model Information", show_header=False)
model_info.add_column("Property", style="cyan")
model_info.add_column("Value", style="green")
model_info.add_row("Checkpoint Path", ckpt_path)
model_info.add_row("Total Parameters", f"{total_params/1e6:.2f}M")
model_info.add_row("Device", device)
model_info.add_row("Context Length", str(cfg.model.context_length))
test_params = Table(title="βοΈ Test Parameters", show_header=False)
test_params.add_column("Parameter", style="cyan")
test_params.add_column("Value", style="green")
test_params.add_row("Batch Size", str(batch_size))
test_params.add_row("Max New Tokens", str(max_new_tokens))
test_params.add_row("Temperature", str(temperature))
test_params.add_row("Top P", str(top_p))
console.print(Columns([model_info, test_params]))
console.print("\n" + "="*70)
console.print(Panel.fit("π₯ Performance Benchmarking", style="bold red"))
# Test 1: Standard generation
console.print("\nπ― Running Standard Generation...")
with measure_performance() as metrics_standard:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task("Generating tokens...", total=None)
generated_standard = generate(
model,
context.clone(),
max_new_tokens=max_new_tokens,
block_size=cfg.model.context_length,
temperature=temperature,
top_p=top_p,
)
progress.update(task, description="Standard generation completed!")
generated_text_standard = tokenizer.decode(generated_standard[0].tolist())
# Test 2: KV cache generation
console.print("\nβ‘ Running KV Cache Generation...")
with measure_performance() as metrics_cached:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task("Generating tokens with KV cache...", total=None)
generated_cached = generate(
model,
context.clone(),
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
use_kv_cache=True
)
progress.update(task, description="KV cache generation completed!")
generated_text_cached = tokenizer.decode(generated_cached[0].tolist())
# Display results
console.print("\n")
tables = Columns([
create_metrics_table("π Standard Generation", metrics_standard, max_new_tokens, batch_size),
create_metrics_table("β‘ KV Cache Generation", metrics_cached, max_new_tokens, batch_size)
])
console.print(tables)
console.print("\n")
console.print(create_comparison_table(metrics_standard, metrics_cached, max_new_tokens, batch_size))
# Display generated text preview
console.print("\n")
console.print(Panel(
f"Standard: {generated_text_standard[:100]}...\n\n"
f"KV Cache: {generated_text_cached[:100]}...",
title="π Generated Text Preview (First Batch)",
style="blue"
))
# Display full KV Cache generated text
console.print("\n")
console.print(Panel(
generated_text_cached,
title="π Full Generated Text (KV Cache - First Batch)",
style="green"
))
console.print(f"\nβ
Output shapes - Standard: {generated_standard.shape}, KV Cache: {generated_cached.shape}")
console.print(f"β
Total tokens generated - Standard: {generated_standard.numel()}, KV Cache: {generated_cached.numel()}")