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 /// An ArgMinMaxDescriptor for ArgMinMaxLayer
34 struct ArgMinMaxDescriptor
42 /// A PermuteDescriptor for the PermuteLayer.
43 struct PermuteDescriptor
49 PermuteDescriptor(const PermutationVector& dimMappings)
50 : m_DimMappings(dimMappings)
53 /// @brief Indicates how to translate tensor elements from a given source into the target destination, when
54 /// source and target potentially have different memory layouts e.g. {0U, 3U, 1U, 2U}.
55 PermutationVector m_DimMappings;
58 /// A SoftmaxDescriptor for the SoftmaxLayer.
59 struct SoftmaxDescriptor
66 /// Exponentiation value.
68 /// Scalar, defaulted to the last index (-1), specifying the dimension the activation will be performed on.
72 /// @brief An OriginsDescriptor for the ConcatLayer.
73 /// Descriptor to configure the concatenation process. Number of views must be equal to the number of inputs, and
74 /// their order must match - e.g. first view corresponds to the first input, second view to the second input, etc.
75 struct OriginsDescriptor
78 OriginsDescriptor(uint32_t numViews, uint32_t numDimensions = 4);
79 OriginsDescriptor(const OriginsDescriptor& other);
80 OriginsDescriptor(OriginsDescriptor&& other);
84 OriginsDescriptor& operator=(OriginsDescriptor rhs);
86 /// @Brief Set the view origin coordinates. The arguments are: view, dimension, value.
87 /// If the view is greater than or equal to GetNumViews(), then the view argument is out of range.
88 /// If the coord is greater than or equal to GetNumDimensions(), then the coord argument is out of range.
89 Status SetViewOriginCoord(uint32_t view, uint32_t coord, uint32_t value);
90 /// Get the number of views.
91 uint32_t GetNumViews() const;
92 /// Get the number of dimensions.
93 uint32_t GetNumDimensions() const;
94 /// Return the view origin at the int value idx.
95 const uint32_t* GetViewOrigin(uint32_t idx) const;
96 /// @brief Reorders the viewOrigins in accordance with the indices presented in newOrdering array.
97 /// The number of views must match number of elements in the new ordering array.
98 void ReorderOrigins(unsigned int* newOrdering, unsigned int numNewOrdering);
99 /// Swap the ViewsDescriptor values first and second.
100 friend void swap(OriginsDescriptor& first, OriginsDescriptor& second);
101 /// Set the concatenation axis value.
102 void SetConcatAxis(unsigned int concatAxis);
103 /// Get the concatenation axis value.
104 unsigned int GetConcatAxis() const;
107 unsigned int m_ConcatAxis;
109 uint32_t m_NumDimensions;
110 uint32_t** m_ViewOrigins;
113 /// @brief A ViewsDescriptor for the SplitterLayer.
114 /// Descriptor to configure the splitting process. Number of Views must be equal to the number of outputs, and
115 /// their order must match - e.g. first view corresponds to the first output, second view to the second output, etc.
116 struct ViewsDescriptor
118 ViewsDescriptor(uint32_t numViews, uint32_t numDimensions = 4);
119 ViewsDescriptor(const ViewsDescriptor& other);
121 ViewsDescriptor(ViewsDescriptor&& other);
125 ViewsDescriptor& operator=(ViewsDescriptor rhs);
126 /// @Brief Set the view origin coordinates. The arguments are: view, dimension, value.
127 /// If the view is greater than or equal to GetNumViews(), then the view argument is out of range.
128 /// If the coord is greater than or equal to GetNumDimensions(), then the coord argument is out of range.
129 Status SetViewOriginCoord(uint32_t view, uint32_t coord, uint32_t value);
130 /// @brief Set the size of the views. The arguments are: view, dimension, value.
131 /// If the view is greater than or equal to GetNumViews(), then the view argument is out of range.
132 /// If the coord is greater than or equal to GetNumDimensions(), then the coord argument is out of range.
133 Status SetViewSize(uint32_t view, uint32_t coord, uint32_t value);
135 /// Get the number of views.
136 uint32_t GetNumViews() const;
137 /// Get the number of dimensions.
138 uint32_t GetNumDimensions() const;
139 /// Get the view origin at the int value idx.
140 const uint32_t* GetViewOrigin(uint32_t idx) const;
141 /// Get the view sizes at the int value idx.
142 const uint32_t* GetViewSizes(uint32_t idx) const;
143 /// Get the View Origins
144 const OriginsDescriptor& GetOrigins() const;
146 /// Swap the ViewsDescriptor value first and second.
147 friend void swap(ViewsDescriptor& first, ViewsDescriptor& second);
149 OriginsDescriptor m_Origins;
150 uint32_t** m_ViewSizes;
153 template <typename TensorShapeIt>
154 ARMNN_DEPRECATED_MSG("Use CreateDescriptorForConcatenation instead")
155 OriginsDescriptor CreateMergerDescriptorForConcatenation(TensorShapeIt first,
157 unsigned int concatenationDimension)
159 return CreateDescriptorForConcatenation(first, last, concatenationDimension);
162 /// @brief Convenience template to create an OriginsDescriptor to use when creating a ConcatLayer for performing
163 /// concatenation of a number of input tensors.
164 template <typename TensorShapeIt>
165 OriginsDescriptor CreateDescriptorForConcatenation(TensorShapeIt first,
167 unsigned int concatenationDimension)
169 auto numInputs = std::distance(first, last);
173 throw InvalidArgumentException("Concatenation requires at least 2 inputs");
176 const auto& firstInputShape = *first;
178 const unsigned int numDimensions = firstInputShape.GetNumDimensions();
179 for (auto it = first + 1; it != last; ++it)
181 if (it->GetNumDimensions() != numDimensions)
183 throw InvalidArgumentException("All inputs to concatenation must have the same number of dimensions");
187 if (concatenationDimension >= numDimensions)
189 throw InvalidArgumentException("concatenationDimension must be between 0 and the number of dimensions.");
192 for (auto it = first; it != last; ++it)
194 for (unsigned int d = 0; d < numDimensions; ++d)
196 const bool dimSizeOk = (d == concatenationDimension) || (firstInputShape[d] == (*it)[d]);
199 throw InvalidArgumentException("All inputs to concatenation must be the same size along all dimensions "
200 " except the concatenation dimension");
205 OriginsDescriptor viewsDescriptor(static_cast<uint32_t>(numInputs), numDimensions);
206 viewsDescriptor.SetConcatAxis(concatenationDimension);
208 uint32_t viewIndex = 0u;
209 uint32_t coordAlongConcatDim = 0u;
210 for (auto it = first; it != last; ++it)
212 const auto& inputShape = *it;
214 for (unsigned int i = 0; i < concatenationDimension; ++i)
216 viewsDescriptor.SetViewOriginCoord(viewIndex, i, 0);
219 viewsDescriptor.SetViewOriginCoord(viewIndex, concatenationDimension, coordAlongConcatDim);
220 unsigned int dimSize = inputShape[concatenationDimension];
221 coordAlongConcatDim += dimSize;
224 for (unsigned int i = concatenationDimension + 1; i < numDimensions; ++i)
226 viewsDescriptor.SetViewOriginCoord(viewIndex, i, 0);
232 return viewsDescriptor;
235 /// A Pooling2dDescriptor for the Pooling2dLayer.
236 struct Pooling2dDescriptor
238 Pooling2dDescriptor()
239 : m_PoolType(PoolingAlgorithm::Max)
248 , m_OutputShapeRounding(OutputShapeRounding::Floor)
249 , m_PaddingMethod(PaddingMethod::Exclude)
250 , m_DataLayout(DataLayout::NCHW)
253 /// The pooling algorithm to use (Max. Average, L2).
254 PoolingAlgorithm m_PoolType;
255 /// Padding left value in the width dimension.
257 /// Padding right value in the width dimension.
259 /// Padding top value in the height dimension.
261 /// Padding bottom value in the height dimension.
262 uint32_t m_PadBottom;
263 /// Pooling width value.
264 uint32_t m_PoolWidth;
265 /// Pooling height value.
266 uint32_t m_PoolHeight;
267 /// Stride value when proceeding through input for the width dimension.
269 /// Stride value when proceeding through input for the height dimension.
271 /// The rounding method for the output shape. (Floor, Ceiling).
272 OutputShapeRounding m_OutputShapeRounding;
273 /// The padding method to be used. (Exclude, IgnoreValue).
274 PaddingMethod m_PaddingMethod;
275 /// The data layout to be used (NCHW, NHWC).
276 DataLayout m_DataLayout;
279 /// A FullyConnectedDescriptor for the FullyConnectedLayer.
280 struct FullyConnectedDescriptor
282 FullyConnectedDescriptor()
283 : m_BiasEnabled(false)
284 , m_TransposeWeightMatrix(false)
287 /// Enable/disable bias.
289 /// Enable/disable transpose weight matrix.
290 bool m_TransposeWeightMatrix;
293 /// A Convolution2dDescriptor for the Convolution2dLayer.
294 struct Convolution2dDescriptor
296 Convolution2dDescriptor()
305 , m_BiasEnabled(false)
306 , m_DataLayout(DataLayout::NCHW)
309 /// Padding left value in the width dimension.
311 /// Padding right value in the width dimension.
313 /// Padding top value in the height dimension.
315 /// Padding bottom value in the height dimension.
316 uint32_t m_PadBottom;
317 /// Stride value when proceeding through input for the width dimension.
319 /// Stride value when proceeding through input for the height dimension.
321 /// Dilation along x axis
322 uint32_t m_DilationX;
323 /// Dilation along y axis
324 uint32_t m_DilationY;
325 /// Enable/disable bias.
327 /// The data layout to be used (NCHW, NHWC).
328 DataLayout m_DataLayout;
331 /// A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
332 struct DepthwiseConvolution2dDescriptor
334 DepthwiseConvolution2dDescriptor()
343 , m_BiasEnabled(false)
344 , m_DataLayout(DataLayout::NCHW)
347 /// Padding left value in the width dimension.
349 /// Padding right value in the width dimension.
351 /// Padding top value in the height dimension.
353 /// Padding bottom value in the height dimension.
354 uint32_t m_PadBottom;
355 /// Stride value when proceeding through input for the width dimension.
357 /// Stride value when proceeding through input for the height dimension.
359 /// Dilation factor value for width dimension.
360 uint32_t m_DilationX;
361 /// Dilation factor value for height dimension.
362 uint32_t m_DilationY;
363 /// Enable/disable bias.
365 /// The data layout to be used (NCHW, NHWC).
366 DataLayout m_DataLayout;
369 struct DetectionPostProcessDescriptor
371 DetectionPostProcessDescriptor()
373 , m_MaxClassesPerDetection(1)
374 , m_DetectionsPerClass(1)
375 , m_NmsScoreThreshold(0)
376 , m_NmsIouThreshold(0)
378 , m_UseRegularNms(false)
385 /// Maximum numbers of detections.
386 uint32_t m_MaxDetections;
387 /// Maximum numbers of classes per detection, used in Fast NMS.
388 uint32_t m_MaxClassesPerDetection;
389 /// Detections per classes, used in Regular NMS.
390 uint32_t m_DetectionsPerClass;
391 /// NMS score threshold.
392 float m_NmsScoreThreshold;
393 /// Intersection over union threshold.
394 float m_NmsIouThreshold;
395 /// Number of classes.
396 uint32_t m_NumClasses;
398 bool m_UseRegularNms;
399 /// Center size encoding scale x.
401 /// Center size encoding scale y.
403 /// Center size encoding scale weight.
405 /// Center size encoding scale height.
409 /// A NormalizationDescriptor for the NormalizationLayer.
410 struct NormalizationDescriptor
412 NormalizationDescriptor()
413 : m_NormChannelType(NormalizationAlgorithmChannel::Across)
414 , m_NormMethodType(NormalizationAlgorithmMethod::LocalBrightness)
419 , m_DataLayout(DataLayout::NCHW)
422 /// Normalization channel algorithm to use (Across, Within).
423 NormalizationAlgorithmChannel m_NormChannelType;
424 /// Normalization method algorithm to use (LocalBrightness, LocalContrast).
425 NormalizationAlgorithmMethod m_NormMethodType;
426 /// Depth radius value.
428 /// Alpha value for the normalization equation.
430 /// Beta value for the normalization equation.
432 /// Kappa value used for the across channel normalization equation.
434 /// The data layout to be used (NCHW, NHWC).
435 DataLayout m_DataLayout;
438 /// A L2NormalizationDescriptor for the L2NormalizationLayer.
439 struct L2NormalizationDescriptor
441 L2NormalizationDescriptor()
443 , m_DataLayout(DataLayout::NCHW)
446 /// Used to avoid dividing by zero.
448 /// The data layout to be used (NCHW, NHWC).
449 DataLayout m_DataLayout;
452 /// A BatchNormalizationDescriptor for the BatchNormalizationLayer.
453 struct BatchNormalizationDescriptor
455 BatchNormalizationDescriptor()
457 , m_DataLayout(DataLayout::NCHW)
460 /// Value to add to the variance. Used to avoid dividing by zero.
462 /// The data layout to be used (NCHW, NHWC).
463 DataLayout m_DataLayout;
466 /// A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
467 struct BatchToSpaceNdDescriptor
469 BatchToSpaceNdDescriptor()
470 : m_BlockShape({1, 1})
471 , m_Crops({{0, 0}, {0, 0}})
472 , m_DataLayout(DataLayout::NCHW)
475 BatchToSpaceNdDescriptor(std::vector<unsigned int> blockShape,
476 std::vector<std::pair<unsigned int, unsigned int>> crops)
477 : m_BlockShape(blockShape)
479 , m_DataLayout(DataLayout::NCHW)
482 /// Block shape values.
483 std::vector<unsigned int> m_BlockShape;
484 /// The values to crop from the input dimension.
485 std::vector<std::pair<unsigned int, unsigned int>> m_Crops;
486 /// The data layout to be used (NCHW, NHWC).
487 DataLayout m_DataLayout;
490 /// A FakeQuantizationDescriptor for the FakeQuantizationLayer.
491 struct FakeQuantizationDescriptor
493 FakeQuantizationDescriptor()
504 /// A ResizeBilinearDescriptor for the ResizeBilinearLayer.
505 struct ResizeBilinearDescriptor
507 ResizeBilinearDescriptor()
510 , m_DataLayout(DataLayout::NCHW)
513 /// Target width value.
514 uint32_t m_TargetWidth;
515 /// Target height value.
516 uint32_t m_TargetHeight;
517 /// The data layout to be used (NCHW, NHWC).
518 DataLayout m_DataLayout;
521 /// A ResizeDescriptor for the ResizeLayer.
522 struct ResizeDescriptor
527 , m_Method(ResizeMethod::NearestNeighbor)
528 , m_DataLayout(DataLayout::NCHW)
531 /// Target width value.
532 uint32_t m_TargetWidth;
533 /// Target height value.
534 uint32_t m_TargetHeight;
535 /// The Interpolation method to use
536 /// (Bilinear, NearestNeighbor).
537 ResizeMethod m_Method;
538 /// The data layout to be used (NCHW, NHWC).
539 DataLayout m_DataLayout;
543 /// A ReshapeDescriptor for the ReshapeLayer.
544 struct ReshapeDescriptor
550 ReshapeDescriptor(const TensorShape& shape)
551 : m_TargetShape(shape)
554 /// Target shape value.
555 TensorShape m_TargetShape;
558 /// A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
559 struct SpaceToBatchNdDescriptor
561 SpaceToBatchNdDescriptor()
562 : m_BlockShape({1, 1})
563 , m_PadList({{0, 0}, {0, 0}})
564 , m_DataLayout(DataLayout::NCHW)
567 SpaceToBatchNdDescriptor(const std::vector<unsigned int>& blockShape,
568 const std::vector<std::pair<unsigned int, unsigned int>>& padList)
569 : m_BlockShape(blockShape)
571 , m_DataLayout(DataLayout::NCHW)
574 /// Block shape value.
575 std::vector<unsigned int> m_BlockShape;
576 /// @brief Specifies the padding values for the input dimension:
577 /// heightPad{top, bottom} widthPad{left, right}.
578 std::vector<std::pair<unsigned int, unsigned int>> m_PadList;
579 /// The data layout to be used (NCHW, NHWC).
580 DataLayout m_DataLayout;
583 /// A SpaceToDepthDescriptor for the SpaceToDepthLayer
584 struct SpaceToDepthDescriptor
586 SpaceToDepthDescriptor()
588 , m_DataLayout(DataLayout::NHWC)
591 /// Scalar specifying the input block size. It must be >= 1
592 unsigned int m_BlockSize;
593 /// The data layout to be used (NCHW, NHWC).
594 DataLayout m_DataLayout;
597 /// An LstmDescriptor for the LstmLayer.
598 struct LstmDescriptor
601 : m_ActivationFunc(1) // 0: None, 1: Relu, 3: Relu6, 4: Tanh, 6: Sigmoid
602 , m_ClippingThresCell(0.0)
603 , m_ClippingThresProj(0.0)
604 , m_CifgEnabled(true)
605 , m_PeepholeEnabled(false)
606 , m_ProjectionEnabled(false)
607 , m_LayerNormEnabled(false)
610 /// @brief The activation function to use.
611 /// 0: None, 1: Relu, 3: Relu6, 4: Tanh, 6: Sigmoid.
612 uint32_t m_ActivationFunc;
613 /// Clipping threshold value for the cell state.
614 float m_ClippingThresCell;
615 /// Clipping threshold value for the projection.
616 float m_ClippingThresProj;
617 /// Enable/disable cifg (coupled input & forget gate).
619 /// Enable/disable peephole.
620 bool m_PeepholeEnabled;
621 /// Enable/disable the projection layer.
622 bool m_ProjectionEnabled;
623 /// Enable/disable layer normalization
624 bool m_LayerNormEnabled;
627 /// A MeanDescriptor for the MeanLayer.
628 struct MeanDescriptor
635 MeanDescriptor(const std::vector<unsigned int>& axis, bool keepDims)
637 , m_KeepDims(keepDims)
640 /// Values for the dimensions to reduce.
641 std::vector<unsigned int> m_Axis;
642 /// Enable/disable keep dimensions. If true, then the reduced dimensions that are of length 1 are kept.
646 /// A PadDescriptor for the PadLayer.
649 PadDescriptor() : m_PadValue(0)
652 PadDescriptor(const std::vector<std::pair<unsigned int, unsigned int>>& padList, const float& padValue = 0)
653 : m_PadList(padList), m_PadValue(padValue)
656 /// @brief Specifies the padding for input dimension.
657 /// First is the number of values to add before the tensor in the dimension.
658 /// Second is the number of values to add after the tensor in the dimension.
659 /// The number of pairs should match the number of dimensions in the input tensor.
660 std::vector<std::pair<unsigned int, unsigned int>> m_PadList;
662 /// Optional value to use for padding, defaults to 0
666 /// A StackDescriptor for the StackLayer.
667 struct StackDescriptor
675 StackDescriptor(uint32_t axis, uint32_t numInputs, const TensorShape& inputShape)
677 , m_NumInputs(numInputs)
678 , m_InputShape(inputShape)
681 /// 0-based axis along which to stack the input tensors.
683 /// Number of input tensors.
684 uint32_t m_NumInputs;
685 /// Required shape of all input tensors.
686 TensorShape m_InputShape;
689 /// A StridedSliceDescriptor for the StridedSliceLayer.
690 struct StridedSliceDescriptor
692 StridedSliceDescriptor(const std::vector<int>& begin,
693 const std::vector<int>& end,
694 const std::vector<int>& stride)
700 , m_ShrinkAxisMask(0)
703 , m_DataLayout(DataLayout::NCHW)
706 StridedSliceDescriptor()
707 : StridedSliceDescriptor({}, {}, {})
710 int GetStartForAxis(const TensorShape& inputShape, unsigned int axis) const;
711 int GetStopForAxis(const TensorShape& inputShape,
713 int startForAxis) const;
715 /// Begin values for the input that will be sliced.
716 std::vector<int> m_Begin;
717 /// End values for the input that will be sliced.
718 std::vector<int> m_End;
719 /// Stride values for the input that will be sliced.
720 std::vector<int> m_Stride;
722 /// @brief Begin mask value. If set, then the begin is disregarded and the fullest
723 /// range is used for the dimension.
725 /// @brief End mask value. If set, then the end is disregarded and the fullest range
726 /// is used for the dimension.
728 /// Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1.
729 int32_t m_ShrinkAxisMask;
730 /// Ellipsis mask value.
731 int32_t m_EllipsisMask;
732 /// @brief New axis mask value. If set, the begin, end and stride is disregarded and
733 /// a new 1 dimension is inserted to this location of the output tensor.
734 int32_t m_NewAxisMask;
736 /// The data layout to be used (NCHW, NHWC).
737 DataLayout m_DataLayout;
740 /// A PreCompiledDescriptor for the PreCompiledLayer.
741 struct PreCompiledDescriptor
743 PreCompiledDescriptor(unsigned int numInputSlots = 1u, unsigned int numOutputSlots = 1u)
744 : m_NumInputSlots(numInputSlots), m_NumOutputSlots(numOutputSlots)
747 ~PreCompiledDescriptor() = default;
749 unsigned int m_NumInputSlots;
750 unsigned int m_NumOutputSlots;
753 /// A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
754 struct TransposeConvolution2dDescriptor
756 TransposeConvolution2dDescriptor() :
763 m_BiasEnabled(false),
764 m_DataLayout(DataLayout::NCHW)
767 /// Padding left value in the width dimension.
769 /// Padding right value in the width dimension.
771 /// Padding top value in the height dimension.
773 /// Padding bottom value in the height dimension.
774 uint32_t m_PadBottom;
775 /// Stride value when proceeding through input for the width dimension.
777 /// Stride value when proceeding through input for the height dimension.
779 /// Enable/disable bias.
781 /// The data layout to be used (NCHW, NHWC).
782 DataLayout m_DataLayout;