v2.0.0
Loading...
Searching...
No Matches
MLLIB::MlTensor Class Reference

N-dimensional tensor with contiguous row-major (C-order) float32 storage. More...

#include <ml_tensor.h>

Public Types

using RowMajorMatrixXf = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
using RowMajorMatrixMap = Eigen::Map<RowMajorMatrixXf>
using ConstRowMajorMatrixMap = Eigen::Map<const RowMajorMatrixXf>

Public Member Functions

 MlTensor ()
 MlTensor (std::vector< float > &&data, std::vector< int64_t > shape)
 MlTensor (const float *data, std::vector< int64_t > shape)
 MlTensor (const Eigen::MatrixXf &mat)
 MlTensor (const Eigen::MatrixXd &mat)
int ndim () const
int64_t size () const
const std::vector< int64_t > & shape () const
int64_t shape (int dim) const
int rows () const
int cols () const
float * data ()
const float * data () const
RowMajorMatrixMap matrix ()
ConstRowMajorMatrixMap matrix () const
Eigen::MatrixXf toMatrixXf () const
Eigen::MatrixXd toMatrixXd () const
MlTensor reshape (std::vector< int64_t > newShape) const
bool isView () const
bool empty () const

Static Public Member Functions

static MlTensor view (float *data, std::vector< int64_t > shape)
static MlTensor fromBuffer (const float *data, int rows, int cols)

Detailed Description

N-dimensional tensor with contiguous row-major (C-order) float32 storage.

Data is held in a reference-counted buffer so that copy, reshape and slice are O(1). A non-owning "view" mode lets the tensor wrap external memory (e.g. memory-mapped files, CUDA device pointers) without any allocation.

Storage layout is row-major to match ONNX Runtime, PyTorch and NumPy conventions. Eigen interop is provided through Map accessors that expose the data without copying.

Definition at line 78 of file ml_tensor.h.

Member Typedef Documentation

◆ ConstRowMajorMatrixMap

Definition at line 84 of file ml_tensor.h.

◆ RowMajorMatrixMap

Definition at line 83 of file ml_tensor.h.

◆ RowMajorMatrixXf

using MLLIB::MlTensor::RowMajorMatrixXf = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>

Definition at line 82 of file ml_tensor.h.

Constructor & Destructor Documentation

◆ MlTensor() [1/5]

MlTensor::MlTensor ( )

Default constructor – creates an empty 0-element tensor with shape {}.

Definition at line 73 of file ml_tensor.cpp.

◆ MlTensor() [2/5]

MlTensor::MlTensor ( std::vector< float > && data,
std::vector< int64_t > shape )

Construct from a moved data buffer and an arbitrary shape. The buffer size must equal the product of shape elements.

Parameters
[in]dataFlat float32 buffer (row-major order). Moved in.
[in]shapeDimension sizes (e.g. {batch, seq, features}).

Definition at line 83 of file ml_tensor.cpp.

◆ MlTensor() [3/5]

MlTensor::MlTensor ( const float * data,
std::vector< int64_t > shape )

Construct by copying from a raw pointer and a shape.

Parameters
[in]dataPointer to contiguous float32 data (row-major).
[in]shapeDimension sizes.

Definition at line 96 of file ml_tensor.cpp.

◆ MlTensor() [4/5]

MLLIB::MlTensor::MlTensor ( const Eigen::MatrixXf & mat)
explicit

Construct from an Eigen column-major MatrixXf. The data is copied and transposed into row-major layout. Shape is set to {rows, cols}.

Parameters
[in]matSource matrix (column-major).

◆ MlTensor() [5/5]

MLLIB::MlTensor::MlTensor ( const Eigen::MatrixXd & mat)
explicit

Construct from an Eigen column-major MatrixXd. The data is cast to float32 and stored in row-major layout. Shape is set to {rows, cols}.

Parameters
[in]matSource matrix (column-major, double precision).

Member Function Documentation

◆ cols()

int MlTensor::cols ( ) const

2-D convenience — equivalent to shape(1). Asserts ndim >= 2.

Returns
Number of columns.

Definition at line 187 of file ml_tensor.cpp.

◆ data() [1/2]

float * MlTensor::data ( )
Returns
Mutable pointer to the contiguous float32 buffer.

Definition at line 195 of file ml_tensor.cpp.

◆ data() [2/2]

const float * MlTensor::data ( ) const
Returns
Const pointer to the contiguous float32 buffer.

Definition at line 202 of file ml_tensor.cpp.

◆ empty()

bool MlTensor::empty ( ) const
Returns
True if the tensor contains zero elements.

Definition at line 266 of file ml_tensor.cpp.

◆ fromBuffer()

MlTensor MlTensor::fromBuffer ( const float * data,
int rows,
int cols )
static

Copy raw data into a new owning tensor (legacy 2-D helper).

Parameters
[in]dataPointer to row-major float data.
[in]rowsNumber of rows.
[in]colsNumber of columns.
Returns
Newly constructed tensor with shape {rows, cols}.

Definition at line 141 of file ml_tensor.cpp.

◆ isView()

bool MlTensor::isView ( ) const
Returns
True if this tensor does not own its data buffer.

Definition at line 259 of file ml_tensor.cpp.

◆ matrix() [1/2]

MlTensor::RowMajorMatrixMap MlTensor::matrix ( )

Mutable row-major Eigen::Map for 2-D tensors. Asserts ndim == 2.

Returns
Row-major mutable Map.

Definition at line 209 of file ml_tensor.cpp.

◆ matrix() [2/2]

MlTensor::ConstRowMajorMatrixMap MlTensor::matrix ( ) const

Const row-major Eigen::Map for 2-D tensors. Asserts ndim == 2.

Returns
Row-major const Map.

Definition at line 217 of file ml_tensor.cpp.

◆ ndim()

int MlTensor::ndim ( ) const
Returns
Number of dimensions.

Definition at line 148 of file ml_tensor.cpp.

◆ reshape()

MlTensor MlTensor::reshape ( std::vector< int64_t > newShape) const

Return a tensor that shares the same storage but has a different shape. The total element count must be unchanged. If this tensor owns its data the result shares ownership (reference-counted). If this tensor is a view, the result is also a view into the same external buffer.

Parameters
[in]newShapeNew shape (product must equal size()).
Returns
Reshaped tensor (zero-copy).

Definition at line 242 of file ml_tensor.cpp.

◆ rows()

int MlTensor::rows ( ) const

2-D convenience — equivalent to shape(0). Asserts ndim >= 1.

Returns
Number of rows.

Definition at line 179 of file ml_tensor.cpp.

◆ shape() [1/2]

const std::vector< int64_t > & MlTensor::shape ( ) const
Returns
Const reference to the full shape vector.

Definition at line 162 of file ml_tensor.cpp.

◆ shape() [2/2]

int64_t MlTensor::shape ( int dim) const
Parameters
[in]dimDimension index (negative indices count from the end).
Returns
Size of the requested dimension.

Definition at line 169 of file ml_tensor.cpp.

◆ size()

int64_t MlTensor::size ( ) const
Returns
Total number of elements (product of all dimensions).

Definition at line 155 of file ml_tensor.cpp.

◆ toMatrixXd()

MatrixXd MlTensor::toMatrixXd ( ) const
Returns
Column-major double copy of the 2-D data. Asserts ndim == 2.

Definition at line 234 of file ml_tensor.cpp.

◆ toMatrixXf()

MatrixXf MlTensor::toMatrixXf ( ) const
Returns
Column-major float copy of the 2-D data. Asserts ndim == 2.

Definition at line 225 of file ml_tensor.cpp.

◆ view()

MlTensor MlTensor::view ( float * data,
std::vector< int64_t > shape )
static

Create a non-owning view over external mutable memory. The caller is responsible for keeping the buffer alive for the lifetime of this tensor (and any copies / reshapes derived from it).

Parameters
[in]dataPointer to external float32 data (row-major).
[in]shapeDimension sizes.
Returns
A non-owning MlTensor.

Definition at line 129 of file ml_tensor.cpp.


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