1 //
2 // Copyright © 2017,2022 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 <armnn/backends/TensorHandle.hpp>
14 #include <armnn/backends/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(param.GetNumInputs(), 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 GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape()
35 };
36 const TensorShape filterShape = inputShapes[1];
37 unsigned int inputChannels = filterShape[1];
38 unsigned int filterWidth = filterShape[3];
39 unsigned int filterHeight = filterShape[2];
40 unsigned int depthMultiplier = filterShape[0];
41
42 fn("FilterWidth",std::to_string(filterWidth));
43 fn("FilterHeight",std::to_string(filterHeight));
44 fn("DepthMultiplier",std::to_string(depthMultiplier));
45 fn("InputChannels",std::to_string(inputChannels));
46
47 LayerWithParameters<DepthwiseConvolution2dDescriptor>::SerializeLayerParameters(fn);
48 }
49
CreateWorkload(const IWorkloadFactory & factory) const50 std::unique_ptr<IWorkload> DepthwiseConvolution2dLayer::CreateWorkload(const IWorkloadFactory& factory) const
51 {
52 DepthwiseConvolution2dQueueDescriptor descriptor;
53 SetAdditionalInfo(descriptor);
54
55 return factory.CreateWorkload(LayerType::DepthwiseConvolution2d, descriptor, PrepInfoAndDesc(descriptor));
56 }
57
Clone(Graph & graph) const58 DepthwiseConvolution2dLayer* DepthwiseConvolution2dLayer::Clone(Graph& graph) const
59 {
60 auto layer = CloneBase<DepthwiseConvolution2dLayer>(graph, m_Param, GetName());
61 return std::move(layer);
62 }
63
64 std::vector<TensorShape>
InferOutputShapes(const std::vector<TensorShape> & inputShapes) const65 DepthwiseConvolution2dLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
66 {
67 ARMNN_ASSERT(inputShapes.size() == 2);
68 const TensorShape& inputShape = inputShapes[0];
69 const TensorShape& filterShape = inputShapes[1];
70
71 ARMNN_ASSERT_MSG(inputShape.GetNumDimensions() == 4, "Convolutions will always have 4D input.");
72
73 ARMNN_ASSERT( m_Param.m_StrideX > 0);
74 ARMNN_ASSERT( m_Param.m_StrideY > 0);
75
76 DataLayoutIndexed dataLayoutIndex(m_Param.m_DataLayout);
77
78 unsigned int inputBatchSize = inputShape[0];
79 unsigned int inputHeight = inputShape[dataLayoutIndex.GetHeightIndex()];
80 unsigned int inputWidth = inputShape[dataLayoutIndex.GetWidthIndex()];
81
82 // Expected filter shape: [ 1, H, W, O ] - This shape does NOT depend on the data layout
83 // Namely: [ 1, filter height, filter width, output channels ]
84
85 unsigned int filterHeight = filterShape[1];
86 unsigned int dilatedFilterHeight = filterHeight + (m_Param.m_DilationY - 1) * (filterHeight - 1);
87 unsigned int readHeight = (inputHeight + m_Param.m_PadTop + m_Param.m_PadBottom) - dilatedFilterHeight;
88 unsigned int outputHeight = 1 + (readHeight / m_Param.m_StrideY);
89
90 unsigned int filterWidth = filterShape[2];
91 unsigned int dilatedFilterWidth = filterWidth + (m_Param.m_DilationX - 1) * (filterWidth - 1);
92 unsigned int readWidth = (inputWidth + m_Param.m_PadLeft + m_Param.m_PadRight) - dilatedFilterWidth;
93 unsigned int outputWidth = 1 + (readWidth / m_Param.m_StrideX);
94
95 unsigned int outputChannels = filterShape[3];
96 unsigned int outputBatchSize = inputBatchSize;
97
98 TensorShape tensorShape = m_Param.m_DataLayout == armnn::DataLayout::NHWC ?
99 TensorShape{ outputBatchSize, outputHeight, outputWidth, outputChannels } :
100 TensorShape{ outputBatchSize, outputChannels, outputHeight, outputWidth };
101
102 return std::vector<TensorShape>{ tensorShape };
103 }
104
ValidateTensorShapesFromInputs()105 void DepthwiseConvolution2dLayer::ValidateTensorShapesFromInputs()
106 {
107 VerifyLayerConnections(m_Param.GetNumInputs(), CHECK_LOCATION());
108
109 const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
110
111 VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod);
112
113 ARMNN_ASSERT_MSG(GetInputSlot(1).GetConnection(),
114 "DepthwiseConvolution2dLayer: Weights data should not be null.");
115
116 auto inferredShapes = InferOutputShapes({
117 GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
118 GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape()
119 });
120
121 ARMNN_ASSERT(inferredShapes.size() == 1);
122
123 ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "DepthwiseConvolution2dLayer");
124 }
125
GetConstantTensorsByRef() const126 Layer::ImmutableConstantTensors DepthwiseConvolution2dLayer::GetConstantTensorsByRef() const
127 {
128 Layer::ImmutableConstantTensors tensors = GetConnectedConstantAsInputTensors();
129 return tensors;
130 }
131
ExecuteStrategy(IStrategy & strategy) const132 void DepthwiseConvolution2dLayer::ExecuteStrategy(IStrategy& strategy) const
133 {
134 strategy.ExecuteStrategy(this, GetParameters(), {}, GetName());
135 }
136
137 } // namespace armnn
138