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 <backendsCommon/OutputHandler.hpp> |
| 8 | #include <aclCommon/ArmComputeTensorUtils.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | #include <arm_compute/runtime/MemoryGroup.h> |
| 11 | #include <arm_compute/runtime/IMemoryGroup.h> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 12 | #include <arm_compute/runtime/Tensor.h> |
| 13 | #include <arm_compute/runtime/SubTensor.h> |
| 14 | #include <arm_compute/core/TensorShape.h> |
| 15 | #include <arm_compute/core/Coordinates.h> |
| 16 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 17 | #include <boost/polymorphic_pointer_cast.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | |
| 19 | namespace armnn |
| 20 | { |
| 21 | |
| 22 | class INeonTensorHandle : public ITensorHandle |
| 23 | { |
| 24 | public: |
| 25 | virtual arm_compute::ITensor& GetTensor() = 0; |
| 26 | virtual arm_compute::ITensor const& GetTensor() const = 0; |
| 27 | virtual arm_compute::DataType GetDataType() const = 0; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 28 | virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>& memoryGroup) = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | class NeonTensorHandle : public INeonTensorHandle |
| 32 | { |
| 33 | public: |
| 34 | NeonTensorHandle(const TensorInfo& tensorInfo) |
| 35 | { |
| 36 | armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo); |
| 37 | } |
| 38 | |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 39 | NeonTensorHandle(const TensorInfo& tensorInfo, DataLayout dataLayout) |
| 40 | { |
| 41 | armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo, dataLayout); |
| 42 | } |
| 43 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 44 | arm_compute::ITensor& GetTensor() override { return m_Tensor; } |
| 45 | arm_compute::ITensor const& GetTensor() const override { return m_Tensor; } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 46 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | virtual void Allocate() override |
| 48 | { |
| 49 | armnn::armcomputetensorutils::InitialiseArmComputeTensorEmpty(m_Tensor); |
| 50 | }; |
| 51 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 52 | virtual void Manage() override |
| 53 | { |
| 54 | BOOST_ASSERT(m_MemoryGroup != nullptr); |
| 55 | m_MemoryGroup->manage(&m_Tensor); |
| 56 | } |
| 57 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | virtual ITensorHandle* GetParent() const override { return nullptr; } |
| 59 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 60 | virtual arm_compute::DataType GetDataType() const override |
| 61 | { |
| 62 | return m_Tensor.info()->data_type(); |
| 63 | } |
| 64 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>& memoryGroup) override |
| 66 | { |
| 67 | m_MemoryGroup = boost::polymorphic_pointer_downcast<arm_compute::MemoryGroup>(memoryGroup); |
| 68 | } |
| 69 | |
| 70 | virtual const void* Map(bool /* blocking = true */) const override |
| 71 | { |
| 72 | return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes()); |
| 73 | } |
| 74 | virtual void Unmap() const override {} |
| 75 | |
| 76 | |
| 77 | TensorShape GetStrides() const override |
| 78 | { |
| 79 | return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes()); |
| 80 | } |
| 81 | |
| 82 | TensorShape GetShape() const override |
| 83 | { |
| 84 | return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape()); |
| 85 | } |
| 86 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 87 | private: |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 88 | // Only used for testing |
| 89 | void CopyOutTo(void* memory) const override |
| 90 | { |
| 91 | switch (this->GetDataType()) |
| 92 | { |
| 93 | case arm_compute::DataType::F32: |
| 94 | armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(), |
| 95 | static_cast<float*>(memory)); |
| 96 | break; |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 97 | case arm_compute::DataType::U8: |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 98 | case arm_compute::DataType::QASYMM8: |
| 99 | armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(), |
| 100 | static_cast<uint8_t*>(memory)); |
| 101 | break; |
| 102 | default: |
| 103 | { |
| 104 | throw armnn::UnimplementedException(); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Only used for testing |
| 110 | void CopyInFrom(const void* memory) override |
| 111 | { |
| 112 | switch (this->GetDataType()) |
| 113 | { |
| 114 | case arm_compute::DataType::F32: |
| 115 | armcomputetensorutils::CopyArmComputeITensorData(static_cast<const float*>(memory), |
| 116 | this->GetTensor()); |
| 117 | break; |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 118 | case arm_compute::DataType::U8: |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 119 | case arm_compute::DataType::QASYMM8: |
| 120 | armcomputetensorutils::CopyArmComputeITensorData(static_cast<const uint8_t*>(memory), |
| 121 | this->GetTensor()); |
| 122 | break; |
| 123 | default: |
| 124 | { |
| 125 | throw armnn::UnimplementedException(); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 130 | arm_compute::Tensor m_Tensor; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 131 | std::shared_ptr<arm_compute::MemoryGroup> m_MemoryGroup; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | class NeonSubTensorHandle : public INeonTensorHandle |
| 135 | { |
| 136 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 137 | NeonSubTensorHandle(INeonTensorHandle* parent, |
| 138 | const arm_compute::TensorShape& shape, |
| 139 | const arm_compute::Coordinates& coords) |
| 140 | : m_Tensor(&parent->GetTensor(), shape, coords) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 141 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | parentHandle = parent; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | arm_compute::ITensor& GetTensor() override { return m_Tensor; } |
| 146 | arm_compute::ITensor const& GetTensor() const override { return m_Tensor; } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 147 | |
| 148 | virtual void Allocate() override {} |
| 149 | virtual void Manage() override {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 150 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 151 | virtual ITensorHandle* GetParent() const override { return parentHandle; } |
| 152 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 153 | virtual arm_compute::DataType GetDataType() const override |
| 154 | { |
| 155 | return m_Tensor.info()->data_type(); |
| 156 | } |
| 157 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 158 | virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>&) override {} |
| 159 | |
| 160 | virtual const void* Map(bool /* blocking = true */) const override |
| 161 | { |
| 162 | return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes()); |
| 163 | } |
| 164 | virtual void Unmap() const override {} |
| 165 | |
| 166 | TensorShape GetStrides() const override |
| 167 | { |
| 168 | return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes()); |
| 169 | } |
| 170 | |
| 171 | TensorShape GetShape() const override |
| 172 | { |
| 173 | return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape()); |
| 174 | } |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 175 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 176 | private: |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 177 | // Only used for testing |
| 178 | void CopyOutTo(void* memory) const override |
| 179 | { |
| 180 | switch (this->GetDataType()) |
| 181 | { |
| 182 | case arm_compute::DataType::F32: |
| 183 | armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(), |
| 184 | static_cast<float*>(memory)); |
| 185 | break; |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 186 | case arm_compute::DataType::U8: |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 187 | case arm_compute::DataType::QASYMM8: |
| 188 | armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(), |
| 189 | static_cast<uint8_t*>(memory)); |
| 190 | break; |
| 191 | default: |
| 192 | { |
| 193 | throw armnn::UnimplementedException(); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Only used for testing |
| 199 | void CopyInFrom(const void* memory) override |
| 200 | { |
| 201 | switch (this->GetDataType()) |
| 202 | { |
| 203 | case arm_compute::DataType::F32: |
| 204 | armcomputetensorutils::CopyArmComputeITensorData(static_cast<const float*>(memory), |
| 205 | this->GetTensor()); |
| 206 | break; |
kevmay01 | 2b4d88e | 2019-01-24 14:05:09 +0000 | [diff] [blame] | 207 | case arm_compute::DataType::U8: |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 208 | case arm_compute::DataType::QASYMM8: |
| 209 | armcomputetensorutils::CopyArmComputeITensorData(static_cast<const uint8_t*>(memory), |
| 210 | this->GetTensor()); |
| 211 | break; |
| 212 | default: |
| 213 | { |
| 214 | throw armnn::UnimplementedException(); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 219 | arm_compute::SubTensor m_Tensor; |
| 220 | ITensorHandle* parentHandle = nullptr; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 223 | } // namespace armnn |