Compute Library
 21.02
Helpers.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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 
27 #include "arm_compute/core/Types.h"
28 #include "arm_compute/core/Utils.h"
29 #include "support/Half.h"
30 #include "tests/Globals.h"
31 #include "tests/SimpleTensor.h"
32 
33 #include <math.h>
34 #include <random>
35 #include <type_traits>
36 #include <utility>
37 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 template <typename T>
45 struct is_floating_point : public std::is_floating_point<T>
46 {
47 };
48 
49 template <>
50 struct is_floating_point<half> : public std::true_type
51 {
52 };
53 
54 /** Helper function to get the testing range for each activation layer.
55  *
56  * @param[in] activation Activation function to test.
57  * @param[in] data_type Data type.
58  *
59  * @return A pair containing the lower upper testing bounds for a given function.
60  */
61 template <typename T>
63 {
64  std::pair<T, T> bounds;
65 
66  switch(data_type)
67  {
68  case DataType::F16:
69  {
70  using namespace half_float::literal;
71 
72  switch(activation)
73  {
78  // Reduce range as exponent overflows
79  bounds = std::make_pair(-2._h, 2._h);
80  break;
82  // Reduce range as sqrt should take a non-negative number
83  bounds = std::make_pair(0._h, 128._h);
84  break;
85  default:
86  bounds = std::make_pair(-255._h, 255._h);
87  break;
88  }
89  break;
90  }
91  case DataType::F32:
92  switch(activation)
93  {
95  // Reduce range as exponent overflows
96  bounds = std::make_pair(-40.f, 40.f);
97  break;
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  default:
108  ARM_COMPUTE_ERROR("Unsupported data type");
109  }
110 
111  return bounds;
112 }
113 
114 /** 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  */
121 void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern);
122 
123 /** 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  */
129 TensorShape calculate_depth_concatenate_shape(const std::vector<TensorShape> &input_shapes);
130 
131 /** Calculate output tensor shape for the concatenate operation along a given axis
132  *
133  * @param[in] input_shapes Shapes of the tensors to concatenate across width.
134  * @param[in] axis Axis to use for the concatenate operation
135  *
136  * @return The shape of output concatenated tensor.
137  */
138 TensorShape calculate_concatenate_shape(const std::vector<TensorShape> &input_shapes, size_t axis);
139 
140 /** Parameters of Harris Corners algorithm. */
142 {
143  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 */
147 };
148 
149 /** Generate parameters for Harris Corners algorithm. */
151 
152 /** Parameters of Canny edge algorithm. */
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. */
162 
163 /** Helper function to fill the Lut random by a ILutAccessor.
164  *
165  * @param[in,out] table Accessor at the Lut.
166  *
167  */
168 template <typename T>
169 void 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 }
179 
180 /** Convert an asymmetric quantized simple tensor into float using tensor quantization information.
181  *
182  * @param[in] src Quantized tensor.
183  *
184  * @return Float tensor.
185  */
186 template <typename T>
188 
189 /** 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.
195  */
196 template <typename T>
197 SimpleTensor<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  */
205 template <typename T>
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  */
215 template <typename T>
216 SimpleTensor<T> convert_to_symmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info);
217 
218 /** 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  */
225 template <typename T>
227 
228 /** Transpose matrix
229  *
230  * @param[in] in Input tensor
231  * @param[out] out Output tensor
232  *
233  */
234 template <typename T>
235 void transpose_matrix(const SimpleTensor<T> &in, SimpleTensor<T> &out);
236 
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  */
245 template <typename T>
246 void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord);
247 
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  */
254 template <typename T>
255 void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape);
256 
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  */
263 std::pair<int, int> get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max);
264 
265 /** Helper function to compute asymmetric quantized signed 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  */
271 std::pair<int, int> get_quantized_qasymm8_signed_bounds(const QuantizationInfo &quant_info, float min, float max);
272 
273 /** Helper function to compute symmetric quantized min and max bounds
274  *
275  * @param[in] quant_info Quantization info to be used for conversion
276  * @param[in] min Floating point minimum value to be quantized
277  * @param[in] max Floating point maximum value to be quantized
278  * @param[in] channel_id Channel id for per channel quantization info.
279  */
280 std::pair<int, int> get_symm_quantized_per_channel_bounds(const QuantizationInfo &quant_info, float min, float max, size_t channel_id = 0);
281 } // namespace validation
282 } // namespace test
283 } // namespace arm_compute
284 #endif /* ARM_COMPUTE_TEST_VALIDATION_HELPERS_H */
CannyEdgeParameters canny_edge_parameters()
Generate parameters for Canny edge algorithm.
Definition: Helpers.cpp:95
std::pair< int, int > get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max)
Helper function to compute quantized min and max bounds.
Definition: Helpers.cpp:377
Shape of a tensor.
Definition: TensorShape.h:39
void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern)
Fill mask with the corresponding given pattern.
Definition: Helpers.cpp:35
SimpleTensor< float > b
Definition: DFT.cpp:157
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void zeros(SimpleTensor< T > &in, const Coordinates &anchor, const TensorShape &shape)
Fill with zeros the input tensor in the area defined by anchor and shape.
Definition: Helpers.cpp:350
half_float::half half
16-bit floating point type
Definition: Types.h:46
1 channel, 1 F32 per channel
SimpleTensor< float > convert_from_asymmetric(const SimpleTensor< uint8_t > &src)
Definition: Helpers.cpp:112
void transpose_matrix(const SimpleTensor< T > &in, SimpleTensor< T > &out)
Transpose matrix.
Definition: Helpers.cpp:268
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
ActivationFunction
Available activation functions.
Definition: Types.h:1554
1 channel, 1 F16 per channel
const DataType data_type
Definition: Im2Col.cpp:150
Quantization information.
Parameters of Canny edge algorithm.
Definition: Helpers.h:153
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:78
Coordinates of an item.
Definition: Coordinates.h:37
std::uniform_real_distribution< float > distribution(-5.f, 5.f)
HarrisCornersParameters harris_corners_parameters()
Generate parameters for Harris Corners algorithm.
Definition: Helpers.cpp:77
SimpleTensor< T > tile(const SimpleTensor< T > &src, const Multiples &multiples)
Definition: Tile.cpp:38
TensorShape calculate_depth_concatenate_shape(const std::vector< TensorShape > &input_shapes)
Calculate output tensor shape give a vector of input tensor to concatenate.
SimpleTensor< uint8_t > convert_to_asymmetric(const SimpleTensor< float > &src, const QuantizationInfo &quantization_info)
Convert float simple tensor into quantized using specified quantization information.
Definition: Helpers.cpp:159
Parameters of Harris Corners algorithm.
Definition: Helpers.h:141
SimpleTensor< int16_t > convert_to_symmetric(const SimpleTensor< float > &src, const QuantizationInfo &quantization_info)
Convert float simple tensor into quantized using specified quantization information.
Definition: Helpers.cpp:207
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
std::pair< int, int > get_quantized_qasymm8_signed_bounds(const QuantizationInfo &quant_info, float min, float max)
Helper function to compute asymmetric quantized signed min and max bounds.
Definition: Helpers.cpp:386
void fill_lookuptable(T &&table)
Helper function to fill the Lut random by a ILutAccessor.
Definition: Helpers.h:169
void get_tile(const SimpleTensor< T > &in, SimpleTensor< T > &tile, const Coordinates &coord)
Get a 2D tile from a tensor.
Definition: Helpers.cpp:290
std::pair< int, int > get_symm_quantized_per_channel_bounds(const QuantizationInfo &quant_info, float min, float max, size_t channel_id)
Helper function to compute symmetric quantized min and max bounds.
Definition: Helpers.cpp:395
std::pair< T, T > get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, DataType data_type)
Helper function to get the testing range for each activation layer.
Definition: Helpers.h:62
TensorShape calculate_concatenate_shape(const std::vector< TensorShape > &input_shapes, size_t axis)
Calculate output tensor shape for the concatenate operation along a given axis.
void matrix_multiply(const SimpleTensor< T > &a, const SimpleTensor< T > &b, SimpleTensor< T > &out)
Matrix multiply between 2 float simple tensors.
Definition: Helpers.cpp:239
DataType
Available data types.
Definition: Types.h:77
MatrixPattern
Available matrix patterns.
Definition: Types.h:504
SimpleTensor< T > threshold(const SimpleTensor< T > &src, T threshold, T false_value, T true_value, ThresholdType type, T upper)
Definition: Threshold.cpp:35
SimpleTensor< float > convert_from_symmetric(const SimpleTensor< int16_t > &src)
Definition: Helpers.cpp:223