ArmNN
 24.02
NeonTensorHandle Class Reference

#include <NeonTensorHandle.hpp>

Inheritance diagram for NeonTensorHandle:
[legend]
Collaboration diagram for NeonTensorHandle:
[legend]

Public Member Functions

 NeonTensorHandle (const TensorInfo &tensorInfo)
 
 NeonTensorHandle (const TensorInfo &tensorInfo, DataLayout dataLayout, MemorySourceFlags importFlags=static_cast< MemorySourceFlags >(MemorySource::Malloc))
 
arm_compute::ITensor & GetTensor () override
 
arm_compute::ITensor const & GetTensor () const override
 
virtual void Allocate () override
 Indicate to the memory manager that this resource is no longer active. More...
 
virtual void Manage () override
 Indicate to the memory manager that this resource is active. More...
 
virtual ITensorHandleGetParent () const override
 Get the parent tensor if this is a subtensor. More...
 
virtual arm_compute::DataType GetDataType () const override
 
virtual void SetMemoryGroup (const std::shared_ptr< arm_compute::IMemoryGroup > &memoryGroup) override
 
virtual const void * Map (bool) const override
 Map the tensor data for access. More...
 
virtual void Unmap () const override
 Unmap the tensor data. More...
 
TensorShape GetStrides () const override
 Get the strides for each dimension ordered from largest to smallest where the smallest value is the same as the size of a single element in the tensor. More...
 
TensorShape GetShape () const override
 Get the number of elements for each dimension ordered from slowest iterating dimension to fastest iterating dimension. More...
 
void SetImportFlags (MemorySourceFlags importFlags)
 
MemorySourceFlags GetImportFlags () const override
 Get flags describing supported import sources. More...
 
void SetImportEnabledFlag (bool importEnabledFlag)
 
bool CanBeImported (void *memory, MemorySource source) override
 Implementations must determine if this memory block can be imported. More...
 
virtual bool Import (void *memory, MemorySource source) override
 Import externally allocated memory. More...
 
virtual std::shared_ptr< ITensorHandleDecorateTensorHandle (const TensorInfo &tensorInfo) override
 Returns a decorated version of this TensorHandle allowing us to override the TensorInfo for it. More...
 
- Public Member Functions inherited from ITensorHandle
virtual ~ITensorHandle ()
 
void * Map (bool blocking=true)
 Map the tensor data for access. More...
 
void Unmap ()
 Unmap the tensor data that was previously mapped with call to Map(). More...
 
virtual void Unimport ()
 Unimport externally allocated memory. More...
 

Detailed Description

Definition at line 29 of file NeonTensorHandle.hpp.

Constructor & Destructor Documentation

◆ NeonTensorHandle() [1/2]

NeonTensorHandle ( const TensorInfo tensorInfo)
inline

Definition at line 32 of file NeonTensorHandle.hpp.

33  : m_ImportFlags(static_cast<MemorySourceFlags>(MemorySource::Malloc)),
34  m_Imported(false),
35  m_IsImportEnabled(false),
36  m_TypeAlignment(GetDataTypeSize(tensorInfo.GetDataType()))
37  {
38  armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo);
39  }

References armnn::Malloc.

◆ NeonTensorHandle() [2/2]

NeonTensorHandle ( const TensorInfo tensorInfo,
DataLayout  dataLayout,
MemorySourceFlags  importFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc) 
)
inline

Definition at line 41 of file NeonTensorHandle.hpp.

44  : m_ImportFlags(importFlags),
45  m_Imported(false),
46  m_IsImportEnabled(false),
47  m_TypeAlignment(GetDataTypeSize(tensorInfo.GetDataType()))
48 
49 
50  {
51  armnn::armcomputetensorutils::BuildArmComputeTensor(m_Tensor, tensorInfo, dataLayout);
52  }

Member Function Documentation

◆ Allocate()

virtual void Allocate ( )
inlineoverridevirtual

Indicate to the memory manager that this resource is no longer active.

This is used to compute overlapping lifetimes of resources.

Implements ITensorHandle.

Definition at line 57 of file NeonTensorHandle.hpp.

58  {
59  // If we have enabled Importing, don't Allocate the tensor
60  if (!m_IsImportEnabled)
61  {
62  armnn::armcomputetensorutils::InitialiseArmComputeTensorEmpty(m_Tensor);
63  }
64  };

◆ CanBeImported()

bool CanBeImported ( void *  memory,
MemorySource  source 
)
inlineoverridevirtual

Implementations must determine if this memory block can be imported.

This might be based on alignment or memory source type.

Returns
true if this memory can be imported.
false by default, cannot be imported.

Reimplemented from ITensorHandle.

Definition at line 120 of file NeonTensorHandle.hpp.

121  {
122  if (source != MemorySource::Malloc || reinterpret_cast<uintptr_t>(memory) % m_TypeAlignment)
123  {
124  return false;
125  }
126  return true;
127  }

References armnn::Malloc.

Referenced by NeonTensorHandle::Import().

◆ DecorateTensorHandle()

std::shared_ptr< ITensorHandle > DecorateTensorHandle ( const TensorInfo tensorInfo)
overridevirtual

Returns a decorated version of this TensorHandle allowing us to override the TensorInfo for it.

Parameters
tensorInfothe overidden TensorInfo.

Reimplemented from ITensorHandle.

Definition at line 12 of file NeonTensorHandle.cpp.

13 {
14  auto* parent = const_cast<NeonTensorHandle*>(this);
15  auto decorated = std::make_shared<NeonTensorHandleDecorator>(parent, tensorInfo);
16  m_Decorated.emplace_back(decorated);
17  return decorated;
18 }

◆ GetDataType()

virtual arm_compute::DataType GetDataType ( ) const
inlineoverridevirtual

Implements IAclTensorHandle.

Definition at line 78 of file NeonTensorHandle.hpp.

79  {
80  return m_Tensor.info()->data_type();
81  }

◆ GetImportFlags()

MemorySourceFlags GetImportFlags ( ) const
inlineoverridevirtual

Get flags describing supported import sources.

Reimplemented from ITensorHandle.

Definition at line 110 of file NeonTensorHandle.hpp.

111  {
112  return m_ImportFlags;
113  }

◆ GetParent()

virtual ITensorHandle* GetParent ( ) const
inlineoverridevirtual

Get the parent tensor if this is a subtensor.

Returns
a pointer to the parent tensor. Otherwise nullptr if not a subtensor.

Implements ITensorHandle.

Definition at line 76 of file NeonTensorHandle.hpp.

76 { return nullptr; }

◆ GetShape()

TensorShape GetShape ( ) const
inlineoverridevirtual

Get the number of elements for each dimension ordered from slowest iterating dimension to fastest iterating dimension.

Returns
a TensorShape filled with the number of elements for each dimension.

Implements ITensorHandle.

Definition at line 100 of file NeonTensorHandle.hpp.

101  {
102  return armcomputetensorutils::GetShape(m_Tensor.info()->tensor_shape());
103  }

Referenced by NeonRankWorkload::Execute().

◆ GetStrides()

TensorShape GetStrides ( ) const
inlineoverridevirtual

Get the strides for each dimension ordered from largest to smallest where the smallest value is the same as the size of a single element in the tensor.

Returns
a TensorShape filled with the strides for each dimension

Implements ITensorHandle.

Definition at line 95 of file NeonTensorHandle.hpp.

96  {
97  return armcomputetensorutils::GetStrides(m_Tensor.info()->strides_in_bytes());
98  }

◆ GetTensor() [1/2]

arm_compute::ITensor const& GetTensor ( ) const
inlineoverridevirtual

Implements IAclTensorHandle.

Definition at line 55 of file NeonTensorHandle.hpp.

55 { return m_Tensor; }

◆ GetTensor() [2/2]

arm_compute::ITensor& GetTensor ( )
inlineoverridevirtual

Implements IAclTensorHandle.

Definition at line 54 of file NeonTensorHandle.hpp.

54 { return m_Tensor; }

◆ Import()

virtual bool Import ( void *  memory,
MemorySource  source 
)
inlineoverridevirtual

Import externally allocated memory.

Parameters
memorybase address of the memory being imported.
sourcesource of the allocation for the memory being imported.
Returns
true on success or false on failure

Reimplemented from ITensorHandle.

Definition at line 129 of file NeonTensorHandle.hpp.

130  {
131  if (m_ImportFlags& static_cast<MemorySourceFlags>(source))
132  {
133  if (source == MemorySource::Malloc && m_IsImportEnabled)
134  {
135  if (!CanBeImported(memory, source))
136  {
137  throw MemoryImportException("NeonTensorHandle::Import Attempting to import unaligned memory");
138  }
139 
140  // m_Tensor not yet Allocated
141  if (!m_Imported && !m_Tensor.buffer())
142  {
143  arm_compute::Status status = m_Tensor.allocator()->import_memory(memory);
144  // Use the overloaded bool operator of Status to check if it worked, if not throw an exception
145  // with the Status error message
146  m_Imported = bool(status);
147  if (!m_Imported)
148  {
149  throw MemoryImportException(status.error_description());
150  }
151  return m_Imported;
152  }
153 
154  // m_Tensor.buffer() initially allocated with Allocate().
155  if (!m_Imported && m_Tensor.buffer())
156  {
157  throw MemoryImportException(
158  "NeonTensorHandle::Import Attempting to import on an already allocated tensor");
159  }
160 
161  // m_Tensor.buffer() previously imported.
162  if (m_Imported)
163  {
164  arm_compute::Status status = m_Tensor.allocator()->import_memory(memory);
165  // Use the overloaded bool operator of Status to check if it worked, if not throw an exception
166  // with the Status error message
167  m_Imported = bool(status);
168  if (!m_Imported)
169  {
170  throw MemoryImportException(status.error_description());
171  }
172  return m_Imported;
173  }
174  }
175  else
176  {
177  throw MemoryImportException("NeonTensorHandle::Import is disabled");
178  }
179  }
180  else
181  {
182  throw MemoryImportException("NeonTensorHandle::Incorrect import flag");
183  }
184  return false;
185  }

References NeonTensorHandle::CanBeImported(), and armnn::Malloc.

◆ Manage()

virtual void Manage ( )
inlineoverridevirtual

Indicate to the memory manager that this resource is active.

This is used to compute overlapping lifetimes of resources.

Implements ITensorHandle.

Definition at line 66 of file NeonTensorHandle.hpp.

67  {
68  // If we have enabled Importing, don't manage the tensor
69  if (!m_IsImportEnabled)
70  {
71  ARMNN_ASSERT(m_MemoryGroup != nullptr);
72  m_MemoryGroup->manage(&m_Tensor);
73  }
74  }

References ARMNN_ASSERT.

◆ Map()

virtual const void* Map ( bool  blocking) const
inlineoverridevirtual

Map the tensor data for access.

Parameters
blockinghint to block the calling thread until all other accesses are complete. (backend dependent)
Returns
pointer to the first element of the mapped data.

Implements ITensorHandle.

Definition at line 88 of file NeonTensorHandle.hpp.

89  {
90  return static_cast<const void*>(m_Tensor.buffer() + m_Tensor.info()->offset_first_element_in_bytes());
91  }

◆ SetImportEnabledFlag()

void SetImportEnabledFlag ( bool  importEnabledFlag)
inline

Definition at line 115 of file NeonTensorHandle.hpp.

116  {
117  m_IsImportEnabled = importEnabledFlag;
118  }

◆ SetImportFlags()

void SetImportFlags ( MemorySourceFlags  importFlags)
inline

Definition at line 105 of file NeonTensorHandle.hpp.

106  {
107  m_ImportFlags = importFlags;
108  }

◆ SetMemoryGroup()

virtual void SetMemoryGroup ( const std::shared_ptr< arm_compute::IMemoryGroup > &  memoryGroup)
inlineoverridevirtual

Implements IAclTensorHandle.

Definition at line 83 of file NeonTensorHandle.hpp.

84  {
85  m_MemoryGroup = PolymorphicPointerDowncast<arm_compute::MemoryGroup>(memoryGroup);
86  }

◆ Unmap()

virtual void Unmap ( ) const
inlineoverridevirtual

Unmap the tensor data.

Implements ITensorHandle.

Definition at line 93 of file NeonTensorHandle.hpp.

93 {}

The documentation for this class was generated from the following files:
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::MemorySource::Malloc
@ Malloc
armnn::NeonTensorHandle::NeonTensorHandle
NeonTensorHandle(const TensorInfo &tensorInfo)
Definition: NeonTensorHandle.hpp:32
armnn::MemorySourceFlags
unsigned int MemorySourceFlags
Definition: MemorySources.hpp:15
armnn::NeonTensorHandle::CanBeImported
bool CanBeImported(void *memory, MemorySource source) override
Implementations must determine if this memory block can be imported.
Definition: NeonTensorHandle.hpp:120
armnn::GetDataTypeSize
constexpr unsigned int GetDataTypeSize(DataType dataType)
Definition: TypesUtils.hpp:182
armnn::Status
Status
Definition: Types.hpp:42