blob: aa415acebe8e574c8587801e5b8d30ec5aff42f4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2016-2018 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_TYPES_H__
25#define __ARM_COMPUTE_TYPES_H__
26
27#include "arm_compute/core/Coordinates.h"
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000028#include "arm_compute/core/QAsymm8.h"
29#include "arm_compute/core/Rounding.h"
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000030#include "arm_compute/core/Strides.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/TensorShape.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010032#include "support/Half.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000034#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include <cstddef>
36#include <cstdint>
37#include <string>
38#include <utility>
39
40namespace arm_compute
41{
Georgios Pinitas583137c2017-08-31 18:12:42 +010042/** 16-bit floating point type */
43using half = half_float::half;
44
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000045/** Permutation vector */
46using PermutationVector = Strides;
47
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048/** Image colour formats */
49enum class Format
50{
Daniil Efremov02bf80d2017-11-22 00:26:51 +070051 UNKNOWN, /**< Unknown image format */
52 U8, /**< 1 channel, 1 U8 per channel */
53 S16, /**< 1 channel, 1 S16 per channel */
54 U16, /**< 1 channel, 1 U16 per channel */
55 S32, /**< 1 channel, 1 S32 per channel */
56 U32, /**< 1 channel, 1 U32 per channel */
57 F16, /**< 1 channel, 1 F16 per channel */
58 F32, /**< 1 channel, 1 F32 per channel */
59 UV88, /**< 2 channel, 1 U8 per channel */
60 RGB888, /**< 3 channels, 1 U8 per channel */
61 RGBA8888, /**< 4 channels, 1 U8 per channel */
62 YUV444, /**< A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes */
63 YUYV422, /**< A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes */
64 NV12, /**< A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling */
65 NV21, /**< A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling */
66 IYUV, /**< A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes */
67 UYVY422 /**< A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068};
69
70/** Available data types */
71enum class DataType
72{
73 UNKNOWN,
74 U8,
75 S8,
76 QS8,
Michel Iwaniec00633802017-10-12 14:14:15 +010077 QASYMM8,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 U16,
79 S16,
80 QS16,
81 U32,
82 S32,
Pablo Tellof87cc7f2017-07-26 10:28:40 +010083 QS32,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 U64,
85 S64,
86 F16,
87 F32,
88 F64,
89 SIZET
90};
91
Daniil Efremov02bf80d2017-11-22 00:26:51 +070092/** Available Sampling Policies */
93enum class SamplingPolicy
94{
95 CENTER, /**< Samples are taken at pixel center */
96 TOP_LEFT /**< Samples are taken at pixel top left corner */
97};
98
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099/** Constant value of the border pixels when using BorderMode::CONSTANT */
100constexpr uint8_t CONSTANT_BORDER_VALUE = 199;
101
102/* Constant value used to indicate a half-scale pyramid */
103constexpr float SCALE_PYRAMID_HALF = 0.5f;
104
105/* Constant value used to indicate a ORB scaled pyramid */
106constexpr float SCALE_PYRAMID_ORB = 8.408964152537146130583778358414e-01;
107
Michel Iwaniec00633802017-10-12 14:14:15 +0100108/** Quantization settings (used for QASYMM8 data type) */
109struct QuantizationInfo
110{
111 QuantizationInfo()
112 : scale(0.0f), offset(0)
113 {
114 }
115
116 QuantizationInfo(float scale, int offset)
117 : scale(scale), offset(offset)
118 {
119 }
120
Daniil Efremoveed841c2017-11-09 19:05:25 +0700121 bool operator==(const QuantizationInfo &other)
122 {
123 return scale == other.scale && offset == other.offset;
124 }
125
126 bool operator!=(const QuantizationInfo &other)
127 {
128 return !(*this == other);
129 }
130
Michel Iwaniec00633802017-10-12 14:14:15 +0100131 float scale; /**< scale */
132 int offset; /**< offset */
133
134 /** Quantizes a value using the scale/offset in this QuantizationInfo */
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000135 qasymm8_t quantize(float value, RoundingPolicy rounding_policy) const
Michel Iwaniec00633802017-10-12 14:14:15 +0100136 {
137 ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::quantize: scale == 0");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000138 return sqcvt_qasymm8_f32(value, scale, offset, rounding_policy);
Michel Iwaniec00633802017-10-12 14:14:15 +0100139 }
140
141 /** Dequantizes a value using the scale/offset in this QuantizationInfo */
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000142 float dequantize(qasymm8_t value) const
Michel Iwaniec00633802017-10-12 14:14:15 +0100143 {
144 ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::dequantize: scale == 0");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000145 return scvt_f32_qasymm8(value, scale, offset);
Michel Iwaniec00633802017-10-12 14:14:15 +0100146 }
147
148 /** Indicates whether this QuantizationInfo has valid settings or not */
149 bool empty() const
150 {
151 return scale == 0;
152 }
153};
154
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155struct ValidRegion
156{
157 ValidRegion()
158 : anchor{}, shape{}
159 {
160 }
161
162 ValidRegion(const ValidRegion &) = default;
163 ValidRegion(ValidRegion &&) = default;
164 ValidRegion &operator=(const ValidRegion &) = default;
165 ValidRegion &operator=(ValidRegion &&) = default;
166 ~ValidRegion() = default;
167
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000168 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape)
169 : anchor{ an_anchor }, shape{ a_shape }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000171 anchor.set_num_dimensions(std::max(anchor.num_dimensions(), shape.num_dimensions()));
172 }
173
174 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape, size_t num_dimensions)
175 : anchor{ an_anchor }, shape{ a_shape }
176 {
177 ARM_COMPUTE_ERROR_ON(num_dimensions < std::max(anchor.num_dimensions(), shape.num_dimensions()));
178 anchor.set_num_dimensions(num_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 }
180
181 /** Return the start of the valid region for the given dimension @p d */
182 int start(unsigned int d) const
183 {
184 return anchor[d];
185 }
186
187 /** Return the end of the valid region for the given dimension @p d */
188 int end(unsigned int d) const
189 {
190 return anchor[d] + shape[d];
191 }
192
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000193 /** Accessor to set the value of anchor and shape for one of the dimensions.
194 *
195 * @param[in] dimension Dimension for which the value is set.
196 * @param[in] start Value to be set in anchor for the dimension.
197 * @param[in] size Value to be set in shape for the dimension.
198 *
199 * @return *this.
200 */
201 ValidRegion &set(size_t dimension, int start, size_t size)
202 {
203 anchor.set(dimension, start);
204 shape.set(dimension, size);
205 return *this;
206 }
207
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208 Coordinates anchor;
209 TensorShape shape;
210};
211
212/** Methods available to handle borders */
213enum class BorderMode
214{
215 UNDEFINED, /**< Borders are left undefined */
216 CONSTANT, /**< Pixels outside the image are assumed to have a constant value */
217 REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */
218};
219
220/** Container for 2D border size */
221struct BorderSize
222{
223 /** Empty border, i.e. no border */
224 constexpr BorderSize()
225 : top{ 0 }, right{ 0 }, bottom{ 0 }, left{ 0 }
226 {
227 }
228
229 /** Border with equal size around the 2D plane */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100230 explicit constexpr BorderSize(unsigned int size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231 : top{ size }, right{ size }, bottom{ size }, left{ size }
232 {
233 }
234
235 /** Border with same size for top/bottom and left/right */
236 constexpr BorderSize(unsigned int top_bottom, unsigned int left_right)
237 : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right }
238 {
239 }
240
241 /** Border with different sizes */
242 constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left)
243 : top{ top }, right{ right }, bottom{ bottom }, left{ left }
244 {
245 }
246
247 /** Check if the entire border is zero */
248 constexpr bool empty() const
249 {
250 return top == 0 && right == 0 && bottom == 0 && left == 0;
251 }
252
253 /** Check if the border is the same size on all sides */
254 constexpr bool uniform() const
255 {
256 return top == right && top == bottom && top == left;
257 }
258
259 BorderSize &operator*=(float scale)
260 {
261 top *= scale;
262 right *= scale;
263 bottom *= scale;
264 left *= scale;
265
266 return *this;
267 }
268
269 BorderSize operator*(float scale)
270 {
271 BorderSize size = *this;
272 size *= scale;
273
274 return size;
275 }
276
277 void limit(const BorderSize &limit)
278 {
279 top = std::min(top, limit.top);
280 right = std::min(right, limit.right);
281 bottom = std::min(bottom, limit.bottom);
282 left = std::min(left, limit.left);
283 }
284
285 unsigned int top;
286 unsigned int right;
287 unsigned int bottom;
288 unsigned int left;
289};
290
291using PaddingSize = BorderSize;
292
293/** Policy to handle overflow */
294enum class ConvertPolicy
295{
296 WRAP, /**< Wrap around */
297 SATURATE /**< Saturate */
298};
299
300/** Interpolation method */
301enum class InterpolationPolicy
302{
303 NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */
304 BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */
305 AREA, /**< Output values are determined by averaging the source pixels whose areas fall under the area of the destination pixel, projected onto the source image */
306};
307
308/** Bilinear Interpolation method used by LKTracker */
309enum class BilinearInterpolation
310{
311 BILINEAR_OLD_NEW,
312 BILINEAR_SCHARR
313};
314
315/** Threshold mode */
316enum class ThresholdType
317{
318 BINARY, /**< Threshold with one value */
319 RANGE /**< Threshold with two values*/
320};
321
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322/** Termination criteria */
323enum class Termination
324{
325 TERM_CRITERIA_EPSILON,
326 TERM_CRITERIA_ITERATIONS,
327 TERM_CRITERIA_BOTH
328};
329
330/** Magnitude calculation type. */
331enum class MagnitudeType
332{
333 L1NORM, /**< L1 normalization type */
334 L2NORM /**< L2 normalization type */
335};
336
337/** Phase calculation type.
338 *
339 * @note When PhaseType == SIGNED, each angle is mapped to the range 0 to 255 inclusive otherwise angles between 0 and 180
340 */
341enum class PhaseType
342{
343 SIGNED, /**< Angle range: [0, 360] */
344 UNSIGNED /**< Angle range: [0, 180] */
345};
346
347/** Keypoint type */
348struct KeyPoint
349{
350 int32_t x{ 0 }; /**< X coordinates */
351 int32_t y{ 0 }; /**< Y coordinates */
352 float strength{ 0.f }; /**< Strength of the point */
353 float scale{ 0.f }; /**< Scale initialized to 0 by the corner detector */
354 float orientation{ 0.f }; /**< Orientation initialized to 0 by the corner detector */
355 int32_t tracking_status{ 0 }; /**< Status initialized to 1 by the corner detector, set to 0 when the point is lost */
356 float error{ 0.f }; /**< Tracking error initialized to 0 by the corner detector */
357};
358
359using InternalKeypoint = std::tuple<float, float, float>; /* x,y,strength */
360
361/** Rectangle type */
362struct Rectangle
363{
364 uint16_t x; /**< Top-left x coordinate */
365 uint16_t y; /**< Top-left y coordinate */
366 uint16_t width; /**< Width of the rectangle */
367 uint16_t height; /**< Height of the rectangle */
368};
369
370/** Coordinate type */
371struct Coordinates2D
372{
373 int32_t x; /**< X coordinates */
374 int32_t y; /**< Y coordinates */
375};
376
377/** Coordinate type */
378struct Coordinates3D
379{
380 uint32_t x; /**< X coordinates */
381 uint32_t y; /**< Y coordinates */
382 uint32_t z; /**< Z coordinates */
383};
384
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100385/** Region of interest */
386struct ROI
387{
388 Rectangle rect; /**< Rectangle specifying the region of interest */
389 uint16_t batch_idx; /**< The batch index of the region of interest */
390};
391
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392/** Available channels */
393enum class Channel
394{
395 UNKNOWN, /** Unknown channel format */
396 C0, /**< First channel (used by formats with unknown channel types). */
397 C1, /**< Second channel (used by formats with unknown channel types). */
398 C2, /**< Third channel (used by formats with unknown channel types). */
399 C3, /**< Fourth channel (used by formats with unknown channel types). */
400 R, /**< Red channel. */
401 G, /**< Green channel. */
402 B, /**< Blue channel. */
403 A, /**< Alpha channel. */
404 Y, /**< Luma channel. */
405 U, /**< Cb/U channel. */
406 V /**< Cr/V/Value channel. */
407};
408
409/** Available matrix patterns */
410enum class MatrixPattern
411{
412 BOX, /**< Box pattern matrix. */
413 CROSS, /**< Cross pattern matrix. */
414 DISK, /**< Disk pattern matrix. */
415 OTHER /**< Any other matrix pattern. */
416};
417
418/** Available non linear functions. */
419enum class NonLinearFilterFunction : unsigned
420{
421 MEDIAN = 0, /**< Non linear median filter. */
422 MIN = 1, /**< Non linear erode. */
423 MAX = 2, /**< Non linear dilate. */
424};
425
Georgios Pinitasd9769582017-08-03 10:19:40 +0100426/** Available reduction operations */
427enum class ReductionOperation
428{
429 SUM_SQUARE, /**< Sum of squares */
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100430 SUM, /**< Sum */
Georgios Pinitasd9769582017-08-03 10:19:40 +0100431};
432
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433/** The normalization type used for the normalization layer */
434enum class NormType
435{
436 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
437 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
438 CROSS_MAP /**< Normalization applied cross maps */
439};
440
441/** Normalization type for Histogram of Oriented Gradients (HOG) */
442enum class HOGNormType
443{
444 L2_NORM = 1, /**< L2-norm */
445 L2HYS_NORM = 2, /**< L2-norm followed by clipping */
446 L1_NORM = 3 /**< L1 norm */
447};
448
449/** Detection window used for the object detection. The detection window keeps the following information:
450 *
451 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
452 * -# Index of the class used for evaluating which class the detection window belongs to
453 * -# Confidence value (score) obtained with the classifier
454 */
455struct DetectionWindow
456{
457 uint16_t x{ 0 }; /**< Top-left x coordinate */
458 uint16_t y{ 0 }; /**< Top-left y coordinate */
459 uint16_t width{ 0 }; /**< Width of the detection window */
460 uint16_t height{ 0 }; /**< Height of the detection window */
461 uint16_t idx_class{ 0 }; /**< Index of the class */
462 float score{ 0.f }; /**< Confidence value for the detection window */
463};
464
465/** Dimension rounding type when down-scaling on CNNs
466 * @note Used in pooling and convolution layer
467 */
468enum class DimensionRoundingType
469{
470 FLOOR, /**< Floor rounding */
471 CEIL /**< Ceil rounding */
472};
473
474/** Available pooling types */
475enum class PoolingType
476{
477 MAX, /**< Max Pooling */
Georgios Pinitascdf51452017-08-31 14:21:36 +0100478 AVG, /**< Average Pooling */
479 L2 /**< L2 Pooling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100480};
481
482/** Padding and stride information class */
483class PadStrideInfo
484{
485public:
486 /** Constructor
487 *
488 * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1.
489 * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1.
490 * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0.
491 * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0.
492 * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR.
493 */
494 PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1,
495 unsigned int pad_x = 0, unsigned int pad_y = 0,
496 DimensionRoundingType round = DimensionRoundingType::FLOOR)
497 : _stride(std::make_pair(stride_x, stride_y)),
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100498 _pad_left(pad_x),
499 _pad_top(pad_y),
500 _pad_right(pad_x),
501 _pad_bottom(pad_y),
502 _round_type(round)
503 {
504 }
505 /** Constructor
506 *
507 * @param[in] stride_x Stride, in elements, across x.
508 * @param[in] stride_y Stride, in elements, across y.
509 * @param[in] pad_left Padding across x on the left, in elements.
510 * @param[in] pad_top Padding across y on the top, in elements.
511 * @param[in] pad_right Padding across x on the right, in elements.
512 * @param[in] pad_bottom Padding across y on the bottom, in elements.
513 * @param[in] round Dimensions rounding.
514 */
515 PadStrideInfo(unsigned int stride_x, unsigned int stride_y,
516 unsigned int pad_left, unsigned int pad_right,
517 unsigned int pad_top, unsigned int pad_bottom,
518 DimensionRoundingType round)
519 : _stride(std::make_pair(stride_x, stride_y)),
520 _pad_left(pad_left),
521 _pad_top(pad_top),
522 _pad_right(pad_right),
523 _pad_bottom(pad_bottom),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100524 _round_type(round)
525 {
526 }
527 std::pair<unsigned int, unsigned int> stride() const
528 {
529 return _stride;
530 }
531 std::pair<unsigned int, unsigned int> pad() const
532 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100533 //this accessor should be used only when padding is symmetric
534 ARM_COMPUTE_ERROR_ON(_pad_left != _pad_right || _pad_top != _pad_bottom);
535 return std::make_pair(_pad_left, _pad_top);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100536 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100537
538 unsigned int pad_left() const
539 {
540 return _pad_left;
541 }
542 unsigned int pad_right() const
543 {
544 return _pad_right;
545 }
546 unsigned int pad_top() const
547 {
548 return _pad_top;
549 }
550 unsigned int pad_bottom() const
551 {
552 return _pad_bottom;
553 }
554
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100555 DimensionRoundingType round() const
556 {
557 return _round_type;
558 }
559
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100560 bool has_padding() const
561 {
562 return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0);
563 }
564
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100565private:
566 std::pair<unsigned int, unsigned int> _stride;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100567 unsigned int _pad_left;
568 unsigned int _pad_top;
569 unsigned int _pad_right;
570 unsigned int _pad_bottom;
571
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100572 DimensionRoundingType _round_type;
573};
574
575/** Pooling Layer Information class */
576class PoolingLayerInfo
577{
578public:
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000579 /** Default Constructor */
580 PoolingLayerInfo()
581 : _pool_type(PoolingType::MAX), _pool_size(0), _pad_stride_info(PadStrideInfo()), _exclude_padding(false), _is_global_pooling(false)
582 {
583 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100584 /** Default Constructor
585 *
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000586 * @param[in] pool_type Pooling type @ref PoolingType.
587 * @param[in] pool_size Pooling size, in elements, across x and y.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100588 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000589 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
590 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
591 * Defaults to false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100592 */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000593 explicit PoolingLayerInfo(PoolingType pool_type,
594 unsigned int pool_size,
595 PadStrideInfo pad_stride_info = PadStrideInfo(),
596 bool exclude_padding = false)
597 : _pool_type(pool_type), _pool_size(pool_size), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false)
598 {
599 }
600 /** Default Constructor
601 *
602 * @note This constructor is used for global pooling
603 *
604 * @param[in] pool_type Pooling type @ref PoolingType.
605 */
606 explicit PoolingLayerInfo(PoolingType pool_type)
607 : _pool_type(pool_type), _pool_size(0), _pad_stride_info(PadStrideInfo(1, 1, 0, 0)), _exclude_padding(false), _is_global_pooling(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100608 {
609 }
610 PoolingType pool_type() const
611 {
612 return _pool_type;
613 }
614 unsigned int pool_size() const
615 {
616 return _pool_size;
617 }
618 PadStrideInfo pad_stride_info() const
619 {
620 return _pad_stride_info;
621 }
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000622 bool exclude_padding() const
623 {
624 return _exclude_padding;
625 }
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000626 bool is_global_pooling() const
627 {
628 return _is_global_pooling;
629 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100630
631private:
632 PoolingType _pool_type;
633 unsigned int _pool_size;
634 PadStrideInfo _pad_stride_info;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000635 bool _exclude_padding;
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000636 bool _is_global_pooling;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100637};
638
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100639/** ROI Pooling Layer Information class */
640class ROIPoolingLayerInfo
641{
642public:
643 /** Default Constructor
644 *
645 * @param[in] pooled_width Pooled width of the layer.
646 * @param[in] pooled_height Pooled height of the layer.
647 * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions.
648 */
649 ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale)
650 : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale)
651 {
652 }
653 unsigned int pooled_width() const
654 {
655 return _pooled_width;
656 }
657 unsigned int pooled_height() const
658 {
659 return _pooled_height;
660 }
661 float spatial_scale() const
662 {
663 return _spatial_scale;
664 }
665
666private:
667 unsigned int _pooled_width;
668 unsigned int _pooled_height;
669 float _spatial_scale;
670};
671
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100672/** Activation Layer Information class */
673class ActivationLayerInfo
674{
675public:
676 /** Available activation functions */
677 enum class ActivationFunction
678 {
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100679 LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */
680 TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */
681 RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */
682 BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */
683 LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */
684 LEAKY_RELU, /**< Leaky Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
685 SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
686 ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */
687 SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/
688 SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/
689 LINEAR /**< Linear ( \f$ f(x)= ax + b \f$ ) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100690 };
691
692 /** Default Constructor
693 *
694 * @param[in] f The activation function to use.
695 * @param[in] a (Optional) The alpha parameter used by some activation functions
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100696 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
697 * @param[in] b (Optional) The beta parameter used by some activation functions (@ref ActivationFunction::LINEAR, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::TANH).
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100698 */
699 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f)
700 : _act(f), _a(a), _b(b)
701 {
702 }
703 ActivationFunction activation() const
704 {
705 return _act;
706 }
707 float a() const
708 {
709 return _a;
710 }
711 float b() const
712 {
713 return _b;
714 }
715
716private:
717 ActivationFunction _act;
718 float _a;
719 float _b;
720};
721
722/** Normalization Layer Information class */
723class NormalizationLayerInfo
724{
725public:
726 /** Default Constructor
727 *
728 * @param[in] type The normalization type. Can be @ref NormType::IN_MAP_1D, @ref NormType::IN_MAP_2D or @ref NORM_TYPE::CROSS_MAP
729 * @param[in] norm_size The normalization size is the number of elements to normalize across. Defaults to 5.
Georgios Pinitas41caa622017-11-16 14:37:08 +0000730 * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001.
731 * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5.
732 * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
733 * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not.
734 * Should be false to follow [Krichevksy 2012].
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100735 */
Georgios Pinitas41caa622017-11-16 14:37:08 +0000736 NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true)
737 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100738 {
739 }
740 NormType type() const
741 {
742 return _type;
743 }
744 uint32_t norm_size() const
745 {
746 return _norm_size;
747 }
748 float alpha() const
749 {
750 return _alpha;
751 }
752 float beta() const
753 {
754 return _beta;
755 }
756 float kappa() const
757 {
758 return _kappa;
759 }
Georgios Pinitas41caa622017-11-16 14:37:08 +0000760 bool is_cross_map() const
761 {
762 return _type == NormType::CROSS_MAP;
763 }
764 bool is_in_map() const
765 {
766 return !is_cross_map();
767 }
768 /** Return the scaling factor of the normalization function.
769 *
770 * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed,
771 * where alpha is returned plainly, else alpha is scaled by the total number of elements used for the normalization.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100772 *
773 * @return The normalization scaling factor.
774 */
775 float scale_coeff() const
776 {
777 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
Georgios Pinitas41caa622017-11-16 14:37:08 +0000778 return (_is_scaled) ? (_alpha / size) : _alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100779 }
780
781private:
782 NormType _type;
783 uint32_t _norm_size;
784 float _alpha;
785 float _beta;
786 float _kappa;
Georgios Pinitas41caa622017-11-16 14:37:08 +0000787 bool _is_scaled;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100788};
789
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100790/** Convolution Layer Weights Information class. This class stores the necessary information to compute convolution layer when the weights are already reshaped */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100791class WeightsInfo
792{
793public:
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100794 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100795 WeightsInfo()
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100796 : _are_reshaped(false), _kernel_width(0), _kernel_height(0), _num_kernels(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100797 {
798 }
799 /** Constructor
800 *
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100801 * @param[in] are_reshaped True if the weights have been reshaped
802 * @param[in] kernel_width Kernel width.
803 * @param[in] kernel_height Kernel height.
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100804 * @param[in] num_kernels Number of convolution kernels.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100805 */
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100806 WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels)
807 : _are_reshaped(are_reshaped), _kernel_width(kernel_width), _kernel_height(kernel_height), _num_kernels(num_kernels)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100808 {
809 }
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100810 /** Flag which specifies if the weights tensor has been reshaped.
811 *
812 * @return True if the weights tensors has been reshaped
813 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100814 bool are_reshaped() const
815 {
816 return _are_reshaped;
817 };
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100818 /** Return the number of convolution kernels
819 *
820 * @return The number of convolution kernels
821 */
822 unsigned int num_kernels() const
823 {
824 return _num_kernels;
825 };
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100826 /** Return the width and height of the kernel
827 *
828 * @return The width and height of the kernel
829 */
830 std::pair<unsigned int, unsigned int> kernel_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100831 {
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100832 return std::make_pair(_kernel_width, _kernel_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100833 }
834
835private:
836 const bool _are_reshaped;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100837 const unsigned int _kernel_width;
838 const unsigned int _kernel_height;
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100839 const unsigned int _num_kernels;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100840};
841
Gian Marco36a0a462018-01-12 10:21:40 +0000842/** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape.
843 *
844 * The matrix A can only be reshaped through @ref CLGEMMInterleave4x4Kernel or @ref NEGEMMInterleave4x4Kernel or @ref GCGEMMInterleave4x4Kernel
845 * Note: Optionally just for @ref CLGEMMInterleave4x4Kernel is it possible to set mult_interleave4x4_height, the multiplication factor for the height of the 4x4 interleaved block
846 *
847 * The matrix B can only be reshaped through @ref CLGEMMTranspose1xWKernel or @ref NEGEMMTranspose1xWKernel or @ref GCGEMMTranspose1xWKernel
848 * Note: Optionally just for @ref CLGEMMTranspose1xWKernel is it possible to set mult_transpose1xW_width, the multiplication factor for the width of the 1xW transposed block
849 *
850 */
851class GEMMReshapeInfo final
852{
853public:
854 /** Default constructor */
855 GEMMReshapeInfo()
856 : _m(1), _n(1), _k(1), _mult_transpose1xW_width(1), _mult_interleave4x4_height(1)
857 {
858 }
859 /** Constructor
860 *
861 * @param[in] m Number of matrix A rows
862 * @param[in] n Number of matrix B columns
863 * @param[in] k Number of matrix A columns or matrix B rows
864 * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block
865 * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block
866 */
867 GEMMReshapeInfo(int m, int n, int k, int mult_transpose1xW_width = 1, int mult_interleave4x4_height = 1)
868 : _m(m), _n(n), _k(k), _mult_transpose1xW_width(mult_transpose1xW_width), _mult_interleave4x4_height(mult_interleave4x4_height)
869 {
870 }
871 /** Number of matrix A rows
872 *
873 * @return the number of matrix A rows
874 */
875 int m() const
876 {
877 return _m;
878 }
879 /** Number of matrix B columns
880 *
881 * @return the number of matrix B columns
882 */
883 int n() const
884 {
885 return _n;
886 }
887 /** Number of matrix A columns or matrix B rows
888 *
889 * @return the number of matrix A columns or matrix B rows
890 */
891 int k() const
892 {
893 return _k;
894 }
895 /** Multiplication factor for the width of the 1xW transposed block
896 *
897 * @return the multiplication factor for the width of the 1xW transposed block
898 */
899 int mult_transpose1xW_width() const
900 {
901 return _mult_transpose1xW_width;
902 }
903 /** Multiplication factor for the height of the 4x4 interleaved block
904 *
905 * @return the multiplication factor for the height of the 4x4 interleaved block
906 */
907 int mult_interleave4x4_height() const
908 {
909 return _mult_interleave4x4_height;
910 }
911
912private:
913 const int _m;
914 const int _n;
915 const int _k;
916 const int _mult_transpose1xW_width;
917 const int _mult_interleave4x4_height;
918};
919
920/** GEMM information class. This class stores the necessary information to compute GEMM functions
921 *
922 * This object also contains the information about how matrix A and matrix B have been reshaped
923 *
924 */
Chunosov5124be52017-11-22 20:42:13 +0700925class GEMMInfo
926{
927public:
928 /** Default constructor */
929 GEMMInfo()
Gian Marco36a0a462018-01-12 10:21:40 +0000930 : _is_a_reshaped(false), _is_b_reshaped(false), _reshape_b_only_on_first_run(false), _reshape_info()
Chunosov5124be52017-11-22 20:42:13 +0700931 {
932 }
933 /** Constructor
934 *
935 * @param[in] is_a_reshaped True if the matrix A has been reshaped
936 * @param[in] is_b_reshaped True if the matrix B has been reshaped
937 * @param[in] reshape_b_only_on_first_run Reshape matrix B only for the first run
Gian Marco36a0a462018-01-12 10:21:40 +0000938 * @param[in] reshape_info (Optional) GEMM reshape information object
Chunosov5124be52017-11-22 20:42:13 +0700939 */
Gian Marco36a0a462018-01-12 10:21:40 +0000940 GEMMInfo(bool is_a_reshaped, bool is_b_reshaped, bool reshape_b_only_on_first_run, const GEMMReshapeInfo &reshape_info = GEMMReshapeInfo())
941 : _is_a_reshaped(is_a_reshaped), _is_b_reshaped(is_b_reshaped), _reshape_b_only_on_first_run(reshape_b_only_on_first_run), _reshape_info(reshape_info)
Chunosov5124be52017-11-22 20:42:13 +0700942 {
943 }
944 /** Flag which specifies if the matrix A has been reshaped
945 *
946 * @return True if the matrix A has been reshaped
947 */
948 bool is_a_reshaped() const
949 {
950 return _is_a_reshaped;
951 };
952 /** Flag which specifies if the matrix B has been reshaped
953 *
954 * @return True if the matrix B has been reshaped
955 */
956 bool is_b_reshaped() const
957 {
958 return _is_b_reshaped;
959 };
960 /** Flag which specifies if the reshape of matrix B should executed only for the first
961 *
962 * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer
963 *
964 * @return True if the reshaped of matrix B happens only for the first run
965 */
966 bool reshape_b_only_on_first_run() const
967 {
968 return _reshape_b_only_on_first_run;
969 };
Gian Marco36a0a462018-01-12 10:21:40 +0000970 /** GEMMReshapeInfo object which stores the necessary information to understand how the matrix A and matrix B have been reshaped
971 *
972 * @return the GEMMReshapeInfo object
973 */
974 const GEMMReshapeInfo &reshape_info() const
975 {
976 return _reshape_info;
977 }
Chunosov5124be52017-11-22 20:42:13 +0700978
979private:
Gian Marco36a0a462018-01-12 10:21:40 +0000980 const bool _is_a_reshaped;
981 const bool _is_b_reshaped;
982 const bool _reshape_b_only_on_first_run;
983 GEMMReshapeInfo _reshape_info;
Chunosov5124be52017-11-22 20:42:13 +0700984};
985
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100986/** IO formatting information class*/
987struct IOFormatInfo
988{
989 /** Precision type used when printing floating point numbers */
990 enum class PrecisionType
991 {
992 Default, /**< Default precision to the one that the current stream has */
993 Custom, /**< Custom precision specified by the user using the precision parameter */
994 Full /**< The maximum precision of the floating point representation */
995 };
996
997 /** Specifies the area to be printed, used by Tensor objects */
998 enum class PrintRegion
999 {
1000 ValidRegion, /**< Prints the valid region of the Tensor object */
1001 NoPadding, /**< Prints the Tensor object without the padding */
1002 Full /**< Print the tensor object including padding */
1003 };
1004
1005 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
1006 PrecisionType precision_type = PrecisionType::Default,
1007 unsigned int precision = 10,
1008 bool align_columns = true,
1009 std::string element_delim = " ",
1010 std::string row_delim = "\n")
1011 : print_region(print_region),
1012 precision_type(precision_type),
1013 precision(precision),
1014 element_delim(element_delim),
1015 row_delim(row_delim),
1016 align_columns(align_columns)
1017 {
1018 }
1019
1020 PrintRegion print_region;
1021 PrecisionType precision_type;
1022 unsigned int precision;
1023 std::string element_delim;
1024 std::string row_delim;
1025 bool align_columns;
1026};
1027}
1028#endif /* __ARM_COMPUTE_TYPES_H__ */