blob: 5a08ac9153d7ff5d84244ce985673b9259b0db4a [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{
112 NCHW,
113 NHWC
114};
115
Michel Iwaniec00633802017-10-12 14:14:15 +0100116/** Quantization settings (used for QASYMM8 data type) */
117struct QuantizationInfo
118{
119 QuantizationInfo()
120 : scale(0.0f), offset(0)
121 {
122 }
123
124 QuantizationInfo(float scale, int offset)
125 : scale(scale), offset(offset)
126 {
127 }
128
Daniil Efremoveed841c2017-11-09 19:05:25 +0700129 bool operator==(const QuantizationInfo &other)
130 {
131 return scale == other.scale && offset == other.offset;
132 }
133
134 bool operator!=(const QuantizationInfo &other)
135 {
136 return !(*this == other);
137 }
138
Michel Iwaniec00633802017-10-12 14:14:15 +0100139 float scale; /**< scale */
140 int offset; /**< offset */
141
142 /** Quantizes a value using the scale/offset in this QuantizationInfo */
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000143 qasymm8_t quantize(float value, RoundingPolicy rounding_policy) const
Michel Iwaniec00633802017-10-12 14:14:15 +0100144 {
145 ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::quantize: scale == 0");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000146 return sqcvt_qasymm8_f32(value, scale, offset, rounding_policy);
Michel Iwaniec00633802017-10-12 14:14:15 +0100147 }
148
149 /** Dequantizes a value using the scale/offset in this QuantizationInfo */
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000150 float dequantize(qasymm8_t value) const
Michel Iwaniec00633802017-10-12 14:14:15 +0100151 {
152 ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::dequantize: scale == 0");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000153 return scvt_f32_qasymm8(value, scale, offset);
Michel Iwaniec00633802017-10-12 14:14:15 +0100154 }
155
156 /** Indicates whether this QuantizationInfo has valid settings or not */
157 bool empty() const
158 {
159 return scale == 0;
160 }
161};
162
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163struct ValidRegion
164{
165 ValidRegion()
166 : anchor{}, shape{}
167 {
168 }
169
170 ValidRegion(const ValidRegion &) = default;
171 ValidRegion(ValidRegion &&) = default;
172 ValidRegion &operator=(const ValidRegion &) = default;
173 ValidRegion &operator=(ValidRegion &&) = default;
174 ~ValidRegion() = default;
175
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000176 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape)
177 : anchor{ an_anchor }, shape{ a_shape }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000179 anchor.set_num_dimensions(std::max(anchor.num_dimensions(), shape.num_dimensions()));
180 }
181
182 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape, size_t num_dimensions)
183 : anchor{ an_anchor }, shape{ a_shape }
184 {
185 ARM_COMPUTE_ERROR_ON(num_dimensions < std::max(anchor.num_dimensions(), shape.num_dimensions()));
186 anchor.set_num_dimensions(num_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 }
188
189 /** Return the start of the valid region for the given dimension @p d */
190 int start(unsigned int d) const
191 {
192 return anchor[d];
193 }
194
195 /** Return the end of the valid region for the given dimension @p d */
196 int end(unsigned int d) const
197 {
198 return anchor[d] + shape[d];
199 }
200
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000201 /** Accessor to set the value of anchor and shape for one of the dimensions.
202 *
203 * @param[in] dimension Dimension for which the value is set.
204 * @param[in] start Value to be set in anchor for the dimension.
205 * @param[in] size Value to be set in shape for the dimension.
206 *
207 * @return *this.
208 */
209 ValidRegion &set(size_t dimension, int start, size_t size)
210 {
211 anchor.set(dimension, start);
212 shape.set(dimension, size);
213 return *this;
214 }
215
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 Coordinates anchor;
217 TensorShape shape;
218};
219
220/** Methods available to handle borders */
221enum class BorderMode
222{
223 UNDEFINED, /**< Borders are left undefined */
224 CONSTANT, /**< Pixels outside the image are assumed to have a constant value */
225 REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */
226};
227
228/** Container for 2D border size */
229struct BorderSize
230{
231 /** Empty border, i.e. no border */
232 constexpr BorderSize()
233 : top{ 0 }, right{ 0 }, bottom{ 0 }, left{ 0 }
234 {
235 }
236
237 /** Border with equal size around the 2D plane */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100238 explicit constexpr BorderSize(unsigned int size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100239 : top{ size }, right{ size }, bottom{ size }, left{ size }
240 {
241 }
242
243 /** Border with same size for top/bottom and left/right */
244 constexpr BorderSize(unsigned int top_bottom, unsigned int left_right)
245 : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right }
246 {
247 }
248
249 /** Border with different sizes */
250 constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left)
251 : top{ top }, right{ right }, bottom{ bottom }, left{ left }
252 {
253 }
254
255 /** Check if the entire border is zero */
256 constexpr bool empty() const
257 {
258 return top == 0 && right == 0 && bottom == 0 && left == 0;
259 }
260
261 /** Check if the border is the same size on all sides */
262 constexpr bool uniform() const
263 {
264 return top == right && top == bottom && top == left;
265 }
266
267 BorderSize &operator*=(float scale)
268 {
269 top *= scale;
270 right *= scale;
271 bottom *= scale;
272 left *= scale;
273
274 return *this;
275 }
276
277 BorderSize operator*(float scale)
278 {
279 BorderSize size = *this;
280 size *= scale;
281
282 return size;
283 }
284
285 void limit(const BorderSize &limit)
286 {
287 top = std::min(top, limit.top);
288 right = std::min(right, limit.right);
289 bottom = std::min(bottom, limit.bottom);
290 left = std::min(left, limit.left);
291 }
292
293 unsigned int top;
294 unsigned int right;
295 unsigned int bottom;
296 unsigned int left;
297};
298
299using PaddingSize = BorderSize;
300
301/** Policy to handle overflow */
302enum class ConvertPolicy
303{
304 WRAP, /**< Wrap around */
305 SATURATE /**< Saturate */
306};
307
308/** Interpolation method */
309enum class InterpolationPolicy
310{
311 NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */
312 BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */
313 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 */
314};
315
316/** Bilinear Interpolation method used by LKTracker */
317enum class BilinearInterpolation
318{
319 BILINEAR_OLD_NEW,
320 BILINEAR_SCHARR
321};
322
323/** Threshold mode */
324enum class ThresholdType
325{
326 BINARY, /**< Threshold with one value */
327 RANGE /**< Threshold with two values*/
328};
329
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100330/** Termination criteria */
331enum class Termination
332{
333 TERM_CRITERIA_EPSILON,
334 TERM_CRITERIA_ITERATIONS,
335 TERM_CRITERIA_BOTH
336};
337
338/** Magnitude calculation type. */
339enum class MagnitudeType
340{
341 L1NORM, /**< L1 normalization type */
342 L2NORM /**< L2 normalization type */
343};
344
345/** Phase calculation type.
346 *
347 * @note When PhaseType == SIGNED, each angle is mapped to the range 0 to 255 inclusive otherwise angles between 0 and 180
348 */
349enum class PhaseType
350{
351 SIGNED, /**< Angle range: [0, 360] */
352 UNSIGNED /**< Angle range: [0, 180] */
353};
354
355/** Keypoint type */
356struct KeyPoint
357{
358 int32_t x{ 0 }; /**< X coordinates */
359 int32_t y{ 0 }; /**< Y coordinates */
360 float strength{ 0.f }; /**< Strength of the point */
361 float scale{ 0.f }; /**< Scale initialized to 0 by the corner detector */
362 float orientation{ 0.f }; /**< Orientation initialized to 0 by the corner detector */
363 int32_t tracking_status{ 0 }; /**< Status initialized to 1 by the corner detector, set to 0 when the point is lost */
364 float error{ 0.f }; /**< Tracking error initialized to 0 by the corner detector */
365};
366
367using InternalKeypoint = std::tuple<float, float, float>; /* x,y,strength */
368
369/** Rectangle type */
370struct Rectangle
371{
372 uint16_t x; /**< Top-left x coordinate */
373 uint16_t y; /**< Top-left y coordinate */
374 uint16_t width; /**< Width of the rectangle */
375 uint16_t height; /**< Height of the rectangle */
376};
377
378/** Coordinate type */
379struct Coordinates2D
380{
381 int32_t x; /**< X coordinates */
382 int32_t y; /**< Y coordinates */
383};
384
385/** Coordinate type */
386struct Coordinates3D
387{
388 uint32_t x; /**< X coordinates */
389 uint32_t y; /**< Y coordinates */
390 uint32_t z; /**< Z coordinates */
391};
392
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100393/** Region of interest */
394struct ROI
395{
396 Rectangle rect; /**< Rectangle specifying the region of interest */
397 uint16_t batch_idx; /**< The batch index of the region of interest */
398};
399
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100400/** Available channels */
401enum class Channel
402{
403 UNKNOWN, /** Unknown channel format */
404 C0, /**< First channel (used by formats with unknown channel types). */
405 C1, /**< Second channel (used by formats with unknown channel types). */
406 C2, /**< Third channel (used by formats with unknown channel types). */
407 C3, /**< Fourth channel (used by formats with unknown channel types). */
408 R, /**< Red channel. */
409 G, /**< Green channel. */
410 B, /**< Blue channel. */
411 A, /**< Alpha channel. */
412 Y, /**< Luma channel. */
413 U, /**< Cb/U channel. */
414 V /**< Cr/V/Value channel. */
415};
416
417/** Available matrix patterns */
418enum class MatrixPattern
419{
420 BOX, /**< Box pattern matrix. */
421 CROSS, /**< Cross pattern matrix. */
422 DISK, /**< Disk pattern matrix. */
423 OTHER /**< Any other matrix pattern. */
424};
425
426/** Available non linear functions. */
427enum class NonLinearFilterFunction : unsigned
428{
429 MEDIAN = 0, /**< Non linear median filter. */
430 MIN = 1, /**< Non linear erode. */
431 MAX = 2, /**< Non linear dilate. */
432};
433
Georgios Pinitasd9769582017-08-03 10:19:40 +0100434/** Available reduction operations */
435enum class ReductionOperation
436{
437 SUM_SQUARE, /**< Sum of squares */
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100438 SUM, /**< Sum */
Georgios Pinitasd9769582017-08-03 10:19:40 +0100439};
440
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100441/** The normalization type used for the normalization layer */
442enum class NormType
443{
444 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
445 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
446 CROSS_MAP /**< Normalization applied cross maps */
447};
448
449/** Normalization type for Histogram of Oriented Gradients (HOG) */
450enum class HOGNormType
451{
452 L2_NORM = 1, /**< L2-norm */
453 L2HYS_NORM = 2, /**< L2-norm followed by clipping */
454 L1_NORM = 3 /**< L1 norm */
455};
456
457/** Detection window used for the object detection. The detection window keeps the following information:
458 *
459 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
460 * -# Index of the class used for evaluating which class the detection window belongs to
461 * -# Confidence value (score) obtained with the classifier
462 */
463struct DetectionWindow
464{
465 uint16_t x{ 0 }; /**< Top-left x coordinate */
466 uint16_t y{ 0 }; /**< Top-left y coordinate */
467 uint16_t width{ 0 }; /**< Width of the detection window */
468 uint16_t height{ 0 }; /**< Height of the detection window */
469 uint16_t idx_class{ 0 }; /**< Index of the class */
470 float score{ 0.f }; /**< Confidence value for the detection window */
471};
472
473/** Dimension rounding type when down-scaling on CNNs
474 * @note Used in pooling and convolution layer
475 */
476enum class DimensionRoundingType
477{
478 FLOOR, /**< Floor rounding */
479 CEIL /**< Ceil rounding */
480};
481
482/** Available pooling types */
483enum class PoolingType
484{
485 MAX, /**< Max Pooling */
Georgios Pinitascdf51452017-08-31 14:21:36 +0100486 AVG, /**< Average Pooling */
487 L2 /**< L2 Pooling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100488};
489
490/** Padding and stride information class */
491class PadStrideInfo
492{
493public:
494 /** Constructor
495 *
496 * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1.
497 * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1.
498 * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0.
499 * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0.
500 * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR.
501 */
502 PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1,
503 unsigned int pad_x = 0, unsigned int pad_y = 0,
504 DimensionRoundingType round = DimensionRoundingType::FLOOR)
505 : _stride(std::make_pair(stride_x, stride_y)),
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100506 _pad_left(pad_x),
507 _pad_top(pad_y),
508 _pad_right(pad_x),
509 _pad_bottom(pad_y),
510 _round_type(round)
511 {
512 }
513 /** Constructor
514 *
515 * @param[in] stride_x Stride, in elements, across x.
516 * @param[in] stride_y Stride, in elements, across y.
517 * @param[in] pad_left Padding across x on the left, in elements.
518 * @param[in] pad_top Padding across y on the top, in elements.
519 * @param[in] pad_right Padding across x on the right, in elements.
520 * @param[in] pad_bottom Padding across y on the bottom, in elements.
521 * @param[in] round Dimensions rounding.
522 */
523 PadStrideInfo(unsigned int stride_x, unsigned int stride_y,
524 unsigned int pad_left, unsigned int pad_right,
525 unsigned int pad_top, unsigned int pad_bottom,
526 DimensionRoundingType round)
527 : _stride(std::make_pair(stride_x, stride_y)),
528 _pad_left(pad_left),
529 _pad_top(pad_top),
530 _pad_right(pad_right),
531 _pad_bottom(pad_bottom),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100532 _round_type(round)
533 {
534 }
535 std::pair<unsigned int, unsigned int> stride() const
536 {
537 return _stride;
538 }
539 std::pair<unsigned int, unsigned int> pad() const
540 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100541 //this accessor should be used only when padding is symmetric
542 ARM_COMPUTE_ERROR_ON(_pad_left != _pad_right || _pad_top != _pad_bottom);
543 return std::make_pair(_pad_left, _pad_top);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100544 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100545
546 unsigned int pad_left() const
547 {
548 return _pad_left;
549 }
550 unsigned int pad_right() const
551 {
552 return _pad_right;
553 }
554 unsigned int pad_top() const
555 {
556 return _pad_top;
557 }
558 unsigned int pad_bottom() const
559 {
560 return _pad_bottom;
561 }
562
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100563 DimensionRoundingType round() const
564 {
565 return _round_type;
566 }
567
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100568 bool has_padding() const
569 {
570 return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0);
571 }
572
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100573private:
574 std::pair<unsigned int, unsigned int> _stride;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100575 unsigned int _pad_left;
576 unsigned int _pad_top;
577 unsigned int _pad_right;
578 unsigned int _pad_bottom;
579
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100580 DimensionRoundingType _round_type;
581};
582
583/** Pooling Layer Information class */
584class PoolingLayerInfo
585{
586public:
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000587 /** Default Constructor */
588 PoolingLayerInfo()
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000589 : _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 +0000590 {
591 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100592 /** Default Constructor
593 *
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000594 * @param[in] pool_type Pooling type @ref PoolingType.
595 * @param[in] pool_size Pooling size, in elements, across x and y.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100596 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000597 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
598 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
599 * Defaults to false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100600 */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000601 explicit PoolingLayerInfo(PoolingType pool_type,
602 unsigned int pool_size,
603 PadStrideInfo pad_stride_info = PadStrideInfo(),
604 bool exclude_padding = false)
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000605 : _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)
606 {
607 }
608 /** Default Constructor
609 *
610 * @param[in] pool_type Pooling type @ref PoolingType.
611 * @param[in] pool_size Pooling size, in elements, across x and y.
612 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
613 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
614 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
615 * Defaults to false;
616 */
617 explicit PoolingLayerInfo(PoolingType pool_type,
618 Size2D pool_size,
619 PadStrideInfo pad_stride_info = PadStrideInfo(),
620 bool exclude_padding = false)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000621 : _pool_type(pool_type), _pool_size(pool_size), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false)
622 {
623 }
624 /** Default Constructor
625 *
626 * @note This constructor is used for global pooling
627 *
628 * @param[in] pool_type Pooling type @ref PoolingType.
629 */
630 explicit PoolingLayerInfo(PoolingType pool_type)
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000631 : _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 +0100632 {
633 }
634 PoolingType pool_type() const
635 {
636 return _pool_type;
637 }
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000638 const Size2D &pool_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100639 {
640 return _pool_size;
641 }
642 PadStrideInfo pad_stride_info() const
643 {
644 return _pad_stride_info;
645 }
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000646 bool exclude_padding() const
647 {
648 return _exclude_padding;
649 }
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000650 bool is_global_pooling() const
651 {
652 return _is_global_pooling;
653 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100654
655private:
656 PoolingType _pool_type;
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000657 Size2D _pool_size;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100658 PadStrideInfo _pad_stride_info;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000659 bool _exclude_padding;
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000660 bool _is_global_pooling;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100661};
662
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100663/** ROI Pooling Layer Information class */
664class ROIPoolingLayerInfo
665{
666public:
667 /** Default Constructor
668 *
669 * @param[in] pooled_width Pooled width of the layer.
670 * @param[in] pooled_height Pooled height of the layer.
671 * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions.
672 */
673 ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale)
674 : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale)
675 {
676 }
677 unsigned int pooled_width() const
678 {
679 return _pooled_width;
680 }
681 unsigned int pooled_height() const
682 {
683 return _pooled_height;
684 }
685 float spatial_scale() const
686 {
687 return _spatial_scale;
688 }
689
690private:
691 unsigned int _pooled_width;
692 unsigned int _pooled_height;
693 float _spatial_scale;
694};
695
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100696/** Activation Layer Information class */
697class ActivationLayerInfo
698{
699public:
700 /** Available activation functions */
701 enum class ActivationFunction
702 {
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100703 LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */
704 TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */
705 RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */
706 BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */
707 LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */
708 LEAKY_RELU, /**< Leaky Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
709 SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
710 ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */
711 SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/
712 SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/
713 LINEAR /**< Linear ( \f$ f(x)= ax + b \f$ ) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100714 };
715
716 /** Default Constructor
717 *
718 * @param[in] f The activation function to use.
719 * @param[in] a (Optional) The alpha parameter used by some activation functions
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100720 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
721 * @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 +0100722 */
723 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f)
724 : _act(f), _a(a), _b(b)
725 {
726 }
727 ActivationFunction activation() const
728 {
729 return _act;
730 }
731 float a() const
732 {
733 return _a;
734 }
735 float b() const
736 {
737 return _b;
738 }
739
740private:
741 ActivationFunction _act;
742 float _a;
743 float _b;
744};
745
746/** Normalization Layer Information class */
747class NormalizationLayerInfo
748{
749public:
750 /** Default Constructor
751 *
752 * @param[in] type The normalization type. Can be @ref NormType::IN_MAP_1D, @ref NormType::IN_MAP_2D or @ref NORM_TYPE::CROSS_MAP
753 * @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 +0000754 * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001.
755 * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5.
756 * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
757 * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not.
758 * Should be false to follow [Krichevksy 2012].
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100759 */
Georgios Pinitas41caa622017-11-16 14:37:08 +0000760 NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true)
761 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100762 {
763 }
764 NormType type() const
765 {
766 return _type;
767 }
768 uint32_t norm_size() const
769 {
770 return _norm_size;
771 }
772 float alpha() const
773 {
774 return _alpha;
775 }
776 float beta() const
777 {
778 return _beta;
779 }
780 float kappa() const
781 {
782 return _kappa;
783 }
Georgios Pinitas41caa622017-11-16 14:37:08 +0000784 bool is_cross_map() const
785 {
786 return _type == NormType::CROSS_MAP;
787 }
788 bool is_in_map() const
789 {
790 return !is_cross_map();
791 }
792 /** Return the scaling factor of the normalization function.
793 *
794 * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed,
795 * 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 +0100796 *
797 * @return The normalization scaling factor.
798 */
799 float scale_coeff() const
800 {
801 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
Georgios Pinitas41caa622017-11-16 14:37:08 +0000802 return (_is_scaled) ? (_alpha / size) : _alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100803 }
804
805private:
806 NormType _type;
807 uint32_t _norm_size;
808 float _alpha;
809 float _beta;
810 float _kappa;
Georgios Pinitas41caa622017-11-16 14:37:08 +0000811 bool _is_scaled;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100812};
813
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100814/** 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 +0100815class WeightsInfo
816{
817public:
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100818 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100819 WeightsInfo()
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100820 : _are_reshaped(false), _kernel_width(0), _kernel_height(0), _num_kernels(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100821 {
822 }
823 /** Constructor
824 *
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100825 * @param[in] are_reshaped True if the weights have been reshaped
826 * @param[in] kernel_width Kernel width.
827 * @param[in] kernel_height Kernel height.
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100828 * @param[in] num_kernels Number of convolution kernels.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100829 */
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100830 WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels)
831 : _are_reshaped(are_reshaped), _kernel_width(kernel_width), _kernel_height(kernel_height), _num_kernels(num_kernels)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100832 {
833 }
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100834 /** Flag which specifies if the weights tensor has been reshaped.
835 *
836 * @return True if the weights tensors has been reshaped
837 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100838 bool are_reshaped() const
839 {
840 return _are_reshaped;
841 };
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100842 /** Return the number of convolution kernels
843 *
844 * @return The number of convolution kernels
845 */
846 unsigned int num_kernels() const
847 {
848 return _num_kernels;
849 };
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100850 /** Return the width and height of the kernel
851 *
852 * @return The width and height of the kernel
853 */
854 std::pair<unsigned int, unsigned int> kernel_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100855 {
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100856 return std::make_pair(_kernel_width, _kernel_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100857 }
858
859private:
860 const bool _are_reshaped;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100861 const unsigned int _kernel_width;
862 const unsigned int _kernel_height;
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100863 const unsigned int _num_kernels;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100864};
865
Gian Marco36a0a462018-01-12 10:21:40 +0000866/** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape.
867 *
868 * The matrix A can only be reshaped through @ref CLGEMMInterleave4x4Kernel or @ref NEGEMMInterleave4x4Kernel or @ref GCGEMMInterleave4x4Kernel
869 * 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
870 *
871 * The matrix B can only be reshaped through @ref CLGEMMTranspose1xWKernel or @ref NEGEMMTranspose1xWKernel or @ref GCGEMMTranspose1xWKernel
872 * 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
873 *
874 */
875class GEMMReshapeInfo final
876{
877public:
878 /** Default constructor */
879 GEMMReshapeInfo()
880 : _m(1), _n(1), _k(1), _mult_transpose1xW_width(1), _mult_interleave4x4_height(1)
881 {
882 }
883 /** Constructor
884 *
885 * @param[in] m Number of matrix A rows
886 * @param[in] n Number of matrix B columns
887 * @param[in] k Number of matrix A columns or matrix B rows
888 * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block
889 * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block
890 */
891 GEMMReshapeInfo(int m, int n, int k, int mult_transpose1xW_width = 1, int mult_interleave4x4_height = 1)
892 : _m(m), _n(n), _k(k), _mult_transpose1xW_width(mult_transpose1xW_width), _mult_interleave4x4_height(mult_interleave4x4_height)
893 {
894 }
895 /** Number of matrix A rows
896 *
897 * @return the number of matrix A rows
898 */
899 int m() const
900 {
901 return _m;
902 }
903 /** Number of matrix B columns
904 *
905 * @return the number of matrix B columns
906 */
907 int n() const
908 {
909 return _n;
910 }
911 /** Number of matrix A columns or matrix B rows
912 *
913 * @return the number of matrix A columns or matrix B rows
914 */
915 int k() const
916 {
917 return _k;
918 }
919 /** Multiplication factor for the width of the 1xW transposed block
920 *
921 * @return the multiplication factor for the width of the 1xW transposed block
922 */
923 int mult_transpose1xW_width() const
924 {
925 return _mult_transpose1xW_width;
926 }
927 /** Multiplication factor for the height of the 4x4 interleaved block
928 *
929 * @return the multiplication factor for the height of the 4x4 interleaved block
930 */
931 int mult_interleave4x4_height() const
932 {
933 return _mult_interleave4x4_height;
934 }
935
936private:
937 const int _m;
938 const int _n;
939 const int _k;
940 const int _mult_transpose1xW_width;
941 const int _mult_interleave4x4_height;
942};
943
944/** GEMM information class. This class stores the necessary information to compute GEMM functions
945 *
946 * This object also contains the information about how matrix A and matrix B have been reshaped
947 *
948 */
Chunosov5124be52017-11-22 20:42:13 +0700949class GEMMInfo
950{
951public:
952 /** Default constructor */
953 GEMMInfo()
Gian Marco36a0a462018-01-12 10:21:40 +0000954 : _is_a_reshaped(false), _is_b_reshaped(false), _reshape_b_only_on_first_run(false), _reshape_info()
Chunosov5124be52017-11-22 20:42:13 +0700955 {
956 }
957 /** Constructor
958 *
959 * @param[in] is_a_reshaped True if the matrix A has been reshaped
960 * @param[in] is_b_reshaped True if the matrix B has been reshaped
961 * @param[in] reshape_b_only_on_first_run Reshape matrix B only for the first run
Gian Marco36a0a462018-01-12 10:21:40 +0000962 * @param[in] reshape_info (Optional) GEMM reshape information object
Chunosov5124be52017-11-22 20:42:13 +0700963 */
Gian Marco36a0a462018-01-12 10:21:40 +0000964 GEMMInfo(bool is_a_reshaped, bool is_b_reshaped, bool reshape_b_only_on_first_run, const GEMMReshapeInfo &reshape_info = GEMMReshapeInfo())
965 : _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 +0700966 {
967 }
968 /** Flag which specifies if the matrix A has been reshaped
969 *
970 * @return True if the matrix A has been reshaped
971 */
972 bool is_a_reshaped() const
973 {
974 return _is_a_reshaped;
975 };
976 /** Flag which specifies if the matrix B has been reshaped
977 *
978 * @return True if the matrix B has been reshaped
979 */
980 bool is_b_reshaped() const
981 {
982 return _is_b_reshaped;
983 };
984 /** Flag which specifies if the reshape of matrix B should executed only for the first
985 *
986 * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer
987 *
988 * @return True if the reshaped of matrix B happens only for the first run
989 */
990 bool reshape_b_only_on_first_run() const
991 {
992 return _reshape_b_only_on_first_run;
993 };
Gian Marco36a0a462018-01-12 10:21:40 +0000994 /** GEMMReshapeInfo object which stores the necessary information to understand how the matrix A and matrix B have been reshaped
995 *
996 * @return the GEMMReshapeInfo object
997 */
998 const GEMMReshapeInfo &reshape_info() const
999 {
1000 return _reshape_info;
1001 }
Chunosov5124be52017-11-22 20:42:13 +07001002
1003private:
Gian Marco36a0a462018-01-12 10:21:40 +00001004 const bool _is_a_reshaped;
1005 const bool _is_b_reshaped;
1006 const bool _reshape_b_only_on_first_run;
1007 GEMMReshapeInfo _reshape_info;
Chunosov5124be52017-11-22 20:42:13 +07001008};
1009
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001010/** IO formatting information class*/
1011struct IOFormatInfo
1012{
1013 /** Precision type used when printing floating point numbers */
1014 enum class PrecisionType
1015 {
1016 Default, /**< Default precision to the one that the current stream has */
1017 Custom, /**< Custom precision specified by the user using the precision parameter */
1018 Full /**< The maximum precision of the floating point representation */
1019 };
1020
1021 /** Specifies the area to be printed, used by Tensor objects */
1022 enum class PrintRegion
1023 {
1024 ValidRegion, /**< Prints the valid region of the Tensor object */
1025 NoPadding, /**< Prints the Tensor object without the padding */
1026 Full /**< Print the tensor object including padding */
1027 };
1028
1029 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
1030 PrecisionType precision_type = PrecisionType::Default,
1031 unsigned int precision = 10,
1032 bool align_columns = true,
1033 std::string element_delim = " ",
1034 std::string row_delim = "\n")
1035 : print_region(print_region),
1036 precision_type(precision_type),
1037 precision(precision),
1038 element_delim(element_delim),
1039 row_delim(row_delim),
1040 align_columns(align_columns)
1041 {
1042 }
1043
1044 PrintRegion print_region;
1045 PrecisionType precision_type;
1046 unsigned int precision;
1047 std::string element_delim;
1048 std::string row_delim;
1049 bool align_columns;
1050};
1051}
1052#endif /* __ARM_COMPUTE_TYPES_H__ */