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" |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 28 | #include "tests/Globals.h" |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 29 | #include "tests/ILutAccessor.h" |
| 30 | #include "tests/Types.h" |
| 31 | #include "tests/validation/ValidationUserConfiguration.h" |
| 32 | #include "tests/validation/half.h" |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 33 | |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 34 | #include <array> |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 35 | #include <random> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | #include <type_traits> |
| 37 | #include <utility> |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 38 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace test |
| 43 | { |
| 44 | namespace validation |
| 45 | { |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 46 | /** Helper function to fill one or more tensors with the uniform distribution with int values. |
| 47 | * |
| 48 | * @param[in] dist Distribution to be used to get the values for the tensor. |
| 49 | * @param[in] seeds List of seeds to be used to fill each tensor. |
| 50 | * @param[in,out] tensor Tensor to be initialized with the values of the distribution. |
| 51 | * @param[in,out] other_tensors (Optional) One or more tensors to be filled. |
| 52 | * |
| 53 | */ |
| 54 | template <typename D, typename T, typename... Ts> |
| 55 | void fill_tensors(D &&dist, std::initializer_list<int> seeds, T &&tensor, Ts &&... other_tensors) |
| 56 | { |
| 57 | const std::array < T, 1 + sizeof...(Ts) > tensors{ { std::forward<T>(tensor), std::forward<Ts>(other_tensors)... } }; |
| 58 | std::vector<int> vs(seeds); |
| 59 | ARM_COMPUTE_ERROR_ON(vs.size() != tensors.size()); |
| 60 | int k = 0; |
| 61 | for(auto tp : tensors) |
| 62 | { |
| 63 | library->fill(*tp, std::forward<D>(dist), vs[k++]); |
| 64 | } |
| 65 | } |
| 66 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | /** Helper function to get the testing range for each activation layer. |
| 68 | * |
| 69 | * @param[in] activation Activation function to test. |
| 70 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1. |
| 71 | * |
| 72 | * @return A pair containing the lower upper testing bounds for a given function. |
| 73 | */ |
| 74 | template <typename T> |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 75 | inline std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | { |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 77 | bool is_float = std::is_same<T, float>::value; |
Moritz Pflanzer | e49e266 | 2017-07-21 15:55:28 +0100 | [diff] [blame] | 78 | is_float = is_float || std::is_same<T, half_float::half>::value; |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 79 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | std::pair<T, T> bounds; |
| 81 | |
| 82 | // Set initial values |
| 83 | if(is_float) |
| 84 | { |
| 85 | bounds = std::make_pair(-255.f, 255.f); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | bounds = std::make_pair(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max()); |
| 90 | } |
| 91 | |
| 92 | // Reduce testing ranges |
| 93 | switch(activation) |
| 94 | { |
| 95 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 96 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 97 | // Reduce range as exponent overflows |
| 98 | if(is_float) |
| 99 | { |
| 100 | bounds.first = -40.f; |
| 101 | bounds.second = 40.f; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | bounds.first = -(1 << (fixed_point_position)); |
| 106 | bounds.second = 1 << (fixed_point_position); |
| 107 | } |
| 108 | break; |
| 109 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 110 | // Reduce range as exponent overflows |
| 111 | if(!is_float) |
| 112 | { |
| 113 | bounds.first = -(1 << (fixed_point_position)); |
| 114 | bounds.second = 1 << (fixed_point_position); |
| 115 | } |
| 116 | break; |
| 117 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 118 | // Reduce range as sqrt should take a non-negative number |
Georgios Pinitas | ccc65d4 | 2017-06-27 17:39:11 +0100 | [diff] [blame] | 119 | bounds.first = (is_float) ? 0 : 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | break; |
| 121 | default: |
| 122 | break; |
| 123 | } |
| 124 | return bounds; |
| 125 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | /** Helper function to get the testing range for batch normalization layer. |
| 127 | * |
| 128 | * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1. |
| 129 | * |
| 130 | * @return A pair containing the lower upper testing bounds. |
| 131 | */ |
| 132 | template <typename T> |
| 133 | std::pair<T, T> get_batchnormalization_layer_test_bounds(int fixed_point_position = 1) |
| 134 | { |
| 135 | bool is_float = std::is_floating_point<T>::value; |
| 136 | std::pair<T, T> bounds; |
| 137 | |
| 138 | // Set initial values |
| 139 | if(is_float) |
| 140 | { |
| 141 | bounds = std::make_pair(-1.f, 1.f); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | bounds = std::make_pair(1, 1 << (fixed_point_position)); |
| 146 | } |
| 147 | |
| 148 | return bounds; |
| 149 | } |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 150 | |
| 151 | /** Fill mask with the corresponding given pattern. |
| 152 | * |
| 153 | * @param[in,out] mask Mask to be filled according to pattern |
| 154 | * @param[in] cols Columns (width) of mask |
| 155 | * @param[in] rows Rows (height) of mask |
| 156 | * @param[in] pattern Pattern to fill the mask according to |
| 157 | */ |
| 158 | inline void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern) |
| 159 | { |
| 160 | unsigned int v = 0; |
| 161 | std::mt19937 gen(user_config.seed.get()); |
| 162 | std::bernoulli_distribution dist(0.5); |
| 163 | |
| 164 | for(int r = 0; r < rows; ++r) |
| 165 | { |
| 166 | for(int c = 0; c < cols; ++c, ++v) |
| 167 | { |
| 168 | uint8_t val = 0; |
| 169 | |
| 170 | switch(pattern) |
| 171 | { |
| 172 | case MatrixPattern::BOX: |
| 173 | val = 255; |
| 174 | break; |
| 175 | case MatrixPattern::CROSS: |
| 176 | val = ((r == (rows / 2)) || (c == (cols / 2))) ? 255 : 0; |
| 177 | break; |
| 178 | case MatrixPattern::DISK: |
| 179 | val = (((r - rows / 2.0f + 0.5f) * (r - rows / 2.0f + 0.5f)) / ((rows / 2.0f) * (rows / 2.0f)) + ((c - cols / 2.0f + 0.5f) * (c - cols / 2.0f + 0.5f)) / ((cols / 2.0f) * |
| 180 | (cols / 2.0f))) <= 1.0f ? 255 : 0; |
| 181 | break; |
| 182 | case MatrixPattern::OTHER: |
| 183 | val = (dist(gen) ? 0 : 255); |
| 184 | break; |
| 185 | default: |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | mask[v] = val; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if(pattern == MatrixPattern::OTHER) |
| 194 | { |
| 195 | std::uniform_int_distribution<uint8_t> distribution_u8(0, ((cols * rows) - 1)); |
| 196 | mask[distribution_u8(gen)] = 255; |
| 197 | } |
| 198 | } |
| 199 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 200 | /** Calculate output tensor shape give a vector of input tensor to concatenate |
| 201 | * |
| 202 | * @param[in] input_shapes Shapes of the tensors to concatenate across depth. |
| 203 | * |
| 204 | * @return The shape of output concatenated tensor. |
| 205 | */ |
| 206 | inline TensorShape calculate_depth_concatenate_shape(std::vector<TensorShape> input_shapes) |
| 207 | { |
| 208 | TensorShape out_shape = input_shapes.at(0); |
| 209 | |
| 210 | unsigned int max_x = 0; |
| 211 | unsigned int max_y = 0; |
| 212 | unsigned int depth = 0; |
| 213 | |
| 214 | for(auto const &shape : input_shapes) |
| 215 | { |
| 216 | max_x = std::max<unsigned int>(shape.x(), max_x); |
| 217 | max_y = std::max<unsigned int>(shape.y(), max_y); |
| 218 | depth += shape.z(); |
| 219 | } |
| 220 | |
| 221 | out_shape.set(0, max_x); |
| 222 | out_shape.set(1, max_y); |
| 223 | out_shape.set(2, depth); |
| 224 | |
| 225 | return out_shape; |
| 226 | } |
| 227 | |
Isabella Gottardi | 6203153 | 2017-07-04 11:21:28 +0100 | [diff] [blame^] | 228 | /** Fill matrix random. |
| 229 | * |
| 230 | * @param[in,out] matrix Matrix |
| 231 | * @param[in] cols Columns (width) of matrix |
| 232 | * @param[in] rows Rows (height) of matrix |
| 233 | */ |
| 234 | template <std::size_t SIZE> |
| 235 | inline void fill_warp_matrix(std::array<float, SIZE> &matrix, int cols, int rows) |
| 236 | { |
| 237 | std::mt19937 gen(user_config.seed.get()); |
| 238 | std::uniform_real_distribution<float> dist(-1, 1); |
| 239 | |
| 240 | for(int v = 0, r = 0; r < rows; ++r) |
| 241 | { |
| 242 | for(int c = 0; c < cols; ++c, ++v) |
| 243 | { |
| 244 | matrix[v] = dist(gen); |
| 245 | } |
| 246 | } |
| 247 | if(SIZE == 9) |
| 248 | { |
| 249 | matrix[(cols * rows) - 1] = 1; |
| 250 | } |
| 251 | } |
| 252 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 253 | /** Create a vector of random ROIs. |
| 254 | * |
| 255 | * @param[in] shape The shape of the input tensor. |
| 256 | * @param[in] pool_info The ROI pooling information. |
| 257 | * @param[in] num_rois The number of ROIs to be created. |
| 258 | * @param[in] seed The random seed to be used. |
| 259 | * |
| 260 | * @return A vector that contains the requested number of random ROIs |
| 261 | */ |
| 262 | std::vector<ROI> generate_random_rois(const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, unsigned int num_rois, std::random_device::result_type seed); |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 263 | |
| 264 | /** Helper function to fill the Lut random by a ILutAccessor. |
| 265 | * |
| 266 | * @param[in,out] table Accessor at the Lut. |
| 267 | * |
| 268 | */ |
| 269 | template <typename T> |
| 270 | void fill_lookuptable(T &&table) |
| 271 | { |
| 272 | std::mt19937 generator(user_config.seed.get()); |
| 273 | 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()); |
| 274 | |
| 275 | for(int i = std::numeric_limits<typename T::value_type>::min(); i <= std::numeric_limits<typename T::value_type>::max(); i++) |
| 276 | { |
| 277 | table[i] = distribution(generator); |
| 278 | } |
| 279 | } |
| 280 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | } // namespace validation |
| 282 | } // namespace test |
| 283 | } // namespace arm_compute |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 284 | #endif /* __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ */ |