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; |
| 97 | case arm_compute::DataType::QASYMM8: |
| 98 | armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(), |
| 99 | static_cast<uint8_t*>(memory)); |
| 100 | break; |
| 101 | default: |
| 102 | { |
| 103 | throw armnn::UnimplementedException(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Only used for testing |
| 109 | void CopyInFrom(const void* memory) override |
| 110 | { |
| 111 | switch (this->GetDataType()) |
| 112 | { |
| 113 | case arm_compute::DataType::F32: |
| 114 | armcomputetensorutils::CopyArmComputeITensorData(static_cast<const float*>(memory), |
| 115 | this->GetTensor()); |
| 116 | break; |
| 117 | case arm_compute::DataType::QASYMM8: |
| 118 | armcomputetensorutils::CopyArmComputeITensorData(static_cast<const uint8_t*>(memory), |
| 119 | this->GetTensor()); |
| 120 | break; |
| 121 | default: |
| 122 | { |
| 123 | throw armnn::UnimplementedException(); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 128 | arm_compute::Tensor m_Tensor; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 129 | std::shared_ptr<arm_compute::MemoryGroup> m_MemoryGroup; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | class NeonSubTensorHandle : public INeonTensorHandle |
| 133 | { |
| 134 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | NeonSubTensorHandle(INeonTensorHandle* parent, |
| 136 | const arm_compute::TensorShape& shape, |
| 137 | const arm_compute::Coordinates& coords) |
| 138 | : m_Tensor(&parent->GetTensor(), shape, coords) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 139 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 140 | parentHandle = parent; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | arm_compute::ITensor& GetTensor() override { return m_Tensor; } |
| 144 | arm_compute::ITensor const& GetTensor() const override { return m_Tensor; } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 145 | |
| 146 | virtual void Allocate() override {} |
| 147 | virtual void Manage() override {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 148 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 149 | virtual ITensorHandle* GetParent() const override { return parentHandle; } |
| 150 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 151 | virtual arm_compute::DataType GetDataType() const override |
| 152 | { |
| 153 | return m_Tensor.info()->data_type(); |
| 154 | } |
| 155 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 156 | virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>&) override {} |
| 157 | |
| 158 | virtual const void* Map(bool /* blocking = true */) const override |
| 159 | { |
| 160 | return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes()); |
| 161 | } |
| 162 | virtual void Unmap() const override {} |
| 163 | |
| 164 | TensorShape GetStrides() const override |
| 165 | { |
| 166 | return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes()); |
| 167 | } |
| 168 | |
| 169 | TensorShape GetShape() const override |
| 170 | { |
| 171 | return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape()); |
| 172 | } |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 173 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 174 | private: |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 175 | // Only used for testing |
| 176 | void CopyOutTo(void* memory) const override |
| 177 | { |
| 178 | switch (this->GetDataType()) |
| 179 | { |
| 180 | case arm_compute::DataType::F32: |
| 181 | armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(), |
| 182 | static_cast<float*>(memory)); |
| 183 | break; |
| 184 | case arm_compute::DataType::QASYMM8: |
| 185 | armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(), |
| 186 | static_cast<uint8_t*>(memory)); |
| 187 | break; |
| 188 | default: |
| 189 | { |
| 190 | throw armnn::UnimplementedException(); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Only used for testing |
| 196 | void CopyInFrom(const void* memory) override |
| 197 | { |
| 198 | switch (this->GetDataType()) |
| 199 | { |
| 200 | case arm_compute::DataType::F32: |
| 201 | armcomputetensorutils::CopyArmComputeITensorData(static_cast<const float*>(memory), |
| 202 | this->GetTensor()); |
| 203 | break; |
| 204 | case arm_compute::DataType::QASYMM8: |
| 205 | armcomputetensorutils::CopyArmComputeITensorData(static_cast<const uint8_t*>(memory), |
| 206 | this->GetTensor()); |
| 207 | break; |
| 208 | default: |
| 209 | { |
| 210 | throw armnn::UnimplementedException(); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 215 | arm_compute::SubTensor m_Tensor; |
| 216 | ITensorHandle* parentHandle = nullptr; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 219 | } // namespace armnn |