xref: /aosp_15_r20/external/armnn/src/armnn/layers/LayerWithParameters.hpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "ConstantLayer.hpp"
8 #include <Layer.hpp>
9 
10 namespace armnn
11 {
12 
13 template <typename Parameters>
14 class LayerWithParameters : public Layer
15 {
16 public:
17     using DescriptorType = Parameters;
18 
GetParameters() const19     const Parameters& GetParameters() const override { return m_Param; }
20 
21     /// Helper to serialize the layer parameters to string
22     /// (currently used in DotSerializer and company).
SerializeLayerParameters(ParameterStringifyFunction & fn) const23     void SerializeLayerParameters(ParameterStringifyFunction& fn) const override
24     {
25         StringifyLayerParameters<Parameters>::Serialize(fn, m_Param);
26         Layer::SerializeLayerParameters(fn);
27     }
28 
29 protected:
LayerWithParameters(unsigned int numInputSlots,unsigned int numOutputSlots,LayerType type,const Parameters & param,const char * name)30     LayerWithParameters(unsigned int numInputSlots,
31                         unsigned int numOutputSlots,
32                         LayerType type,
33                         const Parameters& param,
34                         const char* name)
35         : Layer(numInputSlots, numOutputSlots, type, name)
36         , m_Param(param)
37     {
38     }
39 
40     ~LayerWithParameters() = default;
41 
42     /// Helper function to reduce duplication in *Layer::CreateWorkload.
43     template <typename QueueDescriptor>
PrepInfoAndDesc(QueueDescriptor & descriptor) const44     WorkloadInfo PrepInfoAndDesc(QueueDescriptor& descriptor) const
45     {
46         descriptor.m_Parameters = m_Param;
47         descriptor.m_AllowExpandedDims = GetAllowExpandedDims();
48         return Layer::PrepInfoAndDesc(descriptor);
49     }
50 
51     /// The parameters for the layer (not including tensor-valued weights etc.).
52     Parameters m_Param;
53 
ExecuteStrategy(IStrategy & strategy) const54     void ExecuteStrategy(IStrategy& strategy) const override
55     {
56         strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
57     }
58 
GetConnectedConstantAsInputTensors() const59     Layer::ImmutableConstantTensors GetConnectedConstantAsInputTensors() const
60     {
61         Layer::ImmutableConstantTensors tensors;
62         for (unsigned int i = 0; i < GetNumInputSlots(); ++i)
63         {
64             if (GetInputSlot(i).GetConnection() && GetInputSlot(i).GetConnection()->GetTensorInfo().IsConstant())
65             {
66                 auto &inputLayer = GetInputSlot(i).GetConnectedOutputSlot()->GetOwningLayer();
67                 if (inputLayer.GetType() == armnn::LayerType::Constant)
68                 {
69                     auto &constantLayer = static_cast<ConstantLayer&>(inputLayer);
70 
71                     tensors.push_back(constantLayer.m_LayerOutput);
72                 }
73             }
74         }
75         if (tensors.empty())
76         {
77             const std::string warningMessage{"GetConnectedConstantAsInputTensors() called on Layer with no "
78                                              "connected Constants as Input Tensors."};
79             ARMNN_LOG(warning) << warningMessage;
80         }
81         return tensors;
82     }
83 };
84 
85 
86 
87 
88 } // namespace
89