ArmNN
 22.02
NeonTensorHandle.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include <BFloat16.hpp>
8 #include <Half.hpp>
9 
10 #include <armnn/utility/Assert.hpp>
11 
15 
16 #include <arm_compute/runtime/MemoryGroup.h>
17 #include <arm_compute/runtime/IMemoryGroup.h>
18 #include <arm_compute/runtime/Tensor.h>
19 #include <arm_compute/runtime/SubTensor.h>
20 #include <arm_compute/core/TensorShape.h>
21 #include <arm_compute/core/Coordinates.h>
22 
23 namespace armnn
24 {
25 
27 {
28 public:
29  NeonTensorHandle(const TensorInfo& tensorInfo)
30  : m_ImportFlags(static_cast<MemorySourceFlags>(MemorySource::Malloc)),
31  m_Imported(false),
32  m_IsImportEnabled(false),
33  m_TypeAlignment(GetDataTypeSize(tensorInfo.GetDataType()))
34  {
35  armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo);
36  }
37 
38  NeonTensorHandle(const TensorInfo& tensorInfo,
39  DataLayout dataLayout,
40  MemorySourceFlags importFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc))
41  : m_ImportFlags(importFlags),
42  m_Imported(false),
43  m_IsImportEnabled(false),
44  m_TypeAlignment(GetDataTypeSize(tensorInfo.GetDataType()))
45 
46 
47  {
48  armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo, dataLayout);
49  }
50 
51  arm_compute::ITensor& GetTensor() override { return m_Tensor; }
52  arm_compute::ITensor const& GetTensor() const override { return m_Tensor; }
53 
54  virtual void Allocate() override
55  {
56  // If we have enabled Importing, don't Allocate the tensor
57  if (!m_IsImportEnabled)
58  {
59  armnn::armcomputetensorutils::InitialiseArmComputeTensorEmpty(m_Tensor);
60  }
61  };
62 
63  virtual void Manage() override
64  {
65  // If we have enabled Importing, don't manage the tensor
66  if (!m_IsImportEnabled)
67  {
68  ARMNN_ASSERT(m_MemoryGroup != nullptr);
69  m_MemoryGroup->manage(&m_Tensor);
70  }
71  }
72 
73  virtual ITensorHandle* GetParent() const override { return nullptr; }
74 
75  virtual arm_compute::DataType GetDataType() const override
76  {
77  return m_Tensor.info()->data_type();
78  }
79 
80  virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>& memoryGroup) override
81  {
82  m_MemoryGroup = PolymorphicPointerDowncast<arm_compute::MemoryGroup>(memoryGroup);
83  }
84 
85  virtual const void* Map(bool /* blocking = true */) const override
86  {
87  return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes());
88  }
89 
90  virtual void Unmap() const override {}
91 
92  TensorShape GetStrides() const override
93  {
94  return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes());
95  }
96 
97  TensorShape GetShape() const override
98  {
99  return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape());
100  }
101 
103  {
104  m_ImportFlags = importFlags;
105  }
106 
108  {
109  return m_ImportFlags;
110  }
111 
112  void SetImportEnabledFlag(bool importEnabledFlag)
113  {
114  m_IsImportEnabled = importEnabledFlag;
115  }
116 
117  bool CanBeImported(void* memory, MemorySource source) override
118  {
119  armnn::IgnoreUnused(source);
120  if (reinterpret_cast<uintptr_t>(memory) % m_TypeAlignment)
121  {
122  return false;
123  }
124  return true;
125  }
126 
127  virtual bool Import(void* memory, MemorySource source) override
128  {
129  if (m_ImportFlags & static_cast<MemorySourceFlags>(source))
130  {
131  if (source == MemorySource::Malloc && m_IsImportEnabled)
132  {
133  if (!CanBeImported(memory, source))
134  {
135  throw MemoryImportException("NeonTensorHandle::Import Attempting to import unaligned memory");
136  }
137 
138  // m_Tensor not yet Allocated
139  if (!m_Imported && !m_Tensor.buffer())
140  {
141  arm_compute::Status status = m_Tensor.allocator()->import_memory(memory);
142  // Use the overloaded bool operator of Status to check if it worked, if not throw an exception
143  // with the Status error message
144  m_Imported = bool(status);
145  if (!m_Imported)
146  {
147  throw MemoryImportException(status.error_description());
148  }
149  return m_Imported;
150  }
151 
152  // m_Tensor.buffer() initially allocated with Allocate().
153  if (!m_Imported && m_Tensor.buffer())
154  {
155  throw MemoryImportException(
156  "NeonTensorHandle::Import Attempting to import on an already allocated tensor");
157  }
158 
159  // m_Tensor.buffer() previously imported.
160  if (m_Imported)
161  {
162  arm_compute::Status status = m_Tensor.allocator()->import_memory(memory);
163  // Use the overloaded bool operator of Status to check if it worked, if not throw an exception
164  // with the Status error message
165  m_Imported = bool(status);
166  if (!m_Imported)
167  {
168  throw MemoryImportException(status.error_description());
169  }
170  return m_Imported;
171  }
172  }
173  else
174  {
175  throw MemoryImportException("NeonTensorHandle::Import is disabled");
176  }
177  }
178  else
179  {
180  throw MemoryImportException("NeonTensorHandle::Incorrect import flag");
181  }
182  return false;
183  }
184 
185 private:
186  // Only used for testing
187  void CopyOutTo(void* memory) const override
188  {
189  switch (this->GetDataType())
190  {
191  case arm_compute::DataType::F32:
192  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
193  static_cast<float*>(memory));
194  break;
195  case arm_compute::DataType::U8:
196  case arm_compute::DataType::QASYMM8:
197  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
198  static_cast<uint8_t*>(memory));
199  break;
200  case arm_compute::DataType::QSYMM8:
201  case arm_compute::DataType::QASYMM8_SIGNED:
202  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
203  static_cast<int8_t*>(memory));
204  break;
205  case arm_compute::DataType::BFLOAT16:
206  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
207  static_cast<armnn::BFloat16*>(memory));
208  break;
209  case arm_compute::DataType::F16:
210  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
211  static_cast<armnn::Half*>(memory));
212  break;
213  case arm_compute::DataType::S16:
214  case arm_compute::DataType::QSYMM16:
215  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
216  static_cast<int16_t*>(memory));
217  break;
218  case arm_compute::DataType::S32:
219  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
220  static_cast<int32_t*>(memory));
221  break;
222  default:
223  {
225  }
226  }
227  }
228 
229  // Only used for testing
230  void CopyInFrom(const void* memory) override
231  {
232  switch (this->GetDataType())
233  {
234  case arm_compute::DataType::F32:
235  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const float*>(memory),
236  this->GetTensor());
237  break;
238  case arm_compute::DataType::U8:
239  case arm_compute::DataType::QASYMM8:
240  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const uint8_t*>(memory),
241  this->GetTensor());
242  break;
243  case arm_compute::DataType::QSYMM8:
244  case arm_compute::DataType::QASYMM8_SIGNED:
245  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const int8_t*>(memory),
246  this->GetTensor());
247  break;
248  case arm_compute::DataType::BFLOAT16:
249  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const armnn::BFloat16*>(memory),
250  this->GetTensor());
251  break;
252  case arm_compute::DataType::F16:
253  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const armnn::Half*>(memory),
254  this->GetTensor());
255  break;
256  case arm_compute::DataType::S16:
257  case arm_compute::DataType::QSYMM16:
258  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const int16_t*>(memory),
259  this->GetTensor());
260  break;
261  case arm_compute::DataType::S32:
262  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const int32_t*>(memory),
263  this->GetTensor());
264  break;
265  default:
266  {
268  }
269  }
270  }
271 
272  arm_compute::Tensor m_Tensor;
273  std::shared_ptr<arm_compute::MemoryGroup> m_MemoryGroup;
274  MemorySourceFlags m_ImportFlags;
275  bool m_Imported;
276  bool m_IsImportEnabled;
277  const uintptr_t m_TypeAlignment;
278 };
279 
281 {
282 public:
284  const arm_compute::TensorShape& shape,
285  const arm_compute::Coordinates& coords)
286  : m_Tensor(&parent->GetTensor(), shape, coords)
287  {
288  parentHandle = parent;
289  }
290 
291  arm_compute::ITensor& GetTensor() override { return m_Tensor; }
292  arm_compute::ITensor const& GetTensor() const override { return m_Tensor; }
293 
294  virtual void Allocate() override {}
295  virtual void Manage() override {}
296 
297  virtual ITensorHandle* GetParent() const override { return parentHandle; }
298 
299  virtual arm_compute::DataType GetDataType() const override
300  {
301  return m_Tensor.info()->data_type();
302  }
303 
304  virtual void SetMemoryGroup(const std::shared_ptr<arm_compute::IMemoryGroup>&) override {}
305 
306  virtual const void* Map(bool /* blocking = true */) const override
307  {
308  return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes());
309  }
310  virtual void Unmap() const override {}
311 
312  TensorShape GetStrides() const override
313  {
314  return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes());
315  }
316 
317  TensorShape GetShape() const override
318  {
319  return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape());
320  }
321 
322 private:
323  // Only used for testing
324  void CopyOutTo(void* memory) const override
325  {
326  switch (this->GetDataType())
327  {
328  case arm_compute::DataType::F32:
329  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
330  static_cast<float*>(memory));
331  break;
332  case arm_compute::DataType::U8:
333  case arm_compute::DataType::QASYMM8:
334  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
335  static_cast<uint8_t*>(memory));
336  break;
337  case arm_compute::DataType::QSYMM8:
338  case arm_compute::DataType::QASYMM8_SIGNED:
339  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
340  static_cast<int8_t*>(memory));
341  break;
342  case arm_compute::DataType::S16:
343  case arm_compute::DataType::QSYMM16:
344  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
345  static_cast<int16_t*>(memory));
346  break;
347  case arm_compute::DataType::S32:
348  armcomputetensorutils::CopyArmComputeITensorData(this->GetTensor(),
349  static_cast<int32_t*>(memory));
350  break;
351  default:
352  {
354  }
355  }
356  }
357 
358  // Only used for testing
359  void CopyInFrom(const void* memory) override
360  {
361  switch (this->GetDataType())
362  {
363  case arm_compute::DataType::F32:
364  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const float*>(memory),
365  this->GetTensor());
366  break;
367  case arm_compute::DataType::U8:
368  case arm_compute::DataType::QASYMM8:
369  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const uint8_t*>(memory),
370  this->GetTensor());
371  break;
372  case arm_compute::DataType::QSYMM8:
373  case arm_compute::DataType::QASYMM8_SIGNED:
374  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const int8_t*>(memory),
375  this->GetTensor());
376  break;
377  case arm_compute::DataType::S16:
378  case arm_compute::DataType::QSYMM16:
379  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const int16_t*>(memory),
380  this->GetTensor());
381  break;
382  case arm_compute::DataType::S32:
383  armcomputetensorutils::CopyArmComputeITensorData(static_cast<const int32_t*>(memory),
384  this->GetTensor());
385  break;
386  default:
387  {
389  }
390  }
391  }
392 
393  arm_compute::SubTensor m_Tensor;
394  ITensorHandle* parentHandle = nullptr;
395 };
396 
397 } // namespace armnn
TensorShape GetShape() const override
Get the number of elements for each dimension ordered from slowest iterating dimension to fastest ite...
bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
TensorShape GetStrides() const override
Get the strides for each dimension ordered from largest to smallest where the smallest value is the s...
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
virtual void SetMemoryGroup(const std::shared_ptr< arm_compute::IMemoryGroup > &) override
DataLayout
Definition: Types.hpp:49
virtual arm_compute::DataType GetDataType() const override
virtual void SetMemoryGroup(const std::shared_ptr< arm_compute::IMemoryGroup > &memoryGroup) override
virtual arm_compute::DataType GetDataType() const override
std::array< unsigned int, MaxNumOfTensorDimensions > Coordinates
virtual void Manage() override
Indicate to the memory manager that this resource is active.
virtual void Unmap() const override
Unmap the tensor data.
arm_compute::ITensor const & GetTensor() const override
virtual ITensorHandle * GetParent() const override
Get the parent tensor if this is a subtensor.
unsigned int MemorySourceFlags
arm_compute::ITensor const & GetTensor() const override
virtual void Unmap() const override
Unmap the tensor data.
Copyright (c) 2021 ARM Limited and Contributors.
virtual bool Import(void *memory, MemorySource source) override
Import externally allocated memory.
void IgnoreUnused(Ts &&...)
DataType
Definition: Types.hpp:35
TensorShape GetShape() const override
Get the number of elements for each dimension ordered from slowest iterating dimension to fastest ite...
NeonSubTensorHandle(IAclTensorHandle *parent, const arm_compute::TensorShape &shape, const arm_compute::Coordinates &coords)
Status
enumeration
Definition: Types.hpp:29
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
arm_compute::ITensor & GetTensor() override
MemorySourceFlags GetImportFlags() const override
Get flags describing supported import sources.
TensorShape GetStrides() const override
Get the strides for each dimension ordered from largest to smallest where the smallest value is the s...
virtual const void * Map(bool) const override
Map the tensor data for access.
NeonTensorHandle(const TensorInfo &tensorInfo, DataLayout dataLayout, MemorySourceFlags importFlags=static_cast< MemorySourceFlags >(MemorySource::Malloc))
virtual ITensorHandle * GetParent() const override
Get the parent tensor if this is a subtensor.
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:217
virtual void Allocate() override
Indicate to the memory manager that this resource is no longer active.
NeonTensorHandle(const TensorInfo &tensorInfo)
virtual const void * Map(bool) const override
Map the tensor data for access.
void SetImportFlags(MemorySourceFlags importFlags)
arm_compute::ITensor & GetTensor() override
void SetImportEnabledFlag(bool importEnabledFlag)
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:151
virtual void Manage() override
Indicate to the memory manager that this resource is active.