ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::backend::basic::Tensor Class Reference

#include <Tensor.h>

Collaboration diagram for onert::backend::basic::Tensor:

Public Member Functions

 Tensor ()=delete
 
virtual ~Tensor ()
 
 Tensor (const ir::OperandInfo &info, DynamicMemoryManager *dynamic_mem_mgr)
 
void setBuffer (uint8_t *buffer)
 Set the Buffer object. This method is called for static and non-const tensor.
 
void setBuffer (const std::shared_ptr< Allocator > &alloc)
 Set the Buffer object. This method is called for dynamic or const tensor.
 
void deallocBuffer () override
 Reset the buffer and deallocate the allocation if it is managed by itself.
 
uint8_t * buffer () const override
 
void set_dynamic () override
 set this tensor dynamic
 
bool applyShape (const ir::Shape &new_shape) override
 Set the shape to shape and possibly re-allocate the buffer.
 
virtual void increase_ref ()
 
virtual void decrease_ref ()
 
virtual void reset_ref ()
 Reset reference count to zero and release data.
 
virtual int32_t num_references ()
 
void setShape (const ir::Shape &new_shape) override
 Set the shape of tenser to new_shape.
 
- Public Member Functions inherited from onert::backend::IPortableTensor
 IPortableTensor (const ir::OperandInfo &info)
 
virtual ~IPortableTensor ()
 
const ir::OperandInfoget_info () const
 
const ir::Sparsitysparsity () const
 
size_t total_size () const override final
 
size_t calcOffset (const ir::Coordinates &coords) const override final
 
ir::DataType data_type () const override final
 
float data_scale () const override final
 
int32_t data_zero_point () const override final
 
const std::vector< float > & data_scales () const override final
 
const std::vector< int32_t > & data_zero_points () const override
 
bool is_constant () const override final
 Return true if the tensor is constant.
 
bool is_dynamic () const override final
 Return true if the tensor needs dynamic allocation, meaning that during compile-time the outpus shape cannot be known and the output shape is calculated during kernel execution-time.
 
ir::Shape getShape () const override final
 Get ir::Shape of tensor.
 
bool has_padding () const final
 
void access (const std::function< void(ITensor &tensor)> &fn) final
 
- Public Member Functions inherited from onert::backend::ITensor
virtual ~ITensor ()
 
virtual bool is_subtensor () const
 
virtual bool needMemoryMap () const
 
virtual void enqueueWriteBuffer (const void *, bool)
 
virtual void enqueueReadBuffer (void *, bool)
 

Protected Attributes

uint8_t * _buffer
 
size_t _size
 
int32_t _num_references
 
DynamicMemoryManager_dynamic_mem_mgr
 
- Protected Attributes inherited from onert::backend::IPortableTensor
ir::OperandInfo _info
 

Detailed Description

Definition at line 36 of file Tensor.h.

Constructor & Destructor Documentation

◆ Tensor() [1/2]

onert::backend::basic::Tensor::Tensor ( )
delete

◆ ~Tensor()

Tensor::~Tensor ( )
virtual

Definition at line 29 of file Tensor.cc.

29{}

◆ Tensor() [2/2]

onert::backend::basic::Tensor::Tensor ( const ir::OperandInfo info,
DynamicMemoryManager dynamic_mem_mgr 
)
inline

Definition at line 43 of file Tensor.h.

44 : IPortableTensor(info), _buffer(nullptr), _size(info.total_size()), _num_references(0),
45 _dynamic_mem_mgr(dynamic_mem_mgr), _allocator(nullptr)
46 {
47 // DO NOTHING
48 }
IPortableTensor(const ir::OperandInfo &info)
DynamicMemoryManager * _dynamic_mem_mgr
Definition Tensor.h:131
volatile const char info[]

Member Function Documentation

◆ applyShape()

bool Tensor::applyShape ( const ir::Shape )
overridevirtual

Set the shape to shape and possibly re-allocate the buffer.

If a tensor is dynamic tensor and previously allocated memory exists, it will be deallocated. If a tensor is static tensor (with previously allocated memory by StaticTensorManager), buffer() will be overwriten

Parameters
shapetensor's new shape. While allocating memory for this new_shape, tensor's shape is set to new_shape
Returns
true If applying shape is successful
false If not applying shape is not supported (it throws for other errors)

Reimplemented from onert::backend::ITensor.

Reimplemented in onert::backend::train::Tensor.

Definition at line 33 of file Tensor.cc.

34{
35 if (_buffer != nullptr && new_shape == _info.shape())
36 return true;
37
38 // Always set shape - when buffer with same or larger size was already allocated, shape could
39 // differ
40 _info.shape(new_shape);
42 if (_buffer == nullptr || _size < _info.total_size())
43 {
44 assert(_dynamic_mem_mgr);
45 if (_allocator)
47
50 }
51
52 return true;
53}
std::shared_ptr< Allocator > allocate(const ITensor *tensor, uint32_t capacity)
void set_dynamic() override
set this tensor dynamic
Definition Tensor.h:74
void setBuffer(uint8_t *buffer)
Set the Buffer object. This method is called for static and non-const tensor.
Definition Tensor.h:56
size_t total_size() const
Return size of tensor (bytes)
const Shape & shape() const
Return tensor shape.
Definition OperandInfo.h:95

References _buffer, _dynamic_mem_mgr, onert::backend::IPortableTensor::_info, _size, onert::backend::basic::DynamicMemoryManager::allocate(), onert::backend::basic::DynamicMemoryManager::deallocate(), set_dynamic(), setBuffer(), onert::ir::OperandInfo::shape(), and onert::ir::OperandInfo::total_size().

◆ buffer()

uint8_t * onert::backend::basic::Tensor::buffer ( ) const
inlineoverridevirtual

◆ deallocBuffer()

void Tensor::deallocBuffer ( )
overridevirtual

Reset the buffer and deallocate the allocation if it is managed by itself.

Reimplemented from onert::backend::ITensor.

Definition at line 55 of file Tensor.cc.

56{
57 if (_allocator)
58 {
59 _buffer = nullptr;
60 _allocator.reset();
62 {
64 }
65 }
66}

References _buffer, _dynamic_mem_mgr, and onert::backend::basic::DynamicMemoryManager::deallocate().

◆ decrease_ref()

virtual void onert::backend::basic::Tensor::decrease_ref ( )
inlinevirtual

Reimplemented in onert::backend::basic::ExternalTensor.

Definition at line 86 of file Tensor.h.

87 {
88 assert(_buffer != nullptr || _allocator != nullptr);
89 assert(_num_references > 0);
91 // constant tensor and dynamic tensor has _allocator
92 if (_num_references == 0)
93 {
94 if (_buffer != nullptr)
95 _buffer = nullptr;
96 if (_allocator != nullptr)
97 {
98 _allocator->release();
99 _allocator = nullptr;
100 }
101 }
102 }

References _buffer, and _num_references.

◆ increase_ref()

virtual void onert::backend::basic::Tensor::increase_ref ( )
inlinevirtual

Reimplemented in onert::backend::basic::ExternalTensor.

Definition at line 77 of file Tensor.h.

78 {
79 assert(is_dynamic() ||
80 // when not dynamic
81 (_buffer != nullptr));
82
84 }
bool is_dynamic() const override final
Return true if the tensor needs dynamic allocation, meaning that during compile-time the outpus shape...

References _buffer, _num_references, and onert::backend::IPortableTensor::is_dynamic().

◆ num_references()

virtual int32_t onert::backend::basic::Tensor::num_references ( )
inlinevirtual

Reimplemented in onert::backend::basic::ExternalTensor.

Definition at line 123 of file Tensor.h.

123{ return _num_references; }

References _num_references.

◆ reset_ref()

virtual void onert::backend::basic::Tensor::reset_ref ( )
inlinevirtual

Reset reference count to zero and release data.

Reimplemented in onert::backend::basic::ExternalTensor.

Definition at line 107 of file Tensor.h.

108 {
109 assert(_buffer != nullptr || _allocator != nullptr);
110 assert(_num_references > 0);
111 _num_references = 0;
112
113 // Only constant tensor has allocator pointer
114 if (_buffer != nullptr)
115 _buffer = nullptr;
116 else
117 {
118 _allocator->release();
119 _allocator = nullptr;
120 }
121 }

References _buffer, and _num_references.

◆ set_dynamic()

void onert::backend::basic::Tensor::set_dynamic ( )
inlineoverridevirtual

set this tensor dynamic

Reimplemented from onert::backend::ITensor.

Definition at line 74 of file Tensor.h.

References onert::backend::IPortableTensor::_info, and onert::ir::OperandInfo::setDynamic().

Referenced by applyShape().

◆ setBuffer() [1/2]

void onert::backend::basic::Tensor::setBuffer ( const std::shared_ptr< Allocator > &  alloc)
inline

Set the Buffer object. This method is called for dynamic or const tensor.

Definition at line 61 of file Tensor.h.

62 {
63 _allocator = alloc;
64 _buffer = alloc->base();
65 }

References _buffer.

◆ setBuffer() [2/2]

void onert::backend::basic::Tensor::setBuffer ( uint8_t *  buffer)
inline

Set the Buffer object. This method is called for static and non-const tensor.

Definition at line 56 of file Tensor.h.

56{ _buffer = buffer; }
uint8_t * buffer() const override
Definition Tensor.h:73

References _buffer, and buffer().

Referenced by applyShape(), and onert::backend::basic::train::TrainableTensor::setBuffer().

◆ setShape()

void Tensor::setShape ( const ir::Shape )
overridevirtual

Set the shape of tenser to new_shape.

Note
Higer dimension will be placed on front.

Reimplemented from onert::backend::ITensor.

Definition at line 31 of file Tensor.cc.

31{ _info.shape(new_shape); }

References onert::backend::IPortableTensor::_info, and onert::ir::OperandInfo::shape().

Field Documentation

◆ _buffer

◆ _dynamic_mem_mgr

DynamicMemoryManager* onert::backend::basic::Tensor::_dynamic_mem_mgr
protected

Definition at line 131 of file Tensor.h.

Referenced by applyShape(), and deallocBuffer().

◆ _num_references

◆ _size

size_t onert::backend::basic::Tensor::_size
protected

Definition at line 129 of file Tensor.h.

Referenced by applyShape().


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