![]() |
Belle II Software development
|
Represents an input or output tensor for an ONNX model. More...
#include <ONNX.h>
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. | |
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
.
T | The data type of the tensor elements (e.g., float, int64_t). |
|
inline |
Constructs a tensor from shape.
shape | Shape of the tensor. |
std::invalid_argument | if any shape dimension is negative |
Definition at line 99 of file ONNX.h.
|
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.
values | Flat vector of tensor values. |
shape | Shape of the tensor. |
std::length_error | if the size of values does not match the expected size. |
std::invalid_argument | if any shape dimension is negative |
Definition at line 120 of file ONNX.h.
|
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.
index | The flat index into the tensor's data. |
std::out_of_range | if the index is out of bounds. |
Definition at line 181 of file ONNX.h.
|
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.
index | A vector of indices, one for each dimension. |
std::out_of_range | if any index is out of bounds. |
Definition at line 198 of file ONNX.h.
|
inlineprivate |
|
inlinevirtual |
Create an Ort::Value from pointers to the underlying data and shape.
Primarily intended for internal use by Session::run
.
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).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.Ort::Value
pointing to the Tensor
's internal data. Implements BaseTensor.
Definition at line 249 of file ONNX.h.
|
inlinestatic |
Convenience method to create a shared pointer to a Tensor from shape.
Useful for constructing shared pointers from initializer lists, e.g.
shape | Shape of the tensor. |
Definition at line 145 of file ONNX.h.
|
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):
values | Flat vector of tensor values. |
shape | Shape of the tensor. |
Definition at line 164 of file ONNX.h.
|
inline |
Replaces the internal values with a new vector.
values | A vector of values to be used to update the internal ones |
std::length_error | if the size of the new values vector does not match the internal size. |
Definition at line 221 of file ONNX.h.
|
inlineprivate |
|
private |
|
private |
|
private |