SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 1 | # Copyright (c) 2019 ARM Limited. |
| 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 | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 32 | ALL_STRATEGY_OPTIONS=("reshaped_rhs_only" "reshaped") |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 33 | |
| 34 | # Names of example binary for each strategy |
| 35 | EXAMPLE_BIN_RESHAPED_RHS_ONLY="benchmark_cl_gemm_reshaped_rhs_only" |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 36 | EXAMPLE_BIN_RESHAPED="benchmark_cl_gemm_reshaped" |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 37 | |
| 38 | # Default output directory |
| 39 | DEFAULT_OUT_DIR="out" |
| 40 | |
| 41 | # Number of iterations for each benchmark run |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 42 | NUM_ITERATION=5 |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 43 | # Global }}} |
| 44 | |
| 45 | # Functions {{{ |
| 46 | ####################################### |
| 47 | # Print gemm shape file help message |
| 48 | # Globals: |
| 49 | # None |
| 50 | # Arguments: |
| 51 | # None |
| 52 | # Returns: |
| 53 | # None |
| 54 | ####################################### |
| 55 | function help_gemm_shape_file() { |
| 56 | cat >&2 << EOF |
| 57 | Gemm shape file: |
| 58 | Gemm shape file is a headerless csv file with fields separated by commas and commas only (there cannot be whitespaces |
| 59 | around each field). |
| 60 | A gemm shape is a list of 4 positive integers <M, N, K, B> describing the shapes of the two matrices (LHS and RHS) |
| 61 | with: |
| 62 | M - Number of lhs matrix rows |
| 63 | N - Number of rhs matrix columns |
| 64 | K - Number of lhs matrix columns/rhs matrix rows |
| 65 | B - Batch size |
| 66 | |
| 67 | An example gemm shape file looks like: |
| 68 | 100,100,30,1 |
| 69 | 100,100,30,3 |
| 70 | ... |
| 71 | |
| 72 | EOF |
| 73 | } |
| 74 | |
| 75 | ####################################### |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 76 | # Print gemm config file for reshaped_rhs_only help message |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 77 | # Globals: |
| 78 | # None |
| 79 | # Arguments: |
| 80 | # None |
| 81 | # Returns: |
| 82 | # None |
| 83 | ####################################### |
| 84 | function help_gemm_config_file_reshaped_rhs_only() { |
| 85 | cat >&2 << EOF |
| 86 | Gemm config file (Strategy reshaped_rhs_only): |
| 87 | Gemm config file is a headerless csv file with fields separated by commas and commas only (there cannot be whitespaces |
| 88 | around each field). |
| 89 | A gemm config is a list of 4 positive integers <m0, n0, k0, h0> and 2 boolean values interleave_rhs and transpose_rhs, with: |
| 90 | m0 - Number of rows processed by the matrix multiplication |
| 91 | n0 - Number of columns processed by the matrix multiplication |
| 92 | k0 - Number of partial accumulations performed by the matrix multiplication |
| 93 | h0 - Number of horizontal blocks of size (k0xn0) stored on the same output row |
| 94 | interleave_rhs - Interleave rhs matrix (1) / Do not interleave rhs matrix (0) |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 95 | transpose_rhs - Transpose rhs matrix (1) / Do not transpose rhs matrix (0) |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 96 | |
| 97 | Only the following configurations of M0, N0 and K0 are currently supported: |
| 98 | M0 = 1, 2, 3, 4, 5, 6, 7, 8 |
| 99 | N0 = 2, 3, 4, 8, 16 |
| 100 | K0 = 2, 3, 4, 8, 16 |
| 101 | H0 >= 1 |
| 102 | |
| 103 | An example gemm config file looks like: |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 104 | 4,4,4,1,1,1 |
| 105 | 4,4,4,3,1,0 |
| 106 | ... |
| 107 | |
| 108 | EOF |
| 109 | } |
| 110 | |
| 111 | ####################################### |
| 112 | # Print gemm config file for reshaped help message |
| 113 | # Globals: |
| 114 | # None |
| 115 | # Arguments: |
| 116 | # None |
| 117 | # Returns: |
| 118 | # None |
| 119 | ####################################### |
| 120 | function help_gemm_config_file_reshaped() { |
| 121 | cat >&2 << EOF |
| 122 | Gemm config file (Strategy reshaped): |
| 123 | Gemm config file is a headerless csv file with fields separated by commas and commas only (there cannot be whitespaces |
| 124 | around each field). |
| 125 | A gemm config is a list of 5 positive integers <m0, n0, k0, v0, h0> and 3 boolean values interleave_lhs, interleave_rhs and transpose_rhs, with: |
| 126 | m0 - Number of rows processed by the matrix multiplication |
| 127 | n0 - Number of columns processed by the matrix multiplication |
| 128 | k0 - Number of partial accumulations performed by the matrix multiplication |
| 129 | v0 - Number of vertical blocks of size (m0xk0) stored on the same output row |
| 130 | h0 - Number of horizontal blocks of size (k0xn0) stored on the same output row |
| 131 | interleave_lhs - Interleave lhs matrix (1) / Do not interleave lhs matrix (0) |
| 132 | interleave_rhs - Interleave rhs matrix (1) / Do not interleave rhs matrix (0) |
| 133 | transpose_rhs - Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do transpose lhs matrix (0) |
| 134 | |
| 135 | If rhs matrix is transposed only the following configurations are currently supported: |
| 136 | M0 = 2, 3, 4, 5, 6, 7, 8 |
| 137 | N0 = 2, 3, 4, 8, 16 |
| 138 | K0 = 2, 3, 4, 8, 16 |
| 139 | V0 >= 1 |
| 140 | H0 >= 1 |
| 141 | |
| 142 | If lhs matrix is transposed only the following configurations are currently supported: |
| 143 | M0 = 2, 3, 4, 8 |
| 144 | N0 = 2, 3, 4, 8, 16 |
| 145 | K0 = 2, 3, 4, 8, 16 |
| 146 | V0 >= 1 |
| 147 | H0 >= 1 |
| 148 | |
| 149 | An example gemm config file looks like: |
| 150 | 4,4,4,1,3,1,1,1 |
| 151 | 4,4,4,3,3,1,1,0 |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 152 | ... |
| 153 | |
| 154 | EOF |
| 155 | } |
| 156 | |
| 157 | ####################################### |
| 158 | # Print usage of this program and exit with Error |
| 159 | # Globals: |
| 160 | # Assumes all globals are required |
| 161 | # Arguments: |
| 162 | # None |
| 163 | # Returns: |
| 164 | # Error(1) |
| 165 | ####################################### |
| 166 | function usage() { |
| 167 | cat >&2 << EOF |
| 168 | Run gemm examples of a selected strategy, over provided tunable configurationsa and gemm shapes. |
| 169 | Save the benchmark results to json files in an output directory. |
| 170 | |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 171 | Usage: ${CMD} [-h] -s <strategy> -e <example_binary_dir> -g <gemm_shape_file> -c <gemm_config_file> [-o <out_dir>] |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 172 | |
| 173 | Options: |
| 174 | -h |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 175 | Print help messages. If a strategy is specified with -s <strategy>, then only display messages relevant to that |
| 176 | strategy. Otherwise if no strategy is specified, display messages for all available strategies. |
| 177 | |
| 178 | -s <strategy> |
| 179 | Strategy option. |
| 180 | Options: ${ALL_STRATEGY_OPTIONS[@]}. |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 181 | |
| 182 | -e <example_binary_dir> |
| 183 | Path to directory that holds all example binaries |
| 184 | |
| 185 | -g <gemm_shape_file> |
| 186 | Path to gemm shape csv file |
| 187 | |
| 188 | -c <gemm_config_file> |
| 189 | Path to gemm config csv file |
| 190 | |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 191 | -o <out_dir> |
| 192 | Path to output directory that holds output json files |
| 193 | Default: ${DEFAULT_OUT_DIR} |
| 194 | |
| 195 | EOF |
| 196 | # Print help messages about gemm shapes and various gemm configs |
| 197 | $HELP && help_gemm_shape_file |
| 198 | $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^] | 199 | $HELP && ( [ "${STRATEGY_OPTION}" == "" ] || [ "${STRATEGY_OPTION}" == "reshaped" ] ) && help_gemm_config_file_reshaped |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 200 | exit 1 |
| 201 | } |
| 202 | |
| 203 | ####################################### |
| 204 | # Print error message and exit with Error. |
| 205 | # Globals: |
| 206 | # None |
| 207 | # Arguments: |
| 208 | # $1 - Error message |
| 209 | # Returns: |
| 210 | # None |
| 211 | ####################################### |
| 212 | function error_msg() { |
| 213 | echo "Error: $1" 1>&2 |
| 214 | exit 1 |
| 215 | } |
| 216 | |
| 217 | ####################################### |
| 218 | # Convert string to lower-case |
| 219 | # Globals: |
| 220 | # None |
| 221 | # Arguments: |
| 222 | # target - String |
| 223 | # Returns: |
| 224 | # (stdout) - String in lowercase |
| 225 | ####################################### |
| 226 | function to_lower() { |
| 227 | local target=$1 |
| 228 | echo "$target" | tr '[:upper:]' '[:lower:]' |
| 229 | } |
| 230 | |
| 231 | ####################################### |
| 232 | # Test if the argument is an integer |
| 233 | # Globals: |
| 234 | # None |
| 235 | # Arguments: |
| 236 | # in - Input |
| 237 | # Returns: |
| 238 | # true/false |
| 239 | ####################################### |
| 240 | function is_integer() { |
| 241 | local in=$1 |
| 242 | [ "$in" -eq "$in" ] 2> /dev/null |
| 243 | } |
| 244 | |
| 245 | ####################################### |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 246 | # Test if a string is in an array of strings |
| 247 | # Globals: |
| 248 | # None |
| 249 | # Arguments: |
| 250 | # target - String to test |
| 251 | # array - Array of strings to search |
| 252 | # Returns: |
| 253 | # true/false |
| 254 | ####################################### |
| 255 | function arr_contains() { |
| 256 | local target=$1 |
| 257 | shift |
| 258 | local array |
| 259 | array=("$@") |
| 260 | for s in "${array[@]}" |
| 261 | do |
| 262 | [ "$s" == "${target}" ] && return |
| 263 | done |
| 264 | false |
| 265 | } |
| 266 | |
| 267 | ####################################### |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 268 | # Run all tunable configurations and all input configurations |
| 269 | # Globals: |
| 270 | # OUT_DIR |
| 271 | # EXAMPLE_BIN_DIR |
| 272 | # NUM_ITERATION |
| 273 | # GEMM_CONFIGS_FILE |
| 274 | # GEMM_SHAPES_FILE |
| 275 | # Arguments: |
| 276 | # example_bin Name of the example binary to run |
| 277 | # Returns: |
| 278 | # None |
| 279 | ####################################### |
| 280 | function run() { |
| 281 | local example_bin=$1 |
| 282 | echo "Running all configs for ${example_bin}" 1>&2 |
| 283 | local example_args |
| 284 | local test_id=1 |
| 285 | while read gemm_shape |
| 286 | do |
| 287 | while read gemm_config |
| 288 | do |
| 289 | example_args="${gemm_shape},${gemm_config}" |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 290 | ${EXAMPLE_BIN_DIR}/${example_bin} --example_args=${example_args} --iterations=${NUM_ITERATION} --json-file=${OUT_DIR}/${test_id} --instruments=OPENCL_TIMER_MS |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 291 | (( test_id++ )) |
| 292 | done < "${GEMM_CONFIGS_FILE}" |
| 293 | done < "${GEMM_SHAPES_FILE}" |
| 294 | echo "Finished running all configs for ${example_bin}" 1>&2 |
| 295 | echo "All results saved to ${OUT_DIR}" 1>&2 |
| 296 | } |
| 297 | |
| 298 | # Functions }}} |
| 299 | |
| 300 | # Main: Main script {{{ |
| 301 | # Path to directory containing all benchmark examples binaries |
| 302 | EXAMPLE_BIN_DIR="" |
| 303 | # Path to gemm shapes file |
| 304 | GEMM_SHAPES_FILE="" |
| 305 | # Path to gemm configs file |
| 306 | GEMM_CONFIGS_FILE="" |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 307 | STRATEGY_OPTION="" |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 308 | # Path to output directory |
| 309 | OUT_DIR=${DEFAULT_OUT_DIR} |
| 310 | # Toggle help |
| 311 | HELP=false |
| 312 | |
| 313 | # Obtain options |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 314 | while getopts "hs:e:g:c:o:" opt; do |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 315 | case "$opt" in |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 316 | h) HELP=true ;; |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 317 | s) STRATEGY_OPTION=$(to_lower "${OPTARG}");; |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 318 | e) EXAMPLE_BIN_DIR="${OPTARG}";; |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 319 | g) GEMM_SHAPES_FILE="${OPTARG}";; |
| 320 | c) GEMM_CONFIGS_FILE="${OPTARG}";; |
| 321 | o) OUT_DIR="${OPTARG}";; |
| 322 | esac |
| 323 | done |
| 324 | shift $((OPTIND - 1)) |
| 325 | |
| 326 | # Lazily print usage (after arguments have been parsed) |
| 327 | $HELP && |
| 328 | usage |
| 329 | |
| 330 | # Parse and validate options |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 331 | # Verify all compulsory arguments are passed in |
| 332 | ( [ ! -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] | 333 | usage |
| 334 | |
| 335 | # Verify example binaries directory exists |
| 336 | [ -d "${EXAMPLE_BIN_DIR}" ] || |
| 337 | error_msg "${EXAMPLE_BIN_DIR} does not exist." |
| 338 | |
| 339 | # Verify all benchmark example binaries exist |
| 340 | [ -f "${EXAMPLE_BIN_DIR}/${EXAMPLE_BIN_RESHAPED_RHS_ONLY}" ] || |
| 341 | error_msg "Cannot find ${EXAMPLE_BIN_RESHAPED_RHS_ONLY} at ${EXAMPLE_BIN_DIR}" |
| 342 | |
| 343 | # Verify Gemm shapes file exists |
| 344 | [ -f "${GEMM_SHAPES_FILE}" ] || |
| 345 | error_msg "Cannot find gemm shapes file ${GEMM_SHAPES_FILE}" |
| 346 | |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 347 | # Verify Gemm configs file exists |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 348 | [ -f "${GEMM_CONFIGS_FILE}" ] || |
| 349 | error_msg "Cannot find gemm configs file ${GEMM_CONFIGS_FILE}" |
| 350 | |
| 351 | # Verify strategy option is valid |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 352 | arr_contains "${STRATEGY_OPTION}" "${ALL_STRATEGY_OPTIONS[@]}" || |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 353 | error_msg "Does not support strategy ${STRATEGY_OPTION}" |
| 354 | |
| 355 | # Make sure existing benchmark outputs are not overwritten |
| 356 | [ ! -d "${OUT_DIR}" ] || |
| 357 | error_msg "Output directory ${OUT_DIR} already exists!" |
| 358 | |
| 359 | # Make output directory |
| 360 | mkdir ${OUT_DIR} |
| 361 | |
| 362 | # Run selected strategy with all configurations |
| 363 | [ "${STRATEGY_OPTION}" == "reshaped_rhs_only" ] && run $EXAMPLE_BIN_RESHAPED_RHS_ONLY |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame^] | 364 | [ "${STRATEGY_OPTION}" == "reshaped" ] && run $EXAMPLE_BIN_RESHAPED |
SiCong Li | 8b4c730 | 2019-09-19 12:18:15 +0100 | [diff] [blame] | 365 | # Main: Main script }}} |