blob: 37ba9f93bf59b980922d0dd2dce0584759f5b448 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottiniceaa0bf2021-02-16 15:15:19 +00002 * Copyright (c) 2016-2021 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TYPES_H
25#define ARM_COMPUTE_TYPES_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/Coordinates.h"
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010028#include "arm_compute/core/QuantizationInfo.h"
Isabella Gottardi6e464c32018-01-26 12:32:45 +000029#include "arm_compute/core/Size2D.h"
Adnan AlSinane4563a02021-09-01 15:32:03 +010030#include "arm_compute/core/Size3D.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"
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000033#include "arm_compute/core/utils/misc/Macros.h"
Georgios Pinitase8291ac2020-02-26 09:58:13 +000034#include "support/Bfloat16.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010035#include "support/Half.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000037#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038#include <cstddef>
39#include <cstdint>
Isabella Gottardia7acb3c2019-01-08 13:48:44 +000040#include <map>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041#include <string>
42#include <utility>
43
44namespace arm_compute
45{
Georgios Pinitas583137c2017-08-31 18:12:42 +010046/** 16-bit floating point type */
47using half = half_float::half;
48
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000049/** Permutation vector */
50using PermutationVector = Strides;
Georgios Pinitas77589b52018-08-21 14:41:35 +010051/** Bidirectional strides */
52using BiStrides = Coordinates;
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000053
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054/** Image colour formats */
55enum class Format
56{
Daniil Efremov02bf80d2017-11-22 00:26:51 +070057 UNKNOWN, /**< Unknown image format */
58 U8, /**< 1 channel, 1 U8 per channel */
59 S16, /**< 1 channel, 1 S16 per channel */
60 U16, /**< 1 channel, 1 U16 per channel */
61 S32, /**< 1 channel, 1 S32 per channel */
62 U32, /**< 1 channel, 1 U32 per channel */
Georgios Pinitase8291ac2020-02-26 09:58:13 +000063 BFLOAT16, /**< 16-bit brain floating-point number */
Daniil Efremov02bf80d2017-11-22 00:26:51 +070064 F16, /**< 1 channel, 1 F16 per channel */
65 F32, /**< 1 channel, 1 F32 per channel */
66 UV88, /**< 2 channel, 1 U8 per channel */
67 RGB888, /**< 3 channels, 1 U8 per channel */
68 RGBA8888, /**< 4 channels, 1 U8 per channel */
69 YUV444, /**< A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes */
70 YUYV422, /**< A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes */
71 NV12, /**< A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling */
72 NV21, /**< A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling */
73 IYUV, /**< A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes */
74 UYVY422 /**< A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075};
76
77/** Available data types */
78enum class DataType
79{
Georgios Pinitas8217c8e2019-11-11 18:24:22 +000080 UNKNOWN, /**< Unknown data type */
81 U8, /**< unsigned 8-bit number */
82 S8, /**< signed 8-bit number */
83 QSYMM8, /**< quantized, symmetric fixed-point 8-bit number */
84 QASYMM8, /**< quantized, asymmetric fixed-point 8-bit number unsigned */
85 QASYMM8_SIGNED, /**< quantized, asymmetric fixed-point 8-bit number signed */
86 QSYMM8_PER_CHANNEL, /**< quantized, symmetric per channel fixed-point 8-bit number */
87 U16, /**< unsigned 16-bit number */
88 S16, /**< signed 16-bit number */
89 QSYMM16, /**< quantized, symmetric fixed-point 16-bit number */
90 QASYMM16, /**< quantized, asymmetric fixed-point 16-bit number */
91 U32, /**< unsigned 32-bit number */
92 S32, /**< signed 32-bit number */
93 U64, /**< unsigned 64-bit number */
94 S64, /**< signed 64-bit number */
Georgios Pinitase8291ac2020-02-26 09:58:13 +000095 BFLOAT16, /**< 16-bit brain floating-point number */
Georgios Pinitas8217c8e2019-11-11 18:24:22 +000096 F16, /**< 16-bit floating-point number */
97 F32, /**< 32-bit floating-point number */
98 F64, /**< 64-bit floating-point number */
99 SIZET /**< size_t */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100};
101
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700102/** Available Sampling Policies */
103enum class SamplingPolicy
104{
105 CENTER, /**< Samples are taken at pixel center */
106 TOP_LEFT /**< Samples are taken at pixel top left corner */
107};
108
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000109/** [DataLayout enum definition] **/
110
Georgios Pinitas4074c992018-01-30 18:13:46 +0000111/** Supported tensor data layouts */
112enum class DataLayout
113{
Alex Gildayc357c472018-03-21 13:54:09 +0000114 UNKNOWN, /**< Unknown data layout */
115 NCHW, /**< Num samples, channels, height, width */
Adnan AlSinane4563a02021-09-01 15:32:03 +0100116 NHWC, /**< Num samples, height, width, channels */
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100117 NCDHW, /**< Num samples, channels, depth, height, width */
Adnan AlSinane4563a02021-09-01 15:32:03 +0100118 NDHWC /**< Num samples, depth, height, width, channels */
Georgios Pinitas4074c992018-01-30 18:13:46 +0000119};
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000120/** [DataLayout enum definition] **/
Georgios Pinitas4074c992018-01-30 18:13:46 +0000121
Isabella Gottardid17a6772018-02-27 17:41:55 +0000122/** Supported tensor data layout dimensions */
123enum class DataLayoutDimension
124{
Alex Gildayc357c472018-03-21 13:54:09 +0000125 CHANNEL, /**< channel */
126 HEIGHT, /**< height */
127 WIDTH, /**< width */
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100128 DEPTH, /**< depth */
Alex Gildayc357c472018-03-21 13:54:09 +0000129 BATCHES /**< batches */
Isabella Gottardid17a6772018-02-27 17:41:55 +0000130};
131
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000132/** Available ConvolutionMethod*/
133enum class ConvolutionMethod
134{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000135 GEMM, /**< Convolution using GEMM */
136 GEMM_CONV2D, /**< Direct 2D GEMM convolution */
137 DIRECT, /**< Direct convolution */
138 WINOGRAD, /**< Convolution using Winograd */
139 FFT /**< Convolution using FFT */
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000140};
141
Manuel Bottini05069f02019-09-26 17:18:26 +0100142/** Available DepthwiseConvolutionFunction*/
143enum class DepthwiseConvolutionFunction
144{
145 OPTIMIZED, /**< Optimized Depthwise Convolution */
146 GENERIC, /**< Generic Depthwise Convolution */
147};
148
giuros0146a49a02019-04-01 13:50:22 +0100149/** Available DeconvolutionMethod*/
150enum class DeconvolutionMethod
151{
152 GEMM, /**< Deconvolution using GEMM */
153 DIRECT, /**< Direct deconvolution */
154};
155
Manuel Bottini2732cca2019-05-28 11:44:41 +0100156/** Available FuseBatchNormalizationType*/
157enum class FuseBatchNormalizationType
158{
159 CONVOLUTION, /**< For Convolution weights */
160 DEPTHWISECONVOLUTION /**< For Depthwise Convolution weights*/
161};
162
Usama Arif89890c62019-03-19 10:57:05 +0000163/** Padding mode to use for PadLayer */
164enum class PaddingMode
165{
166 CONSTANT,
167 REFLECT,
168 SYMMETRIC
169};
170
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000171/** Supported comparison operations */
172enum class ComparisonOperation
173{
174 Equal, /**< Equal comparison ( \f$ x == y \f$ ) */
175 NotEqual, /**< NotEqual comparison ( \f$ x != y \f$ ) */
176 Greater, /**< Greater comparison ( \f$ x > y \f$ ) */
177 GreaterEqual, /**< Greater equal comparison ( \f$ x >= y \f$ ) */
178 Less, /**< Less comparison ( \f$ x < y \f$ ) */
179 LessEqual /**< Less equal comparison ( \f$ x <= y \f$ ) */
180};
181
Alex Gildayc357c472018-03-21 13:54:09 +0000182/** Container for valid region of a window */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183struct ValidRegion
184{
Alex Gildayc357c472018-03-21 13:54:09 +0000185 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186 ValidRegion()
187 : anchor{}, shape{}
188 {
189 }
190
Alex Gildayc357c472018-03-21 13:54:09 +0000191 /** Allow instances of this class to be copy constructed */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192 ValidRegion(const ValidRegion &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000193 /** Allow instances of this class to be move constructed */
194 ValidRegion(ValidRegion &&) = default;
195 /** Allow instances of this class to be copied */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 ValidRegion &operator=(const ValidRegion &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000197 /** Allow instances of this class to be moved */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 ValidRegion &operator=(ValidRegion &&) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000199 /** Default destructor */
200 ~ValidRegion() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201
Alex Gildayc357c472018-03-21 13:54:09 +0000202 /** Constructor for a valid region with default number of dimensions
203 *
204 * @param[in] an_anchor Anchor for the start of the valid region.
205 * @param[in] a_shape Shape of the valid region.
206 *
207 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000208 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape)
209 : anchor{ an_anchor }, shape{ a_shape }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000211 anchor.set_num_dimensions(std::max(anchor.num_dimensions(), shape.num_dimensions()));
212 }
213
Alex Gildayc357c472018-03-21 13:54:09 +0000214 /** Constructor for a valid region with specified number of dimensions
215 *
216 * @param[in] an_anchor Anchor for the start of the valid region.
217 * @param[in] a_shape Shape of the valid region.
218 * @param[in] num_dimensions Number of dimensions (must be >= number of dimensions of anchor and shape).
219 *
220 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000221 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape, size_t num_dimensions)
222 : anchor{ an_anchor }, shape{ a_shape }
223 {
224 ARM_COMPUTE_ERROR_ON(num_dimensions < std::max(anchor.num_dimensions(), shape.num_dimensions()));
225 anchor.set_num_dimensions(num_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 }
227
228 /** Return the start of the valid region for the given dimension @p d */
229 int start(unsigned int d) const
230 {
231 return anchor[d];
232 }
233
234 /** Return the end of the valid region for the given dimension @p d */
235 int end(unsigned int d) const
236 {
237 return anchor[d] + shape[d];
238 }
239
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000240 /** Accessor to set the value of anchor and shape for one of the dimensions.
241 *
242 * @param[in] dimension Dimension for which the value is set.
243 * @param[in] start Value to be set in anchor for the dimension.
244 * @param[in] size Value to be set in shape for the dimension.
245 *
246 * @return *this.
247 */
248 ValidRegion &set(size_t dimension, int start, size_t size)
249 {
250 anchor.set(dimension, start);
251 shape.set(dimension, size);
252 return *this;
253 }
254
Alex Gildayc357c472018-03-21 13:54:09 +0000255 Coordinates anchor; /**< Anchor for the start of the valid region. */
256 TensorShape shape; /**< Shape of the valid region. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257};
258
259/** Methods available to handle borders */
260enum class BorderMode
261{
262 UNDEFINED, /**< Borders are left undefined */
263 CONSTANT, /**< Pixels outside the image are assumed to have a constant value */
264 REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */
265};
266
267/** Container for 2D border size */
268struct BorderSize
269{
270 /** Empty border, i.e. no border */
Pablo Marquez Tello383de022021-03-18 11:31:13 +0000271 constexpr BorderSize() noexcept
272 : top{ 0 },
273 right{ 0 },
274 bottom{ 0 },
275 left{ 0 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100276 {
277 }
278
279 /** Border with equal size around the 2D plane */
Pablo Marquez Tello383de022021-03-18 11:31:13 +0000280 explicit constexpr BorderSize(unsigned int size) noexcept
281 : top{ size },
282 right{ size },
283 bottom{ size },
284 left{ size }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100285 {
286 }
287
288 /** Border with same size for top/bottom and left/right */
289 constexpr BorderSize(unsigned int top_bottom, unsigned int left_right)
290 : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right }
291 {
292 }
293
294 /** Border with different sizes */
295 constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left)
296 : top{ top }, right{ right }, bottom{ bottom }, left{ left }
297 {
298 }
299
300 /** Check if the entire border is zero */
301 constexpr bool empty() const
302 {
303 return top == 0 && right == 0 && bottom == 0 && left == 0;
304 }
305
306 /** Check if the border is the same size on all sides */
307 constexpr bool uniform() const
308 {
309 return top == right && top == bottom && top == left;
310 }
311
Alex Gildayc357c472018-03-21 13:54:09 +0000312 /** Scale this border size.
313 *
314 * @param[in] scale Scale to multiply border size by.
315 *
316 * @return *this.
317 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318 BorderSize &operator*=(float scale)
319 {
320 top *= scale;
321 right *= scale;
322 bottom *= scale;
323 left *= scale;
324
325 return *this;
326 }
327
Alex Gildayc357c472018-03-21 13:54:09 +0000328 /** Scale a copy of this border size.
329 *
330 * @param[in] scale Scale to multiply border size by.
331 *
332 * @return a scaled copy of this.
333 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100334 BorderSize operator*(float scale)
335 {
336 BorderSize size = *this;
337 size *= scale;
338
339 return size;
340 }
341
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100342 /** Check equality with another BorderSize struct
343 *
344 * @param[in] rhs other struct to check against
345 *
346 * @return true if they are equal
347 */
348 bool operator==(const BorderSize &rhs)
349 {
350 return (top == rhs.top) && (right == rhs.right) && (bottom == rhs.bottom) && (left == rhs.left);
351 }
352
353 /** Check non-equality with another BorderSize struct
354 *
355 * @param[in] rhs other struct to check against
356 *
357 * @return true if they are different
358 */
359 bool operator!=(const BorderSize &rhs)
360 {
361 return !(*this == rhs);
362 }
363
Alex Gildayc357c472018-03-21 13:54:09 +0000364 /** Limit this border size.
365 *
366 * @param[in] limit Border size to limit this border size to.
367 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100368 void limit(const BorderSize &limit)
369 {
370 top = std::min(top, limit.top);
371 right = std::min(right, limit.right);
372 bottom = std::min(bottom, limit.bottom);
373 left = std::min(left, limit.left);
374 }
375
Alex Gildayc357c472018-03-21 13:54:09 +0000376 unsigned int top; /**< top of the border */
377 unsigned int right; /**< right of the border */
378 unsigned int bottom; /**< bottom of the border */
379 unsigned int left; /**< left of the border */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100380};
381
Alex Gildayc357c472018-03-21 13:54:09 +0000382/** Container for 2D padding size */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100383using PaddingSize = BorderSize;
384
385/** Policy to handle overflow */
386enum class ConvertPolicy
387{
388 WRAP, /**< Wrap around */
389 SATURATE /**< Saturate */
390};
391
392/** Interpolation method */
393enum class InterpolationPolicy
394{
395 NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */
396 BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */
397 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 */
398};
399
400/** Bilinear Interpolation method used by LKTracker */
401enum class BilinearInterpolation
402{
Alex Gildayc357c472018-03-21 13:54:09 +0000403 BILINEAR_OLD_NEW, /**< Old-new method */
404 BILINEAR_SCHARR /**< Scharr method */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100405};
406
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407/** Rectangle type */
408struct Rectangle
409{
410 uint16_t x; /**< Top-left x coordinate */
411 uint16_t y; /**< Top-left y coordinate */
412 uint16_t width; /**< Width of the rectangle */
413 uint16_t height; /**< Height of the rectangle */
414};
415
416/** Coordinate type */
417struct Coordinates2D
418{
419 int32_t x; /**< X coordinates */
420 int32_t y; /**< Y coordinates */
421};
422
423/** Coordinate type */
424struct Coordinates3D
425{
426 uint32_t x; /**< X coordinates */
427 uint32_t y; /**< Y coordinates */
428 uint32_t z; /**< Z coordinates */
429};
430
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100431/** Padding information as a pair of unsigned int start/end */
432using PaddingInfo = std::pair<uint32_t, uint32_t>;
433
434/** List of padding information */
435using PaddingList = std::vector<PaddingInfo>;
436
giuros013175fcf2018-11-21 09:59:17 +0000437/** Information to produce a tiled version of a Tensor */
438using Multiples = std::vector<uint32_t>;
439
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100440/** Available channels */
441enum class Channel
442{
443 UNKNOWN, /** Unknown channel format */
444 C0, /**< First channel (used by formats with unknown channel types). */
445 C1, /**< Second channel (used by formats with unknown channel types). */
446 C2, /**< Third channel (used by formats with unknown channel types). */
447 C3, /**< Fourth channel (used by formats with unknown channel types). */
448 R, /**< Red channel. */
449 G, /**< Green channel. */
450 B, /**< Blue channel. */
451 A, /**< Alpha channel. */
452 Y, /**< Luma channel. */
453 U, /**< Cb/U channel. */
454 V /**< Cr/V/Value channel. */
455};
456
Georgios Pinitasd9769582017-08-03 10:19:40 +0100457/** Available reduction operations */
458enum class ReductionOperation
459{
Michalis Spyrou7930db42018-11-22 17:36:28 +0000460 ARG_IDX_MAX, /**< Index of the max value */
Manuel Bottinib412fab2018-12-10 17:40:23 +0000461 ARG_IDX_MIN, /**< Index of the min value */
462 MEAN_SUM, /**< Mean of sum */
463 PROD, /**< Product */
464 SUM_SQUARE, /**< Sum of squares */
Usama Arifa4a08ad2019-05-20 12:38:33 +0100465 SUM, /**< Sum */
466 MIN, /**< Min */
Usama Arif28f0dd92019-05-20 13:44:34 +0100467 MAX, /**< Max */
Georgios Pinitasd9769582017-08-03 10:19:40 +0100468};
469
giuros01164a2722018-11-20 18:34:46 +0000470/** Available element-wise operations */
471enum class ArithmeticOperation
472{
473 ADD, /**< (x + y) */
474 SUB, /**< (x - y) */
475 DIV, /**< (x / y) */
476 MIN, /**< Min(x, y) */
477 MAX, /**< Max(x, y) */
478 SQUARED_DIFF, /**< (x - y)^2 */
Usama Arif81e671e2019-05-13 13:33:14 +0100479 POWER, /**< x ^ y */
giuros011e6e1b82019-05-14 16:12:53 +0100480 PRELU, /**< y*x if x < 0, x otherwise */
giuros01164a2722018-11-20 18:34:46 +0000481};
482
Michalis Spyroue9362622018-11-23 17:41:37 +0000483/** Available element wise unary operations */
484enum class ElementWiseUnary
485{
Sang-Hoon Park75eea332020-11-13 13:44:13 +0000486 RSQRT, /**< Reverse square root */
487 EXP, /**< Exponential */
488 NEG, /**< Negate */
489 LOG, /**< Natural Logarithm */
490 ABS, /**< Absolute value */
491 SIN, /**< Sine */
492 ROUND, /**< Round */
493 LOGICAL_NOT, /**< Logical Not */
Michalis Spyroue9362622018-11-23 17:41:37 +0000494};
495
Manuel Bottini63bb7ca2020-12-02 13:22:14 +0000496/** Available bitwise operations */
497enum class BitwiseOperation
498{
499 AND, /**< Bitwise AND operation */
500 NOT, /**< Bitwise NOT operation */
501 OR, /**< Bitwise OR operation */
502 XOR, /**< Bitwise XOR operation */
503};
504
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100505/** The normalization type used for the normalization layer */
506enum class NormType
507{
508 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
509 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
510 CROSS_MAP /**< Normalization applied cross maps */
511};
512
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100513/** Detection window used for the object detection. The detection window keeps the following information:
514 *
515 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
516 * -# Index of the class used for evaluating which class the detection window belongs to
517 * -# Confidence value (score) obtained with the classifier
518 */
519struct DetectionWindow
520{
521 uint16_t x{ 0 }; /**< Top-left x coordinate */
522 uint16_t y{ 0 }; /**< Top-left y coordinate */
523 uint16_t width{ 0 }; /**< Width of the detection window */
524 uint16_t height{ 0 }; /**< Height of the detection window */
525 uint16_t idx_class{ 0 }; /**< Index of the class */
526 float score{ 0.f }; /**< Confidence value for the detection window */
527};
528
529/** Dimension rounding type when down-scaling on CNNs
530 * @note Used in pooling and convolution layer
531 */
532enum class DimensionRoundingType
533{
534 FLOOR, /**< Floor rounding */
535 CEIL /**< Ceil rounding */
536};
537
538/** Available pooling types */
539enum class PoolingType
540{
541 MAX, /**< Max Pooling */
Georgios Pinitascdf51452017-08-31 14:21:36 +0100542 AVG, /**< Average Pooling */
543 L2 /**< L2 Pooling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100544};
545
Michalis Spyrou2709d612018-09-19 09:46:47 +0100546/** Available non maxima suppression types */
547enum class NMSType
548{
549 LINEAR, /**< Linear NMS */
550 GAUSSIAN, /**< Gaussian NMS */
551 ORIGINAL /**< Original NMS */
552};
553
554/** BoxWithNonMaximaSuppressionLimit Information class */
555class BoxNMSLimitInfo final
556{
557public:
558 /** Constructor
559 *
560 * @param[in] score_thresh (Optional) Score threshold.
561 * @param[in] nms (Optional) NMS value
562 * @param[in] detections (Optional) Number of detections
563 * @param[in] soft_nms_enabled (Optional) Enable SoftNMS
564 * @param[in] soft_nms_method (Optional) Soft NMS method
565 * @param[in] soft_nms_sigma (Optional) Soft NMS sigma value
566 * @param[in] soft_nms_min_score_thres (Optional) Soft NMS minimum score threshold
Manuel Bottini5209be52019-02-13 16:34:56 +0000567 * @param[in] suppress_size (Optional) Filter out boxes based on their size. Defaults to false
568 * @param[in] min_size (Optional) Smaller boxes than min_size will be filtered out. Defaults to 1
569 * @param[in] im_width (Optional) Boxes whose centers (on the x axis) is beyond im_width will be filtered. Defaults to 1
570 * @param[in] im_height (Optional) Boxes whose centers (on the y axis) is beyond im_height will be filtered. Defaults to 1
Michalis Spyrou2709d612018-09-19 09:46:47 +0100571 */
572 BoxNMSLimitInfo(float score_thresh = 0.05f, float nms = 0.3f,
573 int detections = 100, bool soft_nms_enabled = false,
574 NMSType soft_nms_method = NMSType::LINEAR,
Manuel Bottini5209be52019-02-13 16:34:56 +0000575 float soft_nms_sigma = 0.5f, float soft_nms_min_score_thres = 0.001f, bool suppress_size = false, float min_size = 1.0f, float im_width = 1.0f, float im_height = 1.0f)
Michalis Spyrou2709d612018-09-19 09:46:47 +0100576 : _score_thresh(score_thresh), _nms(nms), _detections_per_im(detections), _soft_nms_enabled(soft_nms_enabled), _soft_nms_method(soft_nms_method), _soft_nms_sigma(soft_nms_sigma),
Manuel Bottini5209be52019-02-13 16:34:56 +0000577 _soft_nms_min_score_thres(soft_nms_min_score_thres), _suppress_size(suppress_size), _min_size(min_size), _im_width(im_width), _im_height(im_height)
Michalis Spyrou2709d612018-09-19 09:46:47 +0100578 {
579 }
580 /** Get the score threshold */
581 float score_thresh() const
582 {
583 return _score_thresh;
584 }
585 /** Get the NMS */
586 float nms() const
587 {
588 return _nms;
589 }
590 /** Get the number of detections */
591 int detections_per_im() const
592 {
593 return _detections_per_im;
594 }
595 /** Check if soft NMS is enabled */
596 bool soft_nms_enabled() const
597 {
598 return _soft_nms_enabled;
599 }
600 /** Get soft NMS method */
601 NMSType soft_nms_method() const
602 {
603 return _soft_nms_method;
604 }
605 /** Get soft NMS sigma */
606 float soft_nms_sigma() const
607 {
608 return _soft_nms_sigma;
609 }
610 /** Get soft nms min score threshold */
611 float soft_nms_min_score_thres() const
612 {
613 return _soft_nms_min_score_thres;
614 }
Manuel Bottini5209be52019-02-13 16:34:56 +0000615 /** Get if NMS will suppress boxes based on their size/position */
616 bool suppress_size() const
617 {
618 return _suppress_size;
619 }
620 /** Get size suppression threshold */
621 float min_size() const
622 {
623 return _min_size;
624 }
625 /** Get image width (NMS may suppress boxes whose center sits beyond the image width) */
626 float im_width() const
627 {
628 return _im_width;
629 }
630 /** Get image height (NMS may suppress boxes whose center sits beyond the image height) */
631 float im_height() const
632 {
633 return _im_height;
634 }
Michalis Spyrou2709d612018-09-19 09:46:47 +0100635
636private:
637 float _score_thresh;
638 float _nms;
639 int _detections_per_im;
640 bool _soft_nms_enabled;
641 NMSType _soft_nms_method;
642 float _soft_nms_sigma;
643 float _soft_nms_min_score_thres;
Manuel Bottini5209be52019-02-13 16:34:56 +0000644 bool _suppress_size;
645 float _min_size;
646 float _im_width;
647 float _im_height;
Michalis Spyrou2709d612018-09-19 09:46:47 +0100648};
649
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100650/** Padding and stride information class */
651class PadStrideInfo
652{
653public:
654 /** Constructor
655 *
656 * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1.
657 * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1.
658 * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0.
659 * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0.
660 * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR.
661 */
662 PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1,
663 unsigned int pad_x = 0, unsigned int pad_y = 0,
664 DimensionRoundingType round = DimensionRoundingType::FLOOR)
665 : _stride(std::make_pair(stride_x, stride_y)),
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100666 _pad_left(pad_x),
667 _pad_top(pad_y),
668 _pad_right(pad_x),
669 _pad_bottom(pad_y),
670 _round_type(round)
671 {
672 }
673 /** Constructor
674 *
675 * @param[in] stride_x Stride, in elements, across x.
676 * @param[in] stride_y Stride, in elements, across y.
677 * @param[in] pad_left Padding across x on the left, in elements.
678 * @param[in] pad_top Padding across y on the top, in elements.
679 * @param[in] pad_right Padding across x on the right, in elements.
680 * @param[in] pad_bottom Padding across y on the bottom, in elements.
681 * @param[in] round Dimensions rounding.
682 */
683 PadStrideInfo(unsigned int stride_x, unsigned int stride_y,
684 unsigned int pad_left, unsigned int pad_right,
685 unsigned int pad_top, unsigned int pad_bottom,
686 DimensionRoundingType round)
687 : _stride(std::make_pair(stride_x, stride_y)),
688 _pad_left(pad_left),
689 _pad_top(pad_top),
690 _pad_right(pad_right),
691 _pad_bottom(pad_bottom),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100692 _round_type(round)
693 {
694 }
Alex Gildayc357c472018-03-21 13:54:09 +0000695 /** Get the stride.
696 *
697 * @return a pair: stride x, stride y.
698 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100699 std::pair<unsigned int, unsigned int> stride() const
700 {
701 return _stride;
702 }
Alex Gildayc357c472018-03-21 13:54:09 +0000703 /** Check whether the padding is symmetric.
704 *
705 * @return True if the padding is symmetric.
706 */
Anthony Barbier21f67d62018-02-16 15:17:48 +0000707 bool padding_is_symmetric() const
708 {
709 return (_pad_left == _pad_right) && (_pad_top == _pad_bottom);
710 }
Alex Gildayc357c472018-03-21 13:54:09 +0000711 /** Get the padding.
712 *
713 * @note This should only be used when the padding is symmetric.
714 *
715 * @return a pair: padding left/right, padding top/bottom
716 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100717 std::pair<unsigned int, unsigned int> pad() const
718 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100719 //this accessor should be used only when padding is symmetric
Anthony Barbier21f67d62018-02-16 15:17:48 +0000720 ARM_COMPUTE_ERROR_ON(!padding_is_symmetric());
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100721 return std::make_pair(_pad_left, _pad_top);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100722 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100723
Alex Gildayc357c472018-03-21 13:54:09 +0000724 /** Get the left padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100725 unsigned int pad_left() const
726 {
727 return _pad_left;
728 }
Alex Gildayc357c472018-03-21 13:54:09 +0000729 /** Get the right padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100730 unsigned int pad_right() const
731 {
732 return _pad_right;
733 }
Alex Gildayc357c472018-03-21 13:54:09 +0000734 /** Get the top padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100735 unsigned int pad_top() const
736 {
737 return _pad_top;
738 }
Alex Gildayc357c472018-03-21 13:54:09 +0000739 /** Get the bottom padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100740 unsigned int pad_bottom() const
741 {
742 return _pad_bottom;
743 }
744
Alex Gildayc357c472018-03-21 13:54:09 +0000745 /** Get the rounding type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100746 DimensionRoundingType round() const
747 {
748 return _round_type;
749 }
750
Alex Gildayc357c472018-03-21 13:54:09 +0000751 /** Check whether this has any padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100752 bool has_padding() const
753 {
754 return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0);
755 }
756
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100757private:
758 std::pair<unsigned int, unsigned int> _stride;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100759 unsigned int _pad_left;
760 unsigned int _pad_top;
761 unsigned int _pad_right;
762 unsigned int _pad_bottom;
763
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100764 DimensionRoundingType _round_type;
765};
766
Adnan AlSinane4563a02021-09-01 15:32:03 +0100767/** Padding information for 3D operations like Conv3d */
768struct Padding3D
769{
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100770 Padding3D()
771 {
772 }
773
774 Padding3D(size_t pad_x, size_t pad_y, size_t pad_z)
775 : left(pad_x), right(pad_x), top(pad_y), bottom(pad_y), front(pad_z), back(pad_z)
776 {
777 }
778
779 Padding3D(size_t left, size_t right, size_t top, size_t bottom, size_t front, size_t back)
780 : left(left), right(right), top(top), bottom(bottom), front(front), back(back)
781 {
782 }
783
Adnan AlSinane4563a02021-09-01 15:32:03 +0100784 size_t left = { 0 }; /**< Padding across the width dimenstion on the left, in elements. */
785 size_t right = { 0 }; /**< Padding across the width dimenstion on the right, in elements. */
786 size_t top = { 0 }; /**< Padding across the height dimenstion on the top, in elements. */
787 size_t bottom = { 0 }; /**< Padding across the height dimenstion on the bottom, in elements. */
788 size_t front = { 0 }; /**< Padding across the depth dimenstion on the front, in elements. */
789 size_t back = { 0 }; /**< Padding across the depth dimenstion on the back, in elements. */
790};
791
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100792/** PriorBox layer info */
793class PriorBoxLayerInfo final
794{
795public:
796 /** Default Constructor */
797 PriorBoxLayerInfo()
798 : _min_sizes(),
799 _variances(),
800 _offset(),
801 _flip(true),
802 _clip(false),
803 _max_sizes(),
804 _aspect_ratios(),
805 _img_size(),
806 _steps()
807 {
808 }
809 /** Constructor
810 *
811 * @param[in] min_sizes Min sizes vector.
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100812 * @param[in] variances Variances vector.
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100813 * @param[in] offset Offset value.
814 * @param[in] flip (Optional) Flip the aspect ratios.
815 * @param[in] clip (Optional) Clip coordinates so that they're within [0,1].
816 * @param[in] max_sizes (Optional) Max sizes vector.
817 * @param[in] aspect_ratios (Optional) Aspect ratios of the boxes.
818 * @param[in] img_size (Optional) Image size.
819 * @param[in] steps (Optional) Step values.
820 */
821 PriorBoxLayerInfo(const std::vector<float> &min_sizes, const std::vector<float> &variances, float offset, bool flip = true, bool clip = false,
Pablo Tello32521432018-11-15 14:43:10 +0000822 const std::vector<float> &max_sizes = {}, const std::vector<float> &aspect_ratios = {},
823 const Coordinates2D &img_size = Coordinates2D{ 0, 0 }, const std::array<float, 2> &steps = { { 0.f, 0.f } })
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100824 : _min_sizes(min_sizes),
825 _variances(variances),
826 _offset(offset),
827 _flip(flip),
828 _clip(clip),
829 _max_sizes(max_sizes),
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100830 _aspect_ratios(),
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100831 _img_size(img_size),
832 _steps(steps)
833 {
834 _aspect_ratios.push_back(1.);
835 for(unsigned int i = 0; i < aspect_ratios.size(); ++i)
836 {
837 float ar = aspect_ratios[i];
838 bool already_exist = false;
839 for(auto ar_new : _aspect_ratios)
840 {
841 if(fabs(ar - ar_new) < 1e-6)
842 {
843 already_exist = true;
844 break;
845 }
846 }
847 if(!already_exist)
848 {
849 _aspect_ratios.push_back(ar);
850 if(flip)
851 {
852 _aspect_ratios.push_back(1.f / ar);
853 }
854 }
855 }
856 }
857 /** Get min sizes. */
858 std::vector<float> min_sizes() const
859 {
860 return _min_sizes;
861 }
862 /** Get min variances. */
863 std::vector<float> variances() const
864 {
865 return _variances;
866 }
867 /** Get the step coordinates */
868 std::array<float, 2> steps() const
869 {
870 return _steps;
871 }
872 /** Get the image size coordinates */
873 Coordinates2D img_size() const
874 {
875 return _img_size;
876 }
877 /** Get the offset */
878 float offset() const
879 {
880 return _offset;
881 }
882 /** Get the flip value */
883 bool flip() const
884 {
885 return _flip;
886 }
887 /** Get the clip value */
888 bool clip() const
889 {
890 return _clip;
891 }
892 /** Get max sizes. */
893 std::vector<float> max_sizes() const
894 {
895 return _max_sizes;
896 }
897 /** Get aspect ratios. */
898 std::vector<float> aspect_ratios() const
899 {
900 return _aspect_ratios;
901 }
902
903private:
904 std::vector<float> _min_sizes;
905 std::vector<float> _variances;
906 float _offset;
907 bool _flip;
908 bool _clip;
909 std::vector<float> _max_sizes;
910 std::vector<float> _aspect_ratios;
911 Coordinates2D _img_size;
912 std::array<float, 2> _steps;
913};
914
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000915// Bounding Box [xmin, ymin, xmax, ymax]
916using BBox = std::array<float, 4>;
917// LabelBBox used for map label and bounding box
918using LabelBBox = std::map<int, std::vector<BBox>>;
919
Isabella Gottardi05e56442018-11-16 11:26:52 +0000920/** Available Detection Output code types */
921enum class DetectionOutputLayerCodeType
922{
923 CORNER, /**< Use box corners */
924 CENTER_SIZE, /**< Use box centers and size */
925 CORNER_SIZE, /**< Use box centers and size */
926 TF_CENTER /**< Use box centers and size but flip x and y co-ordinates */
927};
928
929/** Detection Output layer info */
930class DetectionOutputLayerInfo final
931{
932public:
933 /** Default Constructor */
934 DetectionOutputLayerInfo()
935 : _num_classes(),
936 _share_location(),
937 _code_type(DetectionOutputLayerCodeType::CORNER),
938 _keep_top_k(),
939 _nms_threshold(),
940 _top_k(),
941 _background_label_id(),
942 _confidence_threshold(),
943 _variance_encoded_in_target(false),
944 _eta(),
945 _num_loc_classes()
946 {
947 _num_loc_classes = _share_location ? 1 : _num_classes;
948 }
949 /** Constructor
950 *
951 * @param[in] num_classes Number of classes to be predicted.
952 * @param[in] share_location If true, bounding box are shared among different classes.
953 * @param[in] code_type Type of coding method for bbox.
954 * @param[in] keep_top_k Number of total bounding boxes to be kept per image after NMS step.
955 * @param[in] nms_threshold Threshold to be used in NMS.
956 * @param[in] top_k (Optional) Number of boxes per image with top confidence scores that are fed into the NMS algorithm. Default set to -1.
957 * @param[in] background_label_id (Optional) Background label ID. If there is no background class, set it as -1.
958 * @param[in] confidence_threshold (Optional) Only consider detections whose confidences are larger than a threshold. Default set to -FLT_MAX.
959 * @param[in] variance_encoded_in_target (Optional) If true, variance is encoded in target. Otherwise we need to adjust the predicted offset accordingly.Default set to false.
960 * @param[in] eta (Optional) Eta.
961 */
962 DetectionOutputLayerInfo(int num_classes, bool share_location, DetectionOutputLayerCodeType code_type, int keep_top_k, float nms_threshold, int top_k = -1, int background_label_id = -1,
963 float confidence_threshold = std::numeric_limits<float>::lowest(), bool variance_encoded_in_target = false, float eta = 1)
964 : _num_classes(num_classes),
965 _share_location(share_location),
966 _code_type(code_type),
967 _keep_top_k(keep_top_k),
968 _nms_threshold(nms_threshold),
969 _top_k(top_k),
970 _background_label_id(background_label_id),
971 _confidence_threshold(confidence_threshold),
972 _variance_encoded_in_target(variance_encoded_in_target),
973 _eta(eta),
974 _num_loc_classes()
975 {
976 _num_loc_classes = _share_location ? 1 : _num_classes;
977 }
978 /** Get num classes. */
979 int num_classes() const
980 {
981 return _num_classes;
982 }
983 /** Get share location. */
984 bool share_location() const
985 {
986 return _share_location;
987 }
988 /** Get detection output code type. */
989 DetectionOutputLayerCodeType code_type() const
990 {
991 return _code_type;
992 }
993 /** Get if variance encoded in target. */
994 bool variance_encoded_in_target() const
995 {
996 return _variance_encoded_in_target;
997 }
998 /** Get the number of total bounding boxes to be kept per image. */
999 int keep_top_k() const
1000 {
1001 return _keep_top_k;
1002 }
1003 /** Get nms threshold. */
1004 float nms_threshold() const
1005 {
1006 return _nms_threshold;
1007 }
1008 /** Get eta. */
1009 float eta() const
1010 {
1011 return _eta;
1012 }
1013 /** Get background label ID. */
1014 int background_label_id() const
1015 {
1016 return _background_label_id;
1017 }
1018 /** Get confidence threshold. */
1019 float confidence_threshold() const
1020 {
1021 return _confidence_threshold;
1022 }
1023 /** Get top K. */
1024 int top_k() const
1025 {
1026 return _top_k;
1027 }
1028 /** Get number of location classes. */
1029 int num_loc_classes() const
1030 {
1031 return _num_loc_classes;
1032 }
1033
1034private:
1035 int _num_classes;
1036 bool _share_location;
1037 DetectionOutputLayerCodeType _code_type;
1038 int _keep_top_k;
1039 float _nms_threshold;
1040 int _top_k;
1041 int _background_label_id;
1042 float _confidence_threshold;
1043 bool _variance_encoded_in_target;
1044 float _eta;
1045 int _num_loc_classes;
1046};
1047
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001048/** Detection Output layer info */
1049class DetectionPostProcessLayerInfo final
1050{
1051public:
1052 /** Default Constructor */
1053 DetectionPostProcessLayerInfo()
1054 : _max_detections(),
1055 _max_classes_per_detection(),
1056 _nms_score_threshold(),
1057 _iou_threshold(),
1058 _num_classes(),
1059 _scales_values(),
1060 _use_regular_nms(),
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001061 _detection_per_class(),
1062 _dequantize_scores()
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001063 {
1064 }
1065 /** Constructor
1066 *
1067 * @param[in] max_detections Number of total detection.
1068 * @param[in] max_classes_per_detection Number of total classes to be kept after NMS step. Used in the Fast Non-Max-Suppression
1069 * @param[in] nms_score_threshold Threshold to be used in NMS
1070 * @param[in] iou_threshold Threshold to be used during the intersection over union.
1071 * @param[in] num_classes Number of classes.
1072 * @param[in] scales_values Scales values used for decode center size boxes.
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001073 * @param[in] use_regular_nms (Optional) Boolean to determinate if use regular or fast nms. Defaults to false.
1074 * @param[in] detection_per_class (Optional) Number of detection per class. Used in the Regular Non-Max-Suppression. Defaults to 100.
1075 * @param[in] dequantize_scores (Optional) If the scores need to be dequantized. Defaults to true.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001076 */
1077 DetectionPostProcessLayerInfo(unsigned int max_detections, unsigned int max_classes_per_detection, float nms_score_threshold, float iou_threshold, unsigned int num_classes,
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001078 std::array<float, 4> scales_values, bool use_regular_nms = false, unsigned int detection_per_class = 100, bool dequantize_scores = true)
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001079 : _max_detections(max_detections),
1080 _max_classes_per_detection(max_classes_per_detection),
1081 _nms_score_threshold(nms_score_threshold),
1082 _iou_threshold(iou_threshold),
1083 _num_classes(num_classes),
1084 _scales_values(scales_values),
1085 _use_regular_nms(use_regular_nms),
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001086 _detection_per_class(detection_per_class),
1087 _dequantize_scores(dequantize_scores)
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001088 {
1089 }
1090 /** Get max detections. */
1091 unsigned int max_detections() const
1092 {
1093 return _max_detections;
1094 }
1095 /** Get max_classes per detection. Used in the Fast Non-Max-Suppression.*/
1096 unsigned int max_classes_per_detection() const
1097 {
1098 return _max_classes_per_detection;
1099 }
1100 /** Get detection per class. Used in the Regular Non-Max-Suppression */
1101 unsigned int detection_per_class() const
1102 {
1103 return _detection_per_class;
1104 }
1105 /** Get nms threshold. */
1106 float nms_score_threshold() const
1107 {
1108 return _nms_score_threshold;
1109 }
1110 /** Get intersection over union threshold. */
1111 float iou_threshold() const
1112 {
1113 return _iou_threshold;
1114 }
1115 /** Get num classes. */
1116 unsigned int num_classes() const
1117 {
1118 return _num_classes;
1119 }
1120 /** Get if use regular nms. */
1121 bool use_regular_nms() const
1122 {
1123 return _use_regular_nms;
1124 }
1125 /** Get y scale value. */
1126 float scale_value_y() const
1127 {
1128 // Saved as [y,x,h,w]
1129 return _scales_values[0];
1130 }
1131 /** Get x scale value. */
1132 float scale_value_x() const
1133 {
1134 // Saved as [y,x,h,w]
1135 return _scales_values[1];
1136 }
1137 /** Get h scale value. */
1138 float scale_value_h() const
1139 {
1140 // Saved as [y,x,h,w]
1141 return _scales_values[2];
1142 }
1143 /** Get w scale value. */
1144 float scale_value_w() const
1145 {
1146 // Saved as [y,x,h,w]
1147 return _scales_values[3];
1148 }
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001149 /** Get dequantize_scores value. */
1150 bool dequantize_scores() const
1151 {
1152 return _dequantize_scores;
1153 }
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001154
1155private:
1156 unsigned int _max_detections;
1157 unsigned int _max_classes_per_detection;
1158 float _nms_score_threshold;
1159 float _iou_threshold;
1160 unsigned int _num_classes;
1161 std::array<float, 4> _scales_values;
1162 bool _use_regular_nms;
1163 unsigned int _detection_per_class;
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001164 bool _dequantize_scores;
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001165};
1166
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001167/** Pooling Layer Information struct*/
1168struct PoolingLayerInfo
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001169{
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001170 /** Default Constructor */
1171 PoolingLayerInfo()
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001172 : pool_type(PoolingType::MAX),
1173 pool_size(Size2D()),
1174 data_layout(DataLayout::UNKNOWN),
1175 pad_stride_info(PadStrideInfo()),
1176 exclude_padding(false),
1177 is_global_pooling(false),
1178 fp_mixed_precision(false)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001179 {
1180 }
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001181 /** Constructor
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001182 *
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001183 * @param[in] pool_type Pooling type @ref PoolingType.
1184 * @param[in] pool_size Pooling size, in elements, across x and y.
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001185 * @param[in] data_layout Data layout used by the layer @ref DataLayout
1186 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
1187 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1188 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1189 * Defaults to false;
1190 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
1191 */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001192 explicit PoolingLayerInfo(PoolingType pool_type,
1193 unsigned int pool_size,
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001194 DataLayout data_layout,
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001195 PadStrideInfo pad_stride_info = PadStrideInfo(),
1196 bool exclude_padding = false,
1197 bool fp_mixed_precision = false)
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001198 : pool_type(pool_type),
1199 pool_size(Size2D(pool_size, pool_size)),
1200 data_layout(data_layout),
1201 pad_stride_info(pad_stride_info),
1202 exclude_padding(exclude_padding),
1203 is_global_pooling(false),
1204 fp_mixed_precision(fp_mixed_precision)
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001205 {
1206 }
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001207
1208 /** Constructor
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001209 *
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001210 * @param[in] pool_type Pooling type @ref PoolingType.
1211 * @param[in] pool_size Pooling size, in elements, across x and y.
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001212 * @param[in] data_layout Data layout used by the layer @ref DataLayout
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001213 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
1214 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1215 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1216 * Defaults to false;
1217 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001218 */
1219 explicit PoolingLayerInfo(PoolingType pool_type,
1220 Size2D pool_size,
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001221 DataLayout data_layout,
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001222 PadStrideInfo pad_stride_info = PadStrideInfo(),
1223 bool exclude_padding = false,
1224 bool fp_mixed_precision = false)
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001225 : pool_type(pool_type),
1226 pool_size(pool_size),
1227 data_layout(data_layout),
1228 pad_stride_info(pad_stride_info),
1229 exclude_padding(exclude_padding),
1230 is_global_pooling(false),
1231 fp_mixed_precision(fp_mixed_precision)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001232 {
1233 }
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001234
1235 /** Constructor
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001236 *
1237 * @note This constructor is used for global pooling
1238 *
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001239 * @param[in] pool_type Pooling type @ref PoolingType.
1240 * @param[in] data_layout Data layout used by the layer @ref DataLayout
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001241 */
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001242 explicit PoolingLayerInfo(PoolingType pool_type, DataLayout data_layout)
1243 : pool_type(pool_type),
1244 pool_size(Size2D()),
1245 data_layout(data_layout),
1246 pad_stride_info(PadStrideInfo(1, 1, 0, 0)),
1247 exclude_padding(false),
1248 is_global_pooling(true),
1249 fp_mixed_precision(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001250 {
1251 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001252
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001253 PoolingType pool_type;
1254 Size2D pool_size;
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001255 DataLayout data_layout;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001256 PadStrideInfo pad_stride_info;
1257 bool exclude_padding;
1258 bool is_global_pooling;
1259 bool fp_mixed_precision;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001260};
1261
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001262/** ROI Pooling Layer Information class */
giuros0118870812018-09-13 09:31:40 +01001263class ROIPoolingLayerInfo final
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001264{
1265public:
giuros0118870812018-09-13 09:31:40 +01001266 /** Constructor
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001267 *
giuros0118870812018-09-13 09:31:40 +01001268 * @param[in] pooled_width Pooled width of the layer.
1269 * @param[in] pooled_height Pooled height of the layer.
1270 * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions.
1271 * @param[in] sampling_ratio Number of samples to include in each pooling region (if set to zero, a ceil(roi_dims/pooling_dims))
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001272 */
giuros0118870812018-09-13 09:31:40 +01001273 ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale, unsigned int sampling_ratio = 0)
1274 : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale), _sampling_ratio(sampling_ratio)
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001275 {
1276 }
Alex Gildayc357c472018-03-21 13:54:09 +00001277 /** Get the pooled width of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001278 unsigned int pooled_width() const
1279 {
1280 return _pooled_width;
1281 }
Alex Gildayc357c472018-03-21 13:54:09 +00001282 /** Get the pooled height of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001283 unsigned int pooled_height() const
1284 {
1285 return _pooled_height;
1286 }
Alex Gildayc357c472018-03-21 13:54:09 +00001287 /** Get the spatial scale */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001288 float spatial_scale() const
1289 {
1290 return _spatial_scale;
1291 }
giuros0118870812018-09-13 09:31:40 +01001292 /** Get sampling ratio */
1293 unsigned int sampling_ratio() const
1294 {
1295 return _sampling_ratio;
1296 }
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001297
1298private:
1299 unsigned int _pooled_width;
1300 unsigned int _pooled_height;
1301 float _spatial_scale;
giuros0118870812018-09-13 09:31:40 +01001302 unsigned int _sampling_ratio;
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001303};
1304
Manuel Bottini5209be52019-02-13 16:34:56 +00001305/** Generate Proposals Information class */
1306class GenerateProposalsInfo
1307{
1308public:
1309 /** Constructor
1310 *
1311 * @param[in] im_width Width of the original image
1312 * @param[in] im_height Height of the original image
1313 * @param[in] im_scale Scale applied to the original image
1314 * @param[in] spatial_scale (Optional)Scale applied to the feature map. Defaults to 1.0
1315 * @param[in] pre_nms_topN (Optional)Number of the best scores to be selected from the transformations. Defaults to 6000.
1316 * @param[in] post_nms_topN (Optional)Number of the best scores to be selected from the NMS operation. Defaults to 300.
1317 * @param[in] nms_thres (Optional)NMS overlap threshold. Defaults to 0.7.
1318 * @param[in] min_size (Optional)Size used to validate the anchors produced. Defaults to 16.
1319 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region of interest). Defaults to 4.
1320 */
1321 GenerateProposalsInfo(float im_width, float im_height, float im_scale, float spatial_scale = 1.0, int pre_nms_topN = 6000, int post_nms_topN = 300, float nms_thres = 0.7, float min_size = 16.0,
1322 size_t values_per_roi = 4)
1323 : _im_height(im_height), _im_width(im_width), _im_scale(im_scale), _spatial_scale(spatial_scale), _pre_nms_topN(pre_nms_topN), _post_nms_topN(post_nms_topN), _nms_thres(nms_thres),
1324 _min_size(min_size), _values_per_roi(values_per_roi)
1325 {
1326 }
1327
1328 /* Get the original height */
1329 float im_height() const
1330 {
1331 return _im_height;
1332 }
1333 /* Get the original width */
1334 float im_width() const
1335 {
1336 return _im_width;
1337 }
1338 /* Get the image scale */
1339 float im_scale() const
1340 {
1341 return _im_scale;
1342 }
1343 /* Get the value of how many best scores to select (before NMS) */
1344 int pre_nms_topN() const
1345 {
1346 return _pre_nms_topN;
1347 }
1348 /* Get the value of how many best scores to select (after NMS) */
1349 int post_nms_topN() const
1350 {
1351 return _post_nms_topN;
1352 }
1353 /* Get the NMS overlap threshold */
1354 float nms_thres() const
1355 {
1356 return _nms_thres;
1357 }
1358 /* Get the minimal size */
1359 float min_size() const
1360 {
1361 return _min_size;
1362 }
1363 /* Get the spatial scale to be applied to the feature maps */
1364 float spatial_scale() const
1365 {
1366 return _spatial_scale;
1367 }
1368 /* Get the values used to represent a ROI(Region of interest)*/
1369 size_t values_per_roi() const
1370 {
1371 return _values_per_roi;
1372 }
1373
1374private:
1375 float _im_height;
1376 float _im_width;
1377 float _im_scale;
1378 float _spatial_scale;
1379 int _pre_nms_topN;
1380 int _post_nms_topN;
1381 float _nms_thres;
1382 float _min_size;
1383 size_t _values_per_roi;
1384};
1385
1386/** ComputeAnchors information class */
1387class ComputeAnchorsInfo
1388{
1389public:
1390 /** Constructor
1391 *
1392 * @param[in] feat_width Feature map width
1393 * @param[in] feat_height Feature map height
1394 * @param[in] spatial_scale Feature map scale
1395 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region Of Interest). Defaults to 4
1396 */
1397 ComputeAnchorsInfo(float feat_width, float feat_height, float spatial_scale, size_t values_per_roi = 4)
1398 : _feat_height(feat_height),
1399 _feat_width(feat_width),
1400 _spatial_scale(spatial_scale),
1401 _values_per_roi(values_per_roi)
1402 {
1403 }
1404
1405 /* Get the height of the feature map */
1406 float feat_height() const
1407 {
1408 return _feat_height;
1409 }
1410
1411 /* Get the width of the feature map */
1412 float feat_width() const
1413 {
1414 return _feat_width;
1415 }
1416
1417 /* Get the scale of the feature map */
1418 float spatial_scale() const
1419 {
1420 return _spatial_scale;
1421 }
1422
1423 /* Get the values used to represent a ROI(Region Of Interest)*/
1424 size_t values_per_roi() const
1425 {
1426 return _values_per_roi;
1427 }
1428
1429private:
1430 float _feat_height;
1431 float _feat_width;
1432 float _spatial_scale;
1433 size_t _values_per_roi;
1434};
1435
giuros01c04a0e82018-10-03 12:44:35 +01001436/** Bounding Box Transform information class */
giuros01d696cb62018-11-16 10:39:59 +00001437class BoundingBoxTransformInfo final
giuros01c04a0e82018-10-03 12:44:35 +01001438{
1439public:
1440 /** Constructor
1441 *
giuros01d696cb62018-11-16 10:39:59 +00001442 * @param[in] img_width Width of the original image
1443 * @param[in] img_height Height, of the original image
1444 * @param[in] scale Scale of the original image
1445 * @param[in] apply_scale (Optional)Re-apply scaling after transforming the boxes. Defaults to false
1446 * @param[in] weights (Optional)Weights [wx, wy, ww, wh] for the deltas. Defaults to all ones
1447 * @param[in] correct_transform_coords (Optional)Correct bounding box transform coordinates. Defaults to false
1448 * @param[in] bbox_xform_clip (Optional)Minimum bounding box width and height after bounding box transformation in log-space. Defaults to log(1000/16)
giuros01c04a0e82018-10-03 12:44:35 +01001449 */
giuros01d696cb62018-11-16 10:39:59 +00001450 BoundingBoxTransformInfo(float img_width, float img_height, float scale, bool apply_scale = false, const std::array<float, 4> weights = { { 1.f, 1.f, 1.f, 1.f } }, bool correct_transform_coords =
1451 false,
1452 float bbox_xform_clip =
1453 4.135166556742356f)
1454 : _img_width(img_width), _img_height(img_height), _scale(scale), _apply_scale(apply_scale), _correct_transform_coords(correct_transform_coords), _weights(weights), _bbox_xform_clip(bbox_xform_clip)
giuros01c04a0e82018-10-03 12:44:35 +01001455 {
1456 }
1457
1458 std::array<float, 4> weights() const
1459 {
1460 return _weights;
1461 }
1462
1463 float bbox_xform_clip() const
1464 {
1465 return _bbox_xform_clip;
1466 }
1467
1468 float img_height() const
1469 {
1470 return _img_height;
1471 }
1472
1473 float img_width() const
1474 {
1475 return _img_width;
1476 }
1477
1478 float scale() const
1479 {
1480 return _scale;
1481 }
1482
1483 bool apply_scale() const
1484 {
1485 return _apply_scale;
1486 }
1487
giuros01d696cb62018-11-16 10:39:59 +00001488 bool correct_transform_coords() const
1489 {
1490 return _correct_transform_coords;
1491 }
1492
giuros01c04a0e82018-10-03 12:44:35 +01001493private:
1494 float _img_width;
1495 float _img_height;
1496 float _scale;
1497 bool _apply_scale;
giuros01d696cb62018-11-16 10:39:59 +00001498 bool _correct_transform_coords;
giuros01c04a0e82018-10-03 12:44:35 +01001499 std::array<float, 4> _weights;
1500 float _bbox_xform_clip;
1501};
1502
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001503/** Activation Layer Information class */
1504class ActivationLayerInfo
1505{
1506public:
1507 /** Available activation functions */
1508 enum class ActivationFunction
1509 {
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001510 LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */
1511 TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */
1512 RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */
1513 BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */
1514 LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */
Manuel Bottini581c8982019-02-07 10:31:57 +00001515 LEAKY_RELU, /**< Leaky Rectifier ( \f$ f(x) = \begin{cases} \alpha x & \quad \text{if } x \text{ < 0}\\ x & \quad \text{if } x \geq \text{ 0 } \end{cases} \f$ ) */
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001516 SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +01001517 ELU, /**< Exponential Linear Unit ( \f$ f(x) = \begin{cases} \alpha (exp(x) - 1) & \quad \text{if } x \text{ < 0}\\ x & \quad \text{if } x \geq \text{ 0 } \end{cases} \f$ ) */
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001518 ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */
1519 SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/
1520 SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/
Usama Arif6a98a6e2019-05-10 17:07:27 +01001521 LINEAR, /**< Linear ( \f$ f(x)= ax + b \f$ ) */
morgolock07df3d42020-02-27 11:46:28 +00001522 IDENTITY, /**< Identity ( \f$ f(x)= x \f$ ) */
1523 HARD_SWISH /**< Hard-swish ( \f$ f(x) = (x * relu6(x+3))/6 \f$ ) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001524 };
1525
Giorgio Arena11674872018-02-07 15:38:12 +00001526 ActivationLayerInfo() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001527 /** Default Constructor
1528 *
1529 * @param[in] f The activation function to use.
1530 * @param[in] a (Optional) The alpha parameter used by some activation functions
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001531 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
1532 * @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 +01001533 */
1534 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f)
Giorgio Arena11674872018-02-07 15:38:12 +00001535 : _act(f), _a(a), _b(b), _enabled(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001536 {
1537 }
Alex Gildayc357c472018-03-21 13:54:09 +00001538 /** Get the type of activation function */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001539 ActivationFunction activation() const
1540 {
1541 return _act;
1542 }
Alex Gildayc357c472018-03-21 13:54:09 +00001543 /** Get the alpha value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001544 float a() const
1545 {
1546 return _a;
1547 }
Alex Gildayc357c472018-03-21 13:54:09 +00001548 /** Get the beta value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001549 float b() const
1550 {
1551 return _b;
1552 }
Alex Gildayc357c472018-03-21 13:54:09 +00001553 /** Check if initialised */
Giorgio Arena11674872018-02-07 15:38:12 +00001554 bool enabled() const
1555 {
1556 return _enabled;
1557 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001558
1559private:
Usama Arif6a98a6e2019-05-10 17:07:27 +01001560 ActivationFunction _act = { ActivationLayerInfo::ActivationFunction::IDENTITY };
Giorgio Arena11674872018-02-07 15:38:12 +00001561 float _a = {};
1562 float _b = {};
1563 bool _enabled = { false };
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001564};
1565
Giorgio Arena1856ff72020-02-07 13:46:45 +00001566/** Fully connected layer info */
1567struct FullyConnectedLayerInfo
1568{
Michele Di Giorgioc3f459d2021-05-05 15:42:20 +01001569 /* Fused-activation parameters */
1570 ActivationLayerInfo activation_info{}; /**< Fused activation to apply after the matrix multiplication. */
1571 /* Information about weights */
1572 DataLayout weights_trained_layout{ DataLayout::NCHW }; /**< Layout that the weights have been trained with. */
1573 bool transpose_weights{ true }; /**< Transpose weights if true. */
1574 bool are_weights_reshaped{ false }; /**< Reshape the weights tensor if false. */
1575 bool retain_internal_weights{ false }; /**< Retain internal reshaped weights. */
Michele Di Giorgioc3f459d2021-05-05 15:42:20 +01001576 /* Other parameters */
1577 bool fp_mixed_precision{ false }; /**< Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy. */
Giorgio Arena1856ff72020-02-07 13:46:45 +00001578
1579 /** Sets the weights trained data layout
1580 *
1581 * @param[in] layout Data layout that the weights were trained with
1582 *
1583 * @return Updated object
1584 */
1585 FullyConnectedLayerInfo &set_weights_trained_layout(DataLayout layout)
1586 {
1587 weights_trained_layout = layout;
1588 return *this;
1589 }
1590 /** Sets the transpose weights flag
1591 *
1592 * @param[in] should_transpose_weights Boolean flag indicating if weights should be transposed
1593 *
1594 * @return Updated object
1595 */
1596 FullyConnectedLayerInfo &set_transpose_weights(bool should_transpose_weights)
1597 {
1598 transpose_weights = should_transpose_weights;
1599 return *this;
1600 }
1601};
1602
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001603/** Normalization Layer Information class */
1604class NormalizationLayerInfo
1605{
1606public:
1607 /** Default Constructor
1608 *
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001609 * @param[in] type The normalization type. Can be @ref NormType::IN_MAP_1D, @ref NormType::IN_MAP_2D or @ref NormType::CROSS_MAP
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001610 * @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 +00001611 * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001.
1612 * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5.
1613 * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
1614 * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not.
1615 * Should be false to follow [Krichevksy 2012].
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001616 */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001617 NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true)
1618 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001619 {
1620 }
Alex Gildayc357c472018-03-21 13:54:09 +00001621 /** Get the normalization type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001622 NormType type() const
1623 {
1624 return _type;
1625 }
Alex Gildayc357c472018-03-21 13:54:09 +00001626 /** Get the normalization size */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001627 uint32_t norm_size() const
1628 {
1629 return _norm_size;
1630 }
Alex Gildayc357c472018-03-21 13:54:09 +00001631 /** Get the alpha value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001632 float alpha() const
1633 {
1634 return _alpha;
1635 }
Alex Gildayc357c472018-03-21 13:54:09 +00001636 /** Get the beta value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001637 float beta() const
1638 {
1639 return _beta;
1640 }
Alex Gildayc357c472018-03-21 13:54:09 +00001641 /** Get the kappa value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001642 float kappa() const
1643 {
1644 return _kappa;
1645 }
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001646 /** Get the is_scaled value */
1647 bool is_scaled() const
1648 {
1649 return _is_scaled;
1650 }
Alex Gildayc357c472018-03-21 13:54:09 +00001651 /** Check if normalization is cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001652 bool is_cross_map() const
1653 {
1654 return _type == NormType::CROSS_MAP;
1655 }
Alex Gildayc357c472018-03-21 13:54:09 +00001656 /** Check if normalization is not cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001657 bool is_in_map() const
1658 {
1659 return !is_cross_map();
1660 }
1661 /** Return the scaling factor of the normalization function.
1662 *
1663 * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed,
1664 * 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 +01001665 *
1666 * @return The normalization scaling factor.
1667 */
1668 float scale_coeff() const
1669 {
1670 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001671 return (_is_scaled) ? (_alpha / size) : _alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001672 }
1673
1674private:
1675 NormType _type;
1676 uint32_t _norm_size;
1677 float _alpha;
1678 float _beta;
1679 float _kappa;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001680 bool _is_scaled;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001681};
1682
thecha012bfadd92020-08-12 17:25:51 +01001683class StridedSliceLayerInfo
1684{
1685public:
1686 /** Default Constructor
1687 *
1688 * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
1689 * @param[in] end_mask (Optional) If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
1690 * @param[in] shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
1691 */
1692 StridedSliceLayerInfo(int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0)
1693 : _begin_mask(begin_mask), _end_mask(end_mask), _shrink_axis_mask(shrink_axis_mask)
1694 {
1695 }
1696
1697 /* Get the begin mask value */
1698 int32_t begin_mask() const
1699 {
1700 return _begin_mask;
1701 }
1702
1703 /* Get the end mask value */
1704 int32_t end_mask() const
1705 {
1706 return _end_mask;
1707 }
1708
1709 /* Get the shrink axis mask value */
1710 int32_t shrink_axis_mask() const
1711 {
1712 return _shrink_axis_mask;
1713 }
1714
1715private:
1716 int32_t _begin_mask;
1717 int32_t _end_mask;
1718 int32_t _shrink_axis_mask;
1719};
1720
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001721/** 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 +01001722class WeightsInfo
1723{
1724public:
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001725 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001726 WeightsInfo()
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001727 : _are_reshaped(false), _kernel_width(0), _kernel_height(0), _num_kernels(0), _retain_internal_weights(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001728 {
1729 }
1730 /** Constructor
1731 *
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001732 * @param[in] are_reshaped True if the weights have been reshaped
1733 * @param[in] kernel_width Kernel width.
1734 * @param[in] kernel_height Kernel height.
1735 * @param[in] num_kernels Number of convolution kernels.
1736 * @param[in] retain_internal_weights (Optional) True if internal reshaped weights must be retained. Used for reconfiguration purposes. Default is false.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001737 */
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001738 WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels, bool retain_internal_weights = false)
1739 : _are_reshaped(are_reshaped), _kernel_width(kernel_width), _kernel_height(kernel_height), _num_kernels(num_kernels), _retain_internal_weights(retain_internal_weights)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001740 {
1741 }
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001742 /** Flag which specifies if the weights tensor has been reshaped.
1743 *
1744 * @return True if the weights tensors has been reshaped
1745 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001746 bool are_reshaped() const
1747 {
1748 return _are_reshaped;
1749 };
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001750 /** Return the number of convolution kernels
1751 *
1752 * @return The number of convolution kernels
1753 */
1754 unsigned int num_kernels() const
1755 {
1756 return _num_kernels;
1757 };
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001758 /** Return the width and height of the kernel
1759 *
1760 * @return The width and height of the kernel
1761 */
1762 std::pair<unsigned int, unsigned int> kernel_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001763 {
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001764 return std::make_pair(_kernel_width, _kernel_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001765 }
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001766 bool retain_internal_weights() const
1767 {
1768 return _retain_internal_weights;
1769 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001770
1771private:
SiCong Liebd8fb42020-08-18 11:03:14 +01001772 bool _are_reshaped;
1773 unsigned int _kernel_width;
1774 unsigned int _kernel_height;
1775 unsigned int _num_kernels;
1776 bool _retain_internal_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001777};
1778
Gian Marco36a0a462018-01-12 10:21:40 +00001779/** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape.
1780 *
Michele Di Giorgio93b75e02021-06-21 12:00:43 +01001781 * The matrix A can only be reshaped through @ref opencl::kernels::ClGemmReshapeLhsMatrixKernel or @ref cpu::kernels::CpuGemmInterleave4x4Kernel
Georgios Pinitas856f66e2021-04-22 21:13:21 +01001782 * Note: Optionally just for @ref opencl::kernels::ClGemmReshapeLhsMatrixKernel is it possible to set mult_interleave4x4_height, the multiplication factor for the height of the 4x4 interleaved block
Gian Marco36a0a462018-01-12 10:21:40 +00001783 *
Michele Di Giorgio93b75e02021-06-21 12:00:43 +01001784 * The matrix B can only be reshaped through @ref opencl::kernels::ClGemmReshapeRhsMatrixKernel or @ref cpu::kernels::CpuGemmTranspose1xWKernel
Georgios Pinitas856f66e2021-04-22 21:13:21 +01001785 * Note: Optionally just for @ref opencl::kernels::ClGemmReshapeRhsMatrixKernel is it possible to set mult_transpose1xW_width, the multiplication factor for the width of the 1xW transposed block
Gian Marco36a0a462018-01-12 10:21:40 +00001786 *
1787 */
1788class GEMMReshapeInfo final
1789{
1790public:
1791 /** Default constructor */
1792 GEMMReshapeInfo()
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001793 : _m(1), _n(1), _k(1), _mult_transpose1xW_width(1), _mult_interleave4x4_height(1), _depth_output_gemm3d(0), _reinterpret_input_as_3d(false), _broadcast_bias(false)
Gian Marco36a0a462018-01-12 10:21:40 +00001794 {
1795 }
1796 /** Constructor
1797 *
1798 * @param[in] m Number of matrix A rows
1799 * @param[in] n Number of matrix B columns
1800 * @param[in] k Number of matrix A columns or matrix B rows
1801 * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block
1802 * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001803 * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel.
1804 * If 0 the output will not be reinterpreted as 3D. Default 0
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001805 * @param[in] reinterpret_input_as_3d (Optional) Reinterpret the input as 3D tensor. (i.e. this flag should be set to true when GEMM is used
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001806 * to perform 1x1 convolutions with the NHWC data layout)
1807 * @param[in] broadcast_bias (Optional) Broadcast the shape of the bias tensor from a vector to a matrix.
Gian Marco36a0a462018-01-12 10:21:40 +00001808 */
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001809 GEMMReshapeInfo(int m, int n, int k, int mult_transpose1xW_width = 1, int mult_interleave4x4_height = 1, int depth_output_gemm3d = 0, bool reinterpret_input_as_3d = false, bool broadcast_bias = false)
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001810 : _m(m), _n(n), _k(k), _mult_transpose1xW_width(mult_transpose1xW_width), _mult_interleave4x4_height(mult_interleave4x4_height), _depth_output_gemm3d(depth_output_gemm3d),
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001811 _reinterpret_input_as_3d(reinterpret_input_as_3d), _broadcast_bias(broadcast_bias)
Gian Marco36a0a462018-01-12 10:21:40 +00001812 {
1813 }
1814 /** Number of matrix A rows
1815 *
1816 * @return the number of matrix A rows
1817 */
1818 int m() const
1819 {
1820 return _m;
1821 }
1822 /** Number of matrix B columns
1823 *
1824 * @return the number of matrix B columns
1825 */
1826 int n() const
1827 {
1828 return _n;
1829 }
1830 /** Number of matrix A columns or matrix B rows
1831 *
1832 * @return the number of matrix A columns or matrix B rows
1833 */
1834 int k() const
1835 {
1836 return _k;
1837 }
1838 /** Multiplication factor for the width of the 1xW transposed block
1839 *
1840 * @return the multiplication factor for the width of the 1xW transposed block
1841 */
1842 int mult_transpose1xW_width() const
1843 {
1844 return _mult_transpose1xW_width;
1845 }
1846 /** Multiplication factor for the height of the 4x4 interleaved block
1847 *
1848 * @return the multiplication factor for the height of the 4x4 interleaved block
1849 */
1850 int mult_interleave4x4_height() const
1851 {
1852 return _mult_interleave4x4_height;
1853 }
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001854 /** Depth (third dimension) of the output tensor to be used with the GEMM3D kernel
1855 *
1856 * @note GEMM3D kernel is used when the output has to be reinterpret as 3D tensor. In that case:
1857 * m = depth_output_gemm3d * output_height
1858 *
1859 * @return the depth of the output tensor to be used with the GEMM3D kernel
1860 */
1861 int depth_output_gemm3d() const
1862 {
1863 return _depth_output_gemm3d;
1864 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001865 /** Flag which specifies if the input tensor has to be reinterpreted as 3D
1866 *
1867 * @return True if the input tensor has to be reinterpreted as 3D tensor
1868 */
1869 bool reinterpret_input_as_3d() const
1870 {
1871 return _reinterpret_input_as_3d;
1872 };
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001873 /** Flag which specifies whether to broadcast the shape of the bias tensor.
1874 *
1875 * @return True if the shape of the bias tensor is to be broadcasted.
1876 */
1877 bool broadcast_bias() const
1878 {
1879 return _broadcast_bias;
1880 };
Gian Marco36a0a462018-01-12 10:21:40 +00001881
1882private:
SiCong Liebd8fb42020-08-18 11:03:14 +01001883 int _m;
1884 int _n;
1885 int _k;
1886 int _mult_transpose1xW_width;
1887 int _mult_interleave4x4_height;
1888 int _depth_output_gemm3d;
1889 bool _reinterpret_input_as_3d;
1890 bool _broadcast_bias;
Gian Marco36a0a462018-01-12 10:21:40 +00001891};
1892
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +01001893struct ConvolutionInfo
1894{
1895 ConvolutionInfo() = default;
1896 ConvolutionInfo(const PadStrideInfo &pad_stride_info, unsigned int depth_multiplier, const ActivationLayerInfo &act_info, const Size2D &dilation)
1897 : pad_stride_info(pad_stride_info), depth_multiplier(depth_multiplier), act_info(act_info), dilation(dilation)
1898 {
1899 }
1900 PadStrideInfo pad_stride_info{}; /**< Convolution info (Pads, strides,...) */
1901 unsigned int depth_multiplier{ 1 }; /**< Multiplier to apply to input's depth to retrieve the output depth. Defaults to 1 */
1902 ActivationLayerInfo act_info{}; /**< Fused activation to apply after convolution. */
1903 Size2D dilation{ Size2D(1, 1) }; /**< Dilation, in elements, across x and y. Defaults to (1, 1). */
1904};
1905
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001906/** GEMMLowp output stage type */
1907enum class GEMMLowpOutputStageType
1908{
Manuel Bottini959c26d2019-12-02 16:22:35 +00001909 NONE, /**< No quantization */
1910 QUANTIZE_DOWN, /**< Quantize using an integer multiplication */
1911 QUANTIZE_DOWN_FIXEDPOINT, /**< Quantize using a fixed point multiplication */
1912 QUANTIZE_DOWN_FLOAT /**< Quantize using a floating point multiplication */
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001913};
1914
1915/** GEMMLowp output stage info */
1916struct GEMMLowpOutputStageInfo
1917{
Giorgio Arena1856ff72020-02-07 13:46:45 +00001918 GEMMLowpOutputStageType type{ GEMMLowpOutputStageType::NONE }; /**< GEMMLowp output stage type */
1919 int32_t gemmlowp_offset{ 0 }; /**< GEMMLowp output stage offset used for quantizing to QASYMM8 */
1920 int32_t gemmlowp_multiplier{ 0 }; /**< GEMMLowp output stage multiplier used for quantizing to QASYMM8 */
1921 int32_t gemmlowp_shift{ 0 }; /**< GEMMLowp output stage shift used for quantizing to uint8 */
1922 int32_t gemmlowp_min_bound{ std::numeric_limits<int32_t>::lowest() }; /**< GEMMLowp min value used to saturate down the output result before converting back to QASYMM8 */
1923 int32_t gemmlowp_max_bound{ std::numeric_limits<int32_t>::max() }; /**< GEMMLowp max value used to saturate down the output result before converting back to QASYMM8 */
1924 std::vector<int32_t> gemmlowp_multipliers{}; /**< GEMMLowp output stage multiplier used for quantizing to QASYMM8 */
1925 std::vector<int32_t> gemmlowp_shifts{}; /**< GEMMLowp output stage multiplier used for quantizing to QASYMM8 */
Sheri Zhang1b14c752020-03-09 14:29:52 +00001926 float gemmlowp_real_multiplier{ 0 }; /**< GEMMLowp output stage real multiplier used for quantizing to QASYMM8 */
Giorgio Arena1856ff72020-02-07 13:46:45 +00001927 bool is_quantized_per_channel{ false }; /**< GEMMLowp quantized per-channel flag */
1928 DataType output_data_type{ DataType::UNKNOWN }; /**< Output tensor data type to use if the output is not initialized */
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001929};
1930
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +00001931/** GEMM LHS (Left Hand Side) matrix information */
1932struct GEMMLHSMatrixInfo
1933{
morgolockaba2f912020-05-05 16:28:19 +01001934 GEMMLHSMatrixInfo() = default;
1935 GEMMLHSMatrixInfo(unsigned int m, unsigned int k, unsigned int v, bool trans, bool inter)
1936 : m0(m), k0(k), v0(v), transpose(trans), interleave(inter)
1937 {
1938 }
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +00001939 unsigned int m0{ 1 }; /**< Number of rows processed by the matrix multiplication */
1940 unsigned int k0{ 1 }; /**< Number of partial accumulations performed by the matrix multiplication */
1941 unsigned int v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */
1942 bool transpose{ true }; /**< True if the (m0xk0) block has to be transposed before been stored */
1943 bool interleave{ true }; /**< True if the v0 (m0xk0) blocks have to be interleaved in the output row */
1944};
1945
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00001946/** GEMM RHS (Right Hand Side) matrix information */
1947struct GEMMRHSMatrixInfo
1948{
morgolockaba2f912020-05-05 16:28:19 +01001949 GEMMRHSMatrixInfo() = default;
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +01001950 GEMMRHSMatrixInfo(unsigned int n, unsigned int k, unsigned int h, bool trans, bool inter, bool export_to_cl_img)
1951 : n0(n), k0(k), h0(h), transpose(trans), interleave(inter), export_to_cl_image(export_to_cl_img)
morgolockaba2f912020-05-05 16:28:19 +01001952 {
1953 }
Gian Marco Iodicedd717c32020-05-28 10:22:03 +01001954 unsigned int n0{ 1 }; /**< Number of columns processed by the matrix multiplication */
1955 unsigned int k0{ 1 }; /**< Number of partial accumulations performed by the matrix multiplication */
1956 unsigned int h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
1957 bool transpose{ true }; /**< True if the (k0xn0) block has to be transposed before been stored */
1958 bool interleave{ true }; /**< True if the h0 (k0xn0) blocks have to be interleaved in the output row */
1959 bool export_to_cl_image{ false }; /**< True if the reshaped rhs has to be exported to cl_image. n0 must be equal to 4 */
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00001960};
1961
Gian Marco36a0a462018-01-12 10:21:40 +00001962/** GEMM information class. This class stores the necessary information to compute GEMM functions
1963 *
1964 * This object also contains the information about how matrix A and matrix B have been reshaped
1965 *
1966 */
Chunosov5124be52017-11-22 20:42:13 +07001967class GEMMInfo
1968{
1969public:
1970 /** Default constructor */
Georgios Pinitas37d080f2019-06-21 18:43:12 +01001971 GEMMInfo() noexcept
1972 : _is_a_reshaped(false),
1973 _is_b_reshaped(false),
1974 _reshape_b_only_on_first_run(true),
1975 _depth_output_gemm3d(0),
1976 _reinterpret_input_as_3d(false),
1977 _retain_internal_weights(false),
1978 _gemmlowp_output_stage(),
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01001979 _fast_math(false),
Georgios Pinitas37d080f2019-06-21 18:43:12 +01001980 _fp_mixed_precision(false),
1981 _broadcast_bias(false),
ramelg01cbbb0382021-09-17 17:36:57 +01001982 _pretranspose_B(true),
Giorgio Arena63e0beb2021-09-24 14:04:27 +01001983 _activation_info()
Chunosov5124be52017-11-22 20:42:13 +07001984 {
1985 }
1986 /** Constructor
1987 *
1988 * @param[in] is_a_reshaped True if the matrix A has been reshaped
1989 * @param[in] is_b_reshaped True if the matrix B has been reshaped
1990 * @param[in] reshape_b_only_on_first_run Reshape matrix B only for the first run
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001991 * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001992 * If 0 the output will not be reinterpreted as 3D. Default 0
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001993 * @param[in] reinterpret_input_as_3d (Optional) Reinterpret the input as 3D tensor. (i.e. this flag should be set to true when GEMM is used
1994 * to perform 1x1 convolutions with the NHWC data layout)
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +01001995 * @param[in] retain_internal_weights (Optional) Retain the weights tensor from previous run
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001996 * @param[in] gemmlowp_output_stage (Optional) GEMMLowp Output stage info
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001997 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01001998 * @param[in] fast_math (Optional) Use a data type of shorter width to improve performance
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001999 * @param[in] broadcast_bias (Optional) Broadcast the shape of the bias tensor from a vector to a matrix.
Gian Marco Iodicef3622be2019-07-29 14:27:16 +01002000 * @param[in] activation_info (Optional) Activation to apply after the matrix multiplication
Chunosov5124be52017-11-22 20:42:13 +07002001 */
Gian Marco Iodice3139f032018-11-05 14:26:32 +00002002 GEMMInfo(bool is_a_reshaped, bool is_b_reshaped, bool reshape_b_only_on_first_run, int depth_output_gemm3d = 0, bool reinterpret_input_as_3d = false, bool retain_internal_weights = false,
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01002003 GEMMLowpOutputStageInfo gemmlowp_output_stage = GEMMLowpOutputStageInfo(), bool fp_mixed_precision = false, bool fast_math = false, bool broadcast_bias = false,
Giorgio Arena63e0beb2021-09-24 14:04:27 +01002004 const ActivationLayerInfo &activation_info = ActivationLayerInfo()) noexcept
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002005 : _is_a_reshaped(is_a_reshaped),
2006 _is_b_reshaped(is_b_reshaped),
2007 _reshape_b_only_on_first_run(reshape_b_only_on_first_run),
2008 _depth_output_gemm3d(depth_output_gemm3d),
2009 _reinterpret_input_as_3d(reinterpret_input_as_3d),
2010 _retain_internal_weights(retain_internal_weights),
2011 _gemmlowp_output_stage(gemmlowp_output_stage),
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01002012 _fast_math(fast_math),
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002013 _fp_mixed_precision(fp_mixed_precision),
2014 _broadcast_bias(broadcast_bias),
ramelg01cbbb0382021-09-17 17:36:57 +01002015 _pretranspose_B(reshape_b_only_on_first_run),
Giorgio Arena63e0beb2021-09-24 14:04:27 +01002016 _activation_info(activation_info)
Chunosov5124be52017-11-22 20:42:13 +07002017 {
2018 }
2019 /** Flag which specifies if the matrix A has been reshaped
2020 *
2021 * @return True if the matrix A has been reshaped
2022 */
2023 bool is_a_reshaped() const
2024 {
2025 return _is_a_reshaped;
2026 };
2027 /** Flag which specifies if the matrix B has been reshaped
2028 *
2029 * @return True if the matrix B has been reshaped
2030 */
2031 bool is_b_reshaped() const
2032 {
2033 return _is_b_reshaped;
2034 };
2035 /** Flag which specifies if the reshape of matrix B should executed only for the first
2036 *
2037 * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer
2038 *
2039 * @return True if the reshaped of matrix B happens only for the first run
2040 */
2041 bool reshape_b_only_on_first_run() const
2042 {
2043 return _reshape_b_only_on_first_run;
2044 };
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002045 /** Depth of the output when GEMM output is reinterpreted as 3D tensor
Gian Marco36a0a462018-01-12 10:21:40 +00002046 *
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002047 * @return the depth of the output tensor
Gian Marco36a0a462018-01-12 10:21:40 +00002048 */
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002049 int depth_output_gemm3d() const
Gian Marco36a0a462018-01-12 10:21:40 +00002050 {
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002051 return _depth_output_gemm3d;
2052 };
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01002053 /** Flag which specifies if the input tensor has to be reinterpreted as 3D
2054 *
2055 * @return True if the input tensor has to be reinterpreted as 3D tensor
2056 */
2057 bool reinterpret_input_as_3d() const
2058 {
2059 return _reinterpret_input_as_3d;
2060 };
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +01002061 /** Flag which specifies if the weights tensor has to be retained from previous run
2062 *
2063 * @return True if the weights tensor has to be retained
2064 */
2065 bool retain_internal_weights() const
2066 {
2067 return _retain_internal_weights;
2068 };
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002069 /** GEMMLowp output stage
2070 *
2071 * @return the GEMMLowp output stage info
2072 */
2073 GEMMLowpOutputStageInfo gemmlowp_output_stage() const
2074 {
2075 return _gemmlowp_output_stage;
2076 };
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01002077 /** Sets GEMMLowp output stage
2078 *
2079 * @param[in] output_stage Output stage to set
2080 */
2081 void set_gemmlowp_output_stage(GEMMLowpOutputStageInfo &output_stage)
2082 {
2083 _gemmlowp_output_stage = output_stage;
2084 };
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00002085 /** Flag which specifies if a wider accumulator should be used.
2086 *
2087 * @return True if a wider accumulator has to be used
2088 */
2089 bool fp_mixed_precision() const
2090 {
2091 return _fp_mixed_precision;
2092 };
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01002093 /** Flag which specifies if a shorter accumulator to be used.
2094 *
2095 * @return True if a shorter accumulator has to be used
2096 */
2097 bool fast_math() const
2098 {
2099 return _fast_math;
2100 };
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01002101 /** Flag which specifies whether to broadcast the shape of the bias tensor.
2102 *
2103 * @return True if the shape of the bias tensor is to be broadcasted.
2104 */
2105 bool broadcast_bias() const
2106 {
2107 return _broadcast_bias;
2108 };
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002109 /** Flag which specifies whether b should be pre-transposed if supported.
2110 *
2111 * @return True if b should be pre-transposed else false.
2112 */
ramelg01cbbb0382021-09-17 17:36:57 +01002113 bool pretranspose_B() const
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002114 {
ramelg01cbbb0382021-09-17 17:36:57 +01002115 return _pretranspose_B;
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002116 };
2117 /** Set pre-transpose b flag
2118 *
2119 * @param[in] flag Flag to set
2120 */
ramelg01cbbb0382021-09-17 17:36:57 +01002121 void set_pretranspose_B(bool flag)
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002122 {
ramelg01cbbb0382021-09-17 17:36:57 +01002123 _pretranspose_B = flag;
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002124 }
Gian Marco Iodicef3622be2019-07-29 14:27:16 +01002125 /** Activation layer to apply after the matrix multiplication
2126 *
2127 * @return ActivationLayerInfo object
2128 */
2129 ActivationLayerInfo activation_info() const
2130 {
2131 return _activation_info;
2132 }
SiCongLi2e5fd632020-03-02 15:39:15 +00002133 /** Set activation layer info
2134 *
2135 * @param[in] activation_info ActivationLayerInfo object to set
2136 */
2137 void set_activation_info(const ActivationLayerInfo &activation_info)
2138 {
2139 _activation_info = activation_info;
2140 }
Chunosov5124be52017-11-22 20:42:13 +07002141
2142private:
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002143 bool _is_a_reshaped;
2144 bool _is_b_reshaped;
2145 bool _reshape_b_only_on_first_run;
2146 int _depth_output_gemm3d;
2147 bool _reinterpret_input_as_3d;
2148 bool _retain_internal_weights;
2149 GEMMLowpOutputStageInfo _gemmlowp_output_stage;
Georgios Pinitas4ee8b152021-07-16 16:16:43 +01002150 bool _fast_math;
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002151 bool _fp_mixed_precision;
2152 bool _broadcast_bias;
ramelg01cbbb0382021-09-17 17:36:57 +01002153 bool _pretranspose_B;
Gian Marco Iodicef3622be2019-07-29 14:27:16 +01002154 ActivationLayerInfo _activation_info;
Chunosov5124be52017-11-22 20:42:13 +07002155};
2156
Gian Marco Iodice247f52c2018-03-22 11:24:56 +00002157/** Winograd information */
2158struct WinogradInfo
2159{
2160 /** Default constructor
2161 *
2162 * @param[in] output_tile_sz Width and height of the output tile
2163 * @param[in] kernel_sz Width and height of the kernel
2164 * @param[in] input_dims Width and height of the input tensor before the convolution is applied
2165 * @param[in] conv_info Convolution info (Pads, strides)
2166 * @param[in] data_layout Data layout to use for the output tensor once the convolution has been applied
2167 */
2168 WinogradInfo(Size2D output_tile_sz, Size2D kernel_sz, Size2D input_dims, PadStrideInfo conv_info, DataLayout data_layout)
2169 : output_tile_size(output_tile_sz), kernel_size(kernel_sz), input_dimensions(input_dims), convolution_info(conv_info), output_data_layout(data_layout)
2170 {
2171 }
2172
2173 Size2D output_tile_size{}; /**< Width and height of the output tile */
2174 Size2D kernel_size{}; /**< Width and height of the kernel*/
2175 Size2D input_dimensions{}; /**< Width and height of the input tensor before the convolution is applied */
2176 PadStrideInfo convolution_info{}; /**< Convolution info (Pads, strides,...) */
2177 DataLayout output_data_layout{ DataLayout::NCHW }; /**< Data layout to use for the output tensor once the convolution has been applied (NCHW or NHWC) */
2178};
2179
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002180/** IO formatting information class*/
2181struct IOFormatInfo
2182{
2183 /** Precision type used when printing floating point numbers */
2184 enum class PrecisionType
2185 {
2186 Default, /**< Default precision to the one that the current stream has */
2187 Custom, /**< Custom precision specified by the user using the precision parameter */
2188 Full /**< The maximum precision of the floating point representation */
2189 };
2190
2191 /** Specifies the area to be printed, used by Tensor objects */
2192 enum class PrintRegion
2193 {
2194 ValidRegion, /**< Prints the valid region of the Tensor object */
2195 NoPadding, /**< Prints the Tensor object without the padding */
2196 Full /**< Print the tensor object including padding */
2197 };
2198
Alex Gildayc357c472018-03-21 13:54:09 +00002199 /** Construct a set of IO formatting information.
2200 *
2201 * @param[in] print_region Area to be printed. Used by Tensor objects. Default: ValidRegion.
2202 * @param[in] precision_type Precision type for floating point numbers. Default: stream default.
2203 * @param[in] precision Precision value for float point numbers. Default: 10.
2204 * @param[in] align_columns Whether to align columns when printed. Default: true.
2205 * @param[in] element_delim Delimeter between elements. Default: " ".
2206 * @param[in] row_delim Delimenter between rows. Default: "\n".
2207 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002208 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
2209 PrecisionType precision_type = PrecisionType::Default,
2210 unsigned int precision = 10,
2211 bool align_columns = true,
2212 std::string element_delim = " ",
2213 std::string row_delim = "\n")
2214 : print_region(print_region),
2215 precision_type(precision_type),
2216 precision(precision),
2217 element_delim(element_delim),
2218 row_delim(row_delim),
2219 align_columns(align_columns)
2220 {
2221 }
2222
Alex Gildayc357c472018-03-21 13:54:09 +00002223 /** Area to be printed by Tensor objects */
2224 PrintRegion print_region;
2225 /** Floating point precision type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002226 PrecisionType precision_type;
Alex Gildayc357c472018-03-21 13:54:09 +00002227 /** Floating point precision */
2228 unsigned int precision;
2229 /** Element delimeter */
2230 std::string element_delim;
2231 /** Row delimeter */
2232 std::string row_delim;
2233 /** Align columns */
2234 bool align_columns;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002235};
Georgios Pinitasd8734b52017-12-22 15:27:52 +00002236} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +00002237#endif /* ARM_COMPUTE_TYPES_H */