• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <backendsCommon/CpuTensorHandle.hpp>
10 #include <backendsCommon/WorkloadData.hpp>
11 #include <backendsCommon/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.CreateConstant(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 ? std::make_unique<ScopedCpuTensorHandle>(*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 
Accept(ILayerVisitor & visitor) const65 void ConstantLayer::Accept(ILayerVisitor& visitor) const
66 {
67     ConstTensor layerOutputTensor(m_LayerOutput->GetTensorInfo(), m_LayerOutput->Map(true)) ;
68     visitor.VisitConstantLayer(this, layerOutputTensor, GetName());
69 }
70 
71 } // namespace armnn
72