|
Mila 0.13.48
Deep Neural Network Library
|
Device-agnostic buffer for storing tensor data with abstract type system. More...
Public Types | |
| using | DataType = TensorDataType |
| Abstract data type enumeration. | |
| using | DataTypeTraits = TensorDataTypeTraits<TDataType> |
| Compile-time data type characteristics. | |
Public Member Functions | |
| TensorBuffer (const TensorBuffer &)=delete | |
| Copy operations explicitly deleted for performance safety. | |
| TensorBuffer (int device_id, size_t logical_size) | |
| Constructs buffer with owned memory. | |
| TensorBuffer (TensorBuffer &&other) noexcept | |
| Move constructor for efficient ownership transfer. | |
| ~TensorBuffer () | |
| Destructor with automatic memory cleanup via RAII. | |
| size_t | alignedSize () const noexcept |
| Returns the aligned memory allocation size in bytes. | |
| const void * | data () const noexcept |
| Returns const raw pointer to buffer data. | |
| void * | data () noexcept |
| Returns raw pointer to buffer data. | |
| bool | empty () const noexcept |
| Checks if the buffer is empty. | |
| Compute::MemoryResource * | getMemoryResource () const noexcept |
| Returns pointer to the memory resource managing this buffer's storage. | |
| bool | isAligned () const noexcept |
| Checks if buffer memory is properly aligned. | |
| TensorBuffer & | operator= (const TensorBuffer &)=delete |
| TensorBuffer & | operator= (TensorBuffer &&other) noexcept |
| Move assignment operator for efficient ownership transfer. | |
| void | resize (size_t new_logical_size) |
| Resizes buffer WITHOUT preserving existing data. | |
| size_t | size () const noexcept |
| Returns the number of logical elements in the buffer. | |
| size_t | storageBytes () const noexcept |
| Returns the storage size in bytes. | |
Static Public Attributes | |
| static constexpr size_t | alignment = Detail::get_alignment<TDataType, TMemoryResource>() |
| Optimal memory alignment. | |
| static constexpr TensorDataType | data_type = TDataType |
| Compile-time data type constant. | |
| static constexpr size_t | element_size = DataTypeTraits::size_in_bytes |
| Storage size per element. | |
| static constexpr bool | is_device_only = DataTypeTraits::is_device_only |
| Device-only type restriction. | |
| static constexpr bool | is_float_type = DataTypeTraits::is_float_type |
| Floating-point type classification. | |
| static constexpr bool | is_integer_type = DataTypeTraits::is_integer_type |
| Integer type classification. | |
Private Member Functions | |
| size_t | calculateAlignedSize (size_t storage_bytes) const noexcept |
| Calculates aligned size for given storage requirements. | |
| std::unique_ptr< Compute::MemoryResource > | createMemoryResource () |
| Creates appropriate memory resource with device context. | |
| void | logAllocation () const |
| Logs memory allocation for tracking and profiling. | |
| void | logDeallocation () const |
| Logs memory deallocation for tracking and profiling. | |
Private Attributes | |
| size_t | aligned_size_ { 0 } |
| Total allocated bytes including alignment. | |
| std::byte * | data_ { nullptr } |
| Pointer to allocated memory buffer. | |
| int | device_id_ { -1 } |
| Device Id for memory resource operations. | |
| size_t | logical_size_ { 0 } |
| Number of logical elements in buffer. | |
| std::unique_ptr< Compute::MemoryResource > | mr_ { nullptr } |
| Memory resource for allocation (null for external). | |
| size_t | storage_bytes_ { 0 } |
| Actual storage bytes. | |
Device-agnostic buffer for storing tensor data with abstract type system.
Advanced memory management container providing efficient storage for tensor data across heterogeneous compute environments using abstract TensorDataType enumeration. Supports device-specific alignment optimization and automatic compatibility validation.
Core architectural principles:
The buffer supports both owned memory management and external memory wrapping, enabling integration with existing memory pools and external libraries while maintaining optimal performance characteristics.
| TDataType | Abstract tensor data type from TensorDataType enumeration |
| TMemoryResource | Memory resource type determining allocation strategy and device targeting |
| TrackMemory | When true, enables detailed memory allocation tracking and profiling |
Example usage:
| using Mila::Dnn::TensorBuffer< TDataType, TMemoryResource, TrackMemory >::DataType = TensorDataType |
Abstract data type enumeration.
| using Mila::Dnn::TensorBuffer< TDataType, TMemoryResource, TrackMemory >::DataTypeTraits = TensorDataTypeTraits<TDataType> |
Compile-time data type characteristics.
|
inlineexplicit |
Constructs buffer with owned memory.
Allocates optimally aligned memory using the specified memory resource and initializes all memory to zero for deterministic behavior. Memory alignment is optimized based on data type and target hardware.
| device_context | Device context for memory resource initialization |
| logical_size | Number of logical elements to store in the buffer |
| std::invalid_argument | If device_context is null |
| std::overflow_error | If size causes overflow in storage calculations |
| std::bad_alloc | If memory allocation fails |
| std::runtime_error | If memory resource operations fail |


|
inline |
Destructor with automatic memory cleanup via RAII.
Automatically deallocates owned memory through the memory resource. External memory is not deallocated, maintaining safe resource management. Provides optional deallocation tracking for memory profiling.

|
delete |
Copy operations explicitly deleted for performance safety.
Prevents accidental expensive copy operations involving large memory transfers across different memory spaces and devices.

|
inlinenoexcept |
Move constructor for efficient ownership transfer.
Transfers all resources from source buffer without memory copying, leaving source in valid but empty state.

|
inlinenoexcept |
Returns the aligned memory allocation size in bytes.
This represents the actual memory allocated, which includes alignment padding for optimal hardware performance.
|
inlineprivatenoexcept |
Calculates aligned size for given storage requirements.
Ensures memory allocations meet hardware alignment requirements for optimal performance on target devices.

|
inlineprivate |
Creates appropriate memory resource with device context.
Instantiates memory resource with device context and optional allocation tracking wrapper for debugging and profiling purposes.

|
inlinenoexcept |
Returns const raw pointer to buffer data.
Provides read-only access to the underlying memory buffer.
|
inlinenoexcept |
Returns raw pointer to buffer data.
Provides direct access to the underlying memory buffer for performance-critical operations and device kernel interfacing.

|
inlinenoexcept |
Checks if the buffer is empty.
|
inlinenoexcept |
Returns pointer to the memory resource managing this buffer's storage.
Provides access to the memory resource for efficient dispatch optimization, zero-copy operations when memory resources are compatible, and type-safe downcasting to specific memory resource types.
Example:
|
inlinenoexcept |
Checks if buffer memory is properly aligned.
Verifies that the buffer's data pointer meets the alignment requirements for optimal performance on the target hardware.
|
inlineprivate |
Logs memory allocation for tracking and profiling.


|
inlineprivate |
Logs memory deallocation for tracking and profiling.


|
delete |

|
inlinenoexcept |
Move assignment operator for efficient ownership transfer.
Safely transfers ownership while properly cleaning up existing resources.

|
inline |
Resizes buffer WITHOUT preserving existing data.
Allocates new optimally aligned memory. Existing data is DISCARDED. Use for buffer reuse scenarios where data preservation is not required.
| new_logical_size | New number of logical elements |
| std::runtime_error | If buffer uses external memory |
| std::overflow_error | If new size causes storage overflow |
| std::bad_alloc | If memory allocation fails |

|
inlinenoexcept |
Returns the number of logical elements in the buffer.
|
inlinenoexcept |
Returns the storage size in bytes.
Provides the actual memory storage used.

|
private |
Total allocated bytes including alignment.
|
staticconstexpr |
Optimal memory alignment.
|
private |
Pointer to allocated memory buffer.
|
staticconstexpr |
Compile-time data type constant.
|
private |
Device Id for memory resource operations.
|
staticconstexpr |
Storage size per element.
|
staticconstexpr |
Device-only type restriction.
|
staticconstexpr |
Floating-point type classification.
|
staticconstexpr |
Integer type classification.
|
private |
Number of logical elements in buffer.
|
private |
Memory resource for allocation (null for external).
|
private |
Actual storage bytes.