blob: 8630eeee236d626a20ef2a4a8769679a0b2090e0 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrouf63885b2019-01-16 14:18:09 +00002 * Copyright (c) 2016-2019 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 */
24#ifndef __ARM_COMPUTE_UTILS_H__
25#define __ARM_COMPUTE_UTILS_H__
26
27#include "arm_compute/core/Error.h"
Giuseppe Rossinid7647d42018-07-17 18:13:13 +010028#include "arm_compute/core/PixelValue.h"
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000029#include "arm_compute/core/Rounding.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Types.h"
31
32#include <algorithm>
33#include <cstdint>
34#include <cstdlib>
35#include <numeric>
36#include <sstream>
37#include <string>
38#include <type_traits>
39#include <utility>
steniu017ce53c62017-09-29 14:55:00 +010040#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041
42namespace arm_compute
43{
Alex Gildayc357c472018-03-21 13:54:09 +000044/** Calculate the rounded up quotient of val / m.
45 *
46 * @param[in] val Value to divide and round up.
47 * @param[in] m Value to divide by.
48 *
49 * @return the result.
50 */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000051template <typename S, typename T>
52constexpr auto DIV_CEIL(S val, T m) -> decltype((val + m - 1) / m)
53{
54 return (val + m - 1) / m;
55}
56
Alex Gildayc357c472018-03-21 13:54:09 +000057/** Computes the smallest number larger or equal to value that is a multiple of divisor.
58 *
59 * @param[in] value Lower bound value
60 * @param[in] divisor Value to compute multiple of.
61 *
62 * @return the result.
63 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064template <typename S, typename T>
65inline auto ceil_to_multiple(S value, T divisor) -> decltype(((value + divisor - 1) / divisor) * divisor)
66{
67 ARM_COMPUTE_ERROR_ON(value < 0 || divisor <= 0);
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000068 return DIV_CEIL(value, divisor) * divisor;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069}
70
Alex Gildayc357c472018-03-21 13:54:09 +000071/** Computes the largest number smaller or equal to value that is a multiple of divisor.
72 *
73 * @param[in] value Upper bound value
74 * @param[in] divisor Value to compute multiple of.
75 *
76 * @return the result.
77 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078template <typename S, typename T>
79inline auto floor_to_multiple(S value, T divisor) -> decltype((value / divisor) * divisor)
80{
81 ARM_COMPUTE_ERROR_ON(value < 0 || divisor <= 0);
82 return (value / divisor) * divisor;
83}
84
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085/** Returns the arm_compute library build information
86 *
87 * Contains the version number and the build options used to build the library
88 *
89 * @return The arm_compute library build information
90 */
91std::string build_information();
92
93/** Load an entire file in memory
94 *
95 * @param[in] filename Name of the file to read.
96 * @param[in] binary Is it a binary file ?
97 *
98 * @return The content of the file.
99 */
100std::string read_file(const std::string &filename, bool binary);
101
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102/** The size in bytes of the data type
103 *
104 * @param[in] data_type Input data type
105 *
106 * @return The size in bytes of the data type
107 */
108inline size_t data_size_from_type(DataType data_type)
109{
110 switch(data_type)
111 {
112 case DataType::U8:
113 case DataType::S8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100114 case DataType::QSYMM8:
Michel Iwaniec00633802017-10-12 14:14:15 +0100115 case DataType::QASYMM8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100116 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 return 1;
118 case DataType::U16:
119 case DataType::S16:
120 case DataType::F16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 return 2;
122 case DataType::F32:
123 case DataType::U32:
124 case DataType::S32:
125 return 4;
126 case DataType::F64:
127 case DataType::U64:
128 case DataType::S64:
129 return 8;
130 case DataType::SIZET:
131 return sizeof(size_t);
132 default:
133 ARM_COMPUTE_ERROR("Invalid data type");
134 return 0;
135 }
136}
137
138/** The size in bytes of the pixel format
139 *
140 * @param[in] format Input format
141 *
142 * @return The size in bytes of the pixel format
143 */
144inline size_t pixel_size_from_format(Format format)
145{
146 switch(format)
147 {
148 case Format::U8:
149 return 1;
150 case Format::U16:
151 case Format::S16:
152 case Format::F16:
153 case Format::UV88:
154 case Format::YUYV422:
155 case Format::UYVY422:
156 return 2;
157 case Format::RGB888:
158 return 3;
159 case Format::RGBA8888:
160 return 4;
161 case Format::U32:
162 case Format::S32:
163 case Format::F32:
164 return 4;
165 //Doesn't make sense for planar formats:
166 case Format::NV12:
167 case Format::NV21:
168 case Format::IYUV:
169 case Format::YUV444:
170 default:
171 ARM_COMPUTE_ERROR("Undefined pixel size for given format");
172 return 0;
173 }
174}
175
176/** The size in bytes of the data type
177 *
178 * @param[in] dt Input data type
179 *
180 * @return The size in bytes of the data type
181 */
182inline size_t element_size_from_data_type(DataType dt)
183{
184 switch(dt)
185 {
186 case DataType::S8:
187 case DataType::U8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100188 case DataType::QSYMM8:
Michel Iwaniec00633802017-10-12 14:14:15 +0100189 case DataType::QASYMM8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100190 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 return 1;
192 case DataType::U16:
193 case DataType::S16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 case DataType::F16:
195 return 2;
196 case DataType::U32:
197 case DataType::S32:
198 case DataType::F32:
199 return 4;
200 default:
201 ARM_COMPUTE_ERROR("Undefined element size for given data type");
202 return 0;
203 }
204}
205
206/** Return the data type used by a given single-planar pixel format
207 *
208 * @param[in] format Input format
209 *
210 * @return The size in bytes of the pixel format
211 */
212inline DataType data_type_from_format(Format format)
213{
214 switch(format)
215 {
216 case Format::U8:
217 case Format::UV88:
218 case Format::RGB888:
219 case Format::RGBA8888:
220 case Format::YUYV422:
221 case Format::UYVY422:
222 return DataType::U8;
223 case Format::U16:
224 return DataType::U16;
225 case Format::S16:
226 return DataType::S16;
227 case Format::U32:
228 return DataType::U32;
229 case Format::S32:
230 return DataType::S32;
231 case Format::F16:
232 return DataType::F16;
233 case Format::F32:
234 return DataType::F32;
235 //Doesn't make sense for planar formats:
236 case Format::NV12:
237 case Format::NV21:
238 case Format::IYUV:
239 case Format::YUV444:
240 default:
241 ARM_COMPUTE_ERROR("Not supported data_type for given format");
242 return DataType::UNKNOWN;
243 }
244}
245
246/** Return the plane index of a given channel given an input format.
247 *
248 * @param[in] format Input format
249 * @param[in] channel Input channel
250 *
251 * @return The plane index of the specific channel of the specific format
252 */
253inline int plane_idx_from_channel(Format format, Channel channel)
254{
255 switch(format)
256 {
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100257 // Single planar formats have a single plane
258 case Format::U8:
259 case Format::U16:
260 case Format::S16:
261 case Format::U32:
262 case Format::S32:
263 case Format::F16:
264 case Format::F32:
265 case Format::UV88:
266 case Format::RGB888:
267 case Format::RGBA8888:
268 case Format::YUYV422:
269 case Format::UYVY422:
270 return 0;
271 // Multi planar formats
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 case Format::NV12:
273 case Format::NV21:
274 {
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100275 // Channel U and V share the same plane of format UV88
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100276 switch(channel)
277 {
278 case Channel::Y:
279 return 0;
280 case Channel::U:
281 case Channel::V:
282 return 1;
283 default:
284 ARM_COMPUTE_ERROR("Not supported channel");
285 return 0;
286 }
287 }
288 case Format::IYUV:
289 case Format::YUV444:
290 {
291 switch(channel)
292 {
293 case Channel::Y:
294 return 0;
295 case Channel::U:
296 return 1;
297 case Channel::V:
298 return 2;
299 default:
300 ARM_COMPUTE_ERROR("Not supported channel");
301 return 0;
302 }
303 }
304 default:
305 ARM_COMPUTE_ERROR("Not supported format");
306 return 0;
307 }
308}
309
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100310/** Return the channel index of a given channel given an input format.
311 *
312 * @param[in] format Input format
313 * @param[in] channel Input channel
314 *
315 * @return The channel index of the specific channel of the specific format
316 */
317inline int channel_idx_from_format(Format format, Channel channel)
318{
319 switch(format)
320 {
321 case Format::RGB888:
322 {
323 switch(channel)
324 {
325 case Channel::R:
326 return 0;
327 case Channel::G:
328 return 1;
329 case Channel::B:
330 return 2;
331 default:
332 ARM_COMPUTE_ERROR("Not supported channel");
333 return 0;
334 }
335 }
336 case Format::RGBA8888:
337 {
338 switch(channel)
339 {
340 case Channel::R:
341 return 0;
342 case Channel::G:
343 return 1;
344 case Channel::B:
345 return 2;
346 case Channel::A:
347 return 3;
348 default:
349 ARM_COMPUTE_ERROR("Not supported channel");
350 return 0;
351 }
352 }
353 case Format::YUYV422:
354 {
355 switch(channel)
356 {
357 case Channel::Y:
358 return 0;
359 case Channel::U:
360 return 1;
361 case Channel::V:
362 return 3;
363 default:
364 ARM_COMPUTE_ERROR("Not supported channel");
365 return 0;
366 }
367 }
368 case Format::UYVY422:
369 {
370 switch(channel)
371 {
372 case Channel::Y:
373 return 1;
374 case Channel::U:
375 return 0;
376 case Channel::V:
377 return 2;
378 default:
379 ARM_COMPUTE_ERROR("Not supported channel");
380 return 0;
381 }
382 }
383 case Format::NV12:
384 {
385 switch(channel)
386 {
387 case Channel::Y:
388 return 0;
389 case Channel::U:
390 return 0;
391 case Channel::V:
392 return 1;
393 default:
394 ARM_COMPUTE_ERROR("Not supported channel");
395 return 0;
396 }
397 }
398 case Format::NV21:
399 {
400 switch(channel)
401 {
402 case Channel::Y:
403 return 0;
404 case Channel::U:
405 return 1;
406 case Channel::V:
407 return 0;
408 default:
409 ARM_COMPUTE_ERROR("Not supported channel");
410 return 0;
411 }
412 }
413 case Format::YUV444:
414 case Format::IYUV:
415 {
416 switch(channel)
417 {
418 case Channel::Y:
419 return 0;
420 case Channel::U:
421 return 0;
422 case Channel::V:
423 return 0;
424 default:
425 ARM_COMPUTE_ERROR("Not supported channel");
426 return 0;
427 }
428 }
429 default:
430 ARM_COMPUTE_ERROR("Not supported format");
431 return 0;
432 }
433}
434
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100435/** Return the number of planes for a given format
436 *
437 * @param[in] format Input format
438 *
439 * @return The number of planes for a given image format.
440 */
441inline size_t num_planes_from_format(Format format)
442{
443 switch(format)
444 {
445 case Format::U8:
446 case Format::S16:
447 case Format::U16:
448 case Format::S32:
449 case Format::U32:
450 case Format::F16:
451 case Format::F32:
452 case Format::RGB888:
453 case Format::RGBA8888:
454 case Format::YUYV422:
455 case Format::UYVY422:
456 return 1;
457 case Format::NV12:
458 case Format::NV21:
459 return 2;
460 case Format::IYUV:
461 case Format::YUV444:
462 return 3;
463 default:
464 ARM_COMPUTE_ERROR("Not supported format");
465 return 0;
466 }
467}
468
469/** Return the number of channels for a given single-planar pixel format
470 *
471 * @param[in] format Input format
472 *
473 * @return The number of channels for a given image format.
474 */
475inline size_t num_channels_from_format(Format format)
476{
477 switch(format)
478 {
479 case Format::U8:
480 case Format::U16:
481 case Format::S16:
482 case Format::U32:
483 case Format::S32:
484 case Format::F16:
485 case Format::F32:
486 return 1;
487 // Because the U and V channels are subsampled
488 // these formats appear like having only 2 channels:
489 case Format::YUYV422:
490 case Format::UYVY422:
491 return 2;
492 case Format::UV88:
493 return 2;
494 case Format::RGB888:
495 return 3;
496 case Format::RGBA8888:
497 return 4;
498 //Doesn't make sense for planar formats:
499 case Format::NV12:
500 case Format::NV21:
501 case Format::IYUV:
502 case Format::YUV444:
503 default:
504 return 0;
505 }
506}
507
Chunosovd621bca2017-11-03 17:33:15 +0700508/** Return the promoted data type of a given data type.
509 *
510 * @note If promoted data type is not supported an error will be thrown
511 *
512 * @param[in] dt Data type to get the promoted type of.
513 *
514 * @return Promoted data type
515 */
516inline DataType get_promoted_data_type(DataType dt)
517{
518 switch(dt)
519 {
520 case DataType::U8:
521 return DataType::U16;
522 case DataType::S8:
523 return DataType::S16;
Chunosovd621bca2017-11-03 17:33:15 +0700524 case DataType::U16:
525 return DataType::U32;
526 case DataType::S16:
527 return DataType::S32;
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100528 case DataType::QSYMM8:
Chunosovd621bca2017-11-03 17:33:15 +0700529 case DataType::QASYMM8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100530 case DataType::QSYMM8_PER_CHANNEL:
Chunosovd621bca2017-11-03 17:33:15 +0700531 case DataType::F16:
532 case DataType::U32:
533 case DataType::S32:
534 case DataType::F32:
Chunosovd621bca2017-11-03 17:33:15 +0700535 ARM_COMPUTE_ERROR("Unsupported data type promotions!");
536 default:
537 ARM_COMPUTE_ERROR("Undefined data type!");
538 }
539 return DataType::UNKNOWN;
540}
541
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100542/** Return true if the given format has horizontal subsampling.
543 *
544 * @param[in] format Format to determine subsampling.
545 *
546 * @return True if the format can be subsampled horizontaly.
547 */
548inline bool has_format_horizontal_subsampling(Format format)
549{
550 return (format == Format::YUYV422 || format == Format::UYVY422 || format == Format::NV12 || format == Format::NV21 || format == Format::IYUV || format == Format::UV88) ? true : false;
551}
552
553/** Return true if the given format has vertical subsampling.
554 *
555 * @param[in] format Format to determine subsampling.
556 *
557 * @return True if the format can be subsampled verticaly.
558 */
559inline bool has_format_vertical_subsampling(Format format)
560{
561 return (format == Format::NV12 || format == Format::NV21 || format == Format::IYUV || format == Format::UV88) ? true : false;
562}
563
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100564/** Separate a 2D convolution into two 1D convolutions
Anthony Barbierf202e502017-11-23 18:02:04 +0000565 *
566 * @param[in] conv 2D convolution
567 * @param[out] conv_col 1D vertical convolution
568 * @param[out] conv_row 1D horizontal convolution
569 * @param[in] size Size of the 2D convolution
570 *
571 * @return true if the separation was successful
572 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100573inline bool separate_matrix(const int16_t *conv, int16_t *conv_col, int16_t *conv_row, uint8_t size)
574{
575 int32_t min_col = -1;
576 int16_t min_col_val = -1;
577
578 for(int32_t i = 0; i < size; ++i)
579 {
580 if(conv[i] != 0 && (min_col < 0 || abs(min_col_val) > abs(conv[i])))
581 {
582 min_col = i;
583 min_col_val = conv[i];
584 }
585 }
586
587 if(min_col < 0)
588 {
589 return false;
590 }
591
592 for(uint32_t j = 0; j < size; ++j)
593 {
594 conv_col[j] = conv[min_col + j * size];
595 }
596
597 for(uint32_t i = 0; i < size; i++)
598 {
599 if(static_cast<int>(i) == min_col)
600 {
601 conv_row[i] = 1;
602 }
603 else
604 {
605 int16_t coeff = conv[i] / conv[min_col];
606
607 for(uint32_t j = 1; j < size; ++j)
608 {
609 if(conv[i + j * size] != (conv_col[j] * coeff))
610 {
611 return false;
612 }
613 }
614
615 conv_row[i] = coeff;
616 }
617 }
618
619 return true;
620}
621
622/** Calculate the scale of the given square matrix
623 *
624 * The scale is the absolute value of the sum of all the coefficients in the matrix.
625 *
626 * @note If the coefficients add up to 0 then the scale is set to 1.
627 *
628 * @param[in] matrix Matrix coefficients
629 * @param[in] matrix_size Number of elements per side of the square matrix. (Number of coefficients = matrix_size * matrix_size).
630 *
631 * @return The absolute value of the sum of the coefficients if they don't add up to 0, otherwise 1.
632 */
633inline uint32_t calculate_matrix_scale(const int16_t *matrix, unsigned int matrix_size)
634{
635 const size_t size = matrix_size * matrix_size;
636
637 return std::max(1, std::abs(std::accumulate(matrix, matrix + size, 0)));
638}
639
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100640/** Adjust tensor shape size if width or height are odd for a given multi-planar format. No modification is done for other formats.
641 *
642 * @note Adding here a few links discussing the issue of odd size and sharing the same solution:
Manuel Bottini581c8982019-02-07 10:31:57 +0000643 * <a href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/graphics/java/android/graphics/YuvImage.java">Android Source</a>
644 * <a href="https://groups.google.com/a/webmproject.org/forum/#!topic/webm-discuss/LaCKpqiDTXM">WebM</a>
645 * <a href="https://bugs.chromium.org/p/libyuv/issues/detail?id=198&amp;can=1&amp;q=odd%20width">libYUV</a>
646 * <a href="https://sourceforge.net/p/raw-yuvplayer/bugs/1/">YUVPlayer</a> *
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100647 *
648 * @param[in, out] shape Tensor shape of 2D size
649 * @param[in] format Format of the tensor
650 *
Alex Gildayc357c472018-03-21 13:54:09 +0000651 * @return The adjusted tensor shape.
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100652 */
653inline TensorShape adjust_odd_shape(const TensorShape &shape, Format format)
654{
655 TensorShape output{ shape };
656
657 // Force width to be even for formats which require subsampling of the U and V channels
658 if(has_format_horizontal_subsampling(format))
659 {
660 output.set(0, output.x() & ~1U);
661 }
662
663 // Force height to be even for formats which require subsampling of the U and V channels
664 if(has_format_vertical_subsampling(format))
665 {
666 output.set(1, output.y() & ~1U);
667 }
668
669 return output;
670}
671
672/** Calculate subsampled shape for a given format and channel
673 *
674 * @param[in] shape Shape of the tensor to calculate the extracted channel.
675 * @param[in] format Format of the tensor.
676 * @param[in] channel Channel to create tensor shape to be extracted.
677 *
678 * @return The subsampled tensor shape.
679 */
680inline TensorShape calculate_subsampled_shape(const TensorShape &shape, Format format, Channel channel = Channel::UNKNOWN)
681{
682 TensorShape output{ shape };
683
684 // Subsample shape only for U or V channel
685 if(Channel::U == channel || Channel::V == channel || Channel::UNKNOWN == channel)
686 {
687 // Subsample width for the tensor shape when channel is U or V
688 if(has_format_horizontal_subsampling(format))
689 {
690 output.set(0, output.x() / 2U);
691 }
692
693 // Subsample height for the tensor shape when channel is U or V
694 if(has_format_vertical_subsampling(format))
695 {
696 output.set(1, output.y() / 2U);
697 }
698 }
699
700 return output;
701}
702
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100703/** Calculate accurary required by the horizontal and vertical convolution computations
704 *
705 * @param[in] conv_col Pointer to the vertical vector of the separated convolution filter
706 * @param[in] conv_row Pointer to the horizontal vector of the convolution filter
707 * @param[in] size Number of elements per vector of the separated matrix
708 *
709 * @return The return type is a pair. The first element of the pair is the biggest data type needed for the first stage. The second
710 * element of the pair is the biggest data type needed for the second stage.
711 */
712inline std::pair<DataType, DataType> data_type_for_convolution(const int16_t *conv_col, const int16_t *conv_row, size_t size)
713{
714 DataType first_stage = DataType::UNKNOWN;
715 DataType second_stage = DataType::UNKNOWN;
716
717 auto gez = [](const int16_t &v)
718 {
719 return v >= 0;
720 };
721
722 auto accu_neg = [](const int &first, const int &second)
723 {
724 return first + (second < 0 ? second : 0);
725 };
726
727 auto accu_pos = [](const int &first, const int &second)
728 {
729 return first + (second > 0 ? second : 0);
730 };
731
732 const bool only_positive_coefficients = std::all_of(conv_row, conv_row + size, gez) && std::all_of(conv_col, conv_col + size, gez);
733
734 if(only_positive_coefficients)
735 {
736 const int max_row_value = std::accumulate(conv_row, conv_row + size, 0) * UINT8_MAX;
737 const int max_value = std::accumulate(conv_col, conv_col + size, 0) * max_row_value;
738
739 first_stage = (max_row_value <= UINT16_MAX) ? DataType::U16 : DataType::S32;
740
741 second_stage = (max_value <= UINT16_MAX) ? DataType::U16 : DataType::S32;
742 }
743 else
744 {
745 const int min_row_value = std::accumulate(conv_row, conv_row + size, 0, accu_neg) * UINT8_MAX;
746 const int max_row_value = std::accumulate(conv_row, conv_row + size, 0, accu_pos) * UINT8_MAX;
747 const int neg_coeffs_sum = std::accumulate(conv_col, conv_col + size, 0, accu_neg);
748 const int pos_coeffs_sum = std::accumulate(conv_col, conv_col + size, 0, accu_pos);
749 const int min_value = neg_coeffs_sum * max_row_value + pos_coeffs_sum * min_row_value;
750 const int max_value = neg_coeffs_sum * min_row_value + pos_coeffs_sum * max_row_value;
751
752 first_stage = ((INT16_MIN <= min_row_value) && (max_row_value <= INT16_MAX)) ? DataType::S16 : DataType::S32;
753
754 second_stage = ((INT16_MIN <= min_value) && (max_value <= INT16_MAX)) ? DataType::S16 : DataType::S32;
755 }
756
757 return std::make_pair(first_stage, second_stage);
758}
759
760/** Calculate the accuracy required by the squared convolution calculation.
761 *
762 *
763 * @param[in] conv Pointer to the squared convolution matrix
764 * @param[in] size The total size of the convolution matrix
765 *
766 * @return The return is the biggest data type needed to do the convolution
767 */
768inline DataType data_type_for_convolution_matrix(const int16_t *conv, size_t size)
769{
770 auto gez = [](const int16_t v)
771 {
772 return v >= 0;
773 };
774
775 const bool only_positive_coefficients = std::all_of(conv, conv + size, gez);
776
777 if(only_positive_coefficients)
778 {
779 const int max_conv_value = std::accumulate(conv, conv + size, 0) * UINT8_MAX;
780 if(max_conv_value <= UINT16_MAX)
781 {
782 return DataType::U16;
783 }
784 else
785 {
786 return DataType::S32;
787 }
788 }
789 else
790 {
791 const int min_value = std::accumulate(conv, conv + size, 0, [](int a, int b)
792 {
793 return b < 0 ? a + b : a;
794 })
795 * UINT8_MAX;
796
797 const int max_value = std::accumulate(conv, conv + size, 0, [](int a, int b)
798 {
799 return b > 0 ? a + b : a;
800 })
801 * UINT8_MAX;
802
803 if((INT16_MIN <= min_value) && (INT16_MAX >= max_value))
804 {
805 return DataType::S16;
806 }
807 else
808 {
809 return DataType::S32;
810 }
811 }
812}
813
Pablo Tello35767bc2018-12-05 17:36:30 +0000814/** Permutes the given dimensions according the permutation vector
815 *
816 * @param[in,out] dimensions Dimensions to be permuted.
817 * @param[in] perm Vector describing the permutation.
818 *
819 */
820template <typename T>
821inline void permute_strides(Dimensions<T> &dimensions, const PermutationVector &perm)
822{
823 const auto old_dim = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end());
824 for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
825 {
826 T dimension_val = old_dim[i];
827 dimensions.set(perm[i], dimension_val);
828 }
829}
830
Georgios Pinitas4074c992018-01-30 18:13:46 +0000831/** Calculate padding requirements in case of SAME padding
832 *
833 * @param[in] input_shape Input shape
834 * @param[in] weights_shape Weights shape
835 * @param[in] conv_info Convolution information (containing strides)
Isabella Gottardi6a914402019-01-30 15:45:42 +0000836 * @param[in] data_layout (Optional) Data layout of the input and weights tensor
Pablo Tello01bbacb2019-04-30 10:32:42 +0100837 * @param[in] dilation (Optional) Dilation factor used in the convolution.
Georgios Pinitas4074c992018-01-30 18:13:46 +0000838 *
839 * @return PadStrideInfo for SAME padding
840 */
Pablo Tello01bbacb2019-04-30 10:32:42 +0100841PadStrideInfo calculate_same_pad(TensorShape input_shape, TensorShape weights_shape, PadStrideInfo conv_info, DataLayout data_layout = DataLayout::NCHW, const Size2D &dilation = Size2D(1u, 1u));
Georgios Pinitas4074c992018-01-30 18:13:46 +0000842
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100843/** Returns expected width and height of the deconvolution's output tensor.
844 *
Michalis Spyrouafbc5ff2018-10-03 14:18:19 +0100845 * @param[in] in_width Width of input tensor (Number of columns)
846 * @param[in] in_height Height of input tensor (Number of rows)
847 * @param[in] kernel_width Kernel width.
848 * @param[in] kernel_height Kernel height.
849 * @param[in] padx X axis padding.
850 * @param[in] pady Y axis padding.
851 * @param[in] stride_x X axis input stride.
852 * @param[in] stride_y Y axis input stride.
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100853 *
854 * @return A pair with the new width in the first position and the new height in the second.
855 */
Pablo Tello01bbacb2019-04-30 10:32:42 +0100856std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height,
857 unsigned int kernel_width, unsigned int kernel_height,
858 unsigned int padx, unsigned int pady,
859 unsigned int stride_x, unsigned int stride_y);
Pablo Tellof5f34bb2017-08-22 13:34:13 +0100860
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100861/** Returns expected width and height of output scaled tensor depending on dimensions rounding mode.
862 *
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100863 * @param[in] width Width of input tensor (Number of columns)
864 * @param[in] height Height of input tensor (Number of rows)
865 * @param[in] kernel_width Kernel width.
866 * @param[in] kernel_height Kernel height.
867 * @param[in] pad_stride_info Pad and stride information.
Alex Gilday7da29b62018-03-23 14:16:00 +0000868 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100869 *
870 * @return A pair with the new width in the first position and the new height in the second.
871 */
Pablo Tello01bbacb2019-04-30 10:32:42 +0100872std::pair<unsigned int, unsigned int> scaled_dimensions(unsigned int width, unsigned int height,
873 unsigned int kernel_width, unsigned int kernel_height,
874 const PadStrideInfo &pad_stride_info,
875 const Size2D &dilation = Size2D(1U, 1U));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100876
877/** Convert a tensor format into a string.
878 *
879 * @param[in] format @ref Format to be translated to string.
880 *
881 * @return The string describing the format.
882 */
883const std::string &string_from_format(Format format);
884
885/** Convert a channel identity into a string.
886 *
887 * @param[in] channel @ref Channel to be translated to string.
888 *
889 * @return The string describing the channel.
890 */
891const std::string &string_from_channel(Channel channel);
Michele Di Giorgiobf3c6622018-03-08 11:52:27 +0000892/** Convert a data layout identity into a string.
893 *
894 * @param[in] dl @ref DataLayout to be translated to string.
895 *
896 * @return The string describing the data layout.
897 */
898const std::string &string_from_data_layout(DataLayout dl);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100899/** Convert a data type identity into a string.
900 *
901 * @param[in] dt @ref DataType to be translated to string.
902 *
903 * @return The string describing the data type.
904 */
905const std::string &string_from_data_type(DataType dt);
906/** Convert a matrix pattern into a string.
907 *
908 * @param[in] pattern @ref MatrixPattern to be translated to string.
909 *
910 * @return The string describing the matrix pattern.
911 */
912const std::string &string_from_matrix_pattern(MatrixPattern pattern);
913/** Translates a given activation function to a string.
914 *
915 * @param[in] act @ref ActivationLayerInfo::ActivationFunction to be translated to string.
916 *
917 * @return The string describing the activation function.
918 */
919const std::string &string_from_activation_func(ActivationLayerInfo::ActivationFunction act);
920/** Translates a given non linear function to a string.
921 *
922 * @param[in] function @ref NonLinearFilterFunction to be translated to string.
923 *
924 * @return The string describing the non linear function.
925 */
926const std::string &string_from_non_linear_filter_function(NonLinearFilterFunction function);
927/** Translates a given interpolation policy to a string.
928 *
929 * @param[in] policy @ref InterpolationPolicy to be translated to string.
930 *
931 * @return The string describing the interpolation policy.
932 */
933const std::string &string_from_interpolation_policy(InterpolationPolicy policy);
934/** Translates a given border mode policy to a string.
935 *
936 * @param[in] border_mode @ref BorderMode to be translated to string.
937 *
938 * @return The string describing the border mode.
939 */
940const std::string &string_from_border_mode(BorderMode border_mode);
941/** Translates a given normalization type to a string.
942 *
943 * @param[in] type @ref NormType to be translated to string.
944 *
945 * @return The string describing the normalization type.
946 */
947const std::string &string_from_norm_type(NormType type);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100948/** Translates a given pooling type to a string.
949 *
950 * @param[in] type @ref PoolingType to be translated to string.
951 *
952 * @return The string describing the pooling type.
953 */
954const std::string &string_from_pooling_type(PoolingType type);
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100955/** Translates a given GEMMLowp output stage to a string.
956 *
957 * @param[in] output_stage @ref GEMMLowpOutputStageInfo to be translated to string.
958 *
959 * @return The string describing the GEMMLowp output stage
960 */
961const std::string &string_from_gemmlowp_output_stage(GEMMLowpOutputStageType output_stage);
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100962/** Convert a PixelValue to a string, represented through the specific data type
963 *
964 * @param[in] value The PixelValue to convert
965 * @param[in] data_type The type to be used to convert the @p value
966 *
967 * @return String representation of the PixelValue through the given data type.
968 */
969std::string string_from_pixel_value(const PixelValue &value, const DataType data_type);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100970/** Lower a given string.
971 *
972 * @param[in] val Given string to lower.
973 *
974 * @return The lowered string
975 */
976std::string lower_string(const std::string &val);
977
978/** Check if a given data type is of floating point type
979 *
980 * @param[in] dt Input data type.
981 *
982 * @return True if data type is of floating point type, else false.
983 */
984inline bool is_data_type_float(DataType dt)
985{
986 switch(dt)
987 {
988 case DataType::F16:
989 case DataType::F32:
990 return true;
991 default:
992 return false;
993 }
994}
995
Georgios Pinitas05078ec2017-11-02 13:06:59 +0000996/** Check if a given data type is of quantized type
997 *
998 * @note Quantized is considered a super-set of fixed-point and asymmetric data types.
999 *
1000 * @param[in] dt Input data type.
1001 *
1002 * @return True if data type is of quantized type, else false.
1003 */
1004inline bool is_data_type_quantized(DataType dt)
1005{
1006 switch(dt)
1007 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +01001008 case DataType::QSYMM8:
Georgios Pinitas05078ec2017-11-02 13:06:59 +00001009 case DataType::QASYMM8:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +01001010 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001011 return true;
1012 default:
1013 return false;
1014 }
1015}
1016
Georgios Pinitas05078ec2017-11-02 13:06:59 +00001017/** Check if a given data type is of asymmetric quantized type
1018 *
1019 * @param[in] dt Input data type.
1020 *
1021 * @return True if data type is of symmetric quantized type, else false.
1022 */
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +00001023inline bool is_data_type_quantized_asymmetric(DataType dt)
Georgios Pinitas05078ec2017-11-02 13:06:59 +00001024{
1025 switch(dt)
1026 {
1027 case DataType::QASYMM8:
1028 return true;
1029 default:
1030 return false;
1031 }
1032}
1033
Georgios Pinitas89010962017-08-04 14:58:27 +01001034/** Create a string with the float in full precision.
1035 *
1036 * @param val Floating point value
1037 *
1038 * @return String with the floating point value.
1039 */
1040inline std::string float_to_string_with_full_precision(float val)
1041{
1042 std::stringstream ss;
Georgios Pinitas7900a9e2018-11-23 11:44:58 +00001043 ss.precision(std::numeric_limits<float>::max_digits10);
Georgios Pinitas89010962017-08-04 14:58:27 +01001044 ss << val;
Giorgio Arena73023022018-09-04 14:55:55 +01001045
1046 if(val != static_cast<int>(val))
1047 {
1048 ss << "f";
1049 }
1050
Georgios Pinitas89010962017-08-04 14:58:27 +01001051 return ss.str();
1052}
1053
Michalis Spyrouf63885b2019-01-16 14:18:09 +00001054/** Returns the number of elements required to go from start to end with the wanted step
1055 *
1056 * @param[in] start start value
1057 * @param[in] end end value
1058 * @param[in] step step value between each number in the wanted sequence
1059 *
1060 * @return number of elements to go from start value to end value using the wanted step
1061 */
1062inline size_t num_of_elements_in_range(const float start, const float end, const float step)
1063{
1064 ARM_COMPUTE_ERROR_ON_MSG(step == 0, "Range Step cannot be 0");
1065 return size_t(std::ceil((end - start) / step));
1066}
1067
1068/** Returns true if the value can be represented by the given data type
1069 *
Georgios Pinitas4c5469b2019-05-21 13:32:43 +01001070 * @param[in] val value to be checked
1071 * @param[in] dt data type that is checked
1072 * @param[in] qinfo (Optional) quantization info if the data type is QASYMM8
Michalis Spyrouf63885b2019-01-16 14:18:09 +00001073 *
1074 * @return true if the data type can hold the value.
1075 */
1076template <typename T>
Georgios Pinitas4c5469b2019-05-21 13:32:43 +01001077bool check_value_range(T val, DataType dt, QuantizationInfo qinfo = QuantizationInfo())
Michalis Spyrouf63885b2019-01-16 14:18:09 +00001078{
1079 switch(dt)
1080 {
1081 case DataType::U8:
1082 return ((static_cast<uint8_t>(val) == val) && val >= std::numeric_limits<uint8_t>::lowest() && val <= std::numeric_limits<uint8_t>::max());
1083 case DataType::QASYMM8:
1084 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +01001085 double min = static_cast<double>(dequantize_qasymm8(0, qinfo));
1086 double max = static_cast<double>(dequantize_qasymm8(std::numeric_limits<uint8_t>::max(), qinfo));
Michalis Spyrouf63885b2019-01-16 14:18:09 +00001087 return ((double)val >= min && (double)val <= max);
1088 }
1089 case DataType::S8:
1090 return ((static_cast<int8_t>(val) == val) && val >= std::numeric_limits<int8_t>::lowest() && val <= std::numeric_limits<int8_t>::max());
1091 case DataType::U16:
1092 return ((static_cast<uint16_t>(val) == val) && val >= std::numeric_limits<uint16_t>::lowest() && val <= std::numeric_limits<uint16_t>::max());
1093 case DataType::S16:
1094 return ((static_cast<int16_t>(val) == val) && val >= std::numeric_limits<int16_t>::lowest() && val <= std::numeric_limits<int16_t>::max());
1095 case DataType::U32:
1096 return ((static_cast<uint32_t>(val) == val) && val >= std::numeric_limits<uint32_t>::lowest() && val <= std::numeric_limits<uint32_t>::max());
1097 case DataType::S32:
1098 return ((static_cast<int32_t>(val) == val) && val >= std::numeric_limits<int32_t>::lowest() && val <= std::numeric_limits<int32_t>::max());
1099 case DataType::U64:
1100 return (val >= std::numeric_limits<uint64_t>::lowest() && val <= std::numeric_limits<uint64_t>::max());
1101 case DataType::S64:
1102 return (val >= std::numeric_limits<int64_t>::lowest() && val <= std::numeric_limits<int64_t>::max());
1103 case DataType::F16:
1104 return (val >= std::numeric_limits<half>::lowest() && val <= std::numeric_limits<half>::max());
1105 case DataType::F32:
1106 return (val >= std::numeric_limits<float>::lowest() && val <= std::numeric_limits<float>::max());
1107 case DataType::F64:
1108 return (val >= std::numeric_limits<double>::lowest() && val <= std::numeric_limits<double>::max());
1109 case DataType::SIZET:
1110 return ((static_cast<size_t>(val) == val) && val >= std::numeric_limits<size_t>::lowest() && val <= std::numeric_limits<size_t>::max());
1111 default:
1112 ARM_COMPUTE_ERROR("Data type not supported");
1113 return false;
1114 }
1115}
1116
giuros01edc21e42018-11-16 14:45:31 +00001117#ifdef ARM_COMPUTE_ASSERTS_ENABLED
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001118/** Print consecutive elements to an output stream.
1119 *
1120 * @param[out] s Output stream to print the elements to.
1121 * @param[in] ptr Pointer to print the elements from.
1122 * @param[in] n Number of elements to print.
1123 * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0.
1124 * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter
1125 */
1126template <typename T>
1127void print_consecutive_elements_impl(std::ostream &s, const T *ptr, unsigned int n, int stream_width = 0, const std::string &element_delim = " ")
1128{
1129 using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type;
1130
1131 for(unsigned int i = 0; i < n; ++i)
1132 {
1133 // Set stream width as it is not a "sticky" stream manipulator
1134 if(stream_width != 0)
1135 {
1136 s.width(stream_width);
1137 }
Anthony Barbier7068f992017-10-26 15:23:08 +01001138
1139 if(std::is_same<typename std::decay<T>::type, half>::value)
1140 {
1141 // We use T instead of print_type here is because the std::is_floating_point<half> returns false and then the print_type becomes int.
1142 s << std::right << static_cast<T>(ptr[i]) << element_delim;
1143 }
1144 else
1145 {
1146 s << std::right << static_cast<print_type>(ptr[i]) << element_delim;
1147 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001148 }
1149}
1150
1151/** Identify the maximum width of n consecutive elements.
1152 *
1153 * @param[in] s The output stream which will be used to print the elements. Used to extract the stream format.
1154 * @param[in] ptr Pointer to the elements.
1155 * @param[in] n Number of elements.
1156 *
1157 * @return The maximum width of the elements.
1158 */
1159template <typename T>
1160int max_consecutive_elements_display_width_impl(std::ostream &s, const T *ptr, unsigned int n)
1161{
1162 using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type;
1163
1164 int max_width = -1;
1165 for(unsigned int i = 0; i < n; ++i)
1166 {
1167 std::stringstream ss;
1168 ss.copyfmt(s);
Anthony Barbier7068f992017-10-26 15:23:08 +01001169
1170 if(std::is_same<typename std::decay<T>::type, half>::value)
1171 {
1172 // We use T instead of print_type here is because the std::is_floating_point<half> returns false and then the print_type becomes int.
1173 ss << static_cast<T>(ptr[i]);
1174 }
1175 else
1176 {
1177 ss << static_cast<print_type>(ptr[i]);
1178 }
1179
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001180 max_width = std::max<int>(max_width, ss.str().size());
1181 }
1182 return max_width;
1183}
1184
1185/** Print consecutive elements to an output stream.
1186 *
1187 * @param[out] s Output stream to print the elements to.
1188 * @param[in] dt Data type of the elements
1189 * @param[in] ptr Pointer to print the elements from.
1190 * @param[in] n Number of elements to print.
1191 * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0.
1192 * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter
1193 */
1194void print_consecutive_elements(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n, int stream_width, const std::string &element_delim = " ");
1195
1196/** Identify the maximum width of n consecutive elements.
1197 *
1198 * @param[in] s Output stream to print the elements to.
1199 * @param[in] dt Data type of the elements
1200 * @param[in] ptr Pointer to print the elements from.
1201 * @param[in] n Number of elements to print.
1202 *
1203 * @return The maximum width of the elements.
1204 */
1205int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n);
giuros01edc21e42018-11-16 14:45:31 +00001206#endif /* ARM_COMPUTE_ASSERTS_ENABLED */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001207}
1208#endif /*__ARM_COMPUTE_UTILS_H__ */