-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_roofline.py
More file actions
executable file
·309 lines (250 loc) · 11.1 KB
/
plot_roofline.py
File metadata and controls
executable file
·309 lines (250 loc) · 11.1 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
#!/usr/bin/env python3
"""
GPU Roofline Plotter
Generates roofline plots from CSV benchmark data.
"""
import argparse
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
from pathlib import Path
from typing import List, Dict, Any, Tuple
import json
class RooflinePlotter:
def __init__(self):
self.fig = None
self.ax = None
self.device_colors = {
'cuda': '#00D2FF', # NVIDIA green-blue
'metal': '#FF6B35', # Apple orange
'cpu': '#4ECDC4' # Teal
}
self.kernel_markers = {
'saxpy': 'o',
'triad': 's',
'sgemm': '^',
'wmma_gemm': 'D'
}
def load_csv_data(self, csv_files: List[str]) -> pd.DataFrame:
"""Load and combine CSV files."""
dataframes = []
for csv_file in csv_files:
try:
df = pd.read_csv(csv_file)
dataframes.append(df)
print(f"✓ Loaded {csv_file}: {len(df)} rows")
except Exception as e:
print(f"✗ Error loading {csv_file}: {e}")
continue
if not dataframes:
raise ValueError("No valid CSV files found")
combined_df = pd.concat(dataframes, ignore_index=True)
print(f"Combined dataset: {len(combined_df)} total rows")
return combined_df
def get_device_capabilities(self, device_type: str, device_name: str, precision: str) -> Dict[str, float]:
"""Get device specs for roofline bounds."""
# TODO: load from device_caps.json
capabilities = {
'cuda': {
'A100': {
'peak_bandwidth_gb_s': 1935,
'peak_compute_gflops': {'float32': 19500, 'float16': 78000, 'float64': 9700}
},
'V100': {
'peak_bandwidth_gb_s': 900,
'peak_compute_gflops': {'float32': 15700, 'float16': 31400, 'float64': 7800}
},
'default': {
'peak_bandwidth_gb_s': 500,
'peak_compute_gflops': {'float32': 10000, 'float16': 20000, 'float64': 5000}
}
},
'metal': {
'Apple M3': {
'peak_bandwidth_gb_s': 200,
'peak_compute_gflops': {'float32': 4000, 'float16': 8000, 'float64': 2000}
},
'Apple M2': {
'peak_bandwidth_gb_s': 100,
'peak_compute_gflops': {'float32': 3000, 'float16': 6000, 'float64': 1500}
},
'default': {
'peak_bandwidth_gb_s': 100,
'peak_compute_gflops': {'float32': 3000, 'float16': 6000, 'float64': 1500}
}
},
'cpu': {
'default': {
'peak_bandwidth_gb_s': 50,
'peak_compute_gflops': {'float32': 500, 'float16': 1000, 'float64': 250}
}
}
}
device_caps = capabilities.get(device_type, capabilities['cpu'])
# Match device name or use default
for key in device_caps:
if key.lower() in device_name.lower():
caps = device_caps[key]
break
else:
caps = device_caps['default']
return {
'peak_bandwidth_gb_s': caps['peak_bandwidth_gb_s'],
'peak_compute_gflops': caps['peak_compute_gflops'].get(precision, caps['peak_compute_gflops']['float32'])
}
def create_roofline_plot(self, df: pd.DataFrame, output_file: str = None, interactive: bool = False) -> None:
"""Create the main roofline plot."""
# Setup plot
plt.style.use('default')
self.fig, self.ax = plt.subplots(figsize=(12, 8))
# Log-log plot
self.ax.set_xscale('log')
self.ax.set_yscale('log')
# Draw rooflines for each device
devices = df['device_type'].unique()
for device in devices:
device_df = df[df['device_type'] == device]
if len(device_df) == 0:
continue
# Get device info for bounds
sample_row = device_df.iloc[0]
device_name = sample_row.get('device_name', f'{device} device')
precision = sample_row.get('precision', 'float32')
caps = self.get_device_capabilities(device, device_name, precision)
# Draw the bounds
self.draw_roofline_bounds(
caps['peak_bandwidth_gb_s'],
caps['peak_compute_gflops'],
device, device_name
)
# Plot data points
self.plot_benchmark_points(df)
# Format plot
self.configure_plot_appearance()
# Add legend
self.add_legend(df)
# Save it
if output_file:
self.save_plot(output_file)
if interactive:
plt.show()
def draw_roofline_bounds(self, peak_bandwidth: float, peak_compute: float, device_type: str, device_name: str) -> None:
"""Draw roofline bounds for a device."""
color = self.device_colors.get(device_type, '#808080')
# OI range for plotting
oi_min, oi_max = 0.01, 1000
oi_range = np.logspace(np.log10(oi_min), np.log10(oi_max), 1000)
# Memory bound (diagonal)
memory_bound = peak_bandwidth * oi_range
# Compute bound (horizontal)
compute_bound = np.full_like(oi_range, peak_compute)
# Roofline is min of both
roofline = np.minimum(memory_bound, compute_bound)
# Plot it
self.ax.plot(oi_range, roofline, '--', color=color, linewidth=2, alpha=0.7,
label=f'{device_name} Roofline')
# Text annotations
mem_oi = peak_compute / peak_bandwidth * 0.5 # spot for text
if oi_min <= mem_oi <= oi_max:
self.ax.annotate(f'{peak_bandwidth:.0f} GB/s',
xy=(mem_oi, peak_bandwidth * mem_oi),
xytext=(10, 10), textcoords='offset points',
fontsize=8, color=color, alpha=0.8)
# Compute annotation
comp_oi = oi_max * 0.3
self.ax.annotate(f'{peak_compute:.0f} GFLOP/s',
xy=(comp_oi, peak_compute),
xytext=(10, -15), textcoords='offset points',
fontsize=8, color=color, alpha=0.8)
def plot_benchmark_points(self, df: pd.DataFrame) -> None:
"""Plot benchmark points."""
for _, row in df.iterrows():
device_type = row['device_type']
kernel_name = row['kernel_name']
x = row['operational_intensity']
y = row['gflops_achieved']
color = self.device_colors.get(device_type, '#808080')
marker = self.kernel_markers.get(kernel_name, 'o')
# Plot point
self.ax.scatter(x, y,
c=color, marker=marker, s=100,
alpha=0.8, edgecolors='black', linewidth=0.5,
label=f'{device_type}_{kernel_name}')
# Label it
label = f'{kernel_name}\n{row["problem_size"]//1000000}M'
self.ax.annotate(label, (x, y),
xytext=(5, 5), textcoords='offset points',
fontsize=7, alpha=0.7)
def configure_plot_appearance(self) -> None:
"""Style the plot."""
self.ax.set_xlabel('Operational Intensity (FLOPs/Byte)', fontsize=12)
self.ax.set_ylabel('Performance (GFLOP/s)', fontsize=12)
self.ax.set_title('GPU Roofline Performance Analysis', fontsize=14, fontweight='bold')
# Axis limits
self.ax.set_xlim(0.01, 1000)
self.ax.set_ylim(0.1, 100000)
# Grid
self.ax.grid(True, alpha=0.3, which='both')
# Region labels
self.ax.text(0.05, 10000, 'Memory\nBound\nRegion',
fontsize=10, alpha=0.6, ha='center', va='center',
bbox=dict(boxstyle='round,pad=0.3', facecolor='yellow', alpha=0.2))
self.ax.text(50, 50000, 'Compute\nBound\nRegion',
fontsize=10, alpha=0.6, ha='center', va='center',
bbox=dict(boxstyle='round,pad=0.3', facecolor='green', alpha=0.2))
def add_legend(self, df: pd.DataFrame) -> None:
"""Add legend."""
legend_elements = []
# Device types
for device in df['device_type'].unique():
color = self.device_colors.get(device, '#808080')
legend_elements.append(
plt.Line2D([0], [0], color=color, linestyle='--', linewidth=2,
label=f'{device.upper()} Roofline')
)
# Kernel types
for kernel in df['kernel_name'].unique():
marker = self.kernel_markers.get(kernel, 'o')
legend_elements.append(
plt.Line2D([0], [0], marker=marker, color='gray', linestyle='None',
markersize=8, label=f'{kernel}')
)
self.ax.legend(handles=legend_elements, loc='upper left', fontsize=9)
def save_plot(self, output_file: str) -> None:
"""Save plot."""
output_path = Path(output_file)
output_path.parent.mkdir(parents=True, exist_ok=True)
# PNG for embedding
png_file = output_path.with_suffix('.png')
self.fig.savefig(png_file, dpi=300, bbox_inches='tight')
print(f"✓ Plot saved to {png_file}")
# SVG for scalability
svg_file = output_path.with_suffix('.svg')
self.fig.savefig(svg_file, bbox_inches='tight')
print(f"✓ Vector plot saved to {svg_file}")
def main():
parser = argparse.ArgumentParser(description="GPU Roofline Plotter")
parser.add_argument('csv_files', nargs='+', help='CSV data files to plot')
parser.add_argument('--output', '-o', default='plots/roofline', help='Output file prefix')
parser.add_argument('--show', action='store_true', help='Show interactive plot')
parser.add_argument('--title', help='Custom plot title')
args = parser.parse_args()
# Initialize plotter
plotter = RooflinePlotter()
# Load data
df = plotter.load_csv_data(args.csv_files)
if len(df) == 0:
print("No data to plot. Exiting.")
return
# Create output filename
devices = '-'.join(sorted(df['device_type'].unique()))
output_file = f"{args.output}_{devices}"
# Create plot
plotter.create_roofline_plot(df, output_file, args.show)
print("\n=== Roofline Plot Complete ===")
print(f"Analyzed {len(df)} benchmark results")
print(f"Devices: {', '.join(df['device_type'].unique())}")
print(f"Kernels: {', '.join(df['kernel_name'].unique())}")
if __name__ == "__main__":
main()