blob: f2f60c150e3f7ffca8ea52bd6c2fffe9780a75ad [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Mohammed Suhail Munshi8609ca02024-02-29 17:00:07 +00002 * Copyright (c) 2016-2024 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 */
Jakub Sujak0d27b2e2023-08-24 14:01:20 +010024#ifndef ACL_ARM_COMPUTE_CORE_TYPES_H
25#define ACL_ARM_COMPUTE_CORE_TYPES_H
SiCong Li91295492023-07-21 18:16:13 +010026
27/** The following symbols have been moved to:
28 * half
29 * PermutationVector
30 * Format
31 * DataType
32 * DataLayout
33 * DataLayoutDimension
34 * PadStrideInfo
35 * WeightFormat
36 * Channel
37 * DimensionRoundingType
38 */
39#include "arm_compute/core/CoreTypes.h"
40/** The following symbols have been moved to:
41 * ActivationFunction
42 * ActivationLayerInfo
43 */
44#include "arm_compute/function_info/ActivationLayerInfo.h"
45/** The following symbols have been moved to:
46 * ConvolutionInfo
47 */
48#include "arm_compute/function_info/ConvolutionInfo.h"
49/** The following symbols have been moved to:
50 * FullyConnectedLayerInfo
51 */
52#include "arm_compute/function_info/FullyConnectedLayerInfo.h"
53/** The following symbols have been moved to:
54 * GEMMLowpOutputStageType
55 * GEMMLowpOutputStageInfo
56 * GEMMInfo
57 */
58#include "arm_compute/function_info/GEMMInfo.h"
59/** The following symbols have been moved to:
60 * MatMulInfo
61 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062#include "arm_compute/core/Coordinates.h"
Isabella Gottardi6e464c32018-01-26 12:32:45 +000063#include "arm_compute/core/Size2D.h"
Adnan AlSinane4563a02021-09-01 15:32:03 +010064#include "arm_compute/core/Size3D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065#include "arm_compute/core/TensorShape.h"
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000066#include "arm_compute/core/utils/misc/Macros.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067#include "arm_compute/function_info/MatMulInfo.h"
68
Georgios Pinitase8291ac2020-02-26 09:58:13 +000069#include "support/Bfloat16.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000071#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072#include <cstddef>
73#include <cstdint>
Isabella Gottardia7acb3c2019-01-08 13:48:44 +000074#include <map>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075#include <string>
76#include <utility>
77
78namespace arm_compute
79{
Georgios Pinitas77589b52018-08-21 14:41:35 +010080/** Bidirectional strides */
81using BiStrides = Coordinates;
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000082
Daniil Efremov02bf80d2017-11-22 00:26:51 +070083/** Available Sampling Policies */
84enum class SamplingPolicy
85{
86 CENTER, /**< Samples are taken at pixel center */
87 TOP_LEFT /**< Samples are taken at pixel top left corner */
88};
89
Georgios Pinitas7900a9e2018-11-23 11:44:58 +000090/** Available ConvolutionMethod*/
91enum class ConvolutionMethod
92{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000093 GEMM, /**< Convolution using GEMM */
94 GEMM_CONV2D, /**< Direct 2D GEMM convolution */
95 DIRECT, /**< Direct convolution */
Gian Marco Iodicea5cb79f2022-12-28 13:53:51 +000096 INDIRECT, /**< Indirect convolution */
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000097 WINOGRAD, /**< Convolution using Winograd */
98 FFT /**< Convolution using FFT */
Georgios Pinitas7900a9e2018-11-23 11:44:58 +000099};
100
Manuel Bottini05069f02019-09-26 17:18:26 +0100101/** Available DepthwiseConvolutionFunction*/
102enum class DepthwiseConvolutionFunction
103{
104 OPTIMIZED, /**< Optimized Depthwise Convolution */
105 GENERIC, /**< Generic Depthwise Convolution */
106};
107
giuros0146a49a02019-04-01 13:50:22 +0100108/** Available DeconvolutionMethod*/
109enum class DeconvolutionMethod
110{
SiCong Li4ceb4532023-03-13 15:02:23 +0000111 GEMM, /**< Deconvolution using GEMM */
112 DIRECT, /**< Direct deconvolution */
113 UPSCALE_CONV2D /**< Deconvolution with Upscaling */
giuros0146a49a02019-04-01 13:50:22 +0100114};
115
Manuel Bottini2732cca2019-05-28 11:44:41 +0100116/** Available FuseBatchNormalizationType*/
117enum class FuseBatchNormalizationType
118{
119 CONVOLUTION, /**< For Convolution weights */
120 DEPTHWISECONVOLUTION /**< For Depthwise Convolution weights*/
121};
122
Usama Arif89890c62019-03-19 10:57:05 +0000123/** Padding mode to use for PadLayer */
124enum class PaddingMode
125{
126 CONSTANT,
127 REFLECT,
128 SYMMETRIC
129};
130
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000131/** Supported comparison operations */
132enum class ComparisonOperation
133{
134 Equal, /**< Equal comparison ( \f$ x == y \f$ ) */
135 NotEqual, /**< NotEqual comparison ( \f$ x != y \f$ ) */
136 Greater, /**< Greater comparison ( \f$ x > y \f$ ) */
137 GreaterEqual, /**< Greater equal comparison ( \f$ x >= y \f$ ) */
138 Less, /**< Less comparison ( \f$ x < y \f$ ) */
139 LessEqual /**< Less equal comparison ( \f$ x <= y \f$ ) */
140};
141
Alex Gildayc357c472018-03-21 13:54:09 +0000142/** Container for valid region of a window */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143struct ValidRegion
144{
Alex Gildayc357c472018-03-21 13:54:09 +0000145 /** Default constructor */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100146 ValidRegion() : anchor{}, shape{}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 {
148 }
149
Alex Gildayc357c472018-03-21 13:54:09 +0000150 /** Allow instances of this class to be copy constructed */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 ValidRegion(const ValidRegion &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000152 /** Allow instances of this class to be move constructed */
153 ValidRegion(ValidRegion &&) = default;
154 /** Allow instances of this class to be copied */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 ValidRegion &operator=(const ValidRegion &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000156 /** Allow instances of this class to be moved */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 ValidRegion &operator=(ValidRegion &&) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000158 /** Default destructor */
159 ~ValidRegion() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
Alex Gildayc357c472018-03-21 13:54:09 +0000161 /** Constructor for a valid region with default number of dimensions
162 *
163 * @param[in] an_anchor Anchor for the start of the valid region.
164 * @param[in] a_shape Shape of the valid region.
165 *
166 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape) : anchor{an_anchor}, shape{a_shape}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000169 anchor.set_num_dimensions(std::max(anchor.num_dimensions(), shape.num_dimensions()));
170 }
171
Alex Gildayc357c472018-03-21 13:54:09 +0000172 /** Constructor for a valid region with specified number of dimensions
173 *
174 * @param[in] an_anchor Anchor for the start of the valid region.
175 * @param[in] a_shape Shape of the valid region.
176 * @param[in] num_dimensions Number of dimensions (must be >= number of dimensions of anchor and shape).
177 *
178 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000179 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape, size_t num_dimensions)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100180 : anchor{an_anchor}, shape{a_shape}
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000181 {
182 ARM_COMPUTE_ERROR_ON(num_dimensions < std::max(anchor.num_dimensions(), shape.num_dimensions()));
183 anchor.set_num_dimensions(num_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184 }
185
186 /** Return the start of the valid region for the given dimension @p d */
187 int start(unsigned int d) const
188 {
189 return anchor[d];
190 }
191
192 /** Return the end of the valid region for the given dimension @p d */
193 int end(unsigned int d) const
194 {
195 return anchor[d] + shape[d];
196 }
197
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000198 /** Accessor to set the value of anchor and shape for one of the dimensions.
199 *
200 * @param[in] dimension Dimension for which the value is set.
201 * @param[in] start Value to be set in anchor for the dimension.
202 * @param[in] size Value to be set in shape for the dimension.
203 *
204 * @return *this.
205 */
206 ValidRegion &set(size_t dimension, int start, size_t size)
207 {
208 anchor.set(dimension, start);
209 shape.set(dimension, size);
210 return *this;
211 }
212
SiCong Lib63b1192022-01-28 18:24:39 +0000213 /** Check whether two valid regions are equal.
214 *
215 * @param[in] lhs LHS valid region
216 * @param[in] rhs RHS valid region
217 *
218 * @return True if the valid regions are the same.
219 */
220 inline friend bool operator==(const ValidRegion &lhs, const ValidRegion &rhs);
221
Alex Gildayc357c472018-03-21 13:54:09 +0000222 Coordinates anchor; /**< Anchor for the start of the valid region. */
223 TensorShape shape; /**< Shape of the valid region. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224};
SiCong Lib63b1192022-01-28 18:24:39 +0000225inline bool operator==(const ValidRegion &lhs, const ValidRegion &rhs)
226{
227 return (lhs.anchor == rhs.anchor) && (lhs.shape == rhs.shape);
228}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229
230/** Methods available to handle borders */
231enum class BorderMode
232{
233 UNDEFINED, /**< Borders are left undefined */
234 CONSTANT, /**< Pixels outside the image are assumed to have a constant value */
235 REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */
236};
237
238/** Container for 2D border size */
239struct BorderSize
240{
241 /** Empty border, i.e. no border */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100242 constexpr BorderSize() noexcept : top{0}, right{0}, bottom{0}, left{0}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243 {
244 }
245
246 /** Border with equal size around the 2D plane */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100247 explicit constexpr BorderSize(unsigned int size) noexcept : top{size}, right{size}, bottom{size}, left{size}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248 {
249 }
250
251 /** Border with same size for top/bottom and left/right */
252 constexpr BorderSize(unsigned int top_bottom, unsigned int left_right)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100253 : top{top_bottom}, right{left_right}, bottom{top_bottom}, left{left_right}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100254 {
255 }
256
257 /** Border with different sizes */
258 constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100259 : top{top}, right{right}, bottom{bottom}, left{left}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100260 {
261 }
262
263 /** Check if the entire border is zero */
264 constexpr bool empty() const
265 {
266 return top == 0 && right == 0 && bottom == 0 && left == 0;
267 }
268
269 /** Check if the border is the same size on all sides */
270 constexpr bool uniform() const
271 {
272 return top == right && top == bottom && top == left;
273 }
274
Alex Gildayc357c472018-03-21 13:54:09 +0000275 /** Scale this border size.
276 *
277 * @param[in] scale Scale to multiply border size by.
278 *
279 * @return *this.
280 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100281 BorderSize &operator*=(float scale)
282 {
283 top *= scale;
284 right *= scale;
285 bottom *= scale;
286 left *= scale;
287
288 return *this;
289 }
290
Alex Gildayc357c472018-03-21 13:54:09 +0000291 /** Scale a copy of this border size.
292 *
293 * @param[in] scale Scale to multiply border size by.
294 *
295 * @return a scaled copy of this.
296 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297 BorderSize operator*(float scale)
298 {
299 BorderSize size = *this;
300 size *= scale;
301
302 return size;
303 }
304
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100305 /** Check equality with another BorderSize struct
306 *
307 * @param[in] rhs other struct to check against
308 *
309 * @return true if they are equal
310 */
SiCong Lib63b1192022-01-28 18:24:39 +0000311 bool operator==(const BorderSize &rhs) const
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100312 {
313 return (top == rhs.top) && (right == rhs.right) && (bottom == rhs.bottom) && (left == rhs.left);
314 }
315
316 /** Check non-equality with another BorderSize struct
317 *
318 * @param[in] rhs other struct to check against
319 *
320 * @return true if they are different
321 */
SiCong Lib63b1192022-01-28 18:24:39 +0000322 bool operator!=(const BorderSize &rhs) const
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100323 {
324 return !(*this == rhs);
325 }
326
Alex Gildayc357c472018-03-21 13:54:09 +0000327 /** Limit this border size.
328 *
329 * @param[in] limit Border size to limit this border size to.
330 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100331 void limit(const BorderSize &limit)
332 {
333 top = std::min(top, limit.top);
334 right = std::min(right, limit.right);
335 bottom = std::min(bottom, limit.bottom);
336 left = std::min(left, limit.left);
337 }
338
Alex Gildayc357c472018-03-21 13:54:09 +0000339 unsigned int top; /**< top of the border */
340 unsigned int right; /**< right of the border */
341 unsigned int bottom; /**< bottom of the border */
342 unsigned int left; /**< left of the border */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100343};
344
Alex Gildayc357c472018-03-21 13:54:09 +0000345/** Container for 2D padding size */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100346using PaddingSize = BorderSize;
347
SiCongLi1af54162021-10-06 15:25:57 +0100348/** Policy to handle integer overflow
349 * @note: This is ignored by floating point operations where the overflow behavior adheres to the IEEE-754 standard
350 * which states that in case of overflow ±infinity is returned for the round-to-nearest modes (and follows the
351 * rounding rules for the directed rounding modes) by default.
352 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100353enum class ConvertPolicy
354{
355 WRAP, /**< Wrap around */
356 SATURATE /**< Saturate */
357};
358
359/** Interpolation method */
360enum class InterpolationPolicy
361{
362 NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */
363 BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100364 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 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365};
366
367/** Bilinear Interpolation method used by LKTracker */
368enum class BilinearInterpolation
369{
Alex Gildayc357c472018-03-21 13:54:09 +0000370 BILINEAR_OLD_NEW, /**< Old-new method */
371 BILINEAR_SCHARR /**< Scharr method */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100372};
373
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100374/** Rectangle type */
375struct Rectangle
376{
377 uint16_t x; /**< Top-left x coordinate */
378 uint16_t y; /**< Top-left y coordinate */
379 uint16_t width; /**< Width of the rectangle */
380 uint16_t height; /**< Height of the rectangle */
381};
382
383/** Coordinate type */
384struct Coordinates2D
385{
386 int32_t x; /**< X coordinates */
387 int32_t y; /**< Y coordinates */
388};
389
390/** Coordinate type */
391struct Coordinates3D
392{
393 uint32_t x; /**< X coordinates */
394 uint32_t y; /**< Y coordinates */
395 uint32_t z; /**< Z coordinates */
396};
397
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100398/** Padding information as a pair of unsigned int start/end */
399using PaddingInfo = std::pair<uint32_t, uint32_t>;
400
401/** List of padding information */
402using PaddingList = std::vector<PaddingInfo>;
403
giuros013175fcf2018-11-21 09:59:17 +0000404/** Information to produce a tiled version of a Tensor */
405using Multiples = std::vector<uint32_t>;
406
Georgios Pinitasd9769582017-08-03 10:19:40 +0100407/** Available reduction operations */
408enum class ReductionOperation
409{
Michalis Spyrou7930db42018-11-22 17:36:28 +0000410 ARG_IDX_MAX, /**< Index of the max value */
Manuel Bottinib412fab2018-12-10 17:40:23 +0000411 ARG_IDX_MIN, /**< Index of the min value */
412 MEAN_SUM, /**< Mean of sum */
413 PROD, /**< Product */
414 SUM_SQUARE, /**< Sum of squares */
Usama Arifa4a08ad2019-05-20 12:38:33 +0100415 SUM, /**< Sum */
416 MIN, /**< Min */
Usama Arif28f0dd92019-05-20 13:44:34 +0100417 MAX, /**< Max */
Georgios Pinitasd9769582017-08-03 10:19:40 +0100418};
419
giuros01164a2722018-11-20 18:34:46 +0000420/** Available element-wise operations */
421enum class ArithmeticOperation
422{
423 ADD, /**< (x + y) */
424 SUB, /**< (x - y) */
425 DIV, /**< (x / y) */
426 MIN, /**< Min(x, y) */
427 MAX, /**< Max(x, y) */
428 SQUARED_DIFF, /**< (x - y)^2 */
Usama Arif81e671e2019-05-13 13:33:14 +0100429 POWER, /**< x ^ y */
giuros011e6e1b82019-05-14 16:12:53 +0100430 PRELU, /**< y*x if x < 0, x otherwise */
giuros01164a2722018-11-20 18:34:46 +0000431};
432
Michalis Spyroue9362622018-11-23 17:41:37 +0000433/** Available element wise unary operations */
434enum class ElementWiseUnary
435{
Sang-Hoon Park75eea332020-11-13 13:44:13 +0000436 RSQRT, /**< Reverse square root */
437 EXP, /**< Exponential */
438 NEG, /**< Negate */
439 LOG, /**< Natural Logarithm */
440 ABS, /**< Absolute value */
441 SIN, /**< Sine */
442 ROUND, /**< Round */
443 LOGICAL_NOT, /**< Logical Not */
Michalis Spyroue9362622018-11-23 17:41:37 +0000444};
445
Manuel Bottini63bb7ca2020-12-02 13:22:14 +0000446/** Available bitwise operations */
447enum class BitwiseOperation
448{
449 AND, /**< Bitwise AND operation */
450 NOT, /**< Bitwise NOT operation */
451 OR, /**< Bitwise OR operation */
452 XOR, /**< Bitwise XOR operation */
453};
454
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100455/** The normalization type used for the normalization layer */
456enum class NormType
457{
458 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
459 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
460 CROSS_MAP /**< Normalization applied cross maps */
461};
462
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100463/** Detection window used for the object detection. The detection window keeps the following information:
464 *
465 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
466 * -# Index of the class used for evaluating which class the detection window belongs to
467 * -# Confidence value (score) obtained with the classifier
468 */
469struct DetectionWindow
470{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100471 uint16_t x{0}; /**< Top-left x coordinate */
472 uint16_t y{0}; /**< Top-left y coordinate */
473 uint16_t width{0}; /**< Width of the detection window */
474 uint16_t height{0}; /**< Height of the detection window */
475 uint16_t idx_class{0}; /**< Index of the class */
476 float score{0.f}; /**< Confidence value for the detection window */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100477};
478
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100479/** Available pooling types */
480enum class PoolingType
481{
482 MAX, /**< Max Pooling */
Georgios Pinitascdf51452017-08-31 14:21:36 +0100483 AVG, /**< Average Pooling */
484 L2 /**< L2 Pooling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100485};
486
Michalis Spyrou2709d612018-09-19 09:46:47 +0100487/** Available non maxima suppression types */
488enum class NMSType
489{
490 LINEAR, /**< Linear NMS */
491 GAUSSIAN, /**< Gaussian NMS */
492 ORIGINAL /**< Original NMS */
493};
494
495/** BoxWithNonMaximaSuppressionLimit Information class */
496class BoxNMSLimitInfo final
497{
498public:
499 /** Constructor
500 *
501 * @param[in] score_thresh (Optional) Score threshold.
502 * @param[in] nms (Optional) NMS value
503 * @param[in] detections (Optional) Number of detections
504 * @param[in] soft_nms_enabled (Optional) Enable SoftNMS
505 * @param[in] soft_nms_method (Optional) Soft NMS method
506 * @param[in] soft_nms_sigma (Optional) Soft NMS sigma value
507 * @param[in] soft_nms_min_score_thres (Optional) Soft NMS minimum score threshold
Manuel Bottini5209be52019-02-13 16:34:56 +0000508 * @param[in] suppress_size (Optional) Filter out boxes based on their size. Defaults to false
509 * @param[in] min_size (Optional) Smaller boxes than min_size will be filtered out. Defaults to 1
510 * @param[in] im_width (Optional) Boxes whose centers (on the x axis) is beyond im_width will be filtered. Defaults to 1
511 * @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 +0100512 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100513 BoxNMSLimitInfo(float score_thresh = 0.05f,
514 float nms = 0.3f,
515 int detections = 100,
516 bool soft_nms_enabled = false,
517 NMSType soft_nms_method = NMSType::LINEAR,
518 float soft_nms_sigma = 0.5f,
519 float soft_nms_min_score_thres = 0.001f,
520 bool suppress_size = false,
521 float min_size = 1.0f,
522 float im_width = 1.0f,
523 float im_height = 1.0f)
524 : _score_thresh(score_thresh),
525 _nms(nms),
526 _detections_per_im(detections),
527 _soft_nms_enabled(soft_nms_enabled),
528 _soft_nms_method(soft_nms_method),
529 _soft_nms_sigma(soft_nms_sigma),
530 _soft_nms_min_score_thres(soft_nms_min_score_thres),
531 _suppress_size(suppress_size),
532 _min_size(min_size),
533 _im_width(im_width),
534 _im_height(im_height)
Michalis Spyrou2709d612018-09-19 09:46:47 +0100535 {
536 }
537 /** Get the score threshold */
538 float score_thresh() const
539 {
540 return _score_thresh;
541 }
542 /** Get the NMS */
543 float nms() const
544 {
545 return _nms;
546 }
547 /** Get the number of detections */
548 int detections_per_im() const
549 {
550 return _detections_per_im;
551 }
552 /** Check if soft NMS is enabled */
553 bool soft_nms_enabled() const
554 {
555 return _soft_nms_enabled;
556 }
557 /** Get soft NMS method */
558 NMSType soft_nms_method() const
559 {
560 return _soft_nms_method;
561 }
562 /** Get soft NMS sigma */
563 float soft_nms_sigma() const
564 {
565 return _soft_nms_sigma;
566 }
567 /** Get soft nms min score threshold */
568 float soft_nms_min_score_thres() const
569 {
570 return _soft_nms_min_score_thres;
571 }
Manuel Bottini5209be52019-02-13 16:34:56 +0000572 /** Get if NMS will suppress boxes based on their size/position */
573 bool suppress_size() const
574 {
575 return _suppress_size;
576 }
577 /** Get size suppression threshold */
578 float min_size() const
579 {
580 return _min_size;
581 }
582 /** Get image width (NMS may suppress boxes whose center sits beyond the image width) */
583 float im_width() const
584 {
585 return _im_width;
586 }
587 /** Get image height (NMS may suppress boxes whose center sits beyond the image height) */
588 float im_height() const
589 {
590 return _im_height;
591 }
Michalis Spyrou2709d612018-09-19 09:46:47 +0100592
593private:
594 float _score_thresh;
595 float _nms;
596 int _detections_per_im;
597 bool _soft_nms_enabled;
598 NMSType _soft_nms_method;
599 float _soft_nms_sigma;
600 float _soft_nms_min_score_thres;
Manuel Bottini5209be52019-02-13 16:34:56 +0000601 bool _suppress_size;
602 float _min_size;
603 float _im_width;
604 float _im_height;
Michalis Spyrou2709d612018-09-19 09:46:47 +0100605};
606
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100607/** Padding and stride information class */
SiCong Lif44bbc52022-08-29 18:25:51 +0100608/** Padding information for 2D operations like Conv2d */
609struct Padding2D
610{
611 Padding2D() = default;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100612 Padding2D(size_t left, size_t right, size_t top, size_t bottom) : left(left), right(right), top(top), bottom(bottom)
SiCong Lif44bbc52022-08-29 18:25:51 +0100613 {
614 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100615 size_t left = {0}; /**< Padding across the width dimension on the left, in elements. */
616 size_t right = {0}; /**< Padding across the width dimension on the right, in elements. */
617 size_t top = {0}; /**< Padding across the height dimension on the top, in elements. */
618 size_t bottom = {0}; /**< Padding across the height dimension on the bottom, in elements. */
SiCong Lif44bbc52022-08-29 18:25:51 +0100619};
620
Adnan AlSinane4563a02021-09-01 15:32:03 +0100621/** Padding information for 3D operations like Conv3d */
622struct Padding3D
623{
Freddie Liardetebefe522021-11-25 16:19:28 +0000624 Padding3D() noexcept
Giorgio Arenac9fe9fc2021-10-06 12:54:29 +0100625 {
626 }
627
628 Padding3D(size_t pad_x, size_t pad_y, size_t pad_z)
629 : left(pad_x), right(pad_x), top(pad_y), bottom(pad_y), front(pad_z), back(pad_z)
630 {
631 }
632
633 Padding3D(size_t left, size_t right, size_t top, size_t bottom, size_t front, size_t back)
634 : left(left), right(right), top(top), bottom(bottom), front(front), back(back)
635 {
636 }
637
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100638 size_t left = {0}; /**< Padding across the width dimenstion on the left, in elements. */
639 size_t right = {0}; /**< Padding across the width dimenstion on the right, in elements. */
640 size_t top = {0}; /**< Padding across the height dimenstion on the top, in elements. */
641 size_t bottom = {0}; /**< Padding across the height dimenstion on the bottom, in elements. */
642 size_t front = {0}; /**< Padding across the depth dimenstion on the front, in elements. */
643 size_t back = {0}; /**< Padding across the depth dimenstion on the back, in elements. */
Adnan AlSinane4563a02021-09-01 15:32:03 +0100644};
645
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100646/** PriorBox layer info */
647class PriorBoxLayerInfo final
648{
649public:
650 /** Default Constructor */
651 PriorBoxLayerInfo()
652 : _min_sizes(),
653 _variances(),
654 _offset(),
655 _flip(true),
656 _clip(false),
657 _max_sizes(),
658 _aspect_ratios(),
659 _img_size(),
660 _steps()
661 {
662 }
663 /** Constructor
664 *
665 * @param[in] min_sizes Min sizes vector.
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100666 * @param[in] variances Variances vector.
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100667 * @param[in] offset Offset value.
668 * @param[in] flip (Optional) Flip the aspect ratios.
669 * @param[in] clip (Optional) Clip coordinates so that they're within [0,1].
670 * @param[in] max_sizes (Optional) Max sizes vector.
671 * @param[in] aspect_ratios (Optional) Aspect ratios of the boxes.
672 * @param[in] img_size (Optional) Image size.
673 * @param[in] steps (Optional) Step values.
674 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100675 PriorBoxLayerInfo(const std::vector<float> &min_sizes,
676 const std::vector<float> &variances,
677 float offset,
678 bool flip = true,
679 bool clip = false,
680 const std::vector<float> &max_sizes = {},
681 const std::vector<float> &aspect_ratios = {},
682 const Coordinates2D &img_size = Coordinates2D{0, 0},
683 const std::array<float, 2> &steps = {{0.f, 0.f}})
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100684 : _min_sizes(min_sizes),
685 _variances(variances),
686 _offset(offset),
687 _flip(flip),
688 _clip(clip),
689 _max_sizes(max_sizes),
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100690 _aspect_ratios(),
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100691 _img_size(img_size),
692 _steps(steps)
693 {
694 _aspect_ratios.push_back(1.);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100695 for (unsigned int i = 0; i < aspect_ratios.size(); ++i)
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100696 {
697 float ar = aspect_ratios[i];
698 bool already_exist = false;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100699 for (auto ar_new : _aspect_ratios)
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100700 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100701 if (fabs(ar - ar_new) < 1e-6)
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100702 {
703 already_exist = true;
704 break;
705 }
706 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100707 if (!already_exist)
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100708 {
709 _aspect_ratios.push_back(ar);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100710 if (flip)
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100711 {
712 _aspect_ratios.push_back(1.f / ar);
713 }
714 }
715 }
716 }
717 /** Get min sizes. */
718 std::vector<float> min_sizes() const
719 {
720 return _min_sizes;
721 }
722 /** Get min variances. */
723 std::vector<float> variances() const
724 {
725 return _variances;
726 }
727 /** Get the step coordinates */
728 std::array<float, 2> steps() const
729 {
730 return _steps;
731 }
732 /** Get the image size coordinates */
733 Coordinates2D img_size() const
734 {
735 return _img_size;
736 }
737 /** Get the offset */
738 float offset() const
739 {
740 return _offset;
741 }
742 /** Get the flip value */
743 bool flip() const
744 {
745 return _flip;
746 }
747 /** Get the clip value */
748 bool clip() const
749 {
750 return _clip;
751 }
752 /** Get max sizes. */
753 std::vector<float> max_sizes() const
754 {
755 return _max_sizes;
756 }
757 /** Get aspect ratios. */
758 std::vector<float> aspect_ratios() const
759 {
760 return _aspect_ratios;
761 }
762
763private:
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100764 std::vector<float> _min_sizes;
765 std::vector<float> _variances;
766 float _offset;
767 bool _flip;
768 bool _clip;
769 std::vector<float> _max_sizes;
770 std::vector<float> _aspect_ratios;
771 Coordinates2D _img_size;
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100772 std::array<float, 2> _steps;
773};
774
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000775// Bounding Box [xmin, ymin, xmax, ymax]
776using BBox = std::array<float, 4>;
777// LabelBBox used for map label and bounding box
778using LabelBBox = std::map<int, std::vector<BBox>>;
779
Isabella Gottardi05e56442018-11-16 11:26:52 +0000780/** Available Detection Output code types */
781enum class DetectionOutputLayerCodeType
782{
783 CORNER, /**< Use box corners */
784 CENTER_SIZE, /**< Use box centers and size */
785 CORNER_SIZE, /**< Use box centers and size */
786 TF_CENTER /**< Use box centers and size but flip x and y co-ordinates */
787};
788
789/** Detection Output layer info */
790class DetectionOutputLayerInfo final
791{
792public:
793 /** Default Constructor */
794 DetectionOutputLayerInfo()
795 : _num_classes(),
796 _share_location(),
797 _code_type(DetectionOutputLayerCodeType::CORNER),
798 _keep_top_k(),
799 _nms_threshold(),
800 _top_k(),
801 _background_label_id(),
802 _confidence_threshold(),
803 _variance_encoded_in_target(false),
804 _eta(),
805 _num_loc_classes()
806 {
807 _num_loc_classes = _share_location ? 1 : _num_classes;
808 }
809 /** Constructor
810 *
811 * @param[in] num_classes Number of classes to be predicted.
812 * @param[in] share_location If true, bounding box are shared among different classes.
813 * @param[in] code_type Type of coding method for bbox.
814 * @param[in] keep_top_k Number of total bounding boxes to be kept per image after NMS step.
815 * @param[in] nms_threshold Threshold to be used in NMS.
816 * @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.
817 * @param[in] background_label_id (Optional) Background label ID. If there is no background class, set it as -1.
818 * @param[in] confidence_threshold (Optional) Only consider detections whose confidences are larger than a threshold. Default set to -FLT_MAX.
819 * @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.
820 * @param[in] eta (Optional) Eta.
821 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100822 DetectionOutputLayerInfo(int num_classes,
823 bool share_location,
824 DetectionOutputLayerCodeType code_type,
825 int keep_top_k,
826 float nms_threshold,
827 int top_k = -1,
828 int background_label_id = -1,
829 float confidence_threshold = std::numeric_limits<float>::lowest(),
830 bool variance_encoded_in_target = false,
831 float eta = 1)
Isabella Gottardi05e56442018-11-16 11:26:52 +0000832 : _num_classes(num_classes),
833 _share_location(share_location),
834 _code_type(code_type),
835 _keep_top_k(keep_top_k),
836 _nms_threshold(nms_threshold),
837 _top_k(top_k),
838 _background_label_id(background_label_id),
839 _confidence_threshold(confidence_threshold),
840 _variance_encoded_in_target(variance_encoded_in_target),
841 _eta(eta),
842 _num_loc_classes()
843 {
844 _num_loc_classes = _share_location ? 1 : _num_classes;
845 }
846 /** Get num classes. */
847 int num_classes() const
848 {
849 return _num_classes;
850 }
851 /** Get share location. */
852 bool share_location() const
853 {
854 return _share_location;
855 }
856 /** Get detection output code type. */
857 DetectionOutputLayerCodeType code_type() const
858 {
859 return _code_type;
860 }
861 /** Get if variance encoded in target. */
862 bool variance_encoded_in_target() const
863 {
864 return _variance_encoded_in_target;
865 }
866 /** Get the number of total bounding boxes to be kept per image. */
867 int keep_top_k() const
868 {
869 return _keep_top_k;
870 }
871 /** Get nms threshold. */
872 float nms_threshold() const
873 {
874 return _nms_threshold;
875 }
876 /** Get eta. */
877 float eta() const
878 {
879 return _eta;
880 }
881 /** Get background label ID. */
882 int background_label_id() const
883 {
884 return _background_label_id;
885 }
886 /** Get confidence threshold. */
887 float confidence_threshold() const
888 {
889 return _confidence_threshold;
890 }
891 /** Get top K. */
892 int top_k() const
893 {
894 return _top_k;
895 }
896 /** Get number of location classes. */
897 int num_loc_classes() const
898 {
899 return _num_loc_classes;
900 }
901
902private:
903 int _num_classes;
904 bool _share_location;
905 DetectionOutputLayerCodeType _code_type;
906 int _keep_top_k;
907 float _nms_threshold;
908 int _top_k;
909 int _background_label_id;
910 float _confidence_threshold;
911 bool _variance_encoded_in_target;
912 float _eta;
913 int _num_loc_classes;
914};
915
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000916/** Detection Output layer info */
917class DetectionPostProcessLayerInfo final
918{
919public:
920 /** Default Constructor */
921 DetectionPostProcessLayerInfo()
922 : _max_detections(),
923 _max_classes_per_detection(),
924 _nms_score_threshold(),
925 _iou_threshold(),
926 _num_classes(),
927 _scales_values(),
928 _use_regular_nms(),
Giuseppe Rossinid9853782019-10-25 11:11:44 +0100929 _detection_per_class(),
930 _dequantize_scores()
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000931 {
932 }
933 /** Constructor
934 *
935 * @param[in] max_detections Number of total detection.
936 * @param[in] max_classes_per_detection Number of total classes to be kept after NMS step. Used in the Fast Non-Max-Suppression
937 * @param[in] nms_score_threshold Threshold to be used in NMS
938 * @param[in] iou_threshold Threshold to be used during the intersection over union.
939 * @param[in] num_classes Number of classes.
940 * @param[in] scales_values Scales values used for decode center size boxes.
Giuseppe Rossinid9853782019-10-25 11:11:44 +0100941 * @param[in] use_regular_nms (Optional) Boolean to determinate if use regular or fast nms. Defaults to false.
942 * @param[in] detection_per_class (Optional) Number of detection per class. Used in the Regular Non-Max-Suppression. Defaults to 100.
943 * @param[in] dequantize_scores (Optional) If the scores need to be dequantized. Defaults to true.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000944 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100945 DetectionPostProcessLayerInfo(unsigned int max_detections,
946 unsigned int max_classes_per_detection,
947 float nms_score_threshold,
948 float iou_threshold,
949 unsigned int num_classes,
950 std::array<float, 4> scales_values,
951 bool use_regular_nms = false,
952 unsigned int detection_per_class = 100,
953 bool dequantize_scores = true)
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000954 : _max_detections(max_detections),
955 _max_classes_per_detection(max_classes_per_detection),
956 _nms_score_threshold(nms_score_threshold),
957 _iou_threshold(iou_threshold),
958 _num_classes(num_classes),
959 _scales_values(scales_values),
960 _use_regular_nms(use_regular_nms),
Giuseppe Rossinid9853782019-10-25 11:11:44 +0100961 _detection_per_class(detection_per_class),
962 _dequantize_scores(dequantize_scores)
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000963 {
964 }
965 /** Get max detections. */
966 unsigned int max_detections() const
967 {
968 return _max_detections;
969 }
970 /** Get max_classes per detection. Used in the Fast Non-Max-Suppression.*/
971 unsigned int max_classes_per_detection() const
972 {
973 return _max_classes_per_detection;
974 }
975 /** Get detection per class. Used in the Regular Non-Max-Suppression */
976 unsigned int detection_per_class() const
977 {
978 return _detection_per_class;
979 }
980 /** Get nms threshold. */
981 float nms_score_threshold() const
982 {
983 return _nms_score_threshold;
984 }
985 /** Get intersection over union threshold. */
986 float iou_threshold() const
987 {
988 return _iou_threshold;
989 }
990 /** Get num classes. */
991 unsigned int num_classes() const
992 {
993 return _num_classes;
994 }
995 /** Get if use regular nms. */
996 bool use_regular_nms() const
997 {
998 return _use_regular_nms;
999 }
1000 /** Get y scale value. */
1001 float scale_value_y() const
1002 {
1003 // Saved as [y,x,h,w]
1004 return _scales_values[0];
1005 }
1006 /** Get x scale value. */
1007 float scale_value_x() const
1008 {
1009 // Saved as [y,x,h,w]
1010 return _scales_values[1];
1011 }
1012 /** Get h scale value. */
1013 float scale_value_h() const
1014 {
1015 // Saved as [y,x,h,w]
1016 return _scales_values[2];
1017 }
1018 /** Get w scale value. */
1019 float scale_value_w() const
1020 {
1021 // Saved as [y,x,h,w]
1022 return _scales_values[3];
1023 }
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001024 /** Get dequantize_scores value. */
1025 bool dequantize_scores() const
1026 {
1027 return _dequantize_scores;
1028 }
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001029
1030private:
Jakub Sujak0d27b2e2023-08-24 14:01:20 +01001031 unsigned int _max_detections;
1032 unsigned int _max_classes_per_detection;
1033 float _nms_score_threshold;
1034 float _iou_threshold;
1035 unsigned int _num_classes;
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001036 std::array<float, 4> _scales_values;
Jakub Sujak0d27b2e2023-08-24 14:01:20 +01001037 bool _use_regular_nms;
1038 unsigned int _detection_per_class;
1039 bool _dequantize_scores;
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001040};
1041
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001042/** Pooling Layer Information struct*/
1043struct PoolingLayerInfo
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001044{
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001045 /** Default Constructor */
1046 PoolingLayerInfo()
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001047 : pool_type(PoolingType::MAX),
1048 pool_size(Size2D()),
1049 data_layout(DataLayout::UNKNOWN),
1050 pad_stride_info(PadStrideInfo()),
1051 exclude_padding(false),
1052 is_global_pooling(false),
Adnan AlSinan227db8d2023-02-14 14:24:09 +00001053 fp_mixed_precision(false),
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00001054 use_inf_as_limit(true),
1055 use_kernel_indices(false)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001056 {
1057 }
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001058 /** Constructor
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001059 *
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001060 * @param[in] pool_type Pooling type @ref PoolingType.
1061 * @param[in] pool_size Pooling size, in elements, across x and y.
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001062 * @param[in] data_layout Data layout used by the layer @ref DataLayout
1063 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
1064 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1065 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1066 * Defaults to false;
1067 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
Adnan AlSinan227db8d2023-02-14 14:24:09 +00001068 * @param[in] use_inf_as_limit (Optional) Use inf to represent the limits of datatypes range, instead of using "lowest" property of the data type.
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00001069 * @param[in] use_kernel_indices (Optional) Use kernel indices instead of using source indices while computing indices tensor.
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001070 */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001071 explicit PoolingLayerInfo(PoolingType pool_type,
1072 unsigned int pool_size,
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001073 DataLayout data_layout,
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001074 PadStrideInfo pad_stride_info = PadStrideInfo(),
1075 bool exclude_padding = false,
Adnan AlSinan227db8d2023-02-14 14:24:09 +00001076 bool fp_mixed_precision = false,
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00001077 bool use_inf_as_limit = true,
1078 bool use_kernel_indices = false)
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001079 : pool_type(pool_type),
1080 pool_size(Size2D(pool_size, pool_size)),
1081 data_layout(data_layout),
1082 pad_stride_info(pad_stride_info),
1083 exclude_padding(exclude_padding),
1084 is_global_pooling(false),
Adnan AlSinan227db8d2023-02-14 14:24:09 +00001085 fp_mixed_precision(fp_mixed_precision),
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00001086 use_inf_as_limit(use_inf_as_limit),
1087 use_kernel_indices(use_kernel_indices)
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001088 {
1089 }
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001090
1091 /** Constructor
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001092 *
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001093 * @param[in] pool_type Pooling type @ref PoolingType.
1094 * @param[in] pool_size Pooling size, in elements, across x and y.
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001095 * @param[in] data_layout Data layout used by the layer @ref DataLayout
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001096 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
1097 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1098 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1099 * Defaults to false;
1100 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
Adnan AlSinan227db8d2023-02-14 14:24:09 +00001101 * @param[in] use_inf_as_limit (Optional) Use inf to represent the limits of datatypes range, instead of using "lowest" property of the data type.
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00001102 * @param[in] use_kernel_indices (Optional) Use kernel indices instead of using source indices while computing indices tensor.
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001103 */
1104 explicit PoolingLayerInfo(PoolingType pool_type,
1105 Size2D pool_size,
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001106 DataLayout data_layout,
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001107 PadStrideInfo pad_stride_info = PadStrideInfo(),
1108 bool exclude_padding = false,
Adnan AlSinan227db8d2023-02-14 14:24:09 +00001109 bool fp_mixed_precision = false,
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00001110 bool use_inf_as_limit = true,
1111 bool use_kernel_indices = false)
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001112 : pool_type(pool_type),
1113 pool_size(pool_size),
1114 data_layout(data_layout),
1115 pad_stride_info(pad_stride_info),
1116 exclude_padding(exclude_padding),
1117 is_global_pooling(false),
Adnan AlSinan227db8d2023-02-14 14:24:09 +00001118 fp_mixed_precision(fp_mixed_precision),
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00001119 use_inf_as_limit(use_inf_as_limit),
1120 use_kernel_indices(use_kernel_indices)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001121 {
1122 }
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001123
1124 /** Constructor
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001125 *
1126 * @note This constructor is used for global pooling
1127 *
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001128 * @param[in] pool_type Pooling type @ref PoolingType.
1129 * @param[in] data_layout Data layout used by the layer @ref DataLayout
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001130 */
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001131 explicit PoolingLayerInfo(PoolingType pool_type, DataLayout data_layout)
1132 : pool_type(pool_type),
1133 pool_size(Size2D()),
1134 data_layout(data_layout),
1135 pad_stride_info(PadStrideInfo(1, 1, 0, 0)),
1136 exclude_padding(false),
1137 is_global_pooling(true),
Adnan AlSinan227db8d2023-02-14 14:24:09 +00001138 fp_mixed_precision(false),
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00001139 use_inf_as_limit(true),
1140 use_kernel_indices(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001141 {
1142 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001143
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001144 PoolingType pool_type;
1145 Size2D pool_size;
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001146 DataLayout data_layout;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001147 PadStrideInfo pad_stride_info;
1148 bool exclude_padding;
1149 bool is_global_pooling;
1150 bool fp_mixed_precision;
Adnan AlSinan227db8d2023-02-14 14:24:09 +00001151 bool use_inf_as_limit;
Adnan AlSinanbbf2e742023-02-22 12:15:14 +00001152 bool use_kernel_indices;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001153};
1154
ramelg0137515692022-02-26 22:06:20 +00001155/** Pooling Layer Information struct*/
1156struct Pooling3dLayerInfo
1157{
1158 /** Default Constructor */
Michalis Spyrou50e48aa2022-04-21 16:42:56 +01001159 Pooling3dLayerInfo() noexcept
ramelg0137515692022-02-26 22:06:20 +00001160 : pool_type(PoolingType::MAX),
1161 pool_size(Size3D()),
1162 stride(Size3D()),
1163 padding(Padding3D()),
1164 exclude_padding(false),
1165 is_global_pooling(false),
1166 fp_mixed_precision(false),
1167 round_type(DimensionRoundingType::FLOOR)
1168 {
1169 }
1170 /** Constructor
1171 *
1172 * @param[in] pool_type Pooling type @ref PoolingType.
1173 * @param[in] pool_size Pooling size, in elements, across x, y and z.
1174 * @param[in] stride (Optional) stride information @ref Size3D
1175 * @param[in] padding (Optional) padding information @ref Padding3D
1176 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1177 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1178 * Defaults to false;
1179 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +01001180 * @param[in] round_type (Optional) Dimensions rounding. Defaults to @ref DimensionRoundingType::FLOOR
ramelg0137515692022-02-26 22:06:20 +00001181 */
1182 explicit Pooling3dLayerInfo(PoolingType pool_type,
1183 unsigned int pool_size,
1184 Size3D stride = Size3D(1U, 1U, 1U),
1185 Padding3D padding = Padding3D(),
1186 bool exclude_padding = false,
1187 bool fp_mixed_precision = false,
1188 DimensionRoundingType round_type = DimensionRoundingType::FLOOR)
1189 : pool_type(pool_type),
1190 pool_size(Size3D(pool_size, pool_size, pool_size)),
1191 stride(stride),
1192 padding(padding),
1193 exclude_padding(exclude_padding),
1194 is_global_pooling(false),
1195 fp_mixed_precision(fp_mixed_precision),
1196 round_type(round_type)
1197 {
1198 }
1199
1200 /** Constructor
1201 *
1202 * @param[in] pool_type Pooling type @ref PoolingType.
1203 * @param[in] pool_size Pooling size, in elements, across x, y and z.
1204 * @param[in] stride (Optional) stride information @ref Size3D
1205 * @param[in] padding (Optional) padding information @ref Padding3D
1206 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1207 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1208 * Defaults to false;
1209 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
ramy.elgammal@arm.coma2561f02023-06-16 20:45:48 +01001210 * @param[in] round_type (Optional) Dimensions rounding. Defaults to @ref DimensionRoundingType::FLOOR
ramelg0137515692022-02-26 22:06:20 +00001211 */
1212 explicit Pooling3dLayerInfo(PoolingType pool_type,
1213 Size3D pool_size,
1214 Size3D stride = Size3D(1U, 1U, 1U),
1215 Padding3D padding = Padding3D(),
1216 bool exclude_padding = false,
1217 bool fp_mixed_precision = false,
1218 DimensionRoundingType round_type = DimensionRoundingType::FLOOR)
1219 : pool_type(pool_type),
1220 pool_size(pool_size),
1221 stride(stride),
1222 padding(padding),
1223 exclude_padding(exclude_padding),
1224 is_global_pooling(false),
1225 fp_mixed_precision(fp_mixed_precision),
1226 round_type(round_type)
1227 {
1228 }
1229
1230 /** Constructor
1231 *
1232 * @note This constructor is used for global pooling
1233 *
1234 * @param[in] pool_type Pooling type @ref PoolingType.
1235 */
1236 explicit Pooling3dLayerInfo(PoolingType pool_type)
1237 : pool_type(pool_type),
1238 pool_size(Size3D()),
1239 stride(Size3D(1U, 1U, 1U)),
1240 padding(Padding3D(0, 0, 0)),
1241 exclude_padding(false),
1242 is_global_pooling(true),
1243 fp_mixed_precision(false),
1244 round_type(DimensionRoundingType::FLOOR)
1245 {
1246 }
1247
1248 PoolingType pool_type;
1249 Size3D pool_size;
1250 Size3D stride;
1251 Padding3D padding;
1252 bool exclude_padding;
1253 bool is_global_pooling;
1254 bool fp_mixed_precision;
1255 DimensionRoundingType round_type;
1256};
1257
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001258/** ROI Pooling Layer Information class */
giuros0118870812018-09-13 09:31:40 +01001259class ROIPoolingLayerInfo final
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001260{
1261public:
giuros0118870812018-09-13 09:31:40 +01001262 /** Constructor
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001263 *
giuros0118870812018-09-13 09:31:40 +01001264 * @param[in] pooled_width Pooled width of the layer.
1265 * @param[in] pooled_height Pooled height of the layer.
1266 * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions.
1267 * @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 +01001268 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001269 ROIPoolingLayerInfo(unsigned int pooled_width,
1270 unsigned int pooled_height,
1271 float spatial_scale,
1272 unsigned int sampling_ratio = 0)
1273 : _pooled_width(pooled_width),
1274 _pooled_height(pooled_height),
1275 _spatial_scale(spatial_scale),
1276 _sampling_ratio(sampling_ratio)
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001277 {
1278 }
Alex Gildayc357c472018-03-21 13:54:09 +00001279 /** Get the pooled width of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001280 unsigned int pooled_width() const
1281 {
1282 return _pooled_width;
1283 }
Alex Gildayc357c472018-03-21 13:54:09 +00001284 /** Get the pooled height of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001285 unsigned int pooled_height() const
1286 {
1287 return _pooled_height;
1288 }
Alex Gildayc357c472018-03-21 13:54:09 +00001289 /** Get the spatial scale */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001290 float spatial_scale() const
1291 {
1292 return _spatial_scale;
1293 }
giuros0118870812018-09-13 09:31:40 +01001294 /** Get sampling ratio */
1295 unsigned int sampling_ratio() const
1296 {
1297 return _sampling_ratio;
1298 }
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001299
1300private:
1301 unsigned int _pooled_width;
1302 unsigned int _pooled_height;
1303 float _spatial_scale;
giuros0118870812018-09-13 09:31:40 +01001304 unsigned int _sampling_ratio;
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001305};
1306
Manuel Bottini5209be52019-02-13 16:34:56 +00001307/** Generate Proposals Information class */
1308class GenerateProposalsInfo
1309{
1310public:
1311 /** Constructor
1312 *
1313 * @param[in] im_width Width of the original image
1314 * @param[in] im_height Height of the original image
1315 * @param[in] im_scale Scale applied to the original image
1316 * @param[in] spatial_scale (Optional)Scale applied to the feature map. Defaults to 1.0
1317 * @param[in] pre_nms_topN (Optional)Number of the best scores to be selected from the transformations. Defaults to 6000.
1318 * @param[in] post_nms_topN (Optional)Number of the best scores to be selected from the NMS operation. Defaults to 300.
1319 * @param[in] nms_thres (Optional)NMS overlap threshold. Defaults to 0.7.
1320 * @param[in] min_size (Optional)Size used to validate the anchors produced. Defaults to 16.
1321 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region of interest). Defaults to 4.
1322 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001323 GenerateProposalsInfo(float im_width,
1324 float im_height,
1325 float im_scale,
1326 float spatial_scale = 1.0,
1327 int pre_nms_topN = 6000,
1328 int post_nms_topN = 300,
1329 float nms_thres = 0.7,
1330 float min_size = 16.0,
Manuel Bottini5209be52019-02-13 16:34:56 +00001331 size_t values_per_roi = 4)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001332 : _im_height(im_height),
1333 _im_width(im_width),
1334 _im_scale(im_scale),
1335 _spatial_scale(spatial_scale),
1336 _pre_nms_topN(pre_nms_topN),
1337 _post_nms_topN(post_nms_topN),
1338 _nms_thres(nms_thres),
1339 _min_size(min_size),
1340 _values_per_roi(values_per_roi)
Manuel Bottini5209be52019-02-13 16:34:56 +00001341 {
1342 }
1343
1344 /* Get the original height */
1345 float im_height() const
1346 {
1347 return _im_height;
1348 }
1349 /* Get the original width */
1350 float im_width() const
1351 {
1352 return _im_width;
1353 }
1354 /* Get the image scale */
1355 float im_scale() const
1356 {
1357 return _im_scale;
1358 }
1359 /* Get the value of how many best scores to select (before NMS) */
1360 int pre_nms_topN() const
1361 {
1362 return _pre_nms_topN;
1363 }
1364 /* Get the value of how many best scores to select (after NMS) */
1365 int post_nms_topN() const
1366 {
1367 return _post_nms_topN;
1368 }
1369 /* Get the NMS overlap threshold */
1370 float nms_thres() const
1371 {
1372 return _nms_thres;
1373 }
1374 /* Get the minimal size */
1375 float min_size() const
1376 {
1377 return _min_size;
1378 }
1379 /* Get the spatial scale to be applied to the feature maps */
1380 float spatial_scale() const
1381 {
1382 return _spatial_scale;
1383 }
1384 /* Get the values used to represent a ROI(Region of interest)*/
1385 size_t values_per_roi() const
1386 {
1387 return _values_per_roi;
1388 }
1389
1390private:
1391 float _im_height;
1392 float _im_width;
1393 float _im_scale;
1394 float _spatial_scale;
1395 int _pre_nms_topN;
1396 int _post_nms_topN;
1397 float _nms_thres;
1398 float _min_size;
1399 size_t _values_per_roi;
1400};
1401
1402/** ComputeAnchors information class */
1403class ComputeAnchorsInfo
1404{
1405public:
1406 /** Constructor
1407 *
1408 * @param[in] feat_width Feature map width
1409 * @param[in] feat_height Feature map height
1410 * @param[in] spatial_scale Feature map scale
1411 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region Of Interest). Defaults to 4
1412 */
1413 ComputeAnchorsInfo(float feat_width, float feat_height, float spatial_scale, size_t values_per_roi = 4)
1414 : _feat_height(feat_height),
1415 _feat_width(feat_width),
1416 _spatial_scale(spatial_scale),
1417 _values_per_roi(values_per_roi)
1418 {
1419 }
1420
1421 /* Get the height of the feature map */
1422 float feat_height() const
1423 {
1424 return _feat_height;
1425 }
1426
1427 /* Get the width of the feature map */
1428 float feat_width() const
1429 {
1430 return _feat_width;
1431 }
1432
1433 /* Get the scale of the feature map */
1434 float spatial_scale() const
1435 {
1436 return _spatial_scale;
1437 }
1438
1439 /* Get the values used to represent a ROI(Region Of Interest)*/
1440 size_t values_per_roi() const
1441 {
1442 return _values_per_roi;
1443 }
1444
1445private:
1446 float _feat_height;
1447 float _feat_width;
1448 float _spatial_scale;
1449 size_t _values_per_roi;
1450};
1451
giuros01c04a0e82018-10-03 12:44:35 +01001452/** Bounding Box Transform information class */
giuros01d696cb62018-11-16 10:39:59 +00001453class BoundingBoxTransformInfo final
giuros01c04a0e82018-10-03 12:44:35 +01001454{
1455public:
1456 /** Constructor
1457 *
giuros01d696cb62018-11-16 10:39:59 +00001458 * @param[in] img_width Width of the original image
1459 * @param[in] img_height Height, of the original image
1460 * @param[in] scale Scale of the original image
1461 * @param[in] apply_scale (Optional)Re-apply scaling after transforming the boxes. Defaults to false
1462 * @param[in] weights (Optional)Weights [wx, wy, ww, wh] for the deltas. Defaults to all ones
1463 * @param[in] correct_transform_coords (Optional)Correct bounding box transform coordinates. Defaults to false
1464 * @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 +01001465 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001466 BoundingBoxTransformInfo(float img_width,
1467 float img_height,
1468 float scale,
1469 bool apply_scale = false,
1470 const std::array<float, 4> weights = {{1.f, 1.f, 1.f, 1.f}},
1471 bool correct_transform_coords = false,
1472 float bbox_xform_clip = 4.135166556742356f)
1473 : _img_width(img_width),
1474 _img_height(img_height),
1475 _scale(scale),
1476 _apply_scale(apply_scale),
1477 _correct_transform_coords(correct_transform_coords),
1478 _weights(weights),
1479 _bbox_xform_clip(bbox_xform_clip)
giuros01c04a0e82018-10-03 12:44:35 +01001480 {
1481 }
1482
1483 std::array<float, 4> weights() const
1484 {
1485 return _weights;
1486 }
1487
1488 float bbox_xform_clip() const
1489 {
1490 return _bbox_xform_clip;
1491 }
1492
1493 float img_height() const
1494 {
1495 return _img_height;
1496 }
1497
1498 float img_width() const
1499 {
1500 return _img_width;
1501 }
1502
1503 float scale() const
1504 {
1505 return _scale;
1506 }
1507
1508 bool apply_scale() const
1509 {
1510 return _apply_scale;
1511 }
1512
giuros01d696cb62018-11-16 10:39:59 +00001513 bool correct_transform_coords() const
1514 {
1515 return _correct_transform_coords;
1516 }
1517
giuros01c04a0e82018-10-03 12:44:35 +01001518private:
Jakub Sujak0d27b2e2023-08-24 14:01:20 +01001519 float _img_width;
1520 float _img_height;
1521 float _scale;
1522 bool _apply_scale;
1523 bool _correct_transform_coords;
giuros01c04a0e82018-10-03 12:44:35 +01001524 std::array<float, 4> _weights;
Jakub Sujak0d27b2e2023-08-24 14:01:20 +01001525 float _bbox_xform_clip;
giuros01c04a0e82018-10-03 12:44:35 +01001526};
1527
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001528/** Normalization Layer Information class */
1529class NormalizationLayerInfo
1530{
1531public:
1532 /** Default Constructor
1533 *
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001534 * @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 +01001535 * @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 +00001536 * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001.
1537 * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5.
1538 * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
1539 * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not.
1540 * Should be false to follow [Krichevksy 2012].
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001541 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001542 NormalizationLayerInfo(NormType type,
1543 uint32_t norm_size = 5,
1544 float alpha = 0.0001f,
1545 float beta = 0.5f,
1546 float kappa = 1.f,
1547 bool is_scaled = true)
Georgios Pinitas41caa622017-11-16 14:37:08 +00001548 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001549 {
1550 }
Alex Gildayc357c472018-03-21 13:54:09 +00001551 /** Get the normalization type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001552 NormType type() const
1553 {
1554 return _type;
1555 }
Alex Gildayc357c472018-03-21 13:54:09 +00001556 /** Get the normalization size */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001557 uint32_t norm_size() const
1558 {
1559 return _norm_size;
1560 }
Alex Gildayc357c472018-03-21 13:54:09 +00001561 /** Get the alpha value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001562 float alpha() const
1563 {
1564 return _alpha;
1565 }
Alex Gildayc357c472018-03-21 13:54:09 +00001566 /** Get the beta value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001567 float beta() const
1568 {
1569 return _beta;
1570 }
Alex Gildayc357c472018-03-21 13:54:09 +00001571 /** Get the kappa value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001572 float kappa() const
1573 {
1574 return _kappa;
1575 }
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001576 /** Get the is_scaled value */
1577 bool is_scaled() const
1578 {
1579 return _is_scaled;
1580 }
Alex Gildayc357c472018-03-21 13:54:09 +00001581 /** Check if normalization is cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001582 bool is_cross_map() const
1583 {
1584 return _type == NormType::CROSS_MAP;
1585 }
Alex Gildayc357c472018-03-21 13:54:09 +00001586 /** Check if normalization is not cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001587 bool is_in_map() const
1588 {
1589 return !is_cross_map();
1590 }
1591 /** Return the scaling factor of the normalization function.
1592 *
1593 * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed,
1594 * 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 +01001595 *
1596 * @return The normalization scaling factor.
1597 */
1598 float scale_coeff() const
1599 {
1600 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001601 return (_is_scaled) ? (_alpha / size) : _alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001602 }
1603
1604private:
1605 NormType _type;
1606 uint32_t _norm_size;
1607 float _alpha;
1608 float _beta;
1609 float _kappa;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001610 bool _is_scaled;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001611};
1612
thecha012bfadd92020-08-12 17:25:51 +01001613class StridedSliceLayerInfo
1614{
1615public:
1616 /** Default Constructor
1617 *
1618 * @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.
1619 * @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.
1620 * @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.
1621 */
1622 StridedSliceLayerInfo(int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0)
1623 : _begin_mask(begin_mask), _end_mask(end_mask), _shrink_axis_mask(shrink_axis_mask)
1624 {
1625 }
1626
1627 /* Get the begin mask value */
1628 int32_t begin_mask() const
1629 {
1630 return _begin_mask;
1631 }
1632
1633 /* Get the end mask value */
1634 int32_t end_mask() const
1635 {
1636 return _end_mask;
1637 }
1638
1639 /* Get the shrink axis mask value */
1640 int32_t shrink_axis_mask() const
1641 {
1642 return _shrink_axis_mask;
1643 }
1644
1645private:
1646 int32_t _begin_mask;
1647 int32_t _end_mask;
1648 int32_t _shrink_axis_mask;
1649};
1650
Ramy Elgammal91780022022-07-20 14:57:37 +01001651// OHWIo<interleave_by>i<block_by>
1652inline int interleave_by(const WeightFormat wf)
1653{
Pablo Marquez Tello93581a52022-07-21 13:55:27 +01001654 return (static_cast<int>(wf) >> 8) & 0xFFF;
Ramy Elgammal91780022022-07-20 14:57:37 +01001655}
1656inline int block_by(const WeightFormat wf)
1657{
Pablo Marquez Tello93581a52022-07-21 13:55:27 +01001658 return (static_cast<int>(wf) >> 20) & 0xF;
Ramy Elgammal91780022022-07-20 14:57:37 +01001659}
Pablo Marquez Tello93581a52022-07-21 13:55:27 +01001660inline bool is_fixed_format(const WeightFormat &wf)
Ramy Elgammal91780022022-07-20 14:57:37 +01001661{
1662 return wf != WeightFormat::UNSPECIFIED && wf != WeightFormat::ANY;
1663}
Pablo Marquez Tello93581a52022-07-21 13:55:27 +01001664inline bool is_fixed_format_fast_math(const WeightFormat &wf)
1665{
1666 return (static_cast<int>(wf) >> 4) & 0x1;
1667}
Ramy Elgammal91780022022-07-20 14:57:37 +01001668
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001669/** 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 +01001670class WeightsInfo
1671{
1672public:
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001673 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001674 WeightsInfo()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001675 : _are_reshaped(false),
1676 _kernel_width(0),
1677 _kernel_height(0),
1678 _num_kernels(0),
1679 _retain_internal_weights(false),
1680 _weight_format(arm_compute::WeightFormat::UNSPECIFIED)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001681 {
1682 }
1683 /** Constructor
1684 *
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001685 * @param[in] are_reshaped True if the weights have been reshaped
1686 * @param[in] kernel_width Kernel width.
1687 * @param[in] kernel_height Kernel height.
1688 * @param[in] num_kernels Number of convolution kernels.
1689 * @param[in] retain_internal_weights (Optional) True if internal reshaped weights must be retained. Used for reconfiguration purposes. Default is false.
Ramy Elgammal91780022022-07-20 14:57:37 +01001690 * @param[in] weight_format (Optional) arm_gemm:WeightFormat enumeration requested by the user. Default is arm_compute::WeightFormat::UNSPECIFIED.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001691 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001692 WeightsInfo(bool are_reshaped,
1693 unsigned int kernel_width,
1694 unsigned int kernel_height,
1695 unsigned int num_kernels,
1696 bool retain_internal_weights = false,
1697 arm_compute::WeightFormat weight_format = arm_compute::WeightFormat::UNSPECIFIED)
1698 : _are_reshaped(are_reshaped),
1699 _kernel_width(kernel_width),
1700 _kernel_height(kernel_height),
1701 _num_kernels(num_kernels),
1702 _retain_internal_weights(retain_internal_weights),
1703 _weight_format(weight_format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001704 {
1705 }
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001706 /** Flag which specifies if the weights tensor has been reshaped.
1707 *
1708 * @return True if the weights tensors has been reshaped
1709 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001710 bool are_reshaped() const
1711 {
1712 return _are_reshaped;
1713 };
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001714 /** Return the number of convolution kernels
1715 *
1716 * @return The number of convolution kernels
1717 */
1718 unsigned int num_kernels() const
1719 {
1720 return _num_kernels;
1721 };
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001722 /** Return the width and height of the kernel
1723 *
1724 * @return The width and height of the kernel
1725 */
1726 std::pair<unsigned int, unsigned int> kernel_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001727 {
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001728 return std::make_pair(_kernel_width, _kernel_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001729 }
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001730 bool retain_internal_weights() const
1731 {
1732 return _retain_internal_weights;
1733 }
Ramy Elgammal91780022022-07-20 14:57:37 +01001734 arm_compute::WeightFormat weight_format() const
Francesco Petrogalli553f6952022-06-30 10:22:01 +00001735 {
1736 return _weight_format;
1737 }
Milos Puzovic13b623e2022-07-27 17:53:21 +00001738 void set_weight_format(arm_compute::WeightFormat weight_format)
1739 {
1740 _weight_format = weight_format;
1741 }
1742
Francesco Petrogalli553f6952022-06-30 10:22:01 +00001743 unsigned int kernel_width() const
1744 {
1745 return _kernel_width;
1746 }
1747 unsigned int kernel_height() const
1748 {
1749 return _kernel_height;
1750 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001751
1752private:
Ramy Elgammal91780022022-07-20 14:57:37 +01001753 bool _are_reshaped;
1754 unsigned int _kernel_width;
1755 unsigned int _kernel_height;
1756 unsigned int _num_kernels;
1757 bool _retain_internal_weights;
1758 arm_compute::WeightFormat _weight_format;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001759};
1760
Gian Marco36a0a462018-01-12 10:21:40 +00001761/** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape.
1762 *
Michele Di Giorgio93b75e02021-06-21 12:00:43 +01001763 * The matrix A can only be reshaped through @ref opencl::kernels::ClGemmReshapeLhsMatrixKernel or @ref cpu::kernels::CpuGemmInterleave4x4Kernel
Georgios Pinitas856f66e2021-04-22 21:13:21 +01001764 * 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 +00001765 *
Michele Di Giorgio93b75e02021-06-21 12:00:43 +01001766 * The matrix B can only be reshaped through @ref opencl::kernels::ClGemmReshapeRhsMatrixKernel or @ref cpu::kernels::CpuGemmTranspose1xWKernel
Georgios Pinitas856f66e2021-04-22 21:13:21 +01001767 * 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 +00001768 *
1769 */
1770class GEMMReshapeInfo final
1771{
1772public:
1773 /** Default constructor */
1774 GEMMReshapeInfo()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001775 : _m(1),
1776 _n(1),
1777 _k(1),
1778 _mult_transpose1xW_width(1),
1779 _mult_interleave4x4_height(1),
1780 _depth_output_gemm3d(0),
1781 _reinterpret_input_as_3d(false),
1782 _broadcast_bias(false)
Gian Marco36a0a462018-01-12 10:21:40 +00001783 {
1784 }
1785 /** Constructor
1786 *
1787 * @param[in] m Number of matrix A rows
1788 * @param[in] n Number of matrix B columns
1789 * @param[in] k Number of matrix A columns or matrix B rows
1790 * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block
1791 * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001792 * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel.
1793 * If 0 the output will not be reinterpreted as 3D. Default 0
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001794 * @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 +01001795 * to perform 1x1 convolutions with the NHWC data layout)
1796 * @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 +00001797 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001798 GEMMReshapeInfo(int m,
1799 int n,
1800 int k,
1801 int mult_transpose1xW_width = 1,
1802 int mult_interleave4x4_height = 1,
1803 int depth_output_gemm3d = 0,
1804 bool reinterpret_input_as_3d = false,
1805 bool broadcast_bias = false)
1806 : _m(m),
1807 _n(n),
1808 _k(k),
1809 _mult_transpose1xW_width(mult_transpose1xW_width),
1810 _mult_interleave4x4_height(mult_interleave4x4_height),
1811 _depth_output_gemm3d(depth_output_gemm3d),
1812 _reinterpret_input_as_3d(reinterpret_input_as_3d),
1813 _broadcast_bias(broadcast_bias)
Gian Marco36a0a462018-01-12 10:21:40 +00001814 {
1815 }
1816 /** Number of matrix A rows
1817 *
1818 * @return the number of matrix A rows
1819 */
1820 int m() const
1821 {
1822 return _m;
1823 }
1824 /** Number of matrix B columns
1825 *
1826 * @return the number of matrix B columns
1827 */
1828 int n() const
1829 {
1830 return _n;
1831 }
1832 /** Number of matrix A columns or matrix B rows
1833 *
1834 * @return the number of matrix A columns or matrix B rows
1835 */
1836 int k() const
1837 {
1838 return _k;
1839 }
1840 /** Multiplication factor for the width of the 1xW transposed block
1841 *
1842 * @return the multiplication factor for the width of the 1xW transposed block
1843 */
1844 int mult_transpose1xW_width() const
1845 {
1846 return _mult_transpose1xW_width;
1847 }
1848 /** Multiplication factor for the height of the 4x4 interleaved block
1849 *
1850 * @return the multiplication factor for the height of the 4x4 interleaved block
1851 */
1852 int mult_interleave4x4_height() const
1853 {
1854 return _mult_interleave4x4_height;
1855 }
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001856 /** Depth (third dimension) of the output tensor to be used with the GEMM3D kernel
1857 *
1858 * @note GEMM3D kernel is used when the output has to be reinterpret as 3D tensor. In that case:
1859 * m = depth_output_gemm3d * output_height
1860 *
1861 * @return the depth of the output tensor to be used with the GEMM3D kernel
1862 */
1863 int depth_output_gemm3d() const
1864 {
1865 return _depth_output_gemm3d;
1866 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001867 /** Flag which specifies if the input tensor has to be reinterpreted as 3D
1868 *
1869 * @return True if the input tensor has to be reinterpreted as 3D tensor
1870 */
1871 bool reinterpret_input_as_3d() const
1872 {
1873 return _reinterpret_input_as_3d;
1874 };
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001875 /** Flag which specifies whether to broadcast the shape of the bias tensor.
1876 *
1877 * @return True if the shape of the bias tensor is to be broadcasted.
1878 */
1879 bool broadcast_bias() const
1880 {
1881 return _broadcast_bias;
1882 };
Gian Marco36a0a462018-01-12 10:21:40 +00001883
1884private:
SiCong Liebd8fb42020-08-18 11:03:14 +01001885 int _m;
1886 int _n;
1887 int _k;
1888 int _mult_transpose1xW_width;
1889 int _mult_interleave4x4_height;
1890 int _depth_output_gemm3d;
1891 bool _reinterpret_input_as_3d;
1892 bool _broadcast_bias;
Gian Marco36a0a462018-01-12 10:21:40 +00001893};
1894
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +00001895/** GEMM LHS (Left Hand Side) matrix information */
1896struct GEMMLHSMatrixInfo
1897{
morgolockaba2f912020-05-05 16:28:19 +01001898 GEMMLHSMatrixInfo() = default;
1899 GEMMLHSMatrixInfo(unsigned int m, unsigned int k, unsigned int v, bool trans, bool inter)
1900 : m0(m), k0(k), v0(v), transpose(trans), interleave(inter)
1901 {
1902 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001903 unsigned int m0{1}; /**< Number of rows processed by the matrix multiplication */
1904 unsigned int k0{1}; /**< Number of partial accumulations performed by the matrix multiplication */
1905 unsigned int v0{1}; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */
1906 bool transpose{true}; /**< True if the (m0xk0) block has to be transposed before been stored */
1907 bool interleave{true}; /**< True if the v0 (m0xk0) blocks have to be interleaved in the output row */
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +00001908};
1909
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00001910/** GEMM RHS (Right Hand Side) matrix information */
1911struct GEMMRHSMatrixInfo
1912{
morgolockaba2f912020-05-05 16:28:19 +01001913 GEMMRHSMatrixInfo() = default;
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +01001914 GEMMRHSMatrixInfo(unsigned int n, unsigned int k, unsigned int h, bool trans, bool inter, bool export_to_cl_img)
1915 : n0(n), k0(k), h0(h), transpose(trans), interleave(inter), export_to_cl_image(export_to_cl_img)
morgolockaba2f912020-05-05 16:28:19 +01001916 {
1917 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001918 unsigned int n0{1}; /**< Number of columns processed by the matrix multiplication */
1919 unsigned int k0{1}; /**< Number of partial accumulations performed by the matrix multiplication */
1920 unsigned int h0{1}; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
1921 bool transpose{true}; /**< True if the (k0xn0) block has to be transposed before been stored */
1922 bool interleave{true}; /**< True if the h0 (k0xn0) blocks have to be interleaved in the output row */
1923 bool export_to_cl_image{
1924 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 +00001925};
1926
SiCongLi579ca842021-10-18 09:38:33 +01001927class ITensorInfo;
Chunosov5124be52017-11-22 20:42:13 +07001928
Gian Marco Iodice247f52c2018-03-22 11:24:56 +00001929/** Winograd information */
1930struct WinogradInfo
1931{
1932 /** Default constructor
1933 *
1934 * @param[in] output_tile_sz Width and height of the output tile
1935 * @param[in] kernel_sz Width and height of the kernel
1936 * @param[in] input_dims Width and height of the input tensor before the convolution is applied
1937 * @param[in] conv_info Convolution info (Pads, strides)
1938 * @param[in] data_layout Data layout to use for the output tensor once the convolution has been applied
1939 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001940 WinogradInfo(
1941 Size2D output_tile_sz, Size2D kernel_sz, Size2D input_dims, PadStrideInfo conv_info, DataLayout data_layout)
1942 : output_tile_size(output_tile_sz),
1943 kernel_size(kernel_sz),
1944 input_dimensions(input_dims),
1945 convolution_info(conv_info),
1946 output_data_layout(data_layout)
Gian Marco Iodice247f52c2018-03-22 11:24:56 +00001947 {
1948 }
1949
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +01001950 Size2D output_tile_size{}; /**< Width and height of the output tile */
1951 Size2D kernel_size{}; /**< Width and height of the kernel*/
1952 Size2D input_dimensions{}; /**< Width and height of the input tensor before the convolution is applied */
1953 PadStrideInfo convolution_info{}; /**< Convolution info (Pads, strides,...) */
1954 DataLayout output_data_layout{
1955 DataLayout::
1956 NCHW}; /**< Data layout to use for the output tensor once the convolution has been applied (NCHW or NHWC) */
Gian Marco Iodice247f52c2018-03-22 11:24:56 +00001957};
1958
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001959/** IO formatting information class*/
1960struct IOFormatInfo
1961{
1962 /** Precision type used when printing floating point numbers */
1963 enum class PrecisionType
1964 {
1965 Default, /**< Default precision to the one that the current stream has */
1966 Custom, /**< Custom precision specified by the user using the precision parameter */
1967 Full /**< The maximum precision of the floating point representation */
1968 };
1969
1970 /** Specifies the area to be printed, used by Tensor objects */
1971 enum class PrintRegion
1972 {
1973 ValidRegion, /**< Prints the valid region of the Tensor object */
1974 NoPadding, /**< Prints the Tensor object without the padding */
1975 Full /**< Print the tensor object including padding */
1976 };
1977
Alex Gildayc357c472018-03-21 13:54:09 +00001978 /** Construct a set of IO formatting information.
1979 *
1980 * @param[in] print_region Area to be printed. Used by Tensor objects. Default: ValidRegion.
1981 * @param[in] precision_type Precision type for floating point numbers. Default: stream default.
1982 * @param[in] precision Precision value for float point numbers. Default: 10.
1983 * @param[in] align_columns Whether to align columns when printed. Default: true.
1984 * @param[in] element_delim Delimeter between elements. Default: " ".
1985 * @param[in] row_delim Delimenter between rows. Default: "\n".
1986 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001987 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
1988 PrecisionType precision_type = PrecisionType::Default,
1989 unsigned int precision = 10,
1990 bool align_columns = true,
1991 std::string element_delim = " ",
1992 std::string row_delim = "\n")
1993 : print_region(print_region),
1994 precision_type(precision_type),
1995 precision(precision),
1996 element_delim(element_delim),
1997 row_delim(row_delim),
1998 align_columns(align_columns)
1999 {
2000 }
2001
Alex Gildayc357c472018-03-21 13:54:09 +00002002 /** Area to be printed by Tensor objects */
2003 PrintRegion print_region;
2004 /** Floating point precision type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002005 PrecisionType precision_type;
Alex Gildayc357c472018-03-21 13:54:09 +00002006 /** Floating point precision */
2007 unsigned int precision;
2008 /** Element delimeter */
2009 std::string element_delim;
2010 /** Row delimeter */
2011 std::string row_delim;
2012 /** Align columns */
2013 bool align_columns;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002014};
Mohammed Suhail Munshid538d162023-02-16 16:22:32 +00002015
SiCong Li4ceb4532023-03-13 15:02:23 +00002016/** Class for holding information related to cropping */
2017using CropInfo = Padding2D;
Georgios Pinitasd8734b52017-12-22 15:27:52 +00002018} // namespace arm_compute
Jakub Sujak0d27b2e2023-08-24 14:01:20 +01002019#endif // ACL_ARM_COMPUTE_CORE_TYPES_H