Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 1 | /* |
Gunes Bayir | 9d0c4de | 2023-04-13 18:22:58 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 Arm Limited. |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 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 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 24 | #include "tests/validation/Helpers.h" |
Gunes Bayir | 9d0c4de | 2023-04-13 18:22:58 +0100 | [diff] [blame] | 25 | #include "tests/framework/Asserts.h" |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 26 | |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | #include <cmath> |
| 29 | |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace validation |
| 35 | { |
Michalis Spyrou | ed7b27d | 2019-11-27 16:04:17 +0000 | [diff] [blame] | 36 | template <> |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 37 | SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint8_t> &src) |
| 38 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 39 | const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform(); |
| 40 | SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() }; |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 41 | #if defined(_OPENMP) |
| 42 | #pragma omp parallel for |
| 43 | #endif /* _OPENMP */ |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 44 | for(int i = 0; i < src.num_elements(); ++i) |
| 45 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 46 | dst[i] = dequantize_qasymm8(src[i], quantization_info); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 47 | } |
| 48 | return dst; |
| 49 | } |
| 50 | |
Michalis Spyrou | ed7b27d | 2019-11-27 16:04:17 +0000 | [diff] [blame] | 51 | template <> |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 52 | SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<int8_t> &src) |
| 53 | { |
| 54 | const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform(); |
| 55 | SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() }; |
| 56 | |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 57 | #if defined(_OPENMP) |
| 58 | #pragma omp parallel for |
| 59 | #endif /* _OPENMP */ |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 60 | for(int i = 0; i < src.num_elements(); ++i) |
| 61 | { |
| 62 | dst[i] = dequantize_qasymm8_signed(src[i], quantization_info); |
| 63 | } |
| 64 | return dst; |
| 65 | } |
| 66 | |
| 67 | template <> |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 68 | SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint16_t> &src) |
| 69 | { |
| 70 | const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform(); |
| 71 | SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() }; |
| 72 | |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 73 | #if defined(_OPENMP) |
| 74 | #pragma omp parallel for |
| 75 | #endif /* _OPENMP */ |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 76 | for(int i = 0; i < src.num_elements(); ++i) |
| 77 | { |
| 78 | dst[i] = dequantize_qasymm16(src[i], quantization_info); |
| 79 | } |
| 80 | return dst; |
| 81 | } |
| 82 | |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 83 | template <> |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 84 | SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info) |
| 85 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 86 | SimpleTensor<uint8_t> dst{ src.shape(), DataType::QASYMM8, 1, quantization_info }; |
| 87 | const UniformQuantizationInfo &qinfo = quantization_info.uniform(); |
| 88 | |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 89 | #if defined(_OPENMP) |
| 90 | #pragma omp parallel for |
| 91 | #endif /* _OPENMP */ |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 92 | for(int i = 0; i < src.num_elements(); ++i) |
| 93 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 94 | dst[i] = quantize_qasymm8(src[i], qinfo); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 95 | } |
| 96 | return dst; |
| 97 | } |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 98 | |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 99 | template <> |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 100 | SimpleTensor<int8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info) |
| 101 | { |
| 102 | SimpleTensor<int8_t> dst{ src.shape(), DataType::QASYMM8_SIGNED, 1, quantization_info }; |
| 103 | const UniformQuantizationInfo &qinfo = quantization_info.uniform(); |
| 104 | |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 105 | #if defined(_OPENMP) |
| 106 | #pragma omp parallel for |
| 107 | #endif /* _OPENMP */ |
Sang-Hoon Park | ae6ef7c | 2019-11-13 16:51:45 +0000 | [diff] [blame] | 108 | for(int i = 0; i < src.num_elements(); ++i) |
| 109 | { |
| 110 | dst[i] = quantize_qasymm8_signed(src[i], qinfo); |
| 111 | } |
| 112 | return dst; |
| 113 | } |
| 114 | |
Michalis Spyrou | ed7b27d | 2019-11-27 16:04:17 +0000 | [diff] [blame] | 115 | template <> |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 116 | SimpleTensor<uint16_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info) |
| 117 | { |
| 118 | SimpleTensor<uint16_t> dst{ src.shape(), DataType::QASYMM16, 1, quantization_info }; |
| 119 | const UniformQuantizationInfo &qinfo = quantization_info.uniform(); |
| 120 | |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 121 | #if defined(_OPENMP) |
| 122 | #pragma omp parallel for |
| 123 | #endif /* _OPENMP */ |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 124 | for(int i = 0; i < src.num_elements(); ++i) |
| 125 | { |
| 126 | dst[i] = quantize_qasymm16(src[i], qinfo); |
| 127 | } |
| 128 | return dst; |
| 129 | } |
| 130 | |
| 131 | template <> |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 132 | SimpleTensor<int16_t> convert_to_symmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info) |
| 133 | { |
| 134 | SimpleTensor<int16_t> dst{ src.shape(), DataType::QSYMM16, 1, quantization_info }; |
| 135 | const UniformQuantizationInfo &qinfo = quantization_info.uniform(); |
| 136 | |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 137 | #if defined(_OPENMP) |
| 138 | #pragma omp parallel for |
| 139 | #endif /* _OPENMP */ |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 140 | for(int i = 0; i < src.num_elements(); ++i) |
| 141 | { |
| 142 | dst[i] = quantize_qsymm16(src[i], qinfo); |
| 143 | } |
| 144 | return dst; |
| 145 | } |
| 146 | |
| 147 | template <> |
| 148 | SimpleTensor<float> convert_from_symmetric(const SimpleTensor<int16_t> &src) |
| 149 | { |
| 150 | const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform(); |
| 151 | SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() }; |
| 152 | |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 153 | #if defined(_OPENMP) |
| 154 | #pragma omp parallel for |
| 155 | #endif /* _OPENMP */ |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 156 | for(int i = 0; i < src.num_elements(); ++i) |
| 157 | { |
| 158 | dst[i] = dequantize_qsymm16(src[i], quantization_info); |
| 159 | } |
| 160 | return dst; |
| 161 | } |
| 162 | |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 163 | template <typename T> |
| 164 | void matrix_multiply(const SimpleTensor<T> &a, const SimpleTensor<T> &b, SimpleTensor<T> &out) |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 165 | { |
| 166 | ARM_COMPUTE_ERROR_ON(a.shape()[0] != b.shape()[1]); |
| 167 | ARM_COMPUTE_ERROR_ON(a.shape()[1] != out.shape()[1]); |
| 168 | ARM_COMPUTE_ERROR_ON(b.shape()[0] != out.shape()[0]); |
| 169 | |
| 170 | const int M = a.shape()[1]; // Rows |
| 171 | const int N = b.shape()[0]; // Cols |
| 172 | const int K = b.shape()[1]; |
| 173 | |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 174 | #if defined(_OPENMP) |
| 175 | #pragma omp parallel for collapse(2) |
| 176 | #endif /* _OPENMP */ |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 177 | for(int y = 0; y < M; ++y) |
| 178 | { |
| 179 | for(int x = 0; x < N; ++x) |
| 180 | { |
| 181 | float acc = 0.0f; |
| 182 | for(int k = 0; k < K; ++k) |
| 183 | { |
| 184 | acc += a[y * K + k] * b[x + k * N]; |
| 185 | } |
| 186 | |
| 187 | out[x + y * N] = acc; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 192 | template <typename T> |
| 193 | void transpose_matrix(const SimpleTensor<T> &in, SimpleTensor<T> &out) |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 194 | { |
| 195 | ARM_COMPUTE_ERROR_ON((in.shape()[0] != out.shape()[1]) || (in.shape()[1] != out.shape()[0])); |
| 196 | |
| 197 | const int width = in.shape()[0]; |
| 198 | const int height = in.shape()[1]; |
| 199 | |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 200 | #if defined(_OPENMP) |
| 201 | #pragma omp parallel for collapse(2) |
| 202 | #endif /* _OPENMP */ |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 203 | for(int y = 0; y < height; ++y) |
| 204 | { |
| 205 | for(int x = 0; x < width; ++x) |
| 206 | { |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 207 | const T val = in[x + y * width]; |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 208 | |
| 209 | out[x * height + y] = val; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | template <typename T> |
| 215 | void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord) |
| 216 | { |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 217 | ARM_COMPUTE_ERROR_ON(tile.shape().num_dimensions() > 2); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 218 | |
| 219 | const int w_tile = tile.shape()[0]; |
| 220 | const int h_tile = tile.shape()[1]; |
| 221 | |
| 222 | // Fill the tile with zeros |
| 223 | std::fill(tile.data() + 0, (tile.data() + (w_tile * h_tile)), static_cast<T>(0)); |
| 224 | |
| 225 | // Check if with the dimensions greater than 2 we could have out-of-bound reads |
| 226 | for(size_t d = 2; d < Coordinates::num_max_dimensions; ++d) |
| 227 | { |
| 228 | if(coord[d] < 0 || coord[d] >= static_cast<int>(in.shape()[d])) |
| 229 | { |
| 230 | ARM_COMPUTE_ERROR("coord[d] < 0 || coord[d] >= in.shape()[d] with d >= 2"); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Since we could have out-of-bound reads along the X and Y dimensions, |
| 235 | // we start calculating the input address with x = 0 and y = 0 |
| 236 | Coordinates start_coord = coord; |
| 237 | start_coord[0] = 0; |
| 238 | start_coord[1] = 0; |
| 239 | |
| 240 | // Get input and roi pointers |
| 241 | auto in_ptr = static_cast<const T *>(in(start_coord)); |
| 242 | auto roi_ptr = static_cast<T *>(tile.data()); |
| 243 | |
| 244 | const int x_in_start = std::max(0, coord[0]); |
| 245 | const int y_in_start = std::max(0, coord[1]); |
| 246 | const int x_in_end = std::min(static_cast<int>(in.shape()[0]), coord[0] + w_tile); |
| 247 | const int y_in_end = std::min(static_cast<int>(in.shape()[1]), coord[1] + h_tile); |
| 248 | |
| 249 | // Number of elements to copy per row |
| 250 | const int n = x_in_end - x_in_start; |
| 251 | |
| 252 | // Starting coordinates for the ROI |
| 253 | const int x_tile_start = coord[0] > 0 ? 0 : std::abs(coord[0]); |
| 254 | const int y_tile_start = coord[1] > 0 ? 0 : std::abs(coord[1]); |
| 255 | |
| 256 | // Update input pointer |
| 257 | in_ptr += x_in_start; |
| 258 | in_ptr += (y_in_start * in.shape()[0]); |
| 259 | |
| 260 | // Update ROI pointer |
| 261 | roi_ptr += x_tile_start; |
| 262 | roi_ptr += (y_tile_start * tile.shape()[0]); |
| 263 | |
| 264 | for(int y = y_in_start; y < y_in_end; ++y) |
| 265 | { |
| 266 | // Copy per row |
| 267 | std::copy(in_ptr, in_ptr + n, roi_ptr); |
| 268 | |
| 269 | in_ptr += in.shape()[0]; |
| 270 | roi_ptr += tile.shape()[0]; |
| 271 | } |
| 272 | } |
| 273 | |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 274 | template <typename T> |
| 275 | void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape) |
| 276 | { |
| 277 | ARM_COMPUTE_ERROR_ON(anchor.num_dimensions() != shape.num_dimensions()); |
| 278 | ARM_COMPUTE_ERROR_ON(in.shape().num_dimensions() > 2); |
| 279 | ARM_COMPUTE_ERROR_ON(shape.num_dimensions() > 2); |
| 280 | |
| 281 | // Check if with the dimensions greater than 2 we could have out-of-bound reads |
| 282 | for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d) |
| 283 | { |
| 284 | if(anchor[d] < 0 || ((anchor[d] + shape[d]) > in.shape()[d])) |
| 285 | { |
| 286 | ARM_COMPUTE_ERROR("anchor[d] < 0 || (anchor[d] + shape[d]) > in.shape()[d]"); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // Get input pointer |
| 291 | auto in_ptr = static_cast<T *>(in(anchor[0] + anchor[1] * in.shape()[0])); |
| 292 | |
| 293 | const unsigned int n = in.shape()[0]; |
| 294 | |
| 295 | for(unsigned int y = 0; y < shape[1]; ++y) |
| 296 | { |
| 297 | std::fill(in_ptr, in_ptr + shape[0], 0); |
| 298 | in_ptr += n; |
| 299 | } |
| 300 | } |
| 301 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 302 | std::pair<int, int> get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max) |
| 303 | { |
| 304 | ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max"); |
| 305 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 306 | const int min_bound = quantize_qasymm8(min, quant_info.uniform()); |
| 307 | const int max_bound = quantize_qasymm8(max, quant_info.uniform()); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 308 | return std::pair<int, int> { min_bound, max_bound }; |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 311 | std::pair<int, int> get_quantized_qasymm8_signed_bounds(const QuantizationInfo &quant_info, float min, float max) |
| 312 | { |
| 313 | ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max"); |
| 314 | |
| 315 | const int min_bound = quantize_qasymm8_signed(min, quant_info.uniform()); |
| 316 | const int max_bound = quantize_qasymm8_signed(max, quant_info.uniform()); |
| 317 | return std::pair<int, int> { min_bound, max_bound }; |
| 318 | } |
| 319 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 320 | std::pair<int, int> get_symm_quantized_per_channel_bounds(const QuantizationInfo &quant_info, float min, float max, size_t channel_id) |
| 321 | { |
| 322 | ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max"); |
| 323 | |
| 324 | const int min_bound = quantize_qsymm8_per_channel(min, quant_info, channel_id); |
| 325 | const int max_bound = quantize_qsymm8_per_channel(max, quant_info, channel_id); |
| 326 | return std::pair<int, int> { min_bound, max_bound }; |
| 327 | } |
| 328 | |
Manuel Bottini | f733e03 | 2021-05-19 16:15:36 +0100 | [diff] [blame] | 329 | void add_padding_x(std::initializer_list<ITensor *> tensors, const DataLayout &data_layout, bool only_right_pad) |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 330 | { |
| 331 | if(data_layout == DataLayout::NHWC) |
| 332 | { |
| 333 | constexpr unsigned int lower = 1U; |
| 334 | constexpr unsigned int upper = 16U; |
| 335 | |
| 336 | std::uniform_int_distribution<unsigned int> distribution(lower, upper); |
| 337 | size_t seed_offset = 0; |
| 338 | |
| 339 | for(ITensor *tensor : tensors) |
| 340 | { |
| 341 | ARM_COMPUTE_ERROR_ON(!tensor->info()->is_resizable()); |
| 342 | |
| 343 | std::mt19937 gen(library->seed() + seed_offset++); |
| 344 | |
| 345 | const unsigned int right = distribution(gen); |
Manuel Bottini | f733e03 | 2021-05-19 16:15:36 +0100 | [diff] [blame] | 346 | const unsigned int left = only_right_pad ? 0 : distribution(gen); |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 347 | |
| 348 | tensor->info()->extend_padding(PaddingSize(0U, right, 0U, left)); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
Gian Marco Iodice | 72b5687 | 2021-06-29 10:08:46 +0100 | [diff] [blame] | 353 | void add_padding_y(std::initializer_list<ITensor *> tensors, const DataLayout &data_layout) |
| 354 | { |
| 355 | if(data_layout == DataLayout::NHWC) |
| 356 | { |
| 357 | constexpr unsigned int lower = 1U; |
| 358 | constexpr unsigned int upper = 4U; |
| 359 | |
| 360 | std::uniform_int_distribution<unsigned int> distribution(lower, upper); |
| 361 | size_t seed_offset = 0; |
| 362 | |
| 363 | for(ITensor *tensor : tensors) |
| 364 | { |
| 365 | ARM_COMPUTE_ERROR_ON(!tensor->info()->is_resizable()); |
| 366 | |
| 367 | std::mt19937 gen(library->seed() + seed_offset++); |
| 368 | |
| 369 | const unsigned int top = distribution(gen); |
| 370 | const unsigned int bottom = distribution(gen); |
| 371 | |
| 372 | tensor->info()->extend_padding(PaddingSize(top, 0U, bottom, 0U)); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
Gunes Bayir | 9d0c4de | 2023-04-13 18:22:58 +0100 | [diff] [blame] | 377 | QuantizationInfo calculate_mat_mul_dst_q_info(const QuantizationInfo &a_q_info, const QuantizationInfo &b_q_info, int m, int n, int k, DataType data_type) |
| 378 | { |
| 379 | ARM_COMPUTE_UNUSED(m, n); |
| 380 | QuantizationInfo c_q_info; |
| 381 | |
| 382 | ARM_COMPUTE_ASSERT(data_type == DataType::QASYMM8 || data_type == DataType::QASYMM8_SIGNED); |
| 383 | |
| 384 | const int32_t t_max = static_cast<int32_t>(data_type == DataType::QASYMM8 ? std::numeric_limits<uint8_t>::max() : std::numeric_limits<int8_t>::max()); |
| 385 | const int32_t t_min = static_cast<int32_t>(data_type == DataType::QASYMM8 ? std::numeric_limits<uint8_t>::min() : std::numeric_limits<int8_t>::min()); |
| 386 | |
| 387 | /** Quantization Setup of matrix multiplication |
| 388 | * |
| 389 | * We have a matrix multiplication of the form C = A * B |
| 390 | * where A is (M X K), B is (K x N) and C is therefore (M x N). |
| 391 | * |
| 392 | * If we have some distributions statistics of A and B, i.e. mean and variance, |
| 393 | * we can estimate the mean and variance of a single value in C matrix and |
| 394 | * pick good scale and offset values for the output and have non-saturated tests. |
| 395 | * |
| 396 | * Each element in the output matrix can be calculated as follows: |
| 397 | * C_ij = sum_k(A_ik * B_kj) |
| 398 | * |
| 399 | * All values are float above. |
| 400 | * |
| 401 | * Note: All possible A_ik, B_kj random variables are assumed mutually independent. |
| 402 | * |
| 403 | * Terminology: |
| 404 | * E[X]: Mean of the random variable X (sometimes referred as mu_x) |
| 405 | * var(X): Variance of the random variable X (someimes referred as sigma^2_x) |
| 406 | * std(X): sqrt(var(X)), standard deviation of X |
| 407 | * |
| 408 | * 1) Calculate the mean: |
| 409 | * E[C_ij] = sum_k( E[A_ik] * E[B_kj] ) = K * mean_a * mean_b |
| 410 | * |
| 411 | * Since elements of A and B are uniformly distributed random variables, we have |
| 412 | * mean_a = (max_a + min_a) / 2, mean_b = (max_b + min_b ) / 2 |
| 413 | * max_a and min_a can be calculated with the scale_a/b and offset_a/b |
| 414 | * by replacing data type minimum and maximums in the equations |
| 415 | * |
| 416 | * 2) Calculate the variance: |
| 417 | * var(C_ij) = sum_k( var(A_ik * B_kj) ) |
| 418 | * = sum_k ( E[A_ik^2 * B_kj^2] - E[A_ik]^2E[B_kj^2] ) |
| 419 | * = ... |
| 420 | * = K * (var_a * var_b + var_a * mean^2_b + var_b * mean^2_a) |
| 421 | * |
| 422 | * Similarly, due to uniform random variable properties, we have |
| 423 | * var_a = (max_a - min_a)^2 / 12 |
| 424 | * var_b = (max_b - min_b)^2 / 12 |
| 425 | * |
| 426 | * |
| 427 | * 3) Now, we have an idea of what would an average C_ij will like and how much deviation |
| 428 | * is present around it. The exact distribution of C is not easy to come up with dependent on K. |
| 429 | * But, as K increases, due to Central Limit Theorem, it'll look more like a bell shaped figure, |
| 430 | * approaching normal distribution. |
| 431 | * |
| 432 | * This is useful because, in normal distribution, we know that values +- 2 std_deviation around |
| 433 | * the mean constitute 95% of the values. Therefore, setting a plausible range for us: |
| 434 | * C_range = [C_min, C_max] = [mean_c - 2 * std_c, mean_c + 2 * std_c] |
| 435 | * |
| 436 | * 4) |
| 437 | * If we map this [C_min, C_max] to [0, 255] or [-128, 127] depending on the signedness of the |
| 438 | * data type, we can find a suitable scale and offset for the output. On average, it's expected |
| 439 | * that 5% of the output values will saturate and 95% will remain in the range. |
| 440 | * |
| 441 | * The equations to be solved for offset_c and scale_c are: |
| 442 | * C_min = scale_c * (type_min - offset_c) |
| 443 | * C_max = scale_c * (type_max - offset_c) |
| 444 | */ |
| 445 | |
| 446 | const int32_t a_offset = a_q_info.uniform().offset; |
| 447 | const float a_scale = a_q_info.uniform().scale; |
| 448 | const int32_t b_offset = b_q_info.uniform().offset; |
| 449 | const float b_scale = b_q_info.uniform().scale; |
| 450 | |
| 451 | // Lhs/A stats |
| 452 | const float max_a = (t_max - a_offset) * a_scale; |
| 453 | const float min_a = (t_min - a_offset) * a_scale; |
| 454 | const float mean_a = (max_a + min_a) / 2; |
| 455 | const float var_a = (max_a - min_a) * (max_a - min_a) / 12; |
| 456 | |
| 457 | // Rhs/B stats |
| 458 | const float max_b = (t_max - b_offset) * b_scale; |
| 459 | const float min_b = (t_min - b_offset) * b_scale; |
| 460 | const float mean_b = (max_b + min_b) / 2; |
| 461 | const float var_b = (max_b - min_b) * (max_b - min_b) / 12; |
| 462 | |
| 463 | // Output stats |
| 464 | const float mean_out = k * mean_a * mean_b; |
| 465 | const float var_out = k * (var_a * var_b + var_a * mean_b * mean_b + var_b * mean_a * mean_a); |
| 466 | const float std_out = sqrt(var_out); |
| 467 | |
| 468 | // Output quantization setup |
| 469 | const float scale_out = 4 * std_out / 255; |
| 470 | const int32_t offset_out = static_cast<int32_t>(t_min - (mean_out - 2.f * std_out) / scale_out); |
| 471 | |
| 472 | c_q_info = QuantizationInfo(scale_out, offset_out); |
| 473 | return c_q_info; |
| 474 | } |
| 475 | |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 476 | template void get_tile(const SimpleTensor<float> &in, SimpleTensor<float> &roi, const Coordinates &coord); |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 477 | template void get_tile(const SimpleTensor<half> &in, SimpleTensor<half> &roi, const Coordinates &coord); |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 478 | template void get_tile(const SimpleTensor<int> &in, SimpleTensor<int> &roi, const Coordinates &coord); |
| 479 | template void get_tile(const SimpleTensor<short> &in, SimpleTensor<short> &roi, const Coordinates &coord); |
| 480 | template void get_tile(const SimpleTensor<char> &in, SimpleTensor<char> &roi, const Coordinates &coord); |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 481 | template void zeros(SimpleTensor<float> &in, const Coordinates &anchor, const TensorShape &shape); |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 482 | template void zeros(SimpleTensor<half> &in, const Coordinates &anchor, const TensorShape &shape); |
| 483 | template void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out); |
| 484 | template void transpose_matrix(const SimpleTensor<half> &in, SimpleTensor<half> &out); |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 485 | template void transpose_matrix(const SimpleTensor<int> &in, SimpleTensor<int> &out); |
| 486 | template void transpose_matrix(const SimpleTensor<short> &in, SimpleTensor<short> &out); |
| 487 | template void transpose_matrix(const SimpleTensor<char> &in, SimpleTensor<char> &out); |
Adnan AlSinan | c584958 | 2022-05-05 11:13:19 +0100 | [diff] [blame] | 488 | template void transpose_matrix(const SimpleTensor<int8_t> &in, SimpleTensor<int8_t> &out); |
| 489 | template void transpose_matrix(const SimpleTensor<uint8_t> &in, SimpleTensor<uint8_t> &out); |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 490 | template void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out); |
| 491 | template void matrix_multiply(const SimpleTensor<half> &a, const SimpleTensor<half> &b, SimpleTensor<half> &out); |
| 492 | |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 493 | } // namespace validation |
| 494 | } // namespace test |
| 495 | } // namespace arm_compute |