Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ |
| 25 | #define __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ |
| 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 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 73 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
| 74 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 75 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 76 | // Reduce range as exponent overflows |
| 77 | bounds = std::make_pair(-10._h, 10._h); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 78 | break; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 79 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 80 | // Reduce range as sqrt should take a non-negative number |
| 81 | bounds = std::make_pair(0._h, 255._h); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 82 | break; |
| 83 | default: |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 84 | bounds = std::make_pair(-255._h, 255._h); |
| 85 | break; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 86 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 87 | break; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 88 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 89 | case DataType::F32: |
| 90 | switch(activation) |
| 91 | { |
| 92 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 93 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 94 | // Reduce range as exponent overflows |
| 95 | bounds = std::make_pair(-40.f, 40.f); |
| 96 | break; |
| 97 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 98 | // Reduce range as sqrt should take a non-negative number |
| 99 | bounds = std::make_pair(0.f, 255.f); |
| 100 | break; |
| 101 | default: |
| 102 | bounds = std::make_pair(-255.f, 255.f); |
| 103 | break; |
| 104 | } |
| 105 | break; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 106 | default: |
| 107 | ARM_COMPUTE_ERROR("Unsupported data type"); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 110 | return bounds; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 111 | } |
| 112 | |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 113 | /** Fill mask with the corresponding given pattern. |
| 114 | * |
| 115 | * @param[in,out] mask Mask to be filled according to pattern |
| 116 | * @param[in] cols Columns (width) of mask |
| 117 | * @param[in] rows Rows (height) of mask |
| 118 | * @param[in] pattern Pattern to fill the mask according to |
| 119 | */ |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 120 | 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] | 121 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 122 | /** Calculate output tensor shape give a vector of input tensor to concatenate |
| 123 | * |
| 124 | * @param[in] input_shapes Shapes of the tensors to concatenate across depth. |
| 125 | * |
| 126 | * @return The shape of output concatenated tensor. |
| 127 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 128 | TensorShape calculate_depth_concatenate_shape(const std::vector<TensorShape> &input_shapes); |
Sanghoon Lee | a7a5b7b | 2017-09-14 12:11:03 +0100 | [diff] [blame] | 129 | |
Michalis Spyrou | 55b3d12 | 2018-05-09 09:59:23 +0100 | [diff] [blame] | 130 | /** Calculate output tensor shape give a vector of input tensor to concatenate |
| 131 | * |
| 132 | * @param[in] input_shapes Shapes of the tensors to concatenate across width. |
| 133 | * |
| 134 | * @return The shape of output concatenated tensor. |
| 135 | */ |
| 136 | TensorShape calculate_width_concatenate_shape(const std::vector<TensorShape> &input_shapes); |
| 137 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 138 | /** Parameters of Harris Corners algorithm. */ |
| 139 | struct HarrisCornersParameters |
| 140 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 141 | float threshold{ 0.f }; /**< Threshold */ |
| 142 | float sensitivity{ 0.f }; /**< Sensitivity */ |
| 143 | float min_dist{ 0.f }; /**< Minimum distance */ |
| 144 | uint8_t constant_border_value{ 0 }; /**< Border value */ |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | /** Generate parameters for Harris Corners algorithm. */ |
| 148 | HarrisCornersParameters harris_corners_parameters(); |
| 149 | |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 150 | /** Parameters of Canny edge algorithm. */ |
| 151 | struct CannyEdgeParameters |
| 152 | { |
| 153 | int32_t upper_thresh{ 255 }; |
| 154 | int32_t lower_thresh{ 0 }; |
| 155 | uint8_t constant_border_value{ 0 }; |
| 156 | }; |
| 157 | |
| 158 | /** Generate parameters for Canny edge algorithm. */ |
| 159 | CannyEdgeParameters canny_edge_parameters(); |
| 160 | |
Sanghoon Lee | a7a5b7b | 2017-09-14 12:11:03 +0100 | [diff] [blame] | 161 | /** Helper function to fill the Lut random by a ILutAccessor. |
| 162 | * |
| 163 | * @param[in,out] table Accessor at the Lut. |
| 164 | * |
| 165 | */ |
| 166 | template <typename T> |
| 167 | void fill_lookuptable(T &&table) |
| 168 | { |
| 169 | std::mt19937 generator(library->seed()); |
| 170 | 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()); |
| 171 | |
| 172 | for(int i = std::numeric_limits<typename T::value_type>::min(); i <= std::numeric_limits<typename T::value_type>::max(); i++) |
| 173 | { |
| 174 | table[i] = distribution(generator); |
| 175 | } |
| 176 | } |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 177 | |
| 178 | /** Helper function to get the testing range for batch normalization layer. |
| 179 | * |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 180 | * @return A pair containing the lower upper testing bounds. |
| 181 | */ |
| 182 | template <typename T> |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 183 | std::pair<T, T> get_batchnormalization_layer_test_bounds() |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 184 | { |
Sanghoon Lee | c1294fa | 2017-11-17 11:47:41 +0000 | [diff] [blame] | 185 | const bool is_float = std::is_floating_point<T>::value; |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 186 | std::pair<T, T> bounds; |
| 187 | |
| 188 | // Set initial values |
| 189 | if(is_float) |
| 190 | { |
| 191 | bounds = std::make_pair(-1.f, 1.f); |
| 192 | } |
| 193 | else |
| 194 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 195 | bounds = std::make_pair(1, 1); |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return bounds; |
| 199 | } |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 200 | |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 201 | /** Helper function to get the testing range for NormalizePlanarYUV layer. |
| 202 | * |
| 203 | * @return A pair containing the lower upper testing bounds. |
| 204 | */ |
| 205 | template <typename T> |
| 206 | std::pair<T, T> get_normalize_planar_yuv_layer_test_bounds() |
| 207 | { |
| 208 | std::pair<T, T> bounds; |
| 209 | |
| 210 | bounds = std::make_pair(-1.f, 1.f); |
| 211 | |
| 212 | return bounds; |
| 213 | } |
| 214 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 215 | /** Convert quantized simple tensor into float using tensor quantization information. |
| 216 | * |
| 217 | * @param[in] src Quantized tensor. |
| 218 | * |
| 219 | * @return Float tensor. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 220 | */ |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 221 | SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint8_t> &src); |
| 222 | |
| 223 | /** Convert float simple tensor into quantized using specified quantization information. |
| 224 | * |
| 225 | * @param[in] src Float tensor. |
| 226 | * @param[in] quantization_info Quantification information. |
| 227 | * |
| 228 | * @return Quantized tensor. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 229 | */ |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 230 | SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info); |
Giorgio Arena | 1f9ca1d | 2018-03-01 11:13:45 +0000 | [diff] [blame] | 231 | |
| 232 | /** Matrix multiply between 2 float simple tensors |
| 233 | * |
| 234 | * @param[in] a Input tensor A |
| 235 | * @param[in] b Input tensor B |
| 236 | * @param[out] out Output tensor |
| 237 | * |
| 238 | */ |
| 239 | void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out); |
| 240 | |
| 241 | /** Transpose matrix |
| 242 | * |
| 243 | * @param[in] in Input tensor |
| 244 | * @param[out] out Output tensor |
| 245 | * |
| 246 | */ |
| 247 | void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out); |
| 248 | |
| 249 | /** Get a 2D tile from a tensor |
| 250 | * |
| 251 | * @note In case of out-of-bound reads, the tile will be filled with zeros |
| 252 | * |
| 253 | * @param[in] in Input tensor |
| 254 | * @param[out] tile Tile |
| 255 | * @param[in] coord Coordinates |
| 256 | */ |
| 257 | template <typename T> |
| 258 | 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] | 259 | |
| 260 | /** Fill with zeros the input tensor in the area defined by anchor and shape |
| 261 | * |
| 262 | * @param[in] in Input tensor to fill with zeros |
| 263 | * @param[out] anchor Starting point of the zeros area |
| 264 | * @param[in] shape Ending point of the zeros area |
| 265 | */ |
| 266 | template <typename T> |
| 267 | void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 268 | } // namespace validation |
| 269 | } // namespace test |
| 270 | } // namespace arm_compute |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 271 | #endif /* __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ */ |