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 | |
David Beck | 0dbe0ee | 2018-09-24 15:59:27 +0100 | [diff] [blame^] | 7 | #include <backends/OutputHandler.hpp> |
David Beck | 711fa31 | 2018-09-24 10:46:38 +0100 | [diff] [blame] | 8 | #include <backends/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 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 58 | virtual ITensorHandle::Type GetType() const override { return ITensorHandle::Neon; } |
| 59 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | virtual ITensorHandle* GetParent() const override { return nullptr; } |
| 61 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | virtual arm_compute::DataType GetDataType() const override |
| 63 | { |
| 64 | return m_Tensor.info()->data_type(); |
| 65 | } |
| 66 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 67 | virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>& memoryGroup) override |
| 68 | { |
| 69 | m_MemoryGroup = boost::polymorphic_pointer_downcast<arm_compute::MemoryGroup>(memoryGroup); |
| 70 | } |
| 71 | |
| 72 | virtual const void* Map(bool /* blocking = true */) const override |
| 73 | { |
| 74 | return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes()); |
| 75 | } |
| 76 | virtual void Unmap() const override {} |
| 77 | |
| 78 | |
| 79 | TensorShape GetStrides() const override |
| 80 | { |
| 81 | return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes()); |
| 82 | } |
| 83 | |
| 84 | TensorShape GetShape() const override |
| 85 | { |
| 86 | return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape()); |
| 87 | } |
| 88 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 89 | private: |
| 90 | arm_compute::Tensor m_Tensor; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 91 | std::shared_ptr<arm_compute::MemoryGroup> m_MemoryGroup; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | class NeonSubTensorHandle : public INeonTensorHandle |
| 95 | { |
| 96 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 97 | NeonSubTensorHandle(INeonTensorHandle* parent, |
| 98 | const arm_compute::TensorShape& shape, |
| 99 | const arm_compute::Coordinates& coords) |
| 100 | : m_Tensor(&parent->GetTensor(), shape, coords) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 101 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 102 | parentHandle = parent; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | arm_compute::ITensor& GetTensor() override { return m_Tensor; } |
| 106 | arm_compute::ITensor const& GetTensor() const override { return m_Tensor; } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 107 | |
| 108 | virtual void Allocate() override {} |
| 109 | virtual void Manage() override {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 110 | |
| 111 | virtual ITensorHandle::Type GetType() const override { return ITensorHandle::Neon; } |
| 112 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 113 | virtual ITensorHandle* GetParent() const override { return parentHandle; } |
| 114 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 115 | virtual arm_compute::DataType GetDataType() const override |
| 116 | { |
| 117 | return m_Tensor.info()->data_type(); |
| 118 | } |
| 119 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 120 | virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>&) override {} |
| 121 | |
| 122 | virtual const void* Map(bool /* blocking = true */) const override |
| 123 | { |
| 124 | return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes()); |
| 125 | } |
| 126 | virtual void Unmap() const override {} |
| 127 | |
| 128 | TensorShape GetStrides() const override |
| 129 | { |
| 130 | return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes()); |
| 131 | } |
| 132 | |
| 133 | TensorShape GetShape() const override |
| 134 | { |
| 135 | return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape()); |
| 136 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 137 | private: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 138 | arm_compute::SubTensor m_Tensor; |
| 139 | ITensorHandle* parentHandle = nullptr; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | } |