v2.0.0
Loading...
Searching...
No Matches
ml_tensor.h
Go to the documentation of this file.
1//=============================================================================================================
34
35#ifndef ML_TENSOR_H
36#define ML_TENSOR_H
37
38//=============================================================================================================
39// INCLUDES
40//=============================================================================================================
41
42#include "ml_global.h"
43
44//=============================================================================================================
45// EIGEN INCLUDES
46//=============================================================================================================
47
48#include <Eigen/Core>
49
50//=============================================================================================================
51// STL INCLUDES
52//=============================================================================================================
53
54#include <cassert>
55#include <cstdint>
56#include <memory>
57#include <vector>
58
59//=============================================================================================================
60// DEFINE NAMESPACE MLLIB
61//=============================================================================================================
62
63namespace MLLIB{
64
65//=============================================================================================================
79{
80public:
81 // --- type aliases used in the public API --------------------------------
82 using RowMajorMatrixXf = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
83 using RowMajorMatrixMap = Eigen::Map<RowMajorMatrixXf>;
84 using ConstRowMajorMatrixMap = Eigen::Map<const RowMajorMatrixXf>;
85
86 //=========================================================================================================
90 MlTensor();
91
92 //=========================================================================================================
100 MlTensor(std::vector<float>&& data, std::vector<int64_t> shape);
101
102 //=========================================================================================================
109 MlTensor(const float* data, std::vector<int64_t> shape);
110
111 //=========================================================================================================
119 explicit MlTensor(const Eigen::MatrixXf& mat);
120
121 //=========================================================================================================
129 explicit MlTensor(const Eigen::MatrixXd& mat);
130
131 //=========================================================================================================
141 static MlTensor view(float* data, std::vector<int64_t> shape);
142
143 //=========================================================================================================
152 static MlTensor fromBuffer(const float* data, int rows, int cols);
153
154 // --- shape access -------------------------------------------------------
155
156 //=========================================================================================================
160 int ndim() const;
161
162 //=========================================================================================================
166 int64_t size() const;
167
168 //=========================================================================================================
172 const std::vector<int64_t>& shape() const;
173
174 //=========================================================================================================
179 int64_t shape(int dim) const;
180
181 //=========================================================================================================
186 int rows() const;
187
188 //=========================================================================================================
193 int cols() const;
194
195 // --- raw data access (zero-copy) ----------------------------------------
196
197 //=========================================================================================================
201 float* data();
202
203 //=========================================================================================================
207 const float* data() const;
208
209 // --- Eigen Map accessors (zero-copy, 2-D) -------------------------------
210
211 //=========================================================================================================
219
220 //=========================================================================================================
228
229 // --- Eigen copy helpers (produce column-major copies) -------------------
230
231 //=========================================================================================================
235 Eigen::MatrixXf toMatrixXf() const;
236
237 //=========================================================================================================
241 Eigen::MatrixXd toMatrixXd() const;
242
243 // --- reshape / query ----------------------------------------------------
244
245 //=========================================================================================================
255 MlTensor reshape(std::vector<int64_t> newShape) const;
256
257 //=========================================================================================================
261 bool isView() const;
262
263 //=========================================================================================================
267 bool empty() const;
268
269private:
270 static int64_t computeSize(const std::vector<int64_t>& shape);
271
272 std::shared_ptr<std::vector<float>> m_storage;
273 float* m_data = nullptr;
274 std::vector<int64_t> m_shape;
275 int64_t m_size = 0;
276};
277
278} // namespace MLLIB
279
280#endif // ML_TENSOR_H
ml library export/import macros.
#define MLSHARED_EXPORT
Definition ml_global.h:55
Machine learning (models, pipelines, ONNX Runtime integration).
int cols() const
Eigen::MatrixXf toMatrixXf() const
Eigen::Map< RowMajorMatrixXf > RowMajorMatrixMap
Definition ml_tensor.h:83
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > RowMajorMatrixXf
Definition ml_tensor.h:82
static MlTensor fromBuffer(const float *data, int rows, int cols)
Eigen::MatrixXd toMatrixXd() const
static MlTensor view(float *data, std::vector< int64_t > shape)
MlTensor(const Eigen::MatrixXd &mat)
int ndim() const
RowMajorMatrixMap matrix()
int64_t size() const
MlTensor(const Eigen::MatrixXf &mat)
int rows() const
MlTensor reshape(std::vector< int64_t > newShape) const
bool empty() const
const std::vector< int64_t > & shape() const
bool isView() const
Eigen::Map< const RowMajorMatrixXf > ConstRowMajorMatrixMap
Definition ml_tensor.h:84