blob: ba459683924371dee6d13e9f4b3c73762f5dd7d0 [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 Pflanzera09de0c2017-09-01 20:41:12 +010028#include "arm_compute/core/Utils.h"
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010029#include "support/Half.h"
John Richardson6f4d49f2017-09-07 11:21:10 +010030#include "tests/Globals.h"
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010031#include "tests/SimpleTensor.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>
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010043template <typename T>
44struct is_floating_point : public std::is_floating_point<T>
Pablo Tello8fda1cb2017-07-05 15:20:38 +010045{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010046};
47
48template <>
Georgios Pinitas583137c2017-08-31 18:12:42 +010049struct is_floating_point<half> : public std::true_type
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010050{
51};
Pablo Tello8fda1cb2017-07-05 15:20:38 +010052
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053/** Helper function to get the testing range for each activation layer.
54 *
55 * @param[in] activation Activation function to test.
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010056 * @param[in] data_type Data type.
57 * @param[in] fixed_point_position Number of bits for the fractional part. Defaults to 1.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 *
59 * @return A pair containing the lower upper testing bounds for a given function.
60 */
61template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010062std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, DataType data_type, int fixed_point_position = 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 std::pair<T, T> bounds;
65
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010066 switch(data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010068 case DataType::F16:
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010069 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010070 using namespace half_float::literal;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010071
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010072 switch(activation)
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010073 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010074 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 Gottardi3b77e9d2017-06-22 11:05:41 +010079 break;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010080 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 Gottardi3b77e9d2017-06-22 11:05:41 +010083 break;
84 default:
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010085 bounds = std::make_pair(-255._h, 255._h);
86 break;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010087 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010088 break;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010089 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010090 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 Gottardi3b77e9d2017-06-22 11:05:41 +0100129 }
130
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100131 return bounds;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100132}
133
John Richardson6f4d49f2017-09-07 11:21:10 +0100134/** 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 Pflanzer6c6597c2017-09-24 12:09:41 +0100141void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern);
John Richardson6f4d49f2017-09-07 11:21:10 +0100142
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100143/** 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 Pflanzera09de0c2017-09-01 20:41:12 +0100149TensorShape calculate_depth_concatenate_shape(const std::vector<TensorShape> &input_shapes);
Sanghoon Leea7a5b7b2017-09-14 12:11:03 +0100150
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100151/** Parameters of Harris Corners algorithm. */
152struct 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. */
161HarrisCornersParameters harris_corners_parameters();
162
Sanghoon Leea7a5b7b2017-09-14 12:11:03 +0100163/** Helper function to fill the Lut random by a ILutAccessor.
164 *
165 * @param[in,out] table Accessor at the Lut.
166 *
167 */
168template <typename T>
169void 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 Lee96883782017-09-15 14:10:48 +0100179
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 */
186template <typename T>
187std::pair<T, T> get_batchnormalization_layer_test_bounds(int fixed_point_position = 1)
188{
Sanghoon Leec1294fa2017-11-17 11:47:41 +0000189 const bool is_float = std::is_floating_point<T>::value;
Sanghoon Lee96883782017-09-15 14:10:48 +0100190 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 Lokhmotovaf6204c2017-11-08 09:34:19 +0000204
zhenglin0fb6cf52017-12-12 15:56:09 +0800205/** Helper function to get the testing range for NormalizePlanarYUV layer.
206 *
207 * @return A pair containing the lower upper testing bounds.
208 */
209template <typename T>
210std::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 Lokhmotovaf6204c2017-11-08 09:34:19 +0000219/** Convert quantized simple tensor into float using tensor quantization information.
220 *
221 * @param[in] src Quantized tensor.
222 *
223 * @return Float tensor.
Anthony Barbierf202e502017-11-23 18:02:04 +0000224 */
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000225SimpleTensor<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 Barbierf202e502017-11-23 18:02:04 +0000233 */
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000234SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100235} // namespace validation
236} // namespace test
237} // namespace arm_compute
Isabella Gottardib797fa22017-06-23 15:02:11 +0100238#endif /* __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ */