2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
5 #include "Convolution2dLayer.hpp"
7 #include "LayerCloneBase.hpp"
9 #include <armnn/TypesUtils.hpp>
10 #include <backends/CpuTensorHandle.hpp>
11 #include <backends/WorkloadFactory.hpp>
16 Convolution2dLayer::Convolution2dLayer(const Convolution2dDescriptor& param, const char* name)
17 : LayerWithParameters(1, 1, LayerType::Convolution2d, param, name)
21 std::unique_ptr<IWorkload> Convolution2dLayer::CreateWorkload(const Graph& graph, const IWorkloadFactory& factory) const
23 // on this level constant data should not be released..
24 BOOST_ASSERT_MSG(m_Weight != nullptr, "Convolution2dLayer: Weights data should not be null.");
26 Convolution2dQueueDescriptor descriptor;
28 descriptor.m_Weight = m_Weight.get();
29 if (m_Param.m_BiasEnabled)
31 BOOST_ASSERT_MSG(m_Bias != nullptr, "Convolution2dLayer: Bias data should not be null.");
32 descriptor.m_Bias = m_Bias.get();
34 return factory.CreateConvolution2d(descriptor, PrepInfoAndDesc(descriptor, graph));
37 Convolution2dLayer* Convolution2dLayer::Clone(Graph& graph) const
39 auto layer = CloneBase<Convolution2dLayer>(graph, m_Param, GetName());
41 layer->m_Weight = m_Weight ? std::make_unique<ScopedCpuTensorHandle>(*m_Weight) : nullptr;
43 if (layer->m_Param.m_BiasEnabled)
45 layer->m_Bias = m_Bias ? std::make_unique<ScopedCpuTensorHandle>(*m_Bias) : nullptr;
48 return std::move(layer);
51 std::vector<TensorShape> Convolution2dLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
53 BOOST_ASSERT(inputShapes.size() == 2);
54 const TensorShape& inputShape = inputShapes[0];
55 const TensorShape filterShape = inputShapes[1];
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.");
60 unsigned int inWidth = inputShape[3];
61 unsigned int inHeight = inputShape[2];
62 unsigned int inBatchSize = inputShape[0];
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);
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);
72 unsigned int outChannels = filterShape[0];
73 unsigned int outBatchSize = inBatchSize;
75 return std::vector<TensorShape>({ TensorShape({outBatchSize, outChannels, outHeight, outWidth})});
78 void Convolution2dLayer::ValidateTensorShapesFromInputs()
80 VerifyLayerConnections(1, CHECK_LOCATION());
82 // check if we m_Weight data is not nullptr
83 BOOST_ASSERT_MSG(m_Weight != nullptr, "Convolution2dLayer: Weights data should not be null.");
85 auto inferredShapes = InferOutputShapes({
86 GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
87 m_Weight->GetTensorInfo().GetShape() });
89 BOOST_ASSERT(inferredShapes.size() == 1);
91 ConditionalThrowIfNotEqual<LayerValidationException>(
92 "Convolution2dLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
93 GetOutputSlot(0).GetTensorInfo().GetShape(),
97 Layer::ConstantTensors Convolution2dLayer::GetConstantTensorsByRef()
99 return {m_Weight, m_Bias};