blob: 143ee025f5ca6fffb29c3b1447c6af7f69af1f1f [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"
Isabella Gottardi6e464c32018-01-26 12:32:45 +000030#include "arm_compute/core/Size2D.h"
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000031#include "arm_compute/core/Strides.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/TensorShape.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010033#include "support/Half.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000035#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include <cstddef>
37#include <cstdint>
38#include <string>
39#include <utility>
40
41namespace arm_compute
42{
Georgios Pinitas583137c2017-08-31 18:12:42 +010043/** 16-bit floating point type */
44using half = half_float::half;
45
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000046/** Permutation vector */
47using PermutationVector = Strides;
48
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049/** Image colour formats */
50enum class Format
51{
Daniil Efremov02bf80d2017-11-22 00:26:51 +070052 UNKNOWN, /**< Unknown image format */
53 U8, /**< 1 channel, 1 U8 per channel */
54 S16, /**< 1 channel, 1 S16 per channel */
55 U16, /**< 1 channel, 1 U16 per channel */
56 S32, /**< 1 channel, 1 S32 per channel */
57 U32, /**< 1 channel, 1 U32 per channel */
58 F16, /**< 1 channel, 1 F16 per channel */
59 F32, /**< 1 channel, 1 F32 per channel */
60 UV88, /**< 2 channel, 1 U8 per channel */
61 RGB888, /**< 3 channels, 1 U8 per channel */
62 RGBA8888, /**< 4 channels, 1 U8 per channel */
63 YUV444, /**< A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes */
64 YUYV422, /**< A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes */
65 NV12, /**< A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling */
66 NV21, /**< A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling */
67 IYUV, /**< A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes */
68 UYVY422 /**< A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069};
70
71/** Available data types */
72enum class DataType
73{
74 UNKNOWN,
75 U8,
76 S8,
77 QS8,
Michel Iwaniec00633802017-10-12 14:14:15 +010078 QASYMM8,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 U16,
80 S16,
81 QS16,
82 U32,
83 S32,
Pablo Tellof87cc7f2017-07-26 10:28:40 +010084 QS32,
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 U64,
86 S64,
87 F16,
88 F32,
89 F64,
90 SIZET
91};
92
Daniil Efremov02bf80d2017-11-22 00:26:51 +070093/** Available Sampling Policies */
94enum class SamplingPolicy
95{
96 CENTER, /**< Samples are taken at pixel center */
97 TOP_LEFT /**< Samples are taken at pixel top left corner */
98};
99
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100/** Constant value of the border pixels when using BorderMode::CONSTANT */
101constexpr uint8_t CONSTANT_BORDER_VALUE = 199;
102
103/* Constant value used to indicate a half-scale pyramid */
104constexpr float SCALE_PYRAMID_HALF = 0.5f;
105
106/* Constant value used to indicate a ORB scaled pyramid */
107constexpr float SCALE_PYRAMID_ORB = 8.408964152537146130583778358414e-01;
108
Georgios Pinitas4074c992018-01-30 18:13:46 +0000109/** Supported tensor data layouts */
110enum class DataLayout
111{
Isabella Gottardid17a6772018-02-27 17:41:55 +0000112 UNKNOWN,
Georgios Pinitas4074c992018-01-30 18:13:46 +0000113 NCHW,
114 NHWC
115};
116
Isabella Gottardid17a6772018-02-27 17:41:55 +0000117/** Supported tensor data layout dimensions */
118enum class DataLayoutDimension
119{
120 CHANNEL,
121 HEIGHT,
122 WIDTH,
123 BATCHES
124};
125
Michel Iwaniec00633802017-10-12 14:14:15 +0100126/** Quantization settings (used for QASYMM8 data type) */
127struct QuantizationInfo
128{
129 QuantizationInfo()
130 : scale(0.0f), offset(0)
131 {
132 }
133
134 QuantizationInfo(float scale, int offset)
135 : scale(scale), offset(offset)
136 {
137 }
138
Daniil Efremoveed841c2017-11-09 19:05:25 +0700139 bool operator==(const QuantizationInfo &other)
140 {
141 return scale == other.scale && offset == other.offset;
142 }
143
144 bool operator!=(const QuantizationInfo &other)
145 {
146 return !(*this == other);
147 }
148
Michel Iwaniec00633802017-10-12 14:14:15 +0100149 float scale; /**< scale */
150 int offset; /**< offset */
151
152 /** Quantizes a value using the scale/offset in this QuantizationInfo */
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000153 qasymm8_t quantize(float value, RoundingPolicy rounding_policy) const
Michel Iwaniec00633802017-10-12 14:14:15 +0100154 {
155 ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::quantize: scale == 0");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000156 return sqcvt_qasymm8_f32(value, scale, offset, rounding_policy);
Michel Iwaniec00633802017-10-12 14:14:15 +0100157 }
158
159 /** Dequantizes a value using the scale/offset in this QuantizationInfo */
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000160 float dequantize(qasymm8_t value) const
Michel Iwaniec00633802017-10-12 14:14:15 +0100161 {
162 ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::dequantize: scale == 0");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000163 return scvt_f32_qasymm8(value, scale, offset);
Michel Iwaniec00633802017-10-12 14:14:15 +0100164 }
165
166 /** Indicates whether this QuantizationInfo has valid settings or not */
167 bool empty() const
168 {
169 return scale == 0;
170 }
171};
172
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173struct ValidRegion
174{
175 ValidRegion()
176 : anchor{}, shape{}
177 {
178 }
179
180 ValidRegion(const ValidRegion &) = default;
181 ValidRegion(ValidRegion &&) = default;
182 ValidRegion &operator=(const ValidRegion &) = default;
183 ValidRegion &operator=(ValidRegion &&) = default;
184 ~ValidRegion() = default;
185
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000186 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape)
187 : anchor{ an_anchor }, shape{ a_shape }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000189 anchor.set_num_dimensions(std::max(anchor.num_dimensions(), shape.num_dimensions()));
190 }
191
192 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape, size_t num_dimensions)
193 : anchor{ an_anchor }, shape{ a_shape }
194 {
195 ARM_COMPUTE_ERROR_ON(num_dimensions < std::max(anchor.num_dimensions(), shape.num_dimensions()));
196 anchor.set_num_dimensions(num_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197 }
198
199 /** Return the start of the valid region for the given dimension @p d */
200 int start(unsigned int d) const
201 {
202 return anchor[d];
203 }
204
205 /** Return the end of the valid region for the given dimension @p d */
206 int end(unsigned int d) const
207 {
208 return anchor[d] + shape[d];
209 }
210
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000211 /** Accessor to set the value of anchor and shape for one of the dimensions.
212 *
213 * @param[in] dimension Dimension for which the value is set.
214 * @param[in] start Value to be set in anchor for the dimension.
215 * @param[in] size Value to be set in shape for the dimension.
216 *
217 * @return *this.
218 */
219 ValidRegion &set(size_t dimension, int start, size_t size)
220 {
221 anchor.set(dimension, start);
222 shape.set(dimension, size);
223 return *this;
224 }
225
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 Coordinates anchor;
227 TensorShape shape;
228};
229
230/** Methods available to handle borders */
231enum class BorderMode
232{
233 UNDEFINED, /**< Borders are left undefined */
234 CONSTANT, /**< Pixels outside the image are assumed to have a constant value */
235 REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */
236};
237
238/** Container for 2D border size */
239struct BorderSize
240{
241 /** Empty border, i.e. no border */
242 constexpr BorderSize()
243 : top{ 0 }, right{ 0 }, bottom{ 0 }, left{ 0 }
244 {
245 }
246
247 /** Border with equal size around the 2D plane */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100248 explicit constexpr BorderSize(unsigned int size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 : top{ size }, right{ size }, bottom{ size }, left{ size }
250 {
251 }
252
253 /** Border with same size for top/bottom and left/right */
254 constexpr BorderSize(unsigned int top_bottom, unsigned int left_right)
255 : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right }
256 {
257 }
258
259 /** Border with different sizes */
260 constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left)
261 : top{ top }, right{ right }, bottom{ bottom }, left{ left }
262 {
263 }
264
265 /** Check if the entire border is zero */
266 constexpr bool empty() const
267 {
268 return top == 0 && right == 0 && bottom == 0 && left == 0;
269 }
270
271 /** Check if the border is the same size on all sides */
272 constexpr bool uniform() const
273 {
274 return top == right && top == bottom && top == left;
275 }
276
277 BorderSize &operator*=(float scale)
278 {
279 top *= scale;
280 right *= scale;
281 bottom *= scale;
282 left *= scale;
283
284 return *this;
285 }
286
287 BorderSize operator*(float scale)
288 {
289 BorderSize size = *this;
290 size *= scale;
291
292 return size;
293 }
294
295 void limit(const BorderSize &limit)
296 {
297 top = std::min(top, limit.top);
298 right = std::min(right, limit.right);
299 bottom = std::min(bottom, limit.bottom);
300 left = std::min(left, limit.left);
301 }
302
303 unsigned int top;
304 unsigned int right;
305 unsigned int bottom;
306 unsigned int left;
307};
308
309using PaddingSize = BorderSize;
310
311/** Policy to handle overflow */
312enum class ConvertPolicy
313{
314 WRAP, /**< Wrap around */
315 SATURATE /**< Saturate */
316};
317
318/** Interpolation method */
319enum class InterpolationPolicy
320{
321 NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */
322 BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */
323 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 */
324};
325
326/** Bilinear Interpolation method used by LKTracker */
327enum class BilinearInterpolation
328{
329 BILINEAR_OLD_NEW,
330 BILINEAR_SCHARR
331};
332
333/** Threshold mode */
334enum class ThresholdType
335{
336 BINARY, /**< Threshold with one value */
337 RANGE /**< Threshold with two values*/
338};
339
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100340/** Termination criteria */
341enum class Termination
342{
343 TERM_CRITERIA_EPSILON,
344 TERM_CRITERIA_ITERATIONS,
345 TERM_CRITERIA_BOTH
346};
347
348/** Magnitude calculation type. */
349enum class MagnitudeType
350{
351 L1NORM, /**< L1 normalization type */
352 L2NORM /**< L2 normalization type */
353};
354
355/** Phase calculation type.
356 *
357 * @note When PhaseType == SIGNED, each angle is mapped to the range 0 to 255 inclusive otherwise angles between 0 and 180
358 */
359enum class PhaseType
360{
361 SIGNED, /**< Angle range: [0, 360] */
362 UNSIGNED /**< Angle range: [0, 180] */
363};
364
365/** Keypoint type */
366struct KeyPoint
367{
368 int32_t x{ 0 }; /**< X coordinates */
369 int32_t y{ 0 }; /**< Y coordinates */
370 float strength{ 0.f }; /**< Strength of the point */
371 float scale{ 0.f }; /**< Scale initialized to 0 by the corner detector */
372 float orientation{ 0.f }; /**< Orientation initialized to 0 by the corner detector */
373 int32_t tracking_status{ 0 }; /**< Status initialized to 1 by the corner detector, set to 0 when the point is lost */
374 float error{ 0.f }; /**< Tracking error initialized to 0 by the corner detector */
375};
376
377using InternalKeypoint = std::tuple<float, float, float>; /* x,y,strength */
378
379/** Rectangle type */
380struct Rectangle
381{
382 uint16_t x; /**< Top-left x coordinate */
383 uint16_t y; /**< Top-left y coordinate */
384 uint16_t width; /**< Width of the rectangle */
385 uint16_t height; /**< Height of the rectangle */
386};
387
388/** Coordinate type */
389struct Coordinates2D
390{
391 int32_t x; /**< X coordinates */
392 int32_t y; /**< Y coordinates */
393};
394
395/** Coordinate type */
396struct Coordinates3D
397{
398 uint32_t x; /**< X coordinates */
399 uint32_t y; /**< Y coordinates */
400 uint32_t z; /**< Z coordinates */
401};
402
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100403/** Region of interest */
404struct ROI
405{
406 Rectangle rect; /**< Rectangle specifying the region of interest */
407 uint16_t batch_idx; /**< The batch index of the region of interest */
408};
409
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100410/** Available channels */
411enum class Channel
412{
413 UNKNOWN, /** Unknown channel format */
414 C0, /**< First channel (used by formats with unknown channel types). */
415 C1, /**< Second channel (used by formats with unknown channel types). */
416 C2, /**< Third channel (used by formats with unknown channel types). */
417 C3, /**< Fourth channel (used by formats with unknown channel types). */
418 R, /**< Red channel. */
419 G, /**< Green channel. */
420 B, /**< Blue channel. */
421 A, /**< Alpha channel. */
422 Y, /**< Luma channel. */
423 U, /**< Cb/U channel. */
424 V /**< Cr/V/Value channel. */
425};
426
427/** Available matrix patterns */
428enum class MatrixPattern
429{
430 BOX, /**< Box pattern matrix. */
431 CROSS, /**< Cross pattern matrix. */
432 DISK, /**< Disk pattern matrix. */
433 OTHER /**< Any other matrix pattern. */
434};
435
436/** Available non linear functions. */
437enum class NonLinearFilterFunction : unsigned
438{
439 MEDIAN = 0, /**< Non linear median filter. */
440 MIN = 1, /**< Non linear erode. */
441 MAX = 2, /**< Non linear dilate. */
442};
443
Georgios Pinitasd9769582017-08-03 10:19:40 +0100444/** Available reduction operations */
445enum class ReductionOperation
446{
447 SUM_SQUARE, /**< Sum of squares */
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100448 SUM, /**< Sum */
Georgios Pinitasd9769582017-08-03 10:19:40 +0100449};
450
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100451/** The normalization type used for the normalization layer */
452enum class NormType
453{
454 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
455 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
456 CROSS_MAP /**< Normalization applied cross maps */
457};
458
459/** Normalization type for Histogram of Oriented Gradients (HOG) */
460enum class HOGNormType
461{
462 L2_NORM = 1, /**< L2-norm */
463 L2HYS_NORM = 2, /**< L2-norm followed by clipping */
464 L1_NORM = 3 /**< L1 norm */
465};
466
467/** Detection window used for the object detection. The detection window keeps the following information:
468 *
469 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
470 * -# Index of the class used for evaluating which class the detection window belongs to
471 * -# Confidence value (score) obtained with the classifier
472 */
473struct DetectionWindow
474{
475 uint16_t x{ 0 }; /**< Top-left x coordinate */
476 uint16_t y{ 0 }; /**< Top-left y coordinate */
477 uint16_t width{ 0 }; /**< Width of the detection window */
478 uint16_t height{ 0 }; /**< Height of the detection window */
479 uint16_t idx_class{ 0 }; /**< Index of the class */
480 float score{ 0.f }; /**< Confidence value for the detection window */
481};
482
483/** Dimension rounding type when down-scaling on CNNs
484 * @note Used in pooling and convolution layer
485 */
486enum class DimensionRoundingType
487{
488 FLOOR, /**< Floor rounding */
489 CEIL /**< Ceil rounding */
490};
491
492/** Available pooling types */
493enum class PoolingType
494{
495 MAX, /**< Max Pooling */
Georgios Pinitascdf51452017-08-31 14:21:36 +0100496 AVG, /**< Average Pooling */
497 L2 /**< L2 Pooling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100498};
499
500/** Padding and stride information class */
501class PadStrideInfo
502{
503public:
504 /** Constructor
505 *
506 * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1.
507 * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1.
508 * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0.
509 * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0.
510 * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR.
511 */
512 PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1,
513 unsigned int pad_x = 0, unsigned int pad_y = 0,
514 DimensionRoundingType round = DimensionRoundingType::FLOOR)
515 : _stride(std::make_pair(stride_x, stride_y)),
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100516 _pad_left(pad_x),
517 _pad_top(pad_y),
518 _pad_right(pad_x),
519 _pad_bottom(pad_y),
520 _round_type(round)
521 {
522 }
523 /** Constructor
524 *
525 * @param[in] stride_x Stride, in elements, across x.
526 * @param[in] stride_y Stride, in elements, across y.
527 * @param[in] pad_left Padding across x on the left, in elements.
528 * @param[in] pad_top Padding across y on the top, in elements.
529 * @param[in] pad_right Padding across x on the right, in elements.
530 * @param[in] pad_bottom Padding across y on the bottom, in elements.
531 * @param[in] round Dimensions rounding.
532 */
533 PadStrideInfo(unsigned int stride_x, unsigned int stride_y,
534 unsigned int pad_left, unsigned int pad_right,
535 unsigned int pad_top, unsigned int pad_bottom,
536 DimensionRoundingType round)
537 : _stride(std::make_pair(stride_x, stride_y)),
538 _pad_left(pad_left),
539 _pad_top(pad_top),
540 _pad_right(pad_right),
541 _pad_bottom(pad_bottom),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100542 _round_type(round)
543 {
544 }
545 std::pair<unsigned int, unsigned int> stride() const
546 {
547 return _stride;
548 }
Anthony Barbier21f67d62018-02-16 15:17:48 +0000549 bool padding_is_symmetric() const
550 {
551 return (_pad_left == _pad_right) && (_pad_top == _pad_bottom);
552 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100553 std::pair<unsigned int, unsigned int> pad() const
554 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100555 //this accessor should be used only when padding is symmetric
Anthony Barbier21f67d62018-02-16 15:17:48 +0000556 ARM_COMPUTE_ERROR_ON(!padding_is_symmetric());
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100557 return std::make_pair(_pad_left, _pad_top);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100558 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100559
560 unsigned int pad_left() const
561 {
562 return _pad_left;
563 }
564 unsigned int pad_right() const
565 {
566 return _pad_right;
567 }
568 unsigned int pad_top() const
569 {
570 return _pad_top;
571 }
572 unsigned int pad_bottom() const
573 {
574 return _pad_bottom;
575 }
576
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100577 DimensionRoundingType round() const
578 {
579 return _round_type;
580 }
581
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100582 bool has_padding() const
583 {
584 return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0);
585 }
586
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100587private:
588 std::pair<unsigned int, unsigned int> _stride;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100589 unsigned int _pad_left;
590 unsigned int _pad_top;
591 unsigned int _pad_right;
592 unsigned int _pad_bottom;
593
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100594 DimensionRoundingType _round_type;
595};
596
597/** Pooling Layer Information class */
598class PoolingLayerInfo
599{
600public:
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000601 /** Default Constructor */
602 PoolingLayerInfo()
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000603 : _pool_type(PoolingType::MAX), _pool_size(Size2D()), _pad_stride_info(PadStrideInfo()), _exclude_padding(false), _is_global_pooling(false)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000604 {
605 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100606 /** Default Constructor
607 *
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000608 * @param[in] pool_type Pooling type @ref PoolingType.
609 * @param[in] pool_size Pooling size, in elements, across x and y.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100610 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000611 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
612 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
613 * Defaults to false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100614 */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000615 explicit PoolingLayerInfo(PoolingType pool_type,
616 unsigned int pool_size,
617 PadStrideInfo pad_stride_info = PadStrideInfo(),
618 bool exclude_padding = false)
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000619 : _pool_type(pool_type), _pool_size(Size2D(pool_size, pool_size)), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false)
620 {
621 }
622 /** Default Constructor
623 *
624 * @param[in] pool_type Pooling type @ref PoolingType.
625 * @param[in] pool_size Pooling size, in elements, across x and y.
626 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
627 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
628 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
629 * Defaults to false;
630 */
631 explicit PoolingLayerInfo(PoolingType pool_type,
632 Size2D pool_size,
633 PadStrideInfo pad_stride_info = PadStrideInfo(),
634 bool exclude_padding = false)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000635 : _pool_type(pool_type), _pool_size(pool_size), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false)
636 {
637 }
638 /** Default Constructor
639 *
640 * @note This constructor is used for global pooling
641 *
642 * @param[in] pool_type Pooling type @ref PoolingType.
643 */
644 explicit PoolingLayerInfo(PoolingType pool_type)
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000645 : _pool_type(pool_type), _pool_size(Size2D()), _pad_stride_info(PadStrideInfo(1, 1, 0, 0)), _exclude_padding(false), _is_global_pooling(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100646 {
647 }
648 PoolingType pool_type() const
649 {
650 return _pool_type;
651 }
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000652 const Size2D &pool_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100653 {
654 return _pool_size;
655 }
656 PadStrideInfo pad_stride_info() const
657 {
658 return _pad_stride_info;
659 }
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000660 bool exclude_padding() const
661 {
662 return _exclude_padding;
663 }
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000664 bool is_global_pooling() const
665 {
666 return _is_global_pooling;
667 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100668
669private:
670 PoolingType _pool_type;
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000671 Size2D _pool_size;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100672 PadStrideInfo _pad_stride_info;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000673 bool _exclude_padding;
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000674 bool _is_global_pooling;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100675};
676
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100677/** ROI Pooling Layer Information class */
678class ROIPoolingLayerInfo
679{
680public:
681 /** Default Constructor
682 *
683 * @param[in] pooled_width Pooled width of the layer.
684 * @param[in] pooled_height Pooled height of the layer.
685 * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions.
686 */
687 ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale)
688 : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale)
689 {
690 }
691 unsigned int pooled_width() const
692 {
693 return _pooled_width;
694 }
695 unsigned int pooled_height() const
696 {
697 return _pooled_height;
698 }
699 float spatial_scale() const
700 {
701 return _spatial_scale;
702 }
703
704private:
705 unsigned int _pooled_width;
706 unsigned int _pooled_height;
707 float _spatial_scale;
708};
709
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100710/** Activation Layer Information class */
711class ActivationLayerInfo
712{
713public:
714 /** Available activation functions */
715 enum class ActivationFunction
716 {
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100717 LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */
718 TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */
719 RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */
720 BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */
721 LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */
722 LEAKY_RELU, /**< Leaky Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
723 SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
724 ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */
725 SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/
726 SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/
727 LINEAR /**< Linear ( \f$ f(x)= ax + b \f$ ) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100728 };
729
Giorgio Arena11674872018-02-07 15:38:12 +0000730 ActivationLayerInfo() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100731 /** Default Constructor
732 *
733 * @param[in] f The activation function to use.
734 * @param[in] a (Optional) The alpha parameter used by some activation functions
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100735 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
736 * @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 +0100737 */
738 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f)
Giorgio Arena11674872018-02-07 15:38:12 +0000739 : _act(f), _a(a), _b(b), _enabled(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100740 {
741 }
742 ActivationFunction activation() const
743 {
744 return _act;
745 }
746 float a() const
747 {
748 return _a;
749 }
750 float b() const
751 {
752 return _b;
753 }
Giorgio Arena11674872018-02-07 15:38:12 +0000754 bool enabled() const
755 {
756 return _enabled;
757 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100758
759private:
Giorgio Arena11674872018-02-07 15:38:12 +0000760 ActivationFunction _act = { ActivationLayerInfo::ActivationFunction::LOGISTIC };
761 float _a = {};
762 float _b = {};
763 bool _enabled = { false };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100764};
765
766/** Normalization Layer Information class */
767class NormalizationLayerInfo
768{
769public:
770 /** Default Constructor
771 *
772 * @param[in] type The normalization type. Can be @ref NormType::IN_MAP_1D, @ref NormType::IN_MAP_2D or @ref NORM_TYPE::CROSS_MAP
773 * @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 +0000774 * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001.
775 * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5.
776 * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
777 * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not.
778 * Should be false to follow [Krichevksy 2012].
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100779 */
Georgios Pinitas41caa622017-11-16 14:37:08 +0000780 NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true)
781 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100782 {
783 }
784 NormType type() const
785 {
786 return _type;
787 }
788 uint32_t norm_size() const
789 {
790 return _norm_size;
791 }
792 float alpha() const
793 {
794 return _alpha;
795 }
796 float beta() const
797 {
798 return _beta;
799 }
800 float kappa() const
801 {
802 return _kappa;
803 }
Georgios Pinitas41caa622017-11-16 14:37:08 +0000804 bool is_cross_map() const
805 {
806 return _type == NormType::CROSS_MAP;
807 }
808 bool is_in_map() const
809 {
810 return !is_cross_map();
811 }
812 /** Return the scaling factor of the normalization function.
813 *
814 * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed,
815 * 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 +0100816 *
817 * @return The normalization scaling factor.
818 */
819 float scale_coeff() const
820 {
821 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
Georgios Pinitas41caa622017-11-16 14:37:08 +0000822 return (_is_scaled) ? (_alpha / size) : _alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100823 }
824
825private:
826 NormType _type;
827 uint32_t _norm_size;
828 float _alpha;
829 float _beta;
830 float _kappa;
Georgios Pinitas41caa622017-11-16 14:37:08 +0000831 bool _is_scaled;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100832};
833
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100834/** 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 +0100835class WeightsInfo
836{
837public:
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100838 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100839 WeightsInfo()
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100840 : _are_reshaped(false), _kernel_width(0), _kernel_height(0), _num_kernels(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100841 {
842 }
843 /** Constructor
844 *
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100845 * @param[in] are_reshaped True if the weights have been reshaped
846 * @param[in] kernel_width Kernel width.
847 * @param[in] kernel_height Kernel height.
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100848 * @param[in] num_kernels Number of convolution kernels.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100849 */
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100850 WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels)
851 : _are_reshaped(are_reshaped), _kernel_width(kernel_width), _kernel_height(kernel_height), _num_kernels(num_kernels)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100852 {
853 }
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100854 /** Flag which specifies if the weights tensor has been reshaped.
855 *
856 * @return True if the weights tensors has been reshaped
857 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100858 bool are_reshaped() const
859 {
860 return _are_reshaped;
861 };
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100862 /** Return the number of convolution kernels
863 *
864 * @return The number of convolution kernels
865 */
866 unsigned int num_kernels() const
867 {
868 return _num_kernels;
869 };
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100870 /** Return the width and height of the kernel
871 *
872 * @return The width and height of the kernel
873 */
874 std::pair<unsigned int, unsigned int> kernel_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100875 {
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100876 return std::make_pair(_kernel_width, _kernel_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100877 }
878
879private:
880 const bool _are_reshaped;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100881 const unsigned int _kernel_width;
882 const unsigned int _kernel_height;
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100883 const unsigned int _num_kernels;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100884};
885
Gian Marco36a0a462018-01-12 10:21:40 +0000886/** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape.
887 *
888 * The matrix A can only be reshaped through @ref CLGEMMInterleave4x4Kernel or @ref NEGEMMInterleave4x4Kernel or @ref GCGEMMInterleave4x4Kernel
889 * 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
890 *
891 * The matrix B can only be reshaped through @ref CLGEMMTranspose1xWKernel or @ref NEGEMMTranspose1xWKernel or @ref GCGEMMTranspose1xWKernel
892 * 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
893 *
894 */
895class GEMMReshapeInfo final
896{
897public:
898 /** Default constructor */
899 GEMMReshapeInfo()
900 : _m(1), _n(1), _k(1), _mult_transpose1xW_width(1), _mult_interleave4x4_height(1)
901 {
902 }
903 /** Constructor
904 *
905 * @param[in] m Number of matrix A rows
906 * @param[in] n Number of matrix B columns
907 * @param[in] k Number of matrix A columns or matrix B rows
908 * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block
909 * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block
910 */
911 GEMMReshapeInfo(int m, int n, int k, int mult_transpose1xW_width = 1, int mult_interleave4x4_height = 1)
912 : _m(m), _n(n), _k(k), _mult_transpose1xW_width(mult_transpose1xW_width), _mult_interleave4x4_height(mult_interleave4x4_height)
913 {
914 }
915 /** Number of matrix A rows
916 *
917 * @return the number of matrix A rows
918 */
919 int m() const
920 {
921 return _m;
922 }
923 /** Number of matrix B columns
924 *
925 * @return the number of matrix B columns
926 */
927 int n() const
928 {
929 return _n;
930 }
931 /** Number of matrix A columns or matrix B rows
932 *
933 * @return the number of matrix A columns or matrix B rows
934 */
935 int k() const
936 {
937 return _k;
938 }
939 /** Multiplication factor for the width of the 1xW transposed block
940 *
941 * @return the multiplication factor for the width of the 1xW transposed block
942 */
943 int mult_transpose1xW_width() const
944 {
945 return _mult_transpose1xW_width;
946 }
947 /** Multiplication factor for the height of the 4x4 interleaved block
948 *
949 * @return the multiplication factor for the height of the 4x4 interleaved block
950 */
951 int mult_interleave4x4_height() const
952 {
953 return _mult_interleave4x4_height;
954 }
955
956private:
957 const int _m;
958 const int _n;
959 const int _k;
960 const int _mult_transpose1xW_width;
961 const int _mult_interleave4x4_height;
962};
963
964/** GEMM information class. This class stores the necessary information to compute GEMM functions
965 *
966 * This object also contains the information about how matrix A and matrix B have been reshaped
967 *
968 */
Chunosov5124be52017-11-22 20:42:13 +0700969class GEMMInfo
970{
971public:
972 /** Default constructor */
973 GEMMInfo()
Gian Marco36a0a462018-01-12 10:21:40 +0000974 : _is_a_reshaped(false), _is_b_reshaped(false), _reshape_b_only_on_first_run(false), _reshape_info()
Chunosov5124be52017-11-22 20:42:13 +0700975 {
976 }
977 /** Constructor
978 *
979 * @param[in] is_a_reshaped True if the matrix A has been reshaped
980 * @param[in] is_b_reshaped True if the matrix B has been reshaped
981 * @param[in] reshape_b_only_on_first_run Reshape matrix B only for the first run
Gian Marco36a0a462018-01-12 10:21:40 +0000982 * @param[in] reshape_info (Optional) GEMM reshape information object
Chunosov5124be52017-11-22 20:42:13 +0700983 */
Gian Marco36a0a462018-01-12 10:21:40 +0000984 GEMMInfo(bool is_a_reshaped, bool is_b_reshaped, bool reshape_b_only_on_first_run, const GEMMReshapeInfo &reshape_info = GEMMReshapeInfo())
985 : _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 +0700986 {
987 }
988 /** Flag which specifies if the matrix A has been reshaped
989 *
990 * @return True if the matrix A has been reshaped
991 */
992 bool is_a_reshaped() const
993 {
994 return _is_a_reshaped;
995 };
996 /** Flag which specifies if the matrix B has been reshaped
997 *
998 * @return True if the matrix B has been reshaped
999 */
1000 bool is_b_reshaped() const
1001 {
1002 return _is_b_reshaped;
1003 };
1004 /** Flag which specifies if the reshape of matrix B should executed only for the first
1005 *
1006 * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer
1007 *
1008 * @return True if the reshaped of matrix B happens only for the first run
1009 */
1010 bool reshape_b_only_on_first_run() const
1011 {
1012 return _reshape_b_only_on_first_run;
1013 };
Gian Marco36a0a462018-01-12 10:21:40 +00001014 /** GEMMReshapeInfo object which stores the necessary information to understand how the matrix A and matrix B have been reshaped
1015 *
1016 * @return the GEMMReshapeInfo object
1017 */
1018 const GEMMReshapeInfo &reshape_info() const
1019 {
1020 return _reshape_info;
1021 }
Chunosov5124be52017-11-22 20:42:13 +07001022
1023private:
Gian Marco36a0a462018-01-12 10:21:40 +00001024 const bool _is_a_reshaped;
1025 const bool _is_b_reshaped;
1026 const bool _reshape_b_only_on_first_run;
1027 GEMMReshapeInfo _reshape_info;
Chunosov5124be52017-11-22 20:42:13 +07001028};
1029
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001030/** IO formatting information class*/
1031struct IOFormatInfo
1032{
1033 /** Precision type used when printing floating point numbers */
1034 enum class PrecisionType
1035 {
1036 Default, /**< Default precision to the one that the current stream has */
1037 Custom, /**< Custom precision specified by the user using the precision parameter */
1038 Full /**< The maximum precision of the floating point representation */
1039 };
1040
1041 /** Specifies the area to be printed, used by Tensor objects */
1042 enum class PrintRegion
1043 {
1044 ValidRegion, /**< Prints the valid region of the Tensor object */
1045 NoPadding, /**< Prints the Tensor object without the padding */
1046 Full /**< Print the tensor object including padding */
1047 };
1048
1049 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
1050 PrecisionType precision_type = PrecisionType::Default,
1051 unsigned int precision = 10,
1052 bool align_columns = true,
1053 std::string element_delim = " ",
1054 std::string row_delim = "\n")
1055 : print_region(print_region),
1056 precision_type(precision_type),
1057 precision(precision),
1058 element_delim(element_delim),
1059 row_delim(row_delim),
1060 align_columns(align_columns)
1061 {
1062 }
1063
1064 PrintRegion print_region;
1065 PrecisionType precision_type;
1066 unsigned int precision;
1067 std::string element_delim;
1068 std::string row_delim;
1069 bool align_columns;
1070};
Isabella Gottardif07d28d2018-02-06 14:52:43 +00001071
1072/** Available ConvolutionMethod*/
1073enum class ConvolutionMethod
1074{
1075 GEMM, /**< Convolution using GEMM */
1076 DIRECT, /**< Direct convolution */
1077 WINOGRAD /**< Convolution using Winograd */
1078};
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001079}
1080#endif /* __ARM_COMPUTE_TYPES_H__ */