blob: 191e32813c4746e16a7fdb9f551cb265b86aa2c1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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
Isabella Gottardib797fa22017-06-23 15:02:11 +010027#include "ILutAccessor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "Types.h"
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010029#include "ValidationUserConfiguration.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010031#include "arm_compute/core/Types.h"
32
33#include <random>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include <type_traits>
35#include <utility>
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010036#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Pablo Tello91654c42017-07-05 11:32:17 +010038#ifdef ARM_COMPUTE_ENABLE_FP16
39#include <arm_fp16.h>
40#endif /* ARM_COMPUTE_ENABLE_FP16 */
41
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042namespace arm_compute
43{
44namespace test
45{
46namespace validation
47{
48/** Helper function to get the testing range for each activation layer.
49 *
50 * @param[in] activation Activation function to test.
51 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1.
52 *
53 * @return A pair containing the lower upper testing bounds for a given function.
54 */
55template <typename T>
Pablo Tello91654c42017-07-05 11:32:17 +010056inline std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057{
Pablo Tello91654c42017-07-05 11:32:17 +010058 bool is_float = std::is_same<T, float>::value;
59#ifdef ARM_COMPUTE_ENABLE_FP16
60 is_float = is_float || std::is_same<T, float16_t>::value;
61#endif /* ARM_COMPUTE_ENABLE_FP16 */
62
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 std::pair<T, T> bounds;
64
65 // Set initial values
66 if(is_float)
67 {
68 bounds = std::make_pair(-255.f, 255.f);
69 }
70 else
71 {
72 bounds = std::make_pair(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
73 }
74
75 // Reduce testing ranges
76 switch(activation)
77 {
78 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
79 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
80 // Reduce range as exponent overflows
81 if(is_float)
82 {
83 bounds.first = -40.f;
84 bounds.second = 40.f;
85 }
86 else
87 {
88 bounds.first = -(1 << (fixed_point_position));
89 bounds.second = 1 << (fixed_point_position);
90 }
91 break;
92 case ActivationLayerInfo::ActivationFunction::TANH:
93 // Reduce range as exponent overflows
94 if(!is_float)
95 {
96 bounds.first = -(1 << (fixed_point_position));
97 bounds.second = 1 << (fixed_point_position);
98 }
99 break;
100 case ActivationLayerInfo::ActivationFunction::SQRT:
101 // Reduce range as sqrt should take a non-negative number
Georgios Pinitasccc65d42017-06-27 17:39:11 +0100102 bounds.first = (is_float) ? 0 : 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 break;
104 default:
105 break;
106 }
107 return bounds;
108}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109/** Helper function to get the testing range for batch normalization layer.
110 *
111 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1.
112 *
113 * @return A pair containing the lower upper testing bounds.
114 */
115template <typename T>
116std::pair<T, T> get_batchnormalization_layer_test_bounds(int fixed_point_position = 1)
117{
118 bool is_float = std::is_floating_point<T>::value;
119 std::pair<T, T> bounds;
120
121 // Set initial values
122 if(is_float)
123 {
124 bounds = std::make_pair(-1.f, 1.f);
125 }
126 else
127 {
128 bounds = std::make_pair(1, 1 << (fixed_point_position));
129 }
130
131 return bounds;
132}
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100133
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 */
141inline void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern)
142{
143 unsigned int v = 0;
144 std::mt19937 gen(user_config.seed.get());
145 std::bernoulli_distribution dist(0.5);
146
147 for(int r = 0; r < rows; ++r)
148 {
149 for(int c = 0; c < cols; ++c, ++v)
150 {
151 uint8_t val = 0;
152
153 switch(pattern)
154 {
155 case MatrixPattern::BOX:
156 val = 255;
157 break;
158 case MatrixPattern::CROSS:
159 val = ((r == (rows / 2)) || (c == (cols / 2))) ? 255 : 0;
160 break;
161 case MatrixPattern::DISK:
162 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) *
163 (cols / 2.0f))) <= 1.0f ? 255 : 0;
164 break;
165 case MatrixPattern::OTHER:
166 val = (dist(gen) ? 0 : 255);
167 break;
168 default:
169 return;
170 }
171
172 mask[v] = val;
173 }
174 }
175
176 if(pattern == MatrixPattern::OTHER)
177 {
178 std::uniform_int_distribution<uint8_t> distribution_u8(0, ((cols * rows) - 1));
179 mask[distribution_u8(gen)] = 255;
180 }
181}
182
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100183/** Calculate output tensor shape give a vector of input tensor to concatenate
184 *
185 * @param[in] input_shapes Shapes of the tensors to concatenate across depth.
186 *
187 * @return The shape of output concatenated tensor.
188 */
189inline TensorShape calculate_depth_concatenate_shape(std::vector<TensorShape> input_shapes)
190{
191 TensorShape out_shape = input_shapes.at(0);
192
193 unsigned int max_x = 0;
194 unsigned int max_y = 0;
195 unsigned int depth = 0;
196
197 for(auto const &shape : input_shapes)
198 {
199 max_x = std::max<unsigned int>(shape.x(), max_x);
200 max_y = std::max<unsigned int>(shape.y(), max_y);
201 depth += shape.z();
202 }
203
204 out_shape.set(0, max_x);
205 out_shape.set(1, max_y);
206 out_shape.set(2, depth);
207
208 return out_shape;
209}
210
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100211/** Create a vector of random ROIs.
212 *
213 * @param[in] shape The shape of the input tensor.
214 * @param[in] pool_info The ROI pooling information.
215 * @param[in] num_rois The number of ROIs to be created.
216 * @param[in] seed The random seed to be used.
217 *
218 * @return A vector that contains the requested number of random ROIs
219 */
220std::vector<ROI> generate_random_rois(const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, unsigned int num_rois, std::random_device::result_type seed);
Isabella Gottardib797fa22017-06-23 15:02:11 +0100221
222/** Helper function to fill the Lut random by a ILutAccessor.
223 *
224 * @param[in,out] table Accessor at the Lut.
225 *
226 */
227template <typename T>
228void fill_lookuptable(T &&table)
229{
230 std::mt19937 generator(user_config.seed.get());
231 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());
232
233 for(int i = std::numeric_limits<typename T::value_type>::min(); i <= std::numeric_limits<typename T::value_type>::max(); i++)
234 {
235 table[i] = distribution(generator);
236 }
237}
238
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239} // namespace validation
240} // namespace test
241} // namespace arm_compute
Isabella Gottardib797fa22017-06-23 15:02:11 +0100242#endif /* __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ */