blob: 7e17e8b9fd5a823aa4f5be0299b9563e725520e4 [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5
telsoa014fcda012018-03-09 14:13:49 +00006#include "TensorCopyUtils.hpp"
7
arovir01616e7752018-10-01 17:08:59 +01008#include <armnnUtils/Half.hpp>
9
10
telsoa014fcda012018-03-09 14:13:49 +000011#ifdef ARMCOMPUTECL_ENABLED
David Beckac42efd2018-09-26 17:41:13 +010012#include <backends/cl/ClTensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +000013#endif
14
15#if ARMCOMPUTENEON_ENABLED
David Beck0dbe0ee2018-09-24 15:59:27 +010016#include <backends/neon/NeonTensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +000017#endif
18
19#if ARMCOMPUTECLENABLED || ARMCOMPUTENEON_ENABLED
David Beck711fa312018-09-24 10:46:38 +010020#include <backends/aclCommon/ArmComputeTensorUtils.hpp>
telsoa014fcda012018-03-09 14:13:49 +000021#endif
22
David Beckac42efd2018-09-26 17:41:13 +010023#include <backends/CpuTensorHandle.hpp>
telsoa014fcda012018-03-09 14:13:49 +000024
arovir01616e7752018-10-01 17:08:59 +010025#include <boost/cast.hpp>
26#include <algorithm>
27#include <cstring>
28
telsoa014fcda012018-03-09 14:13:49 +000029void CopyDataToITensorHandle(armnn::ITensorHandle* tensorHandle, const void* mem)
30{
31 switch (tensorHandle->GetType())
32 {
33 case armnn::ITensorHandle::Cpu:
34 {
35 auto handle = boost::polymorphic_downcast<armnn::ScopedCpuTensorHandle*>(tensorHandle);
36 memcpy(handle->GetTensor<void>(), mem, handle->GetTensorInfo().GetNumBytes());
37 break;
38 }
39#ifdef ARMCOMPUTECL_ENABLED
40 case armnn::ITensorHandle::CL:
41 {
42 using armnn::armcomputetensorutils::CopyArmComputeITensorData;
43 auto handle = boost::polymorphic_downcast<armnn::IClTensorHandle*>(tensorHandle);
44 handle->Map(true);
45 switch(handle->GetDataType())
46 {
47 case arm_compute::DataType::F32:
48 CopyArmComputeITensorData(static_cast<const float*>(mem), handle->GetTensor());
49 break;
50 case arm_compute::DataType::QASYMM8:
51 CopyArmComputeITensorData(static_cast<const uint8_t*>(mem), handle->GetTensor());
52 break;
telsoa01c577f2c2018-08-31 09:22:23 +010053 case arm_compute::DataType::F16:
54 CopyArmComputeITensorData(static_cast<const armnn::Half*>(mem), handle->GetTensor());
55 break;
telsoa014fcda012018-03-09 14:13:49 +000056 default:
57 {
58 throw armnn::UnimplementedException();
59 }
60 }
telsoa01c577f2c2018-08-31 09:22:23 +010061 handle->Unmap();
telsoa014fcda012018-03-09 14:13:49 +000062 break;
63 }
64#endif
65#if ARMCOMPUTENEON_ENABLED
66 case armnn::ITensorHandle::Neon:
67 {
68 using armnn::armcomputetensorutils::CopyArmComputeITensorData;
69 auto handle = boost::polymorphic_downcast<armnn::INeonTensorHandle*>(tensorHandle);
70 switch (handle->GetDataType())
71 {
72 case arm_compute::DataType::F32:
73 CopyArmComputeITensorData(static_cast<const float*>(mem), handle->GetTensor());
74 break;
75 case arm_compute::DataType::QASYMM8:
76 CopyArmComputeITensorData(static_cast<const uint8_t*>(mem), handle->GetTensor());
77 break;
78 default:
79 {
80 throw armnn::UnimplementedException();
81 }
82 }
83 break;
84 }
85#endif
86 default:
87 {
88 throw armnn::UnimplementedException();
89 }
90 }
91}
92
93void CopyDataFromITensorHandle(void* mem, const armnn::ITensorHandle* tensorHandle)
94{
95 switch (tensorHandle->GetType())
96 {
97 case armnn::ITensorHandle::Cpu:
98 {
99 auto handle = boost::polymorphic_downcast<const armnn::ScopedCpuTensorHandle*>(tensorHandle);
100 memcpy(mem, handle->GetTensor<void>(), handle->GetTensorInfo().GetNumBytes());
101 break;
102 }
103#ifdef ARMCOMPUTECL_ENABLED
104 case armnn::ITensorHandle::CL:
105 {
106 using armnn::armcomputetensorutils::CopyArmComputeITensorData;
107 auto handle = boost::polymorphic_downcast<const armnn::IClTensorHandle*>(tensorHandle);
108 const_cast<armnn::IClTensorHandle*>(handle)->Map(true);
109 switch(handle->GetDataType())
110 {
111 case arm_compute::DataType::F32:
112 CopyArmComputeITensorData(handle->GetTensor(), static_cast<float*>(mem));
113 break;
114 case arm_compute::DataType::QASYMM8:
115 CopyArmComputeITensorData(handle->GetTensor(), static_cast<uint8_t*>(mem));
116 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100117 case arm_compute::DataType::F16:
118 CopyArmComputeITensorData(handle->GetTensor(), static_cast<armnn::Half*>(mem));
119 break;
telsoa014fcda012018-03-09 14:13:49 +0000120 default:
121 {
122 throw armnn::UnimplementedException();
123 }
124 }
telsoa01c577f2c2018-08-31 09:22:23 +0100125 const_cast<armnn::IClTensorHandle*>(handle)->Unmap();
telsoa014fcda012018-03-09 14:13:49 +0000126 break;
127 }
128#endif
129#if ARMCOMPUTENEON_ENABLED
130 case armnn::ITensorHandle::Neon:
131 {
132 using armnn::armcomputetensorutils::CopyArmComputeITensorData;
133 auto handle = boost::polymorphic_downcast<const armnn::INeonTensorHandle*>(tensorHandle);
134 switch (handle->GetDataType())
135 {
136 case arm_compute::DataType::F32:
137 CopyArmComputeITensorData(handle->GetTensor(), static_cast<float*>(mem));
138 break;
139 case arm_compute::DataType::QASYMM8:
140 CopyArmComputeITensorData(handle->GetTensor(), static_cast<uint8_t*>(mem));
141 break;
142 default:
143 {
144 throw armnn::UnimplementedException();
145 }
146 }
147 break;
148 }
149#endif
150 default:
151 {
152 throw armnn::UnimplementedException();
153 }
154 }
155}
156
157void AllocateAndCopyDataToITensorHandle(armnn::ITensorHandle* tensorHandle, const void* mem)
158{
159 tensorHandle->Allocate();
160 CopyDataToITensorHandle(tensorHandle, mem);
161}