Belle II Software development
Tensor< T > Class Template Reference

Represents an input or output tensor for an ONNX model. More...

#include <ONNX.h>

Inheritance diagram for Tensor< T >:
BaseTensor

Public Member Functions

 Tensor (std::vector< int64_t > shape)
 Constructs a tensor from shape.
 
 Tensor (std::vector< T > values, std::vector< int64_t > shape)
 Constructs a tensor from a data vector and shape.
 
auto & at (size_t index)
 Accesses the element at the specified flat index.
 
auto & at (std::vector< size_t > index)
 Accesses the element at the specified multi-dimensional index.
 
void setValues (const std::vector< T > &values)
 Replaces the internal values with a new vector.
 
Ort::Value createOrtTensor ()
 Create an Ort::Value from pointers to the underlying data and shape.
 

Static Public Member Functions

static auto make_shared (std::vector< int64_t > shape)
 Convenience method to create a shared pointer to a Tensor from shape.
 
static auto make_shared (std::vector< T > values, std::vector< int64_t > shape)
 Convenience method to create a shared pointer to a Tensor from values and shape.
 

Private Member Functions

size_t sizeFromShape (const std::vector< int64_t > &shape)
 Calculates the internal vector size from the product of the shape dimensions.
 
void checkShapePositive ()
 Checks if all shape dimensions are positive.
 

Private Attributes

std::vector< T > m_values
 Flat buffer storing tensor data in row-major order.
 
std::vector< int64_t > m_shape
 The dimensions of the tensor.
 
Ort::MemoryInfo m_memoryInfo
 Memory information used for allocating ONNX Runtime tensors.
 

Detailed Description

template<typename T>
class Belle2::MVA::ONNX::Tensor< T >

Represents an input or output tensor for an ONNX model.

Stores both the data and shape information of a tensor as std::vector. Shared pointers to instances of this class can be passed in a map to Session::run.

Template Parameters
TThe data type of the tensor elements (e.g., float, int64_t).

Definition at line 51 of file ONNX.h.

Constructor & Destructor Documentation

◆ Tensor() [1/2]

template<typename T>
Tensor ( std::vector< int64_t > shape)
inline

Constructs a tensor from shape.

Parameters
shapeShape of the tensor.
Exceptions
std::invalid_argumentif any shape dimension is negative

Definition at line 99 of file ONNX.h.

100 : m_values(sizeFromShape(shape)), m_shape(std::move(shape)),
101 m_memoryInfo(
102 Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU))
103 {
104 checkShapePositive();
105 }

◆ Tensor() [2/2]

template<typename T>
Tensor ( std::vector< T > values,
std::vector< int64_t > shape )
inline

Constructs a tensor from a data vector and shape.

Copies the provided values into the tensor. Ensures that the size of the values vector matches the product of the shape dimensions.

Parameters
valuesFlat vector of tensor values.
shapeShape of the tensor.
Exceptions
std::length_errorif the size of values does not match the expected size.
std::invalid_argumentif any shape dimension is negative

Definition at line 120 of file ONNX.h.

121 : m_values(std::move(values)), m_shape(std::move(shape)),
122 m_memoryInfo(
123 Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU))
124 {
125 if (sizeFromShape(m_shape) != m_values.size()) {
126 throw std::length_error(
127 "Size of the given values vector (" + std::to_string(m_values.size()) + ") "
128 "does not match the product of the shape dimensions (" + std::to_string(sizeFromShape(m_shape)) + ")");
129 }
130 checkShapePositive();
131 }

Member Function Documentation

◆ at() [1/2]

template<typename T>
auto & at ( size_t index)
inline

Accesses the element at the specified flat index.

Returns a reference to the element in the tensor's underlying storage at the given 1D (flattened) index. Performs bounds checking.

Parameters
indexThe flat index into the tensor's data.
Returns
Reference to the tensor element at the specified index.
Exceptions
std::out_of_rangeif the index is out of bounds.

Definition at line 181 of file ONNX.h.

181{ return m_values.at(index); }

◆ at() [2/2]

template<typename T>
auto & at ( std::vector< size_t > index)
inline

Accesses the element at the specified multi-dimensional index.

Converts the given multi-dimensional index into a flat index based on the tensor's shape, and returns a reference to the corresponding element. Performs bounds checking via the underlying flat index access.

Example: For a tensor with shape {2, 3}, index {1, 2} accesses element at flat index 5.

Parameters
indexA vector of indices, one for each dimension.
Returns
Reference to the tensor element at the specified index.
Exceptions
std::out_of_rangeif any index is out of bounds.

Definition at line 198 of file ONNX.h.

199 {
200 size_t flat_index = 0;
201 size_t stride = 1;
202 for (int64_t i = m_shape.size() - 1; i >= 0; --i) {
203 if (index[i] >= static_cast<size_t>(m_shape[i])) {
204 throw std::out_of_range(
205 "index " + std::to_string(index[i]) + " is out of bounds for axis "
206 + std::to_string(i) + " with size " + std::to_string(m_shape[i]));
207 }
208 flat_index += index[i] * stride;
209 stride *= m_shape[i];
210 }
211 return at(flat_index);
212 }

◆ checkShapePositive()

template<typename T>
void checkShapePositive ( )
inlineprivate

Checks if all shape dimensions are positive.

Exceptions
std::invalid_argumentif any shape dimension is negative

Definition at line 84 of file ONNX.h.

85 {
86 for (auto n : m_shape) {
87 if (n < 0) throw std::invalid_argument("All shape dimensions must be positive");
88 }
89 }

◆ createOrtTensor()

template<typename T>
Ort::Value createOrtTensor ( )
inlinevirtual

Create an Ort::Value from pointers to the underlying data and shape.

Primarily intended for internal use by Session::run.

Note
It is generally recommended to use the Session::run overload that accepts maps of Tensor shared pointers. However, for performance-critical scenarios, such as small, fast models executed in tight loops, manually reusing Ort::Value instances may offer slight performance gains (on the order of microseconds).
Warning
The returned Ort::Value does not take ownership of the tensor data. It simply references the existing memory. Therefore, you must ensure that the Tensor object remains alive for as long as the Ort::Value is in use.
Returns
An Ort::Value pointing to the Tensor's internal data.

Implements BaseTensor.

Definition at line 249 of file ONNX.h.

250 {
251 return Ort::Value::CreateTensor(m_memoryInfo, m_values.data(),
252 m_values.size(), m_shape.data(),
253 m_shape.size());
254 }

◆ make_shared() [1/2]

template<typename T>
static auto make_shared ( std::vector< int64_t > shape)
inlinestatic

Convenience method to create a shared pointer to a Tensor from shape.

Useful for constructing shared pointers from initializer lists, e.g.

auto input = Tensor<float>::make_shared({batchSize, nFeatures});
static auto make_shared(std::vector< int64_t > shape)
Convenience method to create a shared pointer to a Tensor from shape.
Definition ONNX.h:145
Parameters
shapeShape of the tensor.
Returns
Shared pointer to the created Tensor.

Definition at line 145 of file ONNX.h.

146 {
147 return std::make_shared<Tensor>(std::move(shape));
148 }

◆ make_shared() [2/2]

template<typename T>
static auto make_shared ( std::vector< T > values,
std::vector< int64_t > shape )
inlinestatic

Convenience method to create a shared pointer to a Tensor from values and shape.

Useful for constructing shared pointers from initializer lists, e.g. to create a Tensor with 3 values and shape (1, 3):

auto input = Tensor<float>::make_shared({v1, v2, v3}, {1, 3});
Parameters
valuesFlat vector of tensor values.
shapeShape of the tensor.
Returns
Shared pointer to the created Tensor.

Definition at line 164 of file ONNX.h.

166 {
167 return std::make_shared<Tensor>(std::move(values), std::move(shape));
168 }

◆ setValues()

template<typename T>
void setValues ( const std::vector< T > & values)
inline

Replaces the internal values with a new vector.

Parameters
valuesA vector of values to be used to update the internal ones
Exceptions
std::length_errorif the size of the new values vector does not match the internal size.

Definition at line 221 of file ONNX.h.

222 {
223 if (m_values.size() != values.size()) {
224 throw std::length_error(
225 "Size of new values vector (" + std::to_string(values.size()) + ") "
226 "differs from internal size (" + std::to_string(m_values.size()) + ")");
227 }
228 m_values = values;
229 }

◆ sizeFromShape()

template<typename T>
size_t sizeFromShape ( const std::vector< int64_t > & shape)
inlineprivate

Calculates the internal vector size from the product of the shape dimensions.

Definition at line 72 of file ONNX.h.

73 {
74 size_t size = 1;
75 for (auto n : shape) size *= n;
76 return size;
77 }

Member Data Documentation

◆ m_memoryInfo

template<typename T>
Ort::MemoryInfo m_memoryInfo
private

Memory information used for allocating ONNX Runtime tensors.

Will be set to CPU allocator in the constructor.

Definition at line 67 of file ONNX.h.

◆ m_shape

template<typename T>
std::vector<int64_t> m_shape
private

The dimensions of the tensor.

Definition at line 60 of file ONNX.h.

◆ m_values

template<typename T>
std::vector<T> m_values
private

Flat buffer storing tensor data in row-major order.

Definition at line 55 of file ONNX.h.


The documentation for this class was generated from the following file: