File: plot.py

package info (click to toggle)
hipblas 6.4.4-2~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 26,700 kB
  • sloc: cpp: 209,856; f90: 49,800; python: 4,680; ansic: 1,141; sh: 799; makefile: 51; xml: 23
file content (278 lines) | stat: -rwxr-xr-x 12,366 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

import argparse
import os
import re
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import yaml
from pathlib import Path

def plot_data(title, gflops_dicts1, gflops_dicts2, const_args_dicts, funcname_list, machine_spec_dict, savedir, theo_max, perf_vs_perf, size_arg = 'N'):
    """
    plots gflops data from dictionaries, one plot for each common precision present in all dictionaries.

    Parameters:
        title (string): title for plots
        gflops_dicts1 (list[dict{string: list[(int, float)]}]): data for one machine as given by :func:`get_data_from_directories`.
        gflops_dicts2 (list[dict{string: list[(int, float)]}]): data for another machine as given by :func:`get_data_from_directories`.
        const_args_dicts (list[dict{string: string}]): arguments that remain constant
        funcname_list (list[string]): a list of funcname for each data set to be plotted and used as a savefile name.
        machine_spec_dict (dict{string: string}): specification and peak performance for machine
        savedir (string): directory where resulting plots will be saved.
        theo_max (string): true for plotting performance versus theoretical maximum performance
        perf_vs_perf (string): true for plotting relative performance of one machine versus another machine
        size_arg (string): x axis title on plot.
    """
    if len(gflops_dicts1) == 0:
        return

    gflops_dict0 = gflops_dicts1[0]
    for prec, _ in gflops_dict0.items():
        colors=iter(cm.rainbow(np.linspace(0,1,len(gflops_dicts1))))
        figure, axes = plt.subplots(figsize=(7,7))

        for gflops_dict1, gflops_dict2, funcname, const_args_dict in zip(gflops_dicts1, gflops_dicts2, funcname_list, const_args_dicts):
            cur_color = next(colors)
            if prec not in gflops_dict1:
                continue
            gflops1 = gflops_dict1[prec]
            gflops2 = gflops_dict2[prec]
            if (perf_vs_perf != True):
                gflops1.append((0, 0)) # I prefer having a 0 at the bottom so the performance looks more accurate
                gflops2.append((0, 0)) # I prefer having a 0 at the bottom so the performance looks more accurate
            sorted_tuples1 = sorted(gflops1)
            sorted_tuples2 = sorted(gflops2)

            sorted_sizes1 = [x[0] for x in sorted_tuples1]
            sorted_sizes2 = [x[0] for x in sorted_tuples2]

            sorted_gflops1 = [x[1] for x in sorted_tuples1]
            sorted_gflops2 = [x[1] for x in sorted_tuples2]

            if sorted_sizes1 != sorted_sizes2:
                print("sizes are not the same for the two datasets")
                return

            if (perf_vs_perf == True):
                for i in range(len(sorted_gflops1)):
                    if sorted_gflops2[i] != 0:
                        sorted_gflops1[i] /= sorted_gflops2[i]

            if(prec == "f32_r"):
                function_label = "s" + funcname
            elif(prec == "f64_r"):
                function_label = "d" + funcname
            elif(prec == "f32_c"):
                function_label = "c" + funcname
            elif(prec == "f64_c"):
                function_label = "z" + funcname

            if(theo_max == True):
                theo_max_value = machine_spec_dict[function_label]
                sorted_gflops1[:] = [gf / theo_max_value for gf in sorted_gflops1]

            function_label = function_label + " :  " + const_args_dict[prec]

            axes.scatter(sorted_sizes1, sorted_gflops1, color=cur_color, label=function_label)
            axes.plot(sorted_sizes1, sorted_gflops1, '-o', color=cur_color)

        if(theo_max == True):
            axes.set_ylim(0, 1)
            axes.set_ylabel('gflops / theoretical_maximum_gflops')
        elif(perf_vs_perf == True):
            axes.set_ylabel('gflops / gflops')
        else:
            axes.set_ylabel('gflops')

        axes.set_xlabel('='.join(size_arg)) # in case we add multiple params

        # magic numbers from performancereport.py to make plots look nice
        axes.legend(fontsize=10, bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left',
                    mode='expand', borderaxespad=0.)
        figure.tight_layout(rect=(0,0.05,1.0,0.94))

        figure.suptitle(title, y=0.96)

        filename = ''
        for funcname in funcname_list:
            if filename != '':
                filename += '_'
            filename += funcname
        filename += '_' + prec
        if not os.path.exists(savedir):
            os.makedirs(savedir)
        figure.savefig(os.path.join(os.getcwd(), savedir, filename))

def get_function_name(filename):
    function_str = "function"
    if os.path.exists(filename):
        lines = open(filename, 'r').readlines()
    else:
        print(filename + " does not exist")
    for i in range(0, len(lines)):
        if(function_str in lines[i]):
            arg_line = lines[i].split(",")
            data_line = re.split(r',\s*(?![^()]*\))', lines[i+1])
            function_idx = arg_line.index(function_str)
            return data_line[function_idx]

def get_data_from_file(filename, output_param='hipblas-Gflops', xaxis_str1='N', xaxis_str2='M', yaxis_str='hipblas-Gflops'):

    precision_str = "compute_type"
    if os.path.exists(filename):
        lines = open(filename, 'r').readlines()

    function_name = get_function_name(filename)

    cur_dict = {}
    for i in range(0, len(lines)):
        if(output_param in lines[i]):
            arg_line = lines[i].split(",")
            data_line = re.split(r',\s*(?![^()]*\))', lines[i+1])
            if(xaxis_str1 in arg_line):
                xaxis_idx = arg_line.index(xaxis_str1)
            if(xaxis_str2 in arg_line):
                xaxis_idx = arg_line.index(xaxis_str2)
            yaxis_idx = arg_line.index(yaxis_str)
            size_perf_tuple = (int(data_line[xaxis_idx]), float(data_line[yaxis_idx]))

            precision_idx = arg_line.index(precision_str)
            precision = data_line[precision_idx]
            if precision in cur_dict:
                cur_dict[precision].append(size_perf_tuple)
            else:
                cur_dict[precision] = [size_perf_tuple]

    return cur_dict

tracked_param_list = [ 'transA', 'transB', 'uplo', 'diag', 'side', 'M', 'N', 'K', 'KL', 'KU', 'alpha', 'alphai', 'beta', 'betai',
                       'incx', 'incy', 'lda', 'ldb', 'ldd', 'stride_x', 'stride_y', 'stride_a', 'stride_b', 'stride_c', 'stride_d',
                       'batch_count']

# return string of arguments that remain constant. For example, transA, transB, alpha, beta, incx may remain
# constant. By contrast, M, N, K, lda, ldb, ldc may change
#def get_const_args_str(filename, output_param='hipblas-Gflops'):
def get_const_args_dict(filename, output_param='hipblas-Gflops'):

    if os.path.exists(filename):
        lines = open(filename, 'r').readlines()


    precision_str = "compute_type"
    precisions = []
    for i in range(0, len(lines)):
        if(output_param in lines[i]):
            arg_line = lines[i].split(",")
            data_line = re.split(r',\s*(?![^()]*\))', lines[i+1])

            precision_idx = arg_line.index(precision_str)
            precision = data_line[precision_idx]
            if precision not in precisions:
                precisions.append(precision)

    const_args_dict = {}

    for precision in precisions:

        function_param_list = tracked_param_list

        arg_line_index_dict = {}
        arg_line_value_dict = {}
        for i in range(0, len(lines)):
            if((output_param in lines[i]) and (precision in lines[i+1])):
                arg_line = lines[i].split(",")
                data_line = re.split(r',\s*(?![^()]*\))', lines[i+1])

                if not arg_line_index_dict:
                    for arg in arg_line :
                        if(arg in function_param_list):
                            index = arg_line.index(arg)
                            value = data_line[index]
                            arg_line_index_dict[arg]=index
                            arg_line_value_dict[arg]=value
                else:
                    for arg in arg_line :
                        if(arg in function_param_list):
                            index = arg_line.index(arg)
                            value = data_line[index]
                            previous_value = arg_line_value_dict[arg]
                            if(value != previous_value):
                                function_param_list.remove(arg)
                                del arg_line_value_dict[arg]

        const_args_str = ""
        for key, value in arg_line_value_dict.items():
            if(const_args_str == ""):
                const_args_str += key + "=" + value
            else:
                const_args_str += ", " + key + "=" + value

        const_args_dict[precision] = const_args_str
    return const_args_dict

if __name__ =='__main__':

    parser = argparse.ArgumentParser(
            description='plot hipblas-bench results for multiple csv files',
            epilog='Example usage: python3 plot_benchmarks.py ' +
                    '-l blas1 -t gfx906  -f scal -f axpy  --label1 "N" --label2 "M"')

    parser.add_argument('-l', '--level',          help='BLAS level',          dest='level',          default='blas1')
    parser.add_argument('-t',  '--tag1',          help='tag1',                dest='tag1',           default='gfx906')
    parser.add_argument(       '--tag2',          help='tag2',                dest='tag2',           default='ref')
    parser.add_argument(     '--label1',          help='label1',              dest='label1',         default='N')
    parser.add_argument(     '--label2',          help='label2',              dest='label2',         default='M')
    parser.add_argument('-f'           ,          help='function name',       dest='function_names', required=True, action='append')
    parser.add_argument(     '--theo_max',        help="perf vs theo_max",    dest='theo_max', default="false", action='store_true')
    parser.add_argument(     '--no_theo_max',     help="no perf vs theo_max", dest='theo_max', action='store_false')
    parser.add_argument(     '--perf_vs_perf',    help="perf vs perf",        dest='perf_vs_perf', default="false", action='store_true')
    parser.add_argument(     '--no_perf_vs_perf', help="no perf vs perf",     dest='perf_vs_perf', action='store_false')

    args = parser.parse_args()

    funcname_list = []

    gflops_dicts1 = []
    gflops_dicts2 = []
    const_args_dicts = []

    const_args_list = []

    if (args.theo_max == True):
        savedir = os.path.join(args.level, args.tag1, "plots_vs_theo_max")
        title = args.tag1 +  "(  performance / theoretical_maximum_performance )"
    elif (args.perf_vs_perf == True):
        savedir = os.path.join(args.level, args.tag1+"_"+args.tag2, "plots_perf_vs_perf")
        title = "Relative Performance ( " + args.tag1 + " /  " + args.tag2 + " )"
    else:
        savedir = os.path.join(args.level, args.tag1, "plots_gflops")
        title = "Performance " + args.tag1

    machine_spec_yaml_file = os.path.join(args.level, args.tag1, "machine_spec.yaml")

    machine_spec_dict = yaml.safe_load(Path(machine_spec_yaml_file).read_text())

    for function_name in args.function_names:

        output_filename1 = os.path.join(args.level, args.tag1, function_name+".csv")
        output_filename2   = os.path.join(args.level, args.tag2, function_name+".csv")

        gflops_dict1 = get_data_from_file(output_filename1, "hipblas-Gflops", args.label1, args.label2, "hipblas-Gflops")
        gflops_dict2 = get_data_from_file(output_filename2, "hipblas-Gflops", args.label1, args.label2, "hipblas-Gflops")

        gflops_dicts1.append(gflops_dict1)
        gflops_dicts2.append(gflops_dict2)

        const_args_dict = get_const_args_dict(output_filename1, "hipblas-Gflops")

        const_args_dicts.append(const_args_dict)

        function_name = get_function_name(output_filename1)
        funcname_list.append(function_name)

    print("plotting for: ", funcname_list)
    plot_data(title, gflops_dicts1, gflops_dicts2, const_args_dicts, funcname_list, machine_spec_dict, savedir, args.theo_max, args.perf_vs_perf, args.label1)