COMPMID-3549: Add support for data type in bash and python scripts

- Bash script allows option for selecting data type which is passed to benchmark example
- Python script parses data type from results and reports as part of GEMMParam

Change-Id: I4202a2ce0a9b87b0bd8be5c26bb3027fe72aaaf4
Signed-off-by: Eren Kopuz <eren.kopuz@arm.com>
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/3560
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: SiCong Li <sicong.li@arm.com>
diff --git a/examples/gemm_tuner/GemmTuner.py b/examples/gemm_tuner/GemmTuner.py
index aab2d55..1361f90 100644
--- a/examples/gemm_tuner/GemmTuner.py
+++ b/examples/gemm_tuner/GemmTuner.py
@@ -48,10 +48,11 @@
     N: int  # Number of rhs matrix columns
     K: int  # Number of lhs matrix columns/rhs matrix rows
     B: int  # Batch size
+    data_type: str  # Data type
 
     @staticmethod
-    def parse_from_strs(*args):
-        return GEMMParam(*map(int, args))
+    def parse_from_strs(*M_N_K_B, data_type):
+        return GEMMParam(*map(int, M_N_K_B),str(data_type))
 
     def __str__(self):
         return ",".join(map(str, self))
@@ -441,9 +442,10 @@
 #   <-GEMMParam-><-------------GEMMConfig-------------->
 # Note that the test strategy_name == strategy.name is in place to avoid unwanted enum aliases
 GEMM_EXAMPLE_ARGS_FACTORY = {
+    # We ignore the data type field from GEMMParam as that is extracted separately
     strategy: namedtuple(
         "{}_Gemm_Example_Args".format(strategy_name),
-        GEMMParam._fields + GEMM_CONFIG_FACTORY[strategy]._fields,
+        GEMMParam._fields[:-1] + GEMM_CONFIG_FACTORY[strategy]._fields,
     )
     for strategy_name, strategy in Strategy.__members__.items()
     if strategy_name == strategy.name
@@ -460,6 +462,9 @@
 def parse_benchmark_commandline(commandline: str) -> Dict[str, str]:
     """ Parse the benchmark example command-line string into a dictionary of command-line agruments
     """
+    # Separate the data type option from the example_args portion of the string
+    commandline = commandline.replace(",--type=", " --type=")
+
     args = commandline.split()
     # Discard program name
     args = args[1:]
@@ -502,9 +507,11 @@
         example_args = Gemm_Example_Args_T(
             *(benchmark_args["example_args"].split(",")))
         # Gemm_Example_Arg consists of GEMMParam first and then GEMMConfig (in that order)
-        gemm_param_fields_len = len(GEMMParam._fields)
+        # However data type option is parsed separately from end of options, hence -1 is applied to fields length
+        gemm_param_fields_len = len(GEMMParam._fields) - 1
         gemm_param = GEMMParam.parse_from_strs(
-            *example_args[:gemm_param_fields_len])
+            *example_args[:gemm_param_fields_len],
+            data_type = benchmark_args["type"])
         GEMMConfig = GEMM_CONFIG_FACTORY[strategy]
         gemm_config = GEMMConfig.parse_from_strs(
             *example_args[gemm_param_fields_len:])
diff --git a/examples/gemm_tuner/benchmark_gemm_examples.sh b/examples/gemm_tuner/benchmark_gemm_examples.sh
index 175e0fa..e802553 100755
--- a/examples/gemm_tuner/benchmark_gemm_examples.sh
+++ b/examples/gemm_tuner/benchmark_gemm_examples.sh
@@ -36,6 +36,9 @@
 EXAMPLE_BIN_RESHAPED_RHS_ONLY="benchmark_cl_gemm_reshaped_rhs_only"
 EXAMPLE_BIN_RESHAPED="benchmark_cl_gemm_reshaped"
 
+# Default data type
+DEFAULT_DATA_TYPE="F32"
+
 # Default output directory
 DEFAULT_OUT_DIR="out"
 
@@ -233,6 +236,10 @@
         -c <gemm_config_file>
         Path to gemm config csv file
 
+        -d <data_type>
+        Data type option with which to run benchmark examples
+        Default: ${DEFAULT_DATA_TYPE}
+
         -o <out_dir>
         Path to output directory that holds output json files
         Default: ${DEFAULT_OUT_DIR}
@@ -350,9 +357,9 @@
     while read gemm_config
     do
       # Ignore empty lines and lines starting with # (comments)
-      if echo "$gemm_shape" | grep -Eq "$match_expression" && echo "$gemm_config" | grep -Pq "$match_expression";then
+      if echo "$gemm_shape" | grep -Eq "$match_expression" && echo "$gemm_config" | grep -Eq "$match_expression";then
         echo "Running..." 1>&2
-        example_args="${gemm_shape},${gemm_config}"
+        example_args="${gemm_shape},${gemm_config},--type=${DATA_TYPE}"
         # Run experiment
         ${EXAMPLE_BIN_DIR}/${example_bin} --example_args=${example_args} --iterations=${NUM_ITERATION} --json-file=${OUT_DIR}/${expr_count}.${OUT_EXTENSION} --instruments=OPENCL_TIMER_MS
         # Print progress
@@ -411,6 +418,8 @@
 # Path to gemm configs file
 GEMM_CONFIGS_FILE=""
 STRATEGY_OPTION=""
+# Data type to use
+DATA_TYPE=${DEFAULT_DATA_TYPE}
 # Path to output directory
 OUT_DIR=${DEFAULT_OUT_DIR}
 # Output benchmark result file extension
@@ -419,13 +428,14 @@
 HELP=false
 
 # Obtain options
-while getopts "hs:e:g:c:o:" opt; do
+while getopts "hs:e:g:c:d:o:" opt; do
   case "$opt" in
     h) HELP=true ;;
     s) STRATEGY_OPTION=$(to_lower "${OPTARG}");;
     e) EXAMPLE_BIN_DIR="${OPTARG}";;
     g) GEMM_SHAPES_FILE="${OPTARG}";;
     c) GEMM_CONFIGS_FILE="${OPTARG}";;
+    d) DATA_TYPE="${OPTARG}";;
     o) OUT_DIR="${OPTARG}";;
   esac
 done