blob: be194dd26622d7b74e73b1154c95f0cddfa1ebf2 [file] [log] [blame]
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +01001/*
Adnan AlSinanc5849582022-05-05 11:13:19 +01002 * Copyright (c) 2017-2022 Arm Limited.
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +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 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010024#include "tests/validation/Helpers.h"
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010025
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000026#include <algorithm>
27#include <cmath>
28
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010029namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
Michalis Spyroued7b27d2019-11-27 16:04:17 +000035template <>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000036SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint8_t> &src)
37{
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010038 const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
39 SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() };
Michalis Spyroud1d77222020-04-08 14:10:15 +010040#if defined(_OPENMP)
41 #pragma omp parallel for
42#endif /* _OPENMP */
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000043 for(int i = 0; i < src.num_elements(); ++i)
44 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010045 dst[i] = dequantize_qasymm8(src[i], quantization_info);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000046 }
47 return dst;
48}
49
Michalis Spyroued7b27d2019-11-27 16:04:17 +000050template <>
Georgios Pinitas6e1791b2019-12-02 19:01:25 +000051SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<int8_t> &src)
52{
53 const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
54 SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() };
55
Michalis Spyroud1d77222020-04-08 14:10:15 +010056#if defined(_OPENMP)
57 #pragma omp parallel for
58#endif /* _OPENMP */
Georgios Pinitas6e1791b2019-12-02 19:01:25 +000059 for(int i = 0; i < src.num_elements(); ++i)
60 {
61 dst[i] = dequantize_qasymm8_signed(src[i], quantization_info);
62 }
63 return dst;
64}
65
66template <>
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010067SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint16_t> &src)
68{
69 const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
70 SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() };
71
Michalis Spyroud1d77222020-04-08 14:10:15 +010072#if defined(_OPENMP)
73 #pragma omp parallel for
74#endif /* _OPENMP */
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010075 for(int i = 0; i < src.num_elements(); ++i)
76 {
77 dst[i] = dequantize_qasymm16(src[i], quantization_info);
78 }
79 return dst;
80}
81
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010082template <>
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000083SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info)
84{
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010085 SimpleTensor<uint8_t> dst{ src.shape(), DataType::QASYMM8, 1, quantization_info };
86 const UniformQuantizationInfo &qinfo = quantization_info.uniform();
87
Michalis Spyroud1d77222020-04-08 14:10:15 +010088#if defined(_OPENMP)
89 #pragma omp parallel for
90#endif /* _OPENMP */
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000091 for(int i = 0; i < src.num_elements(); ++i)
92 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010093 dst[i] = quantize_qasymm8(src[i], qinfo);
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000094 }
95 return dst;
96}
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000097
Manuel Bottini3689fcd2019-06-14 17:18:12 +010098template <>
Sang-Hoon Parkae6ef7c2019-11-13 16:51:45 +000099SimpleTensor<int8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info)
100{
101 SimpleTensor<int8_t> dst{ src.shape(), DataType::QASYMM8_SIGNED, 1, quantization_info };
102 const UniformQuantizationInfo &qinfo = quantization_info.uniform();
103
Michalis Spyroud1d77222020-04-08 14:10:15 +0100104#if defined(_OPENMP)
105 #pragma omp parallel for
106#endif /* _OPENMP */
Sang-Hoon Parkae6ef7c2019-11-13 16:51:45 +0000107 for(int i = 0; i < src.num_elements(); ++i)
108 {
109 dst[i] = quantize_qasymm8_signed(src[i], qinfo);
110 }
111 return dst;
112}
113
Michalis Spyroued7b27d2019-11-27 16:04:17 +0000114template <>
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100115SimpleTensor<uint16_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info)
116{
117 SimpleTensor<uint16_t> dst{ src.shape(), DataType::QASYMM16, 1, quantization_info };
118 const UniformQuantizationInfo &qinfo = quantization_info.uniform();
119
Michalis Spyroud1d77222020-04-08 14:10:15 +0100120#if defined(_OPENMP)
121 #pragma omp parallel for
122#endif /* _OPENMP */
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100123 for(int i = 0; i < src.num_elements(); ++i)
124 {
125 dst[i] = quantize_qasymm16(src[i], qinfo);
126 }
127 return dst;
128}
129
130template <>
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100131SimpleTensor<int16_t> convert_to_symmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info)
132{
133 SimpleTensor<int16_t> dst{ src.shape(), DataType::QSYMM16, 1, quantization_info };
134 const UniformQuantizationInfo &qinfo = quantization_info.uniform();
135
Michalis Spyroud1d77222020-04-08 14:10:15 +0100136#if defined(_OPENMP)
137 #pragma omp parallel for
138#endif /* _OPENMP */
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100139 for(int i = 0; i < src.num_elements(); ++i)
140 {
141 dst[i] = quantize_qsymm16(src[i], qinfo);
142 }
143 return dst;
144}
145
146template <>
147SimpleTensor<float> convert_from_symmetric(const SimpleTensor<int16_t> &src)
148{
149 const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
150 SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() };
151
Michalis Spyroud1d77222020-04-08 14:10:15 +0100152#if defined(_OPENMP)
153 #pragma omp parallel for
154#endif /* _OPENMP */
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100155 for(int i = 0; i < src.num_elements(); ++i)
156 {
157 dst[i] = dequantize_qsymm16(src[i], quantization_info);
158 }
159 return dst;
160}
161
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100162template <typename T>
163void matrix_multiply(const SimpleTensor<T> &a, const SimpleTensor<T> &b, SimpleTensor<T> &out)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000164{
165 ARM_COMPUTE_ERROR_ON(a.shape()[0] != b.shape()[1]);
166 ARM_COMPUTE_ERROR_ON(a.shape()[1] != out.shape()[1]);
167 ARM_COMPUTE_ERROR_ON(b.shape()[0] != out.shape()[0]);
168
169 const int M = a.shape()[1]; // Rows
170 const int N = b.shape()[0]; // Cols
171 const int K = b.shape()[1];
172
Michalis Spyroud1d77222020-04-08 14:10:15 +0100173#if defined(_OPENMP)
174 #pragma omp parallel for collapse(2)
175#endif /* _OPENMP */
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000176 for(int y = 0; y < M; ++y)
177 {
178 for(int x = 0; x < N; ++x)
179 {
180 float acc = 0.0f;
181 for(int k = 0; k < K; ++k)
182 {
183 acc += a[y * K + k] * b[x + k * N];
184 }
185
186 out[x + y * N] = acc;
187 }
188 }
189}
190
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100191template <typename T>
192void transpose_matrix(const SimpleTensor<T> &in, SimpleTensor<T> &out)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000193{
194 ARM_COMPUTE_ERROR_ON((in.shape()[0] != out.shape()[1]) || (in.shape()[1] != out.shape()[0]));
195
196 const int width = in.shape()[0];
197 const int height = in.shape()[1];
198
Michalis Spyroud1d77222020-04-08 14:10:15 +0100199#if defined(_OPENMP)
200 #pragma omp parallel for collapse(2)
201#endif /* _OPENMP */
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000202 for(int y = 0; y < height; ++y)
203 {
204 for(int x = 0; x < width; ++x)
205 {
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000206 const T val = in[x + y * width];
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000207
208 out[x * height + y] = val;
209 }
210 }
211}
212
213template <typename T>
214void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord)
215{
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100216 ARM_COMPUTE_ERROR_ON(tile.shape().num_dimensions() > 2);
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000217
218 const int w_tile = tile.shape()[0];
219 const int h_tile = tile.shape()[1];
220
221 // Fill the tile with zeros
222 std::fill(tile.data() + 0, (tile.data() + (w_tile * h_tile)), static_cast<T>(0));
223
224 // Check if with the dimensions greater than 2 we could have out-of-bound reads
225 for(size_t d = 2; d < Coordinates::num_max_dimensions; ++d)
226 {
227 if(coord[d] < 0 || coord[d] >= static_cast<int>(in.shape()[d]))
228 {
229 ARM_COMPUTE_ERROR("coord[d] < 0 || coord[d] >= in.shape()[d] with d >= 2");
230 }
231 }
232
233 // Since we could have out-of-bound reads along the X and Y dimensions,
234 // we start calculating the input address with x = 0 and y = 0
235 Coordinates start_coord = coord;
236 start_coord[0] = 0;
237 start_coord[1] = 0;
238
239 // Get input and roi pointers
240 auto in_ptr = static_cast<const T *>(in(start_coord));
241 auto roi_ptr = static_cast<T *>(tile.data());
242
243 const int x_in_start = std::max(0, coord[0]);
244 const int y_in_start = std::max(0, coord[1]);
245 const int x_in_end = std::min(static_cast<int>(in.shape()[0]), coord[0] + w_tile);
246 const int y_in_end = std::min(static_cast<int>(in.shape()[1]), coord[1] + h_tile);
247
248 // Number of elements to copy per row
249 const int n = x_in_end - x_in_start;
250
251 // Starting coordinates for the ROI
252 const int x_tile_start = coord[0] > 0 ? 0 : std::abs(coord[0]);
253 const int y_tile_start = coord[1] > 0 ? 0 : std::abs(coord[1]);
254
255 // Update input pointer
256 in_ptr += x_in_start;
257 in_ptr += (y_in_start * in.shape()[0]);
258
259 // Update ROI pointer
260 roi_ptr += x_tile_start;
261 roi_ptr += (y_tile_start * tile.shape()[0]);
262
263 for(int y = y_in_start; y < y_in_end; ++y)
264 {
265 // Copy per row
266 std::copy(in_ptr, in_ptr + n, roi_ptr);
267
268 in_ptr += in.shape()[0];
269 roi_ptr += tile.shape()[0];
270 }
271}
272
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100273template <typename T>
274void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape)
275{
276 ARM_COMPUTE_ERROR_ON(anchor.num_dimensions() != shape.num_dimensions());
277 ARM_COMPUTE_ERROR_ON(in.shape().num_dimensions() > 2);
278 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() > 2);
279
280 // Check if with the dimensions greater than 2 we could have out-of-bound reads
281 for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
282 {
283 if(anchor[d] < 0 || ((anchor[d] + shape[d]) > in.shape()[d]))
284 {
285 ARM_COMPUTE_ERROR("anchor[d] < 0 || (anchor[d] + shape[d]) > in.shape()[d]");
286 }
287 }
288
289 // Get input pointer
290 auto in_ptr = static_cast<T *>(in(anchor[0] + anchor[1] * in.shape()[0]));
291
292 const unsigned int n = in.shape()[0];
293
294 for(unsigned int y = 0; y < shape[1]; ++y)
295 {
296 std::fill(in_ptr, in_ptr + shape[0], 0);
297 in_ptr += n;
298 }
299}
300
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100301std::pair<int, int> get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max)
302{
303 ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max");
304
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100305 const int min_bound = quantize_qasymm8(min, quant_info.uniform());
306 const int max_bound = quantize_qasymm8(max, quant_info.uniform());
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100307 return std::pair<int, int> { min_bound, max_bound };
Michele Di Giorgioed5a4922018-09-13 16:22:01 +0100308}
309
Georgios Pinitas6e1791b2019-12-02 19:01:25 +0000310std::pair<int, int> get_quantized_qasymm8_signed_bounds(const QuantizationInfo &quant_info, float min, float max)
311{
312 ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max");
313
314 const int min_bound = quantize_qasymm8_signed(min, quant_info.uniform());
315 const int max_bound = quantize_qasymm8_signed(max, quant_info.uniform());
316 return std::pair<int, int> { min_bound, max_bound };
317}
318
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100319std::pair<int, int> get_symm_quantized_per_channel_bounds(const QuantizationInfo &quant_info, float min, float max, size_t channel_id)
320{
321 ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max");
322
323 const int min_bound = quantize_qsymm8_per_channel(min, quant_info, channel_id);
324 const int max_bound = quantize_qsymm8_per_channel(max, quant_info, channel_id);
325 return std::pair<int, int> { min_bound, max_bound };
326}
327
Manuel Bottinif733e032021-05-19 16:15:36 +0100328void add_padding_x(std::initializer_list<ITensor *> tensors, const DataLayout &data_layout, bool only_right_pad)
Giorgio Arena63825e82021-03-25 14:54:50 +0000329{
330 if(data_layout == DataLayout::NHWC)
331 {
332 constexpr unsigned int lower = 1U;
333 constexpr unsigned int upper = 16U;
334
335 std::uniform_int_distribution<unsigned int> distribution(lower, upper);
336 size_t seed_offset = 0;
337
338 for(ITensor *tensor : tensors)
339 {
340 ARM_COMPUTE_ERROR_ON(!tensor->info()->is_resizable());
341
342 std::mt19937 gen(library->seed() + seed_offset++);
343
344 const unsigned int right = distribution(gen);
Manuel Bottinif733e032021-05-19 16:15:36 +0100345 const unsigned int left = only_right_pad ? 0 : distribution(gen);
Giorgio Arena63825e82021-03-25 14:54:50 +0000346
347 tensor->info()->extend_padding(PaddingSize(0U, right, 0U, left));
348 }
349 }
350}
351
Gian Marco Iodice72b56872021-06-29 10:08:46 +0100352void add_padding_y(std::initializer_list<ITensor *> tensors, const DataLayout &data_layout)
353{
354 if(data_layout == DataLayout::NHWC)
355 {
356 constexpr unsigned int lower = 1U;
357 constexpr unsigned int upper = 4U;
358
359 std::uniform_int_distribution<unsigned int> distribution(lower, upper);
360 size_t seed_offset = 0;
361
362 for(ITensor *tensor : tensors)
363 {
364 ARM_COMPUTE_ERROR_ON(!tensor->info()->is_resizable());
365
366 std::mt19937 gen(library->seed() + seed_offset++);
367
368 const unsigned int top = distribution(gen);
369 const unsigned int bottom = distribution(gen);
370
371 tensor->info()->extend_padding(PaddingSize(top, 0U, bottom, 0U));
372 }
373 }
374}
375
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000376template void get_tile(const SimpleTensor<float> &in, SimpleTensor<float> &roi, const Coordinates &coord);
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100377template void get_tile(const SimpleTensor<half> &in, SimpleTensor<half> &roi, const Coordinates &coord);
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000378template void get_tile(const SimpleTensor<int> &in, SimpleTensor<int> &roi, const Coordinates &coord);
379template void get_tile(const SimpleTensor<short> &in, SimpleTensor<short> &roi, const Coordinates &coord);
380template void get_tile(const SimpleTensor<char> &in, SimpleTensor<char> &roi, const Coordinates &coord);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +0100381template void zeros(SimpleTensor<float> &in, const Coordinates &anchor, const TensorShape &shape);
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100382template void zeros(SimpleTensor<half> &in, const Coordinates &anchor, const TensorShape &shape);
383template void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out);
384template void transpose_matrix(const SimpleTensor<half> &in, SimpleTensor<half> &out);
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +0000385template void transpose_matrix(const SimpleTensor<int> &in, SimpleTensor<int> &out);
386template void transpose_matrix(const SimpleTensor<short> &in, SimpleTensor<short> &out);
387template void transpose_matrix(const SimpleTensor<char> &in, SimpleTensor<char> &out);
Adnan AlSinanc5849582022-05-05 11:13:19 +0100388template void transpose_matrix(const SimpleTensor<int8_t> &in, SimpleTensor<int8_t> &out);
389template void transpose_matrix(const SimpleTensor<uint8_t> &in, SimpleTensor<uint8_t> &out);
Vidhya Sudhan Loganathan71ecf392018-08-31 16:10:16 +0100390template void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out);
391template void matrix_multiply(const SimpleTensor<half> &a, const SimpleTensor<half> &b, SimpleTensor<half> &out);
392
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +0100393} // namespace validation
394} // namespace test
395} // namespace arm_compute