DenseTensorStorage
DenseTensorStorage
Tensor storage backend using a flat std::vector
#include <DenseTensorStorage.hpp>
Inherits from TensorStorageBase< DenseTensorStorage >
Public Functions
| Name | |
|---|---|
| DenseTensorStorage(std::vector< float > data, std::vector< std::size_t > shape) Construct from flat data and shape. |
|
| DenseTensorStorage(std::vector< std::size_t > shape) Construct an uninitialized tensor with the given shape. |
|
| std::span< float > | mutableFlatData() Get mutable access to the flat data buffer. |
| void | setValueAt(std::span< std::size_t const > indices, float value) Set a single value by multi-dimensional index. |
| void | appendRow(std::span< float const > row_data) Append a row to the end of the tensor. |
| void | insertRow(std::size_t index, std::span< float const > row_data) Insert a row at a specific position. |
| void | setRow(std::size_t index, std::span< float const > row_data) Overwrite an existing row’s data in-place. |
| std::size_t | ndim() const Get the number of dimensions. |
| std::vector< std::size_t > const & | strides() const Get the precomputed row-major strides. |
| float | getValueAtImpl(std::span< std::size_t const > indices) const |
| std::span< float const > | flatDataImpl() const |
| std::vector< float > | sliceAlongAxisImpl(std::size_t axis, std::size_t index) const |
| std::vector< float > | getColumnImpl(std::size_t col) const |
| std::vector< std::size_t > | shapeImpl() const |
| std::size_t | totalElementsImpl() const |
| bool | isContiguousImpl() const |
| TensorStorageType | getStorageTypeImpl() const |
| TensorStorageCache | tryGetCacheImpl() const |
Additional inherited members
Public Functions inherited from TensorStorageBase< DenseTensorStorage >
| Name | |
|---|---|
| float | getValueAt(std::span< std::size_t const > indices) const Get a single float value by multi-dimensional index. |
| std::span< float const > | flatData() const Get a span over the flat (row-major) data. |
| std::vector< float > | sliceAlongAxis(std::size_t axis, std::size_t index) const Extract a sub-tensor by fixing one axis to a single index. |
| std::vector< float > | getColumn(std::size_t col) const Get a single column (axis 1 slice) across all rows (axis 0). |
| std::vector< std::size_t > | shape() const Get the shape as a vector of sizes (one per axis). |
| std::size_t | totalElements() const Get the total number of elements (product of shape). |
| bool | isContiguous() const Whether the underlying data is contiguous in memory. |
| TensorStorageType | getStorageType() const Get the storage type identifier. |
| TensorStorageCache | tryGetCache() const Try to get cached pointers for fast-path access. |
Protected Functions inherited from TensorStorageBase< DenseTensorStorage >
| Name | |
|---|---|
| ~TensorStorageBase() =default |
Detailed Description
class DenseTensorStorage;Tensor storage backend using a flat std::vector
This is the fallback storage backend for tensors with >3 dimensions, where Armadillo’s arma::fcube maximum of 3D does not suffice. It has no external dependencies.
Primary use cases:
3D model I/O with LibTorch (batch × channel × height × width)
- Materialization target when converting between backends of different dimensionality
- Storage for
[TensorData::createND()](/api_generated/Classes/classTensorData.md#function-creatend)when ndim > 3
For ≤3D data, prefer [ArmadilloTensorStorage](/api_generated/Classes/classArmadilloTensorStorage.md) since it provides zero-copy mlpack interop.
Layout
Data is stored in row-major (C-contiguous) order. For a tensor with shape [d0, d1, d2, d3], the element at index (i0, i1, i2, i3) is at flat offset: i0 * stride[0] + i1 * stride[1] + i2 * stride[2] + i3 * stride[3]
Strides are precomputed at construction time: stride[k] = product of shape[k+1..N-1] stride[N-1] = 1
This matches LibTorch’s default C-contiguous layout, making conversion trivial (just copy the flat buffer).
Public Functions Documentation
function DenseTensorStorage
DenseTensorStorage(
std::vector< float > data,
std::vector< std::size_t > shape
)Construct from flat data and shape.
Parameters:
- data Flat float data in row-major order
- shape Dimensions of the tensor (e.g., {2, 3, 4, 5} for a 4D tensor)
Exceptions:
- std::invalid_argument if data.size() != product of shape
- std::invalid_argument if shape is empty
function DenseTensorStorage
explicit DenseTensorStorage(
std::vector< std::size_t > shape
)Construct an uninitialized tensor with the given shape.
Parameters:
- shape Dimensions of the tensor
Exceptions:
- std::invalid_argument if shape is empty
All elements are initialized to 0.0f. Useful for pre-allocating a tensor that will be filled via mutableFlatData().
function mutableFlatData
std::span< float > mutableFlatData()Get mutable access to the flat data buffer.
Intended for construction and bulk-fill scenarios. Callers must not change the size of the underlying data.
function setValueAt
void setValueAt(
std::span< std::size_t const > indices,
float value
)Set a single value by multi-dimensional index.
Parameters:
- indices One index per dimension, in row-major order
- value The value to set
Exceptions:
- std::out_of_range if any index is out of bounds
- std::invalid_argument if indices.size() != ndim
function appendRow
void appendRow(
std::span< float const > row_data
)Append a row to the end of the tensor.
Parameters:
- row_data Row values; size must equal number of columns (shape[1])
Exceptions:
- std::logic_error if storage is not 2D
- std::invalid_argument if row_data.size() != shape[1]
Only valid for 2D storage.
function insertRow
void insertRow(
std::size_t index,
std::span< float const > row_data
)Insert a row at a specific position.
Parameters:
- index Row index to insert before (0-based; numRows inserts at end)
- row_data Row values; size must equal number of columns (shape[1])
Exceptions:
- std::logic_error if storage is not 2D
- std::out_of_range if index > number of rows
- std::invalid_argument if row_data.size() != shape[1]
Only valid for 2D storage. Existing rows at and after the insertion point are shifted down.
function setRow
void setRow(
std::size_t index,
std::span< float const > row_data
)Overwrite an existing row’s data in-place.
Parameters:
- index Row index to overwrite (0-based)
- row_data New values; size must equal number of columns (shape[1])
Exceptions:
- std::logic_error if storage is not 2D
- std::out_of_range if index >= number of rows
- std::invalid_argument if row_data.size() != shape[1]
Only valid for 2D storage.
function ndim
inline std::size_t ndim() constGet the number of dimensions.
function strides
inline std::vector< std::size_t > const & strides() constGet the precomputed row-major strides.
stride[k] = product of shape[k+1..N-1], stride[N-1] = 1
function getValueAtImpl
float getValueAtImpl(
std::span< std::size_t const > indices
) constfunction flatDataImpl
std::span< float const > flatDataImpl() constfunction sliceAlongAxisImpl
std::vector< float > sliceAlongAxisImpl(
std::size_t axis,
std::size_t index
) constfunction getColumnImpl
std::vector< float > getColumnImpl(
std::size_t col
) constfunction shapeImpl
std::vector< std::size_t > shapeImpl() constfunction totalElementsImpl
std::size_t totalElementsImpl() constfunction isContiguousImpl
inline bool isContiguousImpl() constfunction getStorageTypeImpl
inline TensorStorageType getStorageTypeImpl() constfunction tryGetCacheImpl
TensorStorageCache tryGetCacheImpl() constUpdated on 2026-11-13 at 20:25:41 +0000