|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +"""Module of the ROCm composable kernel GEMM benchmark.""" |
| 5 | + |
| 6 | +import os |
| 7 | +import re |
| 8 | + |
| 9 | +from superbench.common.utils import logger |
| 10 | +from superbench.benchmarks import BenchmarkRegistry, Platform, ReturnCode |
| 11 | +from superbench.benchmarks.micro_benchmarks import BlasLtBaseBenchmark |
| 12 | + |
| 13 | + |
| 14 | +class RocmComposableKernelBenchmark(BlasLtBaseBenchmark): |
| 15 | + """The composable kernel GEMM benchmark class.""" |
| 16 | + def __init__(self, name, parameters=''): |
| 17 | + """Constructor. |
| 18 | +
|
| 19 | + Args: |
| 20 | + name (str): benchmark name. |
| 21 | + parameters (str): benchmark parameters. |
| 22 | + """ |
| 23 | + super().__init__(name, parameters) |
| 24 | + |
| 25 | + self._bin_name = 'ckProfiler' |
| 26 | + self._in_types = ['fp32', 'fp16', 'bf16', 'fp8', 'int8'] |
| 27 | + self._in_type_map = { |
| 28 | + 'fp16': '1', |
| 29 | + 'fp32': '0', |
| 30 | + 'bf16': '2', |
| 31 | + 'fp8': '4', |
| 32 | + 'int8': '3', |
| 33 | + } |
| 34 | + |
| 35 | + def add_parser_arguments(self): |
| 36 | + """Add the specified arguments.""" |
| 37 | + super().add_parser_arguments() |
| 38 | + |
| 39 | + self._parser.add_argument( |
| 40 | + '--in_types', |
| 41 | + type=str, |
| 42 | + nargs='+', |
| 43 | + default=['fp16'], |
| 44 | + required=False, |
| 45 | + help='List of input data types, support {}.'.format(' '.join(self._in_types)), |
| 46 | + ) |
| 47 | + self._parser.add_argument( |
| 48 | + '--initialization', |
| 49 | + type=str, |
| 50 | + default='int', |
| 51 | + choices=['float', 'int'], |
| 52 | + required=False, |
| 53 | + help='Initialize matrix data.', |
| 54 | + ) |
| 55 | + self._parser.add_argument( |
| 56 | + '--matrixA_layout', |
| 57 | + type=str, |
| 58 | + default='row', |
| 59 | + choices=['row', 'col'], |
| 60 | + required=False, |
| 61 | + help='Matrix A Layout. RowMajor or ColMajor.', |
| 62 | + ) |
| 63 | + self._parser.add_argument( |
| 64 | + '--matrixB_layout', |
| 65 | + type=str, |
| 66 | + default='row', |
| 67 | + choices=['row', 'col'], |
| 68 | + required=False, |
| 69 | + help='Matrix B Layout. RowMajor or ColMajor.', |
| 70 | + ) |
| 71 | + self._parser.add_argument( |
| 72 | + '--check_data', |
| 73 | + action='store_true', |
| 74 | + required=False, |
| 75 | + help='Whether check data correctness.', |
| 76 | + ) |
| 77 | + self._parser.add_argument( |
| 78 | + '--splitk', |
| 79 | + type=int, |
| 80 | + default=None, |
| 81 | + required=False, |
| 82 | + nargs='+', |
| 83 | + help='Split K dimension.', |
| 84 | + ) |
| 85 | + self._parser.add_argument( |
| 86 | + '--streamk', |
| 87 | + type=int, |
| 88 | + default=None, |
| 89 | + required=False, |
| 90 | + nargs='+', |
| 91 | + help='Stream K blocks.', |
| 92 | + ) |
| 93 | + |
| 94 | + def _preprocess(self): |
| 95 | + """Preprocess/preparation operations before the benchmarking. |
| 96 | +
|
| 97 | + Return: |
| 98 | + True if _preprocess() succeed. |
| 99 | + """ |
| 100 | + if not super()._preprocess(): |
| 101 | + return False |
| 102 | + |
| 103 | + self.__bin_path = os.path.join(self._args.bin_dir, self._bin_name) |
| 104 | + |
| 105 | + self._commands = [] |
| 106 | + self._precision_in_commands = [] |
| 107 | + matrix_layout = '0' |
| 108 | + if self._args.matrixA_layout == 'row' and self._args.matrixB_layout == 'row': |
| 109 | + matrix_layout = '0' |
| 110 | + elif self._args.matrixA_layout == 'row' and self._args.matrixB_layout == 'col': |
| 111 | + matrix_layout = '1' |
| 112 | + elif self._args.matrixA_layout == 'col' and self._args.matrixB_layout == 'row': |
| 113 | + matrix_layout = '2' |
| 114 | + elif self._args.matrixA_layout == 'col' and self._args.matrixB_layout == 'col': |
| 115 | + matrix_layout = '3' |
| 116 | + if self._args.check_data: |
| 117 | + self._args.check_data = '1' |
| 118 | + else: |
| 119 | + self._args.check_data = '0' |
| 120 | + init = 1 if self._args.initialization == 'int' else 2 |
| 121 | + for (_m, _n, _k, _b, _in_type) in self._shapes_to_run: |
| 122 | + params = f'{self._in_type_map[_in_type]}' + \ |
| 123 | + f' {matrix_layout} {self._args.check_data} {init} 0 1' + \ |
| 124 | + f' {_m} {_n} {_k} -1 -1 -1' |
| 125 | + command = f'{self.__bin_path} gemm {params} {self._args.num_warmup} {self._args.num_steps}' |
| 126 | + self._commands.append(command) |
| 127 | + logger.info(command) |
| 128 | + if self._args.splitk: |
| 129 | + if not isinstance(self._args.splitk, list): |
| 130 | + self._args.splitk = [self._args.splitk] |
| 131 | + for splitk in self._args.splitk: |
| 132 | + command = f'{self.__bin_path} gemm_splitk {params} {splitk}' + \ |
| 133 | + f' {self._args.num_warmup} {self._args.num_steps}' |
| 134 | + self._commands.append(command) |
| 135 | + logger.info(command) |
| 136 | + if self._args.streamk: |
| 137 | + if not isinstance(self._args.streamk, list): |
| 138 | + self._args.streamk = [self._args.streamk] |
| 139 | + for streamk in self._args.streamk: |
| 140 | + command = f'{self.__bin_path} gemm_streamk {params} {streamk}' + \ |
| 141 | + f' {self._args.num_warmup} {self._args.num_steps}' |
| 142 | + self._commands.append(command) |
| 143 | + logger.info(command) |
| 144 | + return True |
| 145 | + |
| 146 | + def _process_raw_result(self, cmd_idx, raw_output): |
| 147 | + """Function to parse raw results and save the summarized results. |
| 148 | +
|
| 149 | + self._result.add_raw_data() and self._result.add_result() need to be called to save the results. |
| 150 | +
|
| 151 | + Args: |
| 152 | + cmd_idx (int): the index of command corresponding with the raw_output. |
| 153 | + raw_output (str): raw output string of the micro-benchmark. |
| 154 | +
|
| 155 | + Return: |
| 156 | + True if the raw output string is valid and result can be extracted. |
| 157 | + """ |
| 158 | + self._result.add_raw_data(f'raw_output_{cmd_idx}', raw_output, self._args.log_raw_data) |
| 159 | + |
| 160 | + try: |
| 161 | + lines = raw_output.splitlines() |
| 162 | + index = None |
| 163 | + |
| 164 | + # Find the line containing 'hipblaslt-Gflops' |
| 165 | + for i, line in enumerate(lines): |
| 166 | + if 'Best Perf' in line: |
| 167 | + index = i |
| 168 | + break |
| 169 | + |
| 170 | + if index is not None: |
| 171 | + # Search the text for each pattern |
| 172 | + datatype_match = re.search(r"datatype = (\w+)", line) |
| 173 | + m_match = re.search(r"M = (\d+)", line) |
| 174 | + n_match = re.search(r"N = (\d+)", line) |
| 175 | + k_match = re.search(r"K = (\d+)", line) |
| 176 | + flops_match = re.search(r"(\d+\.?\d*) TFlops", line) |
| 177 | + |
| 178 | + # Extract the matched groups |
| 179 | + datatype = datatype_match.group(1) if datatype_match else None |
| 180 | + m = int(m_match.group(1)) if m_match else None |
| 181 | + n = int(n_match.group(1)) if n_match else None |
| 182 | + k = int(k_match.group(1)) if k_match else None |
| 183 | + flops = float(flops_match.group(1)) if flops_match else None |
| 184 | + |
| 185 | + metric = f'{datatype}_{m}_{n}_{k}_flops' |
| 186 | + self._result.add_result(metric, flops) |
| 187 | + else: |
| 188 | + self._result.set_return_code(ReturnCode.MICROBENCHMARK_RESULT_PARSING_FAILURE) |
| 189 | + logger.error( |
| 190 | + 'The result format is invalid - round: {}, benchmark: {}, raw output: {}.'.format( |
| 191 | + self._curr_run_index, self._name, raw_output |
| 192 | + ) |
| 193 | + ) |
| 194 | + return False |
| 195 | + |
| 196 | + except BaseException as e: |
| 197 | + self._result.set_return_code(ReturnCode.MICROBENCHMARK_RESULT_PARSING_FAILURE) |
| 198 | + logger.error( |
| 199 | + 'The result format is invalid - round: {}, benchmark: {}, raw output: {}, message: {}.'.format( |
| 200 | + self._curr_run_index, self._name, raw_output, str(e) |
| 201 | + ) |
| 202 | + ) |
| 203 | + return False |
| 204 | + finally: |
| 205 | + if cmd_idx == len(self._commands) - 1: |
| 206 | + for metric in self.results: |
| 207 | + self.results[metric] = [max(self.results[metric])] |
| 208 | + return True |
| 209 | + |
| 210 | + |
| 211 | +BenchmarkRegistry.register_benchmark('composable-kernel-gemm', RocmComposableKernelBenchmark, platform=Platform.ROCM) |
0 commit comments