Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "NeonSpaceToBatchNdWorkload.hpp" |
| 7 | |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 8 | #include <armnn/utility/PolymorphicDowncast.hpp> |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 9 | |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 10 | namespace armnn |
| 11 | { |
| 12 | |
| 13 | using namespace armcomputetensorutils; |
| 14 | |
| 15 | arm_compute::Status NeonSpaceToBatchNdWorkloadValidate(const TensorInfo& input, |
| 16 | const TensorInfo& output, |
| 17 | const SpaceToBatchNdDescriptor& descriptor) |
| 18 | { |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 19 | arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 20 | arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 21 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 22 | arm_compute::Status statusSpaceToBatch = arm_compute::Status(arm_compute::ErrorCode::OK); |
| 23 | arm_compute::Status statusReshapeInput = arm_compute::Status(arm_compute::ErrorCode::OK); |
| 24 | arm_compute::Status statusReshapeOutput = arm_compute::Status(arm_compute::ErrorCode::OK); |
| 25 | |
| 26 | arm_compute::TensorInfo aclReshapeInputInfo = aclInputInfo; |
| 27 | arm_compute::TensorInfo aclReshapeOutputInfo = aclOutputInfo; |
| 28 | |
| 29 | // When a spacial dimension is missing (rank=3) set W to 1 |
| 30 | const unsigned int rank = input.GetNumDimensions(); |
| 31 | if (rank == 3) |
| 32 | { |
| 33 | const arm_compute::TensorShape inputShape = aclInputInfo.tensor_shape(); |
| 34 | const arm_compute::TensorShape outputShape = aclOutputInfo.tensor_shape(); |
| 35 | |
| 36 | if (descriptor.m_DataLayout == armnn::DataLayout::NHWC) |
| 37 | { |
| 38 | // In ACL dimensions are right to left: C, W, H, N |
| 39 | aclReshapeInputInfo.set_tensor_shape({inputShape.x(), 1, inputShape.y(), inputShape.z()}); |
| 40 | aclReshapeOutputInfo.set_tensor_shape({outputShape.x(), 1, outputShape.y(), outputShape.z()}); |
| 41 | } |
| 42 | else if (descriptor.m_DataLayout == armnn::DataLayout::NCHW) |
| 43 | { |
| 44 | // In ACL dimensions are right to left: W, H, C, N |
| 45 | aclReshapeInputInfo.set_tensor_shape({1, inputShape.x(), inputShape.y(), inputShape.z()}); |
| 46 | aclReshapeOutputInfo.set_tensor_shape({1, outputShape.x(), outputShape.y(), outputShape.z()}); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | throw InvalidArgumentException("Unsupported or unknown DataLayout", CHECK_LOCATION()); |
| 51 | } |
| 52 | |
| 53 | statusReshapeInput = arm_compute::NEReshapeLayer::validate(&aclInputInfo, &aclReshapeInputInfo); |
| 54 | statusReshapeOutput = arm_compute::NEReshapeLayer::validate(&aclReshapeOutputInfo, &aclOutputInfo); |
| 55 | } |
| 56 | |
| 57 | // ArmNN blockShape is [H, W] ACl asks for W, H |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 58 | int32_t blockHeight = armnn::numeric_cast<int32_t>(descriptor.m_BlockShape[0]); |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 59 | int32_t blockWidth = (rank == 3) ? 1 : armnn::numeric_cast<int32_t>(descriptor.m_BlockShape[1]); |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 60 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 61 | unsigned int padLeft = (rank == 3) ? 0 : descriptor.m_PadList[1].first; |
| 62 | unsigned int padRight = (rank == 3) ? 0 : descriptor.m_PadList[1].second; |
| 63 | arm_compute::Size2D paddingLeftTop = BuildArmComputeSize2D(padLeft, |
| 64 | descriptor.m_PadList[0].first); |
| 65 | arm_compute::Size2D paddingRightBottom = BuildArmComputeSize2D(padRight, |
| 66 | descriptor.m_PadList[0].second); |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 67 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 68 | statusSpaceToBatch = arm_compute::NESpaceToBatchLayer::validate(rank == 3 ? &aclReshapeInputInfo : &aclInputInfo, |
| 69 | blockWidth, |
| 70 | blockHeight, |
| 71 | paddingLeftTop, |
| 72 | paddingRightBottom, |
| 73 | rank == 3 ? &aclReshapeOutputInfo : &aclOutputInfo); |
| 74 | |
| 75 | if (statusReshapeInput.error_code() == arm_compute::ErrorCode::OK && |
| 76 | statusReshapeOutput.error_code() == arm_compute::ErrorCode::OK && |
| 77 | statusSpaceToBatch.error_code() == arm_compute::ErrorCode::OK) |
| 78 | { |
| 79 | return arm_compute::Status(arm_compute::ErrorCode::OK, |
| 80 | "All SpaceToBatch layers validate status OK."); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | return arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, |
| 85 | "SpaceToBatch layer validate status failed." |
| 86 | + statusSpaceToBatch.error_description() |
| 87 | + statusReshapeInput.error_description() |
| 88 | + statusReshapeOutput.error_description()); |
| 89 | } |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 92 | NeonSpaceToBatchNdWorkload::NeonSpaceToBatchNdWorkload(const SpaceToBatchNdQueueDescriptor& descriptor, |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 93 | const WorkloadInfo& info) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 94 | : NeonBaseWorkload<SpaceToBatchNdQueueDescriptor>(descriptor, info) |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 95 | { |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 96 | // Report Profiling Details |
| 97 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonSpaceToBatchNdWorkload_Construct", |
| 98 | descriptor.m_Parameters, |
| 99 | info, |
| 100 | this->GetGuid()); |
| 101 | |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 102 | m_Data.ValidateInputsOutputs("NESpaceToBatchNdWorkload", 1, 1); |
| 103 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 104 | arm_compute::ITensor& input = PolymorphicPointerDowncast<IAclTensorHandle>(m_Data.m_Inputs[0])->GetTensor(); |
| 105 | arm_compute::ITensor& output = PolymorphicPointerDowncast<IAclTensorHandle>(m_Data.m_Outputs[0])->GetTensor(); |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 106 | |
| 107 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 108 | input.info()->set_data_layout(aclDataLayout); |
| 109 | output.info()->set_data_layout(aclDataLayout); |
| 110 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 111 | arm_compute::TensorInfo aclReshapeInputInfo = BuildArmComputeTensorInfo(info.m_InputTensorInfos[0], |
| 112 | m_Data.m_Parameters.m_DataLayout); |
| 113 | arm_compute::TensorInfo aclReshapeOutputInfo = BuildArmComputeTensorInfo(info.m_OutputTensorInfos[0], |
| 114 | m_Data.m_Parameters.m_DataLayout); |
| 115 | |
| 116 | const unsigned int rank = info.m_InputTensorInfos[0].GetNumDimensions(); |
| 117 | if (rank == 3) |
| 118 | { |
| 119 | const arm_compute::TensorShape inputShape = input.info()->tensor_shape(); |
| 120 | const arm_compute::TensorShape outputShape = output.info()->tensor_shape(); |
| 121 | |
| 122 | // When a spacial dimension is missing set W to 1 |
| 123 | if (m_Data.m_Parameters.m_DataLayout == armnn::DataLayout::NHWC) |
| 124 | { |
| 125 | // In ACL dimensions are right to left: C, W, H, N |
| 126 | aclReshapeInputInfo.set_tensor_shape({inputShape.x(), 1, inputShape.y(), inputShape.z()}); |
| 127 | aclReshapeOutputInfo.set_tensor_shape({outputShape.x(), 1, outputShape.y(), outputShape.z()}); |
| 128 | } |
| 129 | else if (m_Data.m_Parameters.m_DataLayout == armnn::DataLayout::NCHW) |
| 130 | { |
| 131 | // In ACL dimensions are right to left: W, H, C, N |
| 132 | aclReshapeInputInfo.set_tensor_shape({1, inputShape.x(), inputShape.y(), inputShape.z()}); |
| 133 | aclReshapeOutputInfo.set_tensor_shape({1, outputShape.x(), outputShape.y(), outputShape.z()}); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | throw InvalidArgumentException("Unsupported or unknown DataLayout", CHECK_LOCATION()); |
| 138 | } |
| 139 | |
| 140 | m_ReshapeInputTensor.allocator()->init(aclReshapeInputInfo); |
| 141 | m_ReshapeOutputTensor.allocator()->init(aclReshapeOutputInfo); |
| 142 | |
| 143 | InitialiseArmComputeTensorEmpty(m_ReshapeInputTensor); |
| 144 | InitialiseArmComputeTensorEmpty(m_ReshapeOutputTensor); |
| 145 | |
| 146 | m_LayerReshapeInput.reset(new arm_compute::NEReshapeLayer()); |
| 147 | m_LayerReshapeOutput.reset(new arm_compute::NEReshapeLayer()); |
| 148 | |
| 149 | m_LayerReshapeInput->configure(&input, &m_ReshapeInputTensor); |
| 150 | m_LayerReshapeOutput->configure(&m_ReshapeOutputTensor, &output); |
| 151 | } |
| 152 | |
| 153 | // ArmNN blockShape is [H, W] ACl asks for W, H |
| 154 | int32_t blockHeight = armnn::numeric_cast<int32_t>(m_Data.m_Parameters.m_BlockShape[0]); |
| 155 | int32_t blockWidth = (rank == 3) ? 1: armnn::numeric_cast<int32_t>(descriptor.m_Parameters.m_BlockShape[1]); |
| 156 | |
| 157 | unsigned int padLeft = (rank == 3) ? 0 : descriptor.m_Parameters.m_PadList[1].first; |
| 158 | unsigned int padRight = (rank == 3) ? 0 : descriptor.m_Parameters.m_PadList[1].second; |
| 159 | arm_compute::Size2D paddingLeftTop = BuildArmComputeSize2D(padLeft, |
| 160 | descriptor.m_Parameters.m_PadList[0].first); |
| 161 | arm_compute::Size2D paddingRightBottom = BuildArmComputeSize2D(padRight, |
| 162 | descriptor.m_Parameters.m_PadList[0].second); |
| 163 | |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 164 | m_Layer.reset(new arm_compute::NESpaceToBatchLayer()); |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 165 | m_Layer->configure((rank == 3) ? &m_ReshapeInputTensor : &input, |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 166 | blockWidth, |
| 167 | blockHeight, |
| 168 | paddingLeftTop, |
| 169 | paddingRightBottom, |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 170 | (rank == 3) ? &m_ReshapeOutputTensor : &output); |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 171 | m_Layer->prepare(); |
| 172 | } |
| 173 | |
| 174 | void NeonSpaceToBatchNdWorkload::Execute() const |
| 175 | { |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 176 | ARMNN_SCOPED_PROFILING_EVENT_NEON_GUID("NeonSpaceToBatchNdWorkload_Execute", this->GetGuid()); |
| 177 | if (m_LayerReshapeInput) |
| 178 | { |
| 179 | m_LayerReshapeInput->run(); |
| 180 | } |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 181 | if (m_Layer) |
| 182 | { |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 183 | m_Layer->run(); |
| 184 | } |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 185 | if (m_LayerReshapeOutput) |
| 186 | { |
| 187 | m_LayerReshapeOutput->run(); |
| 188 | } |
Mike Kelly | 0be3a88 | 2020-01-24 11:27:50 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | } //namespace armnn |