2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include "Convolution2dLayer.hpp"
7 #include "LayerCloneBase.hpp"
9 #include <armnn/TypesUtils.hpp>
10 #include <backendsCommon/CpuTensorHandle.hpp>
11 #include <backendsCommon/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();
30 if (m_Param.m_BiasEnabled)
32 BOOST_ASSERT_MSG(m_Bias != nullptr, "Convolution2dLayer: Bias data should not be null.");
33 descriptor.m_Bias = m_Bias.get();
35 return factory.CreateConvolution2d(descriptor, PrepInfoAndDesc(descriptor, graph));
38 Convolution2dLayer* Convolution2dLayer::Clone(Graph& graph) const
40 auto layer = CloneBase<Convolution2dLayer>(graph, m_Param, GetName());
42 layer->m_Weight = m_Weight ? std::make_unique<ScopedCpuTensorHandle>(*m_Weight) : nullptr;
44 if (layer->m_Param.m_BiasEnabled)
46 layer->m_Bias = m_Bias ? std::make_unique<ScopedCpuTensorHandle>(*m_Bias) : nullptr;
49 return std::move(layer);
52 std::vector<TensorShape> Convolution2dLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
54 BOOST_ASSERT(inputShapes.size() == 2);
55 const TensorShape& inputShape = inputShapes[0];
56 const TensorShape filterShape = inputShapes[1];
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.");
61 DataLayoutIndexed dataLayoutIndex(m_Param.m_DataLayout);
63 unsigned int inWidth = inputShape[dataLayoutIndex.GetWidthIndex()];
64 unsigned int inHeight = inputShape[dataLayoutIndex.GetHeightIndex()];
65 unsigned int inBatchSize = inputShape[0];
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);
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);
75 unsigned int outChannels = filterShape[0];
76 unsigned int outBatchSize = inBatchSize;
78 TensorShape tensorShape = m_Param.m_DataLayout == armnn::DataLayout::NHWC ?
79 TensorShape( { outBatchSize, outHeight, outWidth, outChannels } ) :
80 TensorShape( { outBatchSize, outChannels, outHeight, outWidth });
82 return std::vector<TensorShape>({ tensorShape });
85 void Convolution2dLayer::ValidateTensorShapesFromInputs()
87 VerifyLayerConnections(1, CHECK_LOCATION());
89 // check if we m_Weight data is not nullptr
90 BOOST_ASSERT_MSG(m_Weight != nullptr, "Convolution2dLayer: Weights data should not be null.");
92 auto inferredShapes = InferOutputShapes({
93 GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
94 m_Weight->GetTensorInfo().GetShape() });
96 BOOST_ASSERT(inferredShapes.size() == 1);
98 ConditionalThrowIfNotEqual<LayerValidationException>(
99 "Convolution2dLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
100 GetOutputSlot(0).GetTensorInfo().GetShape(),
104 Layer::ConstantTensors Convolution2dLayer::GetConstantTensorsByRef()
106 return {m_Weight, m_Bias};