Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 2 | // Copyright © 2020-2023 Arm Ltd and Contributors. All rights reserved. |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "NeonBatchToSpaceNdWorkload.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 | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 10 | namespace armnn |
| 11 | { |
| 12 | |
| 13 | using namespace armcomputetensorutils; |
| 14 | |
| 15 | arm_compute::Status NeonBatchToSpaceNdWorkloadValidate(const TensorInfo& input, |
| 16 | const TensorInfo& output, |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 17 | const BatchToSpaceNdDescriptor& descriptor) |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 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 | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 21 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 22 | arm_compute::Status statusBatchToSpace = 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 |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +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 | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 60 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 61 | const arm_compute::CropInfo cropInfo = BuildArmComputeCropInfo(descriptor, rank); |
Teresa Charlin | ca5c82a | 2023-03-28 11:00:36 +0100 | [diff] [blame] | 62 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 63 | statusBatchToSpace = arm_compute::NEBatchToSpaceLayer::validate(rank == 3 ? &aclReshapeInputInfo : &aclInputInfo, |
| 64 | blockWidth, |
| 65 | blockHeight, |
| 66 | rank == 3 ? &aclReshapeOutputInfo : &aclOutputInfo, |
| 67 | cropInfo); |
| 68 | |
| 69 | if (statusReshapeInput.error_code() == arm_compute::ErrorCode::OK && |
| 70 | statusReshapeOutput.error_code() == arm_compute::ErrorCode::OK && |
| 71 | statusBatchToSpace.error_code() == arm_compute::ErrorCode::OK) |
| 72 | { |
| 73 | return arm_compute::Status(arm_compute::ErrorCode::OK, |
| 74 | "All BatchToSpace layers validate status OK."); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | return arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, |
| 79 | "BatchToSpace layer validate status failed." |
| 80 | + statusBatchToSpace.error_description() |
| 81 | + statusReshapeInput.error_description() |
| 82 | + statusReshapeOutput.error_description()); |
| 83 | } |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 86 | NeonBatchToSpaceNdWorkload::NeonBatchToSpaceNdWorkload(const BatchToSpaceNdQueueDescriptor& descriptor, |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 87 | const WorkloadInfo& info) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 88 | : NeonBaseWorkload<BatchToSpaceNdQueueDescriptor>(descriptor, info) |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 89 | { |
Keith Davis | 2d0679f | 2021-08-05 11:35:00 +0100 | [diff] [blame] | 90 | // Report Profiling Details |
| 91 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("NeonBatchToSpaceWorkload_Construct", |
| 92 | descriptor.m_Parameters, |
| 93 | info, |
| 94 | this->GetGuid()); |
| 95 | |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 96 | m_Data.ValidateInputsOutputs("NeonBatchToSpaceNdWorkload", 1, 1); |
| 97 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 98 | arm_compute::ITensor& input = PolymorphicPointerDowncast<IAclTensorHandle>(m_Data.m_Inputs[0])->GetTensor(); |
| 99 | arm_compute::ITensor& output = PolymorphicPointerDowncast<IAclTensorHandle>(m_Data.m_Outputs[0])->GetTensor(); |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 100 | |
| 101 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 102 | input.info()->set_data_layout(aclDataLayout); |
| 103 | output.info()->set_data_layout(aclDataLayout); |
| 104 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 105 | arm_compute::TensorInfo aclReshapeInputInfo = BuildArmComputeTensorInfo(info.m_InputTensorInfos[0], |
| 106 | m_Data.m_Parameters.m_DataLayout); |
| 107 | arm_compute::TensorInfo aclReshapeOutputInfo = BuildArmComputeTensorInfo(info.m_OutputTensorInfos[0], |
| 108 | m_Data.m_Parameters.m_DataLayout); |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 109 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 110 | const unsigned int rank = info.m_InputTensorInfos[0].GetNumDimensions(); |
| 111 | if (rank == 3) |
| 112 | { |
| 113 | const arm_compute::TensorShape inputShape = input.info()->tensor_shape(); |
| 114 | const arm_compute::TensorShape outputShape = output.info()->tensor_shape(); |
| 115 | |
| 116 | // When a spacial dimension is missing set W to 1 |
| 117 | if (m_Data.m_Parameters.m_DataLayout == armnn::DataLayout::NHWC) |
| 118 | { |
| 119 | // In ACL dimensions are right to left: C, W, H, N |
| 120 | aclReshapeInputInfo.set_tensor_shape({inputShape.x(), 1, inputShape.y(), inputShape.z()}); |
| 121 | aclReshapeOutputInfo.set_tensor_shape({outputShape.x(), 1, outputShape.y(), outputShape.z()}); |
| 122 | } |
| 123 | else if (m_Data.m_Parameters.m_DataLayout == armnn::DataLayout::NCHW) |
| 124 | { |
| 125 | // In ACL dimensions are right to left: W, H, C, N |
| 126 | aclReshapeInputInfo.set_tensor_shape({1, inputShape.x(), inputShape.y(), inputShape.z()}); |
| 127 | aclReshapeOutputInfo.set_tensor_shape({1, outputShape.x(), outputShape.y(), outputShape.z()}); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | throw InvalidArgumentException("Unsupported or unknown DataLayout", CHECK_LOCATION()); |
| 132 | } |
| 133 | |
| 134 | m_ReshapeInputTensor.allocator()->init(aclReshapeInputInfo); |
| 135 | m_ReshapeOutputTensor.allocator()->init(aclReshapeOutputInfo); |
| 136 | |
| 137 | InitialiseArmComputeTensorEmpty(m_ReshapeInputTensor); |
| 138 | InitialiseArmComputeTensorEmpty(m_ReshapeOutputTensor); |
| 139 | |
| 140 | m_LayerReshapeInput.reset(new arm_compute::NEReshapeLayer()); |
| 141 | m_LayerReshapeOutput.reset(new arm_compute::NEReshapeLayer()); |
| 142 | |
| 143 | m_LayerReshapeInput->configure(&input, &m_ReshapeInputTensor); |
| 144 | m_LayerReshapeOutput->configure(&m_ReshapeOutputTensor, &output); |
| 145 | } |
| 146 | |
| 147 | // ArmNN blockShape is [H, W] ACl asks for W, H |
| 148 | int32_t blockHeight = armnn::numeric_cast<int32_t>(descriptor.m_Parameters.m_BlockShape[0]); |
| 149 | int32_t blockWidth = (rank == 3) ? 1 : armnn::numeric_cast<int32_t>(descriptor.m_Parameters.m_BlockShape[1]); |
| 150 | |
| 151 | const arm_compute::CropInfo cropInfo = BuildArmComputeCropInfo(descriptor.m_Parameters, rank); |
Teresa Charlin | ca5c82a | 2023-03-28 11:00:36 +0100 | [diff] [blame] | 152 | |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 153 | m_Layer.reset(new arm_compute::NEBatchToSpaceLayer()); |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 154 | m_Layer->configure(rank == 3 ? &m_ReshapeInputTensor : &input, |
| 155 | blockWidth, |
| 156 | blockHeight, |
| 157 | rank == 3 ? &m_ReshapeOutputTensor : &output, |
| 158 | cropInfo); |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 159 | m_Layer->prepare(); |
| 160 | } |
| 161 | |
| 162 | void NeonBatchToSpaceNdWorkload::Execute() const |
| 163 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame^] | 164 | ARMNN_SCOPED_PROFILING_EVENT_NEON_NAME_GUID("NeonBatchToSpaceNdWorkload_Execute"); |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 165 | if (m_LayerReshapeInput) |
| 166 | { |
| 167 | m_LayerReshapeInput->run(); |
| 168 | } |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 169 | if (m_Layer) |
| 170 | { |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 171 | m_Layer->run(); |
| 172 | } |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 173 | if (m_LayerReshapeOutput) |
| 174 | { |
| 175 | m_LayerReshapeOutput->run(); |
| 176 | } |
Mike Kelly | 5685802 | 2020-01-27 12:14:47 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | } //namespace armnn |