ArmNN
 24.08
ClWorkloadUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2024 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_THROW_INVALIDARG_MSG_IF_FALSE(handle, "Null tensor handle passed to InitializeArmComputeTensorData.");
128  armcomputetensorutils::InitialiseArmComputeTensorEmpty(clTensor);
129  switch(handle->GetTensorInfo().GetDataType())
130  {
131  case DataType::Float16:
133  break;
134  case DataType::Float32:
135  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<float>());
136  break;
137  case DataType::QAsymmU8:
138  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<uint8_t>());
139  break;
140  case DataType::QAsymmS8:
141  case DataType::QSymmS8:
142  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int8_t>());
143  break;
144  case DataType::QSymmS16:
145  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int16_t>());
146  break;
147  case DataType::Signed32:
148  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int32_t>());
149  break;
150  case DataType::BFloat16:
152  break;
153  default:
154  // Throw exception; assertion not called in release build.
155  throw Exception("Unexpected tensor type during InitializeArmComputeClTensorData().");
156  }
157 };
158 
159 inline RuntimeException WrapClError(const cl::Error& clError, const CheckLocation& location)
160 {
161  std::stringstream message;
162  message << "CL error: " << clError.what() << ". Error code: " << clError.err();
163 
164  return RuntimeException(message.str(), location);
165 }
166 
167 inline void RunClFunction(arm_compute::IFunction& function, const CheckLocation& location)
168 {
169  try
170  {
171  function.run();
172  }
173  catch (cl::Error& error)
174  {
175  throw WrapClError(error, location);
176  }
177 }
178 
179 template <typename DataType, typename PayloadType>
180 DataType* GetOutputTensorData(unsigned int idx, const PayloadType& data)
181 {
182  ITensorHandle* tensorHandle = data.m_Outputs[idx];
183  return reinterpret_cast<DataType*>(tensorHandle->Map());
184 }
185 
186 } //namespace armnn
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:180
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:167
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:159
armnn::ITensorHandle::Map
virtual const void * Map(bool blocking=true) const =0
Map the tensor data for access.
ARMNN_THROW_INVALIDARG_MSG_IF_FALSE
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
Definition: Exceptions.hpp:210