Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 1 | # Copyright (c) 2019-2021 Arm Limited. |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 2 | # |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | # |
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | # of this software and associated documentation files (the "Software"), to |
| 7 | # deal in the Software without restriction, including without limitation the |
| 8 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 | # sell copies of the Software, and to permit persons to whom the Software is |
| 10 | # furnished to do so, subject to the following conditions: |
| 11 | # |
| 12 | # The above copyright notice and this permission notice shall be included in all |
| 13 | # copies or substantial portions of the Software. |
| 14 | # |
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | # SOFTWARE. |
| 22 | |
| 23 | #!/bin/sh |
| 24 | |
| 25 | # Global: Global variables and global settings {{{ |
| 26 | # Treat unset variables as an error when substituting |
| 27 | set -u |
| 28 | |
| 29 | CMD=$( basename $0 ) |
| 30 | |
| 31 | # All supported strategy options |
SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 32 | ALL_STRATEGY_OPTIONS=("native" "reshaped_rhs_only" "reshaped") |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 33 | |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 34 | # All supported data type options |
| 35 | ALL_DATA_TYPE_OPTIONS=("f32" "f16" "qasymm8") |
| 36 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 37 | # Names of example binary for each strategy |
SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 38 | EXAMPLE_BIN_NATIVE="benchmark_cl_gemm_native" |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 39 | EXAMPLE_BIN_RESHAPED_RHS_ONLY="benchmark_cl_gemm_reshaped_rhs_only" |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 40 | EXAMPLE_BIN_RESHAPED="benchmark_cl_gemm_reshaped" |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 41 | EXAMPLE_BIN_RESHAPED_RHS_ONLY_LOWP="benchmark_cl_gemmlowp_reshaped_rhs_only_fused_output_stage_fixedpoint" |
| 42 | EXAMPLE_BIN_RESHAPED_LOWP="benchmark_cl_gemmlowp_reshaped" |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 43 | |
Eren Kopuz | 6977b37 | 2020-07-13 12:37:06 +0100 | [diff] [blame] | 44 | # Default data type |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 45 | DEFAULT_DATA_TYPE="f32" |
Eren Kopuz | 6977b37 | 2020-07-13 12:37:06 +0100 | [diff] [blame] | 46 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 47 | # Default output directory |
| 48 | DEFAULT_OUT_DIR="out" |
| 49 | |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 50 | # Default ID of the first experiment |
| 51 | DEFAULT_ID_EXPERIMENT_START=0 |
| 52 | |
| 53 | # Default total number of experiments |
| 54 | DEFAULT_NUM_EXPERIMENTS="all" |
| 55 | |
| 56 | # Default output file extension |
| 57 | DEFAULT_OUT_EXTENSION="gemm_benchmark" |
| 58 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 59 | # Number of iterations for each benchmark run |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 60 | NUM_ITERATION=5 |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 61 | # Global }}} |
| 62 | |
| 63 | # Functions {{{ |
| 64 | ####################################### |
| 65 | # Print gemm shape file help message |
| 66 | # Globals: |
| 67 | # None |
| 68 | # Arguments: |
| 69 | # None |
| 70 | # Returns: |
| 71 | # None |
| 72 | ####################################### |
| 73 | function help_gemm_shape_file() { |
| 74 | cat >&2 << EOF |
| 75 | Gemm shape file: |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 76 | Gemm shape file is a csv file with fields separated by commas. The optional header and comments are ignored by the parser. |
SiCong Li | 8abbabd | 2020-04-03 12:39:41 +0100 | [diff] [blame] | 77 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 78 | A gemm shape is a list of 4 positive integers <M, N, K, B> describing the shapes of the two matrices (LHS and RHS) |
| 79 | with: |
| 80 | M - Number of lhs matrix rows |
| 81 | N - Number of rhs matrix columns |
| 82 | K - Number of lhs matrix columns/rhs matrix rows |
| 83 | B - Batch size |
| 84 | |
| 85 | An example gemm shape file looks like: |
| 86 | 100,100,30,1 |
| 87 | 100,100,30,3 |
| 88 | ... |
| 89 | |
| 90 | EOF |
| 91 | } |
| 92 | |
| 93 | ####################################### |
SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 94 | # Print gemm config file for native help message |
| 95 | # Globals: |
| 96 | # None |
| 97 | # Arguments: |
| 98 | # None |
| 99 | # Returns: |
| 100 | # None |
| 101 | ####################################### |
| 102 | function help_gemm_config_file_native() { |
| 103 | cat >&2 << EOF |
| 104 | Gemm config file (Strategy native): |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 105 | Gemm config file is a csv file with fields separated by commas. The optional header and comments are ignored by the parser. |
SiCong Li | 8abbabd | 2020-04-03 12:39:41 +0100 | [diff] [blame] | 106 | |
| 107 | A gemm config is a list of 3 positive integers <m0, n0, k0>, with: |
SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 108 | m0 - Number of rows processed by the matrix multiplication |
| 109 | n0 - Number of columns processed by the matrix multiplication |
| 110 | k0 - Number of partial accumulations performed by the matrix multiplication |
| 111 | |
| 112 | Only the following configurations of M0, N0 and K0 are currently supported: |
| 113 | M0 = 1, 2, 3, 4, 5, 6, 7, 8 |
| 114 | N0 = 2, 3, 4, 8, 16 |
| 115 | K0 = 2, 3, 4, 8, 16 |
| 116 | |
| 117 | An example gemm config file looks like: |
| 118 | 1,4,4 |
| 119 | 2,3,8 |
| 120 | ... |
| 121 | |
| 122 | EOF |
| 123 | } |
| 124 | |
| 125 | ####################################### |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 126 | # Print gemm config file for reshaped_rhs_only help message |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 127 | # Globals: |
| 128 | # None |
| 129 | # Arguments: |
| 130 | # None |
| 131 | # Returns: |
| 132 | # None |
| 133 | ####################################### |
| 134 | function help_gemm_config_file_reshaped_rhs_only() { |
| 135 | cat >&2 << EOF |
| 136 | Gemm config file (Strategy reshaped_rhs_only): |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 137 | Gemm config file is a csv file with fields separated by commas. The optional header and comments are ignored by the parser. |
SiCong Li | 8abbabd | 2020-04-03 12:39:41 +0100 | [diff] [blame] | 138 | |
SiCong Li | b10181b | 2020-08-11 13:00:20 +0100 | [diff] [blame] | 139 | A gemm config is a list of 4 positive integers <m0, n0, k0, h0> and 3 boolean values: |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 140 | m0 - Number of rows processed by the matrix multiplication |
| 141 | n0 - Number of columns processed by the matrix multiplication |
| 142 | k0 - Number of partial accumulations performed by the matrix multiplication |
| 143 | h0 - Number of horizontal blocks of size (k0xn0) stored on the same output row |
| 144 | interleave_rhs - Interleave rhs matrix (1) / Do not interleave rhs matrix (0) |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 145 | transpose_rhs - Transpose rhs matrix (1) / Do not transpose rhs matrix (0) |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 146 | export_to_cl_image_rhs - (Not supported for quantized types) Export rhs matrix to cl_image (1) / Do not export rhs matrix to cl_image (0). Can only be true |
SiCong Li | b10181b | 2020-08-11 13:00:20 +0100 | [diff] [blame] | 147 | with certain combinations of the GEMMParams and other configs. Please refer to CLGEMMReshapeRHSMatrixKernel |
| 148 | for more details |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 149 | |
| 150 | Only the following configurations of M0, N0 and K0 are currently supported: |
| 151 | M0 = 1, 2, 3, 4, 5, 6, 7, 8 |
| 152 | N0 = 2, 3, 4, 8, 16 |
| 153 | K0 = 2, 3, 4, 8, 16 |
| 154 | H0 >= 1 |
| 155 | |
| 156 | An example gemm config file looks like: |
Eren Kopuz | 15205d9 | 2020-07-17 15:13:39 +0100 | [diff] [blame] | 157 | 4,4,4,1,1,1,0 |
| 158 | 4,4,4,3,1,0,1 |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 159 | ... |
| 160 | |
| 161 | EOF |
| 162 | } |
| 163 | |
| 164 | ####################################### |
| 165 | # Print gemm config file for reshaped help message |
| 166 | # Globals: |
| 167 | # None |
| 168 | # Arguments: |
| 169 | # None |
| 170 | # Returns: |
| 171 | # None |
| 172 | ####################################### |
| 173 | function help_gemm_config_file_reshaped() { |
| 174 | cat >&2 << EOF |
| 175 | Gemm config file (Strategy reshaped): |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 176 | Gemm config file is a csv file with fields separated by commas. The header and comments are ignored by the parser. |
SiCong Li | 8abbabd | 2020-04-03 12:39:41 +0100 | [diff] [blame] | 177 | |
SiCong Li | b10181b | 2020-08-11 13:00:20 +0100 | [diff] [blame] | 178 | A gemm config is a list of 5 positive integers <m0, n0, k0, v0, h0> and 4 boolean values: |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 179 | m0 - Number of rows processed by the matrix multiplication |
| 180 | n0 - Number of columns processed by the matrix multiplication |
| 181 | k0 - Number of partial accumulations performed by the matrix multiplication |
| 182 | v0 - Number of vertical blocks of size (m0xk0) stored on the same output row |
| 183 | h0 - Number of horizontal blocks of size (k0xn0) stored on the same output row |
| 184 | interleave_lhs - Interleave lhs matrix (1) / Do not interleave lhs matrix (0) |
| 185 | interleave_rhs - Interleave rhs matrix (1) / Do not interleave rhs matrix (0) |
| 186 | transpose_rhs - Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do transpose lhs matrix (0) |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 187 | export_to_cl_image_rhs - (Not supported for quantized types) Export rhs matrix to cl_image (1) / Do not export rhs matrix to cl_image (0). Can only be true |
SiCong Li | b10181b | 2020-08-11 13:00:20 +0100 | [diff] [blame] | 188 | with certain combinations of the GEMMParams and other configs. Please refer to CLGEMMReshapeRHSMatrixKernel |
| 189 | for more details |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 190 | |
| 191 | If rhs matrix is transposed only the following configurations are currently supported: |
| 192 | M0 = 2, 3, 4, 5, 6, 7, 8 |
| 193 | N0 = 2, 3, 4, 8, 16 |
| 194 | K0 = 2, 3, 4, 8, 16 |
| 195 | V0 >= 1 |
| 196 | H0 >= 1 |
| 197 | |
| 198 | If lhs matrix is transposed only the following configurations are currently supported: |
| 199 | M0 = 2, 3, 4, 8 |
| 200 | N0 = 2, 3, 4, 8, 16 |
| 201 | K0 = 2, 3, 4, 8, 16 |
| 202 | V0 >= 1 |
| 203 | H0 >= 1 |
| 204 | |
| 205 | An example gemm config file looks like: |
Eren Kopuz | 15205d9 | 2020-07-17 15:13:39 +0100 | [diff] [blame] | 206 | 4,4,4,1,3,1,1,1,0 |
| 207 | 4,4,4,3,3,1,1,0,1 |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 208 | ... |
| 209 | |
| 210 | EOF |
| 211 | } |
| 212 | |
| 213 | ####################################### |
| 214 | # Print usage of this program and exit with Error |
| 215 | # Globals: |
| 216 | # Assumes all globals are required |
| 217 | # Arguments: |
| 218 | # None |
| 219 | # Returns: |
| 220 | # Error(1) |
| 221 | ####################################### |
| 222 | function usage() { |
| 223 | cat >&2 << EOF |
| 224 | Run gemm examples of a selected strategy, over provided tunable configurationsa and gemm shapes. |
| 225 | Save the benchmark results to json files in an output directory. |
| 226 | |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 227 | Usage: ${CMD} [-h] -s <strategy> -e <example_binary_dir> -g <gemm_shape_file> -c <gemm_config_file> [-o <out_dir>] [-d <data_type>] [-i <id_experiment_start>] [-n <num_experiments>] [-t <output_extension>] |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 228 | |
| 229 | Options: |
| 230 | -h |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 231 | Print help messages. If a strategy is specified with -s <strategy>, then only display messages relevant to that |
| 232 | strategy. Otherwise if no strategy is specified, display messages for all available strategies. |
| 233 | |
| 234 | -s <strategy> |
| 235 | Strategy option. |
| 236 | Options: ${ALL_STRATEGY_OPTIONS[@]}. |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 237 | |
| 238 | -e <example_binary_dir> |
| 239 | Path to directory that holds all example binaries |
| 240 | |
| 241 | -g <gemm_shape_file> |
| 242 | Path to gemm shape csv file |
| 243 | |
| 244 | -c <gemm_config_file> |
| 245 | Path to gemm config csv file |
| 246 | |
Eren Kopuz | 6977b37 | 2020-07-13 12:37:06 +0100 | [diff] [blame] | 247 | -d <data_type> |
| 248 | Data type option with which to run benchmark examples |
| 249 | Default: ${DEFAULT_DATA_TYPE} |
Eren Kopuz | 15205d9 | 2020-07-17 15:13:39 +0100 | [diff] [blame] | 250 | Supported options: |
| 251 | Strategy : Data Types |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 252 | Native : f32 |
| 253 | Reshaped : f32, f16, qasymm8 |
| 254 | Reshaped RHS Only : f32, f16, qasymm8 |
Eren Kopuz | 6977b37 | 2020-07-13 12:37:06 +0100 | [diff] [blame] | 255 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 256 | -o <out_dir> |
| 257 | Path to output directory that holds output json files |
| 258 | Default: ${DEFAULT_OUT_DIR} |
| 259 | |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 260 | -i <id_experiment_start> |
| 261 | ID of the first experiment. |
| 262 | Default: ${DEFAULT_ID_EXPERIMENT_START} |
| 263 | |
| 264 | -n <num_experiments> |
| 265 | Total number of experiments to execute in this session. [1-all] |
| 266 | Default: ${DEFAULT_NUM_EXPERIMENTS} |
| 267 | |
| 268 | -t <output_extension> |
| 269 | Output file extension. |
| 270 | Default: ${DEFAULT_OUT_EXTENSION} |
| 271 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 272 | EOF |
| 273 | # Print help messages about gemm shapes and various gemm configs |
| 274 | $HELP && help_gemm_shape_file |
SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 275 | $HELP && ( [ "${STRATEGY_OPTION}" == "" ] || [ "${STRATEGY_OPTION}" == "native" ] ) && help_gemm_config_file_native |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 276 | $HELP && ( [ "${STRATEGY_OPTION}" == "" ] || [ "${STRATEGY_OPTION}" == "reshaped_rhs_only" ] ) && help_gemm_config_file_reshaped_rhs_only |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 277 | $HELP && ( [ "${STRATEGY_OPTION}" == "" ] || [ "${STRATEGY_OPTION}" == "reshaped" ] ) && help_gemm_config_file_reshaped |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 278 | exit 1 |
| 279 | } |
| 280 | |
| 281 | ####################################### |
| 282 | # Print error message and exit with Error. |
| 283 | # Globals: |
| 284 | # None |
| 285 | # Arguments: |
| 286 | # $1 - Error message |
| 287 | # Returns: |
| 288 | # None |
| 289 | ####################################### |
| 290 | function error_msg() { |
| 291 | echo "Error: $1" 1>&2 |
| 292 | exit 1 |
| 293 | } |
| 294 | |
| 295 | ####################################### |
| 296 | # Convert string to lower-case |
| 297 | # Globals: |
| 298 | # None |
| 299 | # Arguments: |
| 300 | # target - String |
| 301 | # Returns: |
| 302 | # (stdout) - String in lowercase |
| 303 | ####################################### |
| 304 | function to_lower() { |
| 305 | local target=$1 |
| 306 | echo "$target" | tr '[:upper:]' '[:lower:]' |
| 307 | } |
| 308 | |
| 309 | ####################################### |
| 310 | # Test if the argument is an integer |
| 311 | # Globals: |
| 312 | # None |
| 313 | # Arguments: |
| 314 | # in - Input |
| 315 | # Returns: |
| 316 | # true/false |
| 317 | ####################################### |
| 318 | function is_integer() { |
| 319 | local in=$1 |
| 320 | [ "$in" -eq "$in" ] 2> /dev/null |
| 321 | } |
| 322 | |
| 323 | ####################################### |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 324 | # Test if a string is in an array of strings |
| 325 | # Globals: |
| 326 | # None |
| 327 | # Arguments: |
| 328 | # target - String to test |
| 329 | # array - Array of strings to search |
| 330 | # Returns: |
| 331 | # true/false |
| 332 | ####################################### |
| 333 | function arr_contains() { |
| 334 | local target=$1 |
| 335 | shift |
| 336 | local array |
| 337 | array=("$@") |
| 338 | for s in "${array[@]}" |
| 339 | do |
| 340 | [ "$s" == "${target}" ] && return |
| 341 | done |
| 342 | false |
| 343 | } |
| 344 | |
| 345 | ####################################### |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 346 | # Run a single example with all tunable gemm configurations on all gemm parameters |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 347 | # Globals: |
| 348 | # OUT_DIR |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 349 | # OUT_EXTENSION |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 350 | # EXAMPLE_BIN_DIR |
| 351 | # NUM_ITERATION |
| 352 | # GEMM_CONFIGS_FILE |
| 353 | # GEMM_SHAPES_FILE |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 354 | # STRATEGY_OPTION |
| 355 | # DATA_TYPE |
| 356 | # OUT_DIR |
| 357 | # ID_EXPERIMENT_START |
| 358 | # NUM_EXPERIMENTS |
| 359 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 360 | # Arguments: |
| 361 | # example_bin Name of the example binary to run |
| 362 | # Returns: |
| 363 | # None |
| 364 | ####################################### |
| 365 | function run() { |
| 366 | local example_bin=$1 |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 367 | echo "Running experiments for ${example_bin}" 1>&2 |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 368 | local example_args |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 369 | local json_filename |
| 370 | local expr_count=0 |
| 371 | # Total number of experiments available |
| 372 | local num_experiments_total |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 373 | # Total number of experiment runs scheduled for this session |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 374 | local num_experiments_session |
| 375 | local id_experiment_start |
| 376 | local id_experiment_end |
| 377 | local array_shapes |
| 378 | local array_configs |
| 379 | local array_shapes_len |
| 380 | local array_configs_len |
| 381 | local array_shapes_idx |
| 382 | local array_configs_idx |
Gian Marco Iodice | d72bd12 | 2020-08-12 18:39:55 +0100 | [diff] [blame] | 383 | local match_expression_shape="^([^,]*,){3}[^,]*$" |
| 384 | local match_expression_config="^(\s*[0-9]+\s*,)+\s*[0-9]\s*$" |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 385 | local shapes_list_cmd="grep -E "$match_expression_shape" "${GEMM_SHAPES_FILE}"" |
| 386 | local configs_list_cmd="grep -E "$match_expression_config" "${GEMM_CONFIGS_FILE}"" |
| 387 | |
| 388 | # Create array from CSV file |
| 389 | array_shapes=($( $shapes_list_cmd )) |
| 390 | array_configs=($( $configs_list_cmd )) |
| 391 | |
| 392 | # Get array length |
| 393 | array_shapes_len=${#array_shapes[@]} |
| 394 | array_configs_len=${#array_configs[@]} |
| 395 | |
| 396 | # Get the total number of experiments available |
| 397 | (( num_experiments_total=${array_shapes_len} * ${array_configs_len} )) |
| 398 | |
| 399 | # Get the number of experiments to execute in this session |
| 400 | if [ ${NUM_EXPERIMENTS} == ${DEFAULT_NUM_EXPERIMENTS} ] |
| 401 | then |
| 402 | (( num_experiments_session=${array_shapes_len} * ${array_configs_len} )) |
| 403 | else |
| 404 | num_experiments_session=$NUM_EXPERIMENTS |
| 405 | fi |
| 406 | |
| 407 | # Id experiment start |
| 408 | id_experiment_start=${ID_EXPERIMENT_START} |
| 409 | |
| 410 | # Id experiment end |
| 411 | (( id_experiment_end=(${num_experiments_session} + ${id_experiment_start} - 1) )) |
| 412 | |
| 413 | # Check if the id experiment end is grater than or equal to the total number of experiments available. |
| 414 | # If the condition is satisfied, clamp the id experiment end |
| 415 | if [ "$id_experiment_end" -ge "$num_experiments_total" ] |
| 416 | then |
| 417 | echo "Clamping idx experiment end" 1>&2 |
| 418 | (( id_experiment_end=${num_experiments_total} - 1 )) |
| 419 | (( num_experiments_session=${id_experiment_start} + ${id_experiment_end} + 1 )) |
| 420 | fi |
| 421 | |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 422 | # Time elapsed since the beginning in seconds |
| 423 | local time_elapsed_s |
| 424 | # Time estimated to finish in seconds |
| 425 | local time_est_s |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 426 | echo "Running a total number of ${num_experiments_session} experiments" 1>&2 |
| 427 | echo "Experiment idx start/end [${id_experiment_start}, ${id_experiment_end}]" 1>&2 |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 428 | |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 429 | # Run experiments |
| 430 | for i in $(seq $id_experiment_start $id_experiment_end); |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 431 | do |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 432 | (( array_shapes_idx=${i} / ${array_configs_len} )) |
| 433 | (( array_configs_idx=${i} % ${array_configs_len} )) |
| 434 | |
| 435 | gemm_shape=${array_shapes[$array_shapes_idx]} |
| 436 | gemm_config=${array_configs[$array_configs_idx]} |
| 437 | |
| 438 | echo "Running shape[$array_shapes_idx]=$gemm_shape with config[$array_configs_idx]=$gemm_config" 1>&2 |
| 439 | |
| 440 | example_args="${gemm_shape},${gemm_config},--type=${DATA_TYPE}" |
| 441 | json_filename="${STRATEGY_OPTION}_${gemm_shape}_${gemm_config}_${DATA_TYPE}" |
| 442 | # Replace "," with "_" |
| 443 | json_filename=${json_filename//,/_} |
| 444 | |
| 445 | # Run experiment |
| 446 | ${EXAMPLE_BIN_DIR}/${example_bin} --example_args=${example_args} --iterations=${NUM_ITERATION} --json-file=${OUT_DIR}/${json_filename}.${OUT_EXTENSION} --instruments=OPENCL_TIMER_MS |
| 447 | # Print progress |
| 448 | (( expr_count++ )) |
| 449 | print_progress ${expr_count} ${num_experiments_session} |
| 450 | # Print time statistics |
| 451 | time_elapsed_s=$SECONDS |
| 452 | echo "Time elapsed since beginning: $(( $time_elapsed_s / 60 ))m $(( $time_elapsed_s % 60 ))s" 1>&2 |
| 453 | (( time_est_s=(${num_experiments_session} - ${expr_count}) * ${time_elapsed_s} / ${expr_count} )) |
| 454 | echo "Time estimated to finish: $(( $time_est_s / 60 ))m $(( $time_est_s % 60 ))s" 1>&2 |
| 455 | echo "Done." 1>&2 |
| 456 | done |
| 457 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 458 | echo "Finished running all configs for ${example_bin}" 1>&2 |
| 459 | echo "All results saved to ${OUT_DIR}" 1>&2 |
| 460 | } |
| 461 | |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 462 | ####################################### |
| 463 | # Print the progress of the current session |
| 464 | # Globals: |
| 465 | # None |
| 466 | # Arguments: |
| 467 | # current Current number of items |
| 468 | # total Total number of items |
| 469 | # Returns: |
| 470 | # None |
| 471 | ####################################### |
| 472 | function print_progress() { |
| 473 | local current |
| 474 | local total |
| 475 | current=$1 |
| 476 | total=$2 |
| 477 | # Width of progress bar |
| 478 | local width |
| 479 | width=20 |
| 480 | (( current_width= $width * current / total )) |
| 481 | echo -n -e "Progress [" 1>&2 |
| 482 | for i in $(seq 1 ${width}); do |
| 483 | if [[ $i -le ${current_width} ]]; then |
| 484 | echo -n "#" 1>&2 |
| 485 | else |
| 486 | echo -n " " 1>&2 |
| 487 | fi |
| 488 | done |
| 489 | echo "] $current / $total Experiments" 1>&2 |
| 490 | } |
| 491 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 492 | # Functions }}} |
| 493 | |
| 494 | # Main: Main script {{{ |
| 495 | # Path to directory containing all benchmark examples binaries |
| 496 | EXAMPLE_BIN_DIR="" |
| 497 | # Path to gemm shapes file |
| 498 | GEMM_SHAPES_FILE="" |
| 499 | # Path to gemm configs file |
| 500 | GEMM_CONFIGS_FILE="" |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 501 | # Strategy option |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 502 | STRATEGY_OPTION="" |
Eren Kopuz | 6977b37 | 2020-07-13 12:37:06 +0100 | [diff] [blame] | 503 | # Data type to use |
| 504 | DATA_TYPE=${DEFAULT_DATA_TYPE} |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 505 | # Path to output directory |
| 506 | OUT_DIR=${DEFAULT_OUT_DIR} |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 507 | # ID of the first experiment |
| 508 | ID_EXPERIMENT_START=${DEFAULT_ID_EXPERIMENT_START} |
| 509 | # Total number of experiments to execute in this session |
| 510 | NUM_EXPERIMENTS=${DEFAULT_NUM_EXPERIMENTS} |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 511 | # Output benchmark result file extension |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 512 | OUT_EXTENSION=${DEFAULT_OUT_EXTENSION} |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 513 | # Toggle help |
| 514 | HELP=false |
| 515 | |
| 516 | # Obtain options |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 517 | while getopts "hs:e:g:c:d:o:i:n:t:" opt; do |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 518 | case "$opt" in |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 519 | h) HELP=true ;; |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 520 | s) STRATEGY_OPTION=$(to_lower "${OPTARG}");; |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 521 | e) EXAMPLE_BIN_DIR="${OPTARG}";; |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 522 | g) GEMM_SHAPES_FILE="${OPTARG}";; |
| 523 | c) GEMM_CONFIGS_FILE="${OPTARG}";; |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 524 | d) DATA_TYPE=$(to_lower "${OPTARG}");; |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 525 | o) OUT_DIR="${OPTARG}";; |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 526 | i) ID_EXPERIMENT_START="${OPTARG}";; |
| 527 | n) NUM_EXPERIMENTS="${OPTARG}";; |
| 528 | t) OUT_EXTENSION="${OPTARG}";; |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 529 | esac |
| 530 | done |
| 531 | shift $((OPTIND - 1)) |
| 532 | |
| 533 | # Lazily print usage (after arguments have been parsed) |
| 534 | $HELP && |
| 535 | usage |
| 536 | |
| 537 | # Parse and validate options |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 538 | # Verify all compulsory arguments are passed in |
| 539 | ( [ ! -z "${STRATEGY_OPTION}" ] && [ ! -z "${EXAMPLE_BIN_DIR}" ] && [ ! -z "${GEMM_SHAPES_FILE}" ] && [ ! -z "${GEMM_CONFIGS_FILE}" ] ) || |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 540 | usage |
| 541 | |
| 542 | # Verify example binaries directory exists |
| 543 | [ -d "${EXAMPLE_BIN_DIR}" ] || |
| 544 | error_msg "${EXAMPLE_BIN_DIR} does not exist." |
| 545 | |
| 546 | # Verify all benchmark example binaries exist |
| 547 | [ -f "${EXAMPLE_BIN_DIR}/${EXAMPLE_BIN_RESHAPED_RHS_ONLY}" ] || |
| 548 | error_msg "Cannot find ${EXAMPLE_BIN_RESHAPED_RHS_ONLY} at ${EXAMPLE_BIN_DIR}" |
| 549 | |
| 550 | # Verify Gemm shapes file exists |
| 551 | [ -f "${GEMM_SHAPES_FILE}" ] || |
| 552 | error_msg "Cannot find gemm shapes file ${GEMM_SHAPES_FILE}" |
| 553 | |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 554 | # Verify Gemm configs file exists |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 555 | [ -f "${GEMM_CONFIGS_FILE}" ] || |
| 556 | error_msg "Cannot find gemm configs file ${GEMM_CONFIGS_FILE}" |
| 557 | |
| 558 | # Verify strategy option is valid |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 559 | arr_contains "${STRATEGY_OPTION}" "${ALL_STRATEGY_OPTIONS[@]}" || |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 560 | error_msg "Does not support strategy ${STRATEGY_OPTION}" |
| 561 | |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 562 | # Verify data type option is valid |
| 563 | arr_contains "${DATA_TYPE}" "${ALL_DATA_TYPE_OPTIONS[@]}" || |
| 564 | error_msg "Does not support data type ${DATA_TYPE}" |
| 565 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 566 | # Make sure existing benchmark outputs are not overwritten |
| 567 | [ ! -d "${OUT_DIR}" ] || |
| 568 | error_msg "Output directory ${OUT_DIR} already exists!" |
| 569 | |
| 570 | # Make output directory |
SiCong Li | ac4c030 | 2020-07-28 12:24:45 +0100 | [diff] [blame] | 571 | echo "Making output directory ${OUT_DIR}" 1>&2 |
| 572 | mkdir -p ${OUT_DIR} || error_msg "Failed to make output directory ${OUT_DIR}" |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 573 | |
| 574 | # Run selected strategy with all configurations |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 575 | # Restart the built-in timer |
Gian Marco Iodice | 716b1be | 2021-02-10 17:33:27 +0000 | [diff] [blame^] | 576 | SECONDS=0 |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 577 | if [ "$DATA_TYPE" == "qasymm8" ]; then |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 578 | [ "${STRATEGY_OPTION}" == "reshaped_rhs_only" ] && run $EXAMPLE_BIN_RESHAPED_RHS_ONLY_LOWP |
| 579 | [ "${STRATEGY_OPTION}" == "reshaped" ] && run $EXAMPLE_BIN_RESHAPED_LOWP |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 580 | else |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 581 | [ "${STRATEGY_OPTION}" == "native" ] && run $EXAMPLE_BIN_NATIVE |
| 582 | [ "${STRATEGY_OPTION}" == "reshaped_rhs_only" ] && run $EXAMPLE_BIN_RESHAPED_RHS_ONLY |
| 583 | [ "${STRATEGY_OPTION}" == "reshaped" ] && run $EXAMPLE_BIN_RESHAPED |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 584 | fi |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 585 | # Main: Main script }}} |