ArmNN
 24.08
ClBatchToSpaceNdWorkload.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017, 2019-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
9 
10 #include <cl/ClTensorHandle.hpp>
11 
12 namespace armnn
13 {
14 
15 using namespace armcomputetensorutils;
16 
18  const TensorInfo& output,
19  const BatchToSpaceNdDescriptor& descriptor)
20 {
21  arm_compute::TensorInfo aclInputInfo = BuildArmComputeTensorInfo(input, descriptor.m_DataLayout);
22  arm_compute::TensorInfo aclOutputInfo = BuildArmComputeTensorInfo(output, descriptor.m_DataLayout);
23 
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
60  int32_t blockHeight = armnn::numeric_cast<int32_t>(descriptor.m_BlockShape[0]);
61  int32_t blockWidth = (rank == 3) ? 1 : armnn::numeric_cast<int32_t>(descriptor.m_BlockShape[1]);
62 
63  const arm_compute::CropInfo cropInfo = BuildArmComputeCropInfo(descriptor, rank);
64 
65  statusBatchToSpace = arm_compute::CLBatchToSpaceLayer::validate(rank == 3 ? &aclReshapeInputInfo : &aclInputInfo,
66  blockWidth,
67  blockHeight,
68  rank == 3 ? &aclReshapeOutputInfo : &aclOutputInfo,
69  cropInfo);
70 
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  }
86 }
87 
89  const WorkloadInfo& info,
90  const arm_compute::CLCompileContext& clCompileContext)
92 {
93  // Report Profiling Details
94  ARMNN_REPORT_PROFILING_WORKLOAD_DESC("ClBatchToSpaceWorkload_Construct",
95  descriptor.m_Parameters,
96  info,
97  this->GetGuid());
98 
99  m_Data.ValidateInputsOutputs("ClBatchToSpaceNdWorkload", 1, 1);
100 
101  arm_compute::ICLTensor& input = static_cast<IClTensorHandle*>(m_Data.m_Inputs[0])->GetTensor();
102  arm_compute::ICLTensor& output = static_cast<IClTensorHandle*>(m_Data.m_Outputs[0])->GetTensor();
103 
104  arm_compute::DataLayout aclDataLayout = ConvertDataLayout(m_Data.m_Parameters.m_DataLayout);
105  input.info()->set_data_layout(aclDataLayout);
106  output.info()->set_data_layout(aclDataLayout);
107 
108  arm_compute::TensorInfo aclReshapeInputInfo = BuildArmComputeTensorInfo(info.m_InputTensorInfos[0],
110  arm_compute::TensorInfo aclReshapeOutputInfo = BuildArmComputeTensorInfo(info.m_OutputTensorInfos[0],
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
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  }
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 
154  const arm_compute::CropInfo cropInfo = BuildArmComputeCropInfo(descriptor.m_Parameters);
155 
156  {
157  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClBatchToSpaceNdWorkload_configure");
158  m_Layer.configure(clCompileContext,
159  (rank == 3) ? &m_ReshapeInputTensor : &input,
160  blockWidth,
161  blockHeight,
162  (rank == 3) ? &m_ReshapeOutputTensor : &output,
163  cropInfo);
164  }
165 }
166 
168 {
169  ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID("ClBatchToSpaceNdWorkload_Execute");
170  if (m_LayerReshapeInput)
171  {
172  m_LayerReshapeInput->run();
173  }
174  RunClFunction(m_Layer, CHECK_LOCATION());
175  if (m_LayerReshapeOutput)
176  {
177  m_LayerReshapeOutput->run();
178  }
179 }
180 
181 } //namespace armnn
armnn::DataLayout
DataLayout
Definition: Types.hpp:62
armnn::DataLayout::NHWC
@ NHWC
armnn::QueueDescriptor::ValidateInputsOutputs
void ValidateInputsOutputs(const std::string &descName, unsigned int numExpectedIn, unsigned int numExpectedOut) const
Definition: WorkloadData.cpp:447
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::ClBatchToSpaceNdWorkloadValidate
arm_compute::Status ClBatchToSpaceNdWorkloadValidate(const TensorInfo &input, const TensorInfo &output, const BatchToSpaceNdDescriptor &descriptor)
Definition: ClBatchToSpaceNdWorkload.cpp:17
armnn::BatchToSpaceNdQueueDescriptor
Definition: WorkloadData.hpp:462
armnn::TensorInfo::GetNumDimensions
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:197
armnn::BatchToSpaceNdDescriptor::m_BlockShape
std::vector< unsigned int > m_BlockShape
Block shape values.
Definition: Descriptors.hpp:898
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::ClBaseWorkload
Definition: ClBaseWorkload.hpp:13
ClBatchToSpaceNdWorkload.hpp
armnn::ClBatchToSpaceNdWorkload::Execute
virtual void Execute() const override
Definition: ClBatchToSpaceNdWorkload.cpp:167
armnn::QueueDescriptorWithParameters::m_Parameters
LayerDescriptor m_Parameters
Definition: WorkloadData.hpp:66
ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID
#define ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID(label)
Creates a profiling event that uses GetGuid() and GetName() from the calling class.
Definition: ClWorkloadUtils.hpp:36
armnn::WorkloadInfo
Contains information about TensorInfos of a layer.
Definition: WorkloadInfo.hpp:16
PolymorphicDowncast.hpp
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::BoostLogSeverityMapping::info
@ info
armnn::QueueDescriptor::m_Outputs
std::vector< ITensorHandle * > m_Outputs
Definition: WorkloadData.hpp:27
armnn::BatchToSpaceNdDescriptor
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
Definition: Descriptors.hpp:875
ARMNN_REPORT_PROFILING_WORKLOAD_DESC
#define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos, guid)
Definition: Profiling.hpp:227
armnn::IClTensorHandle
Definition: IClTensorHandle.hpp:13
armnn::Status
Status
Definition: Types.hpp:42
ClTensorHandle.hpp
armnn::BaseWorkload< BatchToSpaceNdQueueDescriptor >::m_Data
BatchToSpaceNdQueueDescriptor m_Data
Definition: Workload.hpp:89
armnn::RunClFunction
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
Definition: ClWorkloadUtils.hpp:167
armnn::ClBatchToSpaceNdWorkload::ClBatchToSpaceNdWorkload
ClBatchToSpaceNdWorkload(const BatchToSpaceNdQueueDescriptor &descriptor, const WorkloadInfo &info, const arm_compute::CLCompileContext &clCompileContext)
Definition: ClBatchToSpaceNdWorkload.cpp:88
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::BatchToSpaceNdDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:902
armnn::QueueDescriptor::m_Inputs
std::vector< ITensorHandle * > m_Inputs
Definition: WorkloadData.hpp:26
armnn::DataLayout::NCHW
@ NCHW