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 | |
| 10 | #include <arm_compute/runtime/CL/CLTensor.h> |
| 11 | #include <arm_compute/runtime/CL/CLSubTensor.h> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | #include <arm_compute/runtime/CL/CLMemoryGroup.h> |
| 13 | #include <arm_compute/runtime/IMemoryGroup.h> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | |
| 23 | class IClTensorHandle : public ITensorHandle |
| 24 | { |
| 25 | public: |
| 26 | virtual arm_compute::ICLTensor& GetTensor() = 0; |
| 27 | virtual arm_compute::ICLTensor const& GetTensor() const = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 28 | virtual arm_compute::DataType GetDataType() const = 0; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>& memoryGroup) = 0; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | class ClTensorHandle : public IClTensorHandle |
| 33 | { |
| 34 | public: |
| 35 | ClTensorHandle(const TensorInfo& tensorInfo) |
| 36 | { |
| 37 | armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo); |
| 38 | } |
| 39 | |
Francis Murtagh | 351d13d | 2018-09-24 15:01:18 +0100 | [diff] [blame] | 40 | ClTensorHandle(const TensorInfo& tensorInfo, DataLayout dataLayout) |
| 41 | { |
| 42 | armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo, dataLayout); |
| 43 | } |
| 44 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 45 | arm_compute::CLTensor& GetTensor() override { return m_Tensor; } |
| 46 | arm_compute::CLTensor const& GetTensor() const override { return m_Tensor; } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 47 | virtual void Allocate() override {armnn::armcomputetensorutils::InitialiseArmComputeTensorEmpty(m_Tensor);} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 48 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | virtual void Manage() override |
| 50 | { |
| 51 | assert(m_MemoryGroup != nullptr); |
| 52 | m_MemoryGroup->manage(&m_Tensor); |
| 53 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 54 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 55 | virtual const void* Map(bool blocking = true) const override |
| 56 | { |
| 57 | const_cast<arm_compute::CLTensor*>(&m_Tensor)->map(blocking); |
| 58 | return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes()); |
| 59 | } |
| 60 | virtual void Unmap() const override { const_cast<arm_compute::CLTensor*>(&m_Tensor)->unmap(); } |
| 61 | |
| 62 | virtual ITensorHandle::Type GetType() const override { return ITensorHandle::CL; } |
| 63 | |
| 64 | virtual ITensorHandle* GetParent() const override { return nullptr; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | |
| 66 | virtual arm_compute::DataType GetDataType() const override |
| 67 | { |
| 68 | return m_Tensor.info()->data_type(); |
| 69 | } |
| 70 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 71 | virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>& memoryGroup) override |
| 72 | { |
| 73 | m_MemoryGroup = boost::polymorphic_pointer_downcast<arm_compute::CLMemoryGroup>(memoryGroup); |
| 74 | } |
| 75 | |
| 76 | TensorShape GetStrides() const override |
| 77 | { |
| 78 | return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes()); |
| 79 | } |
| 80 | |
| 81 | TensorShape GetShape() const override |
| 82 | { |
| 83 | return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape()); |
| 84 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 85 | private: |
| 86 | arm_compute::CLTensor m_Tensor; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 87 | std::shared_ptr<arm_compute::CLMemoryGroup> m_MemoryGroup; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | class ClSubTensorHandle : public IClTensorHandle |
| 91 | { |
| 92 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 93 | ClSubTensorHandle(IClTensorHandle* parent, |
| 94 | const arm_compute::TensorShape& shape, |
| 95 | const arm_compute::Coordinates& coords) |
| 96 | : m_Tensor(&parent->GetTensor(), shape, coords) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 98 | parentHandle = parent; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | arm_compute::CLSubTensor& GetTensor() override { return m_Tensor; } |
| 102 | arm_compute::CLSubTensor const& GetTensor() const override { return m_Tensor; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 103 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 104 | virtual void Allocate() override {} |
| 105 | virtual void Manage() override {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 106 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 107 | virtual const void* Map(bool blocking = true) const override |
| 108 | { |
| 109 | const_cast<arm_compute::CLSubTensor*>(&m_Tensor)->map(blocking); |
| 110 | return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes()); |
| 111 | } |
| 112 | virtual void Unmap() const override { const_cast<arm_compute::CLSubTensor*>(&m_Tensor)->unmap(); } |
| 113 | |
| 114 | virtual ITensorHandle::Type GetType() const override { return ITensorHandle::CL; } |
| 115 | |
| 116 | virtual ITensorHandle* GetParent() const override { return parentHandle; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 117 | |
| 118 | virtual arm_compute::DataType GetDataType() const override |
| 119 | { |
| 120 | return m_Tensor.info()->data_type(); |
| 121 | } |
| 122 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 123 | virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>&) override {} |
| 124 | |
| 125 | TensorShape GetStrides() const override |
| 126 | { |
| 127 | return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes()); |
| 128 | } |
| 129 | |
| 130 | TensorShape GetShape() const override |
| 131 | { |
| 132 | return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape()); |
| 133 | } |
| 134 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 135 | private: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 136 | mutable arm_compute::CLSubTensor m_Tensor; |
| 137 | ITensorHandle* parentHandle = nullptr; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 138 | |
| 139 | }; |
| 140 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 141 | } |