Release 18.08
[platform/upstream/armnn.git] / src / armnn / layers / ConstantLayer.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5 #include "ConstantLayer.hpp"
6 #include "LayerCloneBase.hpp"
7
8 #include <armnn/TypesUtils.hpp>
9 #include <backends/CpuTensorHandle.hpp>
10 #include <backends/WorkloadData.hpp>
11 #include <backends/WorkloadFactory.hpp>
12
13 namespace armnn
14 {
15
16 ConstantLayer::ConstantLayer(const char* name)
17     : Layer(0, 1, LayerType::Constant, name)
18 {
19 }
20
21 std::unique_ptr<IWorkload> ConstantLayer::CreateWorkload(const Graph& graph,
22     const IWorkloadFactory& factory) const
23 {
24     ConstantQueueDescriptor descriptor;
25     descriptor.m_LayerOutput = m_LayerOutput.get();
26     return factory.CreateConstant(descriptor, PrepInfoAndDesc(descriptor, graph));
27 }
28
29 ConstantLayer* ConstantLayer::Clone(Graph& graph) const
30 {
31     // Cloned layers share the same layer output object.
32     auto layer = CloneBase<ConstantLayer>(graph, GetName());
33
34     layer->m_LayerOutput = m_LayerOutput ? std::make_unique<ScopedCpuTensorHandle>(*m_LayerOutput) : nullptr;
35
36     return std::move(layer);
37 }
38
39 std::vector<TensorShape> ConstantLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
40 {
41     return std::vector<TensorShape>({  m_LayerOutput->GetTensorInfo().GetShape() });
42 }
43
44 void ConstantLayer::ValidateTensorShapesFromInputs()
45 {
46     // Get the output shape from the value of the constant layer.
47     TensorShape const& outShape = m_LayerOutput->GetTensorInfo().GetShape();
48     ConditionalThrowIfNotEqual<LayerValidationException>(
49         "ConstantLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
50         GetOutputSlot(0).GetTensorInfo().GetShape(),
51         outShape);
52 }
53
54 } // namespace armnn