Compute Library
 23.08
TensorAllocator Class Reference

Basic implementation of a CPU memory tensor allocator. More...

#include <TensorAllocator.h>

Collaboration diagram for TensorAllocator:
[legend]

Public Member Functions

 TensorAllocator (IMemoryManageable *owner)
 Default constructor. More...
 
 ~TensorAllocator ()
 Default destructor. More...
 
 TensorAllocator (const TensorAllocator &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
TensorAllocatoroperator= (const TensorAllocator &)=delete
 Prevent instances of this class from being copy assigned (As this class contains pointers) More...
 
 TensorAllocator (TensorAllocator &&) noexcept
 Allow instances of this class to be moved. More...
 
TensorAllocatoroperator= (TensorAllocator &&) noexcept
 Allow instances of this class to be moved. More...
 
void init (const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
 Shares the same backing memory with another tensor allocator, while the tensor info might be different. More...
 
uint8_t * data () const
 Returns the pointer to the allocated data. More...
 
void allocate () override
 Allocate size specified by TensorInfo of CPU memory. More...
 
void free () override
 Free allocated CPU memory. More...
 
Status import_memory (void *memory)
 Import an existing memory as a tensor's backing memory. More...
 
void set_associated_memory_group (IMemoryGroup *associated_memory_group)
 Associates the tensor with a memory group. More...
 
void init (const TensorInfo &input, size_t alignment=0)
 Make ITensorAllocator's init methods available. More...
 
- Public Member Functions inherited from ITensorAllocator
 ITensorAllocator ()=default
 Default constructor. More...
 
 ITensorAllocator (const ITensorAllocator &)=default
 Allow instances of this class to be copy constructed. More...
 
ITensorAllocatoroperator= (const ITensorAllocator &)=default
 Allow instances of this class to be copied. More...
 
 ITensorAllocator (ITensorAllocator &&)=default
 Allow instances of this class to be move constructed. More...
 
ITensorAllocatoroperator= (ITensorAllocator &&)=default
 Allow instances of this class to be moved. More...
 
virtual ~ITensorAllocator ()=default
 Default virtual destructor. More...
 
void init (const TensorInfo &input, size_t alignment=0)
 Initialize a tensor based on the passed TensorInfo. More...
 
void soft_init (TensorInfo &input, size_t alignment=0)
 Initialize a tensor based with a reference TensorInfo. More...
 
TensorInfoinfo ()
 Return a reference to the tensor's metadata. More...
 
const TensorInfoinfo () const
 Return a constant reference to the tensor's metadata. More...
 
size_t alignment () const
 Return underlying's tensor buffer alignment. More...
 

Detailed Description

Basic implementation of a CPU memory tensor allocator.

Definition at line 42 of file TensorAllocator.h.

Constructor & Destructor Documentation

◆ TensorAllocator() [1/3]

Default constructor.

Parameters
[in]ownerMemory manageable owner

Definition at line 68 of file TensorAllocator.cpp.

69  : _owner(owner), _associated_memory_group(nullptr), _memory()
70 {
71 }

◆ ~TensorAllocator()

Default destructor.

Definition at line 73 of file TensorAllocator.cpp.

74 {
75  info().set_is_resizable(true);
76 }

References ITensorAllocator::info(), and TensorInfo::set_is_resizable().

◆ TensorAllocator() [2/3]

TensorAllocator ( const TensorAllocator )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ TensorAllocator() [3/3]

TensorAllocator ( TensorAllocator &&  o)
noexcept

Allow instances of this class to be moved.

Definition at line 78 of file TensorAllocator.cpp.

79  : ITensorAllocator(std::move(o)),
80  _owner(o._owner),
81  _associated_memory_group(o._associated_memory_group),
82  _memory(std::move(o._memory))
83 {
84  o._owner = nullptr;
85  o._associated_memory_group = nullptr;
86  o._memory = Memory();
87 }

Member Function Documentation

◆ allocate()

void allocate ( )
overridevirtual

Allocate size specified by TensorInfo of CPU memory.

Note
The tensor must not already be allocated when calling this function.

Implements ITensorAllocator.

Definition at line 132 of file TensorAllocator.cpp.

133 {
134  // Align to 64-byte boundaries by default if alignment is not specified
135  const size_t alignment_to_use = (alignment() != 0) ? alignment() : 64;
136  if(_associated_memory_group == nullptr)
137  {
138  _memory.set_owned_region(std::make_unique<MemoryRegion>(info().total_size(), alignment_to_use));
139  }
140  else
141  {
142  _associated_memory_group->finalize_memory(_owner, _memory, info().total_size(), alignment_to_use);
143  }
144  info().set_is_resizable(false);
145 }

References ITensorAllocator::alignment(), IMemoryGroup::finalize_memory(), ITensorAllocator::info(), TensorInfo::set_is_resizable(), Memory::set_owned_region(), and TensorInfo::total_size().

Referenced by NETensorHandle::allocate(), CPPBoxWithNonMaximaSuppressionLimit::configure(), CPPDetectionPostProcessLayer::configure(), NEFFT2D::configure(), NERNNLayer::configure(), NEL2NormalizeLayer::configure(), NEFFT1D::configure(), NEReductionOperation::configure(), NEInstanceNormalizationLayer::configure(), NEDetectionPostProcessLayer::configure(), NENormalizationLayer::configure(), NEFFTConvolutionLayer::configure(), NEGenerateProposalsLayer::configure(), NELSTMLayerQuantized::configure(), NELSTMLayer::configure(), NEDeconvolutionLayer::configure(), NEQLSTMLayer::configure(), CpuAuxTensorHandler::CpuAuxTensorHandler(), main(), NumPyAccessor::NumPyAccessor(), NEFFTConvolutionLayer::prepare(), NEDeconvolutionLayer::prepare(), NELSTMLayerQuantized::prepare(), NEQLSTMLayer::prepare(), NEFullyConnectedLayerReshapeWeightsManaged::run(), and arm_compute::test::validation::TEST_CASE().

◆ data()

uint8_t * data ( ) const

Returns the pointer to the allocated data.

Returns
a pointer to the allocated data.

Definition at line 127 of file TensorAllocator.cpp.

128 {
129  return (_memory.region() == nullptr) ? nullptr : reinterpret_cast<uint8_t *>(_memory.region()->buffer());
130 }

References IMemoryRegion::buffer(), and Memory::region().

Referenced by Tensor::buffer().

◆ free()

void free ( )
overridevirtual

◆ import_memory()

Status import_memory ( void *  memory)

Import an existing memory as a tensor's backing memory.

Warning
size is expected to be compliant with total_size reported by ITensorInfo.
ownership of memory is not transferred.
tensor shouldn't be memory managed.
padding should be accounted by the client code.
memory must be writable in case of in-place operations
Note
buffer alignment will be checked to be compliant with alignment reported by ITensorInfo.
Parameters
[in]memoryRaw memory pointer to be used as backing memory
Returns
An error status

Definition at line 153 of file TensorAllocator.cpp.

154 {
155  ARM_COMPUTE_RETURN_ERROR_ON(memory == nullptr);
156  ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
158 
159  _memory.set_owned_region(std::make_unique<MemoryRegion>(memory, info().total_size()));
160  info().set_is_resizable(false);
161 
162  return Status{};
163 }

References ITensorAllocator::alignment(), ARM_COMPUTE_RETURN_ERROR_ON, arm_compute::utility::check_aligned(), ITensorAllocator::info(), TensorInfo::set_is_resizable(), and Memory::set_owned_region().

Referenced by CpuAuxTensorHandler::CpuAuxTensorHandler(), NEFFTConvolutionLayer::run(), CpuGemmConv2d::run(), and arm_compute::test::validation::TEST_CASE().

◆ init() [1/2]

void init ( const TensorAllocator allocator,
const Coordinates coords,
TensorInfo sub_info 
)

Shares the same backing memory with another tensor allocator, while the tensor info might be different.

In other words this can be used to create a sub-tensor from another tensor while sharing the same memory.

Note
TensorAllocator have to be of the same specialized type.
Parameters
[in]allocatorThe allocator that owns the backing memory to be shared. Ownership becomes shared afterwards.
[in]coordsThe starting coordinates of the new tensor inside the parent tensor.
[in]sub_infoThe new tensor information (e.g. shape etc)

Definition at line 107 of file TensorAllocator.cpp.

108 {
109  // Get parent info
110  const TensorInfo parent_info = allocator.info();
111 
112  // Check if coordinates and new shape are within the parent tensor
113  ARM_COMPUTE_ERROR_ON(!validate_subtensor_shape(parent_info, sub_info, coords));
114  ARM_COMPUTE_UNUSED(validate_subtensor_shape);
115 
116  // Copy pointer to buffer
117  _memory = Memory(allocator._memory.region());
118 
119  // Init tensor info with new dimensions
120  size_t total_size = parent_info.offset_element_in_bytes(coords) + sub_info.total_size() - sub_info.offset_first_element_in_bytes();
121  sub_info.init(sub_info.tensor_shape(), sub_info.format(), parent_info.strides_in_bytes(), parent_info.offset_element_in_bytes(coords), total_size);
122 
123  // Set TensorInfo
124  init(sub_info);
125 }

References arm_compute::test::validation::allocator(), ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_UNUSED, TensorInfo::format(), TensorInfo::init(), TensorInfo::offset_element_in_bytes(), TensorInfo::offset_first_element_in_bytes(), TensorInfo::strides_in_bytes(), TensorInfo::tensor_shape(), and TensorInfo::total_size().

Referenced by CPPBoxWithNonMaximaSuppressionLimit::configure(), NERNNLayer::configure(), NEFFT1D::configure(), NEReductionOperation::configure(), NENormalizationLayer::configure(), NEFFTConvolutionLayer::configure(), NEGenerateProposalsLayer::configure(), NELSTMLayerQuantized::configure(), NELSTMLayer::configure(), NEDeconvolutionLayer::configure(), NEQLSTMLayer::configure(), main(), NETensorHandle::NETensorHandle(), NumPyAccessor::NumPyAccessor(), and arm_compute::test::validation::TEST_CASE().

◆ init() [2/2]

void init

Make ITensorAllocator's init methods available.

Definition at line 33 of file ITensorAllocator.cpp.

34 {
35  _info_owned = input;
36  _info_external = nullptr;
37  _alignment = alignment;
38 }

◆ operator=() [1/2]

TensorAllocator& operator= ( const TensorAllocator )
delete

Prevent instances of this class from being copy assigned (As this class contains pointers)

◆ operator=() [2/2]

TensorAllocator & operator= ( TensorAllocator &&  o)
noexcept

Allow instances of this class to be moved.

Definition at line 89 of file TensorAllocator.cpp.

90 {
91  if(&o != this)
92  {
93  _owner = o._owner;
94  o._owner = nullptr;
95 
96  _associated_memory_group = o._associated_memory_group;
97  o._associated_memory_group = nullptr;
98 
99  _memory = std::move(o._memory);
100  o._memory = Memory();
101 
102  ITensorAllocator::operator=(std::move(o));
103  }
104  return *this;
105 }

References ITensorAllocator::operator=().

◆ set_associated_memory_group()

void set_associated_memory_group ( IMemoryGroup associated_memory_group)

Associates the tensor with a memory group.

Parameters
[in]associated_memory_groupMemory group to associate the tensor with

Definition at line 165 of file TensorAllocator.cpp.

166 {
167  ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
168  ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr && _associated_memory_group != associated_memory_group);
169  ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.region()->buffer() != nullptr);
170 
171  _associated_memory_group = associated_memory_group;
172 }

References ARM_COMPUTE_ERROR_ON, IMemoryRegion::buffer(), and Memory::region().

Referenced by Tensor::associate_memory_group(), and arm_compute::test::validation::TEST_CASE().


The documentation for this class was generated from the following files:
arm_compute::ITensorAllocator::ITensorAllocator
ITensorAllocator()=default
Default constructor.
arm_compute::TensorAllocator::init
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
Definition: TensorAllocator.cpp:107
arm_compute::TensorInfo::strides_in_bytes
const Strides & strides_in_bytes() const override
The strides in bytes for accessing each dimension of the tensor.
Definition: TensorInfo.h:214
arm_compute::Memory::set_owned_region
void set_owned_region(std::unique_ptr< IMemoryRegion > region) final
Sets a memory region.
Definition: Memory.cpp:64
arm_compute::Memory::region
IMemoryRegion * region() final
Region accessor.
Definition: Memory.cpp:48
arm_compute::ITensorAllocator::alignment
size_t alignment() const
Return underlying's tensor buffer alignment.
Definition: ITensorAllocator.cpp:56
arm_compute::Memory
CPU implementation of memory object.
Definition: Memory.h:37
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:467
arm_compute::ITensorAllocator::info
TensorInfo & info()
Return a reference to the tensor's metadata.
Definition: ITensorAllocator.cpp:46
arm_compute::TensorInfo::format
Format format() const override
Colour format of the image.
Definition: TensorInfo.h:247
arm_compute::utility::check_aligned
bool check_aligned(void *ptr, const size_t alignment)
Checks if a pointer complies with a given alignment.
Definition: Utility.h:195
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:297
arm_compute::TensorInfo::total_size
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: TensorInfo.h:251
arm_compute::ITensorAllocator::operator=
ITensorAllocator & operator=(const ITensorAllocator &)=default
Allow instances of this class to be copied.
arm_compute::Status
Status class.
Definition: Error.h:52
arm_compute::IMemoryGroup::finalize_memory
virtual void finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment)=0
Finalizes memory for a given object.
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
arm_compute::TensorInfo::init
void init(Format format)
Initialize the tensor info with just a format.
Definition: TensorInfo.cpp:125
arm_compute::TensorInfo::set_is_resizable
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:275
arm_compute::TensorInfo
Store the tensor's metadata.
Definition: TensorInfo.h:42
arm_compute::Memory::set_region
void set_region(IMemoryRegion *region) final
Sets a memory region.
Definition: Memory.cpp:58
arm_compute::TensorInfo::offset_element_in_bytes
int32_t offset_element_in_bytes(const Coordinates &pos) const override
The offset in bytes from the beginning of the memory allocation to access the element at position (x,...
Definition: TensorInfo.cpp:402
arm_compute::IMemoryRegion::buffer
virtual void * buffer()=0
Returns the pointer to the allocated data.
arm_compute::TensorInfo::offset_first_element_in_bytes
size_t offset_first_element_in_bytes() const override
The offset from the beginning of the memory allocation to the first element of the tensor.
Definition: TensorInfo.h:218
arm_compute::TensorInfo::tensor_shape
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:235
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486
arm_compute::test::validation::allocator
input allocator() -> allocate()