|
14 | 14 | import iree.runtime as ireert |
15 | 15 | import iree.compiler as ireec |
16 | 16 | from shark.iree_utils._common import IREE_DEVICE_MAP, IREE_TARGET_MAP |
| 17 | +from shark.iree_utils.benchmark_utils import * |
17 | 18 | import numpy as np |
18 | 19 | import os |
| 20 | +import re |
19 | 21 |
|
20 | 22 | # Get the iree-compile arguments given device. |
21 | 23 | def get_iree_device_args(device, extra_args=[]): |
@@ -62,6 +64,125 @@ def get_iree_common_args(): |
62 | 64 | ] |
63 | 65 |
|
64 | 66 |
|
| 67 | +def create_dispatch_dirs(bench_dir, device): |
| 68 | + bench_dir_path = bench_dir.split("/") |
| 69 | + bench_dir_path[-1] = "temp_" + bench_dir_path[-1] |
| 70 | + tmp_bench_dir = "/".join(bench_dir_path) |
| 71 | + for f_ in os.listdir(bench_dir): |
| 72 | + if os.path.isfile(f"{bench_dir}/{f_}"): |
| 73 | + dir_name = re.sub("\.\S*$", "", f_) |
| 74 | + if os.path.exists(f"{bench_dir}/{dir_name}"): |
| 75 | + os.system(f"rm -rf {bench_dir}/{dir_name}") |
| 76 | + os.system(f"mkdir {bench_dir}/{dir_name}") |
| 77 | + os.system(f"mv {bench_dir}/{f_} {bench_dir}/{dir_name}/{f_}") |
| 78 | + for f_ in os.listdir(tmp_bench_dir): |
| 79 | + if os.path.isfile(f"{tmp_bench_dir}/{f_}"): |
| 80 | + dir_name = "" |
| 81 | + for d_ in os.listdir(bench_dir): |
| 82 | + if re.search(f"{d_}(?=\D)", f_): |
| 83 | + dir_name = d_ |
| 84 | + if dir_name != "": |
| 85 | + os.system( |
| 86 | + f"mv {tmp_bench_dir}/{f_} {bench_dir}/{dir_name}/{dir_name}_benchmark.mlir" |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def compile_benchmark_dirs(bench_dir, device, dispatch_benchmarks): |
| 91 | + dispatch_list = [] |
| 92 | + all_dispatches = False |
| 93 | + |
| 94 | + if dispatch_benchmarks.lower().strip() == "all": |
| 95 | + all_dispatches = True |
| 96 | + else: |
| 97 | + try: |
| 98 | + dispatch_list = [ |
| 99 | + int(dispatch_index) |
| 100 | + for dispatch_index in dispatch_benchmarks.split(" ") |
| 101 | + ] |
| 102 | + except: |
| 103 | + print("ERROR: Invalid dispatch benchmarks") |
| 104 | + return None |
| 105 | + for d_ in os.listdir(bench_dir): |
| 106 | + in_dispatches = False |
| 107 | + for dispatch in dispatch_list: |
| 108 | + if str(dispatch) in d_: |
| 109 | + in_dispatches = True |
| 110 | + if all_dispatches or in_dispatches: |
| 111 | + for f_ in os.listdir(f"{bench_dir}/{d_}"): |
| 112 | + |
| 113 | + if "benchmark.mlir" in f_: |
| 114 | + dispatch_file = open(f"{bench_dir}/{d_}/{f_}", "r") |
| 115 | + module = dispatch_file.read() |
| 116 | + dispatch_file.close() |
| 117 | + |
| 118 | + flatbuffer_blob = ireec.compile_str( |
| 119 | + module, target_backends=[IREE_TARGET_MAP[device]] |
| 120 | + ) |
| 121 | + |
| 122 | + vmfb_file = open( |
| 123 | + f"{bench_dir}/{d_}/{d_}_benchmark.vmfb", "wb" |
| 124 | + ) |
| 125 | + vmfb_file.write(flatbuffer_blob) |
| 126 | + vmfb_file.close() |
| 127 | + |
| 128 | + config = ireert.Config(IREE_DEVICE_MAP[device]) |
| 129 | + vm_module = ireert.VmModule.from_flatbuffer( |
| 130 | + config.vm_instance, flatbuffer_blob |
| 131 | + ) |
| 132 | + |
| 133 | + benchmark_cl = build_benchmark_args_non_tensor_input( |
| 134 | + input_file=f"{bench_dir}/{d_}/{d_}_benchmark.vmfb", |
| 135 | + device=device, |
| 136 | + inputs=(0,), |
| 137 | + mlir_dialect="linalg", |
| 138 | + function_name=vm_module.function_names[0], |
| 139 | + ) |
| 140 | + |
| 141 | + benchmark_bash = open( |
| 142 | + f"{bench_dir}/{d_}/{d_}_benchmark.sh", "w+" |
| 143 | + ) |
| 144 | + benchmark_bash.write("#!/bin/bash\n") |
| 145 | + benchmark_bash.write(" ".join(benchmark_cl)) |
| 146 | + benchmark_bash.close() |
| 147 | + |
| 148 | + benchmark_data = run_benchmark_module(benchmark_cl) |
| 149 | + |
| 150 | + benchmark_file = open( |
| 151 | + f"{bench_dir}/{d_}/{d_}_data.txt", "w+" |
| 152 | + ) |
| 153 | + benchmark_file.write(f"DISPATCH: {d_}\n") |
| 154 | + benchmark_file.write(str(benchmark_data) + "\n") |
| 155 | + benchmark_file.write( |
| 156 | + "SHARK BENCHMARK RESULT: " |
| 157 | + + str(1 / (benchmark_data * 0.001)) |
| 158 | + + "\n" |
| 159 | + ) |
| 160 | + benchmark_file.close() |
| 161 | + |
| 162 | + elif ".mlir" in f_ and "benchmark" not in f_: |
| 163 | + dispatch_file = open(f"{bench_dir}/{d_}/{f_}", "r") |
| 164 | + module = dispatch_file.read() |
| 165 | + dispatch_file.close() |
| 166 | + |
| 167 | + module = re.sub( |
| 168 | + "hal.executable private", |
| 169 | + "hal.executable public", |
| 170 | + module, |
| 171 | + ) |
| 172 | + |
| 173 | + flatbuffer_blob = ireec.compile_str( |
| 174 | + module, |
| 175 | + target_backends=[IREE_TARGET_MAP[device]], |
| 176 | + extra_args=["--compile-mode=hal-executable"], |
| 177 | + ) |
| 178 | + |
| 179 | + spirv_file = open( |
| 180 | + f"{bench_dir}/{d_}/{d_}_spirv.vmfb", "wb" |
| 181 | + ) |
| 182 | + spirv_file.write(flatbuffer_blob) |
| 183 | + spirv_file.close() |
| 184 | + |
| 185 | + |
65 | 186 | def compile_module_to_flatbuffer( |
66 | 187 | module, device, frontend, func_name, model_config_path, extra_args |
67 | 188 | ): |
|
0 commit comments