blob: 7782b11f726527f5ec49ca91bbc47e19ec959227 [file] [log] [blame]
SiCong Li8b4c7302019-09-19 12:18:15 +01001# 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
27set -u
28
29CMD=$( basename $0 )
30
31# All supported strategy options
SiCong Libc166d52019-09-26 14:58:53 +010032ALL_STRATEGY_OPTIONS=("reshaped_rhs_only" "reshaped")
SiCong Li8b4c7302019-09-19 12:18:15 +010033
34# Names of example binary for each strategy
35EXAMPLE_BIN_RESHAPED_RHS_ONLY="benchmark_cl_gemm_reshaped_rhs_only"
SiCong Libc166d52019-09-26 14:58:53 +010036EXAMPLE_BIN_RESHAPED="benchmark_cl_gemm_reshaped"
SiCong Li8b4c7302019-09-19 12:18:15 +010037
38# Default output directory
39DEFAULT_OUT_DIR="out"
40
41# Number of iterations for each benchmark run
SiCong Libc166d52019-09-26 14:58:53 +010042NUM_ITERATION=5
SiCong Li8b4c7302019-09-19 12:18:15 +010043# 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#######################################
55function help_gemm_shape_file() {
56 cat >&2 << EOF
57Gemm 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
72EOF
73}
74
75#######################################
SiCong Libc166d52019-09-26 14:58:53 +010076# Print gemm config file for reshaped_rhs_only help message
SiCong Li8b4c7302019-09-19 12:18:15 +010077# Globals:
78# None
79# Arguments:
80# None
81# Returns:
82# None
83#######################################
84function help_gemm_config_file_reshaped_rhs_only() {
85 cat >&2 << EOF
86Gemm 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 Libc166d52019-09-26 14:58:53 +010095 transpose_rhs - Transpose rhs matrix (1) / Do not transpose rhs matrix (0)
SiCong Li8b4c7302019-09-19 12:18:15 +010096
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 Libc166d52019-09-26 14:58:53 +0100104 4,4,4,1,1,1
105 4,4,4,3,1,0
106 ...
107
108EOF
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#######################################
120function help_gemm_config_file_reshaped() {
121 cat >&2 << EOF
122Gemm 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 Li8b4c7302019-09-19 12:18:15 +0100152 ...
153
154EOF
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#######################################
166function usage() {
167 cat >&2 << EOF
168Run gemm examples of a selected strategy, over provided tunable configurationsa and gemm shapes.
169Save the benchmark results to json files in an output directory.
170
SiCong Libc166d52019-09-26 14:58:53 +0100171Usage: ${CMD} [-h] -s <strategy> -e <example_binary_dir> -g <gemm_shape_file> -c <gemm_config_file> [-o <out_dir>]
SiCong Li8b4c7302019-09-19 12:18:15 +0100172
173Options:
174 -h
SiCong Libc166d52019-09-26 14:58:53 +0100175 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 Li8b4c7302019-09-19 12:18:15 +0100181
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 Li8b4c7302019-09-19 12:18:15 +0100191 -o <out_dir>
192 Path to output directory that holds output json files
193 Default: ${DEFAULT_OUT_DIR}
194
195EOF
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 Libc166d52019-09-26 14:58:53 +0100199$HELP && ( [ "${STRATEGY_OPTION}" == "" ] || [ "${STRATEGY_OPTION}" == "reshaped" ] ) && help_gemm_config_file_reshaped
SiCong Li8b4c7302019-09-19 12:18:15 +0100200exit 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#######################################
212function 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#######################################
226function 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#######################################
240function is_integer() {
241 local in=$1
242 [ "$in" -eq "$in" ] 2> /dev/null
243}
244
245#######################################
SiCong Libc166d52019-09-26 14:58:53 +0100246# 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#######################################
255function 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 Li8b4c7302019-09-19 12:18:15 +0100268# 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#######################################
280function 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 Libc166d52019-09-26 14:58:53 +0100290 ${EXAMPLE_BIN_DIR}/${example_bin} --example_args=${example_args} --iterations=${NUM_ITERATION} --json-file=${OUT_DIR}/${test_id} --instruments=OPENCL_TIMER_MS
SiCong Li8b4c7302019-09-19 12:18:15 +0100291 (( 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
302EXAMPLE_BIN_DIR=""
303# Path to gemm shapes file
304GEMM_SHAPES_FILE=""
305# Path to gemm configs file
306GEMM_CONFIGS_FILE=""
SiCong Libc166d52019-09-26 14:58:53 +0100307STRATEGY_OPTION=""
SiCong Li8b4c7302019-09-19 12:18:15 +0100308# Path to output directory
309OUT_DIR=${DEFAULT_OUT_DIR}
310# Toggle help
311HELP=false
312
313# Obtain options
SiCong Libc166d52019-09-26 14:58:53 +0100314while getopts "hs:e:g:c:o:" opt; do
SiCong Li8b4c7302019-09-19 12:18:15 +0100315 case "$opt" in
SiCong Libc166d52019-09-26 14:58:53 +0100316 h) HELP=true ;;
SiCong Li8b4c7302019-09-19 12:18:15 +0100317 s) STRATEGY_OPTION=$(to_lower "${OPTARG}");;
SiCong Libc166d52019-09-26 14:58:53 +0100318 e) EXAMPLE_BIN_DIR="${OPTARG}";;
SiCong Li8b4c7302019-09-19 12:18:15 +0100319 g) GEMM_SHAPES_FILE="${OPTARG}";;
320 c) GEMM_CONFIGS_FILE="${OPTARG}";;
321 o) OUT_DIR="${OPTARG}";;
322 esac
323done
324shift $((OPTIND - 1))
325
326# Lazily print usage (after arguments have been parsed)
327$HELP &&
328 usage
329
330# Parse and validate options
SiCong Libc166d52019-09-26 14:58:53 +0100331# Verify all compulsory arguments are passed in
332( [ ! -z "${STRATEGY_OPTION}" ] && [ ! -z "${EXAMPLE_BIN_DIR}" ] && [ ! -z "${GEMM_SHAPES_FILE}" ] && [ ! -z "${GEMM_CONFIGS_FILE}" ] ) ||
SiCong Li8b4c7302019-09-19 12:18:15 +0100333 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 Libc166d52019-09-26 14:58:53 +0100347# Verify Gemm configs file exists
SiCong Li8b4c7302019-09-19 12:18:15 +0100348[ -f "${GEMM_CONFIGS_FILE}" ] ||
349 error_msg "Cannot find gemm configs file ${GEMM_CONFIGS_FILE}"
350
351# Verify strategy option is valid
SiCong Libc166d52019-09-26 14:58:53 +0100352arr_contains "${STRATEGY_OPTION}" "${ALL_STRATEGY_OPTIONS[@]}" ||
SiCong Li8b4c7302019-09-19 12:18:15 +0100353 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
360mkdir ${OUT_DIR}
361
362# Run selected strategy with all configurations
363[ "${STRATEGY_OPTION}" == "reshaped_rhs_only" ] && run $EXAMPLE_BIN_RESHAPED_RHS_ONLY
SiCong Libc166d52019-09-26 14:58:53 +0100364[ "${STRATEGY_OPTION}" == "reshaped" ] && run $EXAMPLE_BIN_RESHAPED
SiCong Li8b4c7302019-09-19 12:18:15 +0100365# Main: Main script }}}