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