ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
|
A multi-dimensional array of elements of a single data type. More...
#include <tensor.h>
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 TensorShape & | shape () 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 > | |
T * | data () const |
Get a typed pointer to the data buffer of the tensor. | |
const TensorBuffer & | buffer () 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> | |
T & | get_value (Indices... indices) const |
Get the element at the specified indices. | |
template<typename T > | |
T * | inner_most_ptr (const int &index) const |
Get the pointer to the specified row. | |
bool | operator== (const Tensor &other) const |
Equality comparison operator for tensors. | |
Tensor & | operator= (const Tensor &other) |
Assignment operator overload for the Tensor class. | |
Tensor & | operator= (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::Allocator * | GetAllocator (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. | |
TensorBuffer * | buffer_ {} |
The TensorBuffer object that holds the data of the tensor. | |
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.
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.
|
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.
data_type | The data type of the tensor elements. |
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.
data_type | The data type of the tensor. |
shape | The shape of the tensor. |
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.
data_type | The data type of the tensor. |
shape | The shape of the tensor. |
device | The data type of the tensor. |
container::Tensor::Tensor | ( | base::core::Allocator * | a, |
DataType | data_type, | ||
DeviceType | device, | ||
const TensorShape & | shape | ||
) |
container::Tensor::Tensor | ( | const 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.
other | The rvalue reference to the source Tensor object to be moved. |
container::Tensor::~Tensor | ( | ) |
|
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.
T | The data type of the elements in the initializer list. |
|
delete |
|
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.
T | The data type of the elements. |
N | The number of dimensions. |
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.
other | The source Tensor from which data will be copied. |
shape | The TensorShape specifying the shape of the newly allocated memory. |
const TensorBuffer & container::Tensor::buffer | ( | ) | const |
Get the TensorBuffer object that holds the data of the tensor.
|
inlineprotected |
Calculates the linear index corresponding to the given indices.
Indices | The types of the indices. |
indices | The indices to calculate the linear index from. |
Method to transform data from a given tensor object to the output tensor with a given data type.
T | The data type of the returned tensor. |
|
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.
DEVICE | The device type from which the data will be copied. |
T | The data type of the elements being copied. |
data | Pointer to the data array in the device memory that needs to be copied. |
num_elements | 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
.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. 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.
other | The source Tensor from which data will be copied. |
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.
other | The source Tensor from which data will be copied. |
shape | The desired TensorShape for the current tensor after reshaping. |
other.shape()
matches the number of elements in the given shape
.
|
inlineprotected |
void * container::Tensor::data | ( | ) | const |
Get a pointer to the data buffer of the tensor.
Get a typed pointer to the data buffer of the tensor.
T | The data type of the pointer to return. |
T
must match the data type of the tensor. If T
does not match the data type of the tensor, the behavior is undefined.DataType container::Tensor::data_type | ( | ) | const |
Get the data type of the tensor.
DeviceType container::Tensor::device_type | ( | ) | const |
Get the data type of the tensor.
|
inline |
Get the element at the specified indices.
indices | A vector of integers representing the indices of the element. |
|
static |
Get the Allocator object according to the given device type.
device | The device type. |
Get the pointer to the specified row.
row | The row index. |
int64_t container::Tensor::NumElements | ( | ) | const |
Get the total number of elements in the tensor.
|
inlineexplicit |
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.
other | The source Tensor object whose values will be assigned. |
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.
other | The rvalue reference to the source Tensor object to be moved. |
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.
other | The tensor to compare with. |
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.
index | The index to access. Should be within valid bounds. |
void container::Tensor::reshape | ( | TensorShape | shape | ) |
Set all elements in current tensor object to zero.
shape | The new shape of the tensor. |
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.
new_shape | The new shape of the tensor. |
const TensorShape & container::Tensor::shape | ( | ) | const |
Get the shape of the tensor.
Tensor container::Tensor::shaped | ( | const TensorShape & | shape | ) | const |
Set all elements in current tensor object to zero.
shape | The new shape of the tensor. |
|
inlinestatic |
Returns the size of a single element for a given data type.
data_type | The data type. |
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.
start | A vector of integers representing the starting indices of the slice. |
size | A vector of integers representing the size of the slice along each dimension. |
void container::Tensor::sync | ( | const Tensor & | rhs | ) |
|
inline |
Method to transform data from a given tensor object to the output tensor with a given device type.
DEVICE | The device type of the returned tensor. |
void container::Tensor::zero | ( | ) |
Set all elements in current tensor object to zero.
|
protected |
The TensorBuffer object that holds the data of the tensor.
|
protected |
The data type of the tensor.
|
protected |
The device type of the tensor.
|
protected |
The shape of the tensor.