blob: 3f91d55db186417cfc12e9bd02d28358c5a6f1ad [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gunes Bayir9d0c4de2023-04-13 18:22:58 +01002 * Copyright (c) 2017-2023 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 */
Gunes Bayir9d0c4de2023-04-13 18:22:58 +010024#ifndef ACL_TESTS_VALIDATION_HELPERS
25#define ACL_TESTS_VALIDATION_HELPERS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
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"
SiCong Li91295492023-07-21 18:16:13 +010029#include "arm_compute/function_info/ActivationLayerInfo.h"
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010030#include "support/Half.h"
John Richardson6f4d49f2017-09-07 11:21:10 +010031#include "tests/Globals.h"
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010032#include "tests/SimpleTensor.h"
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010033
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000034#include <math.h>
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010035#include <random>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include <type_traits>
37#include <utility>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010045template <typename T>
46struct is_floating_point : public std::is_floating_point<T>
Pablo Tello8fda1cb2017-07-05 15:20:38 +010047{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010048};
49
50template <>
Georgios Pinitas583137c2017-08-31 18:12:42 +010051struct is_floating_point<half> : public std::true_type
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010052{
53};
Pablo Tello8fda1cb2017-07-05 15:20:38 +010054
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055/** Helper function to get the testing range for each activation layer.
56 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010057 * @param[in] activation Activation function to test.
58 * @param[in] data_type Data type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 *
60 * @return A pair containing the lower upper testing bounds for a given function.
61 */
62template <typename T>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010063std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 std::pair<T, T> bounds;
66
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010067 switch(data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010069 case DataType::F16:
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010070 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010071 using namespace half_float::literal;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010072
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010073 switch(activation)
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010074 {
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010075 case ActivationLayerInfo::ActivationFunction::TANH:
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010076 case ActivationLayerInfo::ActivationFunction::SQUARE:
77 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
78 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
79 // Reduce range as exponent overflows
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010080 bounds = std::make_pair(-2._h, 2._h);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010081 break;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010082 case ActivationLayerInfo::ActivationFunction::SQRT:
83 // Reduce range as sqrt should take a non-negative number
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010084 bounds = std::make_pair(0._h, 128._h);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010085 break;
86 default:
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010087 bounds = std::make_pair(-255._h, 255._h);
88 break;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010089 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010090 break;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010091 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010092 case DataType::F32:
93 switch(activation)
94 {
Gunes Bayir01934e92022-11-02 11:50:37 +000095 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
96 // Reduce range as exponent overflows
97 bounds = std::make_pair(-40.f, 40.f);
98 break;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010099 case ActivationLayerInfo::ActivationFunction::SQRT:
100 // Reduce range as sqrt should take a non-negative number
101 bounds = std::make_pair(0.f, 255.f);
102 break;
103 default:
104 bounds = std::make_pair(-255.f, 255.f);
105 break;
106 }
107 break;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100108 default:
109 ARM_COMPUTE_ERROR("Unsupported data type");
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100110 }
111
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100112 return bounds;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100113}
114
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100115/** Calculate output tensor shape give a vector of input tensor to concatenate
116 *
117 * @param[in] input_shapes Shapes of the tensors to concatenate across depth.
118 *
119 * @return The shape of output concatenated tensor.
120 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100121TensorShape calculate_depth_concatenate_shape(const std::vector<TensorShape> &input_shapes);
Sanghoon Leea7a5b7b2017-09-14 12:11:03 +0100122
Pablo Tello3dd5b682019-03-04 14:14:02 +0000123/** Calculate output tensor shape for the concatenate operation along a given axis
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100124 *
125 * @param[in] input_shapes Shapes of the tensors to concatenate across width.
Pablo Tello3dd5b682019-03-04 14:14:02 +0000126 * @param[in] axis Axis to use for the concatenate operation
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100127 *
128 * @return The shape of output concatenated tensor.
129 */
Pablo Tello3dd5b682019-03-04 14:14:02 +0000130TensorShape calculate_concatenate_shape(const std::vector<TensorShape> &input_shapes, size_t axis);
Michalis Spyrou55b3d122018-05-09 09:59:23 +0100131
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000132/** Convert an asymmetric quantized simple tensor into float using tensor quantization information.
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000133 *
134 * @param[in] src Quantized tensor.
135 *
136 * @return Float tensor.
Anthony Barbierf202e502017-11-23 18:02:04 +0000137 */
Michalis Spyroued7b27d2019-11-27 16:04:17 +0000138template <typename T>
139SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<T> &src);
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100140
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000141/** Convert float simple tensor into quantized using specified quantization information.
142 *
143 * @param[in] src Float tensor.
144 * @param[in] quantization_info Quantification information.
145 *
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +0100146 * \relates arm_compute::test::SimpleTensor
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000147 * @return Quantized tensor.
Anthony Barbierf202e502017-11-23 18:02:04 +0000148 */
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100149template <typename T>
150SimpleTensor<T> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info);
151
152/** Convert quantized simple tensor into float using tensor quantization information.
153 *
154 * @param[in] src Quantized tensor.
155 *
156 * @return Float tensor.
157 */
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100158template <typename T>
159SimpleTensor<float> convert_from_symmetric(const SimpleTensor<T> &src);
160
161/** Convert float simple tensor into quantized using specified quantization information.
162 *
163 * @param[in] src Float tensor.
164 * @param[in] quantization_info Quantification information.
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +0100165 * \relates arm_compute::test::SimpleTensor
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100166 * @return Quantized tensor.
167 */
168template <typename T>
169SimpleTensor<T> convert_to_symmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info);
170
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000171/** Matrix multiply between 2 float simple tensors
172 *
173 * @param[in] a Input tensor A
174 * @param[in] b Input tensor B
175 * @param[out] out Output tensor
176 *
177 */
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100178template <typename T>
179void matrix_multiply(const SimpleTensor<T> &a, const SimpleTensor<T> &b, SimpleTensor<T> &out);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000180
181/** Transpose matrix
182 *
183 * @param[in] in Input tensor
184 * @param[out] out Output tensor
185 *
186 */
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100187template <typename T>
188void transpose_matrix(const SimpleTensor<T> &in, SimpleTensor<T> &out);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000189
190/** Get a 2D tile from a tensor
191 *
192 * @note In case of out-of-bound reads, the tile will be filled with zeros
193 *
194 * @param[in] in Input tensor
195 * @param[out] tile Tile
196 * @param[in] coord Coordinates
197 */
198template <typename T>
199void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100200
201/** Fill with zeros the input tensor in the area defined by anchor and shape
202 *
203 * @param[in] in Input tensor to fill with zeros
204 * @param[out] anchor Starting point of the zeros area
205 * @param[in] shape Ending point of the zeros area
206 */
207template <typename T>
208void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape);
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100209
210/** Helper function to compute quantized min and max bounds
211 *
212 * @param[in] quant_info Quantization info to be used for conversion
213 * @param[in] min Floating point minimum value to be quantized
214 * @param[in] max Floating point maximum value to be quantized
215 */
216std::pair<int, int> get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100217
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000218/** Helper function to compute asymmetric quantized signed min and max bounds
219 *
220 * @param[in] quant_info Quantization info to be used for conversion
221 * @param[in] min Floating point minimum value to be quantized
222 * @param[in] max Floating point maximum value to be quantized
223 */
224std::pair<int, int> get_quantized_qasymm8_signed_bounds(const QuantizationInfo &quant_info, float min, float max);
225
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100226/** Helper function to compute symmetric quantized min and max bounds
227 *
228 * @param[in] quant_info Quantization info to be used for conversion
229 * @param[in] min Floating point minimum value to be quantized
230 * @param[in] max Floating point maximum value to be quantized
231 * @param[in] channel_id Channel id for per channel quantization info.
232 */
233std::pair<int, int> get_symm_quantized_per_channel_bounds(const QuantizationInfo &quant_info, float min, float max, size_t channel_id = 0);
Giorgio Arena63825e82021-03-25 14:54:50 +0000234
Giorgio Arenaebbb6f92021-04-13 09:52:18 +0100235/** Add random padding along the X axis (between 1 and 16 columns per side) to all the input tensors.
236 * This is used in our validation suite in order to simulate implicit padding addition after configuring, but before allocating.
Giorgio Arena63825e82021-03-25 14:54:50 +0000237 *
Manuel Bottinif733e032021-05-19 16:15:36 +0100238 * @param[in] tensors List of tensors to add padding to
239 * @param[in] data_layout (Optional) Data layout of the operator
240 * @param[in] only_right_pad (Optional) Only right padding testing, in case of cl image padding
Giorgio Arena63825e82021-03-25 14:54:50 +0000241 *
242 * @note This function adds padding to the input tensors only if data_layout == DataLayout::NHWC
243 */
Manuel Bottinif733e032021-05-19 16:15:36 +0100244void add_padding_x(std::initializer_list<ITensor *> tensors, const DataLayout &data_layout = DataLayout::NHWC, bool only_right_pad = false);
Gian Marco Iodice72b56872021-06-29 10:08:46 +0100245
Gunes Bayir9d0c4de2023-04-13 18:22:58 +0100246/** For MatMulLowp, given the Lhs/Rhs matrix quantization informations and the matrix multiplication dimensions,
247 * calculate a suitable output quantization for obtaining non-saturated outputs with high probability.
248 */
249QuantizationInfo calculate_mat_mul_dst_q_info(const QuantizationInfo &lhs_q_info, const QuantizationInfo &rhs_q_info, int m, int n, int k, DataType data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100250} // namespace validation
251} // namespace test
252} // namespace arm_compute
Gunes Bayir9d0c4de2023-04-13 18:22:58 +0100253#endif /* ACL_TESTS_VALIDATION_HELPERS */