blob: 5197000bf9a2ca37003645f8755ed5f8801fa1a1 [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
193 Coordinates anchor;
194 TensorShape shape;
195};
196
197/** Methods available to handle borders */
198enum class BorderMode
199{
200 UNDEFINED, /**< Borders are left undefined */
201 CONSTANT, /**< Pixels outside the image are assumed to have a constant value */
202 REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */
203};
204
205/** Container for 2D border size */
206struct BorderSize
207{
208 /** Empty border, i.e. no border */
209 constexpr BorderSize()
210 : top{ 0 }, right{ 0 }, bottom{ 0 }, left{ 0 }
211 {
212 }
213
214 /** Border with equal size around the 2D plane */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100215 explicit constexpr BorderSize(unsigned int size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 : top{ size }, right{ size }, bottom{ size }, left{ size }
217 {
218 }
219
220 /** Border with same size for top/bottom and left/right */
221 constexpr BorderSize(unsigned int top_bottom, unsigned int left_right)
222 : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right }
223 {
224 }
225
226 /** Border with different sizes */
227 constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left)
228 : top{ top }, right{ right }, bottom{ bottom }, left{ left }
229 {
230 }
231
232 /** Check if the entire border is zero */
233 constexpr bool empty() const
234 {
235 return top == 0 && right == 0 && bottom == 0 && left == 0;
236 }
237
238 /** Check if the border is the same size on all sides */
239 constexpr bool uniform() const
240 {
241 return top == right && top == bottom && top == left;
242 }
243
244 BorderSize &operator*=(float scale)
245 {
246 top *= scale;
247 right *= scale;
248 bottom *= scale;
249 left *= scale;
250
251 return *this;
252 }
253
254 BorderSize operator*(float scale)
255 {
256 BorderSize size = *this;
257 size *= scale;
258
259 return size;
260 }
261
262 void limit(const BorderSize &limit)
263 {
264 top = std::min(top, limit.top);
265 right = std::min(right, limit.right);
266 bottom = std::min(bottom, limit.bottom);
267 left = std::min(left, limit.left);
268 }
269
270 unsigned int top;
271 unsigned int right;
272 unsigned int bottom;
273 unsigned int left;
274};
275
276using PaddingSize = BorderSize;
277
278/** Policy to handle overflow */
279enum class ConvertPolicy
280{
281 WRAP, /**< Wrap around */
282 SATURATE /**< Saturate */
283};
284
285/** Interpolation method */
286enum class InterpolationPolicy
287{
288 NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */
289 BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */
290 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 */
291};
292
293/** Bilinear Interpolation method used by LKTracker */
294enum class BilinearInterpolation
295{
296 BILINEAR_OLD_NEW,
297 BILINEAR_SCHARR
298};
299
300/** Threshold mode */
301enum class ThresholdType
302{
303 BINARY, /**< Threshold with one value */
304 RANGE /**< Threshold with two values*/
305};
306
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307/** Termination criteria */
308enum class Termination
309{
310 TERM_CRITERIA_EPSILON,
311 TERM_CRITERIA_ITERATIONS,
312 TERM_CRITERIA_BOTH
313};
314
315/** Magnitude calculation type. */
316enum class MagnitudeType
317{
318 L1NORM, /**< L1 normalization type */
319 L2NORM /**< L2 normalization type */
320};
321
322/** Phase calculation type.
323 *
324 * @note When PhaseType == SIGNED, each angle is mapped to the range 0 to 255 inclusive otherwise angles between 0 and 180
325 */
326enum class PhaseType
327{
328 SIGNED, /**< Angle range: [0, 360] */
329 UNSIGNED /**< Angle range: [0, 180] */
330};
331
332/** Keypoint type */
333struct KeyPoint
334{
335 int32_t x{ 0 }; /**< X coordinates */
336 int32_t y{ 0 }; /**< Y coordinates */
337 float strength{ 0.f }; /**< Strength of the point */
338 float scale{ 0.f }; /**< Scale initialized to 0 by the corner detector */
339 float orientation{ 0.f }; /**< Orientation initialized to 0 by the corner detector */
340 int32_t tracking_status{ 0 }; /**< Status initialized to 1 by the corner detector, set to 0 when the point is lost */
341 float error{ 0.f }; /**< Tracking error initialized to 0 by the corner detector */
342};
343
344using InternalKeypoint = std::tuple<float, float, float>; /* x,y,strength */
345
346/** Rectangle type */
347struct Rectangle
348{
349 uint16_t x; /**< Top-left x coordinate */
350 uint16_t y; /**< Top-left y coordinate */
351 uint16_t width; /**< Width of the rectangle */
352 uint16_t height; /**< Height of the rectangle */
353};
354
355/** Coordinate type */
356struct Coordinates2D
357{
358 int32_t x; /**< X coordinates */
359 int32_t y; /**< Y coordinates */
360};
361
362/** Coordinate type */
363struct Coordinates3D
364{
365 uint32_t x; /**< X coordinates */
366 uint32_t y; /**< Y coordinates */
367 uint32_t z; /**< Z coordinates */
368};
369
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100370/** Region of interest */
371struct ROI
372{
373 Rectangle rect; /**< Rectangle specifying the region of interest */
374 uint16_t batch_idx; /**< The batch index of the region of interest */
375};
376
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100377/** Available channels */
378enum class Channel
379{
380 UNKNOWN, /** Unknown channel format */
381 C0, /**< First channel (used by formats with unknown channel types). */
382 C1, /**< Second channel (used by formats with unknown channel types). */
383 C2, /**< Third channel (used by formats with unknown channel types). */
384 C3, /**< Fourth channel (used by formats with unknown channel types). */
385 R, /**< Red channel. */
386 G, /**< Green channel. */
387 B, /**< Blue channel. */
388 A, /**< Alpha channel. */
389 Y, /**< Luma channel. */
390 U, /**< Cb/U channel. */
391 V /**< Cr/V/Value channel. */
392};
393
394/** Available matrix patterns */
395enum class MatrixPattern
396{
397 BOX, /**< Box pattern matrix. */
398 CROSS, /**< Cross pattern matrix. */
399 DISK, /**< Disk pattern matrix. */
400 OTHER /**< Any other matrix pattern. */
401};
402
403/** Available non linear functions. */
404enum class NonLinearFilterFunction : unsigned
405{
406 MEDIAN = 0, /**< Non linear median filter. */
407 MIN = 1, /**< Non linear erode. */
408 MAX = 2, /**< Non linear dilate. */
409};
410
Georgios Pinitasd9769582017-08-03 10:19:40 +0100411/** Available reduction operations */
412enum class ReductionOperation
413{
414 SUM_SQUARE, /**< Sum of squares */
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100415 SUM, /**< Sum */
Georgios Pinitasd9769582017-08-03 10:19:40 +0100416};
417
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418/** The normalization type used for the normalization layer */
419enum class NormType
420{
421 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
422 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
423 CROSS_MAP /**< Normalization applied cross maps */
424};
425
426/** Normalization type for Histogram of Oriented Gradients (HOG) */
427enum class HOGNormType
428{
429 L2_NORM = 1, /**< L2-norm */
430 L2HYS_NORM = 2, /**< L2-norm followed by clipping */
431 L1_NORM = 3 /**< L1 norm */
432};
433
434/** Detection window used for the object detection. The detection window keeps the following information:
435 *
436 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
437 * -# Index of the class used for evaluating which class the detection window belongs to
438 * -# Confidence value (score) obtained with the classifier
439 */
440struct DetectionWindow
441{
442 uint16_t x{ 0 }; /**< Top-left x coordinate */
443 uint16_t y{ 0 }; /**< Top-left y coordinate */
444 uint16_t width{ 0 }; /**< Width of the detection window */
445 uint16_t height{ 0 }; /**< Height of the detection window */
446 uint16_t idx_class{ 0 }; /**< Index of the class */
447 float score{ 0.f }; /**< Confidence value for the detection window */
448};
449
450/** Dimension rounding type when down-scaling on CNNs
451 * @note Used in pooling and convolution layer
452 */
453enum class DimensionRoundingType
454{
455 FLOOR, /**< Floor rounding */
456 CEIL /**< Ceil rounding */
457};
458
459/** Available pooling types */
460enum class PoolingType
461{
462 MAX, /**< Max Pooling */
Georgios Pinitascdf51452017-08-31 14:21:36 +0100463 AVG, /**< Average Pooling */
464 L2 /**< L2 Pooling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100465};
466
467/** Padding and stride information class */
468class PadStrideInfo
469{
470public:
471 /** Constructor
472 *
473 * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1.
474 * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1.
475 * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0.
476 * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0.
477 * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR.
478 */
479 PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1,
480 unsigned int pad_x = 0, unsigned int pad_y = 0,
481 DimensionRoundingType round = DimensionRoundingType::FLOOR)
482 : _stride(std::make_pair(stride_x, stride_y)),
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100483 _pad_left(pad_x),
484 _pad_top(pad_y),
485 _pad_right(pad_x),
486 _pad_bottom(pad_y),
487 _round_type(round)
488 {
489 }
490 /** Constructor
491 *
492 * @param[in] stride_x Stride, in elements, across x.
493 * @param[in] stride_y Stride, in elements, across y.
494 * @param[in] pad_left Padding across x on the left, in elements.
495 * @param[in] pad_top Padding across y on the top, in elements.
496 * @param[in] pad_right Padding across x on the right, in elements.
497 * @param[in] pad_bottom Padding across y on the bottom, in elements.
498 * @param[in] round Dimensions rounding.
499 */
500 PadStrideInfo(unsigned int stride_x, unsigned int stride_y,
501 unsigned int pad_left, unsigned int pad_right,
502 unsigned int pad_top, unsigned int pad_bottom,
503 DimensionRoundingType round)
504 : _stride(std::make_pair(stride_x, stride_y)),
505 _pad_left(pad_left),
506 _pad_top(pad_top),
507 _pad_right(pad_right),
508 _pad_bottom(pad_bottom),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100509 _round_type(round)
510 {
511 }
512 std::pair<unsigned int, unsigned int> stride() const
513 {
514 return _stride;
515 }
516 std::pair<unsigned int, unsigned int> pad() const
517 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100518 //this accessor should be used only when padding is symmetric
519 ARM_COMPUTE_ERROR_ON(_pad_left != _pad_right || _pad_top != _pad_bottom);
520 return std::make_pair(_pad_left, _pad_top);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100521 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100522
523 unsigned int pad_left() const
524 {
525 return _pad_left;
526 }
527 unsigned int pad_right() const
528 {
529 return _pad_right;
530 }
531 unsigned int pad_top() const
532 {
533 return _pad_top;
534 }
535 unsigned int pad_bottom() const
536 {
537 return _pad_bottom;
538 }
539
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100540 DimensionRoundingType round() const
541 {
542 return _round_type;
543 }
544
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100545 bool has_padding() const
546 {
547 return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0);
548 }
549
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100550private:
551 std::pair<unsigned int, unsigned int> _stride;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100552 unsigned int _pad_left;
553 unsigned int _pad_top;
554 unsigned int _pad_right;
555 unsigned int _pad_bottom;
556
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100557 DimensionRoundingType _round_type;
558};
559
560/** Pooling Layer Information class */
561class PoolingLayerInfo
562{
563public:
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000564 /** Default Constructor */
565 PoolingLayerInfo()
566 : _pool_type(PoolingType::MAX), _pool_size(0), _pad_stride_info(PadStrideInfo()), _exclude_padding(false), _is_global_pooling(false)
567 {
568 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100569 /** Default Constructor
570 *
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000571 * @param[in] pool_type Pooling type @ref PoolingType.
572 * @param[in] pool_size Pooling size, in elements, across x and y.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100573 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000574 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
575 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
576 * Defaults to false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100577 */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000578 explicit PoolingLayerInfo(PoolingType pool_type,
579 unsigned int pool_size,
580 PadStrideInfo pad_stride_info = PadStrideInfo(),
581 bool exclude_padding = false)
582 : _pool_type(pool_type), _pool_size(pool_size), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false)
583 {
584 }
585 /** Default Constructor
586 *
587 * @note This constructor is used for global pooling
588 *
589 * @param[in] pool_type Pooling type @ref PoolingType.
590 */
591 explicit PoolingLayerInfo(PoolingType pool_type)
592 : _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 +0100593 {
594 }
595 PoolingType pool_type() const
596 {
597 return _pool_type;
598 }
599 unsigned int pool_size() const
600 {
601 return _pool_size;
602 }
603 PadStrideInfo pad_stride_info() const
604 {
605 return _pad_stride_info;
606 }
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000607 bool exclude_padding() const
608 {
609 return _exclude_padding;
610 }
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000611 bool is_global_pooling() const
612 {
613 return _is_global_pooling;
614 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100615
616private:
617 PoolingType _pool_type;
618 unsigned int _pool_size;
619 PadStrideInfo _pad_stride_info;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000620 bool _exclude_padding;
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000621 bool _is_global_pooling;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100622};
623
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100624/** ROI Pooling Layer Information class */
625class ROIPoolingLayerInfo
626{
627public:
628 /** Default Constructor
629 *
630 * @param[in] pooled_width Pooled width of the layer.
631 * @param[in] pooled_height Pooled height of the layer.
632 * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions.
633 */
634 ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale)
635 : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale)
636 {
637 }
638 unsigned int pooled_width() const
639 {
640 return _pooled_width;
641 }
642 unsigned int pooled_height() const
643 {
644 return _pooled_height;
645 }
646 float spatial_scale() const
647 {
648 return _spatial_scale;
649 }
650
651private:
652 unsigned int _pooled_width;
653 unsigned int _pooled_height;
654 float _spatial_scale;
655};
656
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100657/** Activation Layer Information class */
658class ActivationLayerInfo
659{
660public:
661 /** Available activation functions */
662 enum class ActivationFunction
663 {
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100664 LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */
665 TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */
666 RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */
667 BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */
668 LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */
669 LEAKY_RELU, /**< Leaky Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
670 SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
671 ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */
672 SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/
673 SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/
674 LINEAR /**< Linear ( \f$ f(x)= ax + b \f$ ) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100675 };
676
677 /** Default Constructor
678 *
679 * @param[in] f The activation function to use.
680 * @param[in] a (Optional) The alpha parameter used by some activation functions
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +0100681 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
682 * @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 +0100683 */
684 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f)
685 : _act(f), _a(a), _b(b)
686 {
687 }
688 ActivationFunction activation() const
689 {
690 return _act;
691 }
692 float a() const
693 {
694 return _a;
695 }
696 float b() const
697 {
698 return _b;
699 }
700
701private:
702 ActivationFunction _act;
703 float _a;
704 float _b;
705};
706
707/** Normalization Layer Information class */
708class NormalizationLayerInfo
709{
710public:
711 /** Default Constructor
712 *
713 * @param[in] type The normalization type. Can be @ref NormType::IN_MAP_1D, @ref NormType::IN_MAP_2D or @ref NORM_TYPE::CROSS_MAP
714 * @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 +0000715 * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001.
716 * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5.
717 * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
718 * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not.
719 * Should be false to follow [Krichevksy 2012].
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100720 */
Georgios Pinitas41caa622017-11-16 14:37:08 +0000721 NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true)
722 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100723 {
724 }
725 NormType type() const
726 {
727 return _type;
728 }
729 uint32_t norm_size() const
730 {
731 return _norm_size;
732 }
733 float alpha() const
734 {
735 return _alpha;
736 }
737 float beta() const
738 {
739 return _beta;
740 }
741 float kappa() const
742 {
743 return _kappa;
744 }
Georgios Pinitas41caa622017-11-16 14:37:08 +0000745 bool is_cross_map() const
746 {
747 return _type == NormType::CROSS_MAP;
748 }
749 bool is_in_map() const
750 {
751 return !is_cross_map();
752 }
753 /** Return the scaling factor of the normalization function.
754 *
755 * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed,
756 * 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 +0100757 *
758 * @return The normalization scaling factor.
759 */
760 float scale_coeff() const
761 {
762 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
Georgios Pinitas41caa622017-11-16 14:37:08 +0000763 return (_is_scaled) ? (_alpha / size) : _alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100764 }
765
766private:
767 NormType _type;
768 uint32_t _norm_size;
769 float _alpha;
770 float _beta;
771 float _kappa;
Georgios Pinitas41caa622017-11-16 14:37:08 +0000772 bool _is_scaled;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100773};
774
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100775/** 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 +0100776class WeightsInfo
777{
778public:
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100779 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100780 WeightsInfo()
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100781 : _are_reshaped(false), _kernel_width(0), _kernel_height(0), _num_kernels(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100782 {
783 }
784 /** Constructor
785 *
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100786 * @param[in] are_reshaped True if the weights have been reshaped
787 * @param[in] kernel_width Kernel width.
788 * @param[in] kernel_height Kernel height.
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100789 * @param[in] num_kernels Number of convolution kernels.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100790 */
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100791 WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels)
792 : _are_reshaped(are_reshaped), _kernel_width(kernel_width), _kernel_height(kernel_height), _num_kernels(num_kernels)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100793 {
794 }
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100795 /** Flag which specifies if the weights tensor has been reshaped.
796 *
797 * @return True if the weights tensors has been reshaped
798 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100799 bool are_reshaped() const
800 {
801 return _are_reshaped;
802 };
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100803 /** Return the number of convolution kernels
804 *
805 * @return The number of convolution kernels
806 */
807 unsigned int num_kernels() const
808 {
809 return _num_kernels;
810 };
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100811 /** Return the width and height of the kernel
812 *
813 * @return The width and height of the kernel
814 */
815 std::pair<unsigned int, unsigned int> kernel_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100816 {
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100817 return std::make_pair(_kernel_width, _kernel_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100818 }
819
820private:
821 const bool _are_reshaped;
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100822 const unsigned int _kernel_width;
823 const unsigned int _kernel_height;
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100824 const unsigned int _num_kernels;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100825};
826
Gian Marco36a0a462018-01-12 10:21:40 +0000827/** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape.
828 *
829 * The matrix A can only be reshaped through @ref CLGEMMInterleave4x4Kernel or @ref NEGEMMInterleave4x4Kernel or @ref GCGEMMInterleave4x4Kernel
830 * 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
831 *
832 * The matrix B can only be reshaped through @ref CLGEMMTranspose1xWKernel or @ref NEGEMMTranspose1xWKernel or @ref GCGEMMTranspose1xWKernel
833 * 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
834 *
835 */
836class GEMMReshapeInfo final
837{
838public:
839 /** Default constructor */
840 GEMMReshapeInfo()
841 : _m(1), _n(1), _k(1), _mult_transpose1xW_width(1), _mult_interleave4x4_height(1)
842 {
843 }
844 /** Constructor
845 *
846 * @param[in] m Number of matrix A rows
847 * @param[in] n Number of matrix B columns
848 * @param[in] k Number of matrix A columns or matrix B rows
849 * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block
850 * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block
851 */
852 GEMMReshapeInfo(int m, int n, int k, int mult_transpose1xW_width = 1, int mult_interleave4x4_height = 1)
853 : _m(m), _n(n), _k(k), _mult_transpose1xW_width(mult_transpose1xW_width), _mult_interleave4x4_height(mult_interleave4x4_height)
854 {
855 }
856 /** Number of matrix A rows
857 *
858 * @return the number of matrix A rows
859 */
860 int m() const
861 {
862 return _m;
863 }
864 /** Number of matrix B columns
865 *
866 * @return the number of matrix B columns
867 */
868 int n() const
869 {
870 return _n;
871 }
872 /** Number of matrix A columns or matrix B rows
873 *
874 * @return the number of matrix A columns or matrix B rows
875 */
876 int k() const
877 {
878 return _k;
879 }
880 /** Multiplication factor for the width of the 1xW transposed block
881 *
882 * @return the multiplication factor for the width of the 1xW transposed block
883 */
884 int mult_transpose1xW_width() const
885 {
886 return _mult_transpose1xW_width;
887 }
888 /** Multiplication factor for the height of the 4x4 interleaved block
889 *
890 * @return the multiplication factor for the height of the 4x4 interleaved block
891 */
892 int mult_interleave4x4_height() const
893 {
894 return _mult_interleave4x4_height;
895 }
896
897private:
898 const int _m;
899 const int _n;
900 const int _k;
901 const int _mult_transpose1xW_width;
902 const int _mult_interleave4x4_height;
903};
904
905/** GEMM information class. This class stores the necessary information to compute GEMM functions
906 *
907 * This object also contains the information about how matrix A and matrix B have been reshaped
908 *
909 */
Chunosov5124be52017-11-22 20:42:13 +0700910class GEMMInfo
911{
912public:
913 /** Default constructor */
914 GEMMInfo()
Gian Marco36a0a462018-01-12 10:21:40 +0000915 : _is_a_reshaped(false), _is_b_reshaped(false), _reshape_b_only_on_first_run(false), _reshape_info()
Chunosov5124be52017-11-22 20:42:13 +0700916 {
917 }
918 /** Constructor
919 *
920 * @param[in] is_a_reshaped True if the matrix A has been reshaped
921 * @param[in] is_b_reshaped True if the matrix B has been reshaped
922 * @param[in] reshape_b_only_on_first_run Reshape matrix B only for the first run
Gian Marco36a0a462018-01-12 10:21:40 +0000923 * @param[in] reshape_info (Optional) GEMM reshape information object
Chunosov5124be52017-11-22 20:42:13 +0700924 */
Gian Marco36a0a462018-01-12 10:21:40 +0000925 GEMMInfo(bool is_a_reshaped, bool is_b_reshaped, bool reshape_b_only_on_first_run, const GEMMReshapeInfo &reshape_info = GEMMReshapeInfo())
926 : _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 +0700927 {
928 }
929 /** Flag which specifies if the matrix A has been reshaped
930 *
931 * @return True if the matrix A has been reshaped
932 */
933 bool is_a_reshaped() const
934 {
935 return _is_a_reshaped;
936 };
937 /** Flag which specifies if the matrix B has been reshaped
938 *
939 * @return True if the matrix B has been reshaped
940 */
941 bool is_b_reshaped() const
942 {
943 return _is_b_reshaped;
944 };
945 /** Flag which specifies if the reshape of matrix B should executed only for the first
946 *
947 * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer
948 *
949 * @return True if the reshaped of matrix B happens only for the first run
950 */
951 bool reshape_b_only_on_first_run() const
952 {
953 return _reshape_b_only_on_first_run;
954 };
Gian Marco36a0a462018-01-12 10:21:40 +0000955 /** GEMMReshapeInfo object which stores the necessary information to understand how the matrix A and matrix B have been reshaped
956 *
957 * @return the GEMMReshapeInfo object
958 */
959 const GEMMReshapeInfo &reshape_info() const
960 {
961 return _reshape_info;
962 }
Chunosov5124be52017-11-22 20:42:13 +0700963
964private:
Gian Marco36a0a462018-01-12 10:21:40 +0000965 const bool _is_a_reshaped;
966 const bool _is_b_reshaped;
967 const bool _reshape_b_only_on_first_run;
968 GEMMReshapeInfo _reshape_info;
Chunosov5124be52017-11-22 20:42:13 +0700969};
970
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100971/** IO formatting information class*/
972struct IOFormatInfo
973{
974 /** Precision type used when printing floating point numbers */
975 enum class PrecisionType
976 {
977 Default, /**< Default precision to the one that the current stream has */
978 Custom, /**< Custom precision specified by the user using the precision parameter */
979 Full /**< The maximum precision of the floating point representation */
980 };
981
982 /** Specifies the area to be printed, used by Tensor objects */
983 enum class PrintRegion
984 {
985 ValidRegion, /**< Prints the valid region of the Tensor object */
986 NoPadding, /**< Prints the Tensor object without the padding */
987 Full /**< Print the tensor object including padding */
988 };
989
990 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
991 PrecisionType precision_type = PrecisionType::Default,
992 unsigned int precision = 10,
993 bool align_columns = true,
994 std::string element_delim = " ",
995 std::string row_delim = "\n")
996 : print_region(print_region),
997 precision_type(precision_type),
998 precision(precision),
999 element_delim(element_delim),
1000 row_delim(row_delim),
1001 align_columns(align_columns)
1002 {
1003 }
1004
1005 PrintRegion print_region;
1006 PrecisionType precision_type;
1007 unsigned int precision;
1008 std::string element_delim;
1009 std::string row_delim;
1010 bool align_columns;
1011};
1012}
1013#endif /* __ARM_COMPUTE_TYPES_H__ */