55int64_t MlTensor::computeSize(
const std::vector<int64_t>& shape)
59 return std::accumulate(
shape.begin(),
shape.end(),
60 int64_t(1), std::multiplies<int64_t>());
78: m_shape(std::move(
shape))
79, m_size(computeSize(m_shape))
81 if (
static_cast<int64_t
>(
data.size()) != m_size) {
82 throw std::invalid_argument(
"MlTensor: buffer size does not match shape");
84 m_storage = std::make_shared<std::vector<float>>(std::move(
data));
85 m_data = m_storage->data();
91: m_shape(std::move(
shape))
92, m_size(computeSize(m_shape))
94 m_storage = std::make_shared<std::vector<float>>(
data,
data + m_size);
95 m_data = m_storage->data();
101: m_shape({mat.rows(), mat.cols()})
104 m_storage = std::make_shared<std::vector<float>>(
static_cast<size_t>(m_size));
105 m_data = m_storage->data();
107 Map<RowMajorMatrixXf>(m_data, mat.rows(), mat.cols()) = mat;
113: m_shape({mat.rows(), mat.cols()})
116 m_storage = std::make_shared<std::vector<float>>(
static_cast<size_t>(m_size));
117 m_data = m_storage->data();
118 Map<RowMajorMatrixXf>(m_data, mat.rows(), mat.cols()) = mat.cast<
float>();
126 t.m_shape = std::move(
shape);
127 t.m_size = computeSize(t.m_shape);
128 t.m_storage =
nullptr;
144 return static_cast<int>(m_shape.size());
167 assert(dim >= 0 && dim <
ndim());
168 return m_shape[
static_cast<size_t>(dim)];
176 return static_cast<int>(m_shape[0]);
184 return static_cast<int>(m_shape[1]);
223 return Map<const RowMajorMatrixXf>(m_data, m_shape[0], m_shape[1]);
231 return Map<const RowMajorMatrixXf>(m_data, m_shape[0], m_shape[1]).cast<
double>();
238 int64_t newSize = computeSize(newShape);
239 if (newSize != m_size) {
240 throw std::invalid_argument(
"MlTensor::reshape: new shape size differs from current");
244 t.m_storage = m_storage;
246 t.m_shape = std::move(newShape);
255 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