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) |
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.
| using MLLIB::MlTensor::ConstRowMajorMatrixMap = Eigen::Map<const RowMajorMatrixXf> |
Definition at line 84 of file ml_tensor.h.
| using MLLIB::MlTensor::RowMajorMatrixMap = Eigen::Map<RowMajorMatrixXf> |
Definition at line 83 of file ml_tensor.h.
| using MLLIB::MlTensor::RowMajorMatrixXf = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> |
Definition at line 82 of file ml_tensor.h.
| MlTensor::MlTensor | ( | ) |
Default constructor – creates an empty 0-element tensor with shape {}.
Definition at line 73 of file ml_tensor.cpp.
| 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.
| [in] | data | Flat float32 buffer (row-major order). Moved in. |
| [in] | shape | Dimension sizes (e.g. {batch, seq, features}). |
Definition at line 83 of file ml_tensor.cpp.
| MlTensor::MlTensor | ( | const float * | data, |
| std::vector< int64_t > | shape ) |
Construct by copying from a raw pointer and a shape.
Definition at line 96 of file ml_tensor.cpp.
|
explicit |
Construct from an Eigen column-major MatrixXf. The data is copied and transposed into row-major layout. Shape is set to {rows, cols}.
| [in] | mat | Source matrix (column-major). |
|
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}.
| [in] | mat | Source matrix (column-major, double precision). |
| int MlTensor::cols | ( | ) | const |
2-D convenience — equivalent to shape(1). Asserts ndim >= 2.
Definition at line 187 of file ml_tensor.cpp.
| float * MlTensor::data | ( | ) |
Definition at line 195 of file ml_tensor.cpp.
| const float * MlTensor::data | ( | ) | const |
Definition at line 202 of file ml_tensor.cpp.
| bool MlTensor::empty | ( | ) | const |
Definition at line 266 of file ml_tensor.cpp.
|
static |
Copy raw data into a new owning tensor (legacy 2-D helper).
| [in] | data | Pointer to row-major float data. |
| [in] | rows | Number of rows. |
| [in] | cols | Number of columns. |
Definition at line 141 of file ml_tensor.cpp.
| bool MlTensor::isView | ( | ) | const |
Definition at line 259 of file ml_tensor.cpp.
| MlTensor::RowMajorMatrixMap MlTensor::matrix | ( | ) |
Mutable row-major Eigen::Map for 2-D tensors. Asserts ndim == 2.
Definition at line 209 of file ml_tensor.cpp.
| MlTensor::ConstRowMajorMatrixMap MlTensor::matrix | ( | ) | const |
Const row-major Eigen::Map for 2-D tensors. Asserts ndim == 2.
Definition at line 217 of file ml_tensor.cpp.
| int MlTensor::ndim | ( | ) | const |
Definition at line 148 of file ml_tensor.cpp.
| 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.
| [in] | newShape | New shape (product must equal size()). |
Definition at line 242 of file ml_tensor.cpp.
| int MlTensor::rows | ( | ) | const |
2-D convenience — equivalent to shape(0). Asserts ndim >= 1.
Definition at line 179 of file ml_tensor.cpp.
| const std::vector< int64_t > & MlTensor::shape | ( | ) | const |
Definition at line 162 of file ml_tensor.cpp.
| int64_t MlTensor::shape | ( | int | dim | ) | const |
| [in] | dim | Dimension index (negative indices count from the end). |
Definition at line 169 of file ml_tensor.cpp.
| int64_t MlTensor::size | ( | ) | const |
Definition at line 155 of file ml_tensor.cpp.
| MatrixXd MlTensor::toMatrixXd | ( | ) | const |
Definition at line 234 of file ml_tensor.cpp.
| MatrixXf MlTensor::toMatrixXf | ( | ) | const |
Definition at line 225 of file ml_tensor.cpp.
|
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).
Definition at line 129 of file ml_tensor.cpp.