telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 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> |
| 11 | #include <backendsCommon/CpuTensorHandle.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 13 | #include <arm_compute/runtime/CL/CLFunctions.h> |
| 14 | |
| 15 | #include <sstream> |
| 16 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 17 | #define ARMNN_SCOPED_PROFILING_EVENT_CL(name) \ |
| 18 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(armnn::Compute::GpuAcc, \ |
| 19 | name, \ |
| 20 | armnn::OpenClTimer(), \ |
| 21 | armnn::WallClockTimer()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 22 | |
| 23 | namespace armnn |
| 24 | { |
| 25 | |
| 26 | template <typename T> |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 27 | void CopyArmComputeClTensorData(arm_compute::CLTensor& dstTensor, const T* srcData) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 28 | { |
| 29 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 30 | ARMNN_SCOPED_PROFILING_EVENT_CL("MapClTensorForWriting"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 31 | dstTensor.map(true); |
| 32 | } |
| 33 | |
| 34 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 35 | ARMNN_SCOPED_PROFILING_EVENT_CL("CopyToClTensor"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 36 | armcomputetensorutils::CopyArmComputeITensorData<T>(srcData, dstTensor); |
| 37 | } |
| 38 | |
| 39 | dstTensor.unmap(); |
| 40 | } |
| 41 | |
keidav01 | d74dc91 | 2018-12-10 18:16:07 +0000 | [diff] [blame] | 42 | inline auto SetClStridedSliceData(const std::vector<int>& m_begin, |
| 43 | const std::vector<int>& m_end, |
| 44 | const std::vector<int>& m_stride) |
| 45 | { |
| 46 | arm_compute::Coordinates starts; |
| 47 | arm_compute::Coordinates ends; |
| 48 | arm_compute::Coordinates strides; |
| 49 | |
| 50 | unsigned int num_dims = static_cast<unsigned int>(m_begin.size()); |
| 51 | |
| 52 | for (unsigned int i = 0; i < num_dims; i++) { |
| 53 | unsigned int revertedIndex = num_dims - i - 1; |
| 54 | |
| 55 | starts.set(i, static_cast<int>(m_begin[revertedIndex])); |
| 56 | ends.set(i, static_cast<int>(m_end[revertedIndex])); |
| 57 | strides.set(i, static_cast<int>(m_stride[revertedIndex])); |
| 58 | } |
| 59 | |
| 60 | return std::make_tuple(starts, ends, strides); |
| 61 | } |
| 62 | |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 63 | inline void InitializeArmComputeClTensorData(arm_compute::CLTensor& clTensor, |
| 64 | const ConstCpuTensorHandle* handle) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | { |
| 66 | BOOST_ASSERT(handle); |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 67 | |
| 68 | armcomputetensorutils::InitialiseArmComputeTensorEmpty(clTensor); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 69 | switch(handle->GetTensorInfo().GetDataType()) |
| 70 | { |
| 71 | case DataType::Float16: |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 72 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<armnn::Half>()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 73 | break; |
| 74 | case DataType::Float32: |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 75 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<float>()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 76 | break; |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 77 | case DataType::QuantisedAsymm8: |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 78 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<uint8_t>()); |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 79 | break; |
| 80 | case DataType::Signed32: |
Matthew Bentham | ca6616c | 2018-09-21 15:16:53 +0100 | [diff] [blame] | 81 | CopyArmComputeClTensorData(clTensor, handle->GetConstTensor<int32_t>()); |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 82 | break; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 83 | default: |
Matthew Bentham | 785df50 | 2018-09-21 10:29:58 +0100 | [diff] [blame] | 84 | BOOST_ASSERT_MSG(false, "Unexpected tensor type."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 85 | } |
| 86 | }; |
| 87 | |
Aron Virginas-Tar | a8e06ed | 2018-10-19 16:46:15 +0100 | [diff] [blame] | 88 | inline RuntimeException WrapClError(const cl::Error& clError, const CheckLocation& location) |
| 89 | { |
| 90 | std::stringstream message; |
| 91 | message << "CL error: " << clError.what() << ". Error code: " << clError.err(); |
| 92 | |
| 93 | return RuntimeException(message.str(), location); |
| 94 | } |
| 95 | |
| 96 | inline void RunClFunction(arm_compute::IFunction& function, const CheckLocation& location) |
| 97 | { |
| 98 | try |
| 99 | { |
| 100 | function.run(); |
| 101 | } |
| 102 | catch (cl::Error& error) |
| 103 | { |
| 104 | throw WrapClError(error, location); |
| 105 | } |
| 106 | } |
| 107 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 108 | } //namespace armnn |