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
|
# Copyright (C) 2021 - 2022 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""HTML plotting."""
import os
from .pdf import BaseFigure
from .utils import to_data_frames
def speedup_saturation(speedup):
diff = abs(1.0 - speedup)
# scale up a 20% difference into maximum 50% saturation - lower
# difference should be closer to 255 (white)
saturation_ratio = min(0.5, diff / 0.2 * 0.5)
return 255 - int(saturation_ratio * 255.0)
def speedup_colors(speedup):
ret = []
for s in speedup:
saturation = speedup_saturation(s)
if s < 1.0:
# slowdown is red
ret.append('rgb(255,{0},{0})'.format(saturation))
else:
# speedup is green
ret.append('rgb({0},255,{0})'.format(saturation))
return ret
def significance_colors(significance, threshold):
ret = []
for s in significance:
if s < threshold:
saturation = 128 + int(128 * (s / threshold))
ret.append('rgb({0},{0},255)'.format(saturation))
else:
ret.append('white')
return ret
def title_to_html_anchor(title):
return title.replace(' ', '_')
def token_to_length(tokens):
length = []
for token in tokens:
words = token.split("_")
for idx in range(len(words)):
if (words[idx] == "len"):
lenidx = idx + 1
thislength = []
while lenidx < len(words) and words[lenidx].isnumeric():
thislength.append(int(words[lenidx]))
lenidx += 1
length.append(thislength)
return length
def token_to_batch(tokens):
batch = []
for token in tokens:
words = token.split("_")
for idx in range(len(words)):
if (words[idx] == "batch"):
batchidx = idx + 1
thisbatch = []
while batchidx < len(words) and words[batchidx].isnumeric():
thisbatch.append(int(words[batchidx]))
batchidx += 1
batch.append(thisbatch)
return batch
def token_to_elements(tokens):
length = token_to_length(tokens)
batch = token_to_batch(tokens)
elements = []
for i in range(len(length)):
n = int(1)
for j in range(len(length[i])):
n *= int(length[i][j])
for j in range(len(batch[i])):
n *= int(batch[i][j])
elements.append(n)
return elements
def token_to_size_description(tokens):
length = token_to_length(tokens)
batch = token_to_batch(tokens)
descriptions = []
for cur_len, cur_batch in zip(length, batch):
def join_ints(ints):
return 'x'.join([str(val) for val in ints])
desc = join_ints(cur_len) + 'b' + join_ints(cur_batch)
descriptions.append(desc)
return descriptions
class HTMLFigure(BaseFigure):
def make(self, sig_threshold):
from plotly import graph_objs as go
data_frames = to_data_frames(self.primary, self.secondary)
for df in data_frames:
df['elements'] = token_to_elements(df.token)
df.sort_values(by='elements', inplace=True)
logscale = False
traces = []
traces.append(
go.Scatter(x=data_frames[0].elements,
y=data_frames[0].median_sample,
hovertext=token_to_size_description(
data_frames[0].token),
name=self.labels[0]))
for i in range(1, len(data_frames)):
traces.append(
go.Scatter(x=data_frames[i].elements,
y=data_frames[i].median_sample,
hovertext=data_frames[i].token,
name=self.labels[i]))
traces.append(
go.Scatter(x=data_frames[i].elements,
y=data_frames[i].speedup,
name='Speedup {} over {}'.format(
self.labels[i], self.labels[0]),
yaxis='y2',
error_y=dict(
type='data',
symmetric=False,
array=data_frames[i].speedup_high -
data_frames[i].speedup,
arrayminus=data_frames[i].speedup -
data_frames[i].speedup_low,
)))
if logscale:
x_title = 'Problem size (elements, logarithmic)'
axis_type = 'log'
y_title = 'Time (ms, logarithmic)'
else:
x_title = 'Problem size (elements)'
axis_type = 'linear'
y_title = 'Time (ms)'
layout = go.Layout(title=self.title,
xaxis=dict(
title=x_title,
type=axis_type,
),
yaxis=dict(title=y_title,
type=axis_type,
rangemode='tozero'),
yaxis2=dict(title='Speedup',
overlaying='y',
side='right',
type='linear',
rangemode='tozero'),
hovermode='x unified',
width=900,
height=600,
legend=dict(yanchor="top", xanchor="right", x=1.2))
fig = go.Figure(data=traces, layout=layout)
# Add speedup=1 reference line
fig.add_shape(type='line',
x0=min(data_frames[0].elements),
y0=1,
x1=max(data_frames[0].elements),
y1=1,
line=dict(color='grey', dash='dash'),
yref='y2')
nrows = len(data_frames[0].index)
headers = ['Problem size', 'Elements']
values = [
token_to_size_description(data_frames[0].token),
data_frames[0].elements
]
fill_colors = [
['white'] * nrows,
['white'] * nrows,
]
for i in range(len(data_frames)):
headers.append(self.labels[i] + ' (median)')
values.append(
["{:.4f}".format(x) for x in data_frames[i].median_sample])
fill_colors.append(['white'] * nrows)
if i > 0:
headers.append('Speedup {} over {}'.format(
self.labels[i], self.labels[0]))
values.append(
["{:.4f}".format(x) for x in data_frames[i].speedup])
fill_colors.append(speedup_colors(data_frames[i].speedup))
headers.append('Significance of {} over {}'.format(
self.labels[i], self.labels[0]))
values.append(
["{:.4f}".format(x) for x in data_frames[i].speedup_pval])
fill_colors.append(
significance_colors(data_frames[i].speedup_pval,
sig_threshold))
table = go.Figure(data=[
go.Table(
header=dict(values=headers),
cells=dict(values=values, fill_color=fill_colors),
),
],
layout=go.Layout(title=self.title, ))
# 900 seems to be enough for 2 dirs
# widen table by 200px per extra dir
table_width = 900
if len(data_frames) > 2:
table_width += (len(data_frames) - 2) * 200
table.update_layout(width=table_width, height=600)
self.plot = fig
self.table = table
def make_html(figures, title, docdir, outdirs, significance):
# TODO: this needs to read the output from the post-processing;
# graphing and post-processing should be separate.
# # use first dir's dat files as a basis for what to graph.
# # assumption is that other dirs have the same-named files.
# dat_files = glob.glob(os.path.join(dirs[0], '*.dat'))
# # sort files so diagrams show up in consistent order for each run
# dat_files.sort()
# construct the output file "figs.html"
outfile = open(docdir / 'figs.html', 'w')
outfile.write('''
<html>
<head>
<title>{}</title>
</head>
<body>
'''.format(title))
# make links to each figure at the top of the report
outfile.write('''<b>Quick links:</b><br/>''')
for figure in figures:
plot, table = figure.plot, figure.table
fig_title = plot.layout.title.text
anchor = title_to_html_anchor(fig_title)
outfile.write('''<a href="#{}">{}</a><br/>'''.format(
anchor, fig_title))
# include specs in the report
outfile.write('''<table><tr>''')
for d in outdirs:
outfile.write('''<td><b>{} specs:</b><br/><pre>'''.format(
os.path.basename(os.path.normpath(d))))
specs_path = os.path.join(d, 'specs.txt')
try:
with open(specs_path) as specs:
for line in specs:
outfile.write(line)
except FileNotFoundError:
outfile.write("N/A")
outfile.write('''</pre></td>''')
outfile.write('''</tr></table><br/><table>''')
# only the first figure needs js included
include_js = True
for figure in figures:
plot, table = figure.plot, figure.table
fig_title = plot.layout.title.text
anchor = title_to_html_anchor(fig_title)
outfile.write('''<tr><td id="{}">'''.format(anchor))
outfile.write(
plot.to_html(full_html=False, include_plotlyjs=include_js))
include_js = False
outfile.write('''</td><td>''')
outfile.write(table.to_html(full_html=False, include_plotlyjs=False))
outfile.write('''</td></tr>''')
outfile.write('''
</table>
</body>
</html>
''')
|