1 /*
2 * Copyright (c) 2017-2022 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 #include "tests/validation/Helpers.h"
25
26 #include <algorithm>
27 #include <cmath>
28
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 template <>
convert_from_asymmetric(const SimpleTensor<uint8_t> & src)36 SimpleTensor<float> convert_from_asymmetric(const SimpleTensor<uint8_t> &src)
37 {
38 const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
39 SimpleTensor<float> dst{ src.shape(), DataType::F32, 1, QuantizationInfo(), src.data_layout() };
40 #if defined(_OPENMP)
41 #pragma omp parallel for
42 #endif /* _OPENMP */
43 for(int i = 0; i < src.num_elements(); ++i)
44 {
45 dst[i] = dequantize_qasymm8(src[i], quantization_info);
46 }
47 return dst;
48 }
49
50 template <>
convert_from_asymmetric(const SimpleTensor<int8_t> & src)51 SimpleTensor<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
56 #if defined(_OPENMP)
57 #pragma omp parallel for
58 #endif /* _OPENMP */
59 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
66 template <>
convert_from_asymmetric(const SimpleTensor<uint16_t> & src)67 SimpleTensor<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
72 #if defined(_OPENMP)
73 #pragma omp parallel for
74 #endif /* _OPENMP */
75 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
82 template <>
convert_to_asymmetric(const SimpleTensor<float> & src,const QuantizationInfo & quantization_info)83 SimpleTensor<uint8_t> convert_to_asymmetric(const SimpleTensor<float> &src, const QuantizationInfo &quantization_info)
84 {
85 SimpleTensor<uint8_t> dst{ src.shape(), DataType::QASYMM8, 1, quantization_info };
86 const UniformQuantizationInfo &qinfo = quantization_info.uniform();
87
88 #if defined(_OPENMP)
89 #pragma omp parallel for
90 #endif /* _OPENMP */
91 for(int i = 0; i < src.num_elements(); ++i)
92 {
93 dst[i] = quantize_qasymm8(src[i], qinfo);
94 }
95 return dst;
96 }
97
98 template <>
convert_to_asymmetric(const SimpleTensor<float> & src,const QuantizationInfo & quantization_info)99 SimpleTensor<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
104 #if defined(_OPENMP)
105 #pragma omp parallel for
106 #endif /* _OPENMP */
107 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
114 template <>
convert_to_asymmetric(const SimpleTensor<float> & src,const QuantizationInfo & quantization_info)115 SimpleTensor<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
120 #if defined(_OPENMP)
121 #pragma omp parallel for
122 #endif /* _OPENMP */
123 for(int i = 0; i < src.num_elements(); ++i)
124 {
125 dst[i] = quantize_qasymm16(src[i], qinfo);
126 }
127 return dst;
128 }
129
130 template <>
convert_to_symmetric(const SimpleTensor<float> & src,const QuantizationInfo & quantization_info)131 SimpleTensor<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
136 #if defined(_OPENMP)
137 #pragma omp parallel for
138 #endif /* _OPENMP */
139 for(int i = 0; i < src.num_elements(); ++i)
140 {
141 dst[i] = quantize_qsymm16(src[i], qinfo);
142 }
143 return dst;
144 }
145
146 template <>
convert_from_symmetric(const SimpleTensor<int16_t> & src)147 SimpleTensor<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
152 #if defined(_OPENMP)
153 #pragma omp parallel for
154 #endif /* _OPENMP */
155 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
162 template <typename T>
matrix_multiply(const SimpleTensor<T> & a,const SimpleTensor<T> & b,SimpleTensor<T> & out)163 void matrix_multiply(const SimpleTensor<T> &a, const SimpleTensor<T> &b, SimpleTensor<T> &out)
164 {
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
173 #if defined(_OPENMP)
174 #pragma omp parallel for collapse(2)
175 #endif /* _OPENMP */
176 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
191 template <typename T>
transpose_matrix(const SimpleTensor<T> & in,SimpleTensor<T> & out)192 void transpose_matrix(const SimpleTensor<T> &in, SimpleTensor<T> &out)
193 {
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
199 #if defined(_OPENMP)
200 #pragma omp parallel for collapse(2)
201 #endif /* _OPENMP */
202 for(int y = 0; y < height; ++y)
203 {
204 for(int x = 0; x < width; ++x)
205 {
206 const T val = in[x + y * width];
207
208 out[x * height + y] = val;
209 }
210 }
211 }
212
213 template <typename T>
get_tile(const SimpleTensor<T> & in,SimpleTensor<T> & tile,const Coordinates & coord)214 void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord)
215 {
216 ARM_COMPUTE_ERROR_ON(tile.shape().num_dimensions() > 2);
217
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
273 template <typename T>
zeros(SimpleTensor<T> & in,const Coordinates & anchor,const TensorShape & shape)274 void 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
get_quantized_bounds(const QuantizationInfo & quant_info,float min,float max)301 std::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
305 const int min_bound = quantize_qasymm8(min, quant_info.uniform());
306 const int max_bound = quantize_qasymm8(max, quant_info.uniform());
307 return std::pair<int, int> { min_bound, max_bound };
308 }
309
get_quantized_qasymm8_signed_bounds(const QuantizationInfo & quant_info,float min,float max)310 std::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
get_symm_quantized_per_channel_bounds(const QuantizationInfo & quant_info,float min,float max,size_t channel_id)319 std::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
add_padding_x(std::initializer_list<ITensor * > tensors,const DataLayout & data_layout,bool only_right_pad)328 void add_padding_x(std::initializer_list<ITensor *> tensors, const DataLayout &data_layout, bool only_right_pad)
329 {
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);
345 const unsigned int left = only_right_pad ? 0 : distribution(gen);
346
347 tensor->info()->extend_padding(PaddingSize(0U, right, 0U, left));
348 }
349 }
350 }
351
add_padding_y(std::initializer_list<ITensor * > tensors,const DataLayout & data_layout)352 void 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
376 template void get_tile(const SimpleTensor<float> &in, SimpleTensor<float> &roi, const Coordinates &coord);
377 template void get_tile(const SimpleTensor<half> &in, SimpleTensor<half> &roi, const Coordinates &coord);
378 template void get_tile(const SimpleTensor<int> &in, SimpleTensor<int> &roi, const Coordinates &coord);
379 template void get_tile(const SimpleTensor<short> &in, SimpleTensor<short> &roi, const Coordinates &coord);
380 template void get_tile(const SimpleTensor<char> &in, SimpleTensor<char> &roi, const Coordinates &coord);
381 template void zeros(SimpleTensor<float> &in, const Coordinates &anchor, const TensorShape &shape);
382 template void zeros(SimpleTensor<half> &in, const Coordinates &anchor, const TensorShape &shape);
383 template void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out);
384 template void transpose_matrix(const SimpleTensor<half> &in, SimpleTensor<half> &out);
385 template void transpose_matrix(const SimpleTensor<int> &in, SimpleTensor<int> &out);
386 template void transpose_matrix(const SimpleTensor<short> &in, SimpleTensor<short> &out);
387 template void transpose_matrix(const SimpleTensor<char> &in, SimpleTensor<char> &out);
388 template void transpose_matrix(const SimpleTensor<int8_t> &in, SimpleTensor<int8_t> &out);
389 template void transpose_matrix(const SimpleTensor<uint8_t> &in, SimpleTensor<uint8_t> &out);
390 template void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out);
391 template void matrix_multiply(const SimpleTensor<half> &a, const SimpleTensor<half> &b, SimpleTensor<half> &out);
392
393 } // namespace validation
394 } // namespace test
395 } // namespace arm_compute
396