61int64_t MlTensor::computeSize(
const std::vector<int64_t>& shape)
65 return std::accumulate(
shape.begin(),
shape.end(),
66 int64_t(1), std::multiplies<int64_t>());
84: m_shape(std::move(
shape))
85, m_size(computeSize(m_shape))
87 if (
static_cast<int64_t
>(
data.size()) != m_size) {
88 throw std::invalid_argument(
"MlTensor: buffer size does not match shape");
90 m_storage = std::make_shared<std::vector<float>>(std::move(
data));
91 m_data = m_storage->data();
97: m_shape(std::move(
shape))
98, m_size(computeSize(m_shape))
100 m_storage = std::make_shared<std::vector<float>>(
data,
data + m_size);
101 m_data = m_storage->data();
107: m_shape({mat.rows(), mat.cols()})
110 m_storage = std::make_shared<std::vector<float>>(
static_cast<size_t>(m_size));
111 m_data = m_storage->data();
113 Map<RowMajorMatrixXf>(m_data, mat.rows(), mat.cols()) = mat;
119: m_shape({mat.rows(), mat.cols()})
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>();
132 t.m_shape = std::move(
shape);
133 t.m_size = computeSize(t.m_shape);
134 t.m_storage =
nullptr;
150 return static_cast<int>(m_shape.size());
173 assert(dim >= 0 && dim <
ndim());
174 return m_shape[
static_cast<size_t>(dim)];
182 return static_cast<int>(m_shape[0]);
190 return static_cast<int>(m_shape[1]);
229 return Map<const RowMajorMatrixXf>(m_data, m_shape[0], m_shape[1]);
237 return Map<const RowMajorMatrixXf>(m_data, m_shape[0], m_shape[1]).cast<
double>();
244 int64_t newSize = computeSize(newShape);
245 if (newSize != m_size) {
246 throw std::invalid_argument(
"MlTensor::reshape: new shape size differs from current");
250 t.m_storage = m_storage;
252 t.m_shape = std::move(newShape);
261 return m_storage ==
nullptr && m_data !=
nullptr;
MlTensor class declaration — N-dimensional, row-major, zero-copy.
Machine learning (models, pipelines, ONNX Runtime integration).
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