Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 1 | // |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 2 | // Copyright © 2021-2024 Arm Ltd and Contributors. All rights reserved. |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "Convolution3dLayer.hpp" |
| 7 | #include "LayerCloneBase.hpp" |
| 8 | |
| 9 | #include <armnnUtils/DataLayoutIndexed.hpp> |
| 10 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 11 | #include <armnn/backends/TensorHandle.hpp> |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 12 | |
| 13 | using namespace armnnUtils; |
| 14 | |
| 15 | namespace armnn |
| 16 | { |
| 17 | |
| 18 | Convolution3dLayer::Convolution3dLayer(const Convolution3dDescriptor& param, const char* name) |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 19 | : LayerWithParameters(param.GetNumInputs(), 1, LayerType::Convolution3d, param, name) |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 20 | { |
| 21 | } |
| 22 | |
| 23 | void Convolution3dLayer::SerializeLayerParameters(ParameterStringifyFunction& fn) const |
| 24 | { |
| 25 | const std::vector<TensorShape>& inputShapes = |
| 26 | { |
Mike Kelly | a9ac6ba | 2023-06-30 15:18:26 +0100 | [diff] [blame] | 27 | GetInputSlot(0).GetTensorInfo().GetShape(), |
| 28 | GetInputSlot(1).GetTensorInfo().GetShape(), |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | // Conv3d Filter Layout: [D,H,W,I,O] |
| 32 | const TensorShape filterShape = inputShapes[1]; |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 33 | unsigned int filterDepth = filterShape[0]; |
| 34 | unsigned int filterHeight = filterShape[1]; |
| 35 | unsigned int filterWidth = filterShape[2]; |
| 36 | unsigned int inChannels = filterShape[3]; |
| 37 | unsigned int outChannels = filterShape[4]; |
| 38 | |
| 39 | fn("FilterDepth",std::to_string(filterDepth)); |
| 40 | fn("FilterHeight",std::to_string(filterHeight)); |
| 41 | fn("FilterWidth",std::to_string(filterWidth)); |
| 42 | fn("InputChannels",std::to_string(inChannels)); |
| 43 | fn("OutputChannels",std::to_string(outChannels)); |
| 44 | |
| 45 | LayerWithParameters<Convolution3dDescriptor>::SerializeLayerParameters(fn); |
| 46 | } |
| 47 | |
| 48 | std::unique_ptr<IWorkload> Convolution3dLayer::CreateWorkload(const IWorkloadFactory& factory) const |
| 49 | { |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 50 | Convolution3dQueueDescriptor descriptor; |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 51 | SetAdditionalInfo(descriptor); |
| 52 | |
Teresa Charlin | 611c7fb | 2022-01-07 09:47:29 +0000 | [diff] [blame] | 53 | return factory.CreateWorkload(LayerType::Convolution3d, descriptor, PrepInfoAndDesc(descriptor)); |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Convolution3dLayer* Convolution3dLayer::Clone(Graph& graph) const |
| 57 | { |
| 58 | auto layer = CloneBase<Convolution3dLayer>(graph, m_Param, GetName()); |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 59 | return std::move(layer); |
| 60 | } |
| 61 | |
| 62 | std::vector<TensorShape> Convolution3dLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const |
| 63 | { |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 64 | if (inputShapes.size() != 2) |
| 65 | { |
| 66 | throw armnn::Exception("inputShapes' size is \"" + std::to_string(inputShapes.size()) + |
| 67 | "\" - should be \"2\"."); |
| 68 | } |
| 69 | |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 70 | const TensorShape& inputShape = inputShapes[0]; |
| 71 | const TensorShape& filterShape = inputShapes[1]; |
| 72 | |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 73 | if (inputShape.GetNumDimensions() != 5) |
| 74 | { |
| 75 | throw armnn::Exception("Convolutions will always have 5D input."); |
| 76 | } |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 77 | |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 78 | if (m_Param.m_StrideX == 0) |
| 79 | { |
| 80 | throw armnn::Exception("m_StrideX cannot be 0."); |
| 81 | } |
| 82 | |
| 83 | if (m_Param.m_StrideY == 0) |
| 84 | { |
| 85 | throw armnn::Exception("m_StrideY cannot be 0."); |
| 86 | } |
| 87 | |
| 88 | if (m_Param.m_StrideZ == 0) |
| 89 | { |
| 90 | throw armnn::Exception("m_StrideZ cannot be 0."); |
| 91 | } |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 92 | |
| 93 | DataLayoutIndexed dataLayoutIndex(m_Param.m_DataLayout); |
| 94 | |
| 95 | unsigned int inWidth = inputShape[dataLayoutIndex.GetWidthIndex()]; |
| 96 | unsigned int inHeight = inputShape[dataLayoutIndex.GetHeightIndex()]; |
| 97 | unsigned int inDepth = inputShape[dataLayoutIndex.GetDepthIndex()]; |
| 98 | unsigned int inBatchSize = inputShape[0]; |
| 99 | |
| 100 | // Conv3d Filter Layout: [D,H,W,I,O] |
| 101 | unsigned int filterDepth = filterShape[0]; |
| 102 | unsigned int dilatedFilterDepth = filterDepth + (m_Param.m_DilationZ - 1) * (filterDepth - 1); |
| 103 | unsigned int readDepth = (inDepth + m_Param.m_PadFront + m_Param.m_PadBack) - dilatedFilterDepth; |
| 104 | unsigned int outDepth = 1 + (readDepth / m_Param.m_StrideZ); |
| 105 | |
| 106 | unsigned int filterHeight = filterShape[1]; |
| 107 | unsigned int dilatedFilterHeight = filterHeight + (m_Param.m_DilationY - 1) * (filterHeight - 1); |
| 108 | unsigned int readHeight = (inHeight + m_Param.m_PadTop + m_Param.m_PadBottom) - dilatedFilterHeight; |
| 109 | unsigned int outHeight = 1 + (readHeight / m_Param.m_StrideY); |
| 110 | |
| 111 | unsigned int filterWidth = filterShape[2]; |
| 112 | unsigned int dilatedFilterWidth = filterWidth + (m_Param.m_DilationX - 1) * (filterWidth - 1); |
| 113 | unsigned int readWidth = (inWidth + m_Param.m_PadLeft + m_Param.m_PadRight) - dilatedFilterWidth; |
| 114 | unsigned int outWidth = 1 + (readWidth / m_Param.m_StrideX); |
| 115 | |
| 116 | unsigned int outChannels = filterShape[4]; |
| 117 | unsigned int outBatchSize = inBatchSize; |
| 118 | |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 119 | TensorShape tensorShape = m_Param.m_DataLayout == armnn::DataLayout::NDHWC ? |
| 120 | TensorShape( { outBatchSize, outDepth, outHeight, outWidth, outChannels } ) : |
| 121 | TensorShape( { outBatchSize, outChannels, outDepth, outHeight, outWidth }); |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 122 | |
| 123 | return std::vector<TensorShape>({ tensorShape }); |
| 124 | } |
| 125 | |
| 126 | void Convolution3dLayer::ValidateTensorShapesFromInputs() |
| 127 | { |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 128 | VerifyLayerConnections(m_Param.GetNumInputs(), CHECK_LOCATION()); |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 129 | |
| 130 | const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape(); |
| 131 | |
| 132 | VerifyShapeInferenceType(outputShape, m_ShapeInferenceMethod); |
| 133 | |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 134 | if (!GetInputSlot(1).GetConnection()) |
| 135 | { |
| 136 | throw armnn::LayerValidationException("Convolution3dLayer: Weights should be connected to input slot 1."); |
| 137 | } |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 138 | |
| 139 | auto inferredShapes = InferOutputShapes({ |
Mike Kelly | a9ac6ba | 2023-06-30 15:18:26 +0100 | [diff] [blame] | 140 | GetInputSlot(0).GetTensorInfo().GetShape(), |
| 141 | GetInputSlot(1).GetTensorInfo().GetShape() }); |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 142 | |
Declan-ARM | 7c75e33 | 2024-03-12 16:40:25 +0000 | [diff] [blame^] | 143 | if (inferredShapes.size() != 1) |
| 144 | { |
| 145 | throw armnn::LayerValidationException("inferredShapes has " |
| 146 | + std::to_string(inferredShapes.size()) + |
| 147 | " elements - should only have 1."); |
| 148 | } |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 149 | |
| 150 | ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "Convolution3dLayer"); |
| 151 | } |
| 152 | |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 153 | void Convolution3dLayer::ExecuteStrategy(IStrategy& strategy) const |
| 154 | { |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 155 | strategy.ExecuteStrategy(this, GetParameters(), {}, GetName()); |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace armnn |