blob: e044620556267d9cd630b6f33394435ce521f2b7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Renato Arantes36a75da2024-01-26 17:31:18 +00002 * Copyright (c) 2017-2023,2024 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 */
Anitha Rajb566b6e2023-08-23 11:40:06 +010024#ifndef ACL_TESTS_VALIDATION_HELPERS_H
25#define ACL_TESTS_VALIDATION_HELPERS_H
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"
Renato Arantes36a75da2024-01-26 17:31:18 +000030
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010031#include "support/Half.h"
John Richardson6f4d49f2017-09-07 11:21:10 +010032#include "tests/Globals.h"
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010033#include "tests/SimpleTensor.h"
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010034
Gunes Bayir532ce2c2023-09-14 09:13:49 +010035#include <cmath>
36#include <cstdint>
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010037#include <random>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038#include <type_traits>
39#include <utility>
40
41namespace arm_compute
42{
43namespace test
44{
45namespace validation
46{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010047template <typename T>
48struct is_floating_point : public std::is_floating_point<T>
Pablo Tello8fda1cb2017-07-05 15:20:38 +010049{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010050};
51
52template <>
Georgios Pinitas583137c2017-08-31 18:12:42 +010053struct is_floating_point<half> : public std::true_type
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010054{
55};
Renato Arantes36a75da2024-01-26 17:31:18 +000056template <>
57struct is_floating_point<bfloat16> : public std::true_type
58{
59};
Pablo Tello8fda1cb2017-07-05 15:20:38 +010060
Gunes Bayir532ce2c2023-09-14 09:13:49 +010061/** Helper struct to store the hints for
62 * - destination quantization info
63 * - minimum bias value
64 * - maximum bias value
65 * in quantized test construction.
66 */
67struct QuantizationHint
68{
69 QuantizationInfo q_info;
70 int32_t bias_min;
71 int32_t bias_max;
72};
73
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074/** Helper function to get the testing range for each activation layer.
75 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010076 * @param[in] activation Activation function to test.
77 * @param[in] data_type Data type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 *
79 * @return A pair containing the lower upper testing bounds for a given function.
80 */
81template <typename T>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010082std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, DataType data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 std::pair<T, T> bounds;
85
Renato Arantes36a75da2024-01-26 17:31:18 +000086 switch (data_type)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010088 case DataType::F16:
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010089 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010090 using namespace half_float::literal;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010091
Renato Arantes36a75da2024-01-26 17:31:18 +000092 switch (activation)
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010093 {
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010094 case ActivationLayerInfo::ActivationFunction::TANH:
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010095 case ActivationLayerInfo::ActivationFunction::SQUARE:
96 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
97 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
98 // Reduce range as exponent overflows
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010099 bounds = std::make_pair(-2._h, 2._h);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100100 break;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100101 case ActivationLayerInfo::ActivationFunction::SQRT:
102 // Reduce range as sqrt should take a non-negative number
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100103 bounds = std::make_pair(0._h, 128._h);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100104 break;
105 default:
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100106 bounds = std::make_pair(-255._h, 255._h);
107 break;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100108 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100109 break;
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100110 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100111 case DataType::F32:
Renato Arantes36a75da2024-01-26 17:31:18 +0000112 switch (activation)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100113 {
Gunes Bayir01934e92022-11-02 11:50:37 +0000114 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
115 // Reduce range as exponent overflows
116 bounds = std::make_pair(-40.f, 40.f);
117 break;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100118 case ActivationLayerInfo::ActivationFunction::SQRT:
119 // Reduce range as sqrt should take a non-negative number
120 bounds = std::make_pair(0.f, 255.f);
121 break;
122 default:
123 bounds = std::make_pair(-255.f, 255.f);
124 break;
125 }
126 break;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100127 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
Sang-Hoon Park0779fec2019-11-13 17:08:12 +0000134/** Convert an asymmetric quantized simple tensor into float using tensor quantization information.
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000135 *
136 * @param[in] src Quantized tensor.
137 *
138 * @return Float tensor.
Anthony Barbierf202e502017-11-23 18:02:04 +0000139 */
Michalis Spyroued7b27d2019-11-27 16:04:17 +0000140template <typename T>
141SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<T> &src);
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100142
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000143/** Convert float simple tensor into quantized using specified quantization information.
144 *
145 * @param[in] src Float tensor.
146 * @param[in] quantization_info Quantification information.
147 *
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +0100148 * \relates arm_compute::test::SimpleTensor
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000149 * @return Quantized tensor.
Anthony Barbierf202e502017-11-23 18:02:04 +0000150 */
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100151template <typename T>
152SimpleTensor<T> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info);
153
154/** Convert quantized simple tensor into float using tensor quantization information.
155 *
156 * @param[in] src Quantized tensor.
157 *
158 * @return Float tensor.
159 */
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100160template <typename T>
161SimpleTensor<float> convert_from_symmetric(const SimpleTensor<T> &src);
162
163/** Convert float simple tensor into quantized using specified quantization information.
164 *
165 * @param[in] src Float tensor.
166 * @param[in] quantization_info Quantification information.
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +0100167 * \relates arm_compute::test::SimpleTensor
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100168 * @return Quantized tensor.
169 */
170template <typename T>
171SimpleTensor<T> convert_to_symmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info);
172
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000173/** Matrix multiply between 2 float simple tensors
174 *
175 * @param[in] a Input tensor A
176 * @param[in] b Input tensor B
177 * @param[out] out Output tensor
178 *
179 */
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100180template <typename T>
181void matrix_multiply(const SimpleTensor<T> &a, const SimpleTensor<T> &b, SimpleTensor<T> &out);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000182
183/** Transpose matrix
184 *
185 * @param[in] in Input tensor
186 * @param[out] out Output tensor
187 *
188 */
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100189template <typename T>
190void transpose_matrix(const SimpleTensor<T> &in, SimpleTensor<T> &out);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000191
192/** Get a 2D tile from a tensor
193 *
194 * @note In case of out-of-bound reads, the tile will be filled with zeros
195 *
196 * @param[in] in Input tensor
197 * @param[out] tile Tile
198 * @param[in] coord Coordinates
199 */
200template <typename T>
201void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100202
203/** Fill with zeros the input tensor in the area defined by anchor and shape
204 *
205 * @param[in] in Input tensor to fill with zeros
206 * @param[out] anchor Starting point of the zeros area
207 * @param[in] shape Ending point of the zeros area
208 */
209template <typename T>
210void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape);
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100211
212/** Helper function to compute quantized min and max bounds
213 *
214 * @param[in] quant_info Quantization info to be used for conversion
215 * @param[in] min Floating point minimum value to be quantized
216 * @param[in] max Floating point maximum value to be quantized
217 */
218std::pair<int, int> get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100219
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000220/** Helper function to compute asymmetric quantized signed min and max bounds
221 *
222 * @param[in] quant_info Quantization info to be used for conversion
223 * @param[in] min Floating point minimum value to be quantized
224 * @param[in] max Floating point maximum value to be quantized
225 */
226std::pair<int, int> get_quantized_qasymm8_signed_bounds(const QuantizationInfo &quant_info, float min, float max);
227
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100228/** Helper function to compute symmetric quantized min and max bounds
229 *
230 * @param[in] quant_info Quantization info to be used for conversion
231 * @param[in] min Floating point minimum value to be quantized
232 * @param[in] max Floating point maximum value to be quantized
233 * @param[in] channel_id Channel id for per channel quantization info.
234 */
Renato Arantes36a75da2024-01-26 17:31:18 +0000235std::pair<int, int>
236get_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 +0000237
Giorgio Arenaebbb6f92021-04-13 09:52:18 +0100238/** Add random padding along the X axis (between 1 and 16 columns per side) to all the input tensors.
239 * 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 +0000240 *
Manuel Bottinif733e032021-05-19 16:15:36 +0100241 * @param[in] tensors List of tensors to add padding to
242 * @param[in] data_layout (Optional) Data layout of the operator
243 * @param[in] only_right_pad (Optional) Only right padding testing, in case of cl image padding
Giorgio Arena63825e82021-03-25 14:54:50 +0000244 *
245 * @note This function adds padding to the input tensors only if data_layout == DataLayout::NHWC
246 */
Renato Arantes36a75da2024-01-26 17:31:18 +0000247void add_padding_x(std::initializer_list<ITensor *> tensors,
248 const DataLayout &data_layout = DataLayout::NHWC,
249 bool only_right_pad = false);
Gian Marco Iodice72b56872021-06-29 10:08:46 +0100250
Gunes Bayirdfcd41a2023-10-11 09:56:05 +0100251/** For 2d convolution, given the Lhs/Rhs matrix quantization informations and the convolution dimension,
252 * calculate a suitable output quantization and suggested bias range for obtaining non-saturated outputs with high probability.
253 *
254 * @param[in] in_q_info Input matrix quantization info
255 * @param[in] weight_q_info Weights matrix quantization info
256 * @param[in] height Height of the weights tensor
257 * @param[in] width Width of the weights tensors
258 * @param[in] channels Number of input channels
259 * @param[in] data_type data type, only QASYMM8, QASYMM8_SIGNED are supported
260 * @param[in] bias_fraction see @ref suggest_mac_dst_q_info_and_bias() for explanation
261 *
262 * @return QuantizationHint object containing the suggested output quantization info and min/max bias range
263 */
264QuantizationHint suggest_conv_dst_q_info_and_bias(const QuantizationInfo &in_q_info,
265 const QuantizationInfo &weight_q_info,
Renato Arantes36a75da2024-01-26 17:31:18 +0000266 int32_t height,
267 int32_t width,
268 int32_t channels,
269 DataType data_type,
270 float bias_fraction);
Gunes Bayirdfcd41a2023-10-11 09:56:05 +0100271
Gunes Bayir532ce2c2023-09-14 09:13:49 +0100272/** For a matrix multiplication, given the Lhs/Rhs matrix quantization informations and the matrix multiplication dimensions,
273 * calculate a suitable output quantization and suggested bias range for obtaining non-saturated outputs with high probability.
274 *
275 * @param[in] lhs_q_info Lhs matrix quantization info
276 * @param[in] rhs_q_info Rhs matrix quantization info
277 * @param[in] m Number of rows of Lhs matrix
278 * @param[in] n Number of columns of Rhs Matrix
279 * @param[in] k Number of rows/columns of Rhs/Lhs Matrix
280 * @param[in] data_type data type, only QASYMM8, QASYMM8_SIGNED are supported
Gunes Bayirdfcd41a2023-10-11 09:56:05 +0100281 * @param[in] bias_fraction see @ref suggest_mac_dst_q_info_and_bias() for explanation
Gunes Bayir532ce2c2023-09-14 09:13:49 +0100282 *
283 * @return QuantizationHint object containing the suggested output quantization info and min/max bias range
Gunes Bayir9d0c4de2023-04-13 18:22:58 +0100284 */
Gunes Bayir532ce2c2023-09-14 09:13:49 +0100285QuantizationHint suggest_matmul_dst_q_info_and_bias(const QuantizationInfo &lhs_q_info,
Renato Arantes36a75da2024-01-26 17:31:18 +0000286 const QuantizationInfo &rhs_q_info,
287 int32_t m,
288 int32_t n,
289 int32_t k,
290 DataType data_type,
291 float bias_fraction);
Gunes Bayir532ce2c2023-09-14 09:13:49 +0100292
293/** For a multiply-accumulate (mac), given the Lhs/Rhs vector quantization informations and the dot product dimensions,
294 * calculate a suitable output quantization and suggested bias range for obtaining non-saturated outputs with high probability.
295 *
296 * @param[in] lhs_q_info Lhs matrix quantization info
297 * @param[in] rhs_q_info Rhs matrix quantization info
298 * @param[in] k number of accumulations taking place in the sum, i.e. c_k = sum_k(a_k * b_k)
299 * @param[in] data_type data type, only QASYMM8, QASYMM8_SIGNED are supported
300 * @param[in] bias_fraction the fraction of bias amplitude compared to integer accummulation.
Mohammed Suhail Munshi02c452f2023-10-26 00:14:36 +0100301 * @param[in] num_sd (Optional) number of standard deviations we allow from the mean. Default value is 2.
Gunes Bayir532ce2c2023-09-14 09:13:49 +0100302 *
303 * @return QuantizationHint object containing the suggested output quantization info and min/max bias range
304 */
305QuantizationHint suggest_mac_dst_q_info_and_bias(const QuantizationInfo &lhs_q_info,
Renato Arantes36a75da2024-01-26 17:31:18 +0000306 const QuantizationInfo &rhs_q_info,
307 int32_t k,
308 DataType data_type,
309 float bias_fraction,
310 int num_sd = 2);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311} // namespace validation
312} // namespace test
313} // namespace arm_compute
Anitha Rajb566b6e2023-08-23 11:40:06 +0100314#endif // ACL_TESTS_VALIDATION_HELPERS_H