xref: /aosp_15_r20/external/armnn/src/armnn/layers/TransposeLayer.hpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2020,2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "LayerWithParameters.hpp"
8 
9 #include <armnn/utility/PolymorphicDowncast.hpp>
10 
11 namespace armnn
12 {
13 
14 /// This layer represents a transpose operation.
15 class TransposeLayer : public LayerWithParameters<TransposeDescriptor>
16 {
17 public:
18     /// Makes a workload for the Transpose type.
19     /// @param [in] factory The workload factory which will create the workload.
20     /// @return A pointer to the created workload, or nullptr if not created.
21     virtual std::unique_ptr<IWorkload> CreateWorkload(const IWorkloadFactory& factory) const override;
22 
23     /// Creates a dynamically-allocated copy of this layer.
24     /// @param [in] graph The graph into which this layer is being cloned.
25     TransposeLayer* Clone(Graph& graph) const override;
26 
27     /// Check if the input tensor shape(s)
28     /// will lead to a valid configuration of @ref TransposeLayer.
29     void ValidateTensorShapesFromInputs() override;
30 
31     /// Infers the output shapes from given input shapes and the permutation vector.
32     /// @param [in] inputShapes The input shapes layer has.
33     /// @return A vector to the inferred output shape.
34     std::vector<TensorShape> InferOutputShapes(const std::vector<TensorShape>& inputShapes) const override;
35 
36     /// @return a permutation vector describing the permutation for the dimensions of the input tensor.
GetPermutation() const37     const PermutationVector& GetPermutation() const
38     {
39         return m_Param.m_DimMappings;
40     }
41 
42     /// Indicates if the other layer received is inverse of this one.
43     /// @param [in] other The other layer to be compared with.
44     /// @return true if other layer is inverse of this false otherwise.
IsInverse(const Layer & other) const45     bool IsInverse(const Layer& other) const
46     {
47         return (other.GetType() == LayerType::Transpose) &&
48             GetPermutation().IsInverse(PolymorphicDowncast<const TransposeLayer*>(&other)->GetPermutation());
49     }
50 
51     /// Indicates if the other layer received is equal to this one.
52     /// @param [in] other The other layer to be compare with.
53     /// @return true if other layer is equal to this false otherwise.
IsEqual(const Layer & other) const54     bool IsEqual(const Layer& other) const
55     {
56         return (other.GetType() == LayerType::Transpose) &&
57                GetPermutation().IsEqual(PolymorphicDowncast<const TransposeLayer*>(&other)->GetPermutation());
58     }
59 
60     void ExecuteStrategy(IStrategy& strategy) const override;
61 
62 
63 protected:
64     /// Constructor to create a TransposeLayer.
65     /// @param [in] param TransposeDescriptor to configure the permute operation.
66     /// @param [in] name Optional name for the layer.
67     TransposeLayer(const TransposeDescriptor& param, const char* name);
68 
69     /// Default destructor
70     ~TransposeLayer() = default;
71 };
72 
73 } // namespace
74