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