Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TEST_VALIDATION_HELPERS_H |
| 25 | #define ARM_COMPUTE_TEST_VALIDATION_HELPERS_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Utils.h" |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 29 | #include "support/Half.h" |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 30 | #include "tests/Globals.h" |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 31 | #include "tests/SimpleTensor.h" |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 32 | |
| 33 | #include <random> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include <type_traits> |
| 35 | #include <utility> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 43 | template <typename T> |
| 44 | struct is_floating_point : public std::is_floating_point<T> |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 45 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | template <> |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 49 | struct is_floating_point<half> : public std::true_type |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 50 | { |
| 51 | }; |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 52 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | /** Helper function to get the testing range for each activation layer. |
| 54 | * |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 55 | * @param[in] activation Activation function to test. |
| 56 | * @param[in] data_type Data type. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | * |
| 58 | * @return A pair containing the lower upper testing bounds for a given function. |
| 59 | */ |
| 60 | template <typename T> |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 61 | std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, DataType data_type) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | std::pair<T, T> bounds; |
| 64 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 65 | switch(data_type) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 67 | case DataType::F16: |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 68 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 69 | using namespace half_float::literal; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 70 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 71 | switch(activation) |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 72 | { |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 73 | case ActivationLayerInfo::ActivationFunction::TANH: |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 74 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
| 75 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 76 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 77 | // Reduce range as exponent overflows |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 78 | bounds = std::make_pair(-2._h, 2._h); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 79 | break; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 80 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 81 | // Reduce range as sqrt should take a non-negative number |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 82 | bounds = std::make_pair(0._h, 128._h); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 83 | break; |
| 84 | default: |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 85 | bounds = std::make_pair(-255._h, 255._h); |
| 86 | break; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 87 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 88 | break; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 89 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 90 | case DataType::F32: |
| 91 | switch(activation) |
| 92 | { |
| 93 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 94 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 95 | // Reduce range as exponent overflows |
| 96 | bounds = std::make_pair(-40.f, 40.f); |
| 97 | break; |
| 98 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 99 | // Reduce range as sqrt should take a non-negative number |
| 100 | bounds = std::make_pair(0.f, 255.f); |
| 101 | break; |
| 102 | default: |
| 103 | bounds = std::make_pair(-255.f, 255.f); |
| 104 | break; |
| 105 | } |
| 106 | break; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 107 | default: |
| 108 | ARM_COMPUTE_ERROR("Unsupported data type"); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 111 | return bounds; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 112 | } |
| 113 | |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 114 | /** Fill mask with the corresponding given pattern. |
| 115 | * |
| 116 | * @param[in,out] mask Mask to be filled according to pattern |
| 117 | * @param[in] cols Columns (width) of mask |
| 118 | * @param[in] rows Rows (height) of mask |
| 119 | * @param[in] pattern Pattern to fill the mask according to |
| 120 | */ |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 121 | void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern); |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 122 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 123 | /** Calculate output tensor shape give a vector of input tensor to concatenate |
| 124 | * |
| 125 | * @param[in] input_shapes Shapes of the tensors to concatenate across depth. |
| 126 | * |
| 127 | * @return The shape of output concatenated tensor. |
| 128 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 129 | TensorShape calculate_depth_concatenate_shape(const std::vector<TensorShape> &input_shapes); |
Sanghoon Lee | a7a5b7b | 2017-09-14 12:11:03 +0100 | [diff] [blame] | 130 | |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 131 | /** Calculate output tensor shape for the concatenate operation along a given axis |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 132 | * |
| 133 | * @param[in] input_shapes Shapes of the tensors to concatenate across width. |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 134 | * @param[in] axis Axis to use for the concatenate operation |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 135 | * |
| 136 | * @return The shape of output concatenated tensor. |
| 137 | */ |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 138 | TensorShape calculate_concatenate_shape(const std::vector<TensorShape> &input_shapes, size_t axis); |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 139 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 140 | /** Parameters of Harris Corners algorithm. */ |
| 141 | struct HarrisCornersParameters |
| 142 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 143 | float threshold{ 0.f }; /**< Threshold */ |
| 144 | float sensitivity{ 0.f }; /**< Sensitivity */ |
| 145 | float min_dist{ 0.f }; /**< Minimum distance */ |
| 146 | uint8_t constant_border_value{ 0 }; /**< Border value */ |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /** Generate parameters for Harris Corners algorithm. */ |
| 150 | HarrisCornersParameters harris_corners_parameters(); |
| 151 | |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 152 | /** Parameters of Canny edge algorithm. */ |
| 153 | struct CannyEdgeParameters |
| 154 | { |
| 155 | int32_t upper_thresh{ 255 }; |
| 156 | int32_t lower_thresh{ 0 }; |
| 157 | uint8_t constant_border_value{ 0 }; |
| 158 | }; |
| 159 | |
| 160 | /** Generate parameters for Canny edge algorithm. */ |
| 161 | CannyEdgeParameters canny_edge_parameters(); |
| 162 | |
Sanghoon Lee | a7a5b7b | 2017-09-14 12:11:03 +0100 | [diff] [blame] | 163 | /** Helper function to fill the Lut random by a ILutAccessor. |
| 164 | * |
| 165 | * @param[in,out] table Accessor at the Lut. |
| 166 | * |
| 167 | */ |
| 168 | template <typename T> |
| 169 | void fill_lookuptable(T &&table) |
| 170 | { |
| 171 | std::mt19937 generator(library->seed()); |
| 172 | std::uniform_int_distribution<typename T::value_type> distribution(std::numeric_limits<typename T::value_type>::min(), std::numeric_limits<typename T::value_type>::max()); |
| 173 | |
| 174 | for(int i = std::numeric_limits<typename T::value_type>::min(); i <= std::numeric_limits<typename T::value_type>::max(); i++) |
| 175 | { |
| 176 | table[i] = distribution(generator); |
| 177 | } |
| 178 | } |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 179 | |
Michalis Spyrou | ed7b27d | 2019-11-27 16:04:17 +0000 | [diff] [blame] | 180 | /** Convert a quantized simple tensor into float using tensor quantization information. |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 181 | * |
| 182 | * @param[in] src Quantized tensor. |
| 183 | * |
| 184 | * @return Float tensor. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 185 | */ |
Michalis Spyrou | ed7b27d | 2019-11-27 16:04:17 +0000 | [diff] [blame] | 186 | template <typename T> |
| 187 | SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<T> &src); |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 188 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 189 | /** Convert float simple tensor into quantized using specified quantization information. |
| 190 | * |
| 191 | * @param[in] src Float tensor. |
| 192 | * @param[in] quantization_info Quantification information. |
| 193 | * |
| 194 | * @return Quantized tensor. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 195 | */ |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 196 | template <typename T> |
| 197 | SimpleTensor<T> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info); |
| 198 | |
| 199 | /** Convert quantized simple tensor into float using tensor quantization information. |
| 200 | * |
| 201 | * @param[in] src Quantized tensor. |
| 202 | * |
| 203 | * @return Float tensor. |
| 204 | */ |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 205 | template <typename T> |
| 206 | SimpleTensor<float> convert_from_symmetric(const SimpleTensor<T> &src); |
| 207 | |
| 208 | /** Convert float simple tensor into quantized using specified quantization information. |
| 209 | * |
| 210 | * @param[in] src Float tensor. |
| 211 | * @param[in] quantization_info Quantification information. |
| 212 | * |
| 213 | * @return Quantized tensor. |
| 214 | */ |
| 215 | template <typename T> |
| 216 | SimpleTensor<T> convert_to_symmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info); |
| 217 | |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 218 | /** Matrix multiply between 2 float simple tensors |
| 219 | * |
| 220 | * @param[in] a Input tensor A |
| 221 | * @param[in] b Input tensor B |
| 222 | * @param[out] out Output tensor |
| 223 | * |
| 224 | */ |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 225 | template <typename T> |
| 226 | 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] | 227 | |
| 228 | /** Transpose matrix |
| 229 | * |
| 230 | * @param[in] in Input tensor |
| 231 | * @param[out] out Output tensor |
| 232 | * |
| 233 | */ |
Vidhya Sudhan Loganathan | 71ecf39 | 2018-08-31 16:10:16 +0100 | [diff] [blame] | 234 | template <typename T> |
| 235 | void transpose_matrix(const SimpleTensor<T> &in, SimpleTensor<T> &out); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 236 | |
| 237 | /** Get a 2D tile from a tensor |
| 238 | * |
| 239 | * @note In case of out-of-bound reads, the tile will be filled with zeros |
| 240 | * |
| 241 | * @param[in] in Input tensor |
| 242 | * @param[out] tile Tile |
| 243 | * @param[in] coord Coordinates |
| 244 | */ |
| 245 | template <typename T> |
| 246 | void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord); |
Gian Marco Iodice | f1c2bf0 | 2018-06-13 14:05:54 +0100 | [diff] [blame] | 247 | |
| 248 | /** Fill with zeros the input tensor in the area defined by anchor and shape |
| 249 | * |
| 250 | * @param[in] in Input tensor to fill with zeros |
| 251 | * @param[out] anchor Starting point of the zeros area |
| 252 | * @param[in] shape Ending point of the zeros area |
| 253 | */ |
| 254 | template <typename T> |
| 255 | void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape); |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 256 | |
| 257 | /** Helper function to compute quantized min and max bounds |
| 258 | * |
| 259 | * @param[in] quant_info Quantization info to be used for conversion |
| 260 | * @param[in] min Floating point minimum value to be quantized |
| 261 | * @param[in] max Floating point maximum value to be quantized |
| 262 | */ |
| 263 | std::pair<int, int> get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 264 | |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 265 | /** Helper function to compute asymmetric quantized signed min and max bounds |
| 266 | * |
| 267 | * @param[in] quant_info Quantization info to be used for conversion |
| 268 | * @param[in] min Floating point minimum value to be quantized |
| 269 | * @param[in] max Floating point maximum value to be quantized |
| 270 | */ |
| 271 | std::pair<int, int> get_quantized_qasymm8_signed_bounds(const QuantizationInfo &quant_info, float min, float max); |
| 272 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 273 | /** Helper function to compute symmetric quantized min and max bounds |
| 274 | * |
| 275 | * @param[in] quant_info Quantization info to be used for conversion |
| 276 | * @param[in] min Floating point minimum value to be quantized |
| 277 | * @param[in] max Floating point maximum value to be quantized |
| 278 | * @param[in] channel_id Channel id for per channel quantization info. |
| 279 | */ |
| 280 | std::pair<int, int> get_symm_quantized_per_channel_bounds(const QuantizationInfo &quant_info, float min, float max, size_t channel_id = 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | } // namespace validation |
| 282 | } // namespace test |
| 283 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 284 | #endif /* ARM_COMPUTE_TEST_VALIDATION_HELPERS_H */ |