Release 18.08
[platform/upstream/armnn.git] / src / armnn / layers / Convolution2dLayer.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 "Convolution2dLayer.hpp"
6
7 #include "LayerCloneBase.hpp"
8
9 #include <armnn/TypesUtils.hpp>
10 #include <backends/CpuTensorHandle.hpp>
11 #include <backends/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     if (m_Param.m_BiasEnabled)
30     {
31         BOOST_ASSERT_MSG(m_Bias != nullptr, "Convolution2dLayer: Bias data should not be null.");
32         descriptor.m_Bias = m_Bias.get();
33     }
34     return factory.CreateConvolution2d(descriptor, PrepInfoAndDesc(descriptor, graph));
35 }
36
37 Convolution2dLayer* Convolution2dLayer::Clone(Graph& graph) const
38 {
39     auto layer = CloneBase<Convolution2dLayer>(graph, m_Param, GetName());
40
41     layer->m_Weight = m_Weight ? std::make_unique<ScopedCpuTensorHandle>(*m_Weight) : nullptr;
42
43     if (layer->m_Param.m_BiasEnabled)
44     {
45         layer->m_Bias = m_Bias ? std::make_unique<ScopedCpuTensorHandle>(*m_Bias) : nullptr;
46     }
47
48     return std::move(layer);
49 }
50
51 std::vector<TensorShape> Convolution2dLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
52 {
53     BOOST_ASSERT(inputShapes.size() == 2);
54     const TensorShape& inputShape = inputShapes[0];
55     const TensorShape filterShape = inputShapes[1];
56
57     // If we support multiple batch dimensions in the future, then this assert will need to change.
58     BOOST_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Convolutions will always have 4D input.");
59
60     unsigned int inWidth = inputShape[3];
61     unsigned int inHeight = inputShape[2];
62     unsigned int inBatchSize = inputShape[0];
63
64     unsigned int filterWidth = filterShape[3];
65     unsigned int readWidth = (inWidth + m_Param.m_PadLeft + m_Param.m_PadRight) - (filterWidth);
66     unsigned int outWidth =  1+(readWidth / m_Param.m_StrideX);
67
68     unsigned int filterHeight = filterShape[2];
69     unsigned int readHeight = (inHeight + m_Param.m_PadTop + m_Param.m_PadBottom) - (filterHeight);
70     unsigned int outHeight = 1+(readHeight / m_Param.m_StrideY);
71
72     unsigned int outChannels = filterShape[0];
73     unsigned int outBatchSize = inBatchSize;
74
75     return std::vector<TensorShape>({ TensorShape({outBatchSize, outChannels, outHeight, outWidth})});
76 }
77
78 void Convolution2dLayer::ValidateTensorShapesFromInputs()
79 {
80     VerifyLayerConnections(1, CHECK_LOCATION());
81
82     // check if we m_Weight data is not nullptr
83     BOOST_ASSERT_MSG(m_Weight != nullptr, "Convolution2dLayer: Weights data should not be null.");
84
85     auto inferredShapes = InferOutputShapes({
86         GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
87         m_Weight->GetTensorInfo().GetShape() });
88
89     BOOST_ASSERT(inferredShapes.size() == 1);
90
91     ConditionalThrowIfNotEqual<LayerValidationException>(
92         "Convolution2dLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
93         GetOutputSlot(0).GetTensorInfo().GetShape(),
94         inferredShapes[0]);
95 }
96
97 Layer::ConstantTensors Convolution2dLayer::GetConstantTensorsByRef()
98 {
99     return {m_Weight, m_Bias};
100 }
101
102 } // namespace armnn