|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import pandas as pd |
| 6 | +import yaml |
| 7 | +import subprocess |
| 8 | +import shutil |
| 9 | +import re |
| 10 | +from collections import OrderedDict |
| 11 | + |
| 12 | + |
| 13 | +def get_perf_metrics(): |
| 14 | + pmc0 = OrderedDict() |
| 15 | + pmc0['SQ_INSTS_VALU_ADD_F16'] = {'value': 0, 'factor': 64, 'flop': 0} |
| 16 | + pmc0['SQ_INSTS_VALU_MUL_F16'] = {'value': 0, 'factor': 64, 'flop': 0} |
| 17 | + pmc0['SQ_INSTS_VALU_FMA_F16'] = {'value': 0, 'factor': 128, 'flop': 0} |
| 18 | + pmc0['SQ_INSTS_VALU_TRANS_F16'] = {'value': 0, 'factor': 64, 'flop': 0} |
| 19 | + |
| 20 | + pmc1 = OrderedDict() |
| 21 | + pmc1['SQ_INSTS_VALU_ADD_F32'] = {'value': 0, 'factor': 64, 'flop': 0} |
| 22 | + pmc1['SQ_INSTS_VALU_MUL_F32'] = {'value': 0, 'factor': 64, 'flop': 0} |
| 23 | + pmc1['SQ_INSTS_VALU_FMA_F32'] = {'value': 0, 'factor': 128, 'flop': 0} |
| 24 | + pmc1['SQ_INSTS_VALU_TRANS_F32'] = {'value': 0, 'factor': 64, 'flop': 0} |
| 25 | + |
| 26 | + pmc2 = OrderedDict() |
| 27 | + pmc2['SQ_INSTS_VALU_ADD_F64'] = {'value': 0, 'factor': 64, 'flop': 0} |
| 28 | + pmc2['SQ_INSTS_VALU_MUL_F64'] = {'value': 0, 'factor': 64, 'flop': 0} |
| 29 | + pmc2['SQ_INSTS_VALU_FMA_F64'] = {'value': 0, 'factor': 128, 'flop': 0} |
| 30 | + pmc2['SQ_INSTS_VALU_TRANS_F64'] = {'value': 0, 'factor': 64, 'flop': 0} |
| 31 | + |
| 32 | + pmc3 = OrderedDict() |
| 33 | + pmc3['SQ_INSTS_VALU_MFMA_MOPS_F16'] = {'value': 0, 'factor': 512, 'flop': 0} |
| 34 | + pmc3['SQ_INSTS_VALU_MFMA_MOPS_BF16'] = {'value': 0, 'factor': 512, 'flop': 0} |
| 35 | + pmc3['SQ_INSTS_VALU_MFMA_MOPS_F32'] = {'value': 0, 'factor': 512, 'flop': 0} |
| 36 | + pmc3['SQ_INSTS_VALU_MFMA_MOPS_F64'] = {'value': 0, 'factor': 512, 'flop': 0} |
| 37 | + |
| 38 | + pmc4 = OrderedDict() |
| 39 | + pmc4['GRBM_COUNT'] = {'value': 0} |
| 40 | + pmc4['TCC_HIT_sum'] = {'value': 0} |
| 41 | + pmc4['TCC_MISS_sum'] = {'value': 0} |
| 42 | + |
| 43 | + jobs = OrderedDict() |
| 44 | + jobs[0] = pmc0 |
| 45 | + jobs[1] = pmc1 |
| 46 | + jobs[2] = pmc2 |
| 47 | + jobs[3] = pmc3 |
| 48 | + jobs[4] = pmc4 |
| 49 | + return jobs |
| 50 | + |
| 51 | + |
| 52 | +def get_metrics_as_yaml(): |
| 53 | + perf_metrics = get_perf_metrics() |
| 54 | + pmcs = [ |
| 55 | + {'pmc': list(perf_metrics[0].keys())}, |
| 56 | + {'pmc': list(perf_metrics[1].keys())}, |
| 57 | + {'pmc': list(perf_metrics[2].keys())}, |
| 58 | + {'pmc': list(perf_metrics[3].keys())}, |
| 59 | + {'pmc': list(perf_metrics[4].keys())}, |
| 60 | + ] |
| 61 | + |
| 62 | + spec = {} |
| 63 | + spec['jobs'] = pmcs |
| 64 | + |
| 65 | + spec_str = yaml.dump(spec) |
| 66 | + return spec_str |
| 67 | + |
| 68 | + |
| 69 | +def run_external_binary(binary_path, arguments=[], verbose=False): |
| 70 | + try: |
| 71 | + # Run the external binary and capture its standard output |
| 72 | + cmd = [binary_path] + arguments if binary_path else arguments |
| 73 | + if verbose: |
| 74 | + print(f"CURR.CMD: {' '.join(cmd)}") |
| 75 | + result = subprocess.run(cmd, capture_output=True, text=True) |
| 76 | + |
| 77 | + # Check if the process was successful |
| 78 | + if result.returncode == 0: |
| 79 | + if result.stderr: |
| 80 | + print(result.stderr.strip()) |
| 81 | + return result.stdout.strip() |
| 82 | + else: |
| 83 | + cmd = ' '.join(cmd) |
| 84 | + raise RuntimeError(f'Error: The external binary returned non-zero exit code {result.returncode}. ' |
| 85 | + f'Attempted command:\n{cmd}') |
| 86 | + except FileNotFoundError: |
| 87 | + raise RuntimeError(f'Error: The binary could not be found - i.e., {binary_path}') |
| 88 | + except Exception as err: |
| 89 | + raise RuntimeError(f'Error: {str(err)}') |
| 90 | + |
| 91 | + |
| 92 | +def check_rocprofv3(): |
| 93 | + run_external_binary('which', ['rocprofv3']) |
| 94 | + |
| 95 | + |
| 96 | +def find_file(rootdir, regex): |
| 97 | + for root, _, files in os.walk(rootdir): |
| 98 | + for file in files: |
| 99 | + if regex.match(file): |
| 100 | + return os.path.join(root, file) |
| 101 | + |
| 102 | + |
| 103 | +def filter(df, name): |
| 104 | + return df[df['Kernel_Name'] == name] |
| 105 | + |
| 106 | + |
| 107 | +def process_files(metrics_dir, timing_dir, kernel_name, verbose): |
| 108 | + timing_file = find_file(timing_dir, re.compile(r'.*kernel_trace.csv')) |
| 109 | + df = pd.read_csv(timing_file) |
| 110 | + df = filter(df, kernel_name) |
| 111 | + timing = df['End_Timestamp'] - df['Start_Timestamp'] |
| 112 | + print('Timing info in `nsec`:') |
| 113 | + print(timing.describe()) |
| 114 | + print() |
| 115 | + |
| 116 | + # post process all passes |
| 117 | + num_flop_sum = 0 |
| 118 | + perf_metrics = get_perf_metrics() |
| 119 | + num_passes = 5 |
| 120 | + metrics_file_regex = re.compile(r'.*counter_collection.csv') |
| 121 | + for pass_id in range(1, num_passes + 1): |
| 122 | + search_dir = os.path.join(metrics_dir, f'pass_{pass_id}') |
| 123 | + metrics_file = find_file(search_dir, metrics_file_regex) |
| 124 | + df = pd.read_csv(metrics_file) |
| 125 | + df = filter(df, kernel_name) |
| 126 | + |
| 127 | + curr_metrics = perf_metrics[pass_id - 1] |
| 128 | + curr_metrics_names = list(curr_metrics.keys()) |
| 129 | + for name in curr_metrics_names: |
| 130 | + data = df[df['Counter_Name'] == name] |
| 131 | + value = data['Counter_Value'].mean() |
| 132 | + |
| 133 | + if 'flop' in curr_metrics[name].keys(): |
| 134 | + curr_metrics[name]['value'] = value |
| 135 | + num_flops = value * curr_metrics[name]['factor'] |
| 136 | + |
| 137 | + num_flop_sum += num_flops |
| 138 | + curr_metrics[name]['flop'] = num_flops |
| 139 | + else: |
| 140 | + curr_metrics[name]['value'] = value |
| 141 | + print() |
| 142 | + |
| 143 | + # Print data from non-flop-passes |
| 144 | + print('NON-FLOP related data:') |
| 145 | + table = {'Counter Name': [], 'Max': [], 'Min': [], 'Mean': [], 'Median': []} |
| 146 | + non_flop_passes = [5] |
| 147 | + for pass_id in non_flop_passes: |
| 148 | + search_dir = os.path.join(metrics_dir, f'pass_{pass_id}') |
| 149 | + metrics_file = find_file(search_dir, metrics_file_regex) |
| 150 | + df = pd.read_csv(metrics_file) |
| 151 | + df = filter(df, kernel_name) |
| 152 | + |
| 153 | + curr_metrics = perf_metrics[pass_id - 1] |
| 154 | + curr_metrics_names = list(curr_metrics.keys()) |
| 155 | + for name in curr_metrics_names: |
| 156 | + data = df[df['Counter_Name'] == name] |
| 157 | + values = data['Counter_Value'] |
| 158 | + table['Counter Name'].append(name) |
| 159 | + table['Max'].append(values.max()) |
| 160 | + table['Min'].append(values.min()) |
| 161 | + table['Mean'].append(values.mean()) |
| 162 | + table['Median'].append(values.median()) |
| 163 | + print(pd.DataFrame(table)) |
| 164 | + print() |
| 165 | + |
| 166 | + # Print data from flop-passes |
| 167 | + print('FLOP related data:') |
| 168 | + table = {'Counter Name': [], 'Raw Data': [], 'FLOP': [], 'Relative FLOP, %': []} |
| 169 | + flop_passes = [1, 2, 3, 4] |
| 170 | + for pass_id in flop_passes: |
| 171 | + search_dir = os.path.join(metrics_dir, f'pass_{pass_id}') |
| 172 | + metrics_file = find_file(search_dir, metrics_file_regex) |
| 173 | + df = pd.read_csv(metrics_file) |
| 174 | + df = filter(df, kernel_name) |
| 175 | + |
| 176 | + curr_metrics = perf_metrics[pass_id - 1] |
| 177 | + curr_metrics_names = list(curr_metrics.keys()) |
| 178 | + for name in curr_metrics_names: |
| 179 | + data = df[df['Counter_Name'] == name] |
| 180 | + value = data['Counter_Value'].mean() |
| 181 | + |
| 182 | + num_flops = curr_metrics[name]['flop'] |
| 183 | + relative_value = 100 * num_flops / num_flop_sum |
| 184 | + table['Counter Name'].append(name) |
| 185 | + table['Raw Data'].append(value) |
| 186 | + table['FLOP'].append(num_flops) |
| 187 | + table['Relative FLOP, %'].append(relative_value) |
| 188 | + print(pd.DataFrame(table)) |
| 189 | + print() |
| 190 | + |
| 191 | + print('Performance info in TFLOP/s:') |
| 192 | + performance = num_flop_sum / (timing * 1000) |
| 193 | + print(performance.describe()) |
| 194 | + print() |
| 195 | + |
| 196 | + |
| 197 | +def main(args): |
| 198 | + check_rocprofv3() |
| 199 | + |
| 200 | + curr_dir = os.path.dirname(os.path.abspath(__file__)) |
| 201 | + metrics_spec = get_metrics_as_yaml() |
| 202 | + metrics_spec_path = os.path.join(curr_dir, "metrics_spec.yaml") |
| 203 | + with open(metrics_spec_path, 'w') as file: |
| 204 | + file.write(metrics_spec) |
| 205 | + |
| 206 | + metrics_dir = os.path.join(curr_dir, "metrics_dir") |
| 207 | + timing_dir = os.path.join(curr_dir, "timing_dir") |
| 208 | + |
| 209 | + if not args.display_only: |
| 210 | + # test original command |
| 211 | + if (args.verbose): |
| 212 | + print('running original program...') |
| 213 | + user_cmd = args.cmd |
| 214 | + output = run_external_binary([], user_cmd, args.verbose) |
| 215 | + |
| 216 | + if (args.verbose): |
| 217 | + print(output) |
| 218 | + |
| 219 | + # collect performance metrices |
| 220 | + if (args.verbose): |
| 221 | + print('running rocprofv3 passes...') |
| 222 | + |
| 223 | + if os.path.exists(metrics_dir): |
| 224 | + shutil.rmtree(metrics_dir) |
| 225 | + |
| 226 | + rocprof_cmd = ['rocprofv3', '-i', metrics_spec_path, '-d', metrics_dir, '--'] |
| 227 | + output = run_external_binary([], rocprof_cmd + user_cmd, args.verbose) |
| 228 | + |
| 229 | + if (args.verbose): |
| 230 | + print(output) |
| 231 | + |
| 232 | + # collect timing |
| 233 | + if (args.verbose): |
| 234 | + print('running rocprofv3 for timing info...') |
| 235 | + |
| 236 | + if os.path.exists(timing_dir): |
| 237 | + shutil.rmtree(timing_dir) |
| 238 | + |
| 239 | + rocprof_cmd = ['rocprofv3', '--kernel-trace', '-d', timing_dir, '--'] |
| 240 | + output = run_external_binary([], rocprof_cmd + user_cmd, args.verbose) |
| 241 | + |
| 242 | + if (args.verbose): |
| 243 | + print(output) |
| 244 | + |
| 245 | + process_files(metrics_dir, timing_dir, args.kernel, args.verbose) |
| 246 | + |
| 247 | + |
| 248 | +if __name__ == "__main__": |
| 249 | + parser = argparse.ArgumentParser() |
| 250 | + parser.add_argument("-k", "--kernel", type=str, required=True, help="name of a kernel") |
| 251 | + parser.add_argument('-c', '--cmd', required=True, nargs=argparse.REMAINDER, help='user command') |
| 252 | + parser.add_argument("--display-only", action='store_true', help='display info without running') |
| 253 | + parser.add_argument("-v", "--verbose", action='store_true', help='verbose output') |
| 254 | + args = parser.parse_args() |
| 255 | + |
| 256 | + main(args) |
0 commit comments