ArmNN
 25.11
Loading...
Searching...
No Matches
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
43namespace armnn
44{
45
46inline 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
63template <typename T>
64void 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
79inline auto SetClStridedSliceData(const std::vector<int>& m_begin,
80 const std::vector<int>& m_end,
81 const std::vector<int>& m_stride)
82{
83 arm_compute::Coordinates starts;
84 arm_compute::Coordinates ends;
85 arm_compute::Coordinates strides;
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
100inline 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
105 arm_compute::Coordinates starts;
106 arm_compute::Coordinates ends;
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
124inline 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 {
133 break;
135 CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<float>());
136 break;
138 CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<uint8_t>());
139 break;
142 CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int8_t>());
143 break;
145 CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int16_t>());
146 break;
148 CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int32_t>());
149 break;
152 break;
153 default:
154 // Throw exception; assertion not called in release build.
155 throw Exception("Unexpected tensor type during InitializeArmComputeClTensorData().");
156 }
157};
158
159inline 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
167inline 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
179template <typename DataType, typename PayloadType>
180DataType* 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
#define ARMNN_SCOPED_PROFILING_EVENT_CL(name)
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
const TensorInfo & GetTensorInfo() const
const T * GetConstTensor() const
Base class for all ArmNN exceptions so that users can filter to just those.
virtual const char * what() const noexcept override
virtual const void * Map(bool blocking=true) const =0
Map the tensor data for access.
DataType GetDataType() const
Definition Tensor.hpp:200
Copyright (c) 2021 ARM Limited and Contributors.
half_float::half Half
Definition Half.hpp:22
auto SetClStridedSliceData(const std::vector< int > &m_begin, const std::vector< int > &m_end, const std::vector< int > &m_stride)
void RunClFunction(arm_compute::IFunction &function, const CheckLocation &location)
RuntimeException WrapClError(const cl::Error &clError, const CheckLocation &location)
std::string GetConvolutionMethodString(arm_compute::ConvolutionMethod &convolutionMethod)
void CopyArmComputeClTensorData(arm_compute::CLTensor &dstTensor, const T *srcData)
void InitializeArmComputeClTensorData(arm_compute::CLTensor &clTensor, const ConstTensorHandle *handle)
DataType
Definition Types.hpp:49
auto SetClSliceData(const std::vector< unsigned int > &m_begin, const std::vector< unsigned int > &m_size)
DataType * GetOutputTensorData(unsigned int idx, const PayloadType &data)