blob: 2ffbbdce53b859a56fd7afca1d058a6e16e0f3a0 [file] [log] [blame]
Mike Kelly831faed2018-11-28 11:52:08 +00001//
Teresa Charlin2ea403d2023-06-19 12:06:19 +01002// Copyright © 2017, 2019-2023 Arm Ltd and Contributors. All rights reserved.
Mike Kelly831faed2018-11-28 11:52:08 +00003// SPDX-License-Identifier: MIT
4//
5
6#include "ClBatchToSpaceNdWorkload.hpp"
7
Teresa Charlin2ea403d2023-06-19 12:06:19 +01008#include <armnn/utility/PolymorphicDowncast.hpp>
9
Mike Kelly831faed2018-11-28 11:52:08 +000010#include <cl/ClTensorHandle.hpp>
Mike Kelly831faed2018-11-28 11:52:08 +000011
12namespace armnn
13{
Teresa Charlin2ea403d2023-06-19 12:06:19 +010014
Mike Kelly831faed2018-11-28 11:52:08 +000015using namespace armcomputetensorutils;
16
Teresa Charlinca5c82a2023-03-28 11:00:36 +010017arm_compute::Status ClBatchToSpaceNdWorkloadValidate(const TensorInfo& input,
18 const TensorInfo& output,
19 const BatchToSpaceNdDescriptor& descriptor)
20{
Teresa Charlin2ea403d2023-06-19 12:06:19 +010021 arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
22 arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
Teresa Charlinca5c82a2023-03-28 11:00:36 +010023
Teresa Charlin2ea403d2023-06-19 12:06:19 +010024 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 Charlinca5c82a2023-03-28 11:00:36 +010060 int32_t blockHeight = armnn::numeric_cast<int32_t>(descriptor.m_BlockShape[0]);
Teresa Charlin2ea403d2023-06-19 12:06:19 +010061 int32_t blockWidth = (rank == 3) ? 1 : armnn::numeric_cast<int32_t>(descriptor.m_BlockShape[1]);
Teresa Charlinca5c82a2023-03-28 11:00:36 +010062
Teresa Charlin2ea403d2023-06-19 12:06:19 +010063 const arm_compute::CropInfo cropInfo = BuildArmComputeCropInfo(descriptor, rank);
Teresa Charlinca5c82a2023-03-28 11:00:36 +010064
Teresa Charlin2ea403d2023-06-19 12:06:19 +010065 statusBatchToSpace = arm_compute::CLBatchToSpaceLayer::validate(rank == 3 ? &aclReshapeInputInfo : &aclInputInfo,
66 blockWidth,
67 blockHeight,
68 rank == 3 ? &aclReshapeOutputInfo : &aclOutputInfo,
69 cropInfo);
Teresa Charlinca5c82a2023-03-28 11:00:36 +010070
Teresa Charlin2ea403d2023-06-19 12:06:19 +010071 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 Charlinca5c82a2023-03-28 11:00:36 +010086}
87
Keith Davisbcd860a2021-08-05 14:20:33 +010088ClBatchToSpaceNdWorkload::ClBatchToSpaceNdWorkload(const BatchToSpaceNdQueueDescriptor& descriptor,
Sadik Armagane9444752020-12-02 11:28:58 +000089 const WorkloadInfo& info,
90 const arm_compute::CLCompileContext& clCompileContext)
Teresa Charlin588cbdf2022-01-19 15:55:37 +000091 : ClBaseWorkload<BatchToSpaceNdQueueDescriptor>(descriptor, info)
Mike Kelly831faed2018-11-28 11:52:08 +000092{
Keith Davisbcd860a2021-08-05 14:20:33 +010093 // Report Profiling Details
94 ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClBatchToSpaceWorkload_Construct",
95 descriptor.m_Parameters,
96 info,
97 this->GetGuid());
98
Mike Kelly831faed2018-11-28 11:52:08 +000099 m_Data.ValidateInputsOutputs("ClBatchToSpaceNdWorkload", 1, 1);
100
Mike Kelly831faed2018-11-28 11:52:08 +0000101 arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
Mike Kelly831faed2018-11-28 11:52:08 +0000102 arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
Teresa Charlin2ea403d2023-06-19 12:06:19 +0100103
104 arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
105 input.info()->set_data_layout(aclDataLayout);
Mike Kelly831faed2018-11-28 11:52:08 +0000106 output.info()->set_data_layout(aclDataLayout);
107
Teresa Charlin2ea403d2023-06-19 12:06:19 +0100108 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 Charlinca5c82a2023-03-28 11:00:36 +0100154 const arm_compute::CropInfo cropInfo = BuildArmComputeCropInfo(descriptor.m_Parameters);
155
Kevin May9f6862d2021-10-22 15:42:28 +0100156 {
Mike Kelly7cbe7812023-07-25 17:37:33 +0100157 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClBatchToSpaceNdWorkload_configure");
Teresa Charlin2ea403d2023-06-19 12:06:19 +0100158 m_Layer.configure(clCompileContext,
159 (rank == 3) ? &m_ReshapeInputTensor : &input,
160 blockWidth,
161 blockHeight,
162 (rank == 3) ? &m_ReshapeOutputTensor : &output,
163 cropInfo);
Kevin May9f6862d2021-10-22 15:42:28 +0100164 }
Mike Kelly831faed2018-11-28 11:52:08 +0000165}
166
167void ClBatchToSpaceNdWorkload::Execute() const
168{
Mike Kelly7cbe7812023-07-25 17:37:33 +0100169 ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClBatchToSpaceNdWorkload_Execute");
Teresa Charlin2ea403d2023-06-19 12:06:19 +0100170 if (m_LayerReshapeInput)
171 {
172 m_LayerReshapeInput->run();
173 }
Mike Kelly831faed2018-11-28 11:52:08 +0000174 RunClFunction(m_Layer, CHECK_LOCATION());
Teresa Charlin2ea403d2023-06-19 12:06:19 +0100175 if (m_LayerReshapeOutput)
176 {
177 m_LayerReshapeOutput->run();
178 }
Mike Kelly831faed2018-11-28 11:52:08 +0000179}
180
Mike Kelly831faed2018-11-28 11:52:08 +0000181} //namespace armnn