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

Device-aware N-dimensional tensor. More...

Inheritance diagram for Mila::Dnn::Tensor< TDataType, TMemoryResource >:
Mila::Dnn::ITensor

Public Types

using DataType = TensorDataType
 Abstract data type enumeration.
using DataTypeTraits = TensorDataTypeTraits<TDataType>
 Compile-time data type characteristics.
using host_value_t = std::conditional_t<TensorDataTypeTraits<TDataType>::is_integer_type, int32_t, float>
 Host value type for scalars.
using MemoryResource = TMemoryResource
 Memory resource type for this tensor.

Public Member Functions

 Tensor (Compute::DeviceId device_id, const shape_t &shape, std::string name={})
 Creates a tensor bound to the specified device id and shape.
 Tensor (const Tensor &other)=delete
 Copy constructor - explicitly deleted for performance safety.
 Tensor (Tensor &&other) noexcept
 Efficiently transfers ownership from another tensor.
 ~Tensor ()=default
 Destructor with automatic resource cleanup via RAII.
constexpr const auto * data () const noexcept
 Returns type-safe immutable pointer to tensor data with concrete host type.
constexpr auto * data () noexcept
 Returns type-safe pointer to tensor data with concrete host type.
size_t elementSize () const override
 Returns the size in bytes of a single tensor element.
bool empty () const
 Checks if the tensor contains no elements.
Tensor< TDataType, TMemoryResource > & flatten ()
 Flattens tensor to 2D shape in-place.
TensorDataType getDataType () const override
 Returns the tensor's abstract data type identifier.
std::string getDataTypeName () const override
 Returns human-readable name of the tensor's data type.
Compute::DeviceId getDeviceId () const override
Compute::DeviceType getDeviceType () const override
 Returns the device type of this tensor's memory resource.
Compute::MemoryResourcegetMemoryResource () const override
 Returns pointer to the memory resource managing this tensor's storage.
std::string getName () const override
 Returns the tensor's optional user-assigned name.
size_t getStorageSize () const override
 Returns the total storage size in bytes backing the tensor's buffer.
std::string getUId () const override
 Returns the tensor's unique identifier.
bool isScalar () const noexcept override
 Checks if tensor is a scalar (0-dimensional).
bool isValid () const noexcept
 Check if tensor is in a valid state (not moved-from).
bool isView () const override
 Check if this tensor is a view of another tensor's data.
auto & item ()
 Gets the scalar value for 0-dimensional tensors.
const auto & item () const
 Gets the scalar value for 0-dimensional tensors (const version).
Tensoroperator= (const Tensor &other)=delete
 Copy assignment operator - explicitly deleted.
Tensoroperator= (Tensor &&other) noexcept
 Efficiently moves resources from another tensor.
auto & operator[] (const index_t &indices)
 Accesses tensor element using multi-dimensional indices.
const auto & operator[] (const index_t &indices) const
 Accesses tensor element using multi-dimensional indices (const version).
template<typename... Indices>
requires TMemoryResource::is_host_accessible
auto & operator[] (Indices... indices) &&(std
 Variadic index access (non-const).
template<typename... Indices>
requires TMemoryResource::is_host_accessible
const auto & operator[] (Indices... indices) const &&(std
 Variadic index access (const).
size_t rank () const
 Returns the number of dimensions in the tensor.
const void * rawData () const override
 Returns raw pointer to tensor data (const version).
void * rawData () override
 Returns raw pointer to tensor data (implements ITensor protected API).
void reshape (const shape_t &new_shape)
 Modifies tensor shape while preserving total element count.
void setName (const std::string &value)
 Assigns a descriptive name to the tensor.
const shape_tshape () const override
 Returns the tensor's dimensional shape vector.
dim_t size () const override
 Returns the total number of logical elements in the tensor.
const stride_tstrides () const
 Returns the tensor's memory stride information.
std::string toString (bool showBuffer=false) const
 Generates string representation of the tensor.
Tensor view (const shape_t &new_shape, dim_t offset=0) const
Public Member Functions inherited from Mila::Dnn::ITensor
template<typename T>
bool isType () const
 Type-safe check for tensor element type.

Static Public Member Functions

static constexpr bool is_device_accessible ()
 Checks if tensor data is accessible from device (GPU) code.
static constexpr bool is_host_accessible ()
 Checks if tensor data is accessible from host (CPU) code.

Detailed Description

template<TensorDataType TDataType, typename TMemoryResource>
requires isValidTensor<TDataType, TMemoryResource>
class Mila::Dnn::Tensor< TDataType, TMemoryResource >

Device-aware N-dimensional tensor.

Move-only tensor parameterized by an abstract TensorDataType and a MemoryResource. Scalars are represented by an empty shape ({}), which yields size() == 1.

Template Parameters
TDataTypeAbstract tensor data type from TensorDataType enumeration
TMemoryResourceMemory resource type defining storage location and access patterns

Constructor & Destructor Documentation

◆ Tensor() [1/3]

template<TensorDataType TDataType, typename TMemoryResource>
Mila::Dnn::Tensor< TDataType, TMemoryResource >::Tensor ( Compute::DeviceId device_id,
const shape_t & shape,
std::string name = {} )
inlineexplicit

Creates a tensor bound to the specified device id and shape.

Constructs a tensor using a DeviceId. Empty shape {} creates a scalar (0D tensor) with size 1. Shape containing a zero produces an empty tensor.

Parameters
device_idDevice identifier (type + index)
shapeVector defining the size of each dimension in row-major order
nameOptional tensor name for diagnostics (default: empty)
Exceptions
std::invalid_argumentIf device id is invalid (checked by validateDeviceId)
std::runtime_errorIf device type doesn't match memory resource
std::bad_allocIf memory allocation fails

◆ Tensor() [2/3]

template<TensorDataType TDataType, typename TMemoryResource>
Mila::Dnn::Tensor< TDataType, TMemoryResource >::Tensor ( const Tensor< TDataType, TMemoryResource > & other)
delete

Copy constructor - explicitly deleted for performance safety.

Use clone() for deep copies or std::move() for ownership transfer.

◆ Tensor() [3/3]

template<TensorDataType TDataType, typename TMemoryResource>
Mila::Dnn::Tensor< TDataType, TMemoryResource >::Tensor ( Tensor< TDataType, TMemoryResource > && other)
inlinenoexcept

Efficiently transfers ownership from another tensor.

Moves resources from the source tensor, leaving it in a moved-from state.

Member Function Documentation

◆ data()

template<TensorDataType TDataType, typename TMemoryResource>
auto * Mila::Dnn::Tensor< TDataType, TMemoryResource >::data ( )
inlinenodiscardconstexprnoexcept

Returns type-safe pointer to tensor data with concrete host type.

Only available for host-accessible memory resources.

◆ elementSize()

template<TensorDataType TDataType, typename TMemoryResource>
size_t Mila::Dnn::Tensor< TDataType, TMemoryResource >::elementSize ( ) const
inlineoverridevirtual

Returns the size in bytes of a single tensor element.

Implements Mila::Dnn::ITensor.

◆ empty()

template<TensorDataType TDataType, typename TMemoryResource>
bool Mila::Dnn::Tensor< TDataType, TMemoryResource >::empty ( ) const
inline

Checks if the tensor contains no elements.

Scalars are NOT empty (size == 1). Empty tensors have size == 0.

◆ flatten()

template<TensorDataType TDataType, typename TMemoryResource>
Tensor< TDataType, TMemoryResource > & Mila::Dnn::Tensor< TDataType, TMemoryResource >::flatten ( )
inline

Flattens tensor to 2D shape in-place.

No-op for scalars and vectors.

◆ getDataType()

template<TensorDataType TDataType, typename TMemoryResource>
TensorDataType Mila::Dnn::Tensor< TDataType, TMemoryResource >::getDataType ( ) const
inlineoverridevirtual

Returns the tensor's abstract data type identifier.

Implements Mila::Dnn::ITensor.

◆ getDataTypeName()

template<TensorDataType TDataType, typename TMemoryResource>
std::string Mila::Dnn::Tensor< TDataType, TMemoryResource >::getDataTypeName ( ) const
inlineoverridevirtual

Returns human-readable name of the tensor's data type.

Implements Mila::Dnn::ITensor.

◆ getDeviceId()

template<TensorDataType TDataType, typename TMemoryResource>
Compute::DeviceId Mila::Dnn::Tensor< TDataType, TMemoryResource >::getDeviceId ( ) const
inlineoverridevirtual

Implements Mila::Dnn::ITensor.

◆ getDeviceType()

template<TensorDataType TDataType, typename TMemoryResource>
Compute::DeviceType Mila::Dnn::Tensor< TDataType, TMemoryResource >::getDeviceType ( ) const
inlineoverridevirtual

Returns the device type of this tensor's memory resource.

Equivalent to TMemoryResource::device_type.

Implements Mila::Dnn::ITensor.

◆ getMemoryResource()

template<TensorDataType TDataType, typename TMemoryResource>
Compute::MemoryResource * Mila::Dnn::Tensor< TDataType, TMemoryResource >::getMemoryResource ( ) const
inlineoverridevirtual

Returns pointer to the memory resource managing this tensor's storage.

Implements Mila::Dnn::ITensor.

◆ getName()

template<TensorDataType TDataType, typename TMemoryResource>
std::string Mila::Dnn::Tensor< TDataType, TMemoryResource >::getName ( ) const
inlineoverridevirtual

Returns the tensor's optional user-assigned name.

Implements Mila::Dnn::ITensor.

◆ getStorageSize()

template<TensorDataType TDataType, typename TMemoryResource>
size_t Mila::Dnn::Tensor< TDataType, TMemoryResource >::getStorageSize ( ) const
inlineoverridevirtual

Returns the total storage size in bytes backing the tensor's buffer.

Forwards to the authoritative TensorBuffer::storageBytes() value and returns 0 when no buffer is allocated.

Implements Mila::Dnn::ITensor.

◆ getUId()

template<TensorDataType TDataType, typename TMemoryResource>
std::string Mila::Dnn::Tensor< TDataType, TMemoryResource >::getUId ( ) const
inlineoverridevirtual

Returns the tensor's unique identifier.

Implements Mila::Dnn::ITensor.

◆ isScalar()

template<TensorDataType TDataType, typename TMemoryResource>
bool Mila::Dnn::Tensor< TDataType, TMemoryResource >::isScalar ( ) const
inlineoverridevirtualnoexcept

Checks if tensor is a scalar (0-dimensional).

Scalars: shape {} and size() == 1.

Implements Mila::Dnn::ITensor.

◆ isView()

template<TensorDataType TDataType, typename TMemoryResource>
bool Mila::Dnn::Tensor< TDataType, TMemoryResource >::isView ( ) const
inlineoverridevirtual

Check if this tensor is a view of another tensor's data.

Returns
true if this is a view, false if it owns its buffer.

Implements Mila::Dnn::ITensor.

◆ item() [1/2]

template<TensorDataType TDataType, typename TMemoryResource>
auto & Mila::Dnn::Tensor< TDataType, TMemoryResource >::item ( )
inline

Gets the scalar value for 0-dimensional tensors.

Only available for host-accessible memory resources.

Exceptions
std::runtime_errorIf tensor is not a scalar (rank != 0)

◆ item() [2/2]

template<TensorDataType TDataType, typename TMemoryResource>
const auto & Mila::Dnn::Tensor< TDataType, TMemoryResource >::item ( ) const
inline

Gets the scalar value for 0-dimensional tensors (const version).

Only available for host-accessible memory resources.

Exceptions
std::runtime_errorIf tensor is not a scalar (rank != 0)

◆ operator=() [1/2]

template<TensorDataType TDataType, typename TMemoryResource>
Tensor & Mila::Dnn::Tensor< TDataType, TMemoryResource >::operator= ( const Tensor< TDataType, TMemoryResource > & other)
delete

Copy assignment operator - explicitly deleted.

Use clone() for deep copies or std::move() for transfers.

◆ operator=() [2/2]

template<TensorDataType TDataType, typename TMemoryResource>
Tensor & Mila::Dnn::Tensor< TDataType, TMemoryResource >::operator= ( Tensor< TDataType, TMemoryResource > && other)
inlinenoexcept

Efficiently moves resources from another tensor.

Self-assignment safe implementation.

◆ operator[]()

template<TensorDataType TDataType, typename TMemoryResource>
auto & Mila::Dnn::Tensor< TDataType, TMemoryResource >::operator[] ( const index_t & indices)
inline

Accesses tensor element using multi-dimensional indices.

Only available for host-accessible memory resources.

◆ rawData() [1/2]

template<TensorDataType TDataType, typename TMemoryResource>
const void * Mila::Dnn::Tensor< TDataType, TMemoryResource >::rawData ( ) const
inlineoverridevirtual

Returns raw pointer to tensor data (const version).

Implements Mila::Dnn::ITensor.

◆ rawData() [2/2]

template<TensorDataType TDataType, typename TMemoryResource>
void * Mila::Dnn::Tensor< TDataType, TMemoryResource >::rawData ( )
inlineoverridevirtual

Returns raw pointer to tensor data (implements ITensor protected API).

Implements Mila::Dnn::ITensor.

◆ reshape()

template<TensorDataType TDataType, typename TMemoryResource>
void Mila::Dnn::Tensor< TDataType, TMemoryResource >::reshape ( const shape_t & new_shape)
inline

Modifies tensor shape while preserving total element count.

The new shape must have the same total number of elements as the current shape, unless the tensor is empty.

◆ setName()

template<TensorDataType TDataType, typename TMemoryResource>
void Mila::Dnn::Tensor< TDataType, TMemoryResource >::setName ( const std::string & value)
inline

Assigns a descriptive name to the tensor.

Names must be non-empty strings.

◆ shape()

template<TensorDataType TDataType, typename TMemoryResource>
const shape_t & Mila::Dnn::Tensor< TDataType, TMemoryResource >::shape ( ) const
inlineoverridevirtual

Returns the tensor's dimensional shape vector.

Implements Mila::Dnn::ITensor.

◆ size()

template<TensorDataType TDataType, typename TMemoryResource>
dim_t Mila::Dnn::Tensor< TDataType, TMemoryResource >::size ( ) const
inlineoverridevirtual

Returns the total number of logical elements in the tensor.

Implements Mila::Dnn::ITensor.

◆ toString()

template<TensorDataType TDataType, typename TMemoryResource>
std::string Mila::Dnn::Tensor< TDataType, TMemoryResource >::toString ( bool showBuffer = false) const
inline

Generates string representation of the tensor.

Includes UID, name, shape, data type and device information.


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