1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "DepthwiseConvolution2dLayer.hpp"
7 #include "LayerCloneBase.hpp"
8
9 #include <armnn/TypesUtils.hpp>
10
11 #include <armnnUtils/DataLayoutIndexed.hpp>
12
13 #include <backendsCommon/CpuTensorHandle.hpp>
14 #include <backendsCommon/WorkloadFactory.hpp>
15
16 #include <string>
17
18 using namespace armnnUtils;
19
20 namespace armnn
21 {
22
DepthwiseConvolution2dLayer(const DepthwiseConvolution2dDescriptor & param,const char * name)23 DepthwiseConvolution2dLayer::DepthwiseConvolution2dLayer(const DepthwiseConvolution2dDescriptor& param,
24 const char* name)
25 : LayerWithParameters(1, 1, LayerType::DepthwiseConvolution2d, param, name)
26 {
27 }
28
SerializeLayerParameters(ParameterStringifyFunction & fn) const29 void DepthwiseConvolution2dLayer::SerializeLayerParameters(ParameterStringifyFunction& fn) const
30 {
31 const std::vector<TensorShape>& inputShapes =
32 {
33 GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
34 m_Weight->GetTensorInfo().GetShape()
35 };
36 const TensorShape filterShape = inputShapes[1];
37 DataLayoutIndexed dataLayoutIndex(m_Param.m_DataLayout);
38 unsigned int inputChannels = filterShape[1];
39 unsigned int filterWidth = filterShape[3];
40 unsigned int filterHeight = filterShape[2];
41 unsigned int depthMultiplier = filterShape[0];
42
43 fn("FilterWidth",std::to_string(filterWidth));
44 fn("FilterHeight",std::to_string(filterHeight));
45 fn("DepthMultiplier",std::to_string(depthMultiplier));
46 fn("InputChannels",std::to_string(inputChannels));
47
48 LayerWithParameters<DepthwiseConvolution2dDescriptor>::SerializeLayerParameters(fn);
49 }
50
CreateWorkload(const IWorkloadFactory & factory) const51 std::unique_ptr<IWorkload> DepthwiseConvolution2dLayer::CreateWorkload(const IWorkloadFactory& factory) const
52 {
53 // on this level constant data should not be released..
54 ARMNN_ASSERT_MSG(m_Weight != nullptr, "DepthwiseConvolution2dLayer: Weights data should not be null.");
55
56 DepthwiseConvolution2dQueueDescriptor descriptor;
57
58 descriptor.m_Weight = m_Weight.get();
59
60 if (m_Param.m_BiasEnabled)
61 {
62 ARMNN_ASSERT_MSG(m_Bias != nullptr, "DepthwiseConvolution2dLayer: Bias data should not be null.");
63 descriptor.m_Bias = m_Bias.get();
64 }
65
66 SetAdditionalInfo(descriptor);
67
68 return factory.CreateDepthwiseConvolution2d(descriptor, PrepInfoAndDesc(descriptor));
69 }
70
Clone(Graph & graph) const71 DepthwiseConvolution2dLayer* DepthwiseConvolution2dLayer::Clone(Graph& graph) const
72 {
73 auto layer = CloneBase<DepthwiseConvolution2dLayer>(graph, m_Param, GetName());
74 layer->m_Weight = m_Weight ? std::make_unique<ScopedCpuTensorHandle>(*m_Weight) : nullptr;
75
76 if (layer->m_Param.m_BiasEnabled)
77 {
78 layer->m_Bias = m_Bias ? std::make_unique<ScopedCpuTensorHandle>(*m_Bias) : nullptr;
79 }
80
81 return std::move(layer);
82 }
83
84 std::vector<TensorShape>
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const85 DepthwiseConvolution2dLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
86 {
87 ARMNN_ASSERT(inputShapes.size() == 2);
88 const TensorShape& inputShape = inputShapes[0];
89 const TensorShape& filterShape = inputShapes[1];
90
91 ARMNN_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Convolutions will always have 4D input.");
92
93 DataLayoutIndexed dataLayoutIndex(m_Param.m_DataLayout);
94
95 unsigned int inputBatchSize = inputShape[0];
96 unsigned int inputHeight = inputShape[dataLayoutIndex.GetHeightIndex()];
97 unsigned int inputWidth = inputShape[dataLayoutIndex.GetWidthIndex()];
98 unsigned int inputChannels = inputShape[dataLayoutIndex.GetChannelsIndex()];
99
100 // Expected filter shape: [ M, I, H, W ] - This shape does NOT depend on the data layout
101 // Namely: [ depth multiplier, input channels, filter height, filter width ]
102 // Output channels = input channels * depthMultiplier
103 unsigned int depthMultiplier = filterShape[0];
104
105 unsigned int filterHeight = filterShape[2];
106 unsigned int dilatedFilterHeight = filterHeight + (m_Param.m_DilationY - 1) * (filterHeight - 1);
107 unsigned int readHeight = (inputHeight + m_Param.m_PadTop + m_Param.m_PadBottom) - dilatedFilterHeight;
108 unsigned int outputHeight = 1 + (readHeight / m_Param.m_StrideY);
109
110 unsigned int filterWidth = filterShape[3];
111 unsigned int dilatedFilterWidth = filterWidth + (m_Param.m_DilationX - 1) * (filterWidth - 1);
112 unsigned int readWidth = (inputWidth + m_Param.m_PadLeft + m_Param.m_PadRight) - dilatedFilterWidth;
113 unsigned int outputWidth = 1 + (readWidth / m_Param.m_StrideX);
114
115 unsigned int outputChannels = inputChannels * depthMultiplier;
116 unsigned int outputBatchSize = inputBatchSize;
117
118 TensorShape tensorShape = m_Param.m_DataLayout == armnn::DataLayout::NHWC ?
119 TensorShape{ outputBatchSize, outputHeight, outputWidth, outputChannels } :
120 TensorShape{ outputBatchSize, outputChannels, outputHeight, outputWidth };
121
122 return std::vector<TensorShape>{ tensorShape };
123 }
124
ValidateTensorShapesFromInputs()125 void DepthwiseConvolution2dLayer::ValidateTensorShapesFromInputs()
126 {
127 VerifyLayerConnections(1, CHECK_LOCATION());
128
129 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
130
131 VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
132
133 // on this level constant data should not be released..
134 ARMNN_ASSERT_MSG(m_Weight != nullptr, "DepthwiseConvolution2dLayer: Weights data should not be null.");
135
136 auto inferredShapes = InferOutputShapes({
137 GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
138 m_Weight->GetTensorInfo().GetShape()
139 });
140
141 ARMNN_ASSERT(inferredShapes.size() == 1);
142
143 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "DepthwiseConvolution2dLayer");
144 }
145
GetConstantTensorsByRef()146 Layer::ConstantTensors DepthwiseConvolution2dLayer::GetConstantTensorsByRef()
147 {
148 return {m_Weight, m_Bias};
149 }
150
Accept(ILayerVisitor & visitor) const151 void DepthwiseConvolution2dLayer::Accept(ILayerVisitor& visitor) const
152 {
153 ConstTensor weightsTensor(m_Weight->GetTensorInfo(), m_Weight->Map(true));
154 Optional<ConstTensor> optionalBiasTensor = EmptyOptional();
155
156 if (GetParameters().m_BiasEnabled)
157 {
158 ConstTensor biasTensor(m_Bias->GetTensorInfo(), m_Bias->Map(true));
159 optionalBiasTensor = Optional<ConstTensor>(biasTensor);
160 }
161
162 visitor.VisitDepthwiseConvolution2dLayer(this, GetParameters(), weightsTensor, optionalBiasTensor, GetName());
163 }
164
165 } // namespace armnn
166