ArmNN
 24.02
GpuFsaWorkloadUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2023-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 
12 
13 #include <armnn/Utils.hpp>
14 
15 #include <arm_compute/runtime/CL/CLTensor.h>
16 #include <arm_compute/runtime/IFunction.h>
17 
18 #include <sstream>
19 
20 
21 namespace armnn
22 {
23 
24  inline std::string GetConvolutionMethodString(arm_compute::ConvolutionMethod& convolutionMethod)
25  {
26  switch (convolutionMethod)
27  {
28  case arm_compute::ConvolutionMethod::FFT:
29  return "FFT";
30  case arm_compute::ConvolutionMethod::DIRECT:
31  return "Direct";
32  case arm_compute::ConvolutionMethod::GEMM:
33  return "GEMM";
34  case arm_compute::ConvolutionMethod::WINOGRAD:
35  return "Winograd";
36  default:
37  return "Unknown";
38  }
39  }
40 
41  template <typename T>
42  void CopyArmComputeClTensorData(arm_compute::CLTensor& dstTensor, const T* srcData)
43  {
44  {
45  dstTensor.map(true);
46  }
47 
48  {
49  armcomputetensorutils::CopyArmComputeITensorData<T>(srcData, dstTensor);
50  }
51 
52  dstTensor.unmap();
53  }
54 
55  inline auto SetClStridedSliceData(const std::vector<int>& m_begin,
56  const std::vector<int>& m_end,
57  const std::vector<int>& m_stride)
58  {
62 
63  unsigned int num_dims = static_cast<unsigned int>(m_begin.size());
64 
65  for (unsigned int i = 0; i < num_dims; i++) {
66  unsigned int revertedIndex = num_dims - i - 1;
67 
68  starts.set(i, static_cast<int>(m_begin[revertedIndex]));
69  ends.set(i, static_cast<int>(m_end[revertedIndex]));
70  strides.set(i, static_cast<int>(m_stride[revertedIndex]));
71  }
72 
73  return std::make_tuple(starts, ends, strides);
74  }
75 
76  inline auto SetClSliceData(const std::vector<unsigned int>& m_begin,
77  const std::vector<unsigned int>& m_size)
78  {
79  // This function must translate the size vector given to an end vector
80  // expected by the ACL NESlice workload
83 
84  unsigned int num_dims = static_cast<unsigned int>(m_begin.size());
85 
86  // For strided slices, we have the relationship size = (end - begin) / stride
87  // For slice, we assume stride to be a vector of all ones, yielding the formula
88  // size = (end - begin) therefore we know end = size + begin
89  for (unsigned int i = 0; i < num_dims; i++)
90  {
91  unsigned int revertedIndex = num_dims - i - 1;
92 
93  starts.set(i, static_cast<int>(m_begin[revertedIndex]));
94  ends.set(i, static_cast<int>(m_begin[revertedIndex] + m_size[revertedIndex]));
95  }
96 
97  return std::make_tuple(starts, ends);
98  }
99 
100  inline void InitializeArmComputeClTensorData(arm_compute::CLTensor& clTensor,
101  const ConstTensorHandle* handle)
102  {
103  ARMNN_ASSERT(handle);
104 
105  armcomputetensorutils::InitialiseArmComputeTensorEmpty(clTensor);
106  switch(handle->GetTensorInfo().GetDataType())
107  {
108  case DataType::Float16:
109  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<armnn::Half>());
110  break;
111  case DataType::Float32:
112  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<float>());
113  break;
114  case DataType::QAsymmU8:
115  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<uint8_t>());
116  break;
117  case DataType::QAsymmS8:
118  case DataType::QSymmS8:
119  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int8_t>());
120  break;
121  case DataType::QSymmS16:
122  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int16_t>());
123  break;
124  case DataType::Signed32:
125  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int32_t>());
126  break;
127  case DataType::BFloat16:
128  CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<armnn::BFloat16>());
129  break;
130  default:
131  // Throw exception; assertion not called in release build.
132  throw Exception("Unexpected tensor type during InitializeArmComputeClTensorData().");
133  }
134  };
135 
136  inline RuntimeException WrapClError(const cl::Error& clError, const CheckLocation& location)
137  {
138  std::stringstream message;
139  message << "CL error: " << clError.what() << ". Error code: " << clError.err();
140 
141  return RuntimeException(message.str(), location);
142  }
143 
144  inline void RunClFunction(arm_compute::IFunction& function, const CheckLocation& location)
145  {
146  try
147  {
148  function.run();
149  }
150  catch (cl::Error& error)
151  {
152  throw WrapClError(error, location);
153  }
154  }
155 
156  template <typename DataType, typename PayloadType>
157  DataType* GetOutputTensorData(unsigned int idx, const PayloadType& data)
158  {
159  ITensorHandle* tensorHandle = data.m_Outputs[idx];
160  return reinterpret_cast<DataType*>(tensorHandle->Map());
161  }
162 
163 } //namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
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::GetOutputTensorData
DataType * GetOutputTensorData(unsigned int idx, const PayloadType &data)
Definition: ClWorkloadUtils.hpp:181
armnn::DataType::Float32
@ Float32
armnn::DataType::QAsymmU8
@ QAsymmU8
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::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::DataType::Float16
@ Float16
Utils.hpp
armnn::DataType
DataType
Definition: Types.hpp:48
armnn::GetConvolutionMethodString
std::string GetConvolutionMethodString(arm_compute::ConvolutionMethod &convolutionMethod)
Definition: ClWorkloadUtils.hpp:46
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