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 |
David Beck | 0dbe0ee | 2018-09-24 15:59:27 +0100 | [diff] [blame] | 6 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 7 | #include "CpuTensorHandleFwd.hpp" |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 8 | #include "CompatibleTypes.hpp" |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 9 | |
David Beck | 0dbe0ee | 2018-09-24 15:59:27 +0100 | [diff] [blame] | 10 | #include <armnn/TypesUtils.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 11 | |
| 12 | #include <backendsCommon/OutputHandler.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 13 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 16 | namespace armnn |
| 17 | { |
| 18 | |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 19 | // Get a TensorShape representing the strides (in bytes) for each dimension |
| 20 | // of a tensor, assuming fully packed data with no padding |
| 21 | TensorShape GetUnpaddedTensorStrides(const TensorInfo& tensorInfo); |
| 22 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | // Abstract tensor handles wrapping a CPU-readable region of memory, interpreting it as tensor data. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | class ConstCpuTensorHandle : public ITensorHandle |
| 25 | { |
| 26 | public: |
| 27 | template <typename T> |
| 28 | const T* GetConstTensor() const |
| 29 | { |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 30 | BOOST_ASSERT(CompatibleTypes<T>(GetTensorInfo().GetDataType())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 31 | return reinterpret_cast<const T*>(m_Memory); |
| 32 | } |
| 33 | |
| 34 | const TensorInfo& GetTensorInfo() const |
| 35 | { |
| 36 | return m_TensorInfo; |
| 37 | } |
| 38 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 39 | virtual void Manage() override {} |
| 40 | |
| 41 | virtual ITensorHandle* GetParent() const override { return nullptr; } |
| 42 | |
| 43 | virtual const void* Map(bool /* blocking = true */) const override { return m_Memory; } |
| 44 | virtual void Unmap() const override {} |
| 45 | |
| 46 | TensorShape GetStrides() const override |
| 47 | { |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 48 | return GetUnpaddedTensorStrides(m_TensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | } |
| 50 | TensorShape GetShape() const override { return m_TensorInfo.GetShape(); } |
| 51 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 52 | protected: |
| 53 | ConstCpuTensorHandle(const TensorInfo& tensorInfo); |
| 54 | |
| 55 | void SetConstMemory(const void* mem) { m_Memory = mem; } |
| 56 | |
| 57 | private: |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 58 | // Only used for testing |
Matthew Bentham | 4cefc41 | 2019-06-18 16:14:34 +0100 | [diff] [blame] | 59 | void CopyOutTo(void *) const override { BOOST_ASSERT_MSG(false, "Unimplemented"); } |
| 60 | void CopyInFrom(const void*) override { BOOST_ASSERT_MSG(false, "Unimplemented"); } |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 61 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | ConstCpuTensorHandle(const ConstCpuTensorHandle& other) = delete; |
| 63 | ConstCpuTensorHandle& operator=(const ConstCpuTensorHandle& other) = delete; |
| 64 | |
| 65 | TensorInfo m_TensorInfo; |
| 66 | const void* m_Memory; |
| 67 | }; |
| 68 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 69 | template<> |
| 70 | const void* ConstCpuTensorHandle::GetConstTensor<void>() const; |
| 71 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | // Abstract specialization of ConstCpuTensorHandle that allows write access to the same data. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | class CpuTensorHandle : public ConstCpuTensorHandle |
| 74 | { |
| 75 | public: |
| 76 | template <typename T> |
| 77 | T* GetTensor() const |
| 78 | { |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 79 | BOOST_ASSERT(CompatibleTypes<T>(GetTensorInfo().GetDataType())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 80 | return reinterpret_cast<T*>(m_MutableMemory); |
| 81 | } |
| 82 | |
| 83 | protected: |
| 84 | CpuTensorHandle(const TensorInfo& tensorInfo); |
| 85 | |
| 86 | void SetMemory(void* mem) |
| 87 | { |
| 88 | m_MutableMemory = mem; |
| 89 | SetConstMemory(m_MutableMemory); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | |
| 94 | CpuTensorHandle(const CpuTensorHandle& other) = delete; |
| 95 | CpuTensorHandle& operator=(const CpuTensorHandle& other) = delete; |
| 96 | void* m_MutableMemory; |
| 97 | }; |
| 98 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 99 | template <> |
| 100 | void* CpuTensorHandle::GetTensor<void>() const; |
| 101 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 102 | // A CpuTensorHandle that owns the wrapped memory region. |
| 103 | class ScopedCpuTensorHandle : public CpuTensorHandle |
| 104 | { |
| 105 | public: |
| 106 | explicit ScopedCpuTensorHandle(const TensorInfo& tensorInfo); |
| 107 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | // Copies contents from Tensor. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 109 | explicit ScopedCpuTensorHandle(const ConstTensor& tensor); |
| 110 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | // Copies contents from ConstCpuTensorHandle |
| 112 | explicit ScopedCpuTensorHandle(const ConstCpuTensorHandle& tensorHandle); |
| 113 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 114 | ScopedCpuTensorHandle(const ScopedCpuTensorHandle& other); |
| 115 | ScopedCpuTensorHandle& operator=(const ScopedCpuTensorHandle& other); |
| 116 | ~ScopedCpuTensorHandle(); |
| 117 | |
| 118 | virtual void Allocate() override; |
| 119 | |
| 120 | private: |
David Beck | 09e2f27 | 2018-10-30 11:38:41 +0000 | [diff] [blame] | 121 | // Only used for testing |
| 122 | void CopyOutTo(void* memory) const override; |
| 123 | void CopyInFrom(const void* memory) override; |
| 124 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 125 | void CopyFrom(const ScopedCpuTensorHandle& other); |
| 126 | void CopyFrom(const void* srcMemory, unsigned int numBytes); |
| 127 | }; |
| 128 | |
| 129 | // A CpuTensorHandle that wraps an already allocated memory region. |
| 130 | // |
| 131 | // Clients must make sure the passed in memory region stays alive for the lifetime of |
| 132 | // the PassthroughCpuTensorHandle instance. |
| 133 | // |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 134 | // Note there is no polymorphism to/from ConstPassthroughCpuTensorHandle. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 135 | class PassthroughCpuTensorHandle : public CpuTensorHandle |
| 136 | { |
| 137 | public: |
| 138 | PassthroughCpuTensorHandle(const TensorInfo& tensorInfo, void* mem) |
| 139 | : CpuTensorHandle(tensorInfo) |
| 140 | { |
| 141 | SetMemory(mem); |
| 142 | } |
| 143 | |
| 144 | virtual void Allocate() override; |
| 145 | }; |
| 146 | |
| 147 | // A ConstCpuTensorHandle that wraps an already allocated memory region. |
| 148 | // |
| 149 | // This allows users to pass in const memory to a network. |
| 150 | // Clients must make sure the passed in memory region stays alive for the lifetime of |
| 151 | // the PassthroughCpuTensorHandle instance. |
| 152 | // |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 153 | // Note there is no polymorphism to/from PassthroughCpuTensorHandle. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 154 | class ConstPassthroughCpuTensorHandle : public ConstCpuTensorHandle |
| 155 | { |
| 156 | public: |
| 157 | ConstPassthroughCpuTensorHandle(const TensorInfo& tensorInfo, const void* mem) |
| 158 | : ConstCpuTensorHandle(tensorInfo) |
| 159 | { |
| 160 | SetConstMemory(mem); |
| 161 | } |
| 162 | |
| 163 | virtual void Allocate() override; |
| 164 | }; |
| 165 | |
| 166 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 167 | // Template specializations. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 168 | |
| 169 | template <> |
| 170 | const void* ConstCpuTensorHandle::GetConstTensor() const; |
| 171 | |
| 172 | template <> |
| 173 | void* CpuTensorHandle::GetTensor() const; |
| 174 | |
| 175 | } // namespace armnn |