gpu_stream.py 9.7 KB
Newer Older
1
2
3
4
5
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

"""Module of the GPU Stream Performance benchmark."""

one's avatar
one committed
6
7
import csv
import io
8
9
10
11
12
13
14
15
16
import os

from superbench.common.utils import logger
from superbench.benchmarks import BenchmarkRegistry, ReturnCode
from superbench.benchmarks.micro_benchmarks import MicroBenchmarkWithInvoke


class GpuStreamBenchmark(MicroBenchmarkWithInvoke):
    """The GPU stream performance benchmark class."""
one's avatar
one committed
17
18
19
20
21
22
23
24
25
26
27
28
    _function_metric_map = {
        'Copy': 'COPY',
        'Mul': 'MUL',
        'Add': 'ADD',
        'Triad': 'TRIAD',
        'Dot': 'DOT',
    }
    _phase_metric_map = {
        'Init': 'INIT',
        'Read': 'READ',
    }

29
30
31
32
33
34
35
36
37
    def __init__(self, name, parameters=''):
        """Constructor.

        Args:
            name (str): benchmark name.
            parameters (str): benchmark parameters.
        """
        super().__init__(name, parameters)

one's avatar
one committed
38
39
        self._bin_name = 'hip-stream'
        self.__bin_path = None
40
41
42
43
44
45

    def add_parser_arguments(self):
        """Add the specified arguments."""
        super().add_parser_arguments()

        self._parser.add_argument(
one's avatar
one committed
46
            '--array_size',
47
            type=int,
one's avatar
one committed
48
            default=268435456,
49
            required=False,
one's avatar
one committed
50
            help='Number of elements in array.',
51
52
53
54
55
56
57
        )

        self._parser.add_argument(
            '--num_loops',
            type=int,
            default=100,
            required=False,
one's avatar
one committed
58
            help='Number of benchmark runs, mapping to --numtimes in BabelStream.',
59
60
61
        )

        self._parser.add_argument(
one's avatar
one committed
62
63
64
65
66
67
            '--precision',
            type=str,
            default='double',
            choices=['double', 'float'],
            required=False,
            help='Data type for benchmark.',
68
69
70
71
72
73
74
75
76
77
78
79
80
        )

    def _preprocess(self):
        """Preprocess/preparation operations before the benchmarking.

        Return:
            True if _preprocess() succeed.
        """
        if not super()._preprocess():
            return False

        self.__bin_path = os.path.join(self._args.bin_dir, self._bin_name)

one's avatar
one committed
81
82
83
84
        args = f'--arraysize {self._args.array_size} --numtimes {self._args.num_loops} --csv'
        if self._args.precision == 'float':
            args += ' --float'
        self._commands = ['{} {}'.format(self.__bin_path, args)]
85
86
87

        return True

one's avatar
one committed
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
    def _get_device_name(self, raw_output):
        """Extract device name from BabelStream output when available."""
        for line in raw_output.splitlines():
            if line.startswith('Using HIP device '):
                return line[len('Using HIP device '):].strip()
        return 'Unknown'

    @staticmethod
    def _mbps_to_gbps(value):
        """Convert MB/s to GB/s."""
        return float(value) / 1000

    def _parse_csv_phase_rows(self, raw_output):
        """Extract phase rows from BabelStream CSV output."""
        lines = [line.strip() for line in raw_output.strip().splitlines() if line.strip()]
        header = 'phase,n_elements,sizeof,max_mbytes_per_sec,runtime'
        if header not in lines:
            raise ValueError('No phase CSV header found in output.')

        start_idx = lines.index(header)
        csv_content = '\n'.join(lines[start_idx:])
        reader = csv.DictReader(io.StringIO(csv_content))

        phase_rows = []
        for row in reader:
            phase_name = row.get('phase', '').strip()
            if phase_name in self._phase_metric_map:
                metric_tag = self._phase_metric_map[phase_name]
                array_size = int(row['n_elements'])
one's avatar
one committed
117
118
119
120
121
122
123
124
125
126
127
128
                phase_rows.append(
                    {
                        'metric_name': self._get_phase_bw_metric_name(metric_tag, array_size),
                        'value': self._mbps_to_gbps(row['max_mbytes_per_sec']),
                    }
                )
                phase_rows.append(
                    {
                        'metric_name': self._get_phase_time_metric_name(metric_tag, array_size),
                        'value': float(row['runtime']),
                    }
                )
one's avatar
one committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

        if not phase_rows:
            raise ValueError('No valid phase rows found in CSV output.')

        return phase_rows

    def _parse_csv_function_rows(self, raw_output):
        """Extract function rows from BabelStream CSV output."""
        lines = [line.strip() for line in raw_output.strip().splitlines() if line.strip()]
        header = 'function,num_times,n_elements,sizeof,max_mbytes_per_sec,min_runtime,max_runtime,avg_runtime'
        if header not in lines:
            raise ValueError('No function CSV header found in output.')

        start_idx = lines.index(header)
        csv_content = '\n'.join(lines[start_idx:])
        reader = csv.DictReader(io.StringIO(csv_content))

        function_rows = []
        for row in reader:
            function_name = row.get('function', '').strip()
            if function_name in self._function_metric_map:
                metric_tag = self._function_metric_map[function_name]
                array_size = int(row['n_elements'])
one's avatar
one committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
                function_rows.append(
                    {
                        'metric_name': self._get_function_bw_metric_name(metric_tag, array_size),
                        'value': self._mbps_to_gbps(row['max_mbytes_per_sec']),
                    }
                )
                function_rows.append(
                    {
                        'metric_name': self._get_function_time_metric_name(metric_tag, array_size, 'min'),
                        'value': float(row['min_runtime']),
                    }
                )
                function_rows.append(
                    {
                        'metric_name': self._get_function_time_metric_name(metric_tag, array_size, 'max'),
                        'value': float(row['max_runtime']),
                    }
                )
                function_rows.append(
                    {
                        'metric_name': self._get_function_time_metric_name(metric_tag, array_size, 'avg'),
                        'value': float(row['avg_runtime']),
                    }
                )
one's avatar
one committed
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

        if not function_rows:
            raise ValueError('No valid function rows found in CSV output.')

        return function_rows

    def _get_phase_bw_metric_name(self, metric_tag, array_size):
        """Build phase bandwidth metric name."""
        return 'STREAM_{}_{}_array_{}_bw'.format(metric_tag, self._args.precision, array_size)

    def _get_phase_time_metric_name(self, metric_tag, array_size):
        """Build phase runtime metric name."""
        return 'STREAM_{}_{}_array_{}_time'.format(metric_tag, self._args.precision, array_size)

    def _get_function_bw_metric_name(self, metric_tag, array_size):
        """Build function bandwidth metric name."""
        return 'STREAM_{}_{}_array_{}_bw'.format(metric_tag, self._args.precision, array_size)

    def _get_function_time_metric_name(self, metric_tag, array_size, metric_type):
        """Build function runtime metric name."""
        return 'STREAM_{}_{}_array_{}_time_{}'.format(metric_tag, self._args.precision, array_size, metric_type)

    def _format_device_output(self, device_name, metrics):
        """Render one device section in a human-readable format."""
        metric_width = max(len(metric['metric_name']) for metric in metrics)
        output_lines = ['Device: {}'.format(device_name)]
        for metric in metrics:
one's avatar
one committed
203
204
205
            output_lines.append(
                '{:<{width}}  {:.6f}'.format(metric['metric_name'], metric['value'], width=metric_width)
            )
one's avatar
one committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
        return output_lines

    def _get_text_output_header(self):
        """Render benchmark metadata in the text output header."""
        return [
            'STREAM Benchmark (BabelStream backend)',
            'Array size(elements): {}'.format(self._args.array_size),
            'Number of loops: {}'.format(self._args.num_loops),
            'Precision: {}'.format(self._args.precision),
            'Bandwidth unit: GB/s (converted from MB/s / 1000)',
        ]

    def _parse_device_output(self, raw_output):
        """Parse one device output and return rendered lines and parsed metrics."""
        device_name = self._get_device_name(raw_output)
        metrics = self._parse_csv_phase_rows(raw_output) + self._parse_csv_function_rows(raw_output)
        rendered_lines = self._get_text_output_header()
        rendered_lines.append('')
        rendered_lines.extend(self._format_device_output(device_name, metrics))
        return rendered_lines, metrics

227
228
229
230
231
232
233
234
235
236
237
238
239
    def _process_raw_result(self, cmd_idx, raw_output):
        """Function to parse raw results and save the summarized results.

          self._result.add_raw_data() and self._result.add_result() need to be called to save the results.

        Args:
            cmd_idx (int): the index of command corresponding with the raw_output.
            raw_output (str): raw output string of the micro-benchmark.

        Return:
            True if the raw output string is valid and result can be extracted.
        """
        try:
one's avatar
one committed
240
241
242
243
            rendered_lines, metrics = self._parse_device_output(raw_output)
            self._result.add_raw_data('raw_output_' + str(cmd_idx), '\n'.join(rendered_lines), self._args.log_raw_data)
            for metric in metrics:
                self._result.add_result(metric['metric_name'], metric['value'])
244
245
246
247
248
249
250
251
252
253
254
255
256
        except BaseException as e:
            self._result.set_return_code(ReturnCode.MICROBENCHMARK_RESULT_PARSING_FAILURE)
            logger.error(
                'The result format is invalid - round: {}, benchmark: {}, raw output: {}, message: {}.'.format(
                    self._curr_run_index, self._name, raw_output, str(e)
                )
            )
            return False

        return True


BenchmarkRegistry.register_benchmark('gpu-stream', GpuStreamBenchmark)