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