Isabella Gottardi | 01a214a | 2018-04-09 16:00:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */ |
| 25 | #error "This example needs to be built with -DARM_COMPUTE_CL" |
| 26 | #endif /* ARM_COMPUTE_CL */ |
| 27 | |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 30 | #include "arm_compute/runtime/CL/CLFunctions.h" |
| 31 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 32 | |
| 33 | #include "tests/AssetsLibrary.h" |
| 34 | #include "tests/CL/CLAccessor.h" |
| 35 | #include "tests/Globals.h" |
| 36 | #include "tests/IAccessor.h" |
| 37 | #include "tests/SimpleTensor.h" |
| 38 | #include "tests/validation/Validation.h" |
| 39 | #include "tests/validation/reference/GEMM.h" |
| 40 | #include "tests/validation/reference/GEMMLowp.h" |
| 41 | |
| 42 | #include "ValidateExample.h" |
| 43 | |
| 44 | #include "utils/Utils.h" |
| 45 | |
| 46 | #include <cstdlib> |
| 47 | |
| 48 | using namespace arm_compute; |
| 49 | using namespace utils; |
| 50 | using namespace arm_compute::test; |
| 51 | using namespace arm_compute::test::validation; |
| 52 | |
| 53 | constexpr float abs_tolerance_f32(0.0001f); /**< F32 Absolute tolerance value for comparing reference's output against implementation's output for |
| 54 | * floating point data types in case using relative tolerance fails because of small values */ |
| 55 | RelativeTolerance<float> tolerance_f32(0.001f); /**< F32 Tolerance value for comparing reference's output against implementation's output for floating point data types */ |
| 56 | RelativeTolerance<half_float::half> tolerance_f16(half(0.2)); /**< F16 Tolerance value for comparing reference's output against implementation's output for floating point data types */ |
| 57 | constexpr float tolerance_num_f16 = 0.02f; /**< F16 Tolerance number */ |
| 58 | |
| 59 | class CLGEMMValidateExample : public ValidateExample |
| 60 | { |
| 61 | public: |
| 62 | void do_setup(int argc, char **argv) override |
| 63 | { |
| 64 | CLScheduler::get().default_init(); |
| 65 | if(argc == 2) |
| 66 | { |
| 67 | size_t dt = strtol(argv[1], nullptr, 10); |
| 68 | switch(dt) |
| 69 | { |
| 70 | case 1: |
| 71 | { |
| 72 | data_type = DataType::F16; |
| 73 | std::cout << "Usage: " << argv[0] << "1 M N K [alpha = 1.0f] [beta = 0.0f]\n"; |
| 74 | std::cout << "Using default values: Datatype=FP16 M=7, N=3, K=5, alpha=1.0f and beta=0.0f\n"; |
| 75 | break; |
| 76 | } |
| 77 | case 2: |
| 78 | { |
| 79 | data_type = DataType::QASYMM8; |
| 80 | std::cout << "Usage: " << argv[0] << "2 M N K [scale_src0 = 0.1f] [offset_scr0 = f] [scale_scr1 = 0.1f] [offset_scr1 = 10] [scale_dst = 0.1f] [offset_dst = 10] [bias = 1]\n"; |
| 81 | std::cout << |
| 82 | "Using default values: Datatype=QASYMM8 M=7, N=3, K=5, scale_src0 =(1.0f/255), offset_src0 = 10, scale_src1 =(1.0f/255), offset_src1 = 10, scale_dst =(1.0f/255), offset_dst = 10, bias=1\n\n"; |
| 83 | break; |
| 84 | } |
| 85 | case 0: |
| 86 | default: |
| 87 | { |
| 88 | data_type = DataType::F32; |
| 89 | std::cout << "Usage: " << argv[0] << "0 M N K [alpha = 1.0f] [beta = 0.0f]\n"; |
| 90 | std::cout << "Using default values: Datatype=FP32 M=7, N=3, K=5, alpha=1.0f and beta=0.0f\n"; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | else if(argc < 5) |
| 95 | { |
| 96 | // Print help |
| 97 | std::cout << "Usage with datatype = FP32 : " << argv[0] << "0 M N K [alpha = 1.0f] [beta = 0.0f]\n"; |
| 98 | std::cout << " datatype = FP16 : " << argv[0] << "1 M N K [alpha = 1.0f] [beta = 0.0f]\n"; |
| 99 | std::cout << " datatype = QASYMM8 : " << argv[0] << "2 M N K [scale_src0 = 0.1f] [offset_scr0 = f] [scale_scr1 = 0.1f] [offset_scr1 = 10] [scale_dst = 0.1f] [offset_dst = 10] [bias = 1]\n"; |
| 100 | std::cout << "Too few or no arguments provided.\n"; |
| 101 | std::cout << "Using default values: Datatype=FP32 M=7, N=3, K=5, alpha=1.0f and beta=0.0f\n"; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | size_t dt = strtol(argv[1], nullptr, 10); |
| 106 | switch(dt) |
| 107 | { |
| 108 | case 1: |
| 109 | { |
| 110 | data_type = DataType::F16; |
| 111 | break; |
| 112 | } |
| 113 | case 2: |
| 114 | { |
| 115 | data_type = DataType::QASYMM8; |
| 116 | break; |
| 117 | } |
| 118 | case 0: |
| 119 | default: |
| 120 | data_type = DataType::F32; |
| 121 | } |
| 122 | M = strtol(argv[2], nullptr, 10); |
| 123 | N = strtol(argv[3], nullptr, 10); |
| 124 | K = strtol(argv[4], nullptr, 10); |
| 125 | } |
| 126 | |
| 127 | switch(data_type) |
| 128 | { |
| 129 | case DataType::F16: |
| 130 | case DataType::F32: |
| 131 | { |
| 132 | if(argc > 5) |
| 133 | { |
| 134 | alpha = strtof(argv[5], nullptr); |
| 135 | if(argc > 6) |
| 136 | { |
| 137 | beta = strtof(argv[6], nullptr); |
| 138 | } |
| 139 | } |
| 140 | break; |
| 141 | } |
| 142 | case DataType::QASYMM8: |
| 143 | { |
| 144 | if(argc > 5) |
| 145 | { |
| 146 | scale_src0 = strtof(argv[5], nullptr); |
| 147 | if(argc > 6) |
| 148 | { |
| 149 | offset_src0 = strtol(argv[6], nullptr, 10); |
| 150 | if(argc > 7) |
| 151 | { |
| 152 | scale_src1 = strtof(argv[7], nullptr); |
| 153 | if(argc > 8) |
| 154 | { |
| 155 | offset_src1 = strtol(argv[8], nullptr, 10); |
| 156 | if(argc > 9) |
| 157 | { |
| 158 | scale_dst = strtof(argv[9], nullptr); |
| 159 | if(argc > 10) |
| 160 | { |
| 161 | offset_dst = strtol(argv[10], nullptr, 10); |
| 162 | if(argc > 11) |
| 163 | { |
| 164 | add_bias = (strtol(argv[11], nullptr, 10) == 1); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | float multiplier = scale_src0 * scale_src1 / scale_dst; |
| 173 | quantization::calculate_quantized_multiplier_less_than_one(multiplier, &dst_multiplier, &dst_shift); |
| 174 | break; |
| 175 | } |
| 176 | default: |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | src0.allocator()->init(TensorInfo(TensorShape(K, M), 1, data_type)); |
| 181 | src1.allocator()->init(TensorInfo(TensorShape(N, K), 1, data_type)); |
| 182 | src2.allocator()->init(TensorInfo(TensorShape(N, M), 1, data_type)); |
| 183 | init_sgemm_output(dst, src0, src1, data_type); |
| 184 | |
| 185 | // Configure function |
| 186 | if(data_type == DataType::QASYMM8) |
| 187 | { |
| 188 | src0.info()->set_quantization_info(QuantizationInfo(scale_src0, offset_src0)); |
| 189 | src1.info()->set_quantization_info(QuantizationInfo(scale_src1, offset_src1)); |
| 190 | dst.info()->set_quantization_info(QuantizationInfo(scale_dst, offset_dst)); |
| 191 | biases.allocator()->init(TensorInfo(TensorShape(N), 1, DataType::S32)); |
| 192 | init_sgemm_output(tmp_dst, src0, src1, DataType::S32); |
| 193 | |
| 194 | // Configure GEMMlowp matrix multiply function |
| 195 | mm_gemmlowp.configure(&src0, &src1, &tmp_dst); |
| 196 | |
| 197 | // Configure GEMMlowp output stage |
| 198 | mm_gemmlowp_output_stage.configure(&tmp_dst, add_bias ? &biases : nullptr, &dst, dst_multiplier, dst_shift, offset_dst); |
| 199 | tmp_dst.allocator()->allocate(); |
| 200 | biases.allocator()->allocate(); |
| 201 | fill(CLAccessor(biases), 3); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | // Configure matrix multiply function |
| 206 | mm_gemm.configure(&src0, &src1, &src2, &dst, alpha, beta); |
| 207 | } |
| 208 | |
| 209 | // Allocate all the tensors |
| 210 | src0.allocator()->allocate(); |
| 211 | src1.allocator()->allocate(); |
| 212 | dst.allocator()->allocate(); |
| 213 | src2.allocator()->allocate(); |
| 214 | |
| 215 | fill(CLAccessor(src0), 0); |
| 216 | fill(CLAccessor(src1), 1); |
| 217 | fill(CLAccessor(src2), 2); |
| 218 | } |
| 219 | |
| 220 | void print_parameters(framework::Printer &printer) override |
| 221 | { |
| 222 | printer.print_entry("Datatype", string_from_data_type(data_type)); |
| 223 | printer.print_entry("M", support::cpp11::to_string(M)); |
| 224 | printer.print_entry("N", support::cpp11::to_string(N)); |
| 225 | printer.print_entry("K", support::cpp11::to_string(K)); |
| 226 | if(data_type == DataType::QASYMM8) |
| 227 | { |
| 228 | printer.print_entry("Scale_Src0", support::cpp11::to_string(scale_src0)); |
| 229 | printer.print_entry("Offset_Src0", support::cpp11::to_string(offset_src0)); |
| 230 | printer.print_entry("Scale_Scr1", support::cpp11::to_string(scale_src1)); |
| 231 | printer.print_entry("Offset_Src1", support::cpp11::to_string(offset_src1)); |
| 232 | printer.print_entry("Scale_Dst", support::cpp11::to_string(scale_dst)); |
| 233 | printer.print_entry("Offset_Dst", support::cpp11::to_string(offset_dst)); |
| 234 | printer.print_entry("Bias", support::cpp11::to_string(add_bias)); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | printer.print_entry("Alpha", support::cpp11::to_string(alpha)); |
| 239 | printer.print_entry("Beta", support::cpp11::to_string(beta)); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void do_validate() override |
| 244 | { |
| 245 | switch(data_type) |
| 246 | { |
| 247 | case DataType::F16: |
| 248 | { |
| 249 | SimpleTensor<half> ref_src0 = { TensorShape(K, M), data_type, 1 }; |
| 250 | SimpleTensor<half> ref_src1 = { TensorShape(N, K), data_type, 1 }; |
| 251 | SimpleTensor<half> ref_src2 = { TensorShape(N, M), data_type, 1 }; |
| 252 | |
| 253 | fill(ref_src0, 0); |
| 254 | fill(ref_src1, 1); |
| 255 | fill(ref_src2, 2); |
| 256 | |
| 257 | SimpleTensor<half> ref_dst = reference::gemm<half>(ref_src0, ref_src1, ref_src2, alpha, beta); |
| 258 | validate(CLAccessor(dst), ref_dst, tolerance_f16, tolerance_num_f16); |
| 259 | break; |
| 260 | } |
| 261 | case DataType::F32: |
| 262 | { |
| 263 | SimpleTensor<float> ref_src0 = { TensorShape(K, M), data_type, 1 }; |
| 264 | SimpleTensor<float> ref_src1 = { TensorShape(N, K), data_type, 1 }; |
| 265 | SimpleTensor<float> ref_src2 = { TensorShape(N, M), data_type, 1 }; |
| 266 | |
| 267 | fill(ref_src0, 0); |
| 268 | fill(ref_src1, 1); |
| 269 | fill(ref_src2, 2); |
| 270 | |
| 271 | SimpleTensor<float> ref_dst = reference::gemm<float>(ref_src0, ref_src1, ref_src2, alpha, beta); |
| 272 | validate(CLAccessor(dst), ref_dst, tolerance_f32, 0.f, abs_tolerance_f32); |
| 273 | break; |
| 274 | } |
| 275 | case DataType::QASYMM8: |
| 276 | { |
| 277 | SimpleTensor<uint8_t> ref_src0{ TensorShape(K, M), data_type, 1 }; |
| 278 | SimpleTensor<uint8_t> ref_src1{ TensorShape(N, K), data_type, 1 }; |
| 279 | SimpleTensor<uint8_t> ref_dst; |
| 280 | |
| 281 | // Fill reference |
| 282 | fill(ref_src0, 0); |
| 283 | fill(ref_src1, 1); |
| 284 | |
| 285 | SimpleTensor<int32_t> ref_tmp_dst = reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(ref_src0, ref_src1, offset_src0, offset_src1); |
| 286 | |
| 287 | if(add_bias) |
| 288 | { |
| 289 | SimpleTensor<int32_t> biases{ TensorShape(N), DataType::S32, 1 }; |
| 290 | // Fill bias |
| 291 | fill(biases, 3); |
| 292 | ref_dst = reference::gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint<int32_t>(ref_tmp_dst, biases, dst_multiplier, dst_shift, offset_dst); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | ref_dst = reference::gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint<int32_t>(ref_tmp_dst, dst_multiplier, dst_shift, offset_dst); |
| 297 | } |
| 298 | validate(CLAccessor(dst), ref_dst); |
| 299 | break; |
| 300 | } |
| 301 | default: |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | void do_run() override |
| 306 | { |
| 307 | // Execute the function |
| 308 | if(data_type == DataType::QASYMM8) |
| 309 | { |
| 310 | // Run gemmlowp |
| 311 | mm_gemmlowp.run(); |
| 312 | // Run output stage |
| 313 | mm_gemmlowp_output_stage.run(); |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | // Run gemm |
| 318 | mm_gemm.run(); |
| 319 | } |
| 320 | |
| 321 | // Make sure all the OpenCL jobs are done executing: |
| 322 | CLScheduler::get().sync(); |
| 323 | } |
| 324 | |
| 325 | private: |
| 326 | template <typename U> |
| 327 | void fill(U &&tensor, int i) |
| 328 | { |
| 329 | switch(tensor.data_type()) |
| 330 | { |
| 331 | case DataType::F16: |
| 332 | case DataType::F32: |
| 333 | { |
| 334 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 335 | library->fill(tensor, distribution, i); |
| 336 | break; |
| 337 | } |
| 338 | case DataType::S32: |
| 339 | case DataType::QASYMM8: |
| 340 | { |
| 341 | std::uniform_int_distribution<> distribution(-6000, 6000); |
| 342 | library->fill(tensor, distribution, i); |
| 343 | break; |
| 344 | } |
| 345 | default: |
| 346 | library->fill_tensor_uniform(tensor, i); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | CLTensor src0{}, src1{}, src2{}, dst{}; |
| 351 | CLTensor tmp_dst{}, biases{}; |
| 352 | |
| 353 | CLGEMM mm_gemm{}; |
| 354 | CLGEMMLowpMatrixMultiplyCore mm_gemmlowp{}; |
| 355 | CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint mm_gemmlowp_output_stage{}; |
| 356 | |
| 357 | size_t M{ 7 }, N{ 3 }, K{ 5 }; |
| 358 | DataType data_type{ DataType::F32 }; |
| 359 | float alpha{ 1.0 }, beta{ 0.0 }; |
| 360 | int offset_src0{ 10 }, offset_src1{ 10 }, offset_dst{ 10 }; |
| 361 | float scale_src0{ 1.0f / 255 }, scale_src1{ 1.0f / 255 }, scale_dst{ 1.0f / 255 }; |
| 362 | int32_t dst_multiplier{ 0 }, dst_shift{ 0 }; |
| 363 | bool add_bias{ true }; |
| 364 | }; |
| 365 | |
| 366 | /** Main program for gemm test |
| 367 | * |
| 368 | * @param[in] argc Number of arguments |
| 369 | * @param[in] argv Arguments ( [optional] datatype, [optional] M, [optional] N, [optional] K, [optional] scale_src0, [optional] offset_src0, [optional] scale_src1, [optional] offset_src1, [optional] scale_dst, [optional] offset_dst, [optional] bias, [optional] alpha, [optional] beta ) |
| 370 | * |
| 371 | */ |
| 372 | int main(int argc, char **argv) |
| 373 | { |
| 374 | return utils::run_example<CLGEMMValidateExample>(argc, argv); |
| 375 | } |