IVGCVSW-1946: Remove armnn/src from the include paths
[platform/upstream/armnn.git] / src / armnn / layers / Convolution2dLayer.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "Convolution2dLayer.hpp"
6
7 #include "LayerCloneBase.hpp"
8
9 #include <armnn/TypesUtils.hpp>
10 #include <backendsCommon/CpuTensorHandle.hpp>
11 #include <backendsCommon/WorkloadFactory.hpp>
12
13 namespace armnn
14 {
15
16 Convolution2dLayer::Convolution2dLayer(const Convolution2dDescriptor& param, const char* name)
17     : LayerWithParameters(1, 1, LayerType::Convolution2d, param, name)
18 {
19 }
20
21 std::unique_ptr<IWorkload> Convolution2dLayer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const
22 {
23     // on this level constant data should not be released..
24     BOOST_ASSERT_MSG(m_Weight != nullptr, "Convolution2dLayer: Weights data should not be null.");
25
26     Convolution2dQueueDescriptor descriptor;
27
28     descriptor.m_Weight = m_Weight.get();
29
30     if (m_Param.m_BiasEnabled)
31     {
32         BOOST_ASSERT_MSG(m_Bias != nullptr, "Convolution2dLayer: Bias data should not be null.");
33         descriptor.m_Bias = m_Bias.get();
34     }
35     return factory.CreateConvolution2d(descriptor, PrepInfoAndDesc(descriptor, graph));
36 }
37
38 Convolution2dLayer* Convolution2dLayer::Clone(Graph& graph) const
39 {
40     auto layer = CloneBase<Convolution2dLayer>(graph, m_Param, GetName());
41
42     layer->m_Weight = m_Weight ? std::make_unique<ScopedCpuTensorHandle>(*m_Weight) : nullptr;
43
44     if (layer->m_Param.m_BiasEnabled)
45     {
46         layer->m_Bias = m_Bias ? std::make_unique<ScopedCpuTensorHandle>(*m_Bias) : nullptr;
47     }
48
49     return std::move(layer);
50 }
51
52 std::vector<TensorShape> Convolution2dLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
53 {
54     BOOST_ASSERT(inputShapes.size() == 2);
55     const TensorShape& inputShape = inputShapes[0];
56     const TensorShape filterShape = inputShapes[1];
57
58     // If we support multiple batch dimensions in the future, then this assert will need to change.
59     BOOST_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Convolutions will always have 4D input.");
60
61     DataLayoutIndexed dataLayoutIndex(m_Param.m_DataLayout);
62
63     unsigned int inWidth = inputShape[dataLayoutIndex.GetWidthIndex()];
64     unsigned int inHeight = inputShape[dataLayoutIndex.GetHeightIndex()];
65     unsigned int inBatchSize = inputShape[0];
66
67     unsigned int filterWidth = filterShape[dataLayoutIndex.GetWidthIndex()];
68     unsigned int readWidth = (inWidth + m_Param.m_PadLeft + m_Param.m_PadRight) - (filterWidth);
69     unsigned int outWidth =  1 + (readWidth / m_Param.m_StrideX);
70
71     unsigned int filterHeight = filterShape[dataLayoutIndex.GetHeightIndex()];
72     unsigned int readHeight = (inHeight + m_Param.m_PadTop + m_Param.m_PadBottom) - (filterHeight);
73     unsigned int outHeight = 1 + (readHeight / m_Param.m_StrideY);
74
75     unsigned int outChannels = filterShape[0];
76     unsigned int outBatchSize = inBatchSize;
77
78     TensorShape tensorShape = m_Param.m_DataLayout == armnn::DataLayout::NHWC ?
79         TensorShape( { outBatchSize, outHeight, outWidth, outChannels } ) :
80         TensorShape( { outBatchSize, outChannels, outHeight, outWidth });
81
82     return std::vector<TensorShape>({ tensorShape });
83 }
84
85 void Convolution2dLayer::ValidateTensorShapesFromInputs()
86 {
87     VerifyLayerConnections(1, CHECK_LOCATION());
88
89     // check if we m_Weight data is not nullptr
90     BOOST_ASSERT_MSG(m_Weight != nullptr, "Convolution2dLayer: Weights data should not be null.");
91
92     auto inferredShapes = InferOutputShapes({
93         GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
94         m_Weight->GetTensorInfo().GetShape() });
95
96     BOOST_ASSERT(inferredShapes.size() == 1);
97
98     ConditionalThrowIfNotEqual<LayerValidationException>(
99         "Convolution2dLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
100         GetOutputSlot(0).GetTensorInfo().GetShape(),
101         inferredShapes[0]);
102 }
103
104 Layer::ConstantTensors Convolution2dLayer::GetConstantTensorsByRef()
105 {
106     return {m_Weight, m_Bias};
107 }
108
109 } // namespace armnn