Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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_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 | * |
| 55 | * @param[in] activation Activation function to test. |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 56 | * @param[in] data_type Data type. |
| 57 | * @param[in] fixed_point_position Number of bits for the fractional part. Defaults to 1. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | * |
| 59 | * @return A pair containing the lower upper testing bounds for a given function. |
| 60 | */ |
| 61 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 62 | std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, DataType data_type, int fixed_point_position = 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | std::pair<T, T> bounds; |
| 65 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 66 | switch(data_type) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 68 | case DataType::F16: |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 69 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 70 | using namespace half_float::literal; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 71 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 72 | switch(activation) |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 73 | { |
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 |
| 78 | bounds = std::make_pair(-10._h, 10._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 |
| 82 | bounds = std::make_pair(0._h, 255._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; |
| 107 | case DataType::QS8: |
| 108 | case DataType::QS16: |
| 109 | switch(activation) |
| 110 | { |
| 111 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 112 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 113 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 114 | // Reduce range as exponent overflows |
| 115 | bounds = std::make_pair(-(1 << fixed_point_position), 1 << fixed_point_position); |
| 116 | break; |
| 117 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 118 | // Reduce range as sqrt should take a non-negative number |
| 119 | // Can't be zero either as inv_sqrt is used in NEON. |
| 120 | bounds = std::make_pair(1, std::numeric_limits<T>::max()); |
| 121 | break; |
| 122 | default: |
| 123 | bounds = std::make_pair(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max()); |
| 124 | break; |
| 125 | } |
| 126 | break; |
| 127 | default: |
| 128 | ARM_COMPUTE_ERROR("Unsupported data type"); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 131 | return bounds; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 132 | } |
| 133 | |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 134 | /** Fill mask with the corresponding given pattern. |
| 135 | * |
| 136 | * @param[in,out] mask Mask to be filled according to pattern |
| 137 | * @param[in] cols Columns (width) of mask |
| 138 | * @param[in] rows Rows (height) of mask |
| 139 | * @param[in] pattern Pattern to fill the mask according to |
| 140 | */ |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 141 | 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] | 142 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 143 | /** Calculate output tensor shape give a vector of input tensor to concatenate |
| 144 | * |
| 145 | * @param[in] input_shapes Shapes of the tensors to concatenate across depth. |
| 146 | * |
| 147 | * @return The shape of output concatenated tensor. |
| 148 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 149 | TensorShape calculate_depth_concatenate_shape(const std::vector<TensorShape> &input_shapes); |
Sanghoon Lee | a7a5b7b | 2017-09-14 12:11:03 +0100 | [diff] [blame] | 150 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 151 | /** Parameters of Harris Corners algorithm. */ |
| 152 | struct HarrisCornersParameters |
| 153 | { |
| 154 | float threshold{ 0.f }; |
| 155 | float sensitivity{ 0.f }; |
| 156 | float min_dist{ 0.f }; |
| 157 | uint8_t constant_border_value{ 0 }; |
| 158 | }; |
| 159 | |
| 160 | /** Generate parameters for Harris Corners algorithm. */ |
| 161 | HarrisCornersParameters harris_corners_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 | |
| 180 | /** Helper function to get the testing range for batch normalization layer. |
| 181 | * |
| 182 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1. |
| 183 | * |
| 184 | * @return A pair containing the lower upper testing bounds. |
| 185 | */ |
| 186 | template <typename T> |
| 187 | std::pair<T, T> get_batchnormalization_layer_test_bounds(int fixed_point_position = 1) |
| 188 | { |
Sanghoon Lee | c1294fa | 2017-11-17 11:47:41 +0000 | [diff] [blame] | 189 | const bool is_float = std::is_floating_point<T>::value; |
Sanghoon Lee | 9688378 | 2017-09-15 14:10:48 +0100 | [diff] [blame] | 190 | std::pair<T, T> bounds; |
| 191 | |
| 192 | // Set initial values |
| 193 | if(is_float) |
| 194 | { |
| 195 | bounds = std::make_pair(-1.f, 1.f); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | bounds = std::make_pair(1, 1 << (fixed_point_position)); |
| 200 | } |
| 201 | |
| 202 | return bounds; |
| 203 | } |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 204 | |
zhenglin | 0fb6cf5 | 2017-12-12 15:56:09 +0800 | [diff] [blame] | 205 | /** Helper function to get the testing range for NormalizePlanarYUV layer. |
| 206 | * |
| 207 | * @return A pair containing the lower upper testing bounds. |
| 208 | */ |
| 209 | template <typename T> |
| 210 | std::pair<T, T> get_normalize_planar_yuv_layer_test_bounds() |
| 211 | { |
| 212 | std::pair<T, T> bounds; |
| 213 | |
| 214 | bounds = std::make_pair(-1.f, 1.f); |
| 215 | |
| 216 | return bounds; |
| 217 | } |
| 218 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 219 | /** Convert quantized simple tensor into float using tensor quantization information. |
| 220 | * |
| 221 | * @param[in] src Quantized tensor. |
| 222 | * |
| 223 | * @return Float tensor. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 224 | */ |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 225 | SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint8_t> &src); |
| 226 | |
| 227 | /** Convert float simple tensor into quantized using specified quantization information. |
| 228 | * |
| 229 | * @param[in] src Float tensor. |
| 230 | * @param[in] quantization_info Quantification information. |
| 231 | * |
| 232 | * @return Quantized tensor. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 233 | */ |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 234 | SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 235 | } // namespace validation |
| 236 | } // namespace test |
| 237 | } // namespace arm_compute |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 238 | #endif /* __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ */ |