Compute Library
 20.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 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...
 
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 69 of file TensorAllocator.cpp.

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

◆ ~TensorAllocator()

Default destructor.

Definition at line 74 of file TensorAllocator.cpp.

75 {
76  info().set_is_resizable(true);
77 }
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:293
TensorInfo & info()
Return a reference to the tensor's metadata.

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 79 of file TensorAllocator.cpp.

80  : ITensorAllocator(std::move(o)),
81  _owner(o._owner),
82  _associated_memory_group(o._associated_memory_group),
83  _memory(std::move(o._memory))
84 {
85  o._owner = nullptr;
86  o._associated_memory_group = nullptr;
87  o._memory = Memory();
88 }
CPU implementation of memory object.
Definition: Memory.h:37
ITensorAllocator()
Default constructor.

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 133 of file TensorAllocator.cpp.

134 {
135  // Align to 64-byte boundaries by default if alignment is not specified
136  const size_t alignment_to_use = (alignment() != 0) ? alignment() : 64;
137  if(_associated_memory_group == nullptr)
138  {
139  _memory.set_owned_region(support::cpp14::make_unique<MemoryRegion>(info().total_size(), alignment_to_use));
140  }
141  else
142  {
143  _associated_memory_group->finalize_memory(_owner, _memory, info().total_size(), alignment_to_use);
144  }
145  info().set_is_resizable(false);
146 }
void set_owned_region(std::unique_ptr< IMemoryRegion > region) final
Sets a memory region.
Definition: Memory.cpp:64
virtual void finalize_memory(IMemoryManageable *obj, IMemory &obj_memory, size_t size, size_t alignment)=0
Finalizes memory for a given object.
size_t alignment() const
Return underlying's tensor buffer alignment.
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:293
TensorInfo & info()
Return a reference to the tensor's metadata.

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

Referenced by NETensorHandle::allocate(), NEGEMMLowpAssemblyMatrixMultiplyCore::configure(), NEFFT2D::configure(), NEReductionOperation::configure(), NEL2NormalizeLayer::configure(), NEFFT1D::configure(), NEInstanceNormalizationLayer::configure(), NEHOGDescriptor::configure(), NEGaussian5x5::configure(), NENormalizationLayer::configure(), NEHOGGradient::configure(), NERNNLayer::configure(), CPPBoxWithNonMaximaSuppressionLimit::configure(), NESobel5x5::configure(), NESobel7x7::configure(), NEFastCorners::configure(), NEDepthwiseConvolutionAssemblyDispatch::configure(), NEScale::configure(), CPPDetectionPostProcessLayer::configure(), NEDetectionPostProcessLayer::configure(), NEWinogradConvolutionLayer::configure(), NELocallyConnectedLayer::configure(), NECannyEdge::configure(), NEHarrisCorners::configure(), NEHOGMultiDetection::configure(), NEFFTConvolutionLayer::configure(), NEGEMM::configure(), NEGenerateProposalsLayer::configure(), NELSTMLayer::configure(), NEGEMMLowpMatrixMultiplyCore::configure(), NELSTMLayerQuantized::configure(), NEDeconvolutionLayer::configure(), NEQLSTMLayer::configure(), NEGEMMConvolutionLayer::configure(), main(), NumPyAccessor::NumPyAccessor(), NEWinogradConvolutionLayer::prepare(), NELocallyConnectedLayer::prepare(), NEGEMM::prepare(), NEDepthwiseConvolutionAssemblyDispatch::prepare(), NEFFTConvolutionLayer::prepare(), NEGEMMLowpMatrixMultiplyCore::prepare(), NEDeconvolutionLayer::prepare(), NELSTMLayerQuantized::prepare(), NEFullyConnectedLayer::prepare(), NEGEMMConvolutionLayer::prepare(), NEFullyConnectedLayerReshapeWeightsManaged::run(), NEConvertFullyConnectedWeightsManaged::run(), NEConvolutionLayerReshapeWeightsTransform::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 128 of file TensorAllocator.cpp.

129 {
130  return (_memory.region() == nullptr) ? nullptr : reinterpret_cast<uint8_t *>(_memory.region()->buffer());
131 }
IMemoryRegion * region() final
Region accessor.
Definition: Memory.cpp:48
virtual void * buffer()=0
Returns the pointer to the allocated data.

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

Referenced by Tensor::buffer().

◆ free()

void free ( )
overridevirtual

Free allocated CPU memory.

Note
The tensor must have been allocated when calling this function.

Implements ITensorAllocator.

Definition at line 148 of file TensorAllocator.cpp.

149 {
150  _memory.set_region(nullptr);
151  info().set_is_resizable(true);
152 }
void set_region(IMemoryRegion *region) final
Sets a memory region.
Definition: Memory.cpp:58
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:293
TensorInfo & info()
Return a reference to the tensor's metadata.

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

Referenced by NEDirectConvolutionLayer::configure(), NETensorHandle::free(), NEWinogradConvolutionLayer::prepare(), NEFFTConvolutionLayer::prepare(), NELSTMLayerQuantized::prepare(), NEGEMMConvolutionLayer::prepare(), NEFullyConnectedLayerReshapeWeightsManaged::release(), NEConvertFullyConnectedWeightsManaged::release(), NEConvolutionLayerReshapeWeightsTransform::release(), NETensorHandle::release_if_unused(), and arm_compute::test::validation::TEST_CASE().

◆ 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 154 of file TensorAllocator.cpp.

155 {
156  ARM_COMPUTE_RETURN_ERROR_ON(memory == nullptr);
157  ARM_COMPUTE_RETURN_ERROR_ON(_associated_memory_group != nullptr);
159 
160  _memory.set_owned_region(support::cpp14::make_unique<MemoryRegion>(memory, info().total_size()));
161  info().set_is_resizable(false);
162 
163  return Status{};
164 }
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
void set_owned_region(std::unique_ptr< IMemoryRegion > region) final
Sets a memory region.
Definition: Memory.cpp:64
size_t alignment() const
Return underlying's tensor buffer alignment.
ITensorInfo & set_is_resizable(bool is_resizable) override
Set the flag whether the tensor size can be changed.
Definition: TensorInfo.h:293
TensorInfo & info()
Return a reference to the tensor's metadata.
bool check_aligned(void *ptr, const size_t alignment)
Checks if a pointer complies with a given alignment.
Definition: Utility.h:192

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 NEFFTConvolutionLayer::run(), and arm_compute::test::validation::TEST_CASE().

◆ init() [1/2]

void init

Make ITensorAllocator's init methods available.

Definition at line 38 of file ITensorAllocator.cpp.

39 {
40  _info = input;
41  _alignment = alignment;
42 }
size_t alignment() const
Return underlying's tensor buffer alignment.

◆ init() [2/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 108 of file TensorAllocator.cpp.

109 {
110  // Get parent info
111  const TensorInfo parent_info = allocator.info();
112 
113  // Check if coordinates and new shape are within the parent tensor
114  ARM_COMPUTE_ERROR_ON(!validate_subtensor_shape(parent_info, sub_info, coords));
115  ARM_COMPUTE_UNUSED(validate_subtensor_shape);
116 
117  // Copy pointer to buffer
118  _memory = Memory(allocator._memory.region());
119 
120  // Init tensor info with new dimensions
121  size_t total_size = parent_info.offset_element_in_bytes(coords) + sub_info.total_size() - sub_info.offset_first_element_in_bytes();
122  sub_info.init(sub_info.tensor_shape(), sub_info.format(), parent_info.strides_in_bytes(), parent_info.offset_element_in_bytes(coords), total_size);
123 
124  // Set TensorInfo
125  init(sub_info);
126 }
CPU implementation of memory object.
Definition: Memory.h:37
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...
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
const Strides & strides_in_bytes() const override
The strides in bytes for accessing each dimension of the tensor.
Definition: TensorInfo.h:240
Format format() const override
Colour format of the image.
Definition: TensorInfo.h:269
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
size_t total_size() const override
Returns the total size of the tensor in bytes.
Definition: TensorInfo.h:273
input allocator() -> allocate()
void init(Format format)
Initialize the tensor info with just a format.
Definition: TensorInfo.cpp:107
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:394
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:244
Store the tensor's metadata.
Definition: TensorInfo.h:45
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:261

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 NEGEMMLowpAssemblyMatrixMultiplyCore::configure(), NEReductionOperation::configure(), NEFFT1D::configure(), NEHOGDescriptor::configure(), NEGaussian5x5::configure(), NEHOGGradient::configure(), NENormalizationLayer::configure(), CPPBoxWithNonMaximaSuppressionLimit::configure(), NERNNLayer::configure(), NESobel5x5::configure(), NESobel7x7::configure(), NEFastCorners::configure(), NEDepthwiseConvolutionAssemblyDispatch::configure(), NEScale::configure(), NELocallyConnectedLayer::configure(), NEWinogradConvolutionLayer::configure(), NECannyEdge::configure(), NEHarrisCorners::configure(), NEHOGMultiDetection::configure(), NEFFTConvolutionLayer::configure(), NEGEMM::configure(), NEGenerateProposalsLayer::configure(), NELSTMLayer::configure(), NEGEMMLowpMatrixMultiplyCore::configure(), NELSTMLayerQuantized::configure(), NEDeconvolutionLayer::configure(), NEGEMMConvolutionLayer::configure(), main(), NETensorHandle::NETensorHandle(), NumPyAccessor::NumPyAccessor(), and arm_compute::test::validation::TEST_CASE().

◆ 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 90 of file TensorAllocator.cpp.

91 {
92  if(&o != this)
93  {
94  _owner = o._owner;
95  o._owner = nullptr;
96 
97  _associated_memory_group = o._associated_memory_group;
98  o._associated_memory_group = nullptr;
99 
100  _memory = std::move(o._memory);
101  o._memory = Memory();
102 
103  ITensorAllocator::operator=(std::move(o));
104  }
105  return *this;
106 }
CPU implementation of memory object.
Definition: Memory.h:37
ITensorAllocator & operator=(const ITensorAllocator &)=default
Allow instances of this class to be copied.

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 166 of file TensorAllocator.cpp.

167 {
168  ARM_COMPUTE_ERROR_ON(associated_memory_group == nullptr);
169  ARM_COMPUTE_ERROR_ON(_associated_memory_group != nullptr && _associated_memory_group != associated_memory_group);
170  ARM_COMPUTE_ERROR_ON(_memory.region() != nullptr && _memory.region()->buffer() != nullptr);
171 
172  _associated_memory_group = associated_memory_group;
173 }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
IMemoryRegion * region() final
Region accessor.
Definition: Memory.cpp:48
virtual void * buffer()=0
Returns the pointer to the allocated data.

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

Referenced by Tensor::associate_memory_group().


The documentation for this class was generated from the following files: