ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
container::Tensor Class Reference

A multi-dimensional array of elements of a single data type. More...

#include <tensor.h>

Inheritance diagram for container::Tensor:
Collaboration diagram for container::Tensor:

Public Member Functions

 Tensor ()
 Creates a 1-dimentional, 0-element float tensor.
 
 Tensor (DataType data_type)
 Explicit constructor for the Tensor class.
 
 Tensor (DataType data_type, const TensorShape &shape)
 Constructor that creates a tensor with the given data type and shape using the default allocator.
 
 Tensor (DataType data_type, DeviceType device, const TensorShape &shape)
 Construct a new Tensor object with the given data type, shape and device type.
 
 Tensor (base::core::Allocator *a, DataType data_type, DeviceType device, const TensorShape &shape)
 
 Tensor (const Tensor &other)
 Construct a new Tensor object by copying another Tensor.
 
 Tensor (Tensor &&other) noexcept
 Move constructor for the Tensor class.
 
 ~Tensor ()
 
template<typename T >
 Tensor (std::initializer_list< T > values, DeviceType device=DeviceType::CpuDevice)
 Constructor for the Tensor class using an initializer list of values.
 
DataType data_type () const
 Get the data type of the tensor.
 
DeviceType device_type () const
 Get the data type of the tensor.
 
const TensorShapeshape () const
 Get the shape of the tensor.
 
int64_t NumElements () const
 Get the total number of elements in the tensor.
 
void * data () const
 Get a pointer to the data buffer of the tensor.
 
template<typename T >
Tdata () const
 Get a typed pointer to the data buffer of the tensor.
 
const TensorBufferbuffer () const
 Get the TensorBuffer object that holds the data of the tensor.
 
template<typename DEVICE >
Tensor to_device () const
 Method to transform data from a given tensor object to the output tensor with a given device type.
 
template<typename DEVICE , typename T >
void copy_from_device (const T *data, int64_t num_elements=-1)
 Copies data from a given device to the current tensor object.
 
template<typename T >
Tensor cast () const
 Method to transform data from a given tensor object to the output tensor with a given data type.
 
void zero ()
 Set all elements in current tensor object to zero.
 
void reshape (TensorShape shape)
 Set all elements in current tensor object to zero.
 
Tensor shaped (const TensorShape &shape) const
 Set all elements in current tensor object to zero.
 
Tensor slice (const std::vector< int > &start, const std::vector< int > &size) const
 Return a new Tensor slice starting at the specified indices with the given size.
 
void resize (const TensorShape &new_shape)
 Resize the tensor to the new shape.
 
template<typename T , typename... Indices>
Tget_value (Indices... indices) const
 Get the element at the specified indices.
 
template<typename T >
Tinner_most_ptr (const int &index) const
 Get the pointer to the specified row.
 
bool operator== (const Tensor &other) const
 Equality comparison operator for tensors.
 
Tensoroperator= (const Tensor &other)
 Assignment operator overload for the Tensor class.
 
Tensoroperator= (Tensor &&other) noexcept
 Move assignment operator overload for the Tensor class.
 
bool CopyFrom (const Tensor &other)
 Copy the data from another tensor into this tensor.
 
bool CopyFrom (const Tensor &other, const TensorShape &shape)
 Copy the data from another tensor into this tensor while reshaping it.
 
bool AllocateFrom (const Tensor &other, const TensorShape &shape)
 Copies data from another Tensor with memory allocation and specified shape.
 
template<typename T , size_t N, typename index_t = int64_t>
TensorAccessor< T, N, index_t > accessor () const &
 Accessor function for a multi-dimensional tensor.
 
template<typename T , size_t N, typename index_t = int>
TensorAccessor< T, N, index_t > accessor () &&=delete
 
void sync (const Tensor &rhs)
 Synchronize the current Tensor with another Tensor.
 
Tensor operator[] (const int &index) const
 Access a sub-Tensor based on an index.
 
 operator bool () const
 
template<typename T >
void set_value (T value)
 

Static Public Member Functions

static size_t SizeOfType (DataType data_type)
 Returns the size of a single element for a given data type.
 
static base::core::AllocatorGetAllocator (DeviceType device)
 Get the Allocator object according to the given device type.
 

Protected Member Functions

template<typename... Indices>
size_t calculateLinearIndex (Indices... indices) const
 Calculates the linear index corresponding to the given indices.
 
void CopyFromInternal (const Tensor &other, const TensorShape &shape)
 

Protected Attributes

DataType data_type_
 The data type of the tensor.
 
DeviceType device_
 The device type of the tensor.
 
TensorShape shape_
 The shape of the tensor.
 
TensorBufferbuffer_ {}
 The TensorBuffer object that holds the data of the tensor.
 

Detailed Description

A multi-dimensional array of elements of a single data type.

This class represents a Tensor, which is a fundamental concept in container. A Tensor has a data type, shape, and memory buffer that stores the actual data.

This class is not thread-safe and should not be accessed by multiple threads concurrently.

Constructor & Destructor Documentation

◆ Tensor() [1/8]

container::Tensor::Tensor ( )

Creates a 1-dimentional, 0-element float tensor.

This constructor creates a new Tensor object. It can be used to initialize a tensor with default values or to create an empty tensor.

◆ Tensor() [2/8]

container::Tensor::Tensor ( DataType  data_type)
explicit

Explicit constructor for the Tensor class.

This constructor creates a new Tensor object with the specified data type. The constructor is marked as explicit, which means it requires an explicit call and cannot be used for implicit type conversions.

Parameters
data_typeThe data type of the tensor elements.

◆ Tensor() [3/8]

container::Tensor::Tensor ( DataType  data_type,
const TensorShape shape 
)

Constructor that creates a tensor with the given data type and shape using the default allocator.

Parameters
data_typeThe data type of the tensor.
shapeThe shape of the tensor.

◆ Tensor() [4/8]

container::Tensor::Tensor ( DataType  data_type,
DeviceType  device,
const TensorShape shape 
)

Construct a new Tensor object with the given data type, shape and device type.

The memory for the tensor is allocated according to the given device type.

Parameters
data_typeThe data type of the tensor.
shapeThe shape of the tensor.
deviceThe data type of the tensor.

◆ Tensor() [5/8]

container::Tensor::Tensor ( base::core::Allocator a,
DataType  data_type,
DeviceType  device,
const TensorShape shape 
)

◆ Tensor() [6/8]

container::Tensor::Tensor ( const Tensor other)

Construct a new Tensor object by copying another Tensor.

This constructor performs a deep copy of the data buffer of the other tensor.

Parameters
otherThe tensor to copy from.
Here is the call graph for this function:

◆ Tensor() [7/8]

container::Tensor::Tensor ( Tensor &&  other)
noexcept

Move constructor for the Tensor class.

This constructor is used to move the contents and ownership of another Tensor object into the newly created object using move semantics. The source Tensor's resources will be taken over, and the source object will be left in a valid but unspecified state.

Parameters
otherThe rvalue reference to the source Tensor object to be moved.

◆ ~Tensor()

container::Tensor::~Tensor ( )
Here is the call graph for this function:

◆ Tensor() [8/8]

template<typename T >
container::Tensor::Tensor ( std::initializer_list< T values,
DeviceType  device = DeviceType::CpuDevice 
)
inline

Constructor for the Tensor class using an initializer list of values.

This constructor creates a Tensor object with the specified data type and device type, using the values provided in the initializer list. The Tensor's shape is automatically determined based on the size of the initializer list.

Template Parameters
TThe data type of the elements in the initializer list.
Parameters
valuesThe initializer list containing the values to populate the Tensor with.
deviceThe device type where the Tensor will be allocated (default is CPU).

Member Function Documentation

◆ accessor() [1/2]

template<typename T , size_t N, typename index_t = int>
TensorAccessor< T, N, index_t > container::Tensor::accessor ( ) &&
delete

◆ accessor() [2/2]

template<typename T , size_t N, typename index_t = int64_t>
TensorAccessor< T, N, index_t > container::Tensor::accessor ( ) const &
inline

Accessor function for a multi-dimensional tensor.

This function provides read-only access to the data of a tensor with a specific rank. It performs checks to ensure that the rank of the tensor matches the rank of the accessor.

Template Parameters
TThe data type of the elements.
NThe number of dimensions.
Returns
A TensorAccessor object for accessing the tensor's data.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AllocateFrom()

bool container::Tensor::AllocateFrom ( const Tensor other,
const TensorShape shape 
)

Copies data from another Tensor with memory allocation and specified shape.

This function copies data from another Tensor while also allocating memory for the current Tensor based on the given shape. The data type and device of the current Tensor are set to match those of the source Tensor. The previous memory buffer, if any, is deallocated.

Parameters
otherThe source Tensor from which data will be copied.
shapeThe TensorShape specifying the shape of the newly allocated memory.
Returns
Returns true if the copy and allocation were successful, false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buffer()

const TensorBuffer & container::Tensor::buffer ( ) const

Get the TensorBuffer object that holds the data of the tensor.

Returns
The TensorBuffer object that holds the data of the tensor.
Here is the caller graph for this function:

◆ calculateLinearIndex()

template<typename... Indices>
size_t container::Tensor::calculateLinearIndex ( Indices...  indices) const
inlineprotected

Calculates the linear index corresponding to the given indices.

Template Parameters
IndicesThe types of the indices.
Parameters
indicesThe indices to calculate the linear index from.
Returns
The calculated linear index.
Note
This function assumes that the provided indices are valid and within the bounds of the tensor's shape. It calculates the linear index by iterating over the dimensions of the tensor in reverse order and multiplying each index by the corresponding stride.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cast()

template<typename T >
Tensor container::Tensor::cast ( ) const
inline

Method to transform data from a given tensor object to the output tensor with a given data type.

Template Parameters
TThe data type of the returned tensor.
Returns
Tensor A tensor object with data transformed to the output tensor
Here is the caller graph for this function:

◆ copy_from_device()

template<typename DEVICE , typename T >
void container::Tensor::copy_from_device ( const T data,
int64_t  num_elements = -1 
)
inline

Copies data from a given device to the current tensor object.

This function is designed to copy a given number of elements from a device-specific memory location to the memory associated with this object. It ensures that the size of the data being copied does not exceed the size of the destination tensor.

Template Parameters
DEVICEThe device type from which the data will be copied.
TThe data type of the elements being copied.
Parameters
dataPointer to the data array in the device memory that needs to be copied.
num_elementsThe number of elements to copy.
Precondition
The number of elements to copy (num_elements) must be less than or equal to the number of elements in the destination tensor (this->shape_.num_elements()). If this condition is not met, the function will trigger an error through REQUIRES_OK.
Note
The function uses a template specialization TEMPLATE_CZ_2 to handle the copying of memory based on the data type T and the device type DEVICE. It utilizes the kernels::cast_memory method to perform the actual memory copy operation.
Here is the call graph for this function:

◆ CopyFrom() [1/2]

bool container::Tensor::CopyFrom ( const Tensor other)

Copy the data from another tensor into this tensor.

This function copies the data from another tensor into the current tensor. The underlying storage of the current tensor will be shared with the source tensor.

Parameters
otherThe source Tensor from which data will be copied.
Returns
Returns true if the copy was successful, false otherwise.
Note
The current tensor will share the same underlying storage as the source tensor.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CopyFrom() [2/2]

bool container::Tensor::CopyFrom ( const Tensor other,
const TensorShape shape 
)

Copy the data from another tensor into this tensor while reshaping it.

This function copies the data from another tensor into the current tensor, and also reshapes the current tensor to match the specified shape. The underlying storage of the current tensor will be shared with the source tensor.

Parameters
otherThe source Tensor from which data will be copied.
shapeThe desired TensorShape for the current tensor after reshaping.
Returns
Returns true if the copy and reshaping were successful, false otherwise.
Note
The current tensor will share the same underlying storage as the source tensor.
The function returns true if the number of elements in other.shape() matches the number of elements in the given shape.
Here is the call graph for this function:

◆ CopyFromInternal()

void container::Tensor::CopyFromInternal ( const Tensor other,
const TensorShape shape 
)
inlineprotected
Here is the call graph for this function:
Here is the caller graph for this function:

◆ data() [1/2]

void * container::Tensor::data ( ) const

Get a pointer to the data buffer of the tensor.

Returns
A void pointer to the data buffer of the tensor.
Here is the call graph for this function:

◆ data() [2/2]

template<typename T >
T * container::Tensor::data ( ) const
inline

Get a typed pointer to the data buffer of the tensor.

Template Parameters
TThe data type of the pointer to return.
Returns
A typed pointer to the data buffer of the tensor.
Note
The template parameter T must match the data type of the tensor. If T does not match the data type of the tensor, the behavior is undefined.
This function returns a pointer to the first element in the data buffer of the tensor. If the tensor is empty, the behavior is undefined.
Here is the call graph for this function:

◆ data_type()

DataType container::Tensor::data_type ( ) const

Get the data type of the tensor.

Returns
The data type of the tensor.
Here is the caller graph for this function:

◆ device_type()

DeviceType container::Tensor::device_type ( ) const

Get the data type of the tensor.

Returns
The data type of the tensor.
Here is the caller graph for this function:

◆ get_value()

template<typename T , typename... Indices>
T & container::Tensor::get_value ( Indices...  indices) const
inline

Get the element at the specified indices.

Parameters
indicesA vector of integers representing the indices of the element.
Returns
The element at the specified indices.
Note
The number of indices must match the number of dimensions of the tensor. If the indices are out of bounds, the behavior is undefined.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetAllocator()

base::core::Allocator * container::Tensor::GetAllocator ( DeviceType  device)
static

Get the Allocator object according to the given device type.

Parameters
deviceThe device type.
Returns
The related Allocator class pointer.
Here is the caller graph for this function:

◆ inner_most_ptr()

template<typename T >
T * container::Tensor::inner_most_ptr ( const int &  index) const
inline

Get the pointer to the specified row.

Parameters
rowThe row index.
Returns
The pointer to the specified row.
Note
This function assumes the tensor is treated as a matrix, where each row is a contiguous block of memory. If the row index is out of bounds, the behavior is undefined.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ NumElements()

int64_t container::Tensor::NumElements ( ) const

Get the total number of elements in the tensor.

Returns
The total number of elements in the tensor.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator bool()

container::Tensor::operator bool ( ) const
inlineexplicit
Here is the call graph for this function:

◆ operator=() [1/2]

Tensor & container::Tensor::operator= ( const Tensor other)

Assignment operator overload for the Tensor class.

This operator is used to assign the values of another Tensor object to the current object. It performs a deep copy of the data from the source Tensor to the destination Tensor.

Parameters
otherThe source Tensor object whose values will be assigned.
Returns
A reference to the current Tensor object after the assignment.
Here is the call graph for this function:

◆ operator=() [2/2]

Tensor & container::Tensor::operator= ( Tensor &&  other)
noexcept

Move assignment operator overload for the Tensor class.

This operator is used to move the contents and ownership of another Tensor object into the current object using move semantics. The source Tensor's resources will be taken over, and the source object will be left in a valid but unspecified state.

Parameters
otherThe rvalue reference to the source Tensor object to be moved.
Returns
A reference to the current Tensor object after the move assignment.
Note
This function is declared as noexcept, indicating that it does not throw exceptions.

◆ operator==()

bool container::Tensor::operator== ( const Tensor other) const

Equality comparison operator for tensors.

Compares the current tensor with another tensor for equality. Returns true if the data type, device type, and shape of the two tensors match, and the data elements are equal.

Parameters
otherThe tensor to compare with.
Returns
True if the tensors are equal, otherwise false.
Here is the call graph for this function:

◆ operator[]()

Tensor container::Tensor::operator[] ( const int &  index) const

Access a sub-Tensor based on an index.

This operator allows you to access a sub-Tensor based on the provided index.

Parameters
indexThe index to access. Should be within valid bounds.
Returns
A sub-Tensor with the specified index.
Here is the call graph for this function:

◆ reshape()

void container::Tensor::reshape ( TensorShape  shape)

Set all elements in current tensor object to zero.

Parameters
shapeThe new shape of the tensor.
Note
There can be one -1 dimension in the input shape, indicates the auto reshape.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ resize()

void container::Tensor::resize ( const TensorShape new_shape)

Resize the tensor to the new shape.

The tensor will be resized to the new shape, and its data buffer will be reallocated if necessary. If the new shape has a different number of elements than the current shape, the data buffer will be reallocated. If the new shape has the same number of elements as the current shape, the data buffer will not be reallocated, but the shape of the tensor will be updated.

Parameters
new_shapeThe new shape of the tensor.
Note
This method will automatically zero the resized tensor object.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_value()

template<typename T >
void container::Tensor::set_value ( T  value)
inline
Here is the call graph for this function:
Here is the caller graph for this function:

◆ shape()

const TensorShape & container::Tensor::shape ( ) const

Get the shape of the tensor.

Returns
The shape of the tensor.

◆ shaped()

Tensor container::Tensor::shaped ( const TensorShape shape) const

Set all elements in current tensor object to zero.

Parameters
shapeThe new shape of the tensor.
Note
There can be one -1 dimension in the input shape, indicates the auto reshape.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SizeOfType()

static size_t container::Tensor::SizeOfType ( DataType  data_type)
inlinestatic

Returns the size of a single element for a given data type.

Parameters
data_typeThe data type.
Returns
The size of a single element.
Note
This function is not thread-safe.
If an unsupported data type is passed in, an error message will be printed and the program will exit.
This function returns the size in bytes of a single element of the given data type. If an unsupported data type is provided, the function outputs an error message and exits the program. Supported data types: DT_FLOAT: 4 bytes DT_INT32: 4 bytes DT_DOUBLE: 8 bytes DT_COMPLEX: 8 bytes (2 floats) DT_COMPLEX_DOUBLE: 16 bytes (2 doubles)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ slice()

Tensor container::Tensor::slice ( const std::vector< int > &  start,
const std::vector< int > &  size 
) const

Return a new Tensor slice starting at the specified indices with the given size.

Parameters
startA vector of integers representing the starting indices of the slice.
sizeA vector of integers representing the size of the slice along each dimension.
Returns
A new Tensor slice.
Note
Currently, this method only supports tensors with a ndim of less than or equal to 3.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sync()

void container::Tensor::sync ( const Tensor rhs)

Synchronize the current Tensor with another Tensor.

This function ensures that the current Tensor's data is synchronized with another Tensor.

Parameters
rhsThe Tensor to synchronize with.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_device()

template<typename DEVICE >
Tensor container::Tensor::to_device ( ) const
inline

Method to transform data from a given tensor object to the output tensor with a given device type.

Template Parameters
DEVICEThe device type of the returned tensor.
Returns
Tensor A tensor object with data transformed to the output tensor
Here is the caller graph for this function:

◆ zero()

void container::Tensor::zero ( )

Set all elements in current tensor object to zero.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ buffer_

TensorBuffer* container::Tensor::buffer_ {}
protected

The TensorBuffer object that holds the data of the tensor.

◆ data_type_

DataType container::Tensor::data_type_
protected

The data type of the tensor.

◆ device_

DeviceType container::Tensor::device_
protected

The device type of the tensor.

◆ shape_

TensorShape container::Tensor::shape_
protected

The shape of the tensor.


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