Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | 588cbdf | 2022-01-19 15:55:37 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 7 | #include <Half.hpp> |
Matthew Bentham | 14e4669 | 2018-09-20 15:35:30 +0100 | [diff] [blame] | 8 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
| 10 | #include <cl/OpenClTimer.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 11 | #include <armnn/backends/TensorHandle.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 13 | #include <armnn/Utils.hpp> |
| 14 | |
Matthew Bentham | 9b3e738 | 2020-02-05 21:39:55 +0000 | [diff] [blame] | 15 | #include <arm_compute/runtime/CL/CLTensor.h> |
| 16 | #include <arm_compute/runtime/IFunction.h> |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 17 | |
| 18 | #include <sstream> |
| 19 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | #define ARMNN_SCOPED_PROFILING_EVENT_CL(name) \ |
| 21 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(armnn::Compute::GpuAcc, \ |
Keith Davis | 5a64f22 | 2021-08-04 10:35:20 +0100 | [diff] [blame] | 22 | armnn::EmptyOptional(), \ |
| 23 | name, \ |
| 24 | armnn::OpenClTimer(), \ |
| 25 | armnn::WallClockTimer()) |
| 26 | |
| 27 | #define ARMNN_SCOPED_PROFILING_EVENT_CL_GUID(name, guid) \ |
| 28 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(armnn::Compute::GpuAcc, \ |
| 29 | guid, \ |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 30 | name, \ |
| 31 | armnn::OpenClTimer(), \ |
| 32 | armnn::WallClockTimer()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 33 | |
| 34 | namespace armnn |
| 35 | { |
| 36 | |
Keith Davis | 5a64f22 | 2021-08-04 10:35:20 +0100 | [diff] [blame] | 37 | inline std::string GetConvolutionMethodString(arm_compute::ConvolutionMethod& convolutionMethod) |
| 38 | { |
| 39 | switch (convolutionMethod) |
| 40 | { |
| 41 | case arm_compute::ConvolutionMethod::FFT: |
| 42 | return "FFT"; |
| 43 | case arm_compute::ConvolutionMethod::DIRECT: |
| 44 | return "Direct"; |
| 45 | case arm_compute::ConvolutionMethod::GEMM: |
| 46 | return "GEMM"; |
| 47 | case arm_compute::ConvolutionMethod::WINOGRAD: |
| 48 | return "Winograd"; |
| 49 | default: |
| 50 | return "Unknown"; |
| 51 | } |
| 52 | } |
| 53 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 54 | template <typename T> |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 55 | void CopyArmComputeClTensorData(arm_compute::CLTensor& dstTensor, const T* srcData) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | { |
| 57 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | ARMNN_SCOPED_PROFILING_EVENT_CL("MapClTensorForWriting"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 59 | dstTensor.map(true); |
| 60 | } |
| 61 | |
| 62 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | ARMNN_SCOPED_PROFILING_EVENT_CL("CopyToClTensor"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 64 | armcomputetensorutils::CopyArmComputeITensorData<T>(srcData, dstTensor); |
| 65 | } |
| 66 | |
| 67 | dstTensor.unmap(); |
| 68 | } |
| 69 | |
keidav01 | d74dc91 | 2018-12-10 18:16:07 +0000 | [diff] [blame] | 70 | inline auto SetClStridedSliceData(const std::vector<int>& m_begin, |
| 71 | const std::vector<int>& m_end, |
| 72 | const std::vector<int>& m_stride) |
| 73 | { |
| 74 | arm_compute::Coordinates starts; |
| 75 | arm_compute::Coordinates ends; |
| 76 | arm_compute::Coordinates strides; |
| 77 | |
| 78 | unsigned int num_dims = static_cast<unsigned int>(m_begin.size()); |
| 79 | |
| 80 | for (unsigned int i = 0; i < num_dims; i++) { |
| 81 | unsigned int revertedIndex = num_dims - i - 1; |
| 82 | |
| 83 | starts.set(i, static_cast<int>(m_begin[revertedIndex])); |
| 84 | ends.set(i, static_cast<int>(m_end[revertedIndex])); |
| 85 | strides.set(i, static_cast<int>(m_stride[revertedIndex])); |
| 86 | } |
| 87 | |
| 88 | return std::make_tuple(starts, ends, strides); |
| 89 | } |
| 90 | |
Aron Virginas-Tar | 94c4fef | 2019-11-25 15:37:08 +0000 | [diff] [blame] | 91 | inline auto SetClSliceData(const std::vector<unsigned int>& m_begin, |
| 92 | const std::vector<unsigned int>& m_size) |
| 93 | { |
| 94 | // This function must translate the size vector given to an end vector |
| 95 | // expected by the ACL NESlice workload |
| 96 | arm_compute::Coordinates starts; |
| 97 | arm_compute::Coordinates ends; |
| 98 | |
| 99 | unsigned int num_dims = static_cast<unsigned int>(m_begin.size()); |
| 100 | |
| 101 | // For strided slices, we have the relationship size = (end - begin) / stride |
| 102 | // For slice, we assume stride to be a vector of all ones, yielding the formula |
| 103 | // size = (end - begin) therefore we know end = size + begin |
| 104 | for (unsigned int i = 0; i < num_dims; i++) |
| 105 | { |
| 106 | unsigned int revertedIndex = num_dims - i - 1; |
| 107 | |
| 108 | starts.set(i, static_cast<int>(m_begin[revertedIndex])); |
| 109 | ends.set(i, static_cast<int>(m_begin[revertedIndex] + m_size[revertedIndex])); |
| 110 | } |
| 111 | |
| 112 | return std::make_tuple(starts, ends); |
| 113 | } |
| 114 | |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 115 | inline void InitializeArmComputeClTensorData(arm_compute::CLTensor& clTensor, |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 116 | const ConstTensorHandle* handle) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 117 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 118 | ARMNN_ASSERT(handle); |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 119 | |
| 120 | armcomputetensorutils::InitialiseArmComputeTensorEmpty(clTensor); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 121 | switch(handle->GetTensorInfo().GetDataType()) |
| 122 | { |
| 123 | case DataType::Float16: |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 124 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<armnn::Half>()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 125 | break; |
| 126 | case DataType::Float32: |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 127 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<float>()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 128 | break; |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 129 | case DataType::QAsymmU8: |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 130 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<uint8_t>()); |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 131 | break; |
Narumol Prangnawarat | 33d2c78 | 2020-11-13 18:00:23 +0000 | [diff] [blame] | 132 | case DataType::QAsymmS8: |
Derek Lamberti | d466a54 | 2020-01-22 15:37:29 +0000 | [diff] [blame] | 133 | case DataType::QSymmS8: |
Keith Davis | 899f64f | 2019-11-26 16:01:18 +0000 | [diff] [blame] | 134 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int8_t>()); |
| 135 | break; |
Ryan OShea | 2323af4 | 2020-05-13 16:36:19 +0100 | [diff] [blame] | 136 | case DataType::QSymmS16: |
| 137 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int16_t>()); |
| 138 | break; |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 139 | case DataType::Signed32: |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 140 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int32_t>()); |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 141 | break; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | default: |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 143 | ARMNN_ASSERT_MSG(false, "Unexpected tensor type."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 144 | } |
| 145 | }; |
| 146 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 147 | inline RuntimeException WrapClError(const cl::Error& clError, const CheckLocation& location) |
| 148 | { |
| 149 | std::stringstream message; |
| 150 | message << "CL error: " << clError.what() << ". Error code: " << clError.err(); |
| 151 | |
| 152 | return RuntimeException(message.str(), location); |
| 153 | } |
| 154 | |
| 155 | inline void RunClFunction(arm_compute::IFunction& function, const CheckLocation& location) |
| 156 | { |
| 157 | try |
| 158 | { |
| 159 | function.run(); |
| 160 | } |
| 161 | catch (cl::Error& error) |
| 162 | { |
| 163 | throw WrapClError(error, location); |
| 164 | } |
| 165 | } |
| 166 | |
David Monahan | c11ba46 | 2020-12-03 11:09:46 +0000 | [diff] [blame] | 167 | template <typename DataType, typename PayloadType> |
| 168 | DataType* GetOutputTensorData(unsigned int idx, const PayloadType& data) |
| 169 | { |
| 170 | ITensorHandle* tensorHandle = data.m_Outputs[idx]; |
| 171 | return reinterpret_cast<DataType*>(tensorHandle->Map()); |
| 172 | } |
| 173 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 174 | } //namespace armnn |