Belle II Software development
Session Class Reference

A wrapper around Ort::Session providing model execution. More...

#include <ONNX.h>

Public Member Functions

 Session (const std::string filename)
 Constructs a new ONNX Runtime Session using the specified model file.
 
void run (const std::map< std::string, std::shared_ptr< BaseTensor > > &inputMap, const std::map< std::string, std::shared_ptr< BaseTensor > > &outputMap)
 Runs inference on the model using named Tensor maps.
 
void run (const std::vector< const char * > &inputNames, std::vector< Ort::Value > &inputs, const std::vector< const char * > &outputNames, std::vector< Ort::Value > &outputs)
 Runs inference on the model using raw ONNX Runtime inputs and outputs.
 

Private Attributes

Ort::Env m_env
 Environment object for ONNX session.
 
Ort::SessionOptions m_sessionOptions
 ONNX session configuration.
 
std::unique_ptr< Ort::Session > m_session
 The ONNX inference session.
 
Ort::RunOptions m_runOptions
 Options to be passed to Ort::Session::Run.
 

Detailed Description

A wrapper around Ort::Session providing model execution.

This class encapsulates an ONNX Runtime session, pre-configured with default settings such as single-threaded execution. It offers a more user-friendly interface to run inference using a custom Tensor class, which is easier to work with than raw Ort::Value instances.

Example usage:

#include <mva/methods/ONNX.h>
// ...
// assume my_model.onnx contains a model with inputs
// - "a": float Tensor with shape (1, 3)
// - "b": int64 Tensor with shape (1, 8, 5)
// and one float output called "output" with shape (1, 5)
Belle2::MVA::ONNX::Session session("my_model.onnx");
auto input_a = Tensor<float>::make_shared(3, {1, 3});
auto input_b = Tensor<int64_t>::make_shared(8 * 5, {1, 8, 5});
auto output = Tensor<float>::make_shared(5, {1, 5});
// example for filling data using multi dimensional indexing
input_b->at({0, 2, 4}) = 42;
// run model and fill output values
session.run({{"a", input_a}, {"b", input_b}}, {{"output", output}});
// get 3rd output value - example for 1-dimensional indexing
int output_3 = output->at(3);
A wrapper around Ort::Session providing model execution.
Definition ONNX.h:301
Represents an input or output tensor for an ONNX model.
Definition ONNX.h:51
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
auto & at(size_t index)
Accesses the element at the specified flat index.
Definition ONNX.h:181
Note
This method will not work with Tensor<bool> since the underlying std::vector<bool> does not support getting a pointer to an array. If you have a model with boolean inputs, either convert it to accept a different type (e.g. uint8_t) or use the Session::run overload thet works directl with Ort::Value instances.

Definition at line 301 of file ONNX.h.

Constructor & Destructor Documentation

◆ Session()

Session ( const std::string filename)

Constructs a new ONNX Runtime Session using the specified model file.

Parameters
filenamePath to the ONNX model file.

Definition at line 18 of file ONNX.cc.

19{
20 // Ensure single-threaded execution, see
21 // https://onnxruntime.ai/docs/performance/tune-performance/threading.html
22 //
23 // InterOpNumThreads is probably optional (not used in ORT_SEQUENTIAL mode)
24 // Also, with batch size 1 and ORT_SEQUENTIAL mode, MLP-like models will
25 // always run single threaded, but maybe not e.g. graph networks which can
26 // run in parallel on nodes. Here, setting IntraOpNumThreads to 1 is
27 // important to ensure single-threaded execution.
28 m_sessionOptions.SetIntraOpNumThreads(1);
29 m_sessionOptions.SetInterOpNumThreads(1);
30 m_sessionOptions.SetExecutionMode(ORT_SEQUENTIAL); // default, but make it explicit
31
32 m_session = std::make_unique<Ort::Session>(m_env, filename.c_str(), m_sessionOptions);
33}
Ort::Env m_env
Environment object for ONNX session.
Definition ONNX.h:345
std::unique_ptr< Ort::Session > m_session
The ONNX inference session.
Definition ONNX.h:355
Ort::SessionOptions m_sessionOptions
ONNX session configuration.
Definition ONNX.h:350

Member Function Documentation

◆ run() [1/2]

void run ( const std::map< std::string, std::shared_ptr< BaseTensor > > & inputMap,
const std::map< std::string, std::shared_ptr< BaseTensor > > & outputMap )

Runs inference on the model using named Tensor maps.

This overload accepts maps of input and output tensor names to their corresponding tensor data. It feeds the input tensors into the session and fills the output tensors with inference results.

Parameters
inputMapA map of input tensor names to shared pointers of Tensor objects.
outputMapA map of output tensor names to shared pointers of Tensor objects.

Definition at line 35 of file ONNX.cc.

37{
38 std::vector<Ort::Value> inputs;
39 std::vector<Ort::Value> outputs;
40 std::vector<const char*> inputNames;
41 std::vector<const char*> outputNames;
42 for (auto& x : inputMap) {
43 inputNames.push_back(x.first.c_str());
44 inputs.push_back(std::move(x.second->createOrtTensor()));
45 }
46 for (auto& x : outputMap) {
47 outputNames.push_back(x.first.c_str());
48 outputs.push_back(std::move(x.second->createOrtTensor()));
49 }
50 run(inputNames, inputs, outputNames, outputs);
51}
void run(const std::map< std::string, std::shared_ptr< BaseTensor > > &inputMap, const std::map< std::string, std::shared_ptr< BaseTensor > > &outputMap)
Runs inference on the model using named Tensor maps.
Definition ONNX.cc:35

◆ run() [2/2]

void run ( const std::vector< const char * > & inputNames,
std::vector< Ort::Value > & inputs,
const std::vector< const char * > & outputNames,
std::vector< Ort::Value > & outputs )

Runs inference on the model using raw ONNX Runtime inputs and outputs.

This overload provides works with raw ONNX Runtime Ort::Value objects and names. It's a lower level abstraction that should usually not be directly used, except in performance critical applications (meaning situations where saving O(1 microsecond) per call is helpful).

Parameters
inputNamesA vector of input tensor names.
inputsA vector of Ort::Value objects representing the input tensors.
outputNamesA vector of output tensor names.
outputsA vector of Ort::Value objects to be filled with the model's outputs.

Definition at line 53 of file ONNX.cc.

57{
58 m_session->Run(m_runOptions, inputNames.data(), inputs.data(), inputs.size(),
59 outputNames.data(), outputs.data(), outputs.size());
60}
Ort::RunOptions m_runOptions
Options to be passed to Ort::Session::Run.
Definition ONNX.h:360

Member Data Documentation

◆ m_env

Ort::Env m_env
private

Environment object for ONNX session.

Definition at line 345 of file ONNX.h.

◆ m_runOptions

Ort::RunOptions m_runOptions
private

Options to be passed to Ort::Session::Run.

Definition at line 360 of file ONNX.h.

◆ m_session

std::unique_ptr<Ort::Session> m_session
private

The ONNX inference session.

Definition at line 355 of file ONNX.h.

◆ m_sessionOptions

Ort::SessionOptions m_sessionOptions
private

ONNX session configuration.

Definition at line 350 of file ONNX.h.


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