-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_efficiency.py
More file actions
410 lines (316 loc) · 12.3 KB
/
benchmark_efficiency.py
File metadata and controls
410 lines (316 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/env python3
"""
Model Efficiency Analysis
=========================
Reports model size, FLOPs, memory usage, and inference time.
Usage:
python benchmark_efficiency.py --model resnet1d --n_leads 3
"""
import os
import sys
import json
import time
import argparse
from pathlib import Path
from typing import Dict, List, Tuple
import numpy as np
import torch
import torch.nn as nn
# Add src to path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from src.model import get_model, count_parameters
def count_flops(model: nn.Module, input_shape: Tuple[int, ...]) -> int:
"""
Estimate FLOPs for a forward pass.
Note: This is an approximation. For exact counts, use specialized tools
like fvcore or thop.
"""
from functools import reduce
import operator
total_flops = 0
def flops_hook(module, input, output):
nonlocal total_flops
if isinstance(module, nn.Conv1d):
# FLOPs = 2 * K * Cin * Cout * L_out
batch_size, in_channels, in_length = input[0].shape
out_channels, _, kernel_size = module.weight.shape
out_length = output.shape[2]
flops = 2 * kernel_size * in_channels * out_channels * out_length * batch_size
total_flops += flops
elif isinstance(module, nn.Linear):
# FLOPs = 2 * in_features * out_features
batch_size = input[0].shape[0]
flops = 2 * module.in_features * module.out_features * batch_size
total_flops += flops
elif isinstance(module, nn.BatchNorm1d):
# FLOPs ≈ 2 * num_features * length (mean + var computation)
batch_size = input[0].shape[0]
length = input[0].shape[2] if len(input[0].shape) > 2 else 1
flops = 4 * module.num_features * length * batch_size
total_flops += flops
# Register hooks
hooks = []
for module in model.modules():
if isinstance(module, (nn.Conv1d, nn.Linear, nn.BatchNorm1d)):
hooks.append(module.register_forward_hook(flops_hook))
# Forward pass
x = torch.randn(1, *input_shape)
if next(model.parameters()).is_cuda:
x = x.cuda()
model.eval()
with torch.no_grad():
model(x)
# Remove hooks
for hook in hooks:
hook.remove()
return total_flops
def measure_memory(model: nn.Module, input_shape: Tuple[int, ...], batch_size: int = 32) -> Dict:
"""Measure GPU memory usage during inference."""
if not torch.cuda.is_available():
return {'error': 'CUDA not available'}
model = model.cuda()
model.eval()
# Clear cache
torch.cuda.empty_cache()
torch.cuda.reset_peak_memory_stats()
x = torch.randn(batch_size, *input_shape).cuda()
# Warm up
with torch.no_grad():
for _ in range(3):
_ = model(x)
torch.cuda.synchronize()
torch.cuda.reset_peak_memory_stats()
# Measure
with torch.no_grad():
_ = model(x)
torch.cuda.synchronize()
memory_allocated = torch.cuda.memory_allocated() / 1024**2 # MB
memory_reserved = torch.cuda.memory_reserved() / 1024**2
peak_memory = torch.cuda.max_memory_allocated() / 1024**2
return {
'memory_allocated_mb': memory_allocated,
'memory_reserved_mb': memory_reserved,
'peak_memory_mb': peak_memory,
'batch_size': batch_size,
}
def measure_inference_time(
model: nn.Module,
input_shape: Tuple[int, ...],
batch_sizes: List[int] = [1, 8, 32, 128],
n_warmup: int = 10,
n_iterations: int = 100,
use_amp: bool = True
) -> Dict:
"""Measure inference time for different batch sizes."""
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
model.eval()
results = {}
for batch_size in batch_sizes:
x = torch.randn(batch_size, *input_shape).to(device)
# Warm up
with torch.no_grad():
for _ in range(n_warmup):
if use_amp and torch.cuda.is_available():
with torch.cuda.amp.autocast():
_ = model(x)
else:
_ = model(x)
if torch.cuda.is_available():
torch.cuda.synchronize()
# Measure
times = []
for _ in range(n_iterations):
start = time.perf_counter()
with torch.no_grad():
if use_amp and torch.cuda.is_available():
with torch.cuda.amp.autocast():
_ = model(x)
else:
_ = model(x)
if torch.cuda.is_available():
torch.cuda.synchronize()
end = time.perf_counter()
times.append(end - start)
times = np.array(times) * 1000 # Convert to ms
results[batch_size] = {
'mean_ms': float(np.mean(times)),
'std_ms': float(np.std(times)),
'median_ms': float(np.median(times)),
'min_ms': float(np.min(times)),
'max_ms': float(np.max(times)),
'samples_per_second': float(batch_size / (np.mean(times) / 1000)),
}
return results
def get_model_size(model: nn.Module) -> Dict:
"""Get model size in different metrics."""
n_params = count_parameters(model)
# Size in MB (assuming float32)
size_mb_fp32 = n_params * 4 / 1024**2
size_mb_fp16 = n_params * 2 / 1024**2
# Count by layer type
layer_counts = {}
for name, module in model.named_modules():
layer_type = type(module).__name__
if layer_type not in layer_counts:
layer_counts[layer_type] = 0
layer_counts[layer_type] += 1
return {
'total_parameters': n_params,
'trainable_parameters': n_params, # All are trainable in our case
'size_mb_fp32': size_mb_fp32,
'size_mb_fp16': size_mb_fp16,
'layer_counts': layer_counts,
}
def benchmark_model(
model_name: str = 'resnet1d',
n_leads: int = 12,
seq_length: int = 5000,
n_classes: int = 5,
**model_kwargs
) -> Dict:
"""Run full benchmark on a model configuration."""
print(f"\n{'='*60}")
print(f"Benchmarking: {model_name} ({n_leads} leads)")
print(f"{'='*60}")
# Build model
model = get_model(model_name, n_leads=n_leads, n_classes=n_classes, **model_kwargs)
input_shape = (n_leads, seq_length)
# Model size
print("\n📏 Model Size:")
size_info = get_model_size(model)
print(f" Parameters: {size_info['total_parameters']:,}")
print(f" Size (FP32): {size_info['size_mb_fp32']:.2f} MB")
print(f" Size (FP16): {size_info['size_mb_fp16']:.2f} MB")
# FLOPs
print("\n Computational Cost:")
flops = count_flops(model, input_shape)
print(f" FLOPs: {flops:,}")
print(f" GFLOPs: {flops / 1e9:.3f}")
# Memory
print("\n Memory Usage:")
memory_info = measure_memory(model, input_shape, batch_size=32)
if 'error' not in memory_info:
print(f" Allocated: {memory_info['memory_allocated_mb']:.2f} MB")
print(f" Peak: {memory_info['peak_memory_mb']:.2f} MB")
else:
print(f" {memory_info['error']}")
memory_info = {}
# Inference time
print("\n Inference Time:")
time_info = measure_inference_time(model, input_shape)
for batch_size, times in time_info.items():
print(f" Batch {batch_size:3d}: {times['mean_ms']:.2f}±{times['std_ms']:.2f} ms "
f"({times['samples_per_second']:.1f} samples/sec)")
# Aggregate results
results = {
'model_name': model_name,
'n_leads': n_leads,
'seq_length': seq_length,
'n_classes': n_classes,
'size': size_info,
'flops': flops,
'gflops': flops / 1e9,
'memory': memory_info,
'inference_time': time_info,
}
return results
def benchmark_all_configurations(
output_dir: str = 'outputs/benchmarks'
) -> List[Dict]:
"""Benchmark all lead configurations."""
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
# Lead configurations
configs = [
{'name': '12-lead', 'n_leads': 12},
{'name': '6-lead', 'n_leads': 6},
{'name': '3-lead', 'n_leads': 3},
{'name': '2-lead', 'n_leads': 2},
{'name': '1-lead', 'n_leads': 1},
]
all_results = []
for config in configs:
results = benchmark_model(
model_name='resnet1d',
n_leads=config['n_leads'],
seq_length=5000,
n_classes=5
)
results['config_name'] = config['name']
all_results.append(results)
# Save results
with open(output_dir / 'benchmark_results.json', 'w') as f:
json.dump(all_results, f, indent=2, default=str)
# Generate summary table
generate_efficiency_table(all_results, output_dir)
return all_results
def generate_efficiency_table(results: List[Dict], output_dir: Path):
"""Generate LaTeX table comparing model efficiency."""
print("\n" + "="*70)
print(" EFFICIENCY COMPARISON")
print("="*70)
# Print ASCII table
print(f"\n{'Config':<12} {'Params':>12} {'GFLOPs':>10} {'Size (MB)':>12} {'Time (ms)':>12} {'Throughput':>12}")
print("-" * 70)
for r in results:
config = r.get('config_name', f"{r['n_leads']}-lead")
params = r['size']['total_parameters']
gflops = r['gflops']
size_mb = r['size']['size_mb_fp32']
# Use batch_size=32 timing
time_ms = r['inference_time'].get(32, {}).get('mean_ms', 0)
throughput = r['inference_time'].get(32, {}).get('samples_per_second', 0)
print(f"{config:<12} {params:>12,} {gflops:>10.3f} {size_mb:>12.2f} {time_ms:>12.2f} {throughput:>12.1f}")
# LaTeX table
latex = r"""\begin{table}[htbp]
\centering
\caption{Computational Efficiency by Lead Configuration. Throughput measured on RTX 4090 with batch size 32 and mixed precision.}
\label{tab:efficiency}
\begin{tabular}{lccccc}
\toprule
\textbf{Config} & \textbf{Parameters} & \textbf{GFLOPs} & \textbf{Size (MB)} & \textbf{Time (ms)} & \textbf{Throughput} \\
\midrule
"""
for r in results:
config = r.get('config_name', f"{r['n_leads']}-lead")
params = r['size']['total_parameters']
gflops = r['gflops']
size_mb = r['size']['size_mb_fp32']
time_ms = r['inference_time'].get(32, {}).get('mean_ms', 0)
throughput = r['inference_time'].get(32, {}).get('samples_per_second', 0)
# Format parameters (e.g., 155K, 1.2M)
if params >= 1e6:
params_str = f"{params/1e6:.1f}M"
else:
params_str = f"{params/1e3:.0f}K"
latex += f"{config} & {params_str} & {gflops:.2f} & {size_mb:.1f} & {time_ms:.1f} & {throughput:.0f}/s \\\\\n"
latex += r"""\bottomrule
\end{tabular}
\end{table}
"""
with open(output_dir / 'efficiency_table.tex', 'w') as f:
f.write(latex)
print(f"\n LaTeX table saved to {output_dir / 'efficiency_table.tex'}")
def main():
parser = argparse.ArgumentParser(description="Benchmark model efficiency")
parser.add_argument("--model", type=str, default="resnet1d",
help="Model architecture")
parser.add_argument("--n_leads", type=int, default=12,
help="Number of ECG leads")
parser.add_argument("--all", action="store_true",
help="Benchmark all configurations")
parser.add_argument("--output", type=str, default="outputs/benchmarks",
help="Output directory")
args = parser.parse_args()
if args.all:
benchmark_all_configurations(args.output)
else:
results = benchmark_model(args.model, n_leads=args.n_leads)
# Save
output_dir = Path(args.output)
output_dir.mkdir(parents=True, exist_ok=True)
with open(output_dir / f'benchmark_{args.model}_{args.n_leads}lead.json', 'w') as f:
json.dump(results, f, indent=2, default=str)
if __name__ == "__main__":
main()