xref: /aosp_15_r20/external/armnn/src/armnn/layers/ConstantLayer.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "ConstantLayer.hpp"
6 #include "LayerCloneBase.hpp"
7 
8 #include <armnn/TypesUtils.hpp>
9 #include <armnn/backends/TensorHandle.hpp>
10 #include <armnn/backends/WorkloadData.hpp>
11 #include <armnn/backends/WorkloadFactory.hpp>
12 
13 namespace armnn
14 {
15 
ConstantLayer(const char * name)16 ConstantLayer::ConstantLayer(const char* name)
17     : Layer(0, 1, LayerType::Constant, name)
18 {
19 }
20 
CreateWorkload(const IWorkloadFactory & factory) const21 std::unique_ptr<IWorkload> ConstantLayer::CreateWorkload(const IWorkloadFactory& factory) const
22 {
23     ConstantQueueDescriptor descriptor;
24     descriptor.m_LayerOutput = m_LayerOutput.get();
25     SetAdditionalInfo(descriptor);
26 
27     return factory.CreateWorkload(LayerType::Constant, descriptor, PrepInfoAndDesc(descriptor));
28 }
29 
Clone(Graph & graph) const30 ConstantLayer* ConstantLayer::Clone(Graph& graph) const
31 {
32     // Cloned layers share the same layer output object.
33     auto layer = CloneBase<ConstantLayer>(graph, GetName());
34 
35     layer->m_LayerOutput = m_LayerOutput ? m_LayerOutput : nullptr;
36 
37     return std::move(layer);
38 }
39 
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const40 std::vector<TensorShape> ConstantLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
41 {
42     return std::vector<TensorShape>({  inputShapes[0] });
43 }
44 
ValidateTensorShapesFromInputs()45 void ConstantLayer::ValidateTensorShapesFromInputs()
46 {
47 
48     // Get the output shape from the value of the constant layer.
49     TensorShape const& outShape = m_LayerOutput->GetTensorInfo().GetShape();
50 
51     ConditionalThrow<LayerValidationException>(
52             outShape.GetDimensionality() != Dimensionality::NotSpecified,
53             "Constant layer m_LayerOutput output shape can not be Dimensionality::NotSpecified");
54 
55     ConditionalThrow<LayerValidationException>(
56             outShape.AreAllDimensionsSpecified(),
57             "Constant layer m_LayerOutput output shape can not have an unspecified dimension");
58 
59     ConditionalThrowIfNotEqual<LayerValidationException>(
60                "ConstantLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
61                GetOutputSlot(0).GetTensorInfo().GetShape(),
62                outShape);
63 }
64 
ExecuteStrategy(IStrategy & strategy) const65 void ConstantLayer::ExecuteStrategy(IStrategy& strategy) const
66 {
67     ManagedConstTensorHandle managedLayerOutput(m_LayerOutput);
68     ConstTensor layerOutputTensor(managedLayerOutput.GetTensorInfo(), managedLayerOutput.Map());
69     strategy.ExecuteStrategy(this, BaseDescriptor(), { layerOutputTensor }, GetName());
70 }
71 
72 } // namespace armnn
73