2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
7 #include "Deprecated.hpp"
8 #include "DescriptorsFwd.hpp"
11 #include <initializer_list>
19 /// An ActivationDescriptor for the ActivationLayer.
20 struct ActivationDescriptor
22 ActivationDescriptor() : m_Function(ActivationFunction::Sigmoid), m_A(0), m_B(0) {}
24 /// @brief The activation function to use
25 /// (Sigmoid, TanH, Linear, ReLu, BoundedReLu, SoftReLu, LeakyReLu, Abs, Sqrt, Square).
26 ActivationFunction m_Function;
27 /// Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH).
29 /// Beta lower bound value used by the activation functions. (BoundedReLu, Linear, TanH).
33 /// A PermuteDescriptor for the PermuteLayer.
34 struct PermuteDescriptor
40 PermuteDescriptor(const PermutationVector& dimMappings)
41 : m_DimMappings(dimMappings)
44 /// @brief Indicates how to translate tensor elements from a given source into the target destination, when
45 /// source and target potentially have different memory layouts e.g. {0U, 3U, 1U, 2U}.
46 PermutationVector m_DimMappings;
49 /// A SoftmaxDescriptor for the SoftmaxLayer.
50 struct SoftmaxDescriptor
57 /// Exponentiation value.
59 /// Scalar, defaulted to the last index (-1), specifying the dimension the activation will be performed on.
63 /// @brief An OriginsDescriptor for the ConcatLayer.
64 /// Descriptor to configure the concatenation process. Number of views must be equal to the number of inputs, and
65 /// their order must match - e.g. first view corresponds to the first input, second view to the second input, etc.
66 struct OriginsDescriptor
69 OriginsDescriptor(uint32_t numViews, uint32_t numDimensions = 4);
70 OriginsDescriptor(const OriginsDescriptor& other);
71 OriginsDescriptor(OriginsDescriptor&& other);
75 OriginsDescriptor& operator=(OriginsDescriptor rhs);
77 /// @Brief Set the view origin coordinates. The arguments are: view, dimension, value.
78 /// If the view is greater than or equal to GetNumViews(), then the view argument is out of range.
79 /// If the coord is greater than or equal to GetNumDimensions(), then the coord argument is out of range.
80 Status SetViewOriginCoord(uint32_t view, uint32_t coord, uint32_t value);
81 /// Get the number of views.
82 uint32_t GetNumViews() const;
83 /// Get the number of dimensions.
84 uint32_t GetNumDimensions() const;
85 /// Return the view origin at the int value idx.
86 const uint32_t* GetViewOrigin(uint32_t idx) const;
87 /// @brief Reorders the viewOrigins in accordance with the indices presented in newOrdering array.
88 /// The number of views must match number of elements in the new ordering array.
89 void ReorderOrigins(unsigned int* newOrdering, unsigned int numNewOrdering);
90 /// Swap the ViewsDescriptor values first and second.
91 friend void swap(OriginsDescriptor& first, OriginsDescriptor& second);
92 /// Set the concatenation axis value.
93 void SetConcatAxis(unsigned int concatAxis);
94 /// Get the concatenation axis value.
95 unsigned int GetConcatAxis() const;
98 unsigned int m_ConcatAxis;
100 uint32_t m_NumDimensions;
101 uint32_t** m_ViewOrigins;
104 /// @brief A ViewsDescriptor for the SplitterLayer.
105 /// Descriptor to configure the splitting process. Number of Views must be equal to the number of outputs, and
106 /// their order must match - e.g. first view corresponds to the first output, second view to the second output, etc.
107 struct ViewsDescriptor
109 ViewsDescriptor(uint32_t numViews, uint32_t numDimensions = 4);
110 ViewsDescriptor(const ViewsDescriptor& other);
112 ViewsDescriptor(ViewsDescriptor&& other);
116 ViewsDescriptor& operator=(ViewsDescriptor rhs);
117 /// @Brief Set the view origin coordinates. The arguments are: view, dimension, value.
118 /// If the view is greater than or equal to GetNumViews(), then the view argument is out of range.
119 /// If the coord is greater than or equal to GetNumDimensions(), then the coord argument is out of range.
120 Status SetViewOriginCoord(uint32_t view, uint32_t coord, uint32_t value);
121 /// @brief Set the size of the views. The arguments are: view, dimension, value.
122 /// If the view is greater than or equal to GetNumViews(), then the view argument is out of range.
123 /// If the coord is greater than or equal to GetNumDimensions(), then the coord argument is out of range.
124 Status SetViewSize(uint32_t view, uint32_t coord, uint32_t value);
126 /// Get the number of views.
127 uint32_t GetNumViews() const;
128 /// Get the number of dimensions.
129 uint32_t GetNumDimensions() const;
130 /// Get the view origin at the int value idx.
131 const uint32_t* GetViewOrigin(uint32_t idx) const;
132 /// Get the view sizes at the int value idx.
133 const uint32_t* GetViewSizes(uint32_t idx) const;
134 /// Get the View Origins
135 const OriginsDescriptor& GetOrigins() const;
137 /// Swap the ViewsDescriptor value first and second.
138 friend void swap(ViewsDescriptor& first, ViewsDescriptor& second);
140 OriginsDescriptor m_Origins;
141 uint32_t** m_ViewSizes;
144 template <typename TensorShapeIt>
145 ARMNN_DEPRECATED_MSG("Use CreateDescriptorForConcatenation instead")
146 OriginsDescriptor CreateMergerDescriptorForConcatenation(TensorShapeIt first,
148 unsigned int concatenationDimension)
150 return CreateDescriptorForConcatenation(first, last, concatenationDimension);
153 /// @brief Convenience template to create an OriginsDescriptor to use when creating a ConcatLayer for performing
154 /// concatenation of a number of input tensors.
155 template <typename TensorShapeIt>
156 OriginsDescriptor CreateDescriptorForConcatenation(TensorShapeIt first,
158 unsigned int concatenationDimension)
160 auto numInputs = std::distance(first, last);
164 throw InvalidArgumentException("Concatenation requires at least 2 inputs");
167 const auto& firstInputShape = *first;
169 const unsigned int numDimensions = firstInputShape.GetNumDimensions();
170 for (auto it = first + 1; it != last; ++it)
172 if (it->GetNumDimensions() != numDimensions)
174 throw InvalidArgumentException("All inputs to concatenation must have the same number of dimensions");
178 if (concatenationDimension >= numDimensions)
180 throw InvalidArgumentException("concatenationDimension must be between 0 and the number of dimensions.");
183 for (auto it = first; it != last; ++it)
185 for (unsigned int d = 0; d < numDimensions; ++d)
187 const bool dimSizeOk = (d == concatenationDimension) || (firstInputShape[d] == (*it)[d]);
190 throw InvalidArgumentException("All inputs to concatenation must be the same size along all dimensions "
191 " except the concatenation dimension");
196 OriginsDescriptor viewsDescriptor(static_cast<uint32_t>(numInputs), numDimensions);
197 viewsDescriptor.SetConcatAxis(concatenationDimension);
199 uint32_t viewIndex = 0u;
200 uint32_t coordAlongConcatDim = 0u;
201 for (auto it = first; it != last; ++it)
203 const auto& inputShape = *it;
205 for (unsigned int i = 0; i < concatenationDimension; ++i)
207 viewsDescriptor.SetViewOriginCoord(viewIndex, i, 0);
210 viewsDescriptor.SetViewOriginCoord(viewIndex, concatenationDimension, coordAlongConcatDim);
211 unsigned int dimSize = inputShape[concatenationDimension];
212 coordAlongConcatDim += dimSize;
215 for (unsigned int i = concatenationDimension + 1; i < numDimensions; ++i)
217 viewsDescriptor.SetViewOriginCoord(viewIndex, i, 0);
223 return viewsDescriptor;
226 /// A Pooling2dDescriptor for the Pooling2dLayer.
227 struct Pooling2dDescriptor
229 Pooling2dDescriptor()
230 : m_PoolType(PoolingAlgorithm::Max)
239 , m_OutputShapeRounding(OutputShapeRounding::Floor)
240 , m_PaddingMethod(PaddingMethod::Exclude)
241 , m_DataLayout(DataLayout::NCHW)
244 /// The pooling algorithm to use (Max. Average, L2).
245 PoolingAlgorithm m_PoolType;
246 /// Padding left value in the width dimension.
248 /// Padding right value in the width dimension.
250 /// Padding top value in the height dimension.
252 /// Padding bottom value in the height dimension.
253 uint32_t m_PadBottom;
254 /// Pooling width value.
255 uint32_t m_PoolWidth;
256 /// Pooling height value.
257 uint32_t m_PoolHeight;
258 /// Stride value when proceeding through input for the width dimension.
260 /// Stride value when proceeding through input for the height dimension.
262 /// The rounding method for the output shape. (Floor, Ceiling).
263 OutputShapeRounding m_OutputShapeRounding;
264 /// The padding method to be used. (Exclude, IgnoreValue).
265 PaddingMethod m_PaddingMethod;
266 /// The data layout to be used (NCHW, NHWC).
267 DataLayout m_DataLayout;
270 /// A FullyConnectedDescriptor for the FullyConnectedLayer.
271 struct FullyConnectedDescriptor
273 FullyConnectedDescriptor()
274 : m_BiasEnabled(false)
275 , m_TransposeWeightMatrix(false)
278 /// Enable/disable bias.
280 /// Enable/disable transpose weight matrix.
281 bool m_TransposeWeightMatrix;
284 /// A Convolution2dDescriptor for the Convolution2dLayer.
285 struct Convolution2dDescriptor
287 Convolution2dDescriptor()
296 , m_BiasEnabled(false)
297 , m_DataLayout(DataLayout::NCHW)
300 /// Padding left value in the width dimension.
302 /// Padding right value in the width dimension.
304 /// Padding top value in the height dimension.
306 /// Padding bottom value in the height dimension.
307 uint32_t m_PadBottom;
308 /// Stride value when proceeding through input for the width dimension.
310 /// Stride value when proceeding through input for the height dimension.
312 /// Dilation along x axis
313 uint32_t m_DilationX;
314 /// Dilation along y axis
315 uint32_t m_DilationY;
316 /// Enable/disable bias.
318 /// The data layout to be used (NCHW, NHWC).
319 DataLayout m_DataLayout;
322 /// A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
323 struct DepthwiseConvolution2dDescriptor
325 DepthwiseConvolution2dDescriptor()
334 , m_BiasEnabled(false)
335 , m_DataLayout(DataLayout::NCHW)
338 /// Padding left value in the width dimension.
340 /// Padding right value in the width dimension.
342 /// Padding top value in the height dimension.
344 /// Padding bottom value in the height dimension.
345 uint32_t m_PadBottom;
346 /// Stride value when proceeding through input for the width dimension.
348 /// Stride value when proceeding through input for the height dimension.
350 /// Dilation factor value for width dimension.
351 uint32_t m_DilationX;
352 /// Dilation factor value for height dimension.
353 uint32_t m_DilationY;
354 /// Enable/disable bias.
356 /// The data layout to be used (NCHW, NHWC).
357 DataLayout m_DataLayout;
360 struct DetectionPostProcessDescriptor
362 DetectionPostProcessDescriptor()
364 , m_MaxClassesPerDetection(1)
365 , m_DetectionsPerClass(1)
366 , m_NmsScoreThreshold(0)
367 , m_NmsIouThreshold(0)
369 , m_UseRegularNms(false)
376 /// Maximum numbers of detections.
377 uint32_t m_MaxDetections;
378 /// Maximum numbers of classes per detection, used in Fast NMS.
379 uint32_t m_MaxClassesPerDetection;
380 /// Detections per classes, used in Regular NMS.
381 uint32_t m_DetectionsPerClass;
382 /// NMS score threshold.
383 float m_NmsScoreThreshold;
384 /// Intersection over union threshold.
385 float m_NmsIouThreshold;
386 /// Number of classes.
387 uint32_t m_NumClasses;
389 bool m_UseRegularNms;
390 /// Center size encoding scale x.
392 /// Center size encoding scale y.
394 /// Center size encoding scale weight.
396 /// Center size encoding scale height.
400 /// A NormalizationDescriptor for the NormalizationLayer.
401 struct NormalizationDescriptor
403 NormalizationDescriptor()
404 : m_NormChannelType(NormalizationAlgorithmChannel::Across)
405 , m_NormMethodType(NormalizationAlgorithmMethod::LocalBrightness)
410 , m_DataLayout(DataLayout::NCHW)
413 /// Normalization channel algorithm to use (Across, Within).
414 NormalizationAlgorithmChannel m_NormChannelType;
415 /// Normalization method algorithm to use (LocalBrightness, LocalContrast).
416 NormalizationAlgorithmMethod m_NormMethodType;
417 /// Depth radius value.
419 /// Alpha value for the normalization equation.
421 /// Beta value for the normalization equation.
423 /// Kappa value used for the across channel normalization equation.
425 /// The data layout to be used (NCHW, NHWC).
426 DataLayout m_DataLayout;
429 /// A L2NormalizationDescriptor for the L2NormalizationLayer.
430 struct L2NormalizationDescriptor
432 L2NormalizationDescriptor()
434 , m_DataLayout(DataLayout::NCHW)
437 /// Used to avoid dividing by zero.
439 /// The data layout to be used (NCHW, NHWC).
440 DataLayout m_DataLayout;
443 /// A BatchNormalizationDescriptor for the BatchNormalizationLayer.
444 struct BatchNormalizationDescriptor
446 BatchNormalizationDescriptor()
448 , m_DataLayout(DataLayout::NCHW)
451 /// Value to add to the variance. Used to avoid dividing by zero.
453 /// The data layout to be used (NCHW, NHWC).
454 DataLayout m_DataLayout;
457 /// A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
458 struct BatchToSpaceNdDescriptor
460 BatchToSpaceNdDescriptor()
461 : m_BlockShape({1, 1})
462 , m_Crops({{0, 0}, {0, 0}})
463 , m_DataLayout(DataLayout::NCHW)
466 BatchToSpaceNdDescriptor(std::vector<unsigned int> blockShape,
467 std::vector<std::pair<unsigned int, unsigned int>> crops)
468 : m_BlockShape(blockShape)
470 , m_DataLayout(DataLayout::NCHW)
473 /// Block shape values.
474 std::vector<unsigned int> m_BlockShape;
475 /// The values to crop from the input dimension.
476 std::vector<std::pair<unsigned int, unsigned int>> m_Crops;
477 /// The data layout to be used (NCHW, NHWC).
478 DataLayout m_DataLayout;
481 /// A FakeQuantizationDescriptor for the FakeQuantizationLayer.
482 struct FakeQuantizationDescriptor
484 FakeQuantizationDescriptor()
495 /// A ResizeBilinearDescriptor for the ResizeBilinearLayer.
496 struct ResizeBilinearDescriptor
498 ResizeBilinearDescriptor()
501 , m_DataLayout(DataLayout::NCHW)
504 /// Target width value.
505 uint32_t m_TargetWidth;
506 /// Target height value.
507 uint32_t m_TargetHeight;
508 /// The data layout to be used (NCHW, NHWC).
509 DataLayout m_DataLayout;
512 /// A ResizeDescriptor for the ResizeLayer.
513 struct ResizeDescriptor
518 , m_Method(ResizeMethod::NearestNeighbor)
519 , m_DataLayout(DataLayout::NCHW)
522 /// Target width value.
523 uint32_t m_TargetWidth;
524 /// Target height value.
525 uint32_t m_TargetHeight;
526 /// The Interpolation method to use
527 /// (Bilinear, NearestNeighbor).
528 ResizeMethod m_Method;
529 /// The data layout to be used (NCHW, NHWC).
530 DataLayout m_DataLayout;
534 /// A ReshapeDescriptor for the ReshapeLayer.
535 struct ReshapeDescriptor
541 ReshapeDescriptor(const TensorShape& shape)
542 : m_TargetShape(shape)
545 /// Target shape value.
546 TensorShape m_TargetShape;
549 /// A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
550 struct SpaceToBatchNdDescriptor
552 SpaceToBatchNdDescriptor()
553 : m_BlockShape({1, 1})
554 , m_PadList({{0, 0}, {0, 0}})
555 , m_DataLayout(DataLayout::NCHW)
558 SpaceToBatchNdDescriptor(const std::vector<unsigned int>& blockShape,
559 const std::vector<std::pair<unsigned int, unsigned int>>& padList)
560 : m_BlockShape(blockShape)
562 , m_DataLayout(DataLayout::NCHW)
565 /// Block shape value.
566 std::vector<unsigned int> m_BlockShape;
567 /// @brief Specifies the padding values for the input dimension:
568 /// heightPad{top, bottom} widthPad{left, right}.
569 std::vector<std::pair<unsigned int, unsigned int>> m_PadList;
570 /// The data layout to be used (NCHW, NHWC).
571 DataLayout m_DataLayout;
574 /// A SpaceToDepthDescriptor for the SpaceToDepthLayer
575 struct SpaceToDepthDescriptor
577 SpaceToDepthDescriptor()
579 , m_DataLayout(DataLayout::NHWC)
582 /// Scalar specifying the input block size. It must be >= 1
583 unsigned int m_BlockSize;
584 /// The data layout to be used (NCHW, NHWC).
585 DataLayout m_DataLayout;
588 /// An LstmDescriptor for the LstmLayer.
589 struct LstmDescriptor
592 : m_ActivationFunc(1) // 0: None, 1: Relu, 3: Relu6, 4: Tanh, 6: Sigmoid
593 , m_ClippingThresCell(0.0)
594 , m_ClippingThresProj(0.0)
595 , m_CifgEnabled(true)
596 , m_PeepholeEnabled(false)
597 , m_ProjectionEnabled(false)
598 , m_LayerNormEnabled(false)
601 /// @brief The activation function to use.
602 /// 0: None, 1: Relu, 3: Relu6, 4: Tanh, 6: Sigmoid.
603 uint32_t m_ActivationFunc;
604 /// Clipping threshold value for the cell state.
605 float m_ClippingThresCell;
606 /// Clipping threshold value for the projection.
607 float m_ClippingThresProj;
608 /// Enable/disable cifg (coupled input & forget gate).
610 /// Enable/disable peephole.
611 bool m_PeepholeEnabled;
612 /// Enable/disable the projection layer.
613 bool m_ProjectionEnabled;
614 /// Enable/disable layer normalization
615 bool m_LayerNormEnabled;
618 /// A MeanDescriptor for the MeanLayer.
619 struct MeanDescriptor
626 MeanDescriptor(const std::vector<unsigned int>& axis, bool keepDims)
628 , m_KeepDims(keepDims)
631 /// Values for the dimensions to reduce.
632 std::vector<unsigned int> m_Axis;
633 /// Enable/disable keep dimensions. If true, then the reduced dimensions that are of length 1 are kept.
637 /// A PadDescriptor for the PadLayer.
640 PadDescriptor() : m_PadValue(0)
643 PadDescriptor(const std::vector<std::pair<unsigned int, unsigned int>>& padList, const float& padValue = 0)
644 : m_PadList(padList), m_PadValue(padValue)
647 /// @brief Specifies the padding for input dimension.
648 /// First is the number of values to add before the tensor in the dimension.
649 /// Second is the number of values to add after the tensor in the dimension.
650 /// The number of pairs should match the number of dimensions in the input tensor.
651 std::vector<std::pair<unsigned int, unsigned int>> m_PadList;
653 /// Optional value to use for padding, defaults to 0
657 /// A StackDescriptor for the StackLayer.
658 struct StackDescriptor
666 StackDescriptor(uint32_t axis, uint32_t numInputs, const TensorShape& inputShape)
668 , m_NumInputs(numInputs)
669 , m_InputShape(inputShape)
672 /// 0-based axis along which to stack the input tensors.
674 /// Number of input tensors.
675 uint32_t m_NumInputs;
676 /// Required shape of all input tensors.
677 TensorShape m_InputShape;
680 /// A StridedSliceDescriptor for the StridedSliceLayer.
681 struct StridedSliceDescriptor
683 StridedSliceDescriptor(const std::vector<int>& begin,
684 const std::vector<int>& end,
685 const std::vector<int>& stride)
691 , m_ShrinkAxisMask(0)
694 , m_DataLayout(DataLayout::NCHW)
697 StridedSliceDescriptor()
698 : StridedSliceDescriptor({}, {}, {})
701 int GetStartForAxis(const TensorShape& inputShape, unsigned int axis) const;
702 int GetStopForAxis(const TensorShape& inputShape,
704 int startForAxis) const;
706 /// Begin values for the input that will be sliced.
707 std::vector<int> m_Begin;
708 /// End values for the input that will be sliced.
709 std::vector<int> m_End;
710 /// Stride values for the input that will be sliced.
711 std::vector<int> m_Stride;
713 /// @brief Begin mask value. If set, then the begin is disregarded and the fullest
714 /// range is used for the dimension.
716 /// @brief End mask value. If set, then the end is disregarded and the fullest range
717 /// is used for the dimension.
719 /// Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1.
720 int32_t m_ShrinkAxisMask;
721 /// Ellipsis mask value.
722 int32_t m_EllipsisMask;
723 /// @brief New axis mask value. If set, the begin, end and stride is disregarded and
724 /// a new 1 dimension is inserted to this location of the output tensor.
725 int32_t m_NewAxisMask;
727 /// The data layout to be used (NCHW, NHWC).
728 DataLayout m_DataLayout;
731 /// A PreCompiledDescriptor for the PreCompiledLayer.
732 struct PreCompiledDescriptor
734 PreCompiledDescriptor(unsigned int numInputSlots = 1u, unsigned int numOutputSlots = 1u)
735 : m_NumInputSlots(numInputSlots), m_NumOutputSlots(numOutputSlots)
738 ~PreCompiledDescriptor() = default;
740 unsigned int m_NumInputSlots;
741 unsigned int m_NumOutputSlots;
744 /// A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
745 struct TransposeConvolution2dDescriptor
747 TransposeConvolution2dDescriptor() :
754 m_BiasEnabled(false),
755 m_DataLayout(DataLayout::NCHW)
758 /// Padding left value in the width dimension.
760 /// Padding right value in the width dimension.
762 /// Padding top value in the height dimension.
764 /// Padding bottom value in the height dimension.
765 uint32_t m_PadBottom;
766 /// Stride value when proceeding through input for the width dimension.
768 /// Stride value when proceeding through input for the height dimension.
770 /// Enable/disable bias.
772 /// The data layout to be used (NCHW, NHWC).
773 DataLayout m_DataLayout;