Mila
Deep Neural Network Library
|
Public Types | |
using | MR = TMemoryResource |
using | scalar_t = std::variant< int64_t, int32_t, half, float > |
Public Member Functions | |
Tensor () | |
Tensor (const std::vector< size_t > &shape, std::shared_ptr< TElementType > data_ptr) | |
Constructs a tensor with the given shape and a shared pointer to allocated memory. | |
Tensor (const std::vector< size_t > &shape, TElementType value=TElementType{}) | |
Constructs a tensor with the given shape and initializes it with the specified value. | |
Tensor (const Tensor &other) | |
Copy constructor (creates a shallow copy). | |
Tensor (Tensor &&other) noexcept | |
Move constructor. | |
~Tensor ()=default | |
TElementType | at (const std::vector< size_t > &indices) const |
Tensor< TElementType, TMemoryResource > | clone () const |
Creates a deep copy of this tensor. | |
template<typename SrcMemoryResource > | |
void | copyFrom (const Tensor< TElementType, SrcMemoryResource > &src) |
Copies data from another tensor into this tensor. | |
auto | data () |
Gets a pointer to the tensor data with memory type safety. | |
auto | data () const |
Gets a const pointer to the tensor data with memory type safety. | |
const bool | empty () const |
void | fill (const TElementType &value) |
Tensor< TElementType, TMemoryResource > & | flatten () |
Flattens the tensor to a 2D tensor by combining all dimensions except the last one. | |
Tensor< TElementType, TMemoryResource > | flattened () const |
Creates a flattened copy of this tensor. | |
std::string | get_uid () const |
std::string | getBufferString (size_t start_index=0, size_t depth=0) const |
std::string | getName () const |
Tensor & | operator= (const Tensor &other) |
Copy assignment operator. | |
Tensor & | operator= (Tensor &&other) noexcept |
Move assignment operator. | |
template<typename... Args> | |
TElementType & | operator[] (Args... args) |
template<typename... Args> | |
const TElementType & | operator[] (Args... args) const |
size_t | rank () const |
TElementType * | raw_data () |
Gets a raw pointer to the tensor data for use in CUDA kernels. | |
const TElementType * | raw_data () const |
Gets a const raw pointer to the tensor data for use in CUDA kernels. | |
void | reshape (const std::vector< size_t > &new_shape) |
void | set (const std::vector< size_t > &indices, TElementType value) |
void | setName (const std::string &value) |
const std::vector< size_t > & | shape () const |
size_t | size () const |
const std::vector< size_t > & | strides () const |
template<typename TNewMR > | |
Tensor< TElementType, TNewMR > | to () const |
Converts this tensor to use a different memory resource. | |
template<typename T = TElementType, typename TMR = TMemoryResource> | |
std::enable_if_t< std::is_same_v< T, half >, Tensor< float, TMR > > | toFloat () const |
Converts a half precision tensor to float. | |
template<typename T = TElementType, typename TMR = TMemoryResource> | |
std::enable_if_t< std::is_same_v< T, float >, Tensor< half, TMR > > | toHalf () const |
Converts a float tensor to half precision. | |
template<typename HostAccessibleMR = Compute::HostMemoryResource> | |
Tensor< TElementType, HostAccessibleMR > | toHostAccessible () const |
std::string | toString (bool showBuffer=false) const |
Static Public Member Functions | |
template<typename MR = TMemoryResource> | |
static constexpr bool | is_device_accessible () |
template<typename MR = TMemoryResource> | |
static constexpr bool | is_host_accessible () |
Private Member Functions | |
void | allocateBuffer (TElementType value) |
size_t | computeIndex (const std::vector< size_t > &indices) const |
std::string | outputBuffer (size_t index, size_t depth) const |
std::string | set_uid () |
void | validateIndices (const std::vector< size_t > &indices, const std::string &method_name) const |
Static Private Member Functions | |
static size_t | computeSize (const std::vector< size_t > &shape) |
static std::vector< size_t > | computeStrides (const std::vector< size_t > &shape) |
Private Attributes | |
std::shared_ptr< TensorBuffer< TElementType, TMemoryResource > > | buffer_ { nullptr } |
std::shared_ptr< TElementType > | external_memory_ptr_ { nullptr } |
std::string | name_ |
std::vector< size_t > | shape_ {} |
size_t | size_ { 0 } |
std::vector< size_t > | strides_ {} |
std::string | uid_ |
Friends | |
std::ostream & | operator<< (std::ostream &os, const Tensor &tensor) |
using Mila::Dnn::Tensor< TElementType, TMemoryResource >::MR = TMemoryResource |
using Mila::Dnn::Tensor< TElementType, TMemoryResource >::scalar_t = std::variant<int64_t, int32_t, half, float> |
|
inlineexplicit |
Constructs a tensor with the given shape and initializes it with the specified value.
This constructor initializes the tensor with the provided shape and fills it with the given value. If no value is provided, the tensor is initialized with the default value of the type TElementType.
shape | The shape of the tensor. |
value | The value to initialize the tensor with. Defaults to the default value of type TElementType. |
|
inline |
Constructs a tensor with the given shape and a shared pointer to allocated memory.
This constructor initializes the tensor with the provided shape and uses the given shared pointer to allocated memory. The tensor does not take ownership of the memory.
shape | The shape of the tensor. |
data_ptr | Shared pointer to the allocated memory. |
|
inline |
|
inline |
Copy constructor (creates a shallow copy).
This constructor creates a new tensor that shares the underlying data buffer with the original tensor. Modifications to one tensor's data will affect the other. For a deep, independent copy, use the clone() method instead.
other | The tensor to copy from. |
|
inlinenoexcept |
Move constructor.
This constructor moves the contents of the given tensor to this tensor.
other | The tensor to move from. |
|
default |
|
inlineprivate |
|
inline |
|
inline |
Creates a deep copy of this tensor.
Unlike the copy constructor which shares the underlying buffer, this method creates a completely independent copy with its own data buffer.
|
inlineprivate |
|
inlinestaticprivate |
|
inlinestaticprivate |
|
inline |
Copies data from another tensor into this tensor.
This method copies the contents of the source tensor to this tensor. Both tensors must have the same shape. The data is copied using the appropriate memory transfer mechanism based on the source and destination memory resource types.
SrcMemoryResource | The memory resource type of the source tensor. |
src | The source tensor to copy data from. |
std::runtime_error | If the shapes don't match or if a CUDA memory transfer operation fails. |
|
inline |
Gets a pointer to the tensor data with memory type safety.
Returns a memory-type-aware pointer that prevents unsafe host access to device memory at compile time.
|
inline |
Gets a const pointer to the tensor data with memory type safety.
Returns a memory-type-aware pointer that prevents unsafe host access to device memory at compile time.
|
inline |
|
inline |
|
inline |
Flattens the tensor to a 2D tensor by combining all dimensions except the last one.
This method reshapes the tensor from [D1, D2, ..., Dn-1, Dn] to [D1*D2*...*Dn-1, Dn]. This is particularly useful for operations that treat all but the last dimension as batch dimensions, such as Fully Connected operations.
|
inline |
Creates a flattened copy of this tensor.
Similar to flatten(), but returns a new tensor instead of modifying this one.
|
inline |
|
inline |
|
inline |
|
inlinestaticconstexpr |
|
inlinestaticconstexpr |
|
inline |
Copy assignment operator.
This operator copies the contents of the given tensor to this tensor.
other | The tensor to copy from. |
|
inlinenoexcept |
Move assignment operator.
This operator moves the contents of the given tensor to this tensor.
other | The tensor to move from. |
|
inline |
|
inline |
|
inlineprivate |
|
inline |
|
inline |
Gets a raw pointer to the tensor data for use in CUDA kernels.
This method is intended for internal use by CUDA operation implementations. Use with caution as it bypasses memory type safety.
|
inline |
Gets a const raw pointer to the tensor data for use in CUDA kernels.
This method is intended for internal use by CUDA operation implementations. Use with caution as it bypasses memory type safety.
|
inline |
|
inline |
|
inlineprivate |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Converts this tensor to use a different memory resource.
This method creates a new tensor with the same shape and data but using a different memory resource. The data is copied from the current tensor to the new tensor using the appropriate memory transfer mechanism based on the source and destination memory resource types.
TNewMR | The target memory resource type. |
std::runtime_error | If a CUDA memory transfer operation fails. |
|
inline |
Converts a half precision tensor to float.
This method creates a new tensor with the same shape but with float precision elements. The conversion is performed in a type-safe manner, handling both host and device memory appropriately.
T | The original element type (must be half) |
TMR | The memory resource type |
std::runtime_error | If the source tensor is not of half type or if a CUDA operation fails |
|
inline |
Converts a float tensor to half precision.
This method creates a new tensor with the same shape but with half precision elements. The conversion is performed in a type-safe manner, handling both host and device memory appropriately.
T | The original element type (must be float) |
TMR | The memory resource type |
std::runtime_error | If the source tensor is not of float type or if a CUDA operation fails |
|
inline |
|
inline |
|
inlineprivate |
|
friend |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |