55int64_t MlTensor::computeSize(
const std::vector<int64_t>& shape)
58 return std::accumulate(
shape.begin(),
shape.end(),
59 int64_t(1), std::multiplies<int64_t>());
77: m_shape(std::move(
shape))
78, m_size(computeSize(m_shape))
80 if (
static_cast<int64_t
>(
data.size()) != m_size) {
81 throw std::invalid_argument(
"MlTensor: buffer size does not match shape");
83 m_storage = std::make_shared<std::vector<float>>(std::move(
data));
84 m_data = m_storage->data();
90: m_shape(std::move(
shape))
91, m_size(computeSize(m_shape))
93 m_storage = std::make_shared<std::vector<float>>(
data,
data + m_size);
94 m_data = m_storage->data();
100: m_shape({mat.rows(), mat.cols()})
103 m_storage = std::make_shared<std::vector<float>>(
static_cast<size_t>(m_size));
104 m_data = m_storage->data();
106 Map<RowMajorMatrixXf>(m_data, mat.rows(), mat.cols()) = mat;
112: m_shape({mat.rows(), mat.cols()})
115 m_storage = std::make_shared<std::vector<float>>(
static_cast<size_t>(m_size));
116 m_data = m_storage->data();
117 Map<RowMajorMatrixXf>(m_data, mat.rows(), mat.cols()) = mat.cast<
float>();
125 t.m_shape = std::move(
shape);
126 t.m_size = computeSize(t.m_shape);
127 t.m_storage =
nullptr;
143 return static_cast<int>(m_shape.size());
166 assert(dim >= 0 && dim <
ndim());
167 return m_shape[
static_cast<size_t>(dim)];
175 return static_cast<int>(m_shape[0]);
183 return static_cast<int>(m_shape[1]);
222 return Map<const RowMajorMatrixXf>(m_data, m_shape[0], m_shape[1]);
230 return Map<const RowMajorMatrixXf>(m_data, m_shape[0], m_shape[1]).cast<
double>();
237 int64_t newSize = computeSize(newShape);
238 if (newSize != m_size) {
239 throw std::invalid_argument(
"MlTensor::reshape: new shape size differs from current");
243 t.m_storage = m_storage;
245 t.m_shape = std::move(newShape);
254 return m_storage ==
nullptr && m_data !=
nullptr;
N-dimensional, row-major, reference-counted float32 tensor used as the universal MLLIB data carrier.
Tensors, model abstraction, ONNX Runtime inference and Python training drivers used across mne-cpp.
Eigen::MatrixXf toMatrixXf() const
Eigen::Map< RowMajorMatrixXf > RowMajorMatrixMap
static MlTensor fromBuffer(const float *data, int rows, int cols)
Eigen::MatrixXd toMatrixXd() const
static MlTensor view(float *data, std::vector< int64_t > shape)
RowMajorMatrixMap matrix()
MlTensor reshape(std::vector< int64_t > newShape) const
const std::vector< int64_t > & shape() const
Eigen::Map< const RowMajorMatrixXf > ConstRowMajorMatrixMap