ArmNN
 24.08
ConstantLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 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>
12 
13 namespace armnn
14 {
15 
17  : Layer(0, 1, LayerType::Constant, name)
18 {
19 }
20 
21 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 
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 
40 std::vector<TensorShape> ConstantLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
41 {
42  return std::vector<TensorShape>({ inputShapes[0] });
43 }
44 
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>(
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 
66 {
67  ManagedConstTensorHandle managedLayerOutput(m_LayerOutput);
68  ConstTensor layerOutputTensor(managedLayerOutput.GetTensorInfo(), managedLayerOutput.Map());
69  strategy.ExecuteStrategy(this, BaseDescriptor(), { layerOutputTensor }, GetName());
70 }
71 
73 {
74  fn("DataType",GetDataTypeName(m_LayerOutput->GetTensorInfo().GetDataType()));
76 }
77 
78 } // namespace armnn
armnn::ConstantLayer::InferOutputShapes
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
By default returns inputShapes if the number of inputs are equal to number of outputs,...
Definition: ConstantLayer.cpp:40
WorkloadData.hpp
TypesUtils.hpp
armnn::ConstantLayer::ValidateTensorShapesFromInputs
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of ConstantLayer.
Definition: ConstantLayer.cpp:45
armnn::GetDataTypeName
constexpr const char * GetDataTypeName(DataType dataType)
Definition: TypesUtils.hpp:233
armnn::TensorShape::AreAllDimensionsSpecified
bool AreAllDimensionsSpecified() const
Checks if there is at least one dimension not specified.
Definition: Tensor.cpp:241
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
armnn::ConstantLayer::ExecuteStrategy
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
Definition: ConstantLayer.cpp:65
armnn::ConstantLayer::ConstantLayer
ConstantLayer(const char *name)
Constructor to create a ConstantLayer.
Definition: ConstantLayer.cpp:16
armnn::IStrategy
Definition: IStrategy.hpp:16
armnn::ManagedConstTensorHandle
Definition: TensorHandle.hpp:187
WorkloadFactory.hpp
armnn::Layer::GetName
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:332
armnn::Layer
Definition: Layer.hpp:230
armnn::ConstantQueueDescriptor
Definition: WorkloadData.hpp:368
armnn::Layer::SerializeLayerParameters
virtual void SerializeLayerParameters(ParameterStringifyFunction &fn) const
Helper to serialize the layer parameters to string.
Definition: Layer.cpp:540
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::Layer::PrepInfoAndDesc
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *Layer::CreateWorkload.
Definition: Layer.hpp:409
armnn::IWorkloadFactory
Definition: WorkloadFactory.hpp:22
armnn::ParameterStringifyFunction
std::function< void(const std::string &name, const std::string &value)> ParameterStringifyFunction
Definition: SerializeLayerParameters.hpp:14
armnn::GetTensorInfo
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
Definition: RefWorkloadUtils.hpp:33
armnn::ManagedConstTensorHandle::Map
const void * Map(bool blocking=true)
RAII Managed resource Unmaps MemoryArea once out of scope.
Definition: TensorHandle.hpp:196
armnn::Layer::SetAdditionalInfo
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:303
armnn::Dimensionality::NotSpecified
@ NotSpecified
armnn::BaseDescriptor
Base class for all descriptors.
Definition: Descriptors.hpp:22
armnn::ConstantQueueDescriptor::m_LayerOutput
const ConstTensorHandle * m_LayerOutput
Definition: WorkloadData.hpp:375
TensorHandle.hpp
armnn::ConstantLayer::Clone
ConstantLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: ConstantLayer.cpp:30
armnn::ConstantLayer::m_LayerOutput
std::shared_ptr< ConstTensorHandle > m_LayerOutput
Definition: ConstantLayer.hpp:46
armnn::ConstantLayer::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the Constant type.
Definition: ConstantLayer.cpp:21
ConstantLayer.hpp
armnn::ConstantLayer
A layer that the constant data can be bound to.
Definition: ConstantLayer.hpp:15
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::ConstTensor
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:329
armnn::ConstantLayer::SerializeLayerParameters
void SerializeLayerParameters(ParameterStringifyFunction &fn) const override
Helper to serialize the layer parameters to string.
Definition: ConstantLayer.cpp:72
armnn::TensorShape::GetDimensionality
Dimensionality GetDimensionality() const
Function that returns the tensor type.
Definition: Tensor.hpp:92
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:491
armnn::Graph
Definition: Graph.hpp:30
armnn::IWorkloadFactory::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const =0
Backends should implement their own CreateWorkload function with a switch statement.
armnn::IStrategy::ExecuteStrategy
virtual void ExecuteStrategy(const IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
armnn::PaddingMode::Constant
@ Constant
LayerCloneBase.hpp
armnn::LayerType::Constant
@ Constant
armnn::ManagedConstTensorHandle::GetTensorInfo
const TensorInfo & GetTensorInfo() const
Definition: TensorHandle.hpp:239