ArmNN
 24.02
ClWorkloadUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <BFloat16.hpp>
8 #include <Half.hpp>
9 
11 #include <cl/OpenClTimer.hpp>
13 
14 #include <armnn/Utils.hpp>
15 
16 #include <arm_compute/runtime/CL/CLTensor.h>
17 #include <arm_compute/runtime/IFunction.h>
18 
19 #include <sstream>
20 
21 #define ARMNN_SCOPED_PROFILING_EVENT_CL(name) \
22  ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(armnn::Compute::GpuAcc, \
23  armnn::EmptyOptional(), \
24  name, \
25  armnn::OpenClTimer(), \
26  armnn::WallClockTimer())
27 
28 #define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid) \
29  ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(armnn::Compute::GpuAcc, \
30  guid, \
31  GetName() + "_" + name, \
32  armnn::OpenClTimer(), \
33  armnn::WallClockTimer())
34 
35 /// Creates a profiling event that uses GetGuid() and GetName() from the calling class
36 #define ARMNN_SCOPED_PROFILING_EVENT_CL_NAME_GUID(label) \
37  ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(armnn::Compute::GpuAcc, \
38  this->GetGuid(), \
39  this->GetName() + "_" + label, \
40  armnn::OpenClTimer(), \
41  armnn::WallClockTimer())
42 
43 namespace armnn
44 {
45 
46 inline std::string GetConvolutionMethodString(arm_compute::ConvolutionMethod& convolutionMethod)
47 {
48  switch (convolutionMethod)
49  {
50  case arm_compute::ConvolutionMethod::FFT:
51  return "FFT";
52  case arm_compute::ConvolutionMethod::DIRECT:
53  return "Direct";
54  case arm_compute::ConvolutionMethod::GEMM:
55  return "GEMM";
56  case arm_compute::ConvolutionMethod::WINOGRAD:
57  return "Winograd";
58  default:
59  return "Unknown";
60  }
61 }
62 
63 template <typename T>
64 void CopyArmComputeClTensorData(arm_compute::CLTensor& dstTensor, const T* srcData)
65 {
66  {
67  ARMNN_SCOPED_PROFILING_EVENT_CL("MapClTensorForWriting");
68  dstTensor.map(true);
69  }
70 
71  {
72  ARMNN_SCOPED_PROFILING_EVENT_CL("CopyToClTensor");
73  armcomputetensorutils::CopyArmComputeITensorData<T>(srcData, dstTensor);
74  }
75 
76  dstTensor.unmap();
77 }
78 
79 inline auto SetClStridedSliceData(const std::vector<int>& m_begin,
80  const std::vector<int>& m_end,
81  const std::vector<int>& m_stride)
82 {
86 
87  unsigned int num_dims = static_cast<unsigned int>(m_begin.size());
88 
89  for (unsigned int i = 0; i < num_dims; i++) {
90  unsigned int revertedIndex = num_dims - i - 1;
91 
92  starts.set(i, static_cast<int>(m_begin[revertedIndex]));
93  ends.set(i, static_cast<int>(m_end[revertedIndex]));
94  strides.set(i, static_cast<int>(m_stride[revertedIndex]));
95  }
96 
97  return std::make_tuple(starts, ends, strides);
98 }
99 
100 inline auto SetClSliceData(const std::vector<unsigned int>& m_begin,
101  const std::vector<unsigned int>& m_size)
102 {
103  // This function must translate the size vector given to an end vector
104  // expected by the ACL NESlice workload
107 
108  unsigned int num_dims = static_cast<unsigned int>(m_begin.size());
109 
110  // For strided slices, we have the relationship size = (end - begin) / stride
111  // For slice, we assume stride to be a vector of all ones, yielding the formula
112  // size = (end - begin) therefore we know end = size + begin
113  for (unsigned int i = 0; i < num_dims; i++)
114  {
115  unsigned int revertedIndex = num_dims - i - 1;
116 
117  starts.set(i, static_cast<int>(m_begin[revertedIndex]));
118  ends.set(i, static_cast<int>(m_begin[revertedIndex] + m_size[revertedIndex]));
119  }
120 
121  return std::make_tuple(starts, ends);
122 }
123 
124 inline void InitializeArmComputeClTensorData(arm_compute::CLTensor& clTensor,
125  const ConstTensorHandle* handle)
126 {
127  ARMNN_ASSERT(handle);
128 
129  armcomputetensorutils::InitialiseArmComputeTensorEmpty(clTensor);
130  switch(handle->GetTensorInfo().GetDataType())
131  {
132  case DataType::Float16:
134  break;
135  case DataType::Float32:
136  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<float>());
137  break;
138  case DataType::QAsymmU8:
139  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<uint8_t>());
140  break;
141  case DataType::QAsymmS8:
142  case DataType::QSymmS8:
143  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int8_t>());
144  break;
145  case DataType::QSymmS16:
146  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int16_t>());
147  break;
148  case DataType::Signed32:
149  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int32_t>());
150  break;
151  case DataType::BFloat16:
153  break;
154  default:
155  // Throw exception; assertion not called in release build.
156  throw Exception("Unexpected tensor type during InitializeArmComputeClTensorData().");
157  }
158 };
159 
160 inline RuntimeException WrapClError(const cl::Error& clError, const CheckLocation& location)
161 {
162  std::stringstream message;
163  message << "CL error: " << clError.what() << ". Error code: " << clError.err();
164 
165  return RuntimeException(message.str(), location);
166 }
167 
168 inline void RunClFunction(arm_compute::IFunction& function, const CheckLocation& location)
169 {
170  try
171  {
172  function.run();
173  }
174  catch (cl::Error& error)
175  {
176  throw WrapClError(error, location);
177  }
178 }
179 
180 template <typename DataType, typename PayloadType>
181 DataType* GetOutputTensorData(unsigned int idx, const PayloadType& data)
182 {
183  ITensorHandle* tensorHandle = data.m_Outputs[idx];
184  return reinterpret_cast<DataType*>(tensorHandle->Map());
185 }
186 
187 } //namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
OpenClTimer.hpp
armnn::InitializeArmComputeClTensorData
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstTensorHandle *handle)
Definition: ClWorkloadUtils.hpp:124
armnn::SetClStridedSliceData
auto SetClStridedSliceData(const std::vector< int > &m_begin, const std::vector< int > &m_end, const std::vector< int > &m_stride)
Definition: ClWorkloadUtils.hpp:79
armnn::ConstTensorHandle
Definition: TensorHandle.hpp:24
ARMNN_SCOPED_PROFILING_EVENT_CL
#define ARMNN_SCOPED_PROFILING_EVENT_CL(name)
Definition: ClWorkloadUtils.hpp:21
armnn::GetOutputTensorData
DataType * GetOutputTensorData(unsigned int idx, const PayloadType &data)
Definition: ClWorkloadUtils.hpp:181
armnn::DataType::Float32
@ Float32
armnn::ITensorHandle
Definition: ITensorHandle.hpp:16
armnn::DataType::QAsymmU8
@ QAsymmU8
armnn::ConstTensorHandle::GetTensorInfo
const TensorInfo & GetTensorInfo() const
Definition: TensorHandle.hpp:40
armnn::DataType::QSymmS8
@ QSymmS8
armnn::Half
half_float::half Half
Definition: Half.hpp:22
armnn::CopyArmComputeClTensorData
void CopyArmComputeClTensorData(arm_compute::CLTensor &dstTensor, const T *srcData)
Definition: ClWorkloadUtils.hpp:64
armnn::BoostLogSeverityMapping::error
@ error
armnn::Coordinates
std::array< unsigned int, MaxNumOfTensorDimensions > Coordinates
Definition: InternalTypes.hpp:15
armnn::DataType::QSymmS16
@ QSymmS16
armnn::DataType::BFloat16
@ BFloat16
armnn::Exception::what
virtual const char * what() const noexcept override
Definition: Exceptions.cpp:32
armnn::ConstTensorHandle::GetConstTensor
const T * GetConstTensor() const
Definition: TensorHandle.hpp:28
armnn::DataType::Float16
@ Float16
armnn::CheckLocation
Definition: Exceptions.hpp:14
Utils.hpp
armnn::DataType
DataType
Definition: Types.hpp:48
armnn::GetConvolutionMethodString
std::string GetConvolutionMethodString(arm_compute::ConvolutionMethod &convolutionMethod)
Definition: ClWorkloadUtils.hpp:46
armnn::Exception
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
armnn::RuntimeException
Definition: Exceptions.hpp:120
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:200
armnn::SetClSliceData
auto SetClSliceData(const std::vector< unsigned int > &m_begin, const std::vector< unsigned int > &m_size)
Definition: ClWorkloadUtils.hpp:100
armnn::DataType::Signed32
@ Signed32
armnn::DataType::QAsymmS8
@ QAsymmS8
Half.hpp
TensorHandle.hpp
armnn::RunClFunction
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
Definition: ClWorkloadUtils.hpp:168
armnn::BFloat16
Definition: BFloat16.hpp:15
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
ArmComputeTensorUtils.hpp
BFloat16.hpp
armnn::WrapClError
RuntimeException WrapClError(const cl::Error &clError, const CheckLocation &location)
Definition: ClWorkloadUtils.hpp:160
armnn::ITensorHandle::Map
virtual const void * Map(bool blocking=true) const =0
Map the tensor data for access.