Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 1 | // |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 2 | // Copyright © 2017, 2019-2023 Arm Ltd and Contributors. All rights reserved. |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ClBatchToSpaceNdWorkload.hpp" |
| 7 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 8 | #include <armnn/utility/PolymorphicDowncast.hpp> |
| 9 | |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 10 | #include <cl/ClTensorHandle.hpp> |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 11 | |
| 12 | namespace armnn |
| 13 | { |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 14 | |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 15 | using namespace armcomputetensorutils; |
| 16 | |
Teresa Charlin | ca5c82a | 2023-03-28 11:00:36 +0100 | [diff] [blame] | 17 | arm_compute::Status ClBatchToSpaceNdWorkloadValidate(const TensorInfo& input, |
| 18 | const TensorInfo& output, |
| 19 | const BatchToSpaceNdDescriptor& descriptor) |
| 20 | { |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 21 | arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout); |
| 22 | arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout); |
Teresa Charlin | ca5c82a | 2023-03-28 11:00:36 +0100 | [diff] [blame] | 23 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 24 | arm_compute::Status statusBatchToSpace = arm_compute::Status(arm_compute::ErrorCode::OK); |
| 25 | arm_compute::Status statusReshapeInput = arm_compute::Status(arm_compute::ErrorCode::OK); |
| 26 | arm_compute::Status statusReshapeOutput = arm_compute::Status(arm_compute::ErrorCode::OK); |
| 27 | |
| 28 | arm_compute::TensorInfo aclReshapeInputInfo = aclInputInfo; |
| 29 | arm_compute::TensorInfo aclReshapeOutputInfo = aclOutputInfo; |
| 30 | |
| 31 | // When a spacial dimension is missing (rank=3) set W to 1 |
| 32 | const unsigned int rank = input.GetNumDimensions(); |
| 33 | if (rank == 3) |
| 34 | { |
| 35 | const arm_compute::TensorShape inputShape = aclInputInfo.tensor_shape(); |
| 36 | const arm_compute::TensorShape outputShape = aclOutputInfo.tensor_shape(); |
| 37 | |
| 38 | if (descriptor.m_DataLayout == armnn::DataLayout::NHWC) |
| 39 | { |
| 40 | // In ACL dimensions are right to left: C, W, H, N |
| 41 | aclInputInfo.set_tensor_shape({inputShape.x(), 1, inputShape.y(), inputShape.z()}); |
| 42 | aclOutputInfo.set_tensor_shape({outputShape.x(), 1, outputShape.y(), outputShape.z()}); |
| 43 | } |
| 44 | else if (descriptor.m_DataLayout == armnn::DataLayout::NCHW) |
| 45 | { |
| 46 | // In ACL dimensions are right to left: W, H, C, N |
| 47 | aclInputInfo.set_tensor_shape({1, inputShape.x(), inputShape.y(), inputShape.z()}); |
| 48 | aclOutputInfo.set_tensor_shape({1, outputShape.x(), outputShape.y(), outputShape.z()}); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | throw InvalidArgumentException("Unsupported or unknown DataLayout", CHECK_LOCATION()); |
| 53 | } |
| 54 | |
| 55 | statusReshapeInput = arm_compute::CLReshapeLayer::validate(&aclInputInfo, &aclReshapeInputInfo); |
| 56 | statusReshapeOutput = arm_compute::CLReshapeLayer::validate(&aclReshapeOutputInfo, &aclOutputInfo); |
| 57 | } |
| 58 | |
| 59 | // ArmNN blockShape is [H, W] ACl asks for W, H |
Teresa Charlin | ca5c82a | 2023-03-28 11:00:36 +0100 | [diff] [blame] | 60 | int32_t blockHeight = armnn::numeric_cast<int32_t>(descriptor.m_BlockShape[0]); |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 61 | int32_t blockWidth = (rank == 3) ? 1 : armnn::numeric_cast<int32_t>(descriptor.m_BlockShape[1]); |
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 | const arm_compute::CropInfo cropInfo = BuildArmComputeCropInfo(descriptor, rank); |
Teresa Charlin | ca5c82a | 2023-03-28 11:00:36 +0100 | [diff] [blame] | 64 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 65 | statusBatchToSpace = arm_compute::CLBatchToSpaceLayer::validate(rank == 3 ? &aclReshapeInputInfo : &aclInputInfo, |
| 66 | blockWidth, |
| 67 | blockHeight, |
| 68 | rank == 3 ? &aclReshapeOutputInfo : &aclOutputInfo, |
| 69 | cropInfo); |
Teresa Charlin | ca5c82a | 2023-03-28 11:00:36 +0100 | [diff] [blame] | 70 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 71 | if (statusReshapeInput.error_code() == arm_compute::ErrorCode::OK && |
| 72 | statusReshapeOutput.error_code() == arm_compute::ErrorCode::OK && |
| 73 | statusBatchToSpace.error_code() == arm_compute::ErrorCode::OK) |
| 74 | { |
| 75 | return arm_compute::Status(arm_compute::ErrorCode::OK, |
| 76 | "All BatchToSpace layers validate status OK."); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | return arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, |
| 81 | "BatchToSpace layer validate status failed." |
| 82 | + statusBatchToSpace.error_description() |
| 83 | + statusReshapeInput.error_description() |
| 84 | + statusReshapeOutput.error_description()); |
| 85 | } |
Teresa Charlin | ca5c82a | 2023-03-28 11:00:36 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 88 | ClBatchToSpaceNdWorkload::ClBatchToSpaceNdWorkload(const BatchToSpaceNdQueueDescriptor& descriptor, |
Sadik Armagan | e944475 | 2020-12-02 11:28:58 +0000 | [diff] [blame] | 89 | const WorkloadInfo& info, |
| 90 | const arm_compute::CLCompileContext& clCompileContext) |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 91 | : ClBaseWorkload<BatchToSpaceNdQueueDescriptor>(descriptor, info) |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 92 | { |
Keith Davis | bcd860a | 2021-08-05 14:20:33 +0100 | [diff] [blame] | 93 | // Report Profiling Details |
| 94 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClBatchToSpaceWorkload_Construct", |
| 95 | descriptor.m_Parameters, |
| 96 | info, |
| 97 | this->GetGuid()); |
| 98 | |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 99 | m_Data.ValidateInputsOutputs("ClBatchToSpaceNdWorkload", 1, 1); |
| 100 | |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 101 | arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor(); |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 102 | arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor(); |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 103 | |
| 104 | arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout); |
| 105 | input.info()->set_data_layout(aclDataLayout); |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 106 | output.info()->set_data_layout(aclDataLayout); |
| 107 | |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 108 | arm_compute::TensorInfo aclReshapeInputInfo = BuildArmComputeTensorInfo(info.m_InputTensorInfos[0], |
| 109 | m_Data.m_Parameters.m_DataLayout); |
| 110 | arm_compute::TensorInfo aclReshapeOutputInfo = BuildArmComputeTensorInfo(info.m_OutputTensorInfos[0], |
| 111 | m_Data.m_Parameters.m_DataLayout); |
| 112 | |
| 113 | const unsigned int rank = info.m_InputTensorInfos[0].GetNumDimensions(); |
| 114 | if (rank == 3) |
| 115 | { |
| 116 | const arm_compute::TensorShape inputShape = input.info()->tensor_shape(); |
| 117 | const arm_compute::TensorShape outputShape = output.info()->tensor_shape(); |
| 118 | |
| 119 | // When a spacial dimension is missing set W to 1 |
| 120 | if (m_Data.m_Parameters.m_DataLayout == armnn::DataLayout::NHWC) |
| 121 | { |
| 122 | // In ACL dimensions are right to left: C, W, H, N |
| 123 | aclReshapeInputInfo.set_tensor_shape({inputShape.x(), 1, inputShape.y(), inputShape.z()}); |
| 124 | aclReshapeOutputInfo.set_tensor_shape({outputShape.x(), 1, outputShape.y(), outputShape.z()}); |
| 125 | } |
| 126 | else if (m_Data.m_Parameters.m_DataLayout == armnn::DataLayout::NCHW) |
| 127 | { |
| 128 | // In ACL dimensions are right to left: W, H, C, N |
| 129 | aclReshapeInputInfo.set_tensor_shape({1, inputShape.x(), inputShape.y(), inputShape.z()}); |
| 130 | aclReshapeOutputInfo.set_tensor_shape({1, outputShape.x(), outputShape.y(), outputShape.z()}); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | throw InvalidArgumentException("Unsupported or unknown DataLayout", CHECK_LOCATION()); |
| 135 | } |
| 136 | |
| 137 | m_ReshapeInputTensor.allocator()->init(aclReshapeInputInfo); |
| 138 | m_ReshapeOutputTensor.allocator()->init(aclReshapeOutputInfo); |
| 139 | |
| 140 | InitialiseArmComputeTensorEmpty(m_ReshapeInputTensor); |
| 141 | InitialiseArmComputeTensorEmpty(m_ReshapeOutputTensor); |
| 142 | |
| 143 | m_LayerReshapeInput.reset(new arm_compute::CLReshapeLayer()); |
| 144 | m_LayerReshapeOutput.reset(new arm_compute::CLReshapeLayer()); |
| 145 | |
| 146 | m_LayerReshapeInput->configure(clCompileContext, &input, &m_ReshapeInputTensor); |
| 147 | m_LayerReshapeOutput->configure(clCompileContext, &m_ReshapeOutputTensor, &output); |
| 148 | } |
| 149 | |
| 150 | // ArmNN blockShape is [H, W] ACl asks for W, H |
| 151 | int32_t blockHeight = armnn::numeric_cast<int32_t>(descriptor.m_Parameters.m_BlockShape[0]); |
| 152 | int32_t blockWidth = (rank == 3) ? 1 : armnn::numeric_cast<int32_t>(descriptor.m_Parameters.m_BlockShape[1]); |
| 153 | |
Teresa Charlin | ca5c82a | 2023-03-28 11:00:36 +0100 | [diff] [blame] | 154 | const arm_compute::CropInfo cropInfo = BuildArmComputeCropInfo(descriptor.m_Parameters); |
| 155 | |
Kevin May | 9f6862d | 2021-10-22 15:42:28 +0100 | [diff] [blame] | 156 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 157 | ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClBatchToSpaceNdWorkload_configure"); |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 158 | m_Layer.configure(clCompileContext, |
| 159 | (rank == 3) ? &m_ReshapeInputTensor : &input, |
| 160 | blockWidth, |
| 161 | blockHeight, |
| 162 | (rank == 3) ? &m_ReshapeOutputTensor : &output, |
| 163 | cropInfo); |
Kevin May | 9f6862d | 2021-10-22 15:42:28 +0100 | [diff] [blame] | 164 | } |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void ClBatchToSpaceNdWorkload::Execute() const |
| 168 | { |
Mike Kelly | 7cbe781 | 2023-07-25 17:37:33 +0100 | [diff] [blame] | 169 | ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClBatchToSpaceNdWorkload_Execute"); |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 170 | if (m_LayerReshapeInput) |
| 171 | { |
| 172 | m_LayerReshapeInput->run(); |
| 173 | } |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 174 | RunClFunction(m_Layer, CHECK_LOCATION()); |
Teresa Charlin | 2ea403d | 2023-06-19 12:06:19 +0100 | [diff] [blame] | 175 | if (m_LayerReshapeOutput) |
| 176 | { |
| 177 | m_LayerReshapeOutput->run(); |
| 178 | } |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Mike Kelly | 831faed | 2018-11-28 11:52:08 +0000 | [diff] [blame] | 181 | } //namespace armnn |