Mila
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::Tensor< TElementType, TMemoryResource > Class Template Referenceexport
module Dnn.Tensor

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
 
Tensoroperator= (const Tensor &other)
 Copy assignment operator.
 
Tensoroperator= (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)
 

Member Typedef Documentation

◆ MR

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
using Mila::Dnn::Tensor< TElementType, TMemoryResource >::MR = TMemoryResource

◆ scalar_t

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
using Mila::Dnn::Tensor< TElementType, TMemoryResource >::scalar_t = std::variant<int64_t, int32_t, half, float>

Constructor & Destructor Documentation

◆ Tensor() [1/5]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Mila::Dnn::Tensor< TElementType, TMemoryResource >::Tensor ( const std::vector< size_t > &  shape,
TElementType  value = TElementType{} 
)
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.

Parameters
shapeThe shape of the tensor.
valueThe value to initialize the tensor with. Defaults to the default value of type TElementType.

◆ Tensor() [2/5]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Mila::Dnn::Tensor< TElementType, TMemoryResource >::Tensor ( const std::vector< size_t > &  shape,
std::shared_ptr< TElementType >  data_ptr 
)
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.

Parameters
shapeThe shape of the tensor.
data_ptrShared pointer to the allocated memory.

◆ Tensor() [3/5]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Mila::Dnn::Tensor< TElementType, TMemoryResource >::Tensor ( )
inline
Here is the call graph for this function:

◆ Tensor() [4/5]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Mila::Dnn::Tensor< TElementType, TMemoryResource >::Tensor ( const Tensor< TElementType, TMemoryResource > &  other)
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.

Parameters
otherThe tensor to copy from.

◆ Tensor() [5/5]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Mila::Dnn::Tensor< TElementType, TMemoryResource >::Tensor ( Tensor< TElementType, TMemoryResource > &&  other)
inlinenoexcept

Move constructor.

This constructor moves the contents of the given tensor to this tensor.

Parameters
otherThe tensor to move from.

◆ ~Tensor()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Mila::Dnn::Tensor< TElementType, TMemoryResource >::~Tensor ( )
default

Member Function Documentation

◆ allocateBuffer()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
void Mila::Dnn::Tensor< TElementType, TMemoryResource >::allocateBuffer ( TElementType  value)
inlineprivate
Here is the caller graph for this function:

◆ at()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
TElementType Mila::Dnn::Tensor< TElementType, TMemoryResource >::at ( const std::vector< size_t > &  indices) const
inline
Here is the call graph for this function:

◆ clone()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Tensor< TElementType, TMemoryResource > Mila::Dnn::Tensor< TElementType, TMemoryResource >::clone ( ) const
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.

Returns
Tensor<TElementType, TMemoryResource> A deep copy of this tensor
Here is the call graph for this function:

◆ computeIndex()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
size_t Mila::Dnn::Tensor< TElementType, TMemoryResource >::computeIndex ( const std::vector< size_t > &  indices) const
inlineprivate
Here is the caller graph for this function:

◆ computeSize()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
static size_t Mila::Dnn::Tensor< TElementType, TMemoryResource >::computeSize ( const std::vector< size_t > &  shape)
inlinestaticprivate
Here is the call graph for this function:
Here is the caller graph for this function:

◆ computeStrides()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
static std::vector< size_t > Mila::Dnn::Tensor< TElementType, TMemoryResource >::computeStrides ( const std::vector< size_t > &  shape)
inlinestaticprivate
Here is the call graph for this function:
Here is the caller graph for this function:

◆ copyFrom()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
template<typename SrcMemoryResource >
void Mila::Dnn::Tensor< TElementType, TMemoryResource >::copyFrom ( const Tensor< TElementType, SrcMemoryResource > &  src)
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.

Template Parameters
SrcMemoryResourceThe memory resource type of the source tensor.
Parameters
srcThe source tensor to copy data from.
Exceptions
std::runtime_errorIf the shapes don't match or if a CUDA memory transfer operation fails.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ data() [1/2]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
auto Mila::Dnn::Tensor< TElementType, TMemoryResource >::data ( )
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.

Returns
A TensorPtr wrapper that enforces memory access safety
Here is the caller graph for this function:

◆ data() [2/2]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
auto Mila::Dnn::Tensor< TElementType, TMemoryResource >::data ( ) const
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.

Returns
A const TensorPtr wrapper that enforces memory access safety

◆ empty()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
const bool Mila::Dnn::Tensor< TElementType, TMemoryResource >::empty ( ) const
inline
Here is the caller graph for this function:

◆ fill()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
void Mila::Dnn::Tensor< TElementType, TMemoryResource >::fill ( const TElementType &  value)
inline

◆ flatten()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Tensor< TElementType, TMemoryResource > & Mila::Dnn::Tensor< TElementType, TMemoryResource >::flatten ( )
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.

Returns
A reference to this tensor after flattening.
Here is the call graph for this function:

◆ flattened()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Tensor< TElementType, TMemoryResource > Mila::Dnn::Tensor< TElementType, TMemoryResource >::flattened ( ) const
inline

Creates a flattened copy of this tensor.

Similar to flatten(), but returns a new tensor instead of modifying this one.

Returns
A new tensor with flattened shape.
Here is the call graph for this function:

◆ get_uid()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::string Mila::Dnn::Tensor< TElementType, TMemoryResource >::get_uid ( ) const
inline

◆ getBufferString()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::string Mila::Dnn::Tensor< TElementType, TMemoryResource >::getBufferString ( size_t  start_index = 0,
size_t  depth = 0 
) const
inline
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getName()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::string Mila::Dnn::Tensor< TElementType, TMemoryResource >::getName ( ) const
inline
Here is the caller graph for this function:

◆ is_device_accessible()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
template<typename MR = TMemoryResource>
static constexpr bool Mila::Dnn::Tensor< TElementType, TMemoryResource >::is_device_accessible ( )
inlinestaticconstexpr

◆ is_host_accessible()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
template<typename MR = TMemoryResource>
static constexpr bool Mila::Dnn::Tensor< TElementType, TMemoryResource >::is_host_accessible ( )
inlinestaticconstexpr

◆ operator=() [1/2]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Tensor & Mila::Dnn::Tensor< TElementType, TMemoryResource >::operator= ( const Tensor< TElementType, TMemoryResource > &  other)
inline

Copy assignment operator.

This operator copies the contents of the given tensor to this tensor.

Parameters
otherThe tensor to copy from.
Returns
Tensor& A reference to this tensor.

◆ operator=() [2/2]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
Tensor & Mila::Dnn::Tensor< TElementType, TMemoryResource >::operator= ( Tensor< TElementType, TMemoryResource > &&  other)
inlinenoexcept

Move assignment operator.

This operator moves the contents of the given tensor to this tensor.

Parameters
otherThe tensor to move from.
Returns
Tensor& A reference to this tensor.

◆ operator[]() [1/2]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
template<typename... Args>
TElementType & Mila::Dnn::Tensor< TElementType, TMemoryResource >::operator[] ( Args...  args)
inline
Here is the call graph for this function:

◆ operator[]() [2/2]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
template<typename... Args>
const TElementType & Mila::Dnn::Tensor< TElementType, TMemoryResource >::operator[] ( Args...  args) const
inline
Here is the call graph for this function:

◆ outputBuffer()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::string Mila::Dnn::Tensor< TElementType, TMemoryResource >::outputBuffer ( size_t  index,
size_t  depth 
) const
inlineprivate
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rank()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
size_t Mila::Dnn::Tensor< TElementType, TMemoryResource >::rank ( ) const
inline
Here is the caller graph for this function:

◆ raw_data() [1/2]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
TElementType * Mila::Dnn::Tensor< TElementType, TMemoryResource >::raw_data ( )
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.

Returns
Raw pointer to the tensor data
Here is the caller graph for this function:

◆ raw_data() [2/2]

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
const TElementType * Mila::Dnn::Tensor< TElementType, TMemoryResource >::raw_data ( ) const
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.

Returns
Const raw pointer to the tensor data

◆ reshape()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
void Mila::Dnn::Tensor< TElementType, TMemoryResource >::reshape ( const std::vector< size_t > &  new_shape)
inline
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
void Mila::Dnn::Tensor< TElementType, TMemoryResource >::set ( const std::vector< size_t > &  indices,
TElementType  value 
)
inline
Here is the call graph for this function:

◆ set_uid()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::string Mila::Dnn::Tensor< TElementType, TMemoryResource >::set_uid ( )
inlineprivate
Here is the call graph for this function:

◆ setName()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
void Mila::Dnn::Tensor< TElementType, TMemoryResource >::setName ( const std::string &  value)
inline
Here is the caller graph for this function:

◆ shape()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
const std::vector< size_t > & Mila::Dnn::Tensor< TElementType, TMemoryResource >::shape ( ) const
inline
Here is the caller graph for this function:

◆ size()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
size_t Mila::Dnn::Tensor< TElementType, TMemoryResource >::size ( ) const
inline
Here is the caller graph for this function:

◆ strides()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
const std::vector< size_t > & Mila::Dnn::Tensor< TElementType, TMemoryResource >::strides ( ) const
inline
Here is the caller graph for this function:

◆ to()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
template<typename TNewMR >
Tensor< TElementType, TNewMR > Mila::Dnn::Tensor< TElementType, TMemoryResource >::to ( ) const
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.

Template Parameters
TNewMRThe target memory resource type.
Returns
Tensor<TElementType, TNewMR> A new tensor with the specified memory resource type.
Exceptions
std::runtime_errorIf a CUDA memory transfer operation fails.
Here is the call graph for this function:

◆ toFloat()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
template<typename T = TElementType, typename TMR = TMemoryResource>
std::enable_if_t< std::is_same_v< T, half >, Tensor< float, TMR > > Mila::Dnn::Tensor< TElementType, TMemoryResource >::toFloat ( ) const
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.

Template Parameters
TThe original element type (must be half)
TMRThe memory resource type
Returns
Tensor<float, TMR> A new tensor with float precision values
Exceptions
std::runtime_errorIf the source tensor is not of half type or if a CUDA operation fails
Here is the call graph for this function:

◆ toHalf()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
template<typename T = TElementType, typename TMR = TMemoryResource>
std::enable_if_t< std::is_same_v< T, float >, Tensor< half, TMR > > Mila::Dnn::Tensor< TElementType, TMemoryResource >::toHalf ( ) const
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.

Template Parameters
TThe original element type (must be float)
TMRThe memory resource type
Returns
Tensor<half, TMR> A new tensor with half precision values
Exceptions
std::runtime_errorIf the source tensor is not of float type or if a CUDA operation fails
Here is the call graph for this function:

◆ toHostAccessible()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
template<typename HostAccessibleMR = Compute::HostMemoryResource>
Tensor< TElementType, HostAccessibleMR > Mila::Dnn::Tensor< TElementType, TMemoryResource >::toHostAccessible ( ) const
inline

◆ toString()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::string Mila::Dnn::Tensor< TElementType, TMemoryResource >::toString ( bool  showBuffer = false) const
inline
Here is the call graph for this function:

◆ validateIndices()

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
void Mila::Dnn::Tensor< TElementType, TMemoryResource >::validateIndices ( const std::vector< size_t > &  indices,
const std::string &  method_name 
) const
inlineprivate
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator<<

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::ostream & operator<< ( std::ostream &  os,
const Tensor< TElementType, TMemoryResource > &  tensor 
)
friend

Member Data Documentation

◆ buffer_

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::shared_ptr<TensorBuffer<TElementType, TMemoryResource> > Mila::Dnn::Tensor< TElementType, TMemoryResource >::buffer_ { nullptr }
private

◆ external_memory_ptr_

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::shared_ptr<TElementType> Mila::Dnn::Tensor< TElementType, TMemoryResource >::external_memory_ptr_ { nullptr }
private

◆ name_

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::string Mila::Dnn::Tensor< TElementType, TMemoryResource >::name_
private

◆ shape_

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::vector<size_t> Mila::Dnn::Tensor< TElementType, TMemoryResource >::shape_ {}
private

◆ size_

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
size_t Mila::Dnn::Tensor< TElementType, TMemoryResource >::size_ { 0 }
private

◆ strides_

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::vector<size_t> Mila::Dnn::Tensor< TElementType, TMemoryResource >::strides_ {}
private

◆ uid_

template<typename TElementType , typename TMemoryResource = Compute::CudaMemoryResource>
std::string Mila::Dnn::Tensor< TElementType, TMemoryResource >::uid_
private

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