blob: 3227a98b05aa68901aadae83d4937b1e87f555ab [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Pablo Tello3dd5b682019-03-04 14:14:02 +00002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010055 * @param[in] activation Activation function to test.
56 * @param[in] data_type Data type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 *
58 * @return A pair containing the lower upper testing bounds for a given function.
59 */
60template <typename T>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010061std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 std::pair<T, T> bounds;
64
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010065 switch(data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010067 case DataType::F16:
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010068 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010069 using namespace half_float::literal;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010070
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010071 switch(activation)
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010072 {
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010073 case ActivationLayerInfo::ActivationFunction::TANH:
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
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010078 bounds = std::make_pair(-2._h, 2._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
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010082 bounds = std::make_pair(0._h, 128._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;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100107 default:
108 ARM_COMPUTE_ERROR("Unsupported data type");
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100109 }
110
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100111 return bounds;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100112}
113
John Richardson6f4d49f2017-09-07 11:21:10 +0100114/** Fill mask with the corresponding given pattern.
115 *
116 * @param[in,out] mask Mask to be filled according to pattern
117 * @param[in] cols Columns (width) of mask
118 * @param[in] rows Rows (height) of mask
119 * @param[in] pattern Pattern to fill the mask according to
120 */
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100121void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern);
John Richardson6f4d49f2017-09-07 11:21:10 +0100122
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100123/** Calculate output tensor shape give a vector of input tensor to concatenate
124 *
125 * @param[in] input_shapes Shapes of the tensors to concatenate across depth.
126 *
127 * @return The shape of output concatenated tensor.
128 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100129TensorShape calculate_depth_concatenate_shape(const std::vector<TensorShape> &input_shapes);
Sanghoon Leea7a5b7b2017-09-14 12:11:03 +0100130
Pablo Tello3dd5b682019-03-04 14:14:02 +0000131/** Calculate output tensor shape for the concatenate operation along a given axis
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100132 *
133 * @param[in] input_shapes Shapes of the tensors to concatenate across width.
Pablo Tello3dd5b682019-03-04 14:14:02 +0000134 * @param[in] axis Axis to use for the concatenate operation
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100135 *
136 * @return The shape of output concatenated tensor.
137 */
Pablo Tello3dd5b682019-03-04 14:14:02 +0000138TensorShape calculate_concatenate_shape(const std::vector<TensorShape> &input_shapes, size_t axis);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100139
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100140/** Parameters of Harris Corners algorithm. */
141struct HarrisCornersParameters
142{
Alex Gildayc357c472018-03-21 13:54:09 +0000143 float threshold{ 0.f }; /**< Threshold */
144 float sensitivity{ 0.f }; /**< Sensitivity */
145 float min_dist{ 0.f }; /**< Minimum distance */
146 uint8_t constant_border_value{ 0 }; /**< Border value */
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100147};
148
149/** Generate parameters for Harris Corners algorithm. */
150HarrisCornersParameters harris_corners_parameters();
151
Abe Mbise1b993382017-12-19 13:51:59 +0000152/** Parameters of Canny edge algorithm. */
153struct CannyEdgeParameters
154{
155 int32_t upper_thresh{ 255 };
156 int32_t lower_thresh{ 0 };
157 uint8_t constant_border_value{ 0 };
158};
159
160/** Generate parameters for Canny edge algorithm. */
161CannyEdgeParameters canny_edge_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
Michalis Spyroued7b27d2019-11-27 16:04:17 +0000180/** Convert a quantized simple tensor into float using tensor quantization information.
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000181 *
182 * @param[in] src Quantized tensor.
183 *
184 * @return Float tensor.
Anthony Barbierf202e502017-11-23 18:02:04 +0000185 */
Michalis Spyroued7b27d2019-11-27 16:04:17 +0000186template <typename T>
187SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<T> &src);
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100188
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000189/** Convert float simple tensor into quantized using specified quantization information.
190 *
191 * @param[in] src Float tensor.
192 * @param[in] quantization_info Quantification information.
193 *
194 * @return Quantized tensor.
Anthony Barbierf202e502017-11-23 18:02:04 +0000195 */
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100196template <typename T>
197SimpleTensor<T> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info);
198
199/** Convert quantized simple tensor into float using tensor quantization information.
200 *
201 * @param[in] src Quantized tensor.
202 *
203 * @return Float tensor.
204 */
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100205template <typename T>
206SimpleTensor<float> convert_from_symmetric(const SimpleTensor<T> &src);
207
208/** Convert float simple tensor into quantized using specified quantization information.
209 *
210 * @param[in] src Float tensor.
211 * @param[in] quantization_info Quantification information.
212 *
213 * @return Quantized tensor.
214 */
215template <typename T>
216SimpleTensor<T> convert_to_symmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info);
217
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000218/** Matrix multiply between 2 float simple tensors
219 *
220 * @param[in] a Input tensor A
221 * @param[in] b Input tensor B
222 * @param[out] out Output tensor
223 *
224 */
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100225template <typename T>
226void matrix_multiply(const SimpleTensor<T> &a, const SimpleTensor<T> &b, SimpleTensor<T> &out);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000227
228/** Transpose matrix
229 *
230 * @param[in] in Input tensor
231 * @param[out] out Output tensor
232 *
233 */
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100234template <typename T>
235void transpose_matrix(const SimpleTensor<T> &in, SimpleTensor<T> &out);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000236
237/** Get a 2D tile from a tensor
238 *
239 * @note In case of out-of-bound reads, the tile will be filled with zeros
240 *
241 * @param[in] in Input tensor
242 * @param[out] tile Tile
243 * @param[in] coord Coordinates
244 */
245template <typename T>
246void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100247
248/** Fill with zeros the input tensor in the area defined by anchor and shape
249 *
250 * @param[in] in Input tensor to fill with zeros
251 * @param[out] anchor Starting point of the zeros area
252 * @param[in] shape Ending point of the zeros area
253 */
254template <typename T>
255void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape);
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100256
257/** Helper function to compute quantized min and max bounds
258 *
259 * @param[in] quant_info Quantization info to be used for conversion
260 * @param[in] min Floating point minimum value to be quantized
261 * @param[in] max Floating point maximum value to be quantized
262 */
263std::pair<int, int> get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100264
265/** Helper function to compute symmetric quantized min and max bounds
266 *
267 * @param[in] quant_info Quantization info to be used for conversion
268 * @param[in] min Floating point minimum value to be quantized
269 * @param[in] max Floating point maximum value to be quantized
270 * @param[in] channel_id Channel id for per channel quantization info.
271 */
272std::pair<int, int> get_symm_quantized_per_channel_bounds(const QuantizationInfo &quant_info, float min, float max, size_t channel_id = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273} // namespace validation
274} // namespace test
275} // namespace arm_compute
Isabella Gottardib797fa22017-06-23 15:02:11 +0100276#endif /* __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ */