v2.0.0
Loading...
Searching...
No Matches
ml_tensor.cpp
Go to the documentation of this file.
1//=============================================================================================================
34
35//=============================================================================================================
36// INCLUDES
37//=============================================================================================================
38
39#include "ml_tensor.h"
40
41//=============================================================================================================
42// STL INCLUDES
43//=============================================================================================================
44
45#include <algorithm>
46#include <cstring>
47#include <numeric>
48#include <stdexcept>
49
50//=============================================================================================================
51// USED NAMESPACES
52//=============================================================================================================
53
54using namespace MLLIB;
55using namespace Eigen;
56
57//=============================================================================================================
58// STATIC HELPERS
59//=============================================================================================================
60
61int64_t MlTensor::computeSize(const std::vector<int64_t>& shape)
62{
63 if (shape.empty())
64 return 0;
65 return std::accumulate(shape.begin(), shape.end(),
66 int64_t(1), std::multiplies<int64_t>());
67}
68
69//=============================================================================================================
70// DEFINE MEMBER METHODS
71//=============================================================================================================
72
74: m_storage(nullptr)
75, m_data(nullptr)
76, m_shape()
77, m_size(0)
78{
79}
80
81//=============================================================================================================
82
83MlTensor::MlTensor(std::vector<float>&& data, std::vector<int64_t> shape)
84: m_shape(std::move(shape))
85, m_size(computeSize(m_shape))
86{
87 if (static_cast<int64_t>(data.size()) != m_size) {
88 throw std::invalid_argument("MlTensor: buffer size does not match shape");
89 }
90 m_storage = std::make_shared<std::vector<float>>(std::move(data));
91 m_data = m_storage->data();
92}
93
94//=============================================================================================================
95
96MlTensor::MlTensor(const float* data, std::vector<int64_t> shape)
97: m_shape(std::move(shape))
98, m_size(computeSize(m_shape))
99{
100 m_storage = std::make_shared<std::vector<float>>(data, data + m_size);
101 m_data = m_storage->data();
102}
103
104//=============================================================================================================
105
106MlTensor::MlTensor(const MatrixXf& mat)
107: m_shape({mat.rows(), mat.cols()})
108, m_size(mat.size())
109{
110 m_storage = std::make_shared<std::vector<float>>(static_cast<size_t>(m_size));
111 m_data = m_storage->data();
112 // Copy column-major Eigen → row-major contiguous storage (one memop)
113 Map<RowMajorMatrixXf>(m_data, mat.rows(), mat.cols()) = mat;
114}
115
116//=============================================================================================================
117
118MlTensor::MlTensor(const MatrixXd& mat)
119: m_shape({mat.rows(), mat.cols()})
120, m_size(mat.size())
121{
122 m_storage = std::make_shared<std::vector<float>>(static_cast<size_t>(m_size));
123 m_data = m_storage->data();
124 Map<RowMajorMatrixXf>(m_data, mat.rows(), mat.cols()) = mat.cast<float>();
125}
126
127//=============================================================================================================
128
129MlTensor MlTensor::view(float* data, std::vector<int64_t> shape)
130{
131 MlTensor t;
132 t.m_shape = std::move(shape);
133 t.m_size = computeSize(t.m_shape);
134 t.m_storage = nullptr; // non-owning
135 t.m_data = data;
136 return t;
137}
138
139//=============================================================================================================
140
142{
143 return MlTensor(data, {static_cast<int64_t>(rows), static_cast<int64_t>(cols)});
144}
145
146//=============================================================================================================
147
148int MlTensor::ndim() const
149{
150 return static_cast<int>(m_shape.size());
151}
152
153//=============================================================================================================
154
155int64_t MlTensor::size() const
156{
157 return m_size;
158}
159
160//=============================================================================================================
161
162const std::vector<int64_t>& MlTensor::shape() const
163{
164 return m_shape;
165}
166
167//=============================================================================================================
168
169int64_t MlTensor::shape(int dim) const
170{
171 if (dim < 0)
172 dim += ndim();
173 assert(dim >= 0 && dim < ndim());
174 return m_shape[static_cast<size_t>(dim)];
175}
176
177//=============================================================================================================
178
179int MlTensor::rows() const
180{
181 assert(ndim() >= 1);
182 return static_cast<int>(m_shape[0]);
183}
184
185//=============================================================================================================
186
187int MlTensor::cols() const
188{
189 assert(ndim() >= 2);
190 return static_cast<int>(m_shape[1]);
191}
192
193//=============================================================================================================
194
196{
197 return m_data;
198}
199
200//=============================================================================================================
201
202const float* MlTensor::data() const
203{
204 return m_data;
205}
206
207//=============================================================================================================
208
210{
211 assert(ndim() == 2);
212 return RowMajorMatrixMap(m_data, m_shape[0], m_shape[1]);
213}
214
215//=============================================================================================================
216
218{
219 assert(ndim() == 2);
220 return ConstRowMajorMatrixMap(m_data, m_shape[0], m_shape[1]);
221}
222
223//=============================================================================================================
224
225MatrixXf MlTensor::toMatrixXf() const
226{
227 assert(ndim() == 2);
228 // Assign row-major map to column-major MatrixXf (Eigen transposes layout)
229 return Map<const RowMajorMatrixXf>(m_data, m_shape[0], m_shape[1]);
230}
231
232//=============================================================================================================
233
234MatrixXd MlTensor::toMatrixXd() const
235{
236 assert(ndim() == 2);
237 return Map<const RowMajorMatrixXf>(m_data, m_shape[0], m_shape[1]).cast<double>();
238}
239
240//=============================================================================================================
241
242MlTensor MlTensor::reshape(std::vector<int64_t> newShape) const
243{
244 int64_t newSize = computeSize(newShape);
245 if (newSize != m_size) {
246 throw std::invalid_argument("MlTensor::reshape: new shape size differs from current");
247 }
248
249 MlTensor t;
250 t.m_storage = m_storage; // share ownership (or null for views)
251 t.m_data = m_data;
252 t.m_shape = std::move(newShape);
253 t.m_size = newSize;
254 return t;
255}
256
257//=============================================================================================================
258
260{
261 return m_storage == nullptr && m_data != nullptr;
262}
263
264//=============================================================================================================
265
266bool MlTensor::empty() const
267{
268 return m_size == 0;
269}
MlTensor class declaration — N-dimensional, row-major, zero-copy.
Machine learning (models, pipelines, ONNX Runtime integration).
int cols() const
Eigen::MatrixXf toMatrixXf() const
Eigen::Map< RowMajorMatrixXf > RowMajorMatrixMap
Definition ml_tensor.h:83
static MlTensor fromBuffer(const float *data, int rows, int cols)
Eigen::MatrixXd toMatrixXd() const
static MlTensor view(float *data, std::vector< int64_t > shape)
int ndim() const
RowMajorMatrixMap matrix()
int64_t size() const
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